Search in sources :

Example 21 with ValuesMap

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

the class ActiveNonBlockingRunner method main.

/**
	 * Test throughput with multithread worker with multiple script engine
	 * @param args
	 * @throws JSONException 
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
public static void main(String[] args) throws JSONException, InterruptedException, ExecutionException {
    int numThread = 10;
    final ActiveNonBlockingRunner[] runners = new ActiveNonBlockingRunner[numThread];
    for (int i = 0; i < numThread; i++) {
        runners[i] = new ActiveNonBlockingRunner(null, null);
    }
    final ThreadPoolExecutor executor = new ThreadPoolExecutor(numThread, numThread, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    executor.prestartAllCoreThreads();
    ArrayList<Future<String>> tasks = new ArrayList<Future<String>>();
    String guid = "zhaoyu";
    String field = "gao";
    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("string", "hello world");
    ActiveMessage msg = new ActiveMessage(guid, field, noop_code, value.toString(), 0, 500);
    int n = 1000000;
    long t1 = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        tasks.add(executor.submit(new SimpleTask(runners[0], msg)));
    }
    for (Future<String> 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 " + n * 1000.0 / elapsed);
    /**
		 * Test runner's protected method
		 */
    ActiveNonBlockingRunner runner = new ActiveNonBlockingRunner(null, null);
    String chain_code = null;
    try {
        //chain_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/permissionTest.js")));
        chain_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/permissionTest.js")));
    //chain_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/testLoad.js")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        runner.runCode(guid, field, chain_code, value.toString(), 0, 0);
        // fail here
        assert (false) : "The code should not be here";
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.exit(0);
}
Also used : ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JSONException(org.json.JSONException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 22 with ValuesMap

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

the class NameResolution method lookupGuidField.

/**
   * Lookup the field or fields in the guid.
   * Returns a JSONObject containing the fields and values
   * or null if the domainName doesn't exist.
   * @param addr 
   * @param id 
   * @param domain - the HRN of the guid
   * @param field - the field to lookup (mutually exclusive with fieldNames)
   * @param fields - the fields to lookup (mutually exclusive with fieldNames)
   * @param handler
   * @return a JSONObject containing the fields and values or null
   */
public static JSONObject lookupGuidField(String addr, int id, String domain, String field, ArrayList<String> fields, ClientRequestHandlerInterface handler) {
    /**
     * Querying multiple types together is allowed in DNS protocol, but practically not supported.
     * Therefore, no need for us to implement support for multi-type query.
     */
    /**
     * 1. Lookup guid for the domain name
     */
    String guid = null;
    final ValuesMap result;
    try {
        result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, domain, HRN_GUID, handler.getApp(), false);
        if (result != null) {
            guid = result.getString(HRN_GUID);
        }
    } catch (FailedDBOperationException | JSONException e) {
        NameResolution.getLogger().log(Level.FINE, "No guid for {0}: {1}", new Object[] { domain, e });
        return null;
    }
    /**
     * 2. Lookup the record
     */
    JSONObject value = null;
    if (guid != null) {
        // Generate a DNS header for local read 
        InternalRequestHeader header = new InternalRequestHeader() {

            @Override
            public long getOriginatingRequestID() {
                return id;
            }

            @Override
            public String getOriginatingGUID() {
                try {
                    return result.getString(HRN_GUID);
                } catch (JSONException e) {
                    return null;
                }
            }

            @Override
            public int getTTL() {
                return InternalRequestHeader.DEFAULT_TTL;
            }

            @Override
            public boolean hasBeenCoordinatedOnce() {
                // DNS request does not need coordination
                return false;
            }

            @Override
            public String getSourceAddress() {
                return addr;
            }
        };
        try {
            value = NSFieldAccess.lookupFieldsLocalNoAuth(header, guid, fields, ColumnFieldType.USER_JSON, handler);
        } catch (FailedDBOperationException e) {
            NameResolution.getLogger().log(Level.FINE, "Fetching record failed for {0}: {1}", new Object[] { domain, e });
        }
    } else {
        NameResolution.getLogger().log(Level.FINE, "No guid for {0} is found", new Object[] { domain });
    }
    return value;
}
Also used : JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) InternalRequestHeader(edu.umass.cs.gnsserver.interfaces.InternalRequestHeader) JSONObject(org.json.JSONObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 23 with ValuesMap

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

the class MongoRecords method lookupSomeFields.

@Override
public HashMap<ColumnField, Object> lookupSomeFields(String collectionName, String guid, ColumnField nameField, ColumnField valuesMapField, ArrayList<ColumnField> valuesMapKeys) throws RecordNotFoundException, FailedDBOperationException {
    if (guid == null) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} GUID is null: {1}", new Object[] { dbName, guid });
        throw new RecordNotFoundException(guid);
    }
    db.requestStart();
    try {
        String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject(primaryKey, guid);
        BasicDBObject projection = new BasicDBObject().append("_id", 0);
        if (valuesMapField != null && valuesMapKeys != null) {
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String fieldName = valuesMapField.getName() + "." + valuesMapKeys.get(i).getName();
                projection.append(fieldName, 1);
            }
        }
        DBObject dbObject = collection.findOne(query, projection);
        if (dbObject == null) {
            throw new RecordNotFoundException(guid);
        }
        HashMap<ColumnField, Object> hashMap = new HashMap<>();
        // put the name in the hashmap!! very important!!
        hashMap.put(nameField, guid);
        if (valuesMapField != null && valuesMapKeys != null) {
            // first we pull all the user values from the dbObject and put in a bson object
            // FIXME: Why not convert this to a JSONObject right now? We know that's what it is.
            BasicDBObject bson = (BasicDBObject) dbObject.get(valuesMapField.getName());
            DatabaseConfig.getLogger().log(Level.FINER, "{0} @@@@@@@@ {1}", new Object[] { dbName, bson });
            // then we run thru each userkey in the valuesMapKeys and pull the
            // value put stuffing it into the values map
            ValuesMap valuesMap = new ValuesMap();
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String userKey = valuesMapKeys.get(i).getName();
                if (containsFieldDotNotation(userKey, bson) == false) {
                    DatabaseConfig.getLogger().log(Level.FINE, "{0} DBObject doesn't contain {1}", new Object[] { dbName, userKey });
                    continue;
                }
                try {
                    switch(valuesMapKeys.get(i).type()) {
                        case USER_JSON:
                            Object value = getWithDotNotation(userKey, bson);
                            DatabaseConfig.getLogger().log(Level.FINE, "{0} Object is {1}", new Object[] { dbName, value.toString() });
                            valuesMap.put(userKey, value);
                            break;
                        case LIST_STRING:
                            valuesMap.putAsArray(userKey, JSONUtils.JSONArrayToResultValue(new JSONArray(getWithDotNotation(userKey, bson).toString())));
                            break;
                        default:
                            DatabaseConfig.getLogger().log(Level.SEVERE, "{0} ERROR: Error: User keys field {1} is not a known type: {2}", new Object[] { dbName, userKey, valuesMapKeys.get(i).type() });
                            break;
                    }
                } catch (JSONException e) {
                    DatabaseConfig.getLogger().log(Level.SEVERE, "{0} Error parsing json: {1}", new Object[] { dbName, e.getMessage() });
                    e.printStackTrace();
                }
            }
            hashMap.put(valuesMapField, valuesMap);
        }
        return hashMap;
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} lookupSomeFields failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
    } finally {
        db.requestDone();
    }
}
Also used : MongoException(com.mongodb.MongoException) HashMap(java.util.HashMap) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 24 with ValuesMap

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

