Search in sources :

Example 6 with PoolStats

use of com.linkedin.r2.transport.http.client.PoolStats 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 7 with PoolStats

use of com.linkedin.r2.transport.http.client.PoolStats in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testGetStats.

@Test
public void testGetStats() {
    AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, LIFECYCLE, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    PoolStats stats = pool.getStats();
    Assert.assertNotNull(stats);
    Assert.assertEquals(stats.getMaxPoolSize(), 1);
    Assert.assertEquals(stats.getMinPoolSize(), 0);
    Assert.assertEquals(stats.getIdleCount(), 0);
    Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
    Assert.assertEquals(stats.getTotalDestroyed(), 0);
    Assert.assertEquals(stats.getTotalTimedOut(), 0);
    Assert.assertEquals(stats.getTotalCreateErrors(), 0);
    Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
    Assert.assertEquals(stats.getCheckedOut(), 0);
    Assert.assertEquals(stats.getTotalCreated(), 0);
    Assert.assertEquals(stats.getPoolSize(), 0);
    Assert.assertEquals(stats.getSampleMaxCheckedOut(), 0);
    Assert.assertEquals(stats.getSampleMaxPoolSize(), 0);
    Assert.assertEquals(stats.getWaitTime50Pct(), 0);
    Assert.assertEquals(stats.getWaitTime95Pct(), 0);
    Assert.assertEquals(stats.getWaitTime99Pct(), 0);
    Assert.assertEquals(stats.getWaitTimeAvg(), 0.0);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) PoolStats(com.linkedin.r2.transport.http.client.PoolStats) Test(org.testng.annotations.Test)

Example 8 with PoolStats

use of com.linkedin.r2.transport.http.client.PoolStats in project rest.li by linkedin.

the class TestAsyncSharedPoolImpl method testMaximumConcurrency.

@Test(dataProvider = "lifecycles")
public void testMaximumConcurrency(AsyncPool.Lifecycle<Object> lifecycle) throws Exception {
    final AsyncSharedPoolImpl<Object> pool = new AsyncSharedPoolImpl<>(POOL_NAME, lifecycle, SCHEDULER, LIMITER, NO_POOL_TIMEOUT, MAX_WAITERS);
    pool.start();
    CountDownLatch latch = new CountDownLatch(NUMBER_OF_THREADS);
    IntStream.range(0, NUMBER_OF_THREADS).forEach(i -> SCHEDULER.execute(() -> {
        try {
            FutureCallback<Object> callback = new FutureCallback<>();
            Assert.assertNotNull(pool.get(callback));
            Object item = callback.get(GET_TIMEOUT, TIME_UNIT);
            Assert.assertNotNull(item);
            pool.put(item);
        } catch (Exception e) {
        } finally {
            latch.countDown();
        }
    }));
    if (!latch.await(OPERATION_TIMEOUT, TIME_UNIT)) {
        Assert.fail("Timed out before tasks finish");
    }
    PoolStats stats = pool.getStats();
    System.err.println("Total Created: " + stats.getTotalCreated());
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    pool.shutdown(shutdownCallback);
    shutdownCallback.get(SHUTDOWN_TIMEOUT, TIME_UNIT);
}
Also used : AsyncSharedPoolImpl(com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl) CountDownLatch(java.util.concurrent.CountDownLatch) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) ExecutionException(java.util.concurrent.ExecutionException) PoolStats(com.linkedin.r2.transport.http.client.PoolStats) Test(org.testng.annotations.Test)

Aggregations

PoolStats (com.linkedin.r2.transport.http.client.PoolStats)8 Test (org.testng.annotations.Test)8 AsyncPoolImpl (com.linkedin.r2.transport.http.client.AsyncPoolImpl)6 FutureCallback (com.linkedin.common.callback.FutureCallback)5 ExecutionException (java.util.concurrent.ExecutionException)5 ObjectCreationTimeoutException (com.linkedin.r2.transport.http.client.ObjectCreationTimeoutException)4 ArrayList (java.util.ArrayList)4 TimeoutException (java.util.concurrent.TimeoutException)4 LongTracking (com.linkedin.common.stats.LongTracking)3 None (com.linkedin.common.util.None)3 ExponentialBackOffRateLimiter (com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter)3 AsyncSharedPoolImpl (com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl)2 ClockedExecutor (com.linkedin.test.util.ClockedExecutor)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 NoopRateLimiter (com.linkedin.r2.transport.http.client.NoopRateLimiter)1 SettableClock (com.linkedin.util.clock.SettableClock)1 CountDownLatch (java.util.concurrent.CountDownLatch)1