Search in sources :

Example 1 with RetryForever

use of org.apache.curator.retry.RetryForever in project thingsboard by thingsboard.

the class ZkDiscoveryService method init.

@PostConstruct
public void init() {
    log.info("Initializing...");
    Assert.hasLength(zkUrl, MiscUtils.missingProperty("zk.url"));
    Assert.notNull(zkRetryInterval, MiscUtils.missingProperty("zk.retry_interval_ms"));
    Assert.notNull(zkConnectionTimeout, MiscUtils.missingProperty("zk.connection_timeout_ms"));
    Assert.notNull(zkSessionTimeout, MiscUtils.missingProperty("zk.session_timeout_ms"));
    log.info("Initializing discovery service using ZK connect string: {}", zkUrl);
    zkNodesDir = zkDir + "/nodes";
    try {
        client = CuratorFrameworkFactory.newClient(zkUrl, zkSessionTimeout, zkConnectionTimeout, new RetryForever(zkRetryInterval));
        client.start();
        client.blockUntilConnected();
        cache = new PathChildrenCache(client, zkNodesDir, true);
        cache.getListenable().addListener(this);
        cache.start();
    } catch (Exception e) {
        log.error("Failed to connect to ZK: {}", e.getMessage(), e);
        CloseableUtils.closeQuietly(client);
        throw new RuntimeException(e);
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) RetryForever(org.apache.curator.retry.RetryForever) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) PostConstruct(javax.annotation.PostConstruct)

Example 2 with RetryForever

use of org.apache.curator.retry.RetryForever in project druid by alibaba.

the class ZookeeperNodeListener method init.

/**
 * Init a PathChildrenCache to watch the given path.
 */
@Override
public void init() {
    checkParameters();
    super.init();
    if (client == null) {
        client = CuratorFrameworkFactory.builder().canBeReadOnly(true).connectionTimeoutMs(5000).connectString(zkConnectString).retryPolicy(new RetryForever(10000)).sessionTimeoutMs(30000).build();
        client.start();
        privateZkClient = true;
    }
    cache = new PathChildrenCache(client, path, true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            try {
                LOG.info("Receive an event: " + event.getType());
                lock.lock();
                PathChildrenCacheEvent.Type eventType = event.getType();
                switch(eventType) {
                    case CHILD_REMOVED:
                        updateSingleNode(event, NodeEventTypeEnum.DELETE);
                        break;
                    case CHILD_ADDED:
                        updateSingleNode(event, NodeEventTypeEnum.ADD);
                        break;
                    case CONNECTION_RECONNECTED:
                        refreshAllNodes();
                        break;
                    default:
                        // CHILD_UPDATED
                        // INITIALIZED
                        // CONNECTION_LOST
                        // CONNECTION_SUSPENDED
                        LOG.info("Received a PathChildrenCacheEvent, IGNORE it: " + event);
                }
            } finally {
                lock.unlock();
                LOG.info("Finish the processing of event: " + event.getType());
            }
        }
    });
    try {
        // Use BUILD_INITIAL_CACHE to force build cache in the current Thread.
        // We don't use POST_INITIALIZED_EVENT, so there's no INITIALIZED event.
        cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    } catch (Exception e) {
        LOG.error("Can't start PathChildrenCache", e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) RetryForever(org.apache.curator.retry.RetryForever) IOException(java.io.IOException) DruidRuntimeException(com.alibaba.druid.DruidRuntimeException)

Example 3 with RetryForever

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

the class TestFrameworkEdges method testCreateContainersForBadConnect.

@Test
public void testCreateContainersForBadConnect() throws Exception {
    final int serverPort = server.getPort();
    server.close();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 1000, 1000, new RetryForever(100));
    try {
        new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    server = new TestingServer(serverPort, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        client.start();
        client.createContainers("/this/does/not/exist");
        Assert.assertNotNull(client.checkExists().forPath("/this/does/not/exist"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryForever(org.apache.curator.retry.RetryForever) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 4 with RetryForever

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

the class TestRetryLoop method testRetryForever.

@Test
public void testRetryForever() throws Exception {
    int retryIntervalMs = 1;
    RetrySleeper sleeper = Mockito.mock(RetrySleeper.class);
    RetryForever retryForever = new RetryForever(retryIntervalMs);
    for (int i = 0; i < 10; i++) {
        boolean allowed = retryForever.allowRetry(i, 0, sleeper);
        Assert.assertTrue(allowed);
        Mockito.verify(sleeper, times(i + 1)).sleepFor(retryIntervalMs, TimeUnit.MILLISECONDS);
    }
}
Also used : RetryForever(org.apache.curator.retry.RetryForever) Test(org.testng.annotations.Test)

Aggregations

RetryForever (org.apache.curator.retry.RetryForever)4 IOException (java.io.IOException)2 CuratorFramework (org.apache.curator.framework.CuratorFramework)2 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)2 Test (org.testng.annotations.Test)2 DruidRuntimeException (com.alibaba.druid.DruidRuntimeException)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 PostConstruct (javax.annotation.PostConstruct)1 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)1 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)1 TestingServer (org.apache.curator.test.TestingServer)1 KeeperException (org.apache.zookeeper.KeeperException)1