Repository: curator Updated Branches: refs/heads/CURATOR-161 389e0b0d2 -> 22d034af9
CURATOR-161 - Some progress in getting locally() to work. Working now for the foreground case, but still not working for the background case because it tries to guarantee a connection before the command is executed. Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/04caf36c Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/04caf36c Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/04caf36c Branch: refs/heads/CURATOR-161 Commit: 04caf36cd4ee76b358c7866da18c858b5608ebca Parents: 389e0b0 Author: Cameron McKenzie <[email protected]> Authored: Tue May 12 07:46:43 2015 +1000 Committer: Cameron McKenzie <[email protected]> Committed: Tue May 12 07:46:43 2015 +1000 ---------------------------------------------------------------------- .../framework/api/RemoveWatchesLocal.java | 2 +- .../imps/RemoveWatchesBuilderImpl.java | 62 ++++++++++++-------- .../framework/imps/TestRemoveWatches.java | 42 +++++++++++-- 3 files changed, 77 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/04caf36c/curator-framework/src/main/java/org/apache/curator/framework/api/RemoveWatchesLocal.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/api/RemoveWatchesLocal.java b/curator-framework/src/main/java/org/apache/curator/framework/api/RemoveWatchesLocal.java index 3769d1f..e002857 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/api/RemoveWatchesLocal.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/api/RemoveWatchesLocal.java @@ -9,7 +9,7 @@ public interface RemoveWatchesLocal extends BackgroundPathableQuietly<Void> /** * Specify if the client should just remove client side watches if a connection to ZK - * is not available. + * is not available. Note that the standard Curator retry loop will not be used in t * @return */ public BackgroundPathableQuietly<Void> locally(); http://git-wip-us.apache.org/repos/asf/curator/blob/04caf36c/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java b/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java index 5a34f7d..c9868f4 100644 --- a/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java +++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/RemoveWatchesBuilderImpl.java @@ -151,36 +151,52 @@ public class RemoveWatchesBuilderImpl implements RemoveWatchesBuilder, RemoveWat private void pathInForeground(final String path) throws Exception { - RetryLoop.callWithRetry(client.getZookeeperClient(), - new Callable<Void>() - { - @Override - public Void call() throws Exception + if(local) + { + ZooKeeper zkClient = client.getZooKeeper(); + if(watcher == null) + { + zkClient.removeAllWatches(path, watcherType, local); + } + else + { + zkClient.removeWatches(path, watcher, watcherType, local); + } + } + else + { + RetryLoop.callWithRetry(client.getZookeeperClient(), + new Callable<Void>() { - try + @Override + public Void call() throws Exception { - ZooKeeper zkClient = client.getZooKeeper(); - if(watcher == null) + try { - zkClient.removeAllWatches(path, watcherType, local); + ZooKeeper zkClient = client.getZookeeperClient().getZooKeeper(); + + if(watcher == null) + { + zkClient.removeAllWatches(path, watcherType, local); + } + else + { + zkClient.removeWatches(path, watcher, watcherType, local); + } } - else + catch(KeeperException.NoWatcherException e) { - zkClient.removeWatches(path, watcher, watcherType, local); + //Swallow this exception if the quietly flag is set, otherwise rethrow. + if(!quietly) + { + throw e; + } } - } - catch(KeeperException.NoWatcherException e) - { - //Swallow this exception if the quietly flag is set, otherwise rethrow. - if(!quietly) - { - throw e; - } - } - return null; - } - }); + return null; + } + }); + } } @Override http://git-wip-us.apache.org/repos/asf/curator/blob/04caf36c/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java index 414c819..0912c70 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestRemoveWatches.java @@ -20,6 +20,8 @@ import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.EventType; import org.apache.zookeeper.Watcher.WatcherType; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; import org.testng.Assert; import org.testng.annotations.Test; @@ -302,10 +304,6 @@ public class TestRemoveWatches extends BaseClassForTests } } - /** - * TODO: THIS IS STILL A WORK IN PROGRESS. local() is currently broken if no connection to ZK is available. - * @throws Exception - */ @Test public void testRemoveLocalWatch() throws Exception { Timing timing = new Timing(); @@ -329,7 +327,7 @@ public class TestRemoveWatches extends BaseClassForTests server.stop(); timing.sleepABit(); - + client.watches().removeAll().locally().forPath(path); Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal"); @@ -340,6 +338,40 @@ public class TestRemoveWatches extends BaseClassForTests } } + @Test + public void testRemoveLocalWatchInBackground() throws Exception { + Timing timing = new Timing(); + CuratorFramework client = CuratorFrameworkFactory.builder(). + connectString(server.getConnectString()). + retryPolicy(new RetryOneTime(1)). + build(); + try + { + client.start(); + + final String path = "/"; + + final CountDownLatch removedLatch = new CountDownLatch(1); + + Watcher watcher = new CountDownWatcher(path, removedLatch, EventType.DataWatchRemoved); + + client.checkExists().usingWatcher(watcher).forPath(path); + + //Stop the server so we can check if we can remove watches locally when offline + server.stop(); + + timing.sleepABit(); + + client.watches().removeAll().locally().inBackground().forPath(path); + + Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal"); + } + finally + { + CloseableUtils.closeQuietly(client); + } + } + /** * Test the case where we try and remove an unregistered watcher. In this case we expect a NoWatcherException to * be thrown.
