use of org.apache.zookeeper.Watcher in project XRTB by benmfaul.
the class ZTester method addRecursiveWatch.
public void addRecursiveWatch(String target, Zoolander z, boolean isNew) throws Exception {
if (isNew) {
System.out.println("New Node|: " + target);
}
try {
zk.getChildren(target, new Watcher() {
public void process(WatchedEvent event) {
if (z != null)
try {
z.callBackR(event.getPath(), event.getType());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (Exception error) {
System.out.println("========> " + target);
return;
}
if (nodeExists(target)) {
List<String> list = zk.getChildren(target, null);
for (String s : list) {
System.out.println(target + "/" + s);
addRecursiveWatch(target + "/" + s, z, isNew);
}
}
}
use of org.apache.zookeeper.Watcher in project cdap by caskdata.
the class ResourceCoordinator method fetchAndProcessAllResources.
/**
* Fetches all {@link ResourceRequirement} and perform assignment for the one that changed. Also, it will
* remove assignments for the resource requirements that are removed.
*/
private void fetchAndProcessAllResources(final Watcher watcher) {
Futures.addCallback(zkClient.getChildren(CoordinationConstants.REQUIREMENTS_PATH, watcher), wrapCallback(new FutureCallback<NodeChildren>() {
@Override
public void onSuccess(NodeChildren result) {
Set<String> children = ImmutableSet.copyOf(result.getChildren());
// Handle new resources
for (String child : children) {
String path = CoordinationConstants.REQUIREMENTS_PATH + "/" + child;
Watcher requirementWatcher = wrapWatcher(new ResourceRequirementWatcher(path));
fetchAndProcessRequirement(path, requirementWatcher);
}
// Handle removed resources
for (String removed : ImmutableSet.copyOf(Sets.difference(requirements.keySet(), children))) {
ResourceRequirement requirement = requirements.remove(removed);
LOG.info("Requirement deleted {}", requirement);
// Delete the assignment node.
removeAssignment(removed);
}
}
@Override
public void onFailure(Throwable t) {
// If the resource path node doesn't exists, resort to watch for exists.
if (t instanceof KeeperException.NoNodeException) {
beginWatch(watcher);
}
// Otherwise, it's a unexpected failure.
LOG.error("Failed to getChildren on ZK node {}{}", zkClient.getConnectString(), CoordinationConstants.REQUIREMENTS_PATH, t);
doNotifyFailed(t);
}
}), executor);
}
use of org.apache.zookeeper.Watcher in project cdap by caskdata.
the class ResourceCoordinatorClient method watchAssignmentOnExists.
/**
* Starts watch for assignment changes when the node exists.
*
* @param serviceName Name of the service.
*/
private void watchAssignmentOnExists(final String serviceName) {
final String zkPath = CoordinationConstants.ASSIGNMENTS_PATH + "/" + serviceName;
Watcher watcher = wrapWatcher(new AssignmentWatcher(serviceName, EnumSet.of(Watcher.Event.EventType.NodeCreated)));
Futures.addCallback(zkClient.exists(zkPath, watcher), wrapCallback(new FutureCallback<Stat>() {
@Override
public void onSuccess(Stat result) {
if (result != null) {
watchAssignment(serviceName);
}
}
@Override
public void onFailure(Throwable t) {
LOG.error("Failed to call exists on ZK {}{}", zkClient.getConnectString(), zkPath, t);
doNotifyFailed(t);
}
}), Threads.SAME_THREAD_EXECUTOR);
}
Aggregations