use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class WatchManager method triggerWatch.
@Override
public WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet supress) {
WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
Set<Watcher> watchers = new HashSet<>();
PathParentIterator pathParentIterator = getPathParentIterator(path);
synchronized (this) {
for (String localPath : pathParentIterator.asIterable()) {
Set<Watcher> thisWatchers = watchTable.get(localPath);
if (thisWatchers == null || thisWatchers.isEmpty()) {
continue;
}
Iterator<Watcher> iterator = thisWatchers.iterator();
while (iterator.hasNext()) {
Watcher watcher = iterator.next();
WatcherMode watcherMode = watcherModeManager.getWatcherMode(watcher, localPath);
if (watcherMode.isRecursive()) {
if (type != EventType.NodeChildrenChanged) {
watchers.add(watcher);
}
} else if (!pathParentIterator.atParentPath()) {
watchers.add(watcher);
if (!watcherMode.isPersistent()) {
iterator.remove();
Set<String> paths = watch2Paths.get(watcher);
if (paths != null) {
paths.remove(localPath);
}
}
}
}
if (thisWatchers.isEmpty()) {
watchTable.remove(localPath);
}
}
}
if (watchers.isEmpty()) {
if (LOG.isTraceEnabled()) {
ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "No watchers for " + path);
}
return null;
}
for (Watcher w : watchers) {
if (supress != null && supress.contains(w)) {
continue;
}
w.process(e);
}
switch(type) {
case NodeCreated:
ServerMetrics.getMetrics().NODE_CREATED_WATCHER.add(watchers.size());
break;
case NodeDeleted:
ServerMetrics.getMetrics().NODE_DELETED_WATCHER.add(watchers.size());
break;
case NodeDataChanged:
ServerMetrics.getMetrics().NODE_CHANGED_WATCHER.add(watchers.size());
break;
case NodeChildrenChanged:
ServerMetrics.getMetrics().NODE_CHILDREN_WATCHER.add(watchers.size());
break;
default:
// Other types not logged.
break;
}
return new WatcherOrBitSet(watchers);
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class WatchManagerOptimized method triggerWatch.
@Override
public WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet suppress) {
WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
BitHashSet watchers = remove(path);
if (watchers == null) {
return null;
}
int triggeredWatches = 0;
// WatcherCleaner and iterating here
synchronized (watchers) {
for (Integer wBit : watchers) {
if (suppress != null && suppress.contains(wBit)) {
continue;
}
Watcher w = watcherBitIdMap.get(wBit);
// skip dead watcher
if (w == null || isDeadWatcher(w)) {
continue;
}
w.process(e);
triggeredWatches++;
}
}
updateMetrics(type, triggeredWatches);
return new WatcherOrBitSet(watchers);
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class DisconnectedWatcherTest method testDefaultWatcherAutoResetWithChroot.
@Test
public void testDefaultWatcherAutoResetWithChroot() throws Exception {
zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk2 = createClient(watcher2, hostPort + "/ch1");
zk2.getChildren("/", true);
// this call shouldn't trigger any error or watch
zk1.create("/youdontmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// this should trigger the watch
zk1.create("/ch1/youshouldmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
WatchedEvent e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
assertNotNull(e);
assertEquals(EventType.NodeChildrenChanged, e.getType());
assertEquals("/", e.getPath());
zk2.getChildren("/", true);
stopServer();
watcher2.waitForDisconnected(3000);
startServer();
watcher2.waitForConnected(3000);
watcher1.waitForConnected(3000);
// this should trigger the watch
zk1.create("/ch1/youshouldmatter2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
assertNotNull(e);
assertEquals(EventType.NodeChildrenChanged, e.getType());
assertEquals("/", e.getPath());
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class BaseSysTest method setUp.
@Before
public void setUp() throws Exception {
if (!fakeMachines) {
zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {
public void process(WatchedEvent e) {
}
});
im = new InstanceManager(zk, prefix);
}
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class ClientTest method testNonExistingOpCode.
/**
* We create a perfectly valid 'exists' request, except that the opcode is wrong.
* @throws Exception
*/
@Test
public void testNonExistingOpCode() throws Exception {
final CountDownLatch clientDisconnected = new CountDownLatch(1);
Watcher watcher = new Watcher() {
@Override
public synchronized void process(WatchedEvent event) {
if (event.getState() == KeeperState.Disconnected) {
clientDisconnected.countDown();
}
}
};
TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher);
final String path = "/m1";
RequestHeader h = new RequestHeader();
// This code does not exists
h.setType(888);
ExistsRequest request = new ExistsRequest();
request.setPath(path);
request.setWatch(false);
ExistsResponse response = new ExistsResponse();
ReplyHeader r = zk.submitRequest(h, request, response, null);
assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue());
// Sending a nonexisting opcode should cause the server to disconnect
assertTrue(clientDisconnected.await(5000, TimeUnit.MILLISECONDS), "failed to disconnect");
zk.close();
}
Aggregations