Search in sources :

Example 16 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project drill by axbaretto.

the class TestEphemeralStore method testStoreRegistersDispatcherAndStartsItsClient.

/**
 * This test ensures store subscribes to receive events from underlying client. Dispatcher tests ensures listeners
 * are fired on incoming events. These two sets of tests ensure observer pattern in {@code TransientStore} works fine.
 */
@Test
public void testStoreRegistersDispatcherAndStartsItsClient() throws Exception {
    final StoreWithMockClient<String> store = new StoreWithMockClient<>(config, curator);
    final PathChildrenCache cache = Mockito.mock(PathChildrenCache.class);
    final ZookeeperClient client = store.getClient();
    Mockito.when(client.getCache()).thenReturn(cache);
    final ListenerContainer<PathChildrenCacheListener> container = Mockito.mock(ListenerContainer.class);
    Mockito.when(cache.getListenable()).thenReturn(container);
    store.start();
    Mockito.verify(container).addListener(store.dispatcher);
    Mockito.verify(client).start();
}
Also used : PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Test(org.junit.Test)

Example 17 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache 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 18 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache 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)

Example 19 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project x-pipe by ctripcorp.

the class DefaultKeeperElectorManager method observerShardLeader.

protected void observerShardLeader(final String clusterId, final String shardId) {
    logger.info("[observerShardLeader]{}, {}", clusterId, shardId);
    final String leaderLatchPath = MetaZkConfig.getKeeperLeaderLatchPath(clusterId, shardId);
    final CuratorFramework client = zkClient.get();
    if (currentMetaManager.watchIfNotWatched(clusterId, shardId)) {
        try {
            logger.info("[observerShardLeader][add PathChildrenCache]{}, {}", clusterId, shardId);
            PathChildrenCache pathChildrenCache = new PathChildrenCache(client, leaderLatchPath, true, XpipeThreadFactory.create(String.format("PathChildrenCache:%s-%s", clusterId, shardId)));
            pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

                private AtomicBoolean isFirst = new AtomicBoolean(true);

                @Override
                public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                    if (isFirst.get()) {
                        isFirst.set(false);
                        try {
                            logger.info("[childEvent][first sleep]{}", FIRST_PATH_CHILDREN_CACHE_SLEEP_MILLI);
                            TimeUnit.MILLISECONDS.sleep(FIRST_PATH_CHILDREN_CACHE_SLEEP_MILLI);
                        } catch (Exception e) {
                            logger.error("[childEvent]", e);
                        }
                    }
                    logger.info("[childEvent]{}, {}, {}, {}", clusterId, shardId, event.getType(), ZkUtils.toString(event.getData()));
                    updateShardLeader(leaderLatchPath, pathChildrenCache.getCurrentData(), clusterId, shardId);
                }
            });
            currentMetaManager.addResource(clusterId, shardId, new Releasable() {

                @Override
                public void release() throws Exception {
                    try {
                        logger.info("[release][release children cache]{}, {}", clusterId, shardId);
                        pathChildrenCache.close();
                    } catch (Exception e) {
                        logger.error(String.format("[release][release children cache]%s, %s", clusterId, shardId), e);
                    }
                }
            });
            pathChildrenCache.start();
        } catch (Exception e) {
            logger.error("[observerShardLeader]" + clusterId + " " + shardId, e);
        }
    } else {
        logger.warn("[observerShardLeader][already watched]{}, {}", clusterId, shardId);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) Releasable(com.ctrip.xpipe.api.lifecycle.Releasable)

Example 20 with PathChildrenCache

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.recipes.cache.PathChildrenCache in project x-pipe by ctripcorp.

the class TestPathChildren method testPathChildernAlreadyExist.

@Test
public void testPathChildernAlreadyExist() throws Exception {
    createPath(client, path, 0, count);
    CountDownLatch latch = new CountDownLatch(count);
    List<String> result = new LinkedList<>();
    PathChildrenCache pathChildrenCache = new PathChildrenCache(client, path, true);
    pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            try {
                sleep(50);
                logger.info("event");
                int realSize = pathChildrenCache.getCurrentData().size();
                if (count != realSize) {
                    String desc = String.format("expected:%d, real:%d", count, realSize);
                    result.add(desc);
                    logger.info("expected:{}, real:{}", count, realSize);
                }
            } finally {
                latch.countDown();
            }
        }
    });
    try {
        pathChildrenCache.start();
    } catch (Exception e) {
        logger.error("[start]", e);
    }
    latch.await();
    Assert.assertEquals(0, result.size());
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) Test(org.junit.Test) AbstractMetaServerTest(com.ctrip.xpipe.redis.meta.server.AbstractMetaServerTest)

Aggregations

PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)73 IOException (java.io.IOException)25 CuratorFramework (org.apache.curator.framework.CuratorFramework)21 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)20 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)14 KeeperException (org.apache.zookeeper.KeeperException)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ChildData (org.apache.curator.framework.recipes.cache.ChildData)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)6 Before (org.junit.Before)5 Test (org.junit.Test)5 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Slf4j (lombok.extern.slf4j.Slf4j)4 ZKPaths (org.apache.curator.utils.ZKPaths)4 Preconditions (com.google.common.base.Preconditions)3 StreamImpl (io.pravega.client.stream.impl.StreamImpl)3 CompletableFuture (java.util.concurrent.CompletableFuture)3