Search in sources :

Example 1 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class Select method aclCheckFilterFields.

/**
   * This filters individual fields if the cannot be accessed by the reader.
   *
   * @param packet
   * @param records
   * @param reader
   * @param app
   * @return
   */
private static JSONArray aclCheckFilterFields(SelectRequestPacket packet, JSONArray records, String reader, GNSApplicationInterface<String> app) {
    for (int i = 0; i < records.length(); i++) {
        try {
            JSONObject record = records.getJSONObject(i);
            String guid = record.getString(NameRecord.NAME.getName());
            // Look at the keys in the values map
            JSONObject valuesMap = record.getJSONObject(NameRecord.VALUES_MAP.getName());
            Iterator<?> keys = valuesMap.keys();
            while (keys.hasNext()) {
                String field = (String) keys.next();
                if (!InternalField.isInternalField(field)) {
                    LOGGER.log(Level.FINE, "{0} Checking: {1}", new Object[] { app.getNodeID(), field });
                    ResponseCode responseCode = NSAuthentication.signatureAndACLCheck(null, guid, field, null, reader, null, null, MetaDataTypeName.READ_WHITELIST, app, true);
                    if (!responseCode.isOKResult()) {
                        LOGGER.log(Level.FINE, "{0} Removing: {1}", new Object[] { app.getNodeID(), field });
                        // removing the offending field
                        keys.remove();
                    }
                }
            }
        } catch (JSONException | InvalidKeyException | InvalidKeySpecException | SignatureException | NoSuchAlgorithmException | FailedDBOperationException | UnsupportedEncodingException e) {
            // ignore json errros
            LOGGER.log(Level.FINE, "{0} Problem getting guid from json: {1}", new Object[] { app.getNodeID(), e.getMessage() });
        }
    }
    return records;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 2 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class Select method aclCheckFilterForRecordsArray.

/**
   * This filters entire records if the query uses fields that cannot be accessed in the
   * returned record by the reader. Otherwise the user would be able to determine that
   * some GUIDS contain specific values for fields they can't access.
   *
   * @param packet
   * @param records
   * @param reader
   * @param app
   * @return
   */
private static JSONArray aclCheckFilterForRecordsArray(SelectRequestPacket packet, JSONArray records, String reader, GNSApplicationInterface<String> app) {
    JSONArray result = new JSONArray();
    for (int i = 0; i < records.length(); i++) {
        try {
            JSONObject record = records.getJSONObject(i);
            String guid = record.getString(NameRecord.NAME.getName());
            List<String> queryFields = getFieldsForQueryType(packet);
            ResponseCode responseCode = NSAuthentication.signatureAndACLCheck(null, guid, null, queryFields, reader, null, null, MetaDataTypeName.READ_WHITELIST, app, true);
            LOGGER.log(Level.FINE, "{0} ACL check for select: guid={0} queryFields={1} responsecode={2}", new Object[] { app.getNodeID(), guid, queryFields, responseCode });
            if (responseCode.isOKResult()) {
                result.put(record);
            }
        } catch (JSONException | InvalidKeyException | InvalidKeySpecException | SignatureException | NoSuchAlgorithmException | FailedDBOperationException | UnsupportedEncodingException e) {
            // ignore json errros
            LOGGER.log(Level.FINE, "{0} Problem getting guid from json: {1}", new Object[] { app.getNodeID(), e.getMessage() });
        }
    }
    return result;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 3 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class Select method handleSelectRequestFromNS.

/**
   * Handle a select request from the collecting NS. This is what other NSs do when they
   * get a SelectRequestPacket from the NS that originally received the packet (the one that is collecting
   * all the records).
   * This NS looks up the records and returns them.
   *
   * @param incomingJSON
   * @param app
   * @throws JSONException
   */
@SuppressWarnings("unchecked")
private static void handleSelectRequestFromNS(SelectRequestPacket request, GNSApplicationInterface<String> app) throws JSONException {
    LOGGER.log(Level.FINE, "NS {0} {1} received query {2}", new Object[] { Select.class.getSimpleName(), app.getNodeID(), request.getSummary() });
    try {
        // grab the records
        JSONArray jsonRecords = getJSONRecordsForSelect(request, app);
        jsonRecords = aclCheckFilterReturnedRecord(request, jsonRecords, request.getReader(), app);
        @SuppressWarnings("unchecked") SelectResponsePacket response = SelectResponsePacket.makeSuccessPacketForFullRecords(request.getId(), request.getClientAddress(), request.getCcpQueryId(), request.getNsQueryId(), app.getNodeAddress(), jsonRecords);
        LOGGER.log(Level.FINE, "NS {0} sending back {1} record(s) in response to {2}", new Object[] { app.getNodeID(), jsonRecords.length(), request.getSummary() });
        // and send them back to the originating NS
        app.sendToAddress(request.getNSReturnAddress(), response.toJSONObject());
    } catch (FailedDBOperationException | JSONException | IOException e) {
        LOGGER.log(Level.SEVERE, "Exception while handling select request: {0}", e);
        SelectResponsePacket failResponse = SelectResponsePacket.makeFailPacket(request.getId(), request.getClientAddress(), request.getNsQueryId(), app.getNodeAddress(), e.getMessage());
        try {
            app.sendToAddress(request.getNSReturnAddress(), failResponse.toJSONObject());
        } catch (IOException f) {
            LOGGER.log(Level.SEVERE, "Unable to send Failure SelectResponsePacket: {0}", f);
        }
    }
}
Also used : SelectResponsePacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 4 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class NoSQLRecordsThroughputTest method testlookupMultipleSystemAndUserFields.

private static void testlookupMultipleSystemAndUserFields(String node, String guid, String field) {
    //NoSQLRecords instance = new MongoRecords<String>(node);
    NoSQLRecords instance = new DiskMapRecords(node);
    GNSRecordMap<String> recordMap = new GNSRecordMap<String>(instance, COLLECTION_NAME);
    JSONObject json = new JSONObject();
    try {
        json.put(field, "some value");
    } catch (JSONException e) {
        System.out.println("Problem creating json " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    NameRecord nameRecord = new NameRecord(recordMap, guid, valuesMap);
    try {
        instance.insert(COLLECTION_NAME, guid, nameRecord.toJSONObject());
    } catch (JSONException e) {
        System.out.println("Problem writing json " + e);
    } catch (FailedDBOperationException e) {
        System.out.println("Problem adding " + json.toString() + " as value of " + guid + ": " + e);
    } catch (RecordExistsException e) {
        System.out.println(guid + " record already exists in database. Try something else." + e);
    }
    // and try to read it as fast as possible
    try {
        ArrayList<ColumnField> userFields = new ArrayList<>(Arrays.asList(new ColumnField(field, ColumnFieldType.USER_JSON)));
        int frequency = 10000;
        reset();
        do {
            Map<ColumnField, Object> map = instance.lookupSomeFields(COLLECTION_NAME, guid, NameRecord.NAME, NameRecord.VALUES_MAP, userFields);
            if (incrCount() % frequency == 0) {
                System.out.println(map);
                System.out.println(DelayProfiler.getStats());
                System.out.println("op/s = " + Format.formatTime(getCount() * 1000.0 / (System.currentTimeMillis() - initTime)));
                if (getCount() > frequency * 20) {
                    System.out.println("**********************resetting************************");
                    reset();
                }
            }
        } while (true);
    } catch (FailedDBOperationException | RecordNotFoundException e) {
        System.out.println("Lookup failed: " + e);
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) GNSRecordMap(edu.umass.cs.gnsserver.gnsapp.recordmap.GNSRecordMap) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Example 5 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class MongoRecords method doUpdate.

private void doUpdate(String collectionName, String guid, BasicDBObject updates) throws FailedDBOperationException {
    String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    DBCollection collection = db.getCollection(collectionName);
    BasicDBObject query = new BasicDBObject(primaryKey, guid);
    if (updates.keySet().size() > 0) {
        long startTime = System.currentTimeMillis();
        try {
            collection.update(query, new BasicDBObject("$set", updates));
        } catch (MongoException e) {
            DatabaseConfig.getLogger().log(Level.SEVERE, "{0} doUpdate failed: {1}", new Object[] { dbName, e.getMessage() });
            throw new FailedDBOperationException(collectionName, updates.toString(), "Original mongo exception:" + e.getMessage());
        }
        DelayProfiler.updateDelay("mongoSetUpdate", startTime);
        long finishTime = System.currentTimeMillis();
        if (finishTime - startTime > 10) {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} Long latency mongoUpdate {1}", new Object[] { dbName, (finishTime - startTime) });
        }
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Aggregations

FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)49 JSONException (org.json.JSONException)36 JSONObject (org.json.JSONObject)36 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)22 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)18 BasicDBObject (com.mongodb.BasicDBObject)13 MongoException (com.mongodb.MongoException)13 DBCollection (com.mongodb.DBCollection)12 DBObject (com.mongodb.DBObject)12 IOException (java.io.IOException)11 JSONArray (org.json.JSONArray)9 Test (org.junit.Test)9 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)7 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)7 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)7 DBCursor (com.mongodb.DBCursor)6 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)6 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)6 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)5 RandomString (edu.umass.cs.gnscommon.utils.RandomString)4