Jason918 commented on code in PR #16916:
URL: https://github.com/apache/pulsar/pull/16916#discussion_r937648861


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java:
##########
@@ -1342,17 +1342,31 @@ protected void 
internalGetPartitionedStats(AsyncResponse asyncResponse, boolean
                 return;
             }
             PartitionedTopicStatsImpl stats = new 
PartitionedTopicStatsImpl(partitionMetadata);
-            List<CompletableFuture<TopicStats>> topicStatsFutureList = 
Lists.newArrayList();
+            List<CompletableFuture<TopicStats>> topicStatsFutureList = new 
ArrayList<>(partitionMetadata.partitions);
             for (int i = 0; i < partitionMetadata.partitions; i++) {
-                try {
-                    topicStatsFutureList
-                            
.add(pulsar().getAdminClient().topics().getStatsAsync(
-                                    (topicName.getPartition(i).toString()), 
getPreciseBacklog, subscriptionBacklogSize,
-                                    getEarliestTimeInBacklog));
-                } catch (PulsarServerException e) {
-                    asyncResponse.resume(new RestException(e));
-                    return;
-                }
+                TopicName partition = topicName.getPartition(i);
+                topicStatsFutureList.add(
+                    pulsar().getNamespaceService()
+                        .isServiceUnitOwnedAsync(partition)
+                        .thenCompose(owned -> {
+                            if (owned) {
+                                return getTopicReferenceAsync(partition)
+                                    .thenApply(ref ->
+                                        ref.getStats(getPreciseBacklog, 
subscriptionBacklogSize,
+                                            getEarliestTimeInBacklog));
+                            } else {
+                                CompletableFuture<TopicStats> restFuture = new 
CompletableFuture<>();
+                                try {
+                                    restFuture = 
pulsar().getAdminClient().topics().getStatsAsync(
+                                        partition.toString(), 
getPreciseBacklog, subscriptionBacklogSize,
+                                        getEarliestTimeInBacklog);
+                                } catch (PulsarServerException e) {
+                                    restFuture.completeExceptionally(e);
+                                }
+                                return restFuture;

Review Comment:
   ```suggestion
                                   try {
                                       return 
pulsar().getAdminClient().topics().getStatsAsync(
                                           partition.toString(), 
getPreciseBacklog, subscriptionBacklogSize,
                                           getEarliestTimeInBacklog);
                                   } catch (PulsarServerException e) {
                                     
org.apache.pulsar.common.util.FutureUtil#failedFuture(e);
                                   }
   ```
   Reduce a CompletableFuture object if no exception.



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