the class DiskMapRecords method lookupSomeFields.

@Override
public HashMap<ColumnField, Object> lookupSomeFields(String collection, String name, ColumnField nameField, ColumnField valuesMapField, ArrayList<ColumnField> valuesMapKeys) throws RecordNotFoundException, FailedDBOperationException {
    JSONObject record = lookupEntireRecord(collection, name);
    //    LOGGER.log(Level.FINE, "Full record " + record.toString());
    HashMap<ColumnField, Object> hashMap = new HashMap<>();
    hashMap.put(nameField, name);
    if (valuesMapField != null && valuesMapKeys != null) {
        try {
            JSONObject readValuesMap = record.getJSONObject(valuesMapField.getName());
            //        LOGGER.log(Level.FINE, "Read valuesMap " + readValuesMap.toString());
            ValuesMap valuesMapOut = new ValuesMap();
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String userKey = valuesMapKeys.get(i).getName();
                if (JSONDotNotation.containsFieldDotNotation(userKey, readValuesMap) == false) {
                    //            LOGGER.fine("valuesMap doesn't contain " + userKey);
                    continue;
                }
                try {
                    switch(valuesMapKeys.get(i).type()) {
                        case USER_JSON:
                            Object value = JSONDotNotation.getWithDotNotation(userKey, readValuesMap);
                            LOGGER.log(Level.FINE, "Object is {0}", new Object[] { value.toString() });
                            valuesMapOut.put(userKey, value);
                            break;
                        case LIST_STRING:
                            valuesMapOut.putAsArray(userKey, JSONUtils.JSONArrayToResultValue(new JSONArray(JSONDotNotation.getWithDotNotation(userKey, readValuesMap).toString())));
                            break;
                        default:
                            LOGGER.log(Level.SEVERE, "ERROR: Error: User keys field {0} is not a known type:{1}", new Object[] { userKey, valuesMapKeys.get(i).type() });
                            break;
                    }
                } catch (JSONException e) {
                    LOGGER.log(Level.SEVERE, "Error parsing json: {0}", e.getMessage());
                }
            }
            hashMap.put(valuesMapField, valuesMapOut);
        } catch (JSONException e) {
            LOGGER.log(Level.SEVERE, "Problem getting values map: {0}", e.getMessage());
        }
    }
    return hashMap;
}
Also used : JSONObject(org.json.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 25 with ValuesMap

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

the class NSGroupAccess method lookupFieldInGroupGuid.

/**
   * Returns the values of a field in a set of guids contained in a group guid.
   * The result is return as an array of values.
   *
   * @param header
   * @param groupGuid
   * @param field
   * @param gnsApp
   * @return a ValuesMap containing the field with an array of the values
   * @throws FailedDBOperationException
   * @throws JSONException
   */
public static ValuesMap lookupFieldInGroupGuid(InternalRequestHeader header, String groupGuid, String field, GNSApplicationInterface<String> gnsApp) throws FailedDBOperationException, JSONException {
    JSONArray resultArray = new JSONArray();
    for (Object guidObject : lookupMembers(header, groupGuid, false, gnsApp.getRequestHandler())) {
        String guid = (String) guidObject;
        ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldAnywhere(header, guid, field, gnsApp);
        if (valuesMap != null && valuesMap.has(field)) {
            resultArray.put(valuesMap.get(field));
        }
    }
    ClientSupportConfig.getLogger().log(Level.FINE, "Group result for {0}/{1} = {2}", new Object[] { groupGuid, field, resultArray.toString() });
    ValuesMap result = new ValuesMap();
    result.put(field, resultArray);
    return result;
}
Also used : ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONArray(org.json.JSONArray)

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