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/02/25 10:56:30 UTC

[GitHub] [pulsar] eolivelli commented on a change in pull request #14469: [Broker] Fix ``Future.join()`` causing deadlock.

eolivelli commented on a change in pull request #14469:
URL: https://github.com/apache/pulsar/pull/14469#discussion_r814671876



##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java
##########
@@ -2256,44 +2256,52 @@ public void checkGC() {
             return CompletableFuture.completedFuture(null);
         }
         TopicName topicName = TopicName.get(TopicName.get(topic).getPartitionedTopicName());
-        try {
-            PartitionedTopicResources partitionedTopicResources = getBrokerService().pulsar().getPulsarResources()
-                    .getNamespaceResources()
-                    .getPartitionedTopicResources();
-            if (topicName.isPartitioned() && !partitionedTopicResources.partitionedTopicExists(topicName)) {
-                return CompletableFuture.completedFuture(null);
-            }
-            CompletableFuture<Void> deleteMetadataFuture = new CompletableFuture<>();
-            getBrokerService().fetchPartitionedTopicMetadataAsync(TopicName.get(topicName.getPartitionedTopicName()))
-                    .thenAccept((metadata -> {
-                        // make sure all sub partitions were deleted
-                        for (int i = 0; i < metadata.partitions; i++) {
-                            if (brokerService.getPulsar().getPulsarResources().getTopicResources()
-                                    .persistentTopicExists(topicName.getPartition(i)).join()) {
-                                throw new UnsupportedOperationException();
-                            }
-                        }
-                    }))
-                    .thenAccept((res) -> partitionedTopicResources.deletePartitionedTopicAsync(topicName)
-                            .thenAccept((r) -> {
-                        deleteMetadataFuture.complete(null);
-                    }).exceptionally(ex -> {
-                        deleteMetadataFuture.completeExceptionally(ex.getCause());
-                        return null;
-                    }))
-                    .exceptionally((e) -> {
-                        if (!(e.getCause() instanceof UnsupportedOperationException)) {
-                            log.error("delete metadata fail", e);
+        PartitionedTopicResources partitionedTopicResources = getBrokerService().pulsar().getPulsarResources()
+                .getNamespaceResources()
+                .getPartitionedTopicResources();
+        if (topicName.isPartitioned()) {
+            return partitionedTopicResources.partitionedTopicExistsAsync(topicName)
+                    .thenCompose(partitionedTopicExist -> {
+                        if (!partitionedTopicExist) {
+                            return CompletableFuture.completedFuture(null);
+                        } else {
+                            return innerDeletePartitionedMetadata(topicName, partitionedTopicResources);
                         }
-                        deleteMetadataFuture.complete(null);
-                        return null;
                     });
-            return deleteMetadataFuture;
-        } catch (Exception e) {
-            return FutureUtil.failedFuture(e);
+        } else {
+            return innerDeletePartitionedMetadata(topicName, partitionedTopicResources);
         }
     }
 
+    private CompletableFuture<Void> innerDeletePartitionedMetadata(TopicName topicName,
+                                                                   PartitionedTopicResources partitionedTopicResources)
+    {
+        return getBrokerService()
+                .fetchPartitionedTopicMetadataAsync(TopicName.get(topicName.getPartitionedTopicName()))
+                .thenCompose((metadata -> {
+                    List<CompletableFuture<Boolean>> persistentTopicExists = new ArrayList<>(metadata.partitions);
+                    for (int i = 0; i < metadata.partitions; i++) {
+                        persistentTopicExists.add(brokerService.getPulsar()
+                                .getPulsarResources().getTopicResources()
+                                .persistentTopicExists(topicName.getPartition(i)));
+                    }
+                    return FutureUtil.waitForAll(persistentTopicExists)
+                            .thenCompose(unused -> {
+                                // make sure all sub partitions were deleted after all future complete
+                                Optional<Boolean> anyExistPartition = persistentTopicExists.stream()
+                                        .map(CompletableFuture::join)
+                                        .filter(topicExist -> topicExist)
+                                        .findAny();
+                                if (anyExistPartition.isPresent()) {
+                                    log.error("[{}] Delete metadata fail", topic);

Review comment:
       why this is a error and we are returning a successful future ?




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