Search in sources :

Example 1 with SelectResponsePacket

use of edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket in project GNS by MobilityFirst.

the class Select method handledAllServersResponded.

// If all the servers have sent us a response we're done.
private static void handledAllServersResponded(InternalRequestHeader header, SelectResponsePacket packet, NSSelectInfo info, GNSApplicationInterface<String> replica) throws JSONException, ClientException, IOException, InternalRequestException {
    // must be done before the notify below
    // we're done processing this select query
    QUERIES_IN_PROGRESS.remove(packet.getNsQueryId());
    Set<JSONObject> allRecords = info.getResponsesAsSet();
    // Todo - clean up this use of guids further below in the group code
    Set<String> guids = extractGuidsFromRecords(allRecords);
    LOGGER.log(Level.FINE, "NS{0} guids:{1}", new Object[] { replica.getNodeID(), guids });
    SelectResponsePacket response;
    // If projection is null we return guids (old-style).
    if (info.getProjection() == null) {
        response = SelectResponsePacket.makeSuccessPacketForGuidsOnly(packet.getId(), null, -1, null, new JSONArray(guids));
    // Otherwise we return a list of records.
    } else {
        List<JSONObject> records = filterAndMassageRecords(allRecords);
        LOGGER.log(Level.FINE, "NS{0} record:{1}", new Object[] { replica.getNodeID(), records });
        response = SelectResponsePacket.makeSuccessPacketForFullRecords(packet.getId(), null, -1, -1, null, new JSONArray(records));
    }
    // Put the result where the coordinator can see it.
    QUERY_RESULT.put(packet.getNsQueryId(), response);
    // and let the coordinator know the value is there
    if (GNSApp.DELEGATE_CLIENT_MESSAGING) {
        synchronized (QUERIES_IN_PROGRESS) {
            QUERIES_IN_PROGRESS.notify();
        }
    }
    // Now we update any group guid stuff
    if (info.getGroupBehavior().equals(SelectGroupBehavior.GROUP_SETUP)) {
        LOGGER.log(Level.FINE, "NS{0} storing query string and other info", replica.getNodeID());
        // for setup we need to squirrel away the query for later lookups
        NSGroupAccess.updateQueryString(header, info.getGuid(), info.getQuery(), info.getProjection(), replica.getRequestHandler());
        NSGroupAccess.updateMinRefresh(header, info.getGuid(), info.getMinRefreshInterval(), replica.getRequestHandler());
    }
    if (info.getGroupBehavior().equals(SelectGroupBehavior.GROUP_SETUP) || info.getGroupBehavior().equals(SelectGroupBehavior.GROUP_LOOKUP)) {
        String guid = info.getGuid();
        LOGGER.log(Level.FINE, "NS{0} updating group members", replica.getNodeID());
        GroupAccess.addToGroup(header, guid, new ResultValue(guids), null, null, null, null, replica.getRequestHandler());
        //NSGroupAccess.updateMembers(header, guid, guids, replica.getRequestHandler());
        //NSGroupAccess.updateRecords(guid, processResponsesIntoJSONArray(info.getResponsesAsMap()), replica); 
        NSGroupAccess.updateLastUpdate(header, guid, new Date(), replica.getRequestHandler());
    }
}
Also used : JSONObject(org.json.JSONObject) SelectResponsePacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket) JSONArray(org.json.JSONArray) ResultValue(edu.umass.cs.gnsserver.utils.ResultValue) Date(java.util.Date)

Example 2 with SelectResponsePacket

use of edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket 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 3 with SelectResponsePacket

use of edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket 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;
}
Also used : Packet(edu.umass.cs.gnsserver.gnsapp.packet.Packet) InternalCommandPacket(edu.umass.cs.gnsserver.gnsapp.packet.InternalCommandPacket) CommandPacket(edu.umass.cs.gnscommon.packets.CommandPacket) ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) SelectRequestPacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket) SelectResponsePacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket) AdminCommandPacket(edu.umass.cs.gnscommon.packets.AdminCommandPacket) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) ReconfigurableRequest(edu.umass.cs.reconfiguration.interfaces.ReconfigurableRequest) Request(edu.umass.cs.gigapaxos.interfaces.Request) ClientRequest(edu.umass.cs.gigapaxos.interfaces.ClientRequest) JSONException(org.json.JSONException) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) PacketType(edu.umass.cs.gnsserver.gnsapp.packet.Packet.PacketType) ClientRequest(edu.umass.cs.gigapaxos.interfaces.ClientRequest) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) RequestIdentifier(edu.umass.cs.gigapaxos.interfaces.RequestIdentifier)

Example 4 with SelectResponsePacket

use of edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket in project GNS by MobilityFirst.

the class Select method getMySelectedRecords.

@SuppressWarnings("unchecked")
private static SelectResponsePacket getMySelectedRecords(SelectRequestPacket request, GNSApplicationInterface<String> app) {
    SelectResponsePacket response;
    try {
        // grab the records
        JSONArray jsonRecords = getJSONRecordsForSelect(request, app);
        jsonRecords = aclCheckFilterReturnedRecord(request, jsonRecords, request.getReader(), app);
        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 self-select request {2}", new Object[] { app.getNodeID(), jsonRecords.length(), request.getSummary() });
    } catch (FailedDBOperationException e) {
        LOGGER.log(Level.SEVERE, "Exception while handling self-select request: {0}", e.getMessage());
        //e.printStackTrace();
        response = SelectResponsePacket.makeFailPacket(request.getId(), request.getClientAddress(), request.getNsQueryId(), app.getNodeAddress(), e.getMessage());
    }
    return response;
}
Also used : SelectResponsePacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket) JSONArray(org.json.JSONArray) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Aggregations

SelectResponsePacket (edu.umass.cs.gnsserver.gnsapp.packet.SelectResponsePacket)4 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)3 JSONArray (org.json.JSONArray)3 IOException (java.io.IOException)2 JSONException (org.json.JSONException)2 ClientRequest (edu.umass.cs.gigapaxos.interfaces.ClientRequest)1 Request (edu.umass.cs.gigapaxos.interfaces.Request)1 RequestIdentifier (edu.umass.cs.gigapaxos.interfaces.RequestIdentifier)1 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)1 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)1 AdminCommandPacket (edu.umass.cs.gnscommon.packets.AdminCommandPacket)1 CommandPacket (edu.umass.cs.gnscommon.packets.CommandPacket)1 ResponsePacket (edu.umass.cs.gnscommon.packets.ResponsePacket)1 InternalCommandPacket (edu.umass.cs.gnsserver.gnsapp.packet.InternalCommandPacket)1 Packet (edu.umass.cs.gnsserver.gnsapp.packet.Packet)1 PacketType (edu.umass.cs.gnsserver.gnsapp.packet.Packet.PacketType)1 SelectRequestPacket (edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket)1 ResultValue (edu.umass.cs.gnsserver.utils.ResultValue)1 ReconfigurableRequest (edu.umass.cs.reconfiguration.interfaces.ReconfigurableRequest)1 Date (java.util.Date)1