Search in sources :

Example 1 with NoneType

use of com.linkedin.pinot.transport.common.NoneType 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)

Example 2 with NoneType

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

the class KeyedPoolImplTest method testShutdown.

@Test
public /**
   * Pool contains 5 inner pools with 5 resources as max capacity
   * First checkout and checkin all resources
   * Checkout one from each inner pool.
   * Shutdown now. This should not complete.
   * Destroy the checked out objects. Check stats
   * shutdown should have happened
   * @throws Exception
   */
void testShutdown() throws Exception {
    ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
    ExecutorService service = MoreExecutors.sameThreadExecutor();
    int numKeys = 5;
    int numResourcesPerKey = 5;
    TestResourceManager rm = new TestResourceManager(buildCreateMap(numKeys, numResourcesPerKey), null, null, null);
    KeyedPool<String, String> kPool = new KeyedPoolImpl<String, String>(5, 5, 1000 * 60 * 60L, 100, rm, timedExecutor, service, null);
    kPool.start();
    AggregatedPoolStats s = (AggregatedPoolStats) kPool.getStats();
    int c = 1;
    for (int j = 0; j < numResourcesPerKey; j++) {
        for (int i = 0; i < numKeys; i++) {
            KeyedFuture<String, String> rFuture = kPool.checkoutObject(getKey(i));
            String resource = rFuture.getOne();
            Assert.assertEquals(resource, getResource(i, j));
            s.refresh();
            Assert.assertEquals(s.getCheckedOut(), c++);
        }
    }
    s = (AggregatedPoolStats) kPool.getStats();
    Assert.assertEquals(s.getTotalCreated(), numKeys * numResourcesPerKey);
    int checkedOut = c - 1;
    // checkin back all
    for (int j = 0; j < numResourcesPerKey; j++) {
        for (int i = 0; i < numKeys; i++) {
            kPool.checkinObject(getKey(i), getResource(i, j));
            s.refresh();
            Assert.assertEquals(s.getCheckedOut(), --checkedOut);
        }
    }
    // Check out 1 object for each key
    c = 1;
    for (int i = 0; i < numKeys; i++) {
        KeyedFuture<String, String> rFuture = kPool.checkoutObject(getKey(i));
        String resource = rFuture.getOne();
        Assert.assertEquals(resource, getResource(i, 0));
        s.refresh();
        Assert.assertEquals(s.getCheckedOut(), c);
        c++;
    }
    Assert.assertEquals(s.getPoolSize(), numKeys * numResourcesPerKey);
    Assert.assertEquals(s.getIdleCount(), (numKeys * numResourcesPerKey) - 5);
    // SHutdown but it should not be done.
    Future<Map<String, NoneType>> f = kPool.shutdown();
    FutureReader<Map<String, NoneType>> reader = new FutureReader<Map<String, NoneType>>(f);
    reader.start();
    reader.getBeginLatch().await();
    Assert.assertTrue(reader.isStarted());
    Assert.assertFalse(reader.isDone());
    //none are destroyed
    Assert.assertEquals(rm.getDestroyedMap().keySet().size(), 0);
    // Now destroy some and checkin others
    int d = 0;
    for (int i = 0; i < numKeys; i++) {
        if ((i % 2) == 0) {
            kPool.destroyObject(getKey(i), getResource(i, 0));
            s.refresh();
            Assert.assertEquals(s.getTotalDestroyed(), ++d);
        } else {
            kPool.checkinObject(getKey(i), getResource(i, 0));
        }
    }
    s.refresh();
    Assert.assertEquals(s.getTotalDestroyed(), 3);
    // Now shutdown should complete
    f.get();
    reader.getEndLatch().await();
    Assert.assertTrue(reader.isDone());
    // Do one more shutdown call
    Future<Map<String, NoneType>> f2 = kPool.shutdown();
    f2.get();
    //Verify all objects are destroyed
    Map<String, List<String>> destroyedMap = rm.getDestroyedMap();
    Assert.assertEquals(destroyedMap.keySet().size(), numKeys);
    for (int i = 0; i < numKeys; i++) {
        List<String> r = destroyedMap.get(getKey(i));
        Assert.assertEquals(r.size(), numResourcesPerKey, "Resource for Key (" + getKey(i) + ")");
        for (int j = 0; j < numResourcesPerKey; j++) {
            Assert.assertTrue(r.contains(getResource(i, j)));
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NoneType(com.linkedin.pinot.transport.common.NoneType) AggregatedPoolStats(com.linkedin.pinot.transport.metrics.AggregatedPoolStats) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

NoneType (com.linkedin.pinot.transport.common.NoneType)2 ExecutorService (java.util.concurrent.ExecutorService)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 Test (org.testng.annotations.Test)2 ServerInstance (com.linkedin.pinot.common.response.ServerInstance)1 AsyncResponseFuture (com.linkedin.pinot.transport.common.AsyncResponseFuture)1 Cancellable (com.linkedin.pinot.transport.common.Cancellable)1 AggregatedPoolStats (com.linkedin.pinot.transport.metrics.AggregatedPoolStats)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 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 HashedWheelTimer (io.netty.util.HashedWheelTimer)1 Timer (io.netty.util.Timer)1 ArrayList (java.util.ArrayList)1