You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/05/11 15:00:59 UTC
[GitHub] [pulsar] nodece commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
nodece commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r870426460
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,45 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ }
+ return ret;
+ })).collect(Collectors.toList())
Review Comment:
use `reduce` looks better here, so like:
```suggestion
})).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
(accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
namespaces.addAll(m);
return CompletableFuture.completedFuture(namespaces);
}))));
```
then use `thenCompose` instead of `thenApply` in line 117.
We also can avoid the `waitForAll` and `join`.
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,45 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ }
+ return ret;
+ })).collect(Collectors.toList())
Review Comment:
Use `reduce` looks better here, so like:
```suggestion
})).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
(accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
namespaces.addAll(m);
return CompletableFuture.completedFuture(namespaces);
}))));
```
then use `thenCompose` instead of `thenApply` in line 117.
We also can avoid the `waitForAll` and `join`.
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org