use of 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);
}
}
use of 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);
}
use of 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);
}
}
use of 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());
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project drill by apache.
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 {
@SuppressWarnings("resource") 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);
@SuppressWarnings("unchecked") final MappingListenerManager<PathChildrenCacheListener, PathChildrenCacheListener> container = Mockito.mock(MappingListenerManager.class);
Mockito.when(cache.getListenable()).thenReturn(container);
store.start();
Mockito.verify(container).addListener(store.dispatcher);
Mockito.verify(client).start();
}
Aggregations