Search in sources :

Example 6 with DRPCRequest

use of org.apache.storm.generated.DRPCRequest in project storm by apache.

the class DRPCSpout method nextTuple.

@Override
public void nextTuple() {
    boolean gotRequest = false;
    if (_local_drpc_id == null) {
        int size = 0;
        synchronized (_clients) {
            //This will only ever grow, so no need to worry about falling off the end
            size = _clients.size();
        }
        for (int i = 0; i < size; i++) {
            DRPCInvocationsClient client;
            synchronized (_clients) {
                client = _clients.get(i);
            }
            if (!client.isConnected()) {
                LOG.warn("DRPCInvocationsClient [{}:{}] is not connected.", client.getHost(), client.getPort());
                reconnectAsync(client);
                continue;
            }
            try {
                DRPCRequest req = client.fetchRequest(_function);
                if (req.get_request_id().length() > 0) {
                    Map returnInfo = new HashMap();
                    returnInfo.put("id", req.get_request_id());
                    returnInfo.put("host", client.getHost());
                    returnInfo.put("port", client.getPort());
                    gotRequest = true;
                    _collector.emit(new Values(req.get_func_args(), JSONValue.toJSONString(returnInfo)), new DRPCMessageId(req.get_request_id(), i));
                    break;
                }
            } catch (AuthorizationException aze) {
                reconnectAsync(client);
                LOG.error("Not authorized to fetch DRPC result from DRPC server", aze);
            } catch (TException e) {
                reconnectAsync(client);
                LOG.error("Failed to fetch DRPC result from DRPC server", e);
            } catch (Exception e) {
                LOG.error("Failed to fetch DRPC result from DRPC server", e);
            }
        }
        checkFutures();
    } else {
        DistributedRPCInvocations.Iface drpc = (DistributedRPCInvocations.Iface) ServiceRegistry.getService(_local_drpc_id);
        if (drpc != null) {
            // can happen during shutdown of drpc while topology is still up
            try {
                DRPCRequest req = drpc.fetchRequest(_function);
                if (req.get_request_id().length() > 0) {
                    Map returnInfo = new HashMap();
                    returnInfo.put("id", req.get_request_id());
                    returnInfo.put("host", _local_drpc_id);
                    returnInfo.put("port", 0);
                    gotRequest = true;
                    _collector.emit(new Values(req.get_func_args(), JSONValue.toJSONString(returnInfo)), new DRPCMessageId(req.get_request_id(), 0));
                }
            } catch (AuthorizationException aze) {
                throw new RuntimeException(aze);
            } catch (TException e) {
                throw new RuntimeException(e);
            }
        }
    }
    if (!gotRequest) {
        Utils.sleep(1);
    }
}
Also used : TException(org.apache.thrift.TException) HashMap(java.util.HashMap) AuthorizationException(org.apache.storm.generated.AuthorizationException) Values(org.apache.storm.tuple.Values) DistributedRPCInvocations(org.apache.storm.generated.DistributedRPCInvocations) TException(org.apache.thrift.TException) AuthorizationException(org.apache.storm.generated.AuthorizationException) DRPCRequest(org.apache.storm.generated.DRPCRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with DRPCRequest

use of org.apache.storm.generated.DRPCRequest in project storm by apache.

the class DRPCServerTest method testFailedHttpGet.

@Test
public void testFailedHttpGet() throws Exception {
    LOG.info("STARTING HTTP GET (FAIL) TEST...");
    int drpcPort = Utils.getAvailablePort();
    int invocationsPort = Utils.getAvailablePort(drpcPort + 1);
    int httpPort = Utils.getAvailablePort(invocationsPort + 1);
    Map<String, Object> conf = getConf(drpcPort, invocationsPort, httpPort);
    try (DRPCServer server = new DRPCServer(conf)) {
        exec.submit(() -> {
            server.start();
            return null;
        });
        //TODO need a better way to do this
        Thread.sleep(2000);
        try (DRPCInvocationsClient invoke = new DRPCInvocationsClient(conf, "localhost", invocationsPort)) {
            Future<String> found = exec.submit(() -> GET(httpPort, "testing", "test"));
            DRPCRequest request = getNextAvailableRequest(invoke, "testing");
            assertNotNull(request);
            assertEquals("test", request.get_func_args());
            assertNotNull(request.get_request_id());
            invoke.getClient().failRequest(request.get_request_id());
            try {
                found.get(1000, TimeUnit.MILLISECONDS);
                fail("exec did not throw an exception");
            } catch (ExecutionException e) {
                LOG.warn("Got Expected Exception", e);
            //Getting the exact response code is a bit more complex.
            //TODO should use a better client
            }
        }
    }
}
Also used : DRPCServer(org.apache.storm.daemon.drpc.DRPCServer) DRPCInvocationsClient(org.apache.storm.drpc.DRPCInvocationsClient) DRPCRequest(org.apache.storm.generated.DRPCRequest) ExecutionException(java.util.concurrent.ExecutionException) DRPCExecutionException(org.apache.storm.generated.DRPCExecutionException) Test(org.junit.Test)

Example 8 with DRPCRequest

use of org.apache.storm.generated.DRPCRequest in project storm by apache.

the class DRPCServerTest method getNextAvailableRequest.

private static DRPCRequest getNextAvailableRequest(DRPCInvocationsClient invoke, String func) throws Exception {
    DRPCRequest request = null;
    long timedout = System.currentTimeMillis() + 5_000;
    while (System.currentTimeMillis() < timedout) {
        request = invoke.getClient().fetchRequest(func);
        if (request != null && request.get_request_id() != null && !request.get_request_id().isEmpty()) {
            return request;
        }
        Thread.sleep(1);
    }
    fail("Test timed out waiting for a request on " + func);
    return request;
}
Also used : DRPCRequest(org.apache.storm.generated.DRPCRequest)

Example 9 with DRPCRequest

use of org.apache.storm.generated.DRPCRequest in project storm by apache.

the class DRPC method execute.

public <T extends OutstandingRequest> T execute(String functionName, String funcArgs, RequestFactory<T> factory) throws AuthorizationException {
    meterExecuteCalls.mark();
    checkAuthorization("execute", functionName);
    String id = nextId();
    LOG.debug("Execute {} {}", functionName, funcArgs);
    T req = factory.mkRequest(functionName, new DRPCRequest(funcArgs, id));
    _requests.put(id, req);
    ConcurrentLinkedQueue<OutstandingRequest> q = getQueue(functionName);
    q.add(req);
    return req;
}
Also used : DRPCRequest(org.apache.storm.generated.DRPCRequest)

Example 10 with DRPCRequest

use of org.apache.storm.generated.DRPCRequest in project storm by apache.

the class DRPCTest method testFailedBlocking.

@Test
public void testFailedBlocking() throws Exception {
    try (DRPC server = new DRPC(null, 100)) {
        Future<String> found = exec.submit(() -> server.executeBlocking("testing", "test"));
        DRPCRequest request = getNextAvailableRequest(server, "testing");
        assertNotNull(request);
        assertEquals("test", request.get_func_args());
        assertNotNull(request.get_request_id());
        server.failRequest(request.get_request_id(), null);
        try {
            found.get(100, TimeUnit.MILLISECONDS);
            fail("exec did not throw an exception");
        } catch (ExecutionException e) {
            Throwable t = e.getCause();
            assertEquals(t.getClass(), DRPCExecutionException.class);
            //Don't know a better way to validate that it failed.
            assertEquals(DRPCExceptionType.FAILED_REQUEST, ((DRPCExecutionException) t).get_type());
        }
    }
}
Also used : DRPCRequest(org.apache.storm.generated.DRPCRequest) DRPCExecutionException(org.apache.storm.generated.DRPCExecutionException) ExecutionException(java.util.concurrent.ExecutionException) DRPCExecutionException(org.apache.storm.generated.DRPCExecutionException) Test(org.junit.Test)

Aggregations

DRPCRequest (org.apache.storm.generated.DRPCRequest)12 Test (org.junit.Test)7 DRPCServer (org.apache.storm.daemon.drpc.DRPCServer)4 DRPCInvocationsClient (org.apache.storm.drpc.DRPCInvocationsClient)4 DRPCExecutionException (org.apache.storm.generated.DRPCExecutionException)4 ExecutionException (java.util.concurrent.ExecutionException)3 DRPCClient (org.apache.storm.utils.DRPCClient)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AuthorizationException (org.apache.storm.generated.AuthorizationException)1 DistributedRPCInvocations (org.apache.storm.generated.DistributedRPCInvocations)1 Values (org.apache.storm.tuple.Values)1 TException (org.apache.thrift.TException)1