Skip to content

Commit

Permalink
Merge pull request #4 from Borjianamin98/main
Browse files Browse the repository at this point in the history
Fix corner case of adding node to zookeeper
  • Loading branch information
Borjianamin98 authored Dec 19, 2021
2 parents 9a2db1b + 286bebc commit 42b3ace
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>ir.sahab</groupId>
<artifactId>zk-client</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<description>This is a client library for ZooKeeper. In fact it is a thin wrapper around curator that provides
concise method calls with checked exceptions.
</description>
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/ir/sahab/zk/client/ZkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ public String addSequentiallyNamedNode(String parentPath, String namePrefix, byt
*/
public void addPersistentNode(String path, byte[] data, boolean createParent)
throws ZkClientException, InterruptedException {
// There is a corner case where Curator throws a ConnectionLoss but the ZK node is actually added.
// In order to be sure that the logic used for the exceptional case is correct,
// it is necessary to first check the existence of the path and if it exists, skip the rest
// of the operations.
if (exists(path)) {
throw new ZkClientException("Failed to add ZK node because node exists: " + path);
}

try {
if (createParent) {
curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, data);
Expand All @@ -320,7 +328,14 @@ public void addPersistentNode(String path, byte[] data, boolean createParent)
} catch (InterruptedException ex) {
throw ex;
} catch (Exception ex) {
throw new ZkClientException("Failed to add a persistent ZK node in path: " + path, ex);
// There is a corner case where Curator throws a ConnectionLoss but the ZK node is actually
// added, so when we retry we get NodeExists exception. In here, we check whether the node
// is created or not and ignore the exception if the node exists.
if (exists(path) && Arrays.equals(getData(path), data)) {
logger.warn("Retrieve error in creating ZK path but node created anyway: {}", path);
} else {
throw new ZkClientException("Failed to add a persistent ZK node: " + path, ex);
}
}
}

Expand Down

0 comments on commit 42b3ace

Please sign in to comment.