curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dragonsi...@apache.org
Subject [34/41] curator git commit: CURATOR-241: Write updated data on reconnect
Date Mon, 17 Aug 2015 17:02:42 GMT
CURATOR-241: Write updated data on reconnect

PersistentEphemeralNode can be initialised with certain data, then
later updated. If the client reconnects, the replacing ephemeral
should write the updated data.


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

Branch: refs/heads/CURATOR-3.0
Commit: 7fd4034e30f2ed3690226d2d78bf9f139df8b491
Parents: 3a4d541
Author: Alex Brasetvik <alex@brasetvik.com>
Authored: Mon Aug 3 00:57:51 2015 +0200
Committer: Alex Brasetvik <alex@brasetvik.com>
Committed: Mon Aug 3 01:02:19 2015 +0200

----------------------------------------------------------------------
 .../recipes/nodes/PersistentEphemeralNode.java  |  8 +++-
 .../nodes/TestPersistentEphemeralNode.java      | 41 ++++++++++++++++++++
 2 files changed, 47 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/7fd4034e/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 f50dca4..1011ad5 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
@@ -240,7 +240,7 @@ public class PersistentEphemeralNode implements Closeable
 
                     if(nodeExists)
                     {
-                    	client.setData().inBackground(setDataCallback).forPath(getActualPath(),
data);
+                       client.setData().inBackground(setDataCallback).forPath(getActualPath(),
getData());
                     }
                     else
                     {
@@ -338,10 +338,14 @@ public class PersistentEphemeralNode implements Closeable
         this.data.set(Arrays.copyOf(data, data.length));
         if ( isActive() )
         {
-            client.setData().inBackground().forPath(getActualPath(), this.data.get());
+            client.setData().inBackground().forPath(getActualPath(), getData());
         }
     }
 
+    byte[] getData() {
+        return this.data.get();
+    }
+
     private void deleteNode() throws Exception
     {
         String localNodePath = nodePath.getAndSet(null);

http://git-wip-us.apache.org/repos/asf/curator/blob/7fd4034e/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 34620ff..9f5907a 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
@@ -23,6 +23,8 @@ import com.google.common.collect.Lists;
 
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.api.BackgroundCallback;
+import org.apache.curator.framework.api.CuratorEvent;
 import org.apache.curator.framework.state.ConnectionState;
 import org.apache.curator.framework.state.ConnectionStateListener;
 import org.apache.curator.retry.RetryOneTime;
@@ -536,6 +538,45 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
             node.close();
         }    	
     }
+
+    @Test
+    public void testSetUpdatedDataWhenReconnected() throws Exception
+    {
+        CuratorFramework curator = newCurator();
+
+        byte[] initialData = "Hello World".getBytes();
+        byte[] updatedData = "Updated".getBytes();
+
+        PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.EPHEMERAL,
PATH, initialData);
+        node.start();
+        try
+        {
+            node.waitForInitialCreate(timing.forWaiting().seconds(), TimeUnit.SECONDS);
+            assertTrue(Arrays.equals(curator.getData().forPath(node.getActualPath()), initialData));
+
+            node.setData(updatedData);
+            assertTrue(Arrays.equals(curator.getData().forPath(node.getActualPath()), updatedData));
+
+            server.restart();
+
+            final CountDownLatch dataUpdateLatch = new CountDownLatch(1);
+            curator.getData().inBackground(new BackgroundCallback() {
+
+                @Override
+                public void processResult(CuratorFramework client, CuratorEvent event) throws
Exception {
+                    dataUpdateLatch.countDown();
+                }
+            }).forPath(node.getActualPath());
+
+            assertTrue(timing.awaitLatch(dataUpdateLatch));
+
+            assertTrue(Arrays.equals(curator.getData().forPath(node.getActualPath()), updatedData));
+        }
+        finally
+        {
+            node.close();
+        }
+    }
     
     /**
      * See CURATOR-190


Mime
View raw message