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);
}
}
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) });
}
}
}
use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class MongoRecords method selectRecords.
private MongoRecordCursor selectRecords(String collectionName, ColumnField valuesMapField, String key, Object value, boolean explain) throws FailedDBOperationException {
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
// note that if the value of the key in the database is a list (which it is) this
// query will find all records where the value (a list) *contains* an element whose value is the value
//
//FROM MONGO DOC: Match an Array Element
//Equality matches can specify a single element in the array to match. These specifications match
//if the array contains at least one element with the specified value.
//In the following example, the query matches all documents where the value of the field tags is
//an array that contains 'fruit' as one of its elements:
//db.inventory.find( { tags: 'fruit' } )
String fieldName = valuesMapField.getName() + "." + key;
BasicDBObject query = new BasicDBObject(fieldName, value);
//System.out.println("***GNSProtocol.QUERY.toString()***: " + query.toString());
DBCursor cursor = null;
try {
cursor = collection.find(query);
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecords failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, fieldName, "Original mongo exception:" + e.getMessage());
}
if (explain) {
System.out.println(cursor.explain().toString());
}
return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class AppAdminServer method run.
/**
* Start executing the thread.
*/
@Override
public void run() {
int numRequest = 0;
GNSConfig.getLogger().log(Level.INFO, "NS Node {0} starting Admin Request Server on port {1}", new Object[] { app.getNodeID(), serverSocket.getLocalPort() });
while (true) {
try {
//Read the packet from the input stream
try (Socket socket = serverSocket.accept()) {
//Read the packet from the input stream
JSONObject incomingJSON = Packet.getJSONObjectFrame(socket);
switch(Packet.getPacketType(incomingJSON)) {
case DUMP_REQUEST:
DumpRequestPacket<String> dumpRequestPacket = new DumpRequestPacket<>(incomingJSON, gnsNodeConfig);
dumpRequestPacket.setPrimaryNameServer(app.getNodeID());
JSONArray jsonArray = new JSONArray();
// if there is an argument it is a TAGNAME we return all the records that have that tag
if (dumpRequestPacket.getArgument() != null) {
String tag = dumpRequestPacket.getArgument();
AbstractRecordCursor cursor = NameRecord.getAllRowsIterator(app.getDB());
while (cursor.hasNext()) {
NameRecord nameRecord = null;
JSONObject json = cursor.nextJSONObject();
try {
nameRecord = new NameRecord(app.getDB(), json);
} catch (JSONException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing json into NameRecord: {0} JSON is {1}", new Object[] { e, json.toString() });
}
if (nameRecord != null) {
try {
if (nameRecord.containsUserKey(AccountAccess.GUID_INFO)) {
GuidInfo userInfo = new GuidInfo(nameRecord.getValuesMap().getJSONObject(AccountAccess.GUID_INFO));
//GuidInfo userInfo = new GuidInfo(nameRecord.getUserKeyAsArray(AccountAccess.GUID_INFO).toResultValueString());
if (userInfo.containsTag(tag)) {
jsonArray.put(nameRecord.toJSONObject());
}
}
} catch (FieldNotFoundException e) {
GNSConfig.getLogger().log(Level.SEVERE, "FieldNotFoundException. Field Name = {0}", e.getMessage());
//To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
}
}
}
// OTHERWISE WE RETURN ALL THE RECORD
} else {
//for (NameRecord nameRecord : NameServer.getAllNameRecords()) {
AbstractRecordCursor cursor = NameRecord.getAllRowsIterator(app.getDB());
while (cursor.hasNext()) {
NameRecord nameRecord = null;
JSONObject json = cursor.nextJSONObject();
try {
nameRecord = new NameRecord(app.getDB(), json);
} catch (JSONException e) {
GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing record cursor into NameRecord: {0} JSON is {1}", new Object[] { e, json.toString() });
}
if (nameRecord != null) {
jsonArray.put(nameRecord.toJSONObject());
}
}
}
GNSConfig.getLogger().log(Level.FINER, "AppAdmin for {0} is {1}", new Object[] { app.getNodeID(), jsonArray.toString() });
dumpRequestPacket.setJsonArray(jsonArray);
Packet.sendTCPPacket(dumpRequestPacket.toJSONObject(), dumpRequestPacket.getReturnAddress());
GNSConfig.getLogger().log(Level.FINEST, "AppAdmin: Response to id:{0} --> {1}", new Object[] { dumpRequestPacket.getId(), dumpRequestPacket.toString() });
break;
case ADMIN_REQUEST:
AdminRequestPacket adminRequestPacket = new AdminRequestPacket(incomingJSON);
switch(adminRequestPacket.getOperation()) {
case CLEARCACHE:
GNSConfig.getLogger().log(Level.WARNING, "NSListenerAdmin ({0}) : Ignoring CLEARCACHE request", app.getNodeID());
break;
case DUMPCACHE:
GNSConfig.getLogger().log(Level.WARNING, "NSListenerAdmin ({0}) : Ignoring DUMPCACHE request", app.getNodeID());
break;
}
break;
}
}
} catch (IOException | JSONException | FailedDBOperationException | ParseException | IllegalArgumentException | SecurityException e) {
if (serverSocket.isClosed()) {
GNSConfig.getLogger().warning("NS Admin shutting down.");
// close this thread
return;
}
e.printStackTrace();
}
}
}
use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class GNSApp method execute.
/**
*
* @param request
* @param doNotReplyToClient
* @return true if the command is successfully executed
*/
@SuppressWarnings("unchecked")
// we explicitly check type
@Override
public boolean execute(Request request, boolean doNotReplyToClient) {
boolean executed = false;
if (executeNoop(request)) {
return true;
}
try {
Packet.PacketType packetType = request.getRequestType() instanceof Packet.PacketType ? (Packet.PacketType) request.getRequestType() : null;
GNSConfig.getLogger().log(Level.FINE, "{0} starting execute({1}) doNotReplyToClient={2}", new Object[] { this, request.getSummary(), doNotReplyToClient });
Request prev = null;
// arun: enqueue request, dequeue before returning
if (request instanceof RequestIdentifier) {
if (enqueueCommand()) {
prev = this.outstanding.putIfAbsent(((RequestIdentifier) request).getRequestID(), request);
}
} else {
assert (false) : this + " should not be getting requests that do not implement " + RequestIdentifier.class;
}
switch(packetType) {
case SELECT_REQUEST:
Select.handleSelectRequest((SelectRequestPacket) request, this);
break;
case SELECT_RESPONSE:
Select.handleSelectResponse((SelectResponsePacket) request, this);
break;
case COMMAND:
CommandHandler.handleCommandPacket((CommandPacket) request, doNotReplyToClient, this);
break;
case ADMIN_COMMAND:
CommandHandler.handleCommandPacket((AdminCommandPacket) request, doNotReplyToClient, this);
break;
default:
assert (false) : (this + " should not be getting packets of type " + packetType + "; exiting");
GNSConfig.getLogger().log(Level.SEVERE, " Packet type not found: {0}", request.getSummary());
return false;
}
executed = true;
// arun: always clean up all created state upon exiting
if (request instanceof RequestIdentifier && prev == null) {
GNSConfig.getLogger().log(Level.FINE, "{0} finished execute({1}) -> {2}", new Object[] { this, request.getSummary(), request instanceof ClientRequest && ((ClientRequest) request).getResponse() != null ? ((ClientRequest) request).getResponse().getSummary() : null });
this.outstanding.remove(((RequestIdentifier) request).getRequestID());
}
} catch (JSONException | IOException | ClientException | InternalRequestException e) {
e.printStackTrace();
} catch (FailedDBOperationException e) {
// all database operations throw this exception, therefore we keep
// throwing this exception upwards and catch this
// here.
// A database operation error would imply that the application
// hasn't been able to successfully execute
// the request. therefore, this method returns 'false', hoping that
// whoever calls handleDecision would retry
// the request.
GNSConfig.getLogger().log(Level.SEVERE, "Error handling request: {0}", request.toString());
e.printStackTrace();
}
return executed;
}
Aggregations