Search in sources :

Example 1 with AsyncMethodCallback

use of org.apache.thrift.async.AsyncMethodCallback in project commons by twitter.

the class ThriftTest method testAsyncRetrySelection.

@Test
public void testAsyncRetrySelection() throws Exception {
    // verify subclasses pass the retry filter
    class HopelesslyLost extends NotFoundException {
    }
    Capture<AsyncMethodCallback<Integer>> callbackCapture1 = new Capture<AsyncMethodCallback<Integer>>();
    expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture1));
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
    Capture<AsyncMethodCallback<Integer>> callbackCapture2 = new Capture<AsyncMethodCallback<Integer>>();
    expectAsyncServiceRetry(true).calculateMass(eq("jake"), capture(callbackCapture2));
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
    // Verifies that our callback was called.
    TTransportException nonRetryableException = new TTransportException();
    callback.onError(nonRetryableException);
    Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
    control.replay();
    TestServiceAsync testService = thrift.builder().withRetries(2).retryOn(NotFoundException.class).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create();
    testService.calculateMass("jake", callback);
    callbackCapture1.getValue().onError(new HopelesslyLost());
    callbackCapture2.getValue().onError(nonRetryableException);
    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 1);
    assertReconnectsTotal(thrift, 1);
    assertTimeoutsTotal(thrift, 0);
    control.verify();
}
Also used : AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) TTransportException(org.apache.thrift.transport.TTransportException) Capture(org.easymock.Capture) Test(org.junit.Test)

Example 2 with AsyncMethodCallback

use of org.apache.thrift.async.AsyncMethodCallback in project commons by twitter.

the class ThriftTest method testAsyncRetriesRecover.

@Test
public void testAsyncRetriesRecover() throws Exception {
    // Capture the callback that Thift has wrapped around our callback.
    Capture<AsyncMethodCallback<Integer>> callbackCapture = new Capture<AsyncMethodCallback<Integer>>();
    // 1st call
    expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture));
    expectLastCall().andThrow(new TTransportException());
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
    // 1st retry recovers
    expectAsyncServiceRetry(false).calculateMass(eq("jake"), capture(callbackCapture));
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.SUCCESS), anyLong());
    // Verifies that our callback was called.
    callback.onComplete(42);
    Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
    control.replay();
    thrift.builder().withRetries(1).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create().calculateMass("jake", callback);
    // Mimicks the async response from the server.
    callbackCapture.getValue().onComplete(42);
    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 0);
    assertReconnectsTotal(thrift, 0);
    assertTimeoutsTotal(thrift, 0);
    control.verify();
}
Also used : AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) TTransportException(org.apache.thrift.transport.TTransportException) Capture(org.easymock.Capture) Test(org.junit.Test)

Example 3 with AsyncMethodCallback

use of org.apache.thrift.async.AsyncMethodCallback in project commons by twitter.

the class ThriftFactoryTest method testCreateAsync.

@Test
public void testCreateAsync() throws IOException, InterruptedException, ThriftFactory.ThriftFactoryException {
    final String[] responseHolder = new String[] { null };
    final CountDownLatch done = new CountDownLatch(1);
    AsyncMethodCallback<String> callback = new AsyncMethodCallback<String>() {

        @Override
        public void onComplete(String response) {
            responseHolder[0] = response;
            done.countDown();
        }

        @Override
        public void onError(Throwable throwable) {
            responseHolder[0] = throwable.toString();
            done.countDown();
        }
    };
    final Thrift<AsyncIface> thrift = ThriftFactory.create(GoodService.AsyncIface.class).withMaxConnectionsPerEndpoint(1).useFramedTransport(true).buildAsync(ImmutableSet.of(new InetSocketAddress(1234)));
    addTearDown(new TearDown() {

        @Override
        public void tearDown() {
            thrift.close();
        }
    });
    GoodService.AsyncIface client = thrift.builder().blocking().create();
    client.doWork(callback);
    assertTrue("wasn't called back in time, callback got " + responseHolder[0], done.await(5000, TimeUnit.MILLISECONDS));
    assertEquals(GoodService.DONE, responseHolder[0]);
}
Also used : TearDown(com.google.common.testing.TearDown) AsyncIface(com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface) InetSocketAddress(java.net.InetSocketAddress) AsyncIface(com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) Test(org.junit.Test)

Example 4 with AsyncMethodCallback

use of org.apache.thrift.async.AsyncMethodCallback in project mlib by myshzzx.

the class AsyncTest1 method test2.

