use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project hive by apache.
the class HS2ActivePassiveHARegistry method populateCache.
private void populateCache() throws IOException {
PathChildrenCache pcc = ensureInstancesCache(0);
populateCache(pcc, false);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache 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);
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project pravega by pravega.
the class ZooKeeperBucketService method startBucketChangeListener.
@Override
public void startBucketChangeListener() {
PathChildrenCacheListener bucketListener = (client, event) -> {
StreamImpl stream;
switch(event.getType()) {
case CHILD_ADDED:
case CHILD_UPDATED:
stream = bucketStore.getStreamFromPath(event.getData().getPath());
notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamAdded));
break;
case CHILD_REMOVED:
stream = bucketStore.getStreamFromPath(event.getData().getPath());
notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamRemoved));
break;
case CONNECTION_LOST:
notify(new StreamNotification(null, null, NotificationType.ConnectivityError));
break;
default:
log.warn("Received unknown event {} on bucket", event.getType(), getBucketId());
}
};
PathChildrenCache pathChildrenCache = cacheRef.updateAndGet(existing -> {
if (existing == null) {
PathChildrenCache cache = bucketStore.getBucketPathChildrenCache(getServiceType(), getBucketId());
cache.getListenable().addListener(bucketListener);
log.info("bucket {} change notification listener registered", getBucketId());
return cache;
} else {
return existing;
}
});
try {
pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
} catch (Exception e) {
log.error("{}: Starting listener on bucket {} threw exception", getServiceType(), getBucketId(), e);
throw Exceptions.sneakyThrow(e);
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project pravega by pravega.
the class ZooKeeperBucketManager method stopBucketOwnershipListener.
@Override
public void stopBucketOwnershipListener() {
PathChildrenCache pathChildrenCache = bucketOwnershipCacheMap.remove(getServiceType());
if (pathChildrenCache != null) {
try {
pathChildrenCache.clear();
pathChildrenCache.close();
} catch (IOException e) {
log.warn("unable to close listener for bucket ownership", e);
}
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project pravega by pravega.
the class ZooKeeperBucketManager method startBucketOwnershipListener.
@Override
public void startBucketOwnershipListener() {
PathChildrenCache pathChildrenCache = bucketOwnershipCacheMap.computeIfAbsent(getServiceType(), x -> bucketStore.getServiceOwnershipPathChildrenCache(getServiceType()));
PathChildrenCacheListener bucketListener = (client, event) -> {
switch(event.getType()) {
case CHILD_ADDED:
// no action required
break;
case CHILD_REMOVED:
int bucketId = Integer.parseInt(ZKPaths.getNodeFromPath(event.getData().getPath()));
RetryHelper.withIndefiniteRetriesAsync(() -> tryTakeOwnership(bucketId), e -> log.warn("{}: exception while attempting to take ownership for bucket {}: {}", getServiceType(), bucketId, e.getMessage()), getExecutor());
break;
case CONNECTION_LOST:
log.warn("{}: Received connectivity error", getServiceType());
break;
default:
log.warn("Received unknown event {} on bucket root {} ", event.getType(), getServiceType());
}
};
pathChildrenCache.getListenable().addListener(bucketListener);
log.info("bucket ownership listener registered on bucket root {}", getServiceType());
try {
pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
} catch (Exception e) {
log.error("Starting ownership listener for service {} threw exception", getServiceType(), e);
throw Exceptions.sneakyThrow(e);
}
}
Aggregations