Search in sources :

Example 86 with ExponentialBackoffRetry

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

the class TestDistributedAtomicLong method doSimulation.

private void doSimulation(int executionQty, SummaryStatistics timingStats, AtomicInteger optimisticTries, AtomicInteger promotedLockTries, AtomicInteger failures, AtomicInteger errors) throws Exception {
    Random random = new Random();
    long previousValue = -1;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3, 3);
        PromotedToLock.Builder builder = PromotedToLock.builder().lockPath("/lock").retryPolicy(retryPolicy);
        DistributedAtomicLong dal = new DistributedAtomicLong(client, "/counter", retryPolicy, builder.build());
        for (int i = 0; i < executionQty; ++i) {
            Thread.sleep(random.nextInt(10));
            long start = System.currentTimeMillis();
            AtomicValue<Long> value = dal.increment();
            long elapsed = System.currentTimeMillis() - start;
            timingStats.addValue(elapsed);
            if (value.succeeded()) {
                if (value.postValue() <= previousValue) {
                    errors.incrementAndGet();
                }
                previousValue = value.postValue();
            } else {
                failures.incrementAndGet();
            }
            optimisticTries.addAndGet(value.getStats().getOptimisticTries());
            promotedLockTries.addAndGet(value.getStats().getPromotedLockTries());
        }
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Random(java.util.Random) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

Example 87 with ExponentialBackoffRetry

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

the class TestInterProcessSemaphoreCluster method testKilledServerWithEnsembleProvider.

@Test
public void testKilledServerWithEnsembleProvider() throws Exception {
    final int CLIENT_QTY = 10;
    final Timing timing = new Timing();
    final String PATH = "/foo/bar/lock";
    ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    TestingCluster cluster = new TestingCluster(3);
    try {
        cluster.start();
        final AtomicReference<String> connectionString = new AtomicReference<String>(cluster.getConnectString());
        final EnsembleProvider provider = new EnsembleProvider() {

            @Override
            public void start() throws Exception {
            }

            @Override
            public String getConnectionString() {
                return connectionString.get();
            }

            @Override
            public void close() throws IOException {
            }
        };
        final Semaphore acquiredSemaphore = new Semaphore(0);
        final AtomicInteger acquireCount = new AtomicInteger(0);
        final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY);
        for (int i = 0; i < CLIENT_QTY; ++i) {
            completionService.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
                    try {
                        final Semaphore suspendedSemaphore = new Semaphore(0);
                        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

                            @Override
                            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                                if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
                                    suspendedLatch.countDown();
                                    suspendedSemaphore.release();
                                }
                            }
                        });
                        client.start();
                        InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1);
                        while (!Thread.currentThread().isInterrupted()) {
                            Lease lease = null;
                            try {
                                lease = semaphore.acquire();
                                acquiredSemaphore.release();
                                acquireCount.incrementAndGet();
                                suspendedSemaphore.acquire();
                            } catch (Exception e) {
                            // just retry
                            } finally {
                                if (lease != null) {
                                    acquireCount.decrementAndGet();
                                    CloseableUtils.closeQuietly(lease);
                                }
                            }
                        }
                    } finally {
                        CloseableUtils.closeQuietly(client);
                    }
                    return null;
                }
            });
        }
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        Assert.assertEquals(1, acquireCount.get());
        cluster.close();
        timing.awaitLatch(suspendedLatch);
        timing.forWaiting().sleepABit();
        Assert.assertEquals(0, acquireCount.get());
        cluster = new TestingCluster(3);
        cluster.start();
        connectionString.set(cluster.getConnectString());
        timing.forWaiting().sleepABit();
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(1, acquireCount.get());
    } finally {
        executorService.shutdown();
        executorService.awaitTermination(10, TimeUnit.SECONDS);
        executorService.shutdownNow();
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) 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)

Example 88 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.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 89 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.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 90 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.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)

Aggregations

ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)189 CuratorFramework (org.apache.curator.framework.CuratorFramework)113 RetryPolicy (org.apache.curator.RetryPolicy)46 Before (org.junit.Before)31 TestingCluster (org.apache.curator.test.TestingCluster)28 Test (org.testng.annotations.Test)23 TestingServer (org.apache.curator.test.TestingServer)19 IOException (java.io.IOException)18 Timing (org.apache.curator.test.Timing)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 ArrayList (java.util.ArrayList)14 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)12 ACLProvider (org.apache.curator.framework.api.ACLProvider)12 Test (org.junit.Test)12 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 HashMap (java.util.HashMap)8 KeeperException (org.apache.zookeeper.KeeperException)8