Search in sources :

Example 1 with DRPCInvocationsClient

use of org.apache.storm.drpc.DRPCInvocationsClient in project storm by apache.

the class DRPCServerTest method testGoodThrift.

@Test
public void testGoodThrift() throws Exception {
    int drpcPort = Utils.getAvailablePort();
    int invocationsPort = Utils.getAvailablePort(drpcPort + 1);
    Map<String, Object> conf = getConf(drpcPort, invocationsPort, null);
    try (DRPCServer server = new DRPCServer(conf)) {
        exec.submit(() -> {
            server.start();
            return null;
        });
        try (DRPCClient client = new DRPCClient(conf, "localhost", drpcPort);
            DRPCInvocationsClient invoke = new DRPCInvocationsClient(conf, "localhost", invocationsPort)) {
            Future<String> found = exec.submit(() -> client.getClient().execute("testing", "test"));
            DRPCRequest request = getNextAvailableRequest(invoke, "testing");
            assertNotNull(request);
            assertEquals("test", request.get_func_args());
            assertNotNull(request.get_request_id());
            invoke.result(request.get_request_id(), "tested");
            String result = found.get(1000, TimeUnit.MILLISECONDS);
            assertEquals("tested", result);
        }
    }
}
Also used : DRPCServer(org.apache.storm.daemon.drpc.DRPCServer) DRPCInvocationsClient(org.apache.storm.drpc.DRPCInvocationsClient) DRPCRequest(org.apache.storm.generated.DRPCRequest) DRPCClient(org.apache.storm.utils.DRPCClient) Test(org.junit.Test)

Example 2 with DRPCInvocationsClient

use of org.apache.storm.drpc.DRPCInvocationsClient in project storm by apache.

the class DRPCServerTest method testGoodHttpGet.

@Test
public void testGoodHttpGet() throws Exception {
    LOG.info("STARTING HTTP GET 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.result(request.get_request_id(), "tested");
            String result = found.get(1000, TimeUnit.MILLISECONDS);
            assertEquals("tested", result);
        }
    }
}
Also used : DRPCServer(org.apache.storm.daemon.drpc.DRPCServer) DRPCInvocationsClient(org.apache.storm.drpc.DRPCInvocationsClient) DRPCRequest(org.apache.storm.generated.DRPCRequest) Test(org.junit.Test)

Example 3 with DRPCInvocationsClient

use of org.apache.storm.drpc.DRPCInvocationsClient in project storm by apache.

the class DRPCServerTest method testFailedThrift.

@Test
public void testFailedThrift() throws Exception {
    int drpcPort = Utils.getAvailablePort();
    int invocationsPort = Utils.getAvailablePort(drpcPort + 1);
    Map<String, Object> conf = getConf(drpcPort, invocationsPort, null);
    try (DRPCServer server = new DRPCServer(conf)) {
        exec.submit(() -> {
            server.start();
            return null;
        });
        try (DRPCClient client = new DRPCClient(conf, "localhost", drpcPort);
            DRPCInvocationsClient invoke = new DRPCInvocationsClient(conf, "localhost", invocationsPort)) {
            Future<String> found = exec.submit(() -> client.getClient().execute("testing", "test"));
            DRPCRequest request = getNextAvailableRequest(invoke, "testing");
            assertNotNull(request);
            assertEquals("test", request.get_func_args());
            assertNotNull(request.get_request_id());
            invoke.failRequest(request.get_request_id());
            try {
                found.get(1000, 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("Request failed", ((DRPCExecutionException) t).get_msg());
            }
        }
    }
}
Also used : DRPCServer(org.apache.storm.daemon.drpc.DRPCServer) DRPCInvocationsClient(org.apache.storm.drpc.DRPCInvocationsClient) DRPCRequest(org.apache.storm.generated.DRPCRequest) DRPCExecutionException(org.apache.storm.generated.DRPCExecutionException) DRPCClient(org.apache.storm.utils.DRPCClient) ExecutionException(java.util.concurrent.ExecutionException) DRPCExecutionException(org.apache.storm.generated.DRPCExecutionException) Test(org.junit.Test)

Example 4 with DRPCInvocationsClient

use of org.apache.storm.drpc.DRPCInvocationsClient in project storm by apache.

the class ReturnResultsReducer method complete.

@Override
public void complete(ReturnResultsState state, TridentCollector collector) {
    // only one of the multireducers will receive the tuples
    if (state.returnInfo != null) {
        String result = JSONValue.toJSONString(state.results);
        Map retMap = null;
        try {
            retMap = (Map) JSONValue.parseWithException(state.returnInfo);
        } catch (ParseException e) {
            collector.reportError(e);
            return;
        }
        final String host = (String) retMap.get("host");
        final int port = Utils.getInt(retMap.get("port"));
        String id = (String) retMap.get("id");
        DistributedRPCInvocations.Iface client;
        if (local) {
            client = (DistributedRPCInvocations.Iface) ServiceRegistry.getService(host);
        } else {
            List server = new ArrayList() {

                {
                    add(host);
                    add(port);
                }
            };
            if (!_clients.containsKey(server)) {
                try {
                    _clients.put(server, new DRPCInvocationsClient(conf, host, port));
                } catch (TTransportException ex) {
                    throw new RuntimeException(ex);
                }
            }
            client = _clients.get(server);
        }
        try {
            client.result(id, result);
        } catch (AuthorizationException aze) {
            collector.reportError(aze);
        } catch (TException e) {
            collector.reportError(e);
        }
    }
}
Also used : TException(org.apache.thrift.TException) AuthorizationException(org.apache.storm.generated.AuthorizationException) ArrayList(java.util.ArrayList) DistributedRPCInvocations(org.apache.storm.generated.DistributedRPCInvocations) TTransportException(org.apache.thrift.transport.TTransportException) DRPCInvocationsClient(org.apache.storm.drpc.DRPCInvocationsClient) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(org.json.simple.parser.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with DRPCInvocationsClient

use of org.apache.storm.drpc.DRPCInvocationsClient 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)

Aggregations

DRPCInvocationsClient (org.apache.storm.drpc.DRPCInvocationsClient)5 DRPCServer (org.apache.storm.daemon.drpc.DRPCServer)4 DRPCRequest (org.apache.storm.generated.DRPCRequest)4 Test (org.junit.Test)4 ExecutionException (java.util.concurrent.ExecutionException)2 DRPCExecutionException (org.apache.storm.generated.DRPCExecutionException)2 DRPCClient (org.apache.storm.utils.DRPCClient)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AuthorizationException (org.apache.storm.generated.AuthorizationException)1 DistributedRPCInvocations (org.apache.storm.generated.DistributedRPCInvocations)1 TException (org.apache.thrift.TException)1 TTransportException (org.apache.thrift.transport.TTransportException)1 ParseException (org.json.simple.parser.ParseException)1