Search in sources :

Example 36 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestLeaderSelectorWithExecutor method test.

@Test
public void test() throws Exception {
    Timing timing = new Timing();
    LeaderSelector leaderSelector = null;
    CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(new ExponentialBackoffRetry(100, 3)).connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).build();
    try {
        client.start();
        MyLeaderSelectorListener listener = new MyLeaderSelectorListener();
        ExecutorService executorPool = Executors.newFixedThreadPool(20);
        leaderSelector = new LeaderSelector(client, "/test", threadFactory, executorPool, listener);
        leaderSelector.autoRequeue();
        leaderSelector.start();
        timing.sleepABit();
        Assert.assertEquals(listener.getLeaderCount(), 1);
    } finally {
        CloseableUtils.closeQuietly(leaderSelector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorService(java.util.concurrent.ExecutorService) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 37 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestInterProcessMutexBase method testReentrant2Threads.

@Test
public void testReentrant2Threads() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(100, 3));
    client.start();
    try {
        waitLatchForBar = new CountDownLatch(1);
        countLatchForBar = new CountDownLatch(1);
        final InterProcessLock mutex = makeLock(client);
        Executors.newSingleThreadExecutor().submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Assert.assertTrue(countLatchForBar.await(10, TimeUnit.SECONDS));
                try {
                    mutex.acquire(10, TimeUnit.SECONDS);
                    Assert.fail();
                } catch (Exception e) {
                // correct
                } finally {
                    waitLatchForBar.countDown();
                }
                return null;
            }
        });
        foo(mutex);
        Assert.assertFalse(mutex.isAcquiredInThisProcess());
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 38 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestInterProcessMutexBase method testContainerCleanup.

@Test
public void testContainerCleanup() throws Exception {
    if (!ZKPaths.hasContainerSupport()) {
        System.out.println("ZooKeeper version does not support Containers. Skipping test");
        return;
    }
    server.close();
    System.setProperty("container.checkIntervalMs", "10");
    try {
        server = new TestingServer();
        final int THREAD_QTY = 10;
        ExecutorService service = null;
        final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(100, 3));
        try {
            client.start();
            List<Future<Object>> threads = Lists.newArrayList();
            service = Executors.newCachedThreadPool();
            for (int i = 0; i < THREAD_QTY; ++i) {
                Future<Object> t = service.submit(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        InterProcessLock lock = makeLock(client);
                        lock.acquire();
                        try {
                            Thread.sleep(10);
                        } finally {
                            lock.release();
                        }
                        return null;
                    }
                });
                threads.add(t);
            }
            for (Future<Object> t : threads) {
                t.get();
            }
            new Timing().sleepABit();
            Assert.assertNull(client.checkExists().forPath(LOCK_BASE_PATH));
        } finally {
            if (service != null) {
                service.shutdownNow();
            }
            CloseableUtils.closeQuietly(client);
        }
    } finally {
        System.clearProperty("container.checkIntervalMs");
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 39 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestInterProcessMutexBase method testReentrant.

@Test
public void testReentrant() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(100, 3));
    client.start();
    try {
        InterProcessLock mutex = makeLock(client);
        foo(mutex);
        Assert.assertFalse(mutex.isAcquiredInThisProcess());
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Test(org.testng.annotations.Test)

Example 40 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestInterProcessMutexBase method testWaitingProcessKilledServer.

@Test
public void testWaitingProcessKilledServer() throws Exception {
    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    try {
        client.start();
        final CountDownLatch latch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        final AtomicBoolean isFirst = new AtomicBoolean(true);
        ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(Executors.newFixedThreadPool(2));
        for (int i = 0; i < 2; ++i) {
            service.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    InterProcessLock lock = makeLock(client);
                    lock.acquire();
                    try {
                        if (isFirst.compareAndSet(true, false)) {
                            timing.sleepABit();
                            server.stop();
                            Assert.assertTrue(timing.awaitLatch(latch));
                            server.restart();
                        }
                    } finally {
                        try {
                            lock.release();
                        } catch (Exception e) {
                        // ignore
                        }
                    }
                    return null;
                }
            });
        }
        for (int i = 0; i < 2; ++i) {
            service.take().get(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
        }
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Aggregations

ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)181 CuratorFramework (org.apache.curator.framework.CuratorFramework)107 RetryPolicy (org.apache.curator.RetryPolicy)46 Before (org.junit.Before)26 TestingCluster (org.apache.curator.test.TestingCluster)23 Test (org.testng.annotations.Test)23 IOException (java.io.IOException)18 TestingServer (org.apache.curator.test.TestingServer)18 Timing (org.apache.curator.test.Timing)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)12 ACLProvider (org.apache.curator.framework.api.ACLProvider)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 ConnectionState (org.apache.curator.framework.state.ConnectionState)11 ExecutorService (java.util.concurrent.ExecutorService)10 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)10 TestingServerStarter (io.pravega.test.common.TestingServerStarter)9 KeeperException (org.apache.zookeeper.KeeperException)8 HashMap (java.util.HashMap)7