CURATOR-190 - Modified to always use the base name when creating protected ephemeral nodes. This prevents the current protected name being appended to the name each time the node is created, which in turn prevents multiple nodes being created.
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/9fe802ec Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/9fe802ec Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/9fe802ec Branch: refs/heads/CURATOR-3.0 Commit: 9fe802ec238b8ae7b5f77e61cac7e25c8f42f6d9 Parents: 49eb02a Author: Cameron McKenzie <came...@unico.com.au> Authored: Wed Mar 4 09:09:01 2015 +1100 Committer: Cameron McKenzie <came...@unico.com.au> Committed: Wed Mar 4 09:09:01 2015 +1100 ---------------------------------------------------------------------- .../recipes/nodes/PersistentEphemeralNode.java | 2 +- .../nodes/TestPersistentEphemeralNode.java | 39 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/9fe802ec/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 d78573c..cfc4cbc 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 @@ -336,7 +336,7 @@ public class PersistentEphemeralNode implements Closeable try { String existingPath = nodePath.get(); - String createPath = (existingPath != null) ? existingPath : basePath; + String createPath = (existingPath != null && !mode.isProtected()) ? existingPath : basePath; createMethod.withMode(mode.getCreateMode(existingPath != null)).inBackground(backgroundCallback).forPath(createPath, data.get()); } catch ( Exception e ) http://git-wip-us.apache.org/repos/asf/curator/blob/9fe802ec/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 47ae757..8bc8d3d 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 @@ -20,6 +20,7 @@ package org.apache.curator.framework.recipes.nodes; import com.google.common.base.Throwables; import com.google.common.collect.Lists; + import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.state.ConnectionState; @@ -37,9 +38,11 @@ import org.apache.zookeeper.data.Stat; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; + import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; @@ -459,6 +462,42 @@ public class TestPersistentEphemeralNode extends BaseClassForTests node.close(); } } + + /** + * See CURATOR-190 + * For protected nodes on reconnect the current protected name was passed to the create builder meaning that it got + * appended to the new protected node name. This meant that a new node got created on each reconnect. + * @throws Exception + */ + @Test + public void testProtected() throws Exception + { + CuratorFramework curator = newCurator(); + + PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.PROTECTED_EPHEMERAL, PATH, + new byte[0]); + node.start(); + try + { + node.waitForInitialCreate(timing.forWaiting().seconds(), TimeUnit.SECONDS); + assertNodeExists(curator, node.getActualPath()); + + server.restart(); + + curator.blockUntilConnected(5, TimeUnit.SECONDS); + + assertNodeExists(curator, node.getActualPath()); + + //There should only be a single child, the persisted ephemeral node + List<String> children = curator.getChildren().forPath(DIR); + assertFalse(children == null); + assertEquals(children.size(), 1); + } + finally + { + node.close(); + } + } private void assertNodeExists(CuratorFramework curator, String path) throws Exception {