Search in sources :

Example 41 with RetryPolicy

use of org.apache.curator.RetryPolicy in project BRFS by zhangnianli.

the class Test method main.

public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient(zk_address, retryPolicy);
    client.start();
    StorageNameManager snm = new DefaultStorageNameManager(client.usingNamespace("test"));
    snm.start();
    // Thread.sleep(2000);
    StorageNameNode node = snm.createStorageName("sn_1", 2, 100);
    System.out.println("create node--" + node);
    StorageNameNode node2 = snm.createStorageName("sn_2", 2, 100);
    System.out.println("create node--" + node2);
    System.out.println("find node--" + snm.findStorageName("sn_1"));
    // Thread.sleep(1000);
    System.out.println("delete--" + snm.removeStorageName("sn_1"));
    // System.out.println("find node--" + snm.findStorageName("sn_1"));
    System.out.println("update--" + snm.updateStorageName("sn_2", 110));
    synchronized (client) {
        client.wait();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

Example 42 with RetryPolicy

use of org.apache.curator.RetryPolicy in project nifi by apache.

the class Cluster method createCuratorClient.

public CuratorFramework createCuratorClient() {
    final RetryPolicy retryPolicy = new RetryNTimes(20, 500);
    final CuratorFramework curatorClient = CuratorFrameworkFactory.builder().connectString(getZooKeeperConnectString()).sessionTimeoutMs(3000).connectionTimeoutMs(3000).retryPolicy(retryPolicy).defaultData(new byte[0]).build();
    curatorClient.start();
    return curatorClient;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy)

Example 43 with RetryPolicy

use of org.apache.curator.RetryPolicy in project metron by apache.

the class ProfileBuilderBolt method setupZookeeper.

private void setupZookeeper() {
    try {
        if (zookeeperClient == null) {
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            zookeeperClient = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
        }
        zookeeperClient.start();
        // this is temporary to ensure that any validation passes. the individual bolt
        // will reinitialize stellar to dynamically pull from zookeeper.
        ConfigurationsUtils.setupStellarStatically(zookeeperClient);
        if (zookeeperCache == null) {
            ConfigurationsUpdater<ProfilerConfigurations> updater = createUpdater();
            SimpleEventListener listener = new SimpleEventListener.Builder().with(updater::update, TreeCacheEvent.Type.NODE_ADDED, TreeCacheEvent.Type.NODE_UPDATED).with(updater::delete, TreeCacheEvent.Type.NODE_REMOVED).build();
            zookeeperCache = new ZKCache.Builder().withClient(zookeeperClient).withListener(listener).withRoot(Constants.ZOOKEEPER_TOPOLOGY_ROOT).build();
            updater.forceUpdate(zookeeperClient);
            zookeeperCache.start();
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : SimpleEventListener(org.apache.metron.zookeeper.SimpleEventListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ZKCache(org.apache.metron.zookeeper.ZKCache) RetryPolicy(org.apache.curator.RetryPolicy) ProfilerConfigurations(org.apache.metron.common.configuration.profiler.ProfilerConfigurations)

Example 44 with RetryPolicy

use of org.apache.curator.RetryPolicy in project metron by apache.

the class ConfiguredBolt method prepCache.

protected void prepCache() {
    try {
        if (client == null) {
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
        }
        client.start();
        // this is temporary to ensure that any validation passes.
        // The individual bolt will reinitialize stellar to dynamically pull from
        // zookeeper.
        ConfigurationsUtils.setupStellarStatically(client);
        if (cache == null) {
            ConfigurationsUpdater<CONFIG_T> updater = createUpdater();
            SimpleEventListener listener = new SimpleEventListener.Builder().with(updater::update, TreeCacheEvent.Type.NODE_ADDED, TreeCacheEvent.Type.NODE_UPDATED).with(updater::delete, TreeCacheEvent.Type.NODE_REMOVED).build();
            cache = new ZKCache.Builder().withClient(client).withListener(listener).withRoot(Constants.ZOOKEEPER_TOPOLOGY_ROOT).build();
            updater.forceUpdate(client);
            cache.start();
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : SimpleEventListener(org.apache.metron.zookeeper.SimpleEventListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ZKCache(org.apache.metron.zookeeper.ZKCache) RetryPolicy(org.apache.curator.RetryPolicy)

Example 45 with RetryPolicy

use of org.apache.curator.RetryPolicy in project xian by happyyangyuan.

the class TestFrameworkEdges method testRetry.

@Test
public void testRetry() throws Exception {
    final int MAX_RETRIES = 3;
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(10));
    client.start();
    try {
        final AtomicInteger retries = new AtomicInteger(0);
        final Semaphore semaphore = new Semaphore(0);
        RetryPolicy policy = new RetryPolicy() {

            @Override
            public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper) {
                semaphore.release();
                if (retries.incrementAndGet() == MAX_RETRIES) {
                    try {
                        server.restart();
                    } catch (Exception e) {
                        throw new Error(e);
                    }
                }
                try {
                    sleeper.sleepFor(100, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                return true;
            }
        };
        client.getZookeeperClient().setRetryPolicy(policy);
        server.stop();
        // test foreground retry
        client.checkExists().forPath("/hey");
        Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, timing.forWaiting().seconds(), TimeUnit.SECONDS), "Remaining leases: " + semaphore.availablePermits());
        // make sure we're reconnected
        client.getZookeeperClient().setRetryPolicy(new RetryOneTime(100));
        client.checkExists().forPath("/hey");
        client.getZookeeperClient().setRetryPolicy(policy);
        semaphore.drainPermits();
        retries.set(0);
        server.stop();
        // test background retry
        client.checkExists().inBackground().forPath("/hey");
        Assert.assertTrue(semaphore.tryAcquire(MAX_RETRIES, timing.forWaiting().seconds(), TimeUnit.SECONDS), "Remaining leases: " + semaphore.availablePermits());
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) RetrySleeper(org.apache.curator.RetrySleeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Semaphore(java.util.concurrent.Semaphore) RetryPolicy(org.apache.curator.RetryPolicy) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Aggregations

RetryPolicy (org.apache.curator.RetryPolicy)69 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)47 CuratorFramework (org.apache.curator.framework.CuratorFramework)42 RetryNTimes (org.apache.curator.retry.RetryNTimes)12 IOException (java.io.IOException)10 ACLProvider (org.apache.curator.framework.api.ACLProvider)7 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)6 TestingServer (org.apache.curator.test.TestingServer)5 DefaultZooKeeperClient (com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient)4 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)4 SimpleEventListener (org.apache.metron.zookeeper.SimpleEventListener)4 ZKCache (org.apache.metron.zookeeper.ZKCache)4 ACL (org.apache.zookeeper.data.ACL)4 Before (org.junit.Before)4 DefaultServiceManager (com.bonree.brfs.common.service.impl.DefaultServiceManager)3 AuthInfo (org.apache.curator.framework.AuthInfo)3 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)3 Test (org.junit.Test)3 Service (com.bonree.brfs.common.service.Service)2 ServiceManager (com.bonree.brfs.common.service.ServiceManager)2