use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener 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 {
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();
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener in project exhibitor by soabase.
the class TestZookeeperConfigProvider method testConcurrentModification.
@Test
public void testConcurrentModification() throws Exception {
ZookeeperConfigProvider config1 = new ZookeeperConfigProvider(client, "/foo", new Properties(), "foo");
ZookeeperConfigProvider config2 = new ZookeeperConfigProvider(client, "/foo", new Properties(), "foo");
try {
config1.start();
config2.start();
final Semaphore cacheUpdate2 = new Semaphore(0);
config2.getPathChildrenCache().getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
cacheUpdate2.release();
}
});
Properties properties = new Properties();
properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.ZOO_CFG_EXTRA, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), "1,2,3");
LoadedInstanceConfig loaded1 = config1.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), -1);
Assert.assertTrue(timing.acquireSemaphore(cacheUpdate2));
timing.sleepABit();
LoadedInstanceConfig loaded2 = config2.loadConfig();
Assert.assertEquals("1,2,3", loaded2.getConfig().getRootConfig().getString(StringConfigs.ZOO_CFG_EXTRA));
properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.ZOO_CFG_EXTRA, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), "4,5,6");
config2.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), loaded2.getVersion());
Assert.assertNull(config1.storeConfig(new PropertyBasedInstanceConfig(properties, new Properties()), loaded1.getVersion()));
LoadedInstanceConfig newLoaded1 = config1.loadConfig();
Assert.assertNotEquals(loaded1.getVersion(), newLoaded1.getVersion());
} finally {
CloseableUtils.closeQuietly(config2);
CloseableUtils.closeQuietly(config1);
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener in project pravega by pravega.
the class ZKStreamMetadataStore method registerBucketOwnershipListener.
@Override
@SneakyThrows
public void registerBucketOwnershipListener(BucketOwnershipListener listener) {
Preconditions.checkNotNull(listener);
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()));
listener.notify(new BucketNotification(bucketId, BucketNotification.NotificationType.BucketAvailable));
break;
case CONNECTION_LOST:
listener.notify(new BucketNotification(Integer.MIN_VALUE, BucketNotification.NotificationType.ConnectivityError));
break;
default:
log.warn("Received unknown event {}", event.getType());
}
};
bucketOwnershipCacheRef.compareAndSet(null, new PathChildrenCache(storeHelper.getClient(), ZKStoreHelper.BUCKET_OWNERSHIP_PATH, true));
bucketOwnershipCacheRef.get().getListenable().addListener(bucketListener);
bucketOwnershipCacheRef.get().start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
log.info("bucket ownership listener registered");
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener in project pravega by pravega.
the class ZKStreamMetadataStore method registerBucketChangeListener.
@Override
@SneakyThrows
public void registerBucketChangeListener(int bucket, BucketChangeListener listener) {
Preconditions.checkNotNull(listener);
PathChildrenCacheListener bucketListener = (client, event) -> {
StreamImpl stream;
switch(event.getType()) {
case CHILD_ADDED:
stream = getStreamFromPath(event.getData().getPath());
listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamAdded));
break;
case CHILD_REMOVED:
stream = getStreamFromPath(event.getData().getPath());
listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamRemoved));
break;
case CHILD_UPDATED:
stream = getStreamFromPath(event.getData().getPath());
listener.notify(new StreamNotification(stream.getScope(), stream.getStreamName(), NotificationType.StreamUpdated));
break;
case CONNECTION_LOST:
listener.notify(new StreamNotification(null, null, NotificationType.ConnectivityError));
break;
default:
log.warn("Received unknown event {} on bucket", event.getType(), bucket);
}
};
String bucketRoot = String.format(ZKStoreHelper.BUCKET_PATH, bucket);
bucketCacheMap.put(bucket, new PathChildrenCache(storeHelper.getClient(), bucketRoot, true));
PathChildrenCache pathChildrenCache = bucketCacheMap.get(bucket);
pathChildrenCache.getListenable().addListener(bucketListener);
pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
log.info("bucket {} change notification listener registered", bucket);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheListener in project druid by druid-io.
the class ZkCoordinator method start.
@LifecycleStart
public void start() throws IOException {
synchronized (lock) {
if (started) {
return;
}
log.info("Starting zkCoordinator for server[%s]", me.getName());
final String loadQueueLocation = ZKPaths.makePath(zkPaths.getLoadQueuePath(), me.getName());
final String servedSegmentsLocation = ZKPaths.makePath(zkPaths.getServedSegmentsPath(), me.getName());
final String liveSegmentsLocation = ZKPaths.makePath(zkPaths.getLiveSegmentsPath(), me.getName());
loadQueueCache = new PathChildrenCache(curator, loadQueueLocation, true, true, Execs.multiThreaded(config.getNumLoadingThreads(), "ZkCoordinator-%s"));
try {
curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient());
curator.newNamespaceAwareEnsurePath(servedSegmentsLocation).ensure(curator.getZookeeperClient());
curator.newNamespaceAwareEnsurePath(liveSegmentsLocation).ensure(curator.getZookeeperClient());
loadLocalCache();
loadQueueCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
final ChildData child = event.getData();
switch(event.getType()) {
case CHILD_ADDED:
final String path = child.getPath();
final DataSegmentChangeRequest request = jsonMapper.readValue(child.getData(), DataSegmentChangeRequest.class);
log.info("New request[%s] with zNode[%s].", request.asString(), path);
try {
request.go(getDataSegmentChangeHandler(), new DataSegmentChangeCallback() {
boolean hasRun = false;
@Override
public void execute() {
try {
if (!hasRun) {
curator.delete().guaranteed().forPath(path);
log.info("Completed request [%s]", request.asString());
hasRun = true;
}
} catch (Exception e) {
try {
curator.delete().guaranteed().forPath(path);
} catch (Exception e1) {
log.error(e1, "Failed to delete zNode[%s], but ignoring exception.", path);
}
log.error(e, "Exception while removing zNode[%s]", path);
throw Throwables.propagate(e);
}
}
});
} catch (Exception e) {
try {
curator.delete().guaranteed().forPath(path);
} catch (Exception e1) {
log.error(e1, "Failed to delete zNode[%s], but ignoring exception.", path);
}
log.makeAlert(e, "Segment load/unload: uncaught exception.").addData("node", path).addData("nodeProperties", request).emit();
}
break;
case CHILD_REMOVED:
log.info("zNode[%s] was removed", event.getData().getPath());
break;
default:
log.info("Ignoring event[%s]", event);
}
}
});
loadQueueCache.start();
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.class);
throw Throwables.propagate(e);
}
started = true;
}
}
Aggregations