Search in sources :

Example 1 with AsyncResponseFuture

use of com.linkedin.pinot.transport.common.AsyncResponseFuture in project pinot by linkedin.

the class KeyedPoolImpl method checkoutObject.

@Override
public KeyedFuture<K, T> checkoutObject(K key) {
    AsyncPool<T> pool = _keyedPool.get(key);
    if (null == pool) {
        synchronized (_mutex) {
            pool = _keyedPool.get(key);
            if (null == pool) {
                String poolName = "Pool for (" + key + ")";
                AsyncPoolResourceManagerAdapter<K, T> rmAdapter = new AsyncPoolResourceManagerAdapter<K, T>(key, _resourceManager, _executorService, _metricRegistry);
                pool = new AsyncPoolImpl<T>(poolName, rmAdapter, _maxResourcesPerKey, _idleTimeoutMs, _timeoutExecutor, _executorService, _maxPendingCheckoutRequests, Strategy.LRU, _minResourcesPerKey, _metricRegistry);
                _keyedPool.put(key, pool);
                _poolStats.add(pool);
                _pooledResourceManagerMap.put(key, rmAdapter);
                pool.start();
            }
        }
    }
    AsyncResponseFuture<K, T> future = new AsyncResponseFuture<K, T>(key, "Checkout future for key " + key);
    Cancellable cancellable = pool.get(future);
    future.setCancellable(cancellable);
    return future;
}
Also used : Cancellable(com.linkedin.pinot.transport.common.Cancellable) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture)

Example 2 with AsyncResponseFuture

use of com.linkedin.pinot.transport.common.AsyncResponseFuture in project pinot by linkedin.

the class KeyedPoolImplTest method testShutdownWhileCheckingOut.

@Test
public void testShutdownWhileCheckingOut() throws Exception {
    ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
    ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
    int numKeys = 1;
    int numResourcesPerKey = 1;
    Map<String, List<String>> resources = buildCreateMap(numKeys, numResourcesPerKey);
    BlockingTestResourceManager rm = new BlockingTestResourceManager(resources, null, null, null);
    KeyedPool<String, String> kPool = new KeyedPoolImpl<String, String>(0, 1, 1000L, 1000 * 60 * 60, rm, timedExecutor, service, null);
    kPool.start();
    AsyncResponseFuture<String, String> f = (AsyncResponseFuture<String, String>) kPool.checkoutObject(getKey(0));
    boolean isTimedout = false;
    try {
        f.get(2, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        isTimedout = true;
    }
    Assert.assertTrue(isTimedout);
    kPool.shutdown().get();
    // Future should have been done with error
    Assert.assertNull(f.get());
    Assert.assertNotNull(f.getError());
    boolean cancelled = f.cancel(false);
    Assert.assertFalse(cancelled);
    Assert.assertFalse(f.isCancelled());
    Assert.assertTrue(f.isDone());
    rm.getCreateBlockLatch().countDown();
    Thread.sleep(5000);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 3 with AsyncResponseFuture

use of com.linkedin.pinot.transport.common.AsyncResponseFuture in project pinot by linkedin.

the class KeyedPoolImplTest method testInvalidCheckinDestroy.

@Test
public void testInvalidCheckinDestroy() throws Exception {
    ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
    ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
    int numKeys = 1;
    int numResourcesPerKey = 1;
    Map<String, List<String>> resources = buildCreateMap(numKeys, numResourcesPerKey);
    TestResourceManager rm = new TestResourceManager(resources, null, null, null);
    KeyedPool<String, String> kPool = new KeyedPoolImpl<String, String>(0, 1, 1000L, 1000 * 60 * 60, rm, timedExecutor, service, null);
    kPool.start();
    AsyncResponseFuture<String, String> f = (AsyncResponseFuture<String, String>) kPool.checkoutObject(getKey(0));
    String s1 = f.getOne();
    // checkin with invalid key
    boolean isException = false;
    try {
        kPool.checkinObject(getKey(1), s1);
    } catch (IllegalStateException e) {
        isException = true;
    }
    Assert.assertTrue(isException);
    // destroy with invalid key
    isException = false;
    try {
        kPool.destroyObject(getKey(1), s1);
    } catch (IllegalStateException e) {
        isException = true;
    }
    Assert.assertTrue(isException);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Test(org.testng.annotations.Test)

Example 4 with AsyncResponseFuture

use of com.linkedin.pinot.transport.common.AsyncResponseFuture in project pinot by linkedin.

the class KeyedPoolImplTest method testCancelAfterCheckingOut.

@Test
public void testCancelAfterCheckingOut() throws Exception {
    ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
    ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
    int numKeys = 1;
    int numResourcesPerKey = 1;
    Map<String, List<String>> resources = buildCreateMap(numKeys, numResourcesPerKey);
    BlockingTestResourceManager rm = new BlockingTestResourceManager(resources, null, null, null);
    KeyedPool<String, String> kPool = new KeyedPoolImpl<String, String>(0, 1, 1000L, 1000 * 60 * 60, rm, timedExecutor, service, null);
    kPool.start();
    AsyncResponseFuture<String, String> f = (AsyncResponseFuture<String, String>) kPool.checkoutObject(getKey(0));
    boolean isTimedout = false;
    try {
        f.get(2, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        isTimedout = true;
    }
    Assert.assertTrue(isTimedout);
    boolean cancelled = f.cancel(false);
    Assert.assertTrue(cancelled);
    Assert.assertTrue(f.isCancelled());
    Assert.assertTrue(f.isDone());
    rm.getCreateBlockLatch().countDown();
    kPool.shutdown().get();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 5 with AsyncResponseFuture

use of com.linkedin.pinot.transport.common.AsyncResponseFuture 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();
    }
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Cancellable(com.linkedin.pinot.transport.common.Cancellable) ServerInstance(com.linkedin.pinot.common.response.ServerInstance) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) MetricsRegistry(com.yammer.metrics.core.MetricsRegistry) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) AsyncPoolResourceManagerAdapter(com.linkedin.pinot.transport.pool.AsyncPoolResourceManagerAdapter) NoneType(com.linkedin.pinot.transport.common.NoneType) HashedWheelTimer(io.netty.util.HashedWheelTimer) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) PoolStats(com.linkedin.pinot.transport.metrics.PoolStats) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) AsyncPoolImpl(com.linkedin.pinot.transport.pool.AsyncPoolImpl) AsyncPool(com.linkedin.pinot.transport.pool.AsyncPool) Test(org.testng.annotations.Test)

Aggregations

AsyncResponseFuture (com.linkedin.pinot.transport.common.AsyncResponseFuture)7 ExecutorService (java.util.concurrent.ExecutorService)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)6 Test (org.testng.annotations.Test)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 Cancellable (com.linkedin.pinot.transport.common.Cancellable)2 AggregatedPoolStats (com.linkedin.pinot.transport.metrics.AggregatedPoolStats)2 TimeoutException (java.util.concurrent.TimeoutException)2 ServerInstance (com.linkedin.pinot.common.response.ServerInstance)1 NoneType (com.linkedin.pinot.transport.common.NoneType)1 NettyClientMetrics (com.linkedin.pinot.transport.metrics.NettyClientMetrics)1 PoolStats (com.linkedin.pinot.transport.metrics.PoolStats)1 AsyncPool (com.linkedin.pinot.transport.pool.AsyncPool)1 AsyncPoolImpl (com.linkedin.pinot.transport.pool.AsyncPoolImpl)1 AsyncPoolResourceManagerAdapter (com.linkedin.pinot.transport.pool.AsyncPoolResourceManagerAdapter)1 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1