liudezhi2098 commented on a change in pull request #13880:
URL: https://github.com/apache/pulsar/pull/13880#discussion_r790537725



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
##########
@@ -3317,131 +3317,184 @@ protected void 
internalTerminatePartitionedTopic(AsyncResponse asyncResponse, bo
 
     protected void internalExpireMessagesByTimestamp(AsyncResponse 
asyncResponse, String subName,
                                                      int expireTimeInSeconds, 
boolean authoritative) {
+        CompletableFuture<Void> future;
         if (topicName.isGlobal()) {
-            validateGlobalNamespaceOwnership(namespaceName);
-        }
-        // If the topic name is a partition name, no need to get partition 
topic metadata again
-        if (topicName.isPartitioned()) {
-            try {
-                internalExpireMessagesByTimestampForSinglePartition(subName, 
expireTimeInSeconds, authoritative);
-            } catch (WebApplicationException wae) {
-                asyncResponse.resume(wae);
-                return;
-            } catch (Exception e) {
-                asyncResponse.resume(new RestException(e));
-                return;
-            }
-            asyncResponse.resume(Response.noContent().build());
+            future = validateGlobalNamespaceOwnershipAsync(namespaceName);
         } else {
-            PartitionedTopicMetadata partitionMetadata = 
getPartitionedTopicMetadata(topicName, authoritative, false);
-            if (partitionMetadata.partitions > 0) {
-                final List<CompletableFuture<Void>> futures = 
Lists.newArrayList();
+            future = CompletableFuture.completedFuture(null);
+        }
+        future.thenAccept(__ ->
+            // If the topic name is a partition name, no need to get partition 
topic metadata again
+            getPartitionedTopicMetadataAsync(topicName, authoritative, false)
+                    .thenAccept(partitionMetadata -> {
+                        if (topicName.isPartitioned()) {
+                            
internalExpireMessagesByTimestampForSinglePartitionAsync(partitionMetadata, 
subName,
+                                    expireTimeInSeconds, authoritative)
+                                    .thenAccept(unused -> 
asyncResponse.resume(Response.noContent().build()))
+                                    .exceptionally(e -> {
+                                        Throwable cause = e.getCause();
+                                        log.error("[{}] Failed to expire 
messages up to {} on {}", clientAppId(),
+                                                expireTimeInSeconds, 
topicName, cause);
+                                        
resumeAsyncResponseExceptionally(asyncResponse, cause);
+                                        return null;
+                                    });
+                        } else {
+                            if (partitionMetadata.partitions > 0) {
+                                final List<CompletableFuture<Void>> futures = 
Lists.newArrayList();
 
-                // expire messages for each partition topic
-                for (int i = 0; i < partitionMetadata.partitions; i++) {
-                    TopicName topicNamePartition = topicName.getPartition(i);
-                    try {
-                        futures.add(pulsar()
-                                .getAdminClient()
-                                .topics()
-                                
.expireMessagesAsync(topicNamePartition.toString(),
-                                        subName, expireTimeInSeconds));
-                    } catch (Exception e) {
-                        log.error("[{}] Failed to expire messages up to {} on 
{}", clientAppId(), expireTimeInSeconds,
-                            topicNamePartition, e);
-                        asyncResponse.resume(new RestException(e));
-                        return;
-                    }
-                }
+                                // expire messages for each partition topic
+                                for (int i = 0; i < 
partitionMetadata.partitions; i++) {
+                                    TopicName topicNamePartition = 
topicName.getPartition(i);
+                                    try {
+                                        futures.add(pulsar()
+                                                .getAdminClient()
+                                                .topics()
+                                                
.expireMessagesAsync(topicNamePartition.toString(),
+                                                        subName, 
expireTimeInSeconds));
+                                    } catch (Exception e) {
+                                        log.error("[{}] Failed to expire 
messages up to {} on {}", clientAppId(),
+                                                expireTimeInSeconds, 
topicNamePartition, e);
+                                        asyncResponse.resume(new 
RestException(e));
+                                        return;
+                                    }
+                                }
 
-                FutureUtil.waitForAll(futures).handle((result, exception) -> {
-                    if (exception != null) {
-                        Throwable t = exception.getCause();
-                        if (t instanceof NotFoundException) {
-                            asyncResponse.resume(new 
RestException(Status.NOT_FOUND, "Subscription not found"));
-                            return null;
-                        } else {
-                            log.error("[{}] Failed to expire messages up to {} 
on {}",
-                                    clientAppId(), expireTimeInSeconds,
-                                    topicName, t);
-                            asyncResponse.resume(new RestException(t));
-                            return null;
+                                FutureUtil.waitForAll(futures).handle((result, 
exception) -> {
+                                    if (exception != null) {
+                                        Throwable t = exception.getCause();
+                                        if (t instanceof NotFoundException) {
+                                            asyncResponse.resume(new 
RestException(Status.NOT_FOUND,
+                                                    "Subscription not found"));
+                                            return null;
+                                        } else {
+                                            log.error("[{}] Failed to expire 
messages up to {} on {}",
+                                                    clientAppId(), 
expireTimeInSeconds,
+                                                    topicName, t);
+                                            asyncResponse.resume(new 
RestException(t));
+                                            return null;
+                                        }
+                                    }
+                                    
asyncResponse.resume(Response.noContent().build());
+                                    return null;
+                                });
+                            } else {
+                                
internalExpireMessagesByTimestampForSinglePartitionAsync(partitionMetadata, 
subName,
+                                        expireTimeInSeconds, authoritative)
+                                        .thenAccept(unused -> 
asyncResponse.resume(Response.noContent().build()))
+                                        .exceptionally(e -> {
+                                            Throwable cause = e.getCause();
+                                            log.error("[{}] Failed to expire 
messages up to {} on {}", clientAppId(),
+                                                    expireTimeInSeconds, 
topicName, cause);
+                                            
resumeAsyncResponseExceptionally(asyncResponse, cause);
+                                            return null;
+                                        });
+                            }
                         }
-                    }
+                    }).exceptionally(e -> {
+                        Throwable cause = e.getCause();
+                        log.error("[{}] Failed to expire messages up to {} on 
{}", clientAppId(), expireTimeInSeconds,
+                                topicName, cause);
+                        resumeAsyncResponseExceptionally(asyncResponse, cause);
+                        return null;
+                    })).exceptionally(e -> {
+                        Throwable cause = e.getCause();
+                        log.error("[{}] Failed to validate global namespace 
ownership to expire messages up to {} on {}"
+                                , clientAppId(), expireTimeInSeconds, 
topicName, cause);
+                        resumeAsyncResponseExceptionally(asyncResponse, cause);
+                        return null;
+                    });
+    }
 
-                    asyncResponse.resume(Response.noContent().build());
-                    return null;
-                });
+    // this method used in 
internalExpireMessagesForAllSubscriptionsForNonPartitionedTopic,
+    private void internalExpireMessagesByTimestampForSinglePartition(String 
subName, int expireTimeInSeconds,
+            boolean authoritative) {
+        PartitionedTopicMetadata partitionMetadata = 
getPartitionedTopicMetadata(topicName, authoritative, false);
+        try {
+            
internalExpireMessagesByTimestampForSinglePartitionAsync(partitionMetadata, 
subName, expireTimeInSeconds,
+                    authoritative).join();
+        } catch (CompletionException ce) {
+            if (ce.getCause() instanceof WebApplicationException) {
+                throw (WebApplicationException) ce.getCause();
             } else {
-                try {
-                    
internalExpireMessagesByTimestampForSinglePartition(subName, 
expireTimeInSeconds, authoritative);
-                } catch (WebApplicationException wae) {
-                    asyncResponse.resume(wae);
-                    return;
-                } catch (Exception e) {
-                    asyncResponse.resume(new RestException(e));
-                    return;
-                }
-                asyncResponse.resume(Response.noContent().build());
+                throw new RestException(ce.getCause());
             }
         }
     }
 
-    private void internalExpireMessagesByTimestampForSinglePartition(String 
subName, int expireTimeInSeconds,
+    private CompletableFuture<Void> 
internalExpireMessagesByTimestampForSinglePartitionAsync(
+            PartitionedTopicMetadata partitionMetadata, String subName, int 
expireTimeInSeconds,

Review comment:
       OK,I will fix both of these methods in 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