ids = new HashSet<>(e.getValue().size());
path2ids.put(e.getKey(), ids);
for (Watcher watcher : e.getValue()) {
ids.add(((ServerCnxn) watcher).getSessionId());
@@ -256,4 +305,15 @@ public synchronized WatchesSummary getWatchesSummary() {
@Override
public void shutdown() { /* do nothing */ }
+ @Override
+ public int getRecursiveWatchQty() {
+ return watcherModeManager.getRecursiveQty();
+ }
+
+ private PathParentIterator getPathParentIterator(String path) {
+ if (watcherModeManager.getRecursiveQty() == 0) {
+ return PathParentIterator.forPathOnly(path);
+ }
+ return PathParentIterator.forAll(path);
+ }
}
diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherMode.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherMode.java
new file mode 100644
index 00000000000..b8a1dda7408
--- /dev/null
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherMode.java
@@ -0,0 +1,56 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.watch;
+
+import org.apache.zookeeper.ZooDefs;
+
+public enum WatcherMode {
+ STANDARD(false, false),
+ PERSISTENT(true, false),
+ PERSISTENT_RECURSIVE(true, true)
+ ;
+
+ public static final WatcherMode DEFAULT_WATCHER_MODE = WatcherMode.STANDARD;
+
+ public static WatcherMode fromZooDef(int mode) {
+ switch (mode) {
+ case ZooDefs.AddWatchModes.persistent:
+ return PERSISTENT;
+ case ZooDefs.AddWatchModes.persistentRecursive:
+ return PERSISTENT_RECURSIVE;
+ }
+ throw new IllegalArgumentException("Unsupported mode: " + mode);
+ }
+
+ private final boolean isPersistent;
+ private final boolean isRecursive;
+
+ WatcherMode(boolean isPersistent, boolean isRecursive) {
+ this.isPersistent = isPersistent;
+ this.isRecursive = isRecursive;
+ }
+
+ public boolean isPersistent() {
+ return isPersistent;
+ }
+
+ public boolean isRecursive() {
+ return isRecursive;
+ }
+}
diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherModeManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherModeManager.java
new file mode 100644
index 00000000000..c1a8225f8ae
--- /dev/null
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherModeManager.java
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.watch;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.zookeeper.Watcher;
+
+class WatcherModeManager {
+ private final Map watcherModes = new ConcurrentHashMap<>();
+ private final AtomicInteger recursiveQty = new AtomicInteger(0);
+
+ private static class Key {
+ private final Watcher watcher;
+ private final String path;
+
+ Key(Watcher watcher, String path) {
+ this.watcher = watcher;
+ this.path = path;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ Key key = (Key) o;
+ return watcher.equals(key.watcher) && path.equals(key.path);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(watcher, path);
+ }
+ }
+
+ // VisibleForTesting
+ Map getWatcherModes() {
+ return watcherModes;
+ }
+
+ void setWatcherMode(Watcher watcher, String path, WatcherMode mode) {
+ if (mode == WatcherMode.DEFAULT_WATCHER_MODE) {
+ removeWatcher(watcher, path);
+ } else {
+ adjustRecursiveQty(watcherModes.put(new Key(watcher, path), mode), mode);
+ }
+ }
+
+ WatcherMode getWatcherMode(Watcher watcher, String path) {
+ return watcherModes.getOrDefault(new Key(watcher, path), WatcherMode.DEFAULT_WATCHER_MODE);
+ }
+
+ void removeWatcher(Watcher watcher, String path) {
+ adjustRecursiveQty(watcherModes.remove(new Key(watcher, path)), WatcherMode.DEFAULT_WATCHER_MODE);
+ }
+
+ int getRecursiveQty() {
+ return recursiveQty.get();
+ }
+
+ // recursiveQty is an optimization to avoid having to walk the map every time this value is needed
+ private void adjustRecursiveQty(WatcherMode oldMode, WatcherMode newMode) {
+ if (oldMode == null) {
+ oldMode = WatcherMode.DEFAULT_WATCHER_MODE;
+ }
+ if (oldMode.isRecursive() != newMode.isRecursive()) {
+ if (newMode.isRecursive()) {
+ recursiveQty.incrementAndGet();
+ } else {
+ recursiveQty.decrementAndGet();
+ }
+ }
+ }
+}
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/PathParentIteratorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/PathParentIteratorTest.java
new file mode 100644
index 00000000000..59bb17adaa3
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/PathParentIteratorTest.java
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.watch;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PathParentIteratorTest {
+ @Test
+ public void testRoot() {
+ PathParentIterator pathParentIterator = PathParentIterator.forAll("/");
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertFalse(pathParentIterator.atParentPath());
+ Assert.assertEquals(pathParentIterator.next(), "/");
+ Assert.assertFalse(pathParentIterator.hasNext());
+ }
+
+ @Test
+ public void test1Level() {
+ PathParentIterator pathParentIterator = PathParentIterator.forAll("/a");
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertFalse(pathParentIterator.atParentPath());
+ Assert.assertEquals(pathParentIterator.next(), "/a");
+
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertEquals(pathParentIterator.next(), "/");
+ Assert.assertTrue(pathParentIterator.atParentPath());
+
+ Assert.assertFalse(pathParentIterator.hasNext());
+ }
+
+ @Test
+ public void testLong() {
+ PathParentIterator pathParentIterator = PathParentIterator.forAll("/a/b/c/d");
+
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertEquals(pathParentIterator.next(), "/a/b/c/d");
+ Assert.assertFalse(pathParentIterator.atParentPath());
+
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertEquals(pathParentIterator.next(), "/a/b/c");
+ Assert.assertTrue(pathParentIterator.atParentPath());
+
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertEquals(pathParentIterator.next(), "/a/b");
+ Assert.assertTrue(pathParentIterator.atParentPath());
+
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertEquals(pathParentIterator.next(), "/a");
+ Assert.assertTrue(pathParentIterator.atParentPath());
+
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertEquals(pathParentIterator.next(), "/");
+ Assert.assertTrue(pathParentIterator.atParentPath());
+
+ Assert.assertFalse(pathParentIterator.hasNext());
+ }
+
+ @Test
+ public void testForPathOnly() {
+ PathParentIterator pathParentIterator = PathParentIterator.forPathOnly("/a/b/c/d");
+ Assert.assertTrue(pathParentIterator.hasNext());
+ Assert.assertEquals(pathParentIterator.next(), "/a/b/c/d");
+ Assert.assertFalse(pathParentIterator.atParentPath());
+
+ Assert.assertFalse(pathParentIterator.hasNext());
+ }
+}
\ No newline at end of file
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/RecursiveWatchQtyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/RecursiveWatchQtyTest.java
new file mode 100644
index 00000000000..067cb2af94a
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/RecursiveWatchQtyTest.java
@@ -0,0 +1,197 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.watch;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.junit.Before;
+import org.junit.Test;
+
+public class RecursiveWatchQtyTest {
+ private WatchManager watchManager;
+
+ private static final int clientQty = 25;
+ private static final int iterations = 1000;
+
+ private static class DummyWatcher implements Watcher {
+ @Override
+ public void process(WatchedEvent event) {
+ // NOP
+ }
+ }
+
+ @Before
+ public void setup() {
+ watchManager = new WatchManager();
+ }
+
+ @Test
+ public void testRecursiveQty() {
+ WatcherModeManager manager = new WatcherModeManager();
+ DummyWatcher watcher = new DummyWatcher();
+ manager.setWatcherMode(watcher, "/a", WatcherMode.DEFAULT_WATCHER_MODE);
+ assertEquals(0, manager.getRecursiveQty());
+ manager.setWatcherMode(watcher, "/a", WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(1, manager.getRecursiveQty());
+ manager.setWatcherMode(watcher, "/a/b", WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(2, manager.getRecursiveQty());
+ manager.setWatcherMode(watcher, "/a", WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(2, manager.getRecursiveQty());
+ manager.setWatcherMode(watcher, "/a/b", WatcherMode.PERSISTENT);
+ assertEquals(1, manager.getRecursiveQty());
+ manager.setWatcherMode(watcher, "/a/b", WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(2, manager.getRecursiveQty());
+ manager.setWatcherMode(watcher, "/a/b", WatcherMode.DEFAULT_WATCHER_MODE);
+ assertEquals(1, manager.getRecursiveQty());
+ manager.setWatcherMode(watcher, "/a", WatcherMode.PERSISTENT);
+ assertEquals(0, manager.getRecursiveQty());
+ }
+
+ @Test
+ public void testAddRemove() {
+ Watcher watcher1 = new DummyWatcher();
+ Watcher watcher2 = new DummyWatcher();
+
+ watchManager.addWatch("/a", watcher1, WatcherMode.PERSISTENT_RECURSIVE);
+ watchManager.addWatch("/b", watcher2, WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(2, watchManager.getRecursiveWatchQty());
+ assertTrue(watchManager.removeWatcher("/a", watcher1));
+ assertTrue(watchManager.removeWatcher("/b", watcher2));
+ assertEquals(0, watchManager.getRecursiveWatchQty());
+ }
+
+ @Test
+ public void testAddRemoveAlt() {
+ Watcher watcher1 = new DummyWatcher();
+ Watcher watcher2 = new DummyWatcher();
+
+ watchManager.addWatch("/a", watcher1, WatcherMode.PERSISTENT_RECURSIVE);
+ watchManager.addWatch("/b", watcher2, WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(2, watchManager.getRecursiveWatchQty());
+ watchManager.removeWatcher(watcher1);
+ watchManager.removeWatcher(watcher2);
+ assertEquals(0, watchManager.getRecursiveWatchQty());
+ }
+
+ @Test
+ public void testDoubleAdd() {
+ Watcher watcher = new DummyWatcher();
+
+ watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE);
+ watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(1, watchManager.getRecursiveWatchQty());
+ watchManager.removeWatcher(watcher);
+ assertEquals(0, watchManager.getRecursiveWatchQty());
+ }
+
+ @Test
+ public void testSameWatcherMultiPath() {
+ Watcher watcher = new DummyWatcher();
+
+ watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE);
+ watchManager.addWatch("/a/b", watcher, WatcherMode.PERSISTENT_RECURSIVE);
+ watchManager.addWatch("/a/b/c", watcher, WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(3, watchManager.getRecursiveWatchQty());
+ assertTrue(watchManager.removeWatcher("/a/b", watcher));
+ assertEquals(2, watchManager.getRecursiveWatchQty());
+ watchManager.removeWatcher(watcher);
+ assertEquals(0, watchManager.getRecursiveWatchQty());
+ }
+
+ @Test
+ public void testChangeType() {
+ Watcher watcher = new DummyWatcher();
+
+ watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT);
+ assertEquals(0, watchManager.getRecursiveWatchQty());
+ watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE);
+ assertEquals(1, watchManager.getRecursiveWatchQty());
+ watchManager.addWatch("/a", watcher, WatcherMode.STANDARD);
+ assertEquals(0, watchManager.getRecursiveWatchQty());
+ assertTrue(watchManager.removeWatcher("/a", watcher));
+ assertEquals(0, watchManager.getRecursiveWatchQty());
+ }
+
+ @Test
+ public void testRecursiveQtyConcurrency() {
+ ThreadLocalRandom random = ThreadLocalRandom.current();
+ WatcherModeManager manager = new WatcherModeManager();
+ ExecutorService threadPool = Executors.newFixedThreadPool(clientQty);
+ List> tasks = null;
+ CountDownLatch completedLatch = new CountDownLatch(clientQty);
+ try {
+ tasks = IntStream.range(0, clientQty)
+ .mapToObj(__ -> threadPool.submit(() -> iterate(manager, completedLatch)))
+ .collect(Collectors.toList());
+ try {
+ completedLatch.await();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ } finally {
+ if (tasks != null) {
+ tasks.forEach(t -> t.cancel(true));
+ }
+ threadPool.shutdownNow();
+ }
+
+ int expectedRecursiveQty = (int) manager.getWatcherModes().values()
+ .stream()
+ .filter(mode -> mode == WatcherMode.PERSISTENT_RECURSIVE)
+ .count();
+ assertEquals(expectedRecursiveQty, manager.getRecursiveQty());
+ }
+
+ private void iterate(WatcherModeManager manager, CountDownLatch completedLatch) {
+ ThreadLocalRandom random = ThreadLocalRandom.current();
+ try {
+ for (int i = 0; i < iterations; ++i) {
+ String path = "/" + random.nextInt(clientQty);
+ boolean doSet = random.nextInt(100) > 33; // 2/3 will be sets
+ if (doSet) {
+ WatcherMode mode = WatcherMode.values()[random.nextInt(WatcherMode.values().length)];
+ manager.setWatcherMode(new DummyWatcher(), path, mode);
+ } else {
+ manager.removeWatcher(new DummyWatcher(), path);
+ }
+
+ int sleepMillis = random.nextInt(2);
+ if (sleepMillis > 0) {
+ try {
+ Thread.sleep(sleepMillis);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+ } finally {
+ completedLatch.countDown();
+ }
+ }
+}
\ No newline at end of file
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchManagerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchManagerTest.java
index 0ce0a59a0da..e29dab90649 100644
--- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchManagerTest.java
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchManagerTest.java
@@ -49,7 +49,7 @@ public class WatchManagerTest extends ZKTestCase {
protected static final Logger LOG = LoggerFactory.getLogger(WatchManagerTest.class);
- private static final String PATH_PREFIX = "path";
+ private static final String PATH_PREFIX = "/path";
private ConcurrentHashMap watchers;
private Random r;
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentRecursiveWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentRecursiveWatcherTest.java
new file mode 100644
index 00000000000..67f19dc0549
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentRecursiveWatcherTest.java
@@ -0,0 +1,174 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.test;
+
+import static org.apache.zookeeper.AddWatchMode.PERSISTENT_RECURSIVE;
+import java.io.IOException;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.zookeeper.AsyncCallback;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.ZooKeeper;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PersistentRecursiveWatcherTest extends ClientBase {
+ private static final Logger LOG = LoggerFactory.getLogger(PersistentRecursiveWatcherTest.class);
+ private BlockingQueue events;
+ private Watcher persistentWatcher;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+
+ events = new LinkedBlockingQueue<>();
+ persistentWatcher = event -> events.add(event);
+ }
+
+ @Test
+ public void testBasic()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE);
+ internalTestBasic(zk);
+ }
+ }
+
+ @Test
+ public void testBasicAsync()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
+ if (rc == 0) {
+ latch.countDown();
+ }
+ };
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE, cb, null);
+ Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+ internalTestBasic(zk);
+ }
+ }
+
+ private void internalTestBasic(ZooKeeper zk) throws KeeperException, InterruptedException {
+ zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b/c/d", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b/c/d/e", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.setData("/a/b/c/d/e", new byte[0], -1);
+ zk.delete("/a/b/c/d/e", -1);
+ zk.create("/a/b/c/d/e", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c/d");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c/d/e");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c/d/e");
+ assertEvent(events, Watcher.Event.EventType.NodeDeleted, "/a/b/c/d/e");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c/d/e");
+ }
+
+ @Test
+ public void testRemoval()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE);
+ zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c");
+
+ zk.removeWatches("/a/b", persistentWatcher, Watcher.WatcherType.Any, false);
+ zk.create("/a/b/c/d", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ assertEvent(events, Watcher.Event.EventType.PersistentWatchRemoved, "/a/b");
+ }
+ }
+
+ @Test
+ public void testDisconnect() throws Exception {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE);
+ stopServer();
+ assertEvent(events, Watcher.Event.EventType.None, null);
+ startServer();
+ assertEvent(events, Watcher.Event.EventType.None, null);
+ internalTestBasic(zk);
+ }
+ }
+
+ @Test
+ public void testMultiClient()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk1 = createClient(new CountdownWatcher(), hostPort); ZooKeeper zk2 = createClient(new CountdownWatcher(), hostPort)) {
+
+ zk1.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk1.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk1.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+
+ zk1.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE);
+ zk1.setData("/a/b/c", "one".getBytes(), -1);
+ Thread.sleep(1000); // give some time for the event to arrive
+
+ zk2.setData("/a/b/c", "two".getBytes(), -1);
+ zk2.setData("/a/b/c", "three".getBytes(), -1);
+ zk2.setData("/a/b/c", "four".getBytes(), -1);
+
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c");
+ }
+ }
+
+ @Test
+ public void testRootWatcher()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/", persistentWatcher, PERSISTENT_RECURSIVE);
+ zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/b");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/b/c");
+ }
+ }
+
+ private void assertEvent(BlockingQueue events, Watcher.Event.EventType eventType, String path)
+ throws InterruptedException {
+ WatchedEvent event = events.poll(5, TimeUnit.SECONDS);
+ Assert.assertNotNull(event);
+ Assert.assertEquals(eventType, event.getType());
+ Assert.assertEquals(path, event.getPath());
+ }
+}
\ No newline at end of file
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java
new file mode 100644
index 00000000000..bffa8e0a2db
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java
@@ -0,0 +1,211 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.test;
+
+import static org.apache.zookeeper.AddWatchMode.PERSISTENT;
+import java.io.IOException;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import org.apache.zookeeper.AsyncCallback;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.ZooKeeper;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PersistentWatcherTest extends ClientBase {
+ private static final Logger LOG = LoggerFactory.getLogger(PersistentWatcherTest.class);
+ private BlockingQueue events;
+ private Watcher persistentWatcher;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+
+ events = new LinkedBlockingQueue<>();
+ persistentWatcher = event -> events.add(event);
+ }
+
+ @Test
+ public void testBasic()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT);
+ internalTestBasic(zk);
+ }
+ }
+
+ @Test
+ public void testDefaultWatcher()
+ throws IOException, InterruptedException, KeeperException {
+ CountdownWatcher watcher = new CountdownWatcher() {
+ @Override
+ public synchronized void process(WatchedEvent event) {
+ super.process(event);
+ events.add(event);
+ }
+ };
+ try (ZooKeeper zk = createClient(watcher, hostPort)) {
+ zk.addWatch("/a/b", PERSISTENT);
+ events.clear(); // clear any events added during client connection
+ internalTestBasic(zk);
+ }
+ }
+
+ @Test
+ public void testBasicAsync()
+ throws IOException, InterruptedException, KeeperException {
+ CountdownWatcher watcher = new CountdownWatcher() {
+ @Override
+ public synchronized void process(WatchedEvent event) {
+ super.process(event);
+ events.add(event);
+ }
+ };
+ try (ZooKeeper zk = createClient(watcher, hostPort)) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
+ if (rc == 0) {
+ latch.countDown();
+ }
+ };
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null);
+ Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+ events.clear(); // clear any events added during client connection
+ internalTestBasic(zk);
+ }
+ }
+
+ @Test
+ public void testAsyncDefaultWatcher()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
+ if (rc == 0) {
+ latch.countDown();
+ }
+ };
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null);
+ Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+ internalTestBasic(zk);
+ }
+ }
+
+ private void internalTestBasic(ZooKeeper zk) throws KeeperException, InterruptedException {
+ zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.setData("/a/b", new byte[0], -1);
+ zk.delete("/a/b/c", -1);
+ zk.delete("/a/b", -1);
+ zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeDeleted, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b");
+ }
+
+ @Test
+ public void testRemoval()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT);
+ zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/a/b");
+
+ zk.removeWatches("/a/b", persistentWatcher, Watcher.WatcherType.Any, false);
+ zk.delete("/a/b/c", -1);
+ zk.delete("/a/b", -1);
+ assertEvent(events, Watcher.Event.EventType.PersistentWatchRemoved, "/a/b");
+ }
+ }
+
+ @Test
+ public void testDisconnect() throws Exception {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/a/b", persistentWatcher, PERSISTENT);
+ stopServer();
+ assertEvent(events, Watcher.Event.EventType.None, null);
+ startServer();
+ assertEvent(events, Watcher.Event.EventType.None, null);
+ internalTestBasic(zk);
+ }
+ }
+
+ @Test
+ public void testMultiClient()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk1 = createClient(new CountdownWatcher(), hostPort);
+ ZooKeeper zk2 = createClient(new CountdownWatcher(), hostPort)) {
+
+ zk1.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk1.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+
+ zk1.addWatch("/a/b", persistentWatcher, PERSISTENT);
+ zk1.setData("/a/b", "one".getBytes(), -1);
+ Thread.sleep(1000); // give some time for the event to arrive
+
+ zk2.setData("/a/b", "two".getBytes(), -1);
+ zk2.setData("/a/b", "three".getBytes(), -1);
+ zk2.setData("/a/b", "four".getBytes(), -1);
+
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b");
+ assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b");
+ }
+ }
+
+ @Test
+ public void testRootWatcher()
+ throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
+ zk.addWatch("/", persistentWatcher, PERSISTENT);
+ zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ zk.setData("/a", new byte[0], -1);
+ zk.create("/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/");
+ assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/");
+ }
+ }
+
+ private void assertEvent(BlockingQueue events, Watcher.Event.EventType eventType, String path)
+ throws InterruptedException {
+ WatchedEvent event = events.poll(5, TimeUnit.SECONDS);
+ Assert.assertNotNull(event);
+ Assert.assertEquals(eventType, event.getType());
+ Assert.assertEquals(path, event.getPath());
+ }
+}
\ No newline at end of file
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/UnsupportedAddWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/UnsupportedAddWatcherTest.java
new file mode 100644
index 00000000000..95b5569bb08
--- /dev/null
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/UnsupportedAddWatcherTest.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.test;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Collections;
+import org.apache.zookeeper.AddWatchMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.server.watch.IWatchManager;
+import org.apache.zookeeper.server.watch.WatchManagerFactory;
+import org.apache.zookeeper.server.watch.WatcherOrBitSet;
+import org.apache.zookeeper.server.watch.WatchesPathReport;
+import org.apache.zookeeper.server.watch.WatchesReport;
+import org.apache.zookeeper.server.watch.WatchesSummary;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UnsupportedAddWatcherTest extends ClientBase {
+
+ public static class StubbedWatchManager implements IWatchManager {
+ @Override
+ public boolean addWatch(String path, Watcher watcher) {
+ return false;
+ }
+
+ @Override
+ public boolean containsWatcher(String path, Watcher watcher) {
+ return false;
+ }
+
+ @Override
+ public boolean removeWatcher(String path, Watcher watcher) {
+ return false;
+ }
+
+ @Override
+ public void removeWatcher(Watcher watcher) {
+ // NOP
+ }
+
+ @Override
+ public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type) {
+ return new WatcherOrBitSet(Collections.emptySet());
+ }
+
+ @Override
+ public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type, WatcherOrBitSet suppress) {
+ return new WatcherOrBitSet(Collections.emptySet());
+ }
+
+ @Override
+ public int size() {
+ return 0;
+ }
+
+ @Override
+ public void shutdown() {
+ // NOP
+ }
+
+ @Override
+ public WatchesSummary getWatchesSummary() {
+ return null;
+ }
+
+ @Override
+ public WatchesReport getWatches() {
+ return null;
+ }
+
+ @Override
+ public WatchesPathReport getWatchesByPath() {
+ return null;
+ }
+
+ @Override
+ public void dumpWatches(PrintWriter pwriter, boolean byPath) {
+ // NOP
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ System.setProperty(WatchManagerFactory.ZOOKEEPER_WATCH_MANAGER_NAME, StubbedWatchManager.class.getName());
+ super.setUp();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ try {
+ super.tearDown();
+ } finally {
+ System.clearProperty(WatchManagerFactory.ZOOKEEPER_WATCH_MANAGER_NAME);
+ }
+ }
+
+ @Test(expected = KeeperException.MarshallingErrorException.class)
+ public void testBehavior() throws IOException, InterruptedException, KeeperException {
+ try (ZooKeeper zk = createClient(hostPort)) {
+ // the server will generate an exception as our custom watch manager doesn't implement
+ // the new version of addWatch()
+ zk.addWatch("/foo", event -> {}, AddWatchMode.PERSISTENT_RECURSIVE);
+ }
+ }
+}