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);
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class MultiTransactionTest method testMultiRollback.
/**
* ZOOKEEPER-2052:
* Multi abort shouldn't have any side effect.
* We fix a bug in rollback and the following scenario should work:
* 1. multi delete abort because of not empty directory
* 2. ephemeral nodes under that directory are deleted
* 3. multi delete should succeed.
*/
@Test
public void testMultiRollback() throws Exception {
zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
ZooKeeper epheZk = createClient();
epheZk.create("/foo/bar", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
List<Op> opList = Arrays.asList(Op.delete("/foo", -1));
try {
zk.multi(opList);
Assert.fail("multi delete should failed for not empty directory");
} catch (KeeperException.NotEmptyException e) {
}
final CountDownLatch latch = new CountDownLatch(1);
zk.exists("/foo/bar", new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeDeleted) {
latch.countDown();
}
}
});
epheZk.close();
latch.await();
try {
zk.getData("/foo/bar", false, null);
Assert.fail("ephemeral node should have been deleted");
} catch (KeeperException.NoNodeException e) {
}
zk.multi(opList);
try {
zk.getData("/foo", false, null);
Assert.fail("persistent node should have been deleted after multi");
} catch (KeeperException.NoNodeException e) {
}
}
use of org.apache.zookeeper.Watcher in project zookeeper by apache.
the class WatchManager method triggerWatch.
Set<Watcher> triggerWatch(String path, EventType type, Set<Watcher> supress) {
WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
Set<Watcher> watchers;
synchronized (this) {
watchers = watchTable.remove(path);
if (watchers == null || watchers.isEmpty()) {
if (LOG.isTraceEnabled()) {
ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "No watchers for " + path);
}
return null;
}
for (Watcher w : watchers) {
Set<String> paths = watch2Paths.get(w);
if (paths != null) {
paths.remove(path);
}
}
}
for (Watcher w : watchers) {
if (supress != null && supress.contains(w)) {
continue;
}
w.process(e);
}
return watchers;
}
Aggregations