use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestAsyncPool method testGetStats.
@Test(retryAnalyzer = SingleRetry.class)
public void testGetStats() throws Exception {
final int POOL_SIZE = 25;
final int MIN_SIZE = 0;
final int MAX_WAITER_SIZE = Integer.MAX_VALUE;
final SettableClock clock = new SettableClock();
final LongTracking waitTimeTracker = new LongTracking();
final int GET = 20;
final int PUT_GOOD = 2;
final int PUT_BAD = 3;
final int DISPOSE = 4;
final int TIMEOUT = 100;
final int WAITER_TIMEOUT = 200;
final int DELAY = 1200;
final UnreliableLifecycle lifecycle = new UnreliableLifecycle();
final AsyncPool<AtomicBoolean> pool = new AsyncPoolImpl<>("object pool", lifecycle, POOL_SIZE, TIMEOUT, WAITER_TIMEOUT, _executor, MAX_WAITER_SIZE, AsyncPoolImpl.Strategy.MRU, MIN_SIZE, new NoopRateLimiter(), clock, waitTimeTracker);
PoolStats stats;
final List<AtomicBoolean> objects = new ArrayList<>();
pool.start();
// test values at initialization
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), 0);
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), 0);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalWaiterTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), 0);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), 0);
Assert.assertEquals(stats.getSampleMaxPoolSize(), 0);
// 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);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET);
// do some puts with good objects
for (int i = 0; i < PUT_GOOD; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
pool.put(obj);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET);
// do some puts with bad objects
for (int i = 0; i < PUT_BAD; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
// invalidate the object
obj.set(false);
pool.put(obj);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), PUT_BAD);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD - PUT_BAD);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET - PUT_BAD);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET - PUT_GOOD);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET);
// do some disposes
for (int i = 0; i < DISPOSE; i++) {
AtomicBoolean obj = objects.remove(objects.size() - 1);
pool.dispose(obj);
}
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getTotalTimedOut(), 0);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET - PUT_GOOD - PUT_BAD);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET - PUT_BAD);
// wait for a reap -- should destroy the PUT_GOOD objects
Thread.sleep(DELAY);
clock.addDuration(SAMPLING_DURATION_INCREMENT);
stats = pool.getStats();
Assert.assertEquals(stats.getTotalCreated(), GET);
Assert.assertEquals(stats.getTotalDestroyed(), PUT_GOOD + PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), 0);
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getTotalTimedOut(), PUT_GOOD);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getMaxPoolSize(), POOL_SIZE);
Assert.assertEquals(stats.getMinPoolSize(), 0);
Assert.assertEquals(stats.getPoolSize(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getSampleMaxCheckedOut(), GET - PUT_GOOD - PUT_BAD - DISPOSE);
Assert.assertEquals(stats.getSampleMaxPoolSize(), GET - PUT_BAD - DISPOSE);
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class TestAsyncPool method testCancelTriggerShutdown.
/**
* Tests {@link AsyncPool}'s shutdown sequence is properly triggered when outstanding
* waiters cancel the previous get calls.
*/
@Test
public void testCancelTriggerShutdown() throws Exception {
SynchronousLifecycle lifecycle = new SynchronousLifecycle();
AsyncPool<Object> pool = new AsyncPoolImpl<>("object pool", lifecycle, 1, 100, _executor);
pool.start();
FutureCallback<Object> callback1 = new FutureCallback<>();
Cancellable cancellable1 = pool.get(callback1);
FutureCallback<Object> callback2 = new FutureCallback<>();
Cancellable cancellable2 = pool.get(callback2);
FutureCallback<None> shutdownCallback = new FutureCallback<>();
pool.shutdown(shutdownCallback);
// Disposes the previously checked out object. The pool now has no outstanding checkouts but waiter
// size is still one due to the second #get call above.
pool.dispose(callback1.get(5, TimeUnit.SECONDS));
// Caller cancels the second #get call. The pool should be in the right condition and initiate shutdown.
cancellable2.cancel();
// Pool should shutdown successfully without the callback timeout
shutdownCallback.get(5, TimeUnit.SECONDS);
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class BaseTestSmoothRateLimiter method testSubmitWithinPermits.
@Test(timeOut = TEST_TIMEOUT)
public void testSubmitWithinPermits() throws Exception {
AsyncRateLimiter rateLimiter = getRateLimiter(_scheduledExecutorService, _executor, _clock);
rateLimiter.setRate(ONE_PERMIT_PER_PERIOD, ONE_SECOND_PERIOD, UNLIMITED_BURST);
FutureCallback<None> callback = new FutureCallback<>();
rateLimiter.submit(callback);
callback.get();
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class BaseTestSmoothRateLimiter method testSetRateInstantaneous.
@Test(timeOut = TEST_TIMEOUT)
public void testSetRateInstantaneous() {
ClockedExecutor clockedExecutor = new ClockedExecutor();
AsyncRateLimiter rateLimiter = getRateLimiter(clockedExecutor, clockedExecutor, clockedExecutor);
List<FutureCallback<None>> callbacks = new ArrayList<>();
IntStream.range(0, 10).forEachOrdered(i -> {
FutureCallback<None> callback = new FutureCallback<>();
rateLimiter.submit(callback);
callbacks.add(callback);
});
// the last set should take immediately effect, and therefore at ms 0, we should have 3 permits available
rateLimiter.setRate(0d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
rateLimiter.setRate(1d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
rateLimiter.setRate(2d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
rateLimiter.setRate(3d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
// trigger task to run them until current time
clockedExecutor.runFor(0);
// We have one permit to begin with so the first task should run immediate and left with four pending
IntStream.range(0, 3).forEach(i -> assertTrue(callbacks.get(i).isDone(), i + " should have been executed " + callbacks.get(i)));
IntStream.range(3, 10).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
IntStream.range(3, 6).forEach(i -> assertTrue(callbacks.get(i).isDone(), i + " should have been executed " + callbacks.get(i)));
IntStream.range(6, 10).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
}
use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.
the class BaseTestSmoothRateLimiter method testSetRate.
@Test(timeOut = TEST_TIMEOUT)
public void testSetRate() throws Exception {
ClockedExecutor clockedExecutor = new ClockedExecutor();
AsyncRateLimiter rateLimiter = getRateLimiter(clockedExecutor, clockedExecutor, clockedExecutor);
rateLimiter.setRate(ONE_PERMIT_PER_PERIOD, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
List<FutureCallback<None>> callbacks = new ArrayList<>();
IntStream.range(0, 5).forEach(i -> {
FutureCallback<None> callback = new FutureCallback<>();
rateLimiter.submit(callback);
callbacks.add(callback);
});
// trigger task to run them until current time
clockedExecutor.runFor(0);
// We have one permit to begin with so the first task should run immediate and left with four pending
callbacks.get(0).get();
IntStream.range(0, 1).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(1, 5).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
// We set the permit rate to two per period and increment the clock by one millisecond. We expect two
// more callbacks to be invoked at the next permit issuance
rateLimiter.setRate(2d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
clockedExecutor.runFor(0);
callbacks.get(1).get();
callbacks.get(2).get();
IntStream.range(0, 3).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(3, 5).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
// We set the permit rate back to one per period and increment the clock by one millisecond. We expect
// only one more callbacks to be invoked at the next permit issuance
rateLimiter.setRate(1d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
callbacks.get(3).get();
IntStream.range(0, 4).forEach(i -> assertTrue(callbacks.get(i).isDone()));
IntStream.range(4, 5).forEach(i -> assertFalse(callbacks.get(i).isDone(), i + " should not have been executed"));
// We set the permit rate to two per period again and increment the clock by one millisecond. We expect
// only one more callbacks to be invoked at the next permit issuance because only one is left
rateLimiter.setRate(2d, ONE_MILLISECOND_PERIOD, UNLIMITED_BURST);
clockedExecutor.runFor(ONE_MILLISECOND_PERIOD);
callbacks.get(4).get();
IntStream.range(0, 5).forEach(i -> assertTrue(callbacks.get(i).isDone()));
}
Aggregations