Search in sources :

Example 1 with Cancellable

use of com.linkedin.r2.util.Cancellable in project rest.li by linkedin.

the class Http2NettyStreamClient method doWriteRequest.

@Override
protected void doWriteRequest(Request request, final RequestContext context, SocketAddress address, TimeoutTransportCallback<StreamResponse> callback) {
    final AsyncPool<Channel> pool;
    try {
        pool = _channelPoolManager.getPoolForAddress(address);
    } catch (IllegalStateException e) {
        errorResponse(callback, e);
        return;
    }
    context.putLocalAttr(R2Constants.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_2);
    Callback<Channel> getCallback = new ChannelPoolGetCallback(pool, request, callback);
    final Cancellable pendingGet = pool.get(getCallback);
    if (pendingGet != null) {
        callback.addTimeoutTask(() -> pendingGet.cancel());
    }
}
Also used : Cancellable(com.linkedin.r2.util.Cancellable) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel)

Example 2 with Cancellable

use of com.linkedin.r2.util.Cancellable in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testMultipleGetItemSucceeds.

@Test
public void testMultipleGetItemSucceeds() throws Exception {
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    final List<Object> items = new ArrayList<>(GET_COUNT);
    for (int i = 0; i < GET_COUNT; i++) {
        FutureCallback<Object> getCallback = new FutureCallback<>();
        Cancellable cancellable = pool.get(getCallback);
        // Operation should not be cancellable
        Assert.assertNotNull(cancellable);
        Assert.assertEquals(cancellable.cancel(), false);
        Object item = getCallback.get(GET_TIMEOUT, TIME_UNIT);
        Assert.assertNotNull(item);
        items.add(item);
    }
    // All items should essentially be the same instance
    Assert.assertEquals(items.size(), GET_COUNT);
    items.stream().forEach(item -> Assert.assertSame(item, items.get(0)));
    verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
    // Put items back to the pool
    IntStream.range(0, GET_COUNT).forEach(i -> pool.put(items.get(i)));
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) Cancellable(com.linkedin.r2.util.Cancellable) ArrayList(java.util.ArrayList) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 3 with Cancellable

use of com.linkedin.r2.util.Cancellable in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testGetItemCreateFails.

@Test(expectedExceptions = ExecutionException.class)
public void testGetItemCreateFails() throws Exception {
    final LifecycleMock lifecycle = new LifecycleMock();
    lifecycle.setCreateConsumer(callback -> callback.onError(new Exception("Simulated create failure")));
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    FutureCallback<Object> getCallback = new FutureCallback<>();
    Cancellable cancellable = pool.get(getCallback);
    Assert.assertNotNull(cancellable);
    verifyStats(pool.getStats(), 0, 0, 0, 0, 0, 0, 0, 1, 0);
    getCallback.get(GET_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) Cancellable(com.linkedin.r2.util.Cancellable) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 4 with Cancellable

use of com.linkedin.r2.util.Cancellable in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testGetExceedMaxWaiters.

@Test
public void testGetExceedMaxWaiters() throws Exception {
    final int maxWaiters = 5;
    final CountDownLatch latch = new CountDownLatch(1);
    final LifecycleMock lifecycle = new LifecycleMock();
    lifecycle.setCreateConsumer(callback -> {
        try {
            latch.await();
            callback.onSuccess(new Object());
        } catch (Exception e) {
            callback.onError(e);
        }
    });
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, maxWaiters);
    pool.start();
    CountDownLatch getLatch = new CountDownLatch(maxWaiters - 1);
    ConcurrentLinkedQueue<FutureCallback<Object>> callbacks = new ConcurrentLinkedQueue<>();
    for (int i = 0; i < maxWaiters; i++) {
        SCHEDULER.execute(() -> {
            FutureCallback<Object> callback = new FutureCallback<>();
            Cancellable cancellable = pool.get(callback);
            Assert.assertNotNull(cancellable);
            callbacks.add(callback);
            getLatch.countDown();
        });
    }
    getLatch.await(GET_TIMEOUT, TIME_UNIT);
    FutureCallback<Object> waiterCallback = new FutureCallback<>();
    Cancellable cancellable = pool.get(waiterCallback);
    Assert.assertNotNull(cancellable);
    Assert.assertFalse(cancellable.cancel());
    try {
        waiterCallback.get(GET_TIMEOUT, TIME_UNIT);
        Assert.fail("Callback should fail but did not");
    } catch (ExecutionException e) {
    // Exception is recoverable and expected
    }
    latch.countDown();
    callbacks.forEach(callback -> {
        try {
            Object item = callback.get();
            Assert.assertNotNull(item);
            pool.put(item);
        } catch (Exception e) {
            Assert.fail("Unexpected exception during #get()");
        }
    });
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : Cancellable(com.linkedin.r2.util.Cancellable) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 5 with Cancellable

use of com.linkedin.r2.util.Cancellable in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testMultipleDisposeItemSucceeds.

@Test
public void testMultipleDisposeItemSucceeds() throws Exception {
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    final List<Object> items = new ArrayList<>(GET_COUNT);
    for (int i = 0; i < GET_COUNT; i++) {
        FutureCallback<Object> getCallback = new FutureCallback<>();
        Cancellable cancellable = pool.get(getCallback);
        // Operation should not be cancellable
        Assert.assertNotNull(cancellable);
        Assert.assertEquals(cancellable.cancel(), false);
        Object item = getCallback.get(GET_TIMEOUT, TIME_UNIT);
        Assert.assertNotNull(item);
        items.add(item);
    }
    // All items should essentially be the same instance
    Assert.assertEquals(items.size(), GET_COUNT);
    items.stream().forEach(item -> Assert.assertSame(item, items.get(0)));
    verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
    // Put items back to the pool
    IntStream.range(0, GET_COUNT).forEach(i -> pool.dispose(items.get(i)));
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) Cancellable(com.linkedin.r2.util.Cancellable) ArrayList(java.util.ArrayList) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

Cancellable (com.linkedin.r2.util.Cancellable)17 FutureCallback (com.linkedin.common.callback.FutureCallback)9 Test (org.testng.annotations.Test)9 None (com.linkedin.common.util.None)8 AsyncSharedPoolImpl (com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl)8 Channel (io.netty.channel.Channel)7 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Callback (com.linkedin.common.callback.Callback)2 SimpleCallback (com.linkedin.common.callback.SimpleCallback)2 SizeLimitExceededException (com.linkedin.r2.SizeLimitExceededException)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 NettyClientState (com.linkedin.r2.netty.common.NettyClientState)2 LinkedDeque (com.linkedin.r2.util.LinkedDeque)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 UnknownHostException (java.net.UnknownHostException)2