diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java index a991ca715d8a1..e97192086a858 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java @@ -1573,11 +1573,13 @@ protected void internalGetPartitionedStats(AsyncResponse asyncResponse, boolean FutureUtil.waitForAll(topicStatsFutureList).handle((result, exception) -> { CompletableFuture statFuture = null; + int successCount = 0; for (int i = 0; i < topicStatsFutureList.size(); i++) { statFuture = topicStatsFutureList.get(i); if (statFuture.isDone() && !statFuture.isCompletedExceptionally()) { try { stats.add(statFuture.get()); + successCount++; if (perPartition) { stats.getPartitions().put(topicName.getPartition(i).toString(), statFuture.get()); } @@ -1587,22 +1589,15 @@ protected void internalGetPartitionedStats(AsyncResponse asyncResponse, boolean } } } - if (perPartition && stats.partitions.isEmpty()) { - namespaceResources().getPartitionedTopicResources() - .partitionedTopicExistsAsync(topicName) - .thenAccept(exists -> { - if (exists) { - stats.partitions.put(topicName.toString(), new TopicStatsImpl()); - asyncResponse.resume(stats); - } else { - asyncResponse.resume( - new RestException(Status.NOT_FOUND, - "Internal topics have not been generated yet")); - } - }); - } else { - asyncResponse.resume(stats); + // If no partition returned stats successfully (e.g. none of the partitions has a + // managed-ledger znode yet), behave the same as getStats and return 404 instead of + // an empty stats object. + if (successCount == 0) { + asyncResponse.resume(new RestException(Status.NOT_FOUND, + getPartitionedTopicNotFoundErrorMessage(topicName.toString()))); + return null; } + asyncResponse.resume(stats); return null; }); }).exceptionally(ex -> {