@Test
public void test2() throws Exception {
    TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(new InetSocketAddress("l", 19000), 0);
    TServer server = new THsHaServer(new THsHaServer.Args(serverTransport).processor(new TService1.AsyncProcessor<TService1.AsyncIface>(new Service2Impl())).protocolFactory(new TCompactProtocol.Factory()));
    new Thread() {

        @Override
        public void run() {
            server.serve();
        }
    }.start();
    Thread.sleep(1000);
    TNonblockingSocket transport = new TNonblockingSocket("l", 19000, 5000);
    TService1.AsyncIface client = new TService1.AsyncClient(new TCompactProtocol.Factory(), new TAsyncClientManager(), transport);
    byte[] b = { 1, 2, 3 };
    AsyncMethodCallback<TService1.AsyncClient.getStr_call> h = new AsyncMethodCallback<TService1.AsyncClient.getStr_call>() {

        @Override
        public void onComplete(TService1.AsyncClient.getStr_call response) {
            try {
                System.out.println(response.getResult());
            } catch (TException e) {
                log.error("async get rsp fail.", e);
            }
        }

        @Override
        public void onError(Exception e) {
            log.error("async call fail.", e);
        }
    };
    for (int i = 0; i < 2; i++) {
        client.getStr("mysh", ByteBuffer.wrap(b), h);
    }
    Thread.sleep(10000000);
}
Also used : TException(org.apache.thrift.TException) TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket) TServer(org.apache.thrift.server.TServer) InetSocketAddress(java.net.InetSocketAddress) LoggerFactory(org.slf4j.LoggerFactory) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TAsyncClientManager(org.apache.thrift.async.TAsyncClientManager) THsHaServer(org.apache.thrift.server.THsHaServer) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) TException(org.apache.thrift.TException) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) Test(org.junit.Test)

Example 5 with AsyncMethodCallback

use of org.apache.thrift.async.AsyncMethodCallback in project commons by twitter.

the class Thrift method createClient.

private T createClient(Config config) {
    StatsProvider statsProvider = config.getStatsProvider();
    // lease/call/[invalidate]/release
    boolean debug = config.isDebug();
    Caller decorated = new ThriftCaller<T>(connectionPool, requestTracker, clientFactory, config.getConnectTimeout(), debug);
    // [retry]
    if (config.getMaxRetries() > 0) {
        decorated = new RetryingCaller(decorated, async, statsProvider, serviceName, config.getMaxRetries(), config.getRetryableExceptions(), debug);
    }
    // [deadline]
    if (config.getRequestTimeout().getValue() > 0) {
        Preconditions.checkArgument(!async, "Request deadlines may not be used with an asynchronous client.");
        decorated = new DeadlineCaller(decorated, async, executorService, config.getRequestTimeout());
    }
    // [debug]
    if (debug) {
        decorated = new DebugCaller(decorated, async);
    }
    // stats
    if (config.enableStats()) {
        decorated = new StatTrackingCaller(decorated, async, statsProvider, serviceName);
    }
    final Caller caller = decorated;
    final InvocationHandler invocationHandler = new InvocationHandler() {

        @Override
        public Object invoke(Object o, Method method, Object[] args) throws Throwable {
            AsyncMethodCallback callback = null;
            if (args != null && async) {
                List<Object> argsList = Lists.newArrayList(args);
                callback = extractCallback(argsList);
                args = argsList.toArray();
            }
            return caller.call(method, args, callback, null);
        }
    };
    @SuppressWarnings("unchecked") T instance = (T) Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class<?>[] { serviceInterface }, invocationHandler);
    return instance;
}
Also used : DeadlineCaller(com.twitter.common.thrift.callers.DeadlineCaller) ThriftCaller(com.twitter.common.thrift.callers.ThriftCaller) DebugCaller(com.twitter.common.thrift.callers.DebugCaller) Method(java.lang.reflect.Method) StatTrackingCaller(com.twitter.common.thrift.callers.StatTrackingCaller) InvocationHandler(java.lang.reflect.InvocationHandler) StatsProvider(com.twitter.common.stats.StatsProvider) StatTrackingCaller(com.twitter.common.thrift.callers.StatTrackingCaller) RetryingCaller(com.twitter.common.thrift.callers.RetryingCaller) DebugCaller(com.twitter.common.thrift.callers.DebugCaller) DeadlineCaller(com.twitter.common.thrift.callers.DeadlineCaller) ThriftCaller(com.twitter.common.thrift.callers.ThriftCaller) Caller(com.twitter.common.thrift.callers.Caller) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) RetryingCaller(com.twitter.common.thrift.callers.RetryingCaller)

Aggregations

AsyncMethodCallback (org.apache.thrift.async.AsyncMethodCallback)7 Test (org.junit.Test)5 TTransportException (org.apache.thrift.transport.TTransportException)3 Capture (org.easymock.Capture)3 TearDown (com.google.common.testing.TearDown)2 Method (java.lang.reflect.Method)2 InetSocketAddress (java.net.InetSocketAddress)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Amount (com.twitter.common.quantity.Amount)1 StatsProvider (com.twitter.common.stats.StatsProvider)1 TTimeoutException (com.twitter.common.thrift.TTimeoutException)1 AsyncIface (com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface)1 Caller (com.twitter.common.thrift.callers.Caller)1 DeadlineCaller (com.twitter.common.thrift.callers.DeadlineCaller)1 DebugCaller (com.twitter.common.thrift.callers.DebugCaller)1 RetryingCaller (com.twitter.common.thrift.callers.RetryingCaller)1 StatTrackingCaller (com.twitter.common.thrift.callers.StatTrackingCaller)1 ThriftCaller (com.twitter.common.thrift.callers.ThriftCaller)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1