use of com.linkedin.pinot.transport.pool.AsyncPoolImpl in project pinot by linkedin.
the class NettySingleConnectionIntegrationTest method testValidatePool.
/*
* WARNING: This test has potential failures due to timing.
*/
@Test
public void testValidatePool() throws Exception {
NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
Timer timer = new HashedWheelTimer();
MyServer server = new MyServer();
Thread.sleep(1000);
// used as a key to pool. Can be anything.
final String serverName = "SomeServer";
ServerInstance serverInstance = server.getServerInstance();
MetricsRegistry metricsRegistry = new MetricsRegistry();
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
PooledNettyClientResourceManager resourceManager = new PooledNettyClientResourceManager(eventLoopGroup, new HashedWheelTimer(), metric);
ExecutorService executorService = Executors.newCachedThreadPool();
ScheduledExecutorService timeoutExecutor = new ScheduledThreadPoolExecutor(5);
AsyncPoolResourceManagerAdapter<ServerInstance, NettyClientConnection> rmAdapter = new AsyncPoolResourceManagerAdapter<ServerInstance, NettyClientConnection>(serverInstance, resourceManager, executorService, metricsRegistry);
AsyncPool pool = new AsyncPoolImpl<NettyClientConnection>(serverName, rmAdapter, /*maxSize=*/
5, /*idleTimeoutMs=*/
100000, timeoutExecutor, executorService, /*maxWaiters=*/
10, AsyncPoolImpl.Strategy.LRU, /*minSize=*/
2, metricsRegistry);
pool.start();
Callback<NoneType> callback;
callback = new Callback<NoneType>() {
@Override
public void onSuccess(NoneType arg0) {
}
@Override
public void onError(Throwable arg0) {
Assert.fail("Shutdown error");
}
};
boolean serverShutdown = false;
try {
PoolStats stats;
/* Validate with no connection in pool */
// Give the pool enough time to create connections (in this case, 2 connections minSize)
Thread.sleep(3000);
// System.out.println("Validating with no used objects in the pool");
pool.validate(false);
// System.out.println(stats);
stats = pool.getStats();
Assert.assertEquals(2, stats.getPoolSize());
Assert.assertEquals(0, stats.getTotalBadDestroyed());
/* checkout one connection, it should not destroy anything */
AsyncResponseFuture<ServerInstance, NettyClientConnection> future = new AsyncResponseFuture<ServerInstance, NettyClientConnection>(serverInstance, "Future for " + serverName);
Cancellable cancellable = pool.get(future);
future.setCancellable(cancellable);
NettyClientConnection conn = future.getOne();
// System.out.println(stats);
stats = pool.getStats();
// System.out.println("Validating with one used object in the pool");
pool.validate(false);
Assert.assertEquals(2, stats.getPoolSize());
Assert.assertEquals(0, stats.getTotalBadDestroyed());
Assert.assertEquals(1, stats.getCheckedOut());
// Now stop the server, so that the checked out connection is invalidated.
server.shutdown();
serverShutdown = true;
;
// Wait for the client channel to be closed.
Thread.sleep(2000);
pool.validate(false);
// Wait for the callback into AsyncPoolImpl after the destroy thread completes destroying the connection
Thread.sleep(5000);
// System.out.println("Validating with one used object in the pool, after server shutdown");
// System.out.println(stats);
stats = pool.getStats();
Assert.assertEquals(2, stats.getPoolSize());
Assert.assertEquals(1, stats.getTotalBadDestroyed());
Assert.assertEquals(1, stats.getCheckedOut());
} finally {
server.shutdown();
pool.shutdown(callback);
executorService.shutdown();
timeoutExecutor.shutdown();
}
}
Aggregations