From 7fd4034e30f2ed3690226d2d78bf9f139df8b491 Mon Sep 17 00:00:00 2001 From: Alex Brasetvik Date: Mon, 3 Aug 2015 00:57:51 +0200 Subject: [PATCH 1/3] 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. --- .../nodes/PersistentEphemeralNode.java | 8 +++- .../nodes/TestPersistentEphemeralNode.java | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) 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 f50dca48b7..1011ad5eee 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 @@ else if ( event.getResultCode() == KeeperException.Code.OK.intValue() ) if(nodeExists) { - client.setData().inBackground(setDataCallback).forPath(getActualPath(), data); + client.setData().inBackground(setDataCallback).forPath(getActualPath(), getData()); } else { @@ -338,10 +338,14 @@ public void setData(byte[] data) throws Exception 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); 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 34620ff978..9f5907a3ec 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 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 void process(WatchedEvent event) 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 From 5cfa4839e0798249d32b34b792b5f59f111dd022 Mon Sep 17 00:00:00 2001 From: Alex Brasetvik Date: Mon, 3 Aug 2015 01:52:51 +0200 Subject: [PATCH 2/3] Make getData private and access data directly in setData --- .../framework/recipes/nodes/PersistentEphemeralNode.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 1011ad5eee..7a2ab7307b 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 @@ -338,11 +338,11 @@ public void setData(byte[] data) throws Exception this.data.set(Arrays.copyOf(data, data.length)); if ( isActive() ) { - client.setData().inBackground().forPath(getActualPath(), getData()); + client.setData().inBackground().forPath(getActualPath(), this.data.get()); } } - byte[] getData() { + private byte[] getData() { return this.data.get(); } From f3ff7e75f305a6d52a8fc9f7e9defae4299a66a2 Mon Sep 17 00:00:00 2001 From: Alex Brasetvik Date: Mon, 3 Aug 2015 02:00:44 +0200 Subject: [PATCH 3/3] Access data the same way --- .../framework/recipes/nodes/PersistentEphemeralNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7a2ab7307b..0d963e00b3 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 @@ -338,7 +338,7 @@ public void setData(byte[] data) throws Exception this.data.set(Arrays.copyOf(data, data.length)); if ( isActive() ) { - client.setData().inBackground().forPath(getActualPath(), this.data.get()); + client.setData().inBackground().forPath(getActualPath(), getData()); } }