codelipenghui commented on a change in pull request #13846:
URL: https://github.com/apache/pulsar/pull/13846#discussion_r789395263



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
##########
@@ -1044,72 +1045,88 @@ protected void internalDeleteTopic(boolean 
authoritative, boolean deleteSchema)
     }
 
     protected void internalGetSubscriptions(AsyncResponse asyncResponse, 
boolean authoritative) {
+        CompletableFuture<Void> future;
         if (topicName.isGlobal()) {
-            try {
-                validateGlobalNamespaceOwnership(namespaceName);
-            } catch (Exception e) {
-                log.error("[{}] Failed to get subscriptions for topic {}", 
clientAppId(), topicName, e);
-                resumeAsyncResponseExceptionally(asyncResponse, e);
-                return;
-            }
-        }
-
-        validateTopicOwnership(topicName, authoritative);
-
-        // If the topic name is a partition name, no need to get partition 
topic metadata again
-        if (topicName.isPartitioned()) {
-            internalGetSubscriptionsForNonPartitionedTopic(asyncResponse, 
authoritative);
+            future = validateGlobalNamespaceOwnershipAsync(namespaceName);
         } else {
-            getPartitionedTopicMetadataAsync(topicName, authoritative,
-                    false).thenAccept(partitionMetadata -> {
-                if (partitionMetadata.partitions > 0) {
-                    try {
-                        final Set<String> subscriptions = 
Sets.newConcurrentHashSet();
-                        final List<CompletableFuture<Object>> 
subscriptionFutures = Lists.newArrayList();
-                        if (topicName.getDomain() == TopicDomain.persistent) {
-                            final Map<Integer, CompletableFuture<Boolean>> 
existsFutures = Maps.newConcurrentMap();
-                            for (int i = 0; i < partitionMetadata.partitions; 
i++) {
-                                existsFutures.put(i, 
topicResources().persistentTopicExists(topicName.getPartition(i)));
-                            }
-                            
FutureUtil.waitForAll(Lists.newArrayList(existsFutures.values())).thenApply(__ 
->
-                                    existsFutures.entrySet().stream().filter(e 
-> e.getValue().join())
-                                            .map(item -> 
topicName.getPartition(item.getKey()).toString())
-                                            .collect(Collectors.toList())
-                            ).thenAccept(topics -> {
-                                if (log.isDebugEnabled()) {
-                                    log.debug("activeTopics : {}", topics);
-                                }
-                                topics.forEach(topic -> {
-                                    try {
-                                        CompletableFuture<List<String>> 
subscriptionsAsync = pulsar().getAdminClient()
-                                                
.topics().getSubscriptionsAsync(topic);
-                                        
subscriptionFutures.add(subscriptionsAsync.thenApply(subscriptions::addAll));
-                                    } catch (PulsarServerException e) {
-                                        throw new RestException(e);
+            future = CompletableFuture.completedFuture(null);
+        }
+        future.thenAccept(__ -> validateTopicOwnershipAsync(topicName, 
authoritative))
+                .thenAccept(unused -> {
+                    // If the topic name is a partition name, no need to get 
partition topic metadata again
+                    if (topicName.isPartitioned()) {
+                        
internalGetSubscriptionsForNonPartitionedTopic(asyncResponse, authoritative);
+                    } else {
+                        getPartitionedTopicMetadataAsync(topicName, 
authoritative, false)
+                                .thenAccept(partitionMetadata -> {
+                            if (partitionMetadata.partitions > 0) {
+                                try {
+                                    final Set<String> subscriptions =
+                                            Collections.newSetFromMap(
+                                                    new 
ConcurrentHashMap<>(partitionMetadata.partitions));
+                                    final List<CompletableFuture<Object>> 
subscriptionFutures = Lists.newArrayList();
+                                    if (topicName.getDomain() == 
TopicDomain.persistent) {
+                                        final Map<Integer, 
CompletableFuture<Boolean>> existsFutures =
+                                                new 
ConcurrentHashMap<>(partitionMetadata.partitions);
+                                        for (int i = 0; i < 
partitionMetadata.partitions; i++) {
+                                            existsFutures.put(i,
+                                                    
topicResources().persistentTopicExists(topicName.getPartition(i)));
+                                        }
+                                        
FutureUtil.waitForAll(Lists.newArrayList(existsFutures.values()))
+                                                .thenApply(__ ->
+                                                
existsFutures.entrySet().stream().filter(e -> e.getValue().join())
+                                                        .map(item -> 
topicName.getPartition(item.getKey()).toString())
+                                                        
.collect(Collectors.toList())
+                                        ).thenAccept(topics -> {
+                                            if (log.isDebugEnabled()) {
+                                                log.debug("activeTopics : {}", 
topics);
+                                            }
+                                            topics.forEach(topic -> {
+                                                try {
+                                                    
CompletableFuture<List<String>> subscriptionsAsync = pulsar()
+                                                            .getAdminClient()
+                                                            
.topics().getSubscriptionsAsync(topic);
+                                                    
subscriptionFutures.add(subscriptionsAsync
+                                                            
.thenApply(subscriptions::addAll));
+                                                } catch (PulsarServerException 
e) {
+                                                    throw new RestException(e);
+                                                }
+                                            });
+                                        }).thenAccept(__ -> 
resumeAsyncResponse(asyncResponse,
+                                                        subscriptions, 
subscriptionFutures));
+                                    } else {
+                                        for (int i = 0; i < 
partitionMetadata.partitions; i++) {
+                                            CompletableFuture<List<String>> 
subscriptionsAsync = pulsar()
+                                                    .getAdminClient().topics()
+                                                    
.getSubscriptionsAsync(topicName.getPartition(i).toString());
+                                            
subscriptionFutures.add(subscriptionsAsync
+                                                    
.thenApply(subscriptions::addAll));
+                                        }
+                                        resumeAsyncResponse(asyncResponse, 
subscriptions, subscriptionFutures);

Review comment:
       We can consider if the `resumeAsyncResponse` can be changed into a 
general method, not only for list subscriptions.  If we can't have a general 
method to handle the response, it's better to hand it over to a specific method 
to handle the response by themselves. 
   
   It's not the scope of this PR.




-- 
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]


Reply via email to