Search in sources :

Example 6 with ValuesMap

use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.

the class ActiveHandler method main.

/***************** Test methods ****************/
/**
	 * @param args
	 * @throws JSONException 
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
public static void main(String[] args) throws JSONException, InterruptedException, ExecutionException {
    int numProcess = Integer.parseInt(args[0]);
    int numThread = Integer.parseInt(args[1]);
    boolean blocking = Boolean.parseBoolean(args[2]);
    if (numProcess <= 0) {
        System.out.println("Number of clients must be larger than 0.");
        System.exit(0);
    }
    final ThreadPoolExecutor executor;
    executor = new ThreadPoolExecutor(numProcess * numThread, numProcess * numThread, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    executor.prestartAllCoreThreads();
    String guid = "4B48F507395639FD806459281C3C09BCBB16FDFF";
    String field = "someField";
    String noop_code = "";
    try {
        noop_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/noop.js")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    ValuesMap value = new ValuesMap();
    value.put(field, "someValue");
    // initialize a handler
    ActiveHandler handler = new ActiveHandler("", null, numProcess, numThread, blocking);
    ArrayList<Future<JSONObject>> tasks = new ArrayList<Future<JSONObject>>();
    int n = 1000000;
    long t1 = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        tasks.add(executor.submit(new ActiveTask(clientPool[i % numProcess], guid, field, noop_code, value, 0)));
    }
    for (Future<JSONObject> task : tasks) {
        task.get();
    }
    long elapsed = System.currentTimeMillis() - t1;
    System.out.println("It takes " + elapsed + "ms, and the average latency for each operation is " + (elapsed * 1000.0 / n) + "us");
    System.out.println("The throughput is " + Util.df(n * 1000.0 / elapsed) + "reqs/sec");
    handler.shutdown();
    System.out.println(DelayProfiler.getStats());
    System.exit(0);
}
Also used : ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) JSONObject(org.json.JSONObject) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 7 with ValuesMap

use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.

the class ActiveBlockingClient method main.

/**
	 * Unit test
	 * 
	 * @param args
	 * @throws InterruptedException 
	 * @throws JSONException 
	 * @throws ActiveException 
	 */
