Search in sources :

Example 1 with ActiveReplicaError

use of edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError in project GNS by MobilityFirst.

the class AbstractGNSClient method desktopSendCommmandAndWait.

// arun: changed this to return CommandValueReturnPacket
private ResponsePacket desktopSendCommmandAndWait(JSONObject command) throws IOException {
    Object myMonitor = new Object();
    long id;
    monitorMap.put(id = this.generateNextRequestID(), myMonitor);
    CommandPacket commandPacket = desktopSendCommmandNoWait(command, id);
    // now we wait until the correct packet comes back
    try {
        GNSClientConfig.getLogger().log(Level.FINE, "{0} waiting for query {1}", new Object[] { this, id + "" });
        long monitorStartTime = System.currentTimeMillis();
        if (!USE_GLOBAL_MONITOR) {
            synchronized (myMonitor) {
                while (monitorMap.containsKey(id) && (readTimeout == 0 || System.currentTimeMillis() - monitorStartTime < readTimeout)) {
                    myMonitor.wait(readTimeout);
                }
            }
        } else {
            synchronized (monitor) {
                while (!resultMap.containsKey(id) && (readTimeout == 0 || System.currentTimeMillis() - monitorStartTime < readTimeout)) {
                    monitor.wait(readTimeout);
                }
            }
        }
        if (readTimeout != 0 && System.currentTimeMillis() - monitorStartTime >= readTimeout) {
            return getTimeoutResponse(this, commandPacket);
        }
        GNSClientConfig.getLogger().log(Level.FINE, "Response received for query {0}", new Object[] { id + "" });
    } catch (InterruptedException x) {
        GNSClientConfig.getLogger().severe("Wait for return packet was interrupted " + x);
    }
    //CommandResult 
    Request result = resultMap.remove(id);
    GNSClientConfig.getLogger().log(Level.FINE, "{0} received response {1} ", new Object[] { this, result.getSummary() });
    return result instanceof ResponsePacket ? ((ResponsePacket) result) : new ResponsePacket(result.getServiceName(), id, ResponseCode.ACTIVE_REPLICA_EXCEPTION, ((ActiveReplicaError) result).getResponseMessage());
}
Also used : ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) Request(edu.umass.cs.gigapaxos.interfaces.Request) JSONObject(org.json.JSONObject) ActiveReplicaError(edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError) CommandPacket(edu.umass.cs.gnscommon.packets.CommandPacket)

Example 2 with ActiveReplicaError

use of edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError in project GNS by MobilityFirst.

the class AbstractGNSClient method handleCommandValueReturnPacket.

/**
   * arun: Handles both command return values and active replica error
   * messages.
   *
   * @param response
   * @param receivedTime
   * @throws JSONException
   */
private void handleCommandValueReturnPacket(Request response) {
    long methodStartTime = System.currentTimeMillis();
    ResponsePacket packet = response instanceof ResponsePacket ? (ResponsePacket) response : null;
    ActiveReplicaError error = response instanceof ActiveReplicaError ? (ActiveReplicaError) response : null;
    assert (packet != null || error != null);
    long id = packet != null ? packet.getClientRequestId() : error.getRequestID();
    GNSClientConfig.getLogger().log(Level.FINE, "{0} received response {1}:{2} from {3}", new Object[] { this, id + "", response.getSummary(), packet != null ? //packet.getResponder() 
    "unknown" : error.getSender() });
    // store the response away
    if (packet != null) {
        resultMap.put(id, packet);
    } else {
        resultMap.put(id, error);
    }
    // differentiates between synchronusly and asynchronusly sent
    if (!pendingAsynchPackets.containsKey(id)) {
        Object myMonitor = monitorMap.remove(id);
        assert (myMonitor != null) : Util.suicide("No monitor entry found for request " + id);
        if (!USE_GLOBAL_MONITOR && myMonitor != null) {
            synchronized (myMonitor) {
                myMonitor.notify();
            }
        }
        /* for synchronous sends we notify waiting threads.
       * arun: Needed now only for Android if at all.
       */
        synchronized (monitor) {
            monitor.notifyAll();
        }
    } else {
        // Handle the asynchronus packets
        // note that we have recieved the reponse
        pendingAsynchPackets.remove(id);
    }
    DelayProfiler.updateDelay("handleCommandValueReturnPacket", methodStartTime);
}
Also used : ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) ActiveReplicaError(edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError) JSONObject(org.json.JSONObject)

