Search in sources :

Example 41 with ExponentialBackoffRetry

use of 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 42 with ExponentialBackoffRetry

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

the class TestLockACLs method createClient.

private CuratorFramework createClient(ACLProvider provider) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.builder().namespace("ns").connectString(server.getConnectString()).retryPolicy(retryPolicy).aclProvider(provider).build();
    client.start();
    return client;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

Example 43 with ExponentialBackoffRetry

use of 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 44 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project VX-API-Gateway by EliMirren.

the class VxApiClusterZookeeperImpl method getConfig.

@Override
public void getConfig(Handler<AsyncResult<JsonObject>> event) {
    vertx.<JsonObject>executeBlocking(get -> {
        JsonObject retry = zkConf.getJsonObject("retry", new JsonObject());
        String hosts = zkConf.getString("zookeeperHosts", "127.0.0.1");
        int baseSleepTimeMs = retry.getInteger("initialSleepTime", 1000);
        int maxRetries = retry.getInteger("maxTimes", 5);
        int maxSleepMs = retry.getInteger("intervalTimes", 10000);
        int sessionTimeoutMs = zkConf.getInteger("sessionTimeout", 20000);
        int connectionTimeoutMs = zkConf.getInteger("connectTimeout", 3000);
        ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries, maxSleepMs);
        String vxApiConfPath = zkConf.getString("vxApiConfPath", "/io.vertx/vx.api.gateway/conf");
        CuratorFramework client = CuratorFrameworkFactory.newClient(hosts, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
        try {
            client.start();
            byte[] data = client.getData().forPath(vxApiConfPath);
            String jsons = new String(data);
            get.complete(new JsonObject(jsons));
        } catch (Exception e) {
            get.fail(e);
        } finally {
            client.close();
        }
    }, event);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) JsonObject(io.vertx.core.json.JsonObject)

Example 45 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project dble by actiontech.

the class DistributedSequenceHandler method initializeZK.

public void initializeZK(String zkAddress) {
    if (zkAddress == null) {
        throw new RuntimeException("please check zkURL is correct in config file \"myid.prperties\" .");
    }
    if (this.client != null) {
        this.client.close();
    }
    this.client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
    this.client.start();
    try {
        if (client.checkExists().forPath(INSTANCE_PATH) == null) {
            client.create().creatingParentContainersIfNeeded().forPath(INSTANCE_PATH);
        }
    } catch (Exception e) {
        throw new RuntimeException("create instance path " + INSTANCE_PATH + "error", e);
    }
    this.leaderSelector = new LeaderSelector(client, KVPathUtil.getSequencesLeaderPath(), this);
    this.leaderSelector.autoRequeue();
    this.leaderSelector.start();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                while (leaderSelector.getLeader() == null) {
                    Thread.currentThread().yield();
                }
                if (!leaderSelector.hasLeadership()) {
                    isLeader = false;
                    if (slavePath != null && client.checkExists().forPath(slavePath) != null) {
                        return;
                    }
                    slavePath = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(PATH.concat("/instance/node"), "ready".getBytes());
                    while ("ready".equals(new String(client.getData().forPath(slavePath)))) {
                        Thread.currentThread().yield();
                    }
                    instanceId = Long.parseLong(new String(client.getData().forPath(slavePath)));
                    ready = true;
                }
            } catch (Exception e) {
                LOGGER.info("Caught exception while handling zk!", e);
            }
        }
    };
    long selfCheckPeriod = 10L;
    timerExecutor.scheduleAtFixedRate(runnable, 1L, selfCheckPeriod, TimeUnit.SECONDS);
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) IOException(java.io.IOException) CancelLeadershipException(org.apache.curator.framework.recipes.leader.CancelLeadershipException)

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