Search in sources :

Example 46 with ExponentialBackoffRetry

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

the class TestRetryLoop method testExponentialBackoffRetryLimit.

@Test
public void testExponentialBackoffRetryLimit() {
    RetrySleeper sleeper = new RetrySleeper() {

        @Override
        public void sleepFor(long time, TimeUnit unit) throws InterruptedException {
            Assert.assertTrue(unit.toMillis(time) <= 100);
        }
    };
    ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1, Integer.MAX_VALUE, 100);
    for (int i = 0; i >= 0; ++i) {
        retry.allowRetry(i, 0, sleeper);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) TimeUnit(java.util.concurrent.TimeUnit) Test(org.testng.annotations.Test)

Example 47 with ExponentialBackoffRetry

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

the class TestExhibitorEnsembleProvider method testSimple.

@Test
public void testSimple() throws Exception {
    Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
    ExhibitorRestClient mockRestClient = new ExhibitorRestClient() {

        @Override
        public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception {
            return "count=1&port=" + server.getPort() + "&server0=localhost";
        }
    };
    ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
    provider.pollForInitialEnsemble();
    Timing timing = new Timing();
    CuratorZookeeperClient client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new ExponentialBackoffRetry(timing.milliseconds(), 3));
    client.start();
    try {
        client.blockUntilConnectedOrTimedOut();
        client.getZooKeeper().exists("/", false);
    } catch (Exception e) {
        Assert.fail("provider.getConnectionString(): " + provider.getConnectionString() + " server.getPort(): " + server.getPort(), e);
    } finally {
        client.close();
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorZookeeperClient(org.apache.curator.CuratorZookeeperClient) Timing(org.apache.curator.test.Timing) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 48 with ExponentialBackoffRetry

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

the class ZkConnection method start.

// 仅供内部测试使用
public static void start(String connectionStr) {
    synchronized (zkConnectionStartStopLock) {
        if (connected.get()) {
            LOG.info("zkConnection已经启动,不再重复启动");
            return;
        }
        try {
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            client = CuratorFrameworkFactory.newClient(connectionStr, retryPolicy);
            client.start();
            LOG.info("阻塞直到与zookeeper连接建立完毕!");
            client.blockUntilConnected();
        } catch (Throwable e) {
            LOG.error(e);
        } finally {
            connected.set(true);
        }
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

Example 49 with ExponentialBackoffRetry

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

the class DiscoveryExample method main.

public static void main(String[] args) throws Exception {
    // This method is scaffolding to get the example up and running
    TestingServer server = new TestingServer();
    CuratorFramework client = null;
    ServiceDiscovery<InstanceDetails> serviceDiscovery = null;
    Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap();
    try {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
        client.start();
        JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class);
        serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(PATH).serializer(serializer).build();
        serviceDiscovery.start();
        processCommands(serviceDiscovery, providers, client);
    } finally {
        for (ServiceProvider<InstanceDetails> cache : providers.values()) {
            CloseableUtils.closeQuietly(cache);
        }
        CloseableUtils.closeQuietly(serviceDiscovery);
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(server);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) JsonInstanceSerializer(org.apache.curator.x.discovery.details.JsonInstanceSerializer) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ServiceProvider(org.apache.curator.x.discovery.ServiceProvider)

Example 50 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project phoenix by apache.

the class LoadBalancer method start.

private void start() throws Exception {
    curaFramework = CuratorFrameworkFactory.newClient(getZkConnectString(), new ExponentialBackoffRetry(1000, 3));
    curaFramework.start();
    curaFramework.setACL().withACL(CONFIG.getAcls());
    connectionStateListener = getConnectionStateListener();
    curaFramework.getConnectionStateListenable().addListener(connectionStateListener);
    unhandledErrorListener = getUnhandledErrorListener();
    curaFramework.getUnhandledErrorListenable().addListener(unhandledErrorListener);
    cache = new PathChildrenCache(curaFramework, CONFIG.getParentPath(), true);
    cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    closeAbles.add(cache);
    closeAbles.add(curaFramework);
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache)

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