Skip to content

Commit

Permalink
[fix][meta] fix getChildren in MemoryMetadataStore and EtcdMetadataS…
Browse files Browse the repository at this point in the history
…tore (apache#18172)

(cherry picked from commit 4ffa741)
(cherry picked from commit 3792d63)
  • Loading branch information
coderzc authored and nicoloboschi committed Jan 10, 2023
1 parent 03462b2 commit ec2f453
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,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 @@ -123,7 +123,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 @@ -23,11 +23,13 @@
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import com.google.common.collect.Sets;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
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 @@ -479,4 +481,33 @@ 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) ? Sets.newHashSet("a", "b", "zookeeper") :
Sets.newHashSet("a", "b");
for (String subPath : subPaths) {
assertTrue(expectedSet.contains(subPath));
}

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

List<String> subPaths3 = store.getChildren("/b").get();
Set<String> expectedSet3 = Sets.newHashSet("c");
for (String subPath : subPaths3) {
assertTrue(expectedSet3.contains(subPath));
}
}
}

0 comments on commit ec2f453

Please sign in to comment.