Search in sources :

Example 11 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage in project GNS by MobilityFirst.

the class ActiveBlockingClient method runCode.

/**
	 * This runCode method sends the request to worker, and
	 * wait for worker to finish the request. If the worker
	 * crashed during the request execution, this method
	 * will resend the request to a new created worker, and
	 * the new worker will execute this request again. 
	 * <p>If the worker fails to execute the request, it will 
	 * send back an error to inform this method that the execution
	 * gets accomplished with an error. This method will raise
	 * an ActiveException, and the method which calls this method
	 * needs to handle this exception.
	 * 
	 * @param guid
	 * @param accessor
	 * @param code
	 * @param value
	 * @param ttl
	 * @return executed result sent back from worker
         * @throws edu.umass.cs.gnsserver.activecode.prototype.ActiveException
	 */
@Override
public synchronized JSONObject runCode(InternalRequestHeader header, String guid, String accessor, String code, JSONObject value, int ttl, long budget) throws ActiveException {
    ActiveMessage msg = new ActiveMessage(guid, accessor, code, value.toString(), ttl, budget);
    sendMessage(msg);
    ActiveMessage response = null;
    while (true) {
        try {
            response = (ActiveMessage) channel.receiveMessage();
        } catch (IOException e) {
        // do nothing, as the worker is crashed, the response is null
        }
        if (response == null) {
            /**
				 *  The worker is crashed, restart the
				 *  worker.
				 */
            if (!isRestarting.getAndSet(true)) {
                this.shutdown();
                this.initializeChannelAndStartWorker();
                isRestarting.set(false);
            }
            break;
        } else if (response.type != Type.RESPONSE) {
            ActiveCodeHandler.getLogger().log(Level.FINE, "receive a query from worker:{0}", new Object[] { response });
            ActiveMessage result = queryHandler.handleQuery(response, header);
            sendMessage(result);
            ActiveCodeHandler.getLogger().log(Level.FINE, "send a response to worker:{0} for the query: {1}", new Object[] { result, response });
        } else {
            assert (response.type == Type.RESPONSE) : "The message type is not RESPONSE";
            break;
        }
    }
    ActiveCodeHandler.getLogger().log(Level.FINE, "receive a response from the worker:{0}", new Object[] { response });
    if (response == null) {
        throw new ActiveException("Worker crashed!");
    }
    if (response.getError() != null) {
        throw new ActiveException(msg.toString());
    }
    counter.getAndIncrement();
    try {
        return new JSONObject(response.getValue());
    } catch (JSONException e) {
        return value;
    }
}
Also used : JSONObject(org.json.JSONObject) ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 12 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage in project GNS by MobilityFirst.

the class ActiveBlockingQuerier method writeValueIntoField.

private void writeValueIntoField(String querierGuid, String targetGuid, String value, int ttl) throws ActiveException {
    ActiveMessage am = new ActiveMessage(ttl, querierGuid, null, targetGuid, value, currentID);
    try {
        channel.sendMessage(am);
        ActiveMessage response = (ActiveMessage) channel.receiveMessage();
        if (response == null) {
            throw new ActiveException();
        }
        if (response.getError() != null) {
            throw new ActiveException();
        }
    } catch (IOException e) {
        throw new ActiveException();
    }
}
Also used : ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 13 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage 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)

Aggregations

ActiveMessage (edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)13 IOException (java.io.IOException)9 ActiveException (edu.umass.cs.gnsserver.activecode.prototype.ActiveException)6 JSONException (org.json.JSONException)6 ExecutionException (java.util.concurrent.ExecutionException)4 JSONObject (org.json.JSONObject)3 ArrayList (java.util.ArrayList)2 Future (java.util.concurrent.Future)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 TimeoutException (java.util.concurrent.TimeoutException)2 ScriptException (javax.script.ScriptException)2 Message (edu.umass.cs.gnsserver.activecode.prototype.interfaces.Message)1 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)1 DatagramPacket (java.net.DatagramPacket)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1