curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [1/4] curator git commit: continue watching the node until it is deleted or the recipe is stopped
Date Sun, 11 Oct 2015 20:51:16 GMT
Repository: curator
Updated Branches:
  refs/heads/CURATOR-3.0 2fa711c50 -> 9a9e18e24


continue watching the node until it is deleted or the recipe is stopped


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/cdb3a803
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/cdb3a803
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/cdb3a803

Branch: refs/heads/CURATOR-3.0
Commit: cdb3a8035a551e3fb23e972a19cbeae47b14c27f
Parents: 970bd39
Author: Njal Karevoll <njal@karevoll.no>
Authored: Wed Sep 2 20:03:28 2015 +0200
Committer: Njal Karevoll <njal@karevoll.no>
Committed: Wed Sep 2 20:03:28 2015 +0200

----------------------------------------------------------------------
 .../recipes/nodes/PersistentEphemeralNode.java  | 18 +++--
 .../nodes/TestPersistentEphemeralNode.java      | 82 ++++++++++++++++++++
 2 files changed, 95 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/cdb3a803/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
index 7e00e10..4868c7a 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
@@ -74,10 +74,18 @@ public class PersistentEphemeralNode implements Closeable
         @Override
         public void process(WatchedEvent event)
         {
-        	if ( event.getType() == EventType.NodeDeleted)
-        	{
-        		createNode();
-        	}
+            if ( event.getType() == EventType.NodeDeleted)
+            {
+                createNode();
+            }
+            else if ( event.getType() == EventType.NodeDataChanged)
+            {
+                try {
+                    watchNode();
+                } catch (Exception e) {
+                    log.error(String.format("Unexpected error during watching of path: %s",
basePath), e);
+                }
+            }
         }
     };
     private final BackgroundCallback checkExistsCallback = new BackgroundCallback()
@@ -426,4 +434,4 @@ public class PersistentEphemeralNode implements Closeable
     {
     	return authFailure.get();
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/cdb3a803/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
index 8d5d05f..84eaa52 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
@@ -391,6 +391,46 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
     }
 
     @Test
+    public void testRecreatesNodeWhenEphemeralOwnerSessionExpires() throws Exception
+    {
+        CuratorFramework curator = newCurator();
+        CuratorFramework nodeCreator = newCurator();
+        CuratorFramework observer = newCurator();
+
+        nodeCreator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(PATH,
new byte[0]);
+
+        Trigger dataChangedTrigger = Trigger.dataChanged();
+        observer.getData().usingWatcher(dataChangedTrigger).forPath(PATH);
+
+        PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.EPHEMERAL,
PATH, new byte[0]);
+        node.start();
+        try
+        {
+            node.waitForInitialCreate(5, TimeUnit.SECONDS);
+            assertNodeExists(observer, node.getActualPath());
+
+            assertTrue(dataChangedTrigger.firedWithin(timing.forWaiting().seconds(), TimeUnit.SECONDS));
+
+            Trigger deletedTrigger = Trigger.deleted();
+            observer.checkExists().usingWatcher(deletedTrigger).forPath(node.getActualPath());
+
+            killSession(nodeCreator);
+
+            // Make sure the node got deleted...
+            assertTrue(deletedTrigger.firedWithin(timing.forWaiting().seconds(), TimeUnit.SECONDS));
+
+            // Check for it to be recreated...
+            Trigger createdTrigger = Trigger.created();
+            Stat stat = observer.checkExists().usingWatcher(createdTrigger).forPath(node.getActualPath());
+            assertTrue(stat != null || createdTrigger.firedWithin(timing.forWaiting().seconds(),
TimeUnit.SECONDS));
+        }
+        finally
+        {
+            node.close();
+        }
+    }
+
+    @Test
     public void testRecreatesNodeWhenItGetsDeleted() throws Exception
     {
         CuratorFramework curator = newCurator();
@@ -419,6 +459,43 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
     }
 
     @Test
+    public void testRecreatesNodeWhenItGetsDeletedAfterSetData() throws Exception
+    {
+        CuratorFramework curator = newCurator();
+
+        PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.EPHEMERAL,
PATH, new byte[0]);
+        node.start();
+        try
+        {
+            node.waitForInitialCreate(timing.forWaiting().seconds(), TimeUnit.SECONDS);
+            String originalNode = node.getActualPath();
+            assertNodeExists(curator, originalNode);
+
+            Trigger dataChangedTrigger = Trigger.dataChanged();
+            curator.getData().usingWatcher(dataChangedTrigger).forPath(originalNode);
+
+            // update the data of the node
+            node.setData(new byte[0]);
+
+            // wait for the data to be updated:
+            assertTrue(dataChangedTrigger.firedWithin(timing.forWaiting().seconds(), TimeUnit.SECONDS));
+
+            // Delete the original node...
+            curator.delete().forPath(originalNode);
+
+            // Since we're using an ephemeral node, and the original session hasn't been
interrupted the name of the new
+            // node that gets created is going to be exactly the same as the original.
+            Trigger createdWatchTrigger = Trigger.created();
+            Stat stat = curator.checkExists().usingWatcher(createdWatchTrigger).forPath(originalNode);
+            assertTrue(stat != null || createdWatchTrigger.firedWithin(timing.forWaiting().seconds(),
TimeUnit.SECONDS));
+        }
+        finally
+        {
+            node.close();
+        }
+    }
+
+    @Test
     public void testNodesCreateUniquePaths() throws Exception
     {
         CuratorFramework curator = newCurator();
@@ -721,5 +798,10 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
         {
             return new Trigger(Event.EventType.NodeDeleted);
         }
+
+        private static Trigger dataChanged()
+        {
+            return new Trigger(EventType.NodeDataChanged);
+        }
     }
 }


Mime
View raw message