junkaixue commented on code in PR #2607:
URL: https://github.com/apache/helix/pull/2607#discussion_r1325342670


##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClient.java:
##########
@@ -115,6 +118,86 @@ public void create(String key, Object data, 
MetaClientInterface.EntryMode mode)
     }
   }
 
+  @Override
+  public void recursiveCreate(String key, T data, EntryMode mode) {
+    iterativeCreateHelperTwo(key, data, mode, -1);
+  }
+
+  @Override
+  public void recursiveCreateWithTTL(String key, T data, long ttl) {
+    iterativeCreateHelperTwo(key, data, EntryMode.TTL, ttl);
+  }
+
+  private void iterativeCreateHelper(String key, T data, EntryMode mode, long 
ttl) {
+    ArrayList<Integer> failedCreationIndices = new ArrayList<>();
+    EntryMode entryMode = mode;
+
+    // Iterate backwards over path and try to create full first then each 
successive parent
+    // For key /a/b/c try to create /a/b/c --> then try /a/b --> then try /a
+    for (int i = key.length(); i > 0; i--) {
+      if (i == key.length() || key.charAt(i) == '/') {
+        try {
+          if (EntryMode.TTL.equals(entryMode)) {
+            createWithTTL(key.substring(0,i), data, ttl);
+          } else {
+            create(key.substring(0, i), data, entryMode);
+          }
+        } catch (MetaClientNoNodeException ignoredParentDoesntExistException) {
+          failedCreationIndices.add(i);
+          // Ephemeral nodes cant have children, so change mode when creating 
parents
+          entryMode = (EntryMode.EPHEMERAL.equals(entryMode) ?
+              EntryMode.PERSISTENT : entryMode);
+          continue;
+        }
+
+        // Node was successfully created, now try to create children
+        // Keep list of indices where create failed, so we don't have to 
reiterate over the string
+        // looking for the '/' we've encountered or end of string
+        for (int j = failedCreationIndices.size()-1; j >= 0; j--) {
+          if (EntryMode.TTL.equals(entryMode)) {
+            createWithTTL(key.substring(0, failedCreationIndices.get(j)), 
data, ttl);
+          } else {
+            // If creating full key (j==0, first element in list will always 
be from creating full
+            // key) then use the original entryMode specified.
+            create(key.substring(0, failedCreationIndices.get(j)), data, j==0 
? mode : entryMode);
+          }
+        }
+        break;
+      }
+    }
+  }
+
+  private void iterativeCreateHelperTwo(String key, T data, EntryMode mode, 
long ttl) {
+    List<String> nodePaths = separateIntoUniqueNodePaths(key);
+    int i = 0;
+    // Ephemeral nodes cant have children, so change mode when creating parents
+    EntryMode parentMode = (EntryMode.EPHEMERAL.equals(mode) ?
+        EntryMode.PERSISTENT : mode);
+    // Iterate over paths, starting with full key then attempting each 
successive parent
+    while (i < nodePaths.size()) {
+      try {
+        if (EntryMode.TTL.equals(mode)) {
+          createWithTTL(nodePaths.get(i), data, ttl);
+        } else {
+          create(nodePaths.get(i), data, i == 0 ? mode : parentMode);
+        }
+        break;
+      } catch (MetaClientNoNodeException ignoredParentDoesntExistException) {

Review Comment:
   I would prefer not use NoNodeException to check it. The right logic should 
be:
   
   if exist the a?
     create b
   else {
    try to create a
   
   catch node already exist
   }
   
   
   The reason of doing that is because you will get two exception if you use no 
node exceptions as there could be parallel creation between threads. So the 
logic will be more clean if we check the path exists or not. So we only handle 
one potential exception.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to