Search in sources :

Example 96 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project pinot by linkedin.

the class KeyedPoolImplTest method testDestroyError.

@Test
public void testDestroyError() throws Exception {
    ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
    ExecutorService service = MoreExecutors.sameThreadExecutor();
    int numKeys = 1;
    int numResourcesPerKey = 1;
    Map<String, List<String>> resources = buildCreateMap(numKeys, numResourcesPerKey);
    TestResourceManager rm = new TestResourceManager(resources, null, resources, null);
    KeyedPool<String, String> kPool = new KeyedPoolImpl<String, String>(0, 5, 1000L, 1000 * 60 * 60, rm, timedExecutor, service, null);
    AsyncResponseFuture<String, String> f = (AsyncResponseFuture<String, String>) kPool.checkoutObject(getKey(0));
    String r = f.getOne();
    Assert.assertTrue(f.isDone());
    Assert.assertNull(f.getError());
    // Create a countdown latch that waits for the attempt to delete the resource
    CountDownLatch latch = new CountDownLatch(1);
    rm.setCountDownLatch(latch);
    kPool.destroyObject(getKey(0), r);
    latch.await();
    // shutdown
    kPool.shutdown().get();
    AggregatedPoolStats s = (AggregatedPoolStats) kPool.getStats();
    s.refresh();
    Assert.assertEquals(s.getTotalDestroyErrors(), 1);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) 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) Test(org.testng.annotations.Test)

Example 97 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project pinot by linkedin.

the class KeyedPoolImplTest method testCreateError.

@Test
public void testCreateError() throws Exception {
    ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
    ExecutorService service = MoreExecutors.sameThreadExecutor();
    int numKeys = 1;
    int numResourcesPerKey = 1;
    Map<String, List<String>> resources = buildCreateMap(numKeys, numResourcesPerKey);
    TestResourceManager rm = new TestResourceManager(resources, resources, null, null);
    KeyedPool<String, String> kPool = new KeyedPoolImpl<String, String>(0, 1, 1000L, 1000 * 60 * 60, rm, timedExecutor, service, null);
    AsyncResponseFuture<String, String> f = (AsyncResponseFuture<String, String>) kPool.checkoutObject(getKey(0));
    Assert.assertTrue(f.isDone());
    Assert.assertNull(f.get());
    Assert.assertNotNull(f.getError());
    kPool.shutdown().get();
    AggregatedPoolStats s = (AggregatedPoolStats) kPool.getStats();
    s.refresh();
    Assert.assertEquals(s.getTotalCreateErrors(), 1);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) 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) Test(org.testng.annotations.Test)

Example 98 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor 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)

Example 99 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project pinot by linkedin.

the class ScatterGatherPerfClient method setup.

private void setup() {
    MetricsRegistry registry = new MetricsRegistry();
    _timedExecutor = new ScheduledThreadPoolExecutor(1);
    _service = new ThreadPoolExecutor(10, 10, 10, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
    _eventLoopGroup = new NioEventLoopGroup(10);
    _timer = new HashedWheelTimer();
    NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_");
    PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(_eventLoopGroup, _timer, clientMetrics);
    _pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(1, _maxActiveConnections, 300000, 10, rm, _timedExecutor, MoreExecutors.sameThreadExecutor(), registry);
    rm.setPool(_pool);
    _scatterGather = new ScatterGatherImpl(_pool, _service);
    for (AsyncReader r : _readerThreads) {
        r.start();
    }
}
Also used : MetricsRegistry(com.yammer.metrics.core.MetricsRegistry) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) PooledNettyClientResourceManager(com.linkedin.pinot.transport.netty.PooledNettyClientResourceManager) HashedWheelTimer(io.netty.util.HashedWheelTimer) NettyClientConnection(com.linkedin.pinot.transport.netty.NettyClientConnection) ScatterGatherImpl(com.linkedin.pinot.transport.scattergather.ScatterGatherImpl) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ServerInstance(com.linkedin.pinot.common.response.ServerInstance) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 100 with ScheduledThreadPoolExecutor