public static void main(String[] args) throws InterruptedException, JSONException, ActiveException {
    final String suffix = "";
    String cfile = "/tmp/client" + suffix;
    String sfile = "/tmp/server" + suffix;
    final int numThread = 2;
    long executionTime = 1000;
    if (System.getProperty("executionTime") != null) {
        executionTime = Long.parseLong(System.getProperty("executionTime"));
    }
    String codeFile = "./scripts/activeCode/noop.js";
    if (System.getProperty("codeFile") != null) {
        codeFile = System.getProperty("codeFile");
    }
    ActiveBlockingClient client = new ActiveBlockingClient("", null, cfile, sfile, 0, numThread);
    String guid = "guid";
    String field = "name";
    String code = "";
    try {
        code = new String(Files.readAllBytes(Paths.get(codeFile)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    ValuesMap value = new ValuesMap();
    value.put(field, executionTime);
    JSONObject result = client.runCode(null, guid, field, code, value, 0, 1000);
    System.out.println(result);
    assertEquals(result.toString(), value.toString());
    int initial = client.getRecv();
    int n = 100000;
    long t1 = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        client.runCode(null, guid, field, code, value, 0, executionTime);
    }
    long elapsed = System.currentTimeMillis() - t1;
    System.out.println("It takes " + elapsed + "ms");
    System.out.println("The average time for each task is " + elapsed / n + "ms.");
    assert (client.getRecv() - initial == n) : "the number of responses is not the same as the number of requests";
    System.out.println("Sequential throughput test succeeds.");
    client.shutdown();
}
Also used : JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) IOException(java.io.IOException)

Example 8 with ValuesMap

use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.

the class FieldAccess method lookupMultipleValues.

/**
   * Reads the value of all the fields in a guid.
   * Doesn't return internal system fields.
   *
   * @param header
   * @param commandPacket
   * @param guid
   * @param reader
   * @param signature
   * @param message
   * @param handler
   * @param timestamp
   * @return a command response
   */
public static CommandResponse lookupMultipleValues(InternalRequestHeader header, CommandPacket commandPacket, String guid, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
    ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, GNSProtocol.ENTIRE_RECORD.toString(), //fields
    null, reader, signature, message, timestamp, handler.getApp());
    if (errorCode.isExceptionOrError()) {
        return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
    }
    String resultString;
    ResponseCode responseCode;
    try {
        ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldLocally(header, guid, GNSProtocol.ENTIRE_RECORD.toString(), handler.getApp());
        if (valuesMap != null) {
            resultString = valuesMap.removeInternalFields().toString();
            responseCode = ResponseCode.NO_ERROR;
        } else {
            resultString = GNSProtocol.BAD_RESPONSE.toString();
            responseCode = ResponseCode.BAD_GUID_ERROR;
        }
    } catch (FailedDBOperationException e) {
        resultString = GNSProtocol.BAD_RESPONSE.toString();
        responseCode = ResponseCode.DATABASE_OPERATION_ERROR;
    }
    return new CommandResponse(responseCode, resultString);
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 9 with ValuesMap

use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.

the class FieldAccess method lookupOneMultipleValues.

/**
   * Returns the first value of all the fields in a guid in an old-style JSONArray field value.
   *
   * @param header
   * @param commandPacket
   * @param guid
   * @param reader
   * @param signature
   * @param message
   * @param timestamp
   * @param handler
   * @return a command response
   */
public static CommandResponse lookupOneMultipleValues(InternalRequestHeader header, CommandPacket commandPacket, String guid, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
    ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, GNSProtocol.ENTIRE_RECORD.toString(), //fields
    null, reader, signature, message, timestamp, handler.getApp());
    if (errorCode.isExceptionOrError()) {
        return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
    }
    String resultString;
    ResponseCode responseCode;
    try {
        ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldLocally(null, guid, GNSProtocol.ENTIRE_RECORD.toString(), handler.getApp());
        if (valuesMap != null) {
            resultString = valuesMap.removeInternalFields().toJSONObjectFirstOnes().toString();
            responseCode = ResponseCode.NO_ERROR;
        } else {
            resultString = GNSProtocol.BAD_RESPONSE.toString();
            responseCode = ResponseCode.BAD_GUID_ERROR;
        }
    } catch (FailedDBOperationException e) {
        resultString = GNSProtocol.BAD_RESPONSE.toString();
        responseCode = ResponseCode.DATABASE_OPERATION_ERROR;
    } catch (JSONException e) {
        resultString = GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e.getMessage();
        responseCode = ResponseCode.JSON_PARSE_ERROR;
    }
    return new CommandResponse(responseCode, resultString);
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 10 with ValuesMap

use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.

the class AccountAccess method lookupAccountInfoFromGuid.

/**
   * Obtains the account info record for the given guid if that guid was used
   * to createField an account.
   * <p>
   * GUID: "ACCOUNT_INFO" -- {account} for primary guid<br>
   * GUID: "GUID" -- guid (primary) for secondary guid<br>
   * GUID: "GUID_INFO" -- {guid info}<br>
   * HRN: "GUID" -- GUID<br>
   * <p>
   * GUID = Globally Unique Identifier<br>
   * HRN = Human Readable Name<br>
   *
   * @param guid
   * @param handler
   * @param allowRemoteLookup
   * @return the account info record or null if it could not be found
   */
private static AccountInfo lookupAccountInfoFromGuid(InternalRequestHeader header, String guid, ClientRequestHandlerInterface handler, boolean allowRemoteLookup) {
    try {
        ValuesMap result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, guid, ACCOUNT_INFO, handler.getApp(), false);
        GNSConfig.getLogger().log(Level.FINE, "ValuesMap for {0} / {1}: {2}", new Object[] { guid, ACCOUNT_INFO, result != null ? result.getSummary() : result });
        if (result != null) {
            return new AccountInfo(new JSONObject(result.getString(ACCOUNT_INFO)));
        }
    } catch (FailedDBOperationException | JSONException | ParseException e) {
    // Do nothing as this is a normal result when the record doesn't
    // exist.
    }
    GNSConfig.getLogger().log(Level.FINE, "ACCOUNT_INFO NOT FOUND for {0}", guid);
    if (allowRemoteLookup) {
        GNSConfig.getLogger().log(Level.FINE, "LOOKING REMOTELY for ACCOUNT_INFO for {0}", guid);
        String value = null;
        try {
            value = handler.getInternalClient().execute(GNSCommandInternal.fieldRead(guid, ACCOUNT_INFO, header)).getResultString();
        } catch (IOException | JSONException | ClientException e) {
        } catch (InternalRequestException e) {
            //FIXME: This should do something other than print a stack trace
            e.printStackTrace();
        }
        // exist.
        if (value != null) {
            try {
                return new AccountInfo(new JSONObject(value));
            } catch (JSONException | ParseException e) {
            // Do nothing as this is a normal result when the record
            // doesn't exist.
            }
        }
    }
    return null;
}
Also used : InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) RandomString(edu.umass.cs.gnscommon.utils.RandomString) IOException(java.io.IOException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) JSONObject(org.json.JSONObject) ParseException(java.text.ParseException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Aggregations

ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)35 JSONException (org.json.JSONException)25 JSONObject (org.json.JSONObject)24 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)20 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)9 IOException (java.io.IOException)8 Test (org.junit.Test)8 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)7 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)6 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)5 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)5 RandomString (edu.umass.cs.gnscommon.utils.RandomString)4 ArrayList (java.util.ArrayList)4 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)3 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)3 HashMap (java.util.HashMap)3 JSONArray (org.json.JSONArray)3 ParseException (java.text.ParseException)2 Future (java.util.concurrent.Future)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2