Example 3 with ActiveReplicaError

use of edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError in project GNS by MobilityFirst.

the class LNSPacketDemultiplexer method handleCommandReturnValuePacket.

/**
   * Handles sending the results of a command packet back to the client. Passing
   * json as well for legacy reasons and to avoid an unnecessary toJSON call.
   *
   * @throws JSONException
   * @throws IOException
   */
private void handleCommandReturnValuePacket(Request response, JSONObject json) throws JSONException, IOException {
    ResponsePacket returnPacket = response instanceof ResponsePacket ? (ResponsePacket) response : null;
    ActiveReplicaError error = response instanceof ActiveReplicaError ? (ActiveReplicaError) response : null;
    GNSConfig.getLogger().log(Level.INFO, "{0} received response {1}", new Object[] { this, returnPacket != null ? returnPacket : error.getSummary() });
    assert (returnPacket != null || error != null);
    long id = returnPacket != null ? returnPacket.getRequestID() : error.getRequestID();
    String serviceName = returnPacket != null ? returnPacket.getServiceName() : error.getServiceName();
    LNSRequestInfo sentInfo;
    GNSConfig.getLogger().log(Level.INFO, "{0} matching {1} with {2}", new Object[] { this, id + "", handler.getRequestInfo(id) });
    if ((sentInfo = handler.getRequestInfo(id)) != null) {
        // doublecheck that it is for the same service name
        if ((sentInfo.getServiceName().equals(serviceName) || // arun: except when service name is special name
        (sentInfo.getServiceName().equals(Config.getGlobalString(RC.SPECIAL_NAME))))) {
            // String serviceName = returnPacket.getServiceName();
            GNSConfig.getLogger().log(Level.INFO, "{0} about to remove {1}", new Object[] { this, id + "" });
            handler.removeRequestInfo(id);
            // REQUEST
            if (!CommandPacket.BOGUS_SERVICE_NAME.equals(serviceName) && sentInfo.getCommandType().isRead() && //&& sentInfo.getCommandName().equals(GNSCommandProtocol.READ)
            returnPacket != null) {
                handler.updateCacheEntry(serviceName, returnPacket.getReturnValue());
            }
            // send the response back
            GNSConfig.getLogger().log(Level.FINE, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< LNS IS SENDING VALUE BACK TO {0}: {1}", new Object[] { sentInfo.getHost() + ":" + sentInfo.getPort(), returnPacket != null ? returnPacket.getSummary() : error.getSummary() });
            handler.sendToClient(new InetSocketAddress(sentInfo.getHost(), sentInfo.getPort()), json != null ? json : returnPacket != null ? returnPacket.toJSONObject() : error.toJSONObject());
        } else {
            GNSConfig.getLogger().log(Level.SEVERE, "Command response packet mismatch: {0} vs. {1}", new Object[] { sentInfo.getServiceName(), returnPacket.getServiceName() });
        }
    } else {
        GNSConfig.getLogger().log(Level.FINE, "Duplicate response for {0}: {1}", new Object[] { id, json != null ? json : returnPacket != null ? returnPacket.toJSONObject() : error.toJSONObject() });
    }
}
Also used : ResponsePacket(edu.umass.cs.gnscommon.packets.ResponsePacket) InetSocketAddress(java.net.InetSocketAddress) ActiveReplicaError(edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError)

Aggregations

ResponsePacket (edu.umass.cs.gnscommon.packets.ResponsePacket)3 ActiveReplicaError (edu.umass.cs.reconfiguration.reconfigurationpackets.ActiveReplicaError)3 JSONObject (org.json.JSONObject)2 Request (edu.umass.cs.gigapaxos.interfaces.Request)1 CommandPacket (edu.umass.cs.gnscommon.packets.CommandPacket)1 InetSocketAddress (java.net.InetSocketAddress)1