Skip to content

Commit

Permalink
fix getChildren in memoryMetadata and etcdMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
coderzc committed Oct 19, 2022
1 parent 83e830e commit 40d4456
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,12 @@ private void handleBatchOperationResult(TxnResponse txnResponse,
case GET_CHILDREN: {
OpGetChildren getChildren = op.asGetChildren();
GetResponse gr = txnResponse.getGetResponses().get(getIdx++);
String basePath = getChildren.getPath() + "/";
String basePath =
getChildren.getPath().equals("/") ? "/" : getChildren.getPath() + "/";

Set<String> children = gr.getKvs().stream()
.map(kv -> kv.getKey().toString(StandardCharsets.UTF_8))
.map(p -> p.replace(basePath, ""))
.map(p -> p.replaceFirst(basePath, ""))
// Only return first-level children
.map(k -> k.split("/", 2)[0])
.collect(Collectors.toCollection(TreeSet::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public CompletableFuture<List<String>> getChildrenFromStore(String path) {

Set<String> children = new TreeSet<>();
map.subMap(firstKey, false, lastKey, false).forEach((key, value) -> {
String relativePath = key.replace(firstKey, "");
String relativePath = key.replaceFirst(firstKey, "");

// Only return first-level children
String child = relativePath.split("/", 2)[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -500,4 +501,32 @@ public void testConcurrentDelete(String provider, Supplier<String> urlSupplier)
assertTrue(f1.isCompletedExceptionally() && !f2.isCompletedExceptionally() ||
! f1.isCompletedExceptionally() && f2.isCompletedExceptionally());
}

@Test(dataProvider = "impl")
public void testGetChildren(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup
MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());

store.put("/a/a-1", "value1".getBytes(StandardCharsets.UTF_8), Optional.empty()).join();
store.put("/a/a-2", "value1".getBytes(StandardCharsets.UTF_8), Optional.empty()).join();
store.put("/b/c/b/1", "value1".getBytes(StandardCharsets.UTF_8), Optional.empty()).join();

List<String> subPaths = store.getChildren("/").get();
Set<String> expectedSet = "ZooKeeper".equals(provider) ? Set.of("a", "b", "zookeeper") : Set.of("a", "b");
for (String subPath : subPaths) {
assertTrue(expectedSet.contains(subPath));
}

List<String> subPaths2 = store.getChildren("/a").get();
Set<String> expectedSet2 = Set.of("a-1", "a-2");
for (String subPath : subPaths2) {
assertTrue(expectedSet2.contains(subPath));
}

List<String> subPaths3 = store.getChildren("/b").get();
Set<String> expectedSet3 = Set.of("c");
for (String subPath : subPaths3) {
assertTrue(expectedSet3.contains(subPath));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testNotifyEvent() throws Exception {
assertEquals((long) event2.getExpectedVersion(), exptectedVersion);
assertEquals(event2.getType(), NotificationType.Modified);
}

// (3) delete node
sync.notifiedEvents.remove(path);
store1.delete(path, Optional.of(exptectedVersion)).join();
Expand Down Expand Up @@ -176,7 +176,7 @@ public void testSyncListener() throws Exception {
store1.put(path, value1, Optional.empty()).join();

assertTrue(store1.exists(path).join());

Stat stats = store1.get(path).get().get().getStat();
MetadataEvent event = new MetadataEvent(path, value2, EMPTY_SET, stats.getVersion(),
stats.getModificationTimestamp() + 1, sync.clusterName, NotificationType.Modified);
Expand Down

0 comments on commit 40d4456

Please sign in to comment.