use of java.util.concurrent.ScheduledThreadPoolExecutor in project rest.li by linkedin.

the class LoadBalancerClientCli method getLoadBalancer.

public static SimpleLoadBalancer getLoadBalancer(ZKConnection zkclient, String zkserver, String d2path, String service) throws IOException, IllegalStateException, URISyntaxException, PropertyStoreException, ExecutionException, TimeoutException, InterruptedException {
    // zk stores
    String clstoreString = zkserver + ZKFSUtil.clusterPath(d2path);
    String scstoreString = zkserver + ZKFSUtil.servicePath(d2path);
    String uristoreString = zkserver + ZKFSUtil.uriPath(d2path);
    ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry = (ZooKeeperPermanentStore<ClusterProperties>) getStore(zkclient, clstoreString, new ClusterPropertiesJsonSerializer());
    ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry = (ZooKeeperPermanentStore<ServiceProperties>) getStore(zkclient, scstoreString, new ServicePropertiesJsonSerializer());
    ZooKeeperEphemeralStore<UriProperties> zkUriRegistry = (ZooKeeperEphemeralStore<UriProperties>) getEphemeralStore(zkclient, uristoreString, new UriPropertiesJsonSerializer(), new UriPropertiesMerger());
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("D2 PropertyEventExecutor"));
    PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<ServiceProperties>(executor, zkServiceRegistry);
    PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<UriProperties>(executor, zkUriRegistry);
    PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<ClusterProperties>(executor, zkClusterRegistry);
    Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
    loadBalancerStrategyFactories.put("random", new RandomLoadBalancerStrategyFactory());
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV2());
    loadBalancerStrategyFactories.put("degraderV2", new DegraderLoadBalancerStrategyFactoryV2());
    loadBalancerStrategyFactories.put("degraderV3", new DegraderLoadBalancerStrategyFactoryV3());
    loadBalancerStrategyFactories.put("degraderV2_1", new DegraderLoadBalancerStrategyFactoryV2_1());
    Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
    clientFactories.put("http", new HttpClientFactory());
    // create the state
    SimpleLoadBalancerState state = new SimpleLoadBalancerState(executor, uriBus, clusterBus, serviceBus, clientFactories, loadBalancerStrategyFactories, null, null, false);
    SimpleLoadBalancer balancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS);
    FutureCallback<None> callback = new FutureCallback<None>();
    balancer.start(callback);
    callback.get(5, TimeUnit.SECONDS);
    new JmxManager().registerLoadBalancer("balancer", balancer).registerLoadBalancerState("state", state).registerScheduledThreadPoolExecutor("executorService", executor).registerZooKeeperPermanentStore("zkClusterRegistry", zkClusterRegistry).registerZooKeeperPermanentStore("zkServiceRegistry", zkServiceRegistry).registerZooKeeperEphemeralStore("zkUriRegistry", zkUriRegistry);
    return balancer;
}
Also used : DegraderLoadBalancerStrategyFactoryV2(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV2) ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ZooKeeperPermanentStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperPermanentStore) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger) UriPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer) JmxManager(com.linkedin.d2.jmx.JmxManager) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) FutureCallback(com.linkedin.common.callback.FutureCallback) SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ZooKeeperEphemeralStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) DegraderLoadBalancerStrategyFactoryV2_1(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV2_1) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) None(com.linkedin.common.util.None)

Aggregations

ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)143 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)28 Test (org.junit.Test)25 ExecutorService (java.util.concurrent.ExecutorService)22 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)20 Test (org.testng.annotations.Test)15 ThreadFactory (java.util.concurrent.ThreadFactory)14 ArrayList (java.util.ArrayList)12 List (java.util.List)10 ServerInstance (com.linkedin.pinot.common.response.ServerInstance)8 NettyClientMetrics (com.linkedin.pinot.transport.metrics.NettyClientMetrics)8 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)8 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)8 HashedWheelTimer (io.netty.util.HashedWheelTimer)8 HashMap (java.util.HashMap)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)7 IOException (java.io.IOException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 NotificationManager (android.app.NotificationManager)6