use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project druid by druid-io.
the class Announcer method announce.
/**
* Announces the provided bytes at the given path. Announcement means that it will create an ephemeral node
* and monitor it to make sure that it always exists until it is unannounced or this object is closed.
*
* @param path The path to announce at
* @param bytes The payload to announce
* @param removeParentIfCreated remove parent of "path" if we had created that parent
*/
public void announce(String path, byte[] bytes, boolean removeParentIfCreated) {
synchronized (toAnnounce) {
if (!started) {
toAnnounce.add(new Announceable(path, bytes, removeParentIfCreated));
return;
}
}
final ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path);
final String parentPath = pathAndNode.getPath();
boolean buildParentPath = false;
ConcurrentMap<String, byte[]> subPaths = announcements.get(parentPath);
if (subPaths == null) {
try {
if (curator.checkExists().forPath(parentPath) == null) {
buildParentPath = true;
}
} catch (Exception e) {
log.debug(e, "Problem checking if the parent existed, ignoring.");
}
// I don't have a watcher on this path yet, create a Map and start watching.
announcements.putIfAbsent(parentPath, new MapMaker().<String, byte[]>makeMap());
// Guaranteed to be non-null, but might be a map put in there by another thread.
final ConcurrentMap<String, byte[]> finalSubPaths = announcements.get(parentPath);
// Synchronize to make sure that I only create a listener once.
synchronized (finalSubPaths) {
if (!listeners.containsKey(parentPath)) {
final PathChildrenCache cache = factory.make(curator, parentPath);
cache.getListenable().addListener(new PathChildrenCacheListener() {
private final AtomicReference<Set<String>> pathsLost = new AtomicReference<Set<String>>(null);
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
log.debug("Path[%s] got event[%s]", parentPath, event);
switch(event.getType()) {
case CHILD_REMOVED:
final ChildData child = event.getData();
final ZKPaths.PathAndNode childPath = ZKPaths.getPathAndNode(child.getPath());
final byte[] value = finalSubPaths.get(childPath.getNode());
if (value != null) {
log.info("Node[%s] dropped, reinstating.", child.getPath());
createAnnouncement(child.getPath(), value);
}
break;
case CONNECTION_LOST:
// Lost connection, which means session is broken, take inventory of what has been seen.
// This is to protect from a race condition in which the ephemeral node could have been
// created but not actually seen by the PathChildrenCache, which means that it won't know
// that it disappeared and thus will not generate a CHILD_REMOVED event for us. Under normal
// circumstances, this can only happen upon connection loss; but technically if you have
// an adversary in the system, they could also delete the ephemeral node before the cache sees
// it. This does not protect from that case, so don't have adversaries.
Set<String> pathsToReinstate = Sets.newHashSet();
for (String node : finalSubPaths.keySet()) {
String path = ZKPaths.makePath(parentPath, node);
log.info("Node[%s] is added to reinstate.", path);
pathsToReinstate.add(path);
}
if (!pathsToReinstate.isEmpty() && !pathsLost.compareAndSet(null, pathsToReinstate)) {
log.info("Already had a pathsLost set!?[%s]", parentPath);
}
break;
case CONNECTION_RECONNECTED:
final Set<String> thePathsLost = pathsLost.getAndSet(null);
if (thePathsLost != null) {
for (String path : thePathsLost) {
log.info("Reinstating [%s]", path);
final ZKPaths.PathAndNode split = ZKPaths.getPathAndNode(path);
createAnnouncement(path, announcements.get(split.getPath()).get(split.getNode()));
}
}
break;
}
}
});
synchronized (toAnnounce) {
if (started) {
if (buildParentPath) {
createPath(parentPath, removeParentIfCreated);
}
startCache(cache);
listeners.put(parentPath, cache);
}
}
}
}
subPaths = finalSubPaths;
}
boolean created = false;
synchronized (toAnnounce) {
if (started) {
byte[] oldBytes = subPaths.putIfAbsent(pathAndNode.getNode(), bytes);
if (oldBytes == null) {
created = true;
} else if (!Arrays.equals(oldBytes, bytes)) {
throw new IAE("Cannot reannounce different values under the same path");
}
}
}
if (created) {
try {
createAnnouncement(path, bytes);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project drill by axbaretto.
the class TestEventDispatcher method testDispatcherPropagatesEvents.
@Test
public void testDispatcherPropagatesEvents() throws Exception {
final PathChildrenCacheEvent.Type[] types = new PathChildrenCacheEvent.Type[] { PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_REMOVED, PathChildrenCacheEvent.Type.CHILD_UPDATED };
for (final PathChildrenCacheEvent.Type type : types) {
dispatcher.childEvent(null, new PathChildrenCacheEvent(type, child));
final TransientStoreEvent event = TransientStoreEvent.of(EventDispatcher.MAPPINGS.get(type), key, value);
Mockito.verify(store).fireListeners(event);
}
Assert.assertEquals("Number of event types that dispatcher can handle is different", types.length, EventDispatcher.MAPPINGS.size());
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project fabric8 by jboss-fuse.
the class ZkDataStoreImpl method childEvent.
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (isValid()) {
// guard against events with null data or path
String path;
ChildData childData = event.getData();
if (childData != null) {
path = childData.getPath();
} else {
path = null;
}
byte[] data = null;
if (childData != null) {
data = childData.getData();
}
PathChildrenCacheEvent.Type type = event.getType();
switch(type) {
case CHILD_ADDED:
case CHILD_REMOVED:
case CHILD_UPDATED:
case INITIALIZED:
if (shouldRunCallbacks(type, path) && !path.contains("password")) {
String s = data != null ? new String(data, "UTF-8") : "";
LOGGER.info("Event {} detected on {} with data {}. Sending notification.", type.name(), path, s);
fireChangeNotifications();
}
break;
}
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project twister2 by DSC-SPIDAL.
the class ZKWorkerController method addEventsChildrenCacheListener.
/**
* listen for additions to the job events directory in zk server
* @param cache
*/
private void addEventsChildrenCacheListener(PathChildrenCache cache) {
PathChildrenCacheListener listener = new PathChildrenCacheListener() {
public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) {
switch(event.getType()) {
case CHILD_ADDED:
eventPublished(event);
break;
default:
}
}
};
cache.getListenable().addListener(listener);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project twister2 by DSC-SPIDAL.
the class ZKMasterController method addEphemChildrenCacheListener.
/**
* create the listener for ephemeral worker znodes to determine worker joins and failures
*/
private void addEphemChildrenCacheListener(PathChildrenCache cache) {
PathChildrenCacheListener listener = new PathChildrenCacheListener() {
public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) {
switch(event.getType()) {
case CHILD_ADDED:
workerZnodeAdded(event);
break;
case CHILD_REMOVED:
workerZnodeRemoved(event);
break;
default:
}
}
};
cache.getListenable().addListener(listener);
}
Aggregations