Search in sources :

Example 86 with Timeout

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

the class HttpServerFactory method createRAPServer.

public HttpServer createRAPServer(int port, TransportDispatcher transportDispatcher, int timeout, boolean restOverStream) {
    final TransportDispatcher filterDispatcher = new FilterChainDispatcher(transportDispatcher, _filters);
    HttpServlet httpServlet = restOverStream ? new RAPStreamServlet(filterDispatcher, timeout, DEFAULT_LOG_SERVLET_EXCEPTIONS) : new RAPServlet(filterDispatcher);
    return new HttpJettyServer(port, httpServlet);
}
Also used : FilterChainDispatcher(com.linkedin.r2.filter.transport.FilterChainDispatcher) HttpServlet(javax.servlet.http.HttpServlet) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)

Example 87 with Timeout

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

the class TestAsyncPool method testGetStatsWithErrors.

@Test
public void testGetStatsWithErrors() throws Exception {
    final int POOL_SIZE = 25;
    final int GET = 20;
    final int PUT_BAD = 5;
    final int DISPOSE = 7;
    final int CREATE_BAD = 9;
    final int TIMEOUT = 100;
    final UnreliableLifecycle lifecycle = new UnreliableLifecycle();
    final AsyncPool<AtomicBoolean> pool = new AsyncPoolImpl<>("object pool", lifecycle, POOL_SIZE, TIMEOUT, _executor);
    PoolStats stats;
    final List<AtomicBoolean> objects = new ArrayList<>();
    pool.start();
    // do a few gets
    for (int i = 0; i < GET; i++) {
        FutureCallback<AtomicBoolean> cb = new FutureCallback<>();
        pool.get(cb);
        AtomicBoolean obj = cb.get();
        objects.add(obj);
    }
    // put and destroy some, with errors
    lifecycle.setFail(true);
    for (int i = 0; i < PUT_BAD; i++) {
        AtomicBoolean obj = objects.remove(objects.size() - 1);
        obj.set(false);
        pool.put(obj);
    }
    for (int i = 0; i < DISPOSE; i++) {
        AtomicBoolean obj = objects.remove(objects.size() - 1);
        pool.dispose(obj);
    }
    stats = pool.getStats();
    Assert.assertEquals(stats.getTotalDestroyed(), 0);
    Assert.assertEquals(stats.getTotalCreateErrors(), 0);
    Assert.assertEquals(stats.getTotalDestroyErrors(), PUT_BAD + DISPOSE);
    Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
    // create some with errors
    for (int i = 0; i < CREATE_BAD; i++) {
        FutureCallback<AtomicBoolean> cb = new FutureCallback<>();
        try {
            pool.get(cb);
        } catch (Exception e) {
        // this error is expected
        }
    }
    stats = pool.getStats();
    Assert.assertEquals(stats.getCheckedOut(), GET - PUT_BAD - DISPOSE);
    Assert.assertEquals(stats.getTotalCreateErrors(), CREATE_BAD);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncPoolImpl(com.linkedin.r2.transport.http.client.AsyncPoolImpl) ArrayList(java.util.ArrayList) FutureCallback(com.linkedin.common.callback.FutureCallback) ObjectCreationTimeoutException(com.linkedin.r2.transport.http.client.ObjectCreationTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) PoolStats(com.linkedin.r2.transport.http.client.PoolStats) Test(org.testng.annotations.Test)

Example 88 with Timeout

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

the class TestAsyncSharedPoolImpl method testShutdownWithMultiplePendingPutValidationFails.

@Test
public void testShutdownWithMultiplePendingPutValidationFails() throws Exception {
    final LifecycleMock lifecycleMock = new LifecycleMock();
    lifecycleMock.setValidatePutSupplier(() -> false);
    final AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycleMock, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    final CountDownLatch latch = new CountDownLatch(GET_COUNT);
    final Collection<FutureCallback<Object>> getCallbacks = new ConcurrentLinkedQueue<>();
    IntStream.range(0, GET_COUNT).forEach(i -> SCHEDULER.execute(() -> {
        FutureCallback<Object> getCallback = new FutureCallback<>();
        pool.get(getCallback);
        getCallbacks.add(getCallback);
        latch.countDown();
    }));
    if (!latch.await(OPERATION_TIMEOUT, TIME_UNIT)) {
        Assert.fail("Timeout waiting for get calls");
    }
    Assert.assertEquals(getCallbacks.size(), GET_COUNT);
    final Collection<Object> items = new ConcurrentLinkedQueue<>();
    getCallbacks.stream().forEach(callback -> {
        try {
            items.add(callback.get(GET_TIMEOUT, TIME_UNIT));
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    Assert.assertEquals(items.size(), GET_COUNT);
    verifyStats(pool.getStats(), 1, GET_COUNT, 0, 0, 0, 0, 1, 0, 0);
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    // Put items back to the pool
    items.stream().forEach(item -> SCHEDULER.execute(() -> pool.put(item)));
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 89 with Timeout

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

the class TestDisruptor method testRestNoDisrupt.

@Test
public void testRestNoDisrupt() throws Exception {
    System.out.println(_serverProvider);
    final RequestContext requestContext = new RequestContext();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    _client.restRequest(new RestRequestBuilder(getHttpURI()).build(), requestContext, new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse result) {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            success.set(false);
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 90 with Timeout

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

the class TestDisruptor method testRestLatencyDisrupt.

@Test
public void testRestLatencyDisrupt() throws Exception {
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.delay(REQUEST_LATENCY));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    _client.restRequest(new RestRequestBuilder(getHttpURI()).build(), requestContext, new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse result) {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            success.set(false);
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Aggregations

Test (org.testng.annotations.Test)78 RequestContext (com.linkedin.r2.message.RequestContext)46 CountDownLatch (java.util.concurrent.CountDownLatch)40 TimeoutException (java.util.concurrent.TimeoutException)40 None (com.linkedin.common.util.None)33 FutureCallback (com.linkedin.common.callback.FutureCallback)32 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)26 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 URI (java.net.URI)25 RestRequest (com.linkedin.r2.message.rest.RestRequest)21 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)21 ByteString (com.linkedin.data.ByteString)19 HashMap (java.util.HashMap)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 RestResponse (com.linkedin.r2.message.rest.RestResponse)18 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)17 ExecutionException (java.util.concurrent.ExecutionException)17 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)15 IOException (java.io.IOException)15 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)13