Skip to content

Commit

Permalink
respond to reviewer feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantPSpencer committed Sep 20, 2023
1 parent 3f989ab commit 866fc3e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,20 @@ private void iterativeCreate(String key, T data, EntryMode mode, long ttl) {
EntryMode.PERSISTENT : mode);

// Iterate over paths, starting with full key then attempting each successive parent
// Try /a/b/c, if fails due to NoNode then try /a/b .. etc..
// Try /a/b/c, if parent /a/b, does not exist, then try to create parent, etc..
while (i < nodePaths.size()) {
try {
// If parent exists then create and exit loop
if (i == nodePaths.size() -1 || _zkClient.exists(nodePaths.get(i+1))) {
if (EntryMode.TTL.equals(mode)) {
createWithTTL(nodePaths.get(i), data, ttl);
} else {
create(nodePaths.get(i), data, i == 0 ? mode : parentMode);
}
break;
// NoNodeException thrown when parent path does not exist. We allow this and re-attempt
// creation of these nodes below
} catch (MetaClientNoNodeException ignoredParentDoesntExistException) {
i++;
// Else try to create parent in next loop iteration
} else {
i++;
}

}

// Reattempt creation of children that failed due to NoNodeException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
Expand Down Expand Up @@ -359,24 +360,27 @@ public static MetaClientException.ReturnCode translateZooKeeperCodeToMetaClientC
}
}

// Returns null if no parent path
public static String getZkParentPath(String path) {
if (path.equals("/")) {
return null;
}

int idx = path.lastIndexOf('/');
return idx == 0 ? "/" : path.substring(0, idx);
return idx == 0 ? null : path.substring(0, idx);
}

// Splits a path into the paths for each node along the way.
// /a/b/c --> /a/b/c, /a/b, /a
public static List<String> separateIntoUniqueNodePaths(String path) {
ArrayList<String> nodePaths = new ArrayList<>();
String tempStr = path;
while (tempStr.length() > 1) {
nodePaths.add(tempStr);
tempStr = getZkParentPath(tempStr);
if (path == null || "/".equals(path)) {
return null;
}

String[] subPath = path.split("/");
String[] nodePaths = new String[subPath.length-1];
StringBuilder tempPath = new StringBuilder();
for (int i = 1; i < subPath.length; i++) {
tempPath.append( "/");
tempPath.append(subPath[i]);
nodePaths[subPath.length - 1 - i] = tempPath.toString();
}
return nodePaths;
return Arrays.asList(nodePaths);
}
}

0 comments on commit 866fc3e

Please sign in to comment.