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


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/TransactionsBase.java:
##########
@@ -67,216 +67,144 @@
 
     protected void internalGetCoordinatorStats(AsyncResponse asyncResponse, 
boolean authoritative,
                                                Integer coordinatorId) {
-        if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
-            if (coordinatorId != null) {
-                
validateTopicOwnership(TopicName.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId),
-                        authoritative);
-                TransactionMetadataStore transactionMetadataStore =
-                        
pulsar().getTransactionMetadataStoreService().getStores()
-                                
.get(TransactionCoordinatorID.get(coordinatorId));
-                if (transactionMetadataStore == null) {
-                    asyncResponse.resume(new RestException(NOT_FOUND,
-                            "Transaction coordinator not found! coordinator id 
: " + coordinatorId));
+        if (coordinatorId != null) {
+            
validateTopicOwnership(TopicName.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId),
+                    authoritative);
+            TransactionMetadataStore transactionMetadataStore =
+                    pulsar().getTransactionMetadataStoreService().getStores()
+                            .get(TransactionCoordinatorID.get(coordinatorId));
+            if (transactionMetadataStore == null) {
+                asyncResponse.resume(new RestException(NOT_FOUND,
+                        "Transaction coordinator not found! coordinator id : " 
+ coordinatorId));
+                return;
+            }
+            
asyncResponse.resume(transactionMetadataStore.getCoordinatorStats());
+        } else {
+            
getPartitionedTopicMetadataAsync(TopicName.TRANSACTION_COORDINATOR_ASSIGN,
+                    false, false).thenAccept(partitionMetadata -> {
+                if (partitionMetadata.partitions == 0) {
+                    asyncResponse.resume(new 
RestException(Response.Status.NOT_FOUND,
+                            "Transaction coordinator not found"));
                     return;
                 }
-                
asyncResponse.resume(transactionMetadataStore.getCoordinatorStats());
-            } else {
-                
getPartitionedTopicMetadataAsync(TopicName.TRANSACTION_COORDINATOR_ASSIGN,
-                        false, false).thenAccept(partitionMetadata -> {
-                    if (partitionMetadata.partitions == 0) {
-                        asyncResponse.resume(new 
RestException(Response.Status.NOT_FOUND,
-                                "Transaction coordinator not found"));
+                List<CompletableFuture<TransactionCoordinatorStats>> 
transactionMetadataStoreInfoFutures =
+                        Lists.newArrayList();
+                for (int i = 0; i < partitionMetadata.partitions; i++) {
+                    try {
+                        transactionMetadataStoreInfoFutures
+                                
.add(pulsar().getAdminClient().transactions().getCoordinatorStatsByIdAsync(i));
+                    } catch (PulsarServerException e) {
+                        asyncResponse.resume(new RestException(e));
                         return;
                     }
-                    List<CompletableFuture<TransactionCoordinatorStats>> 
transactionMetadataStoreInfoFutures =
-                            Lists.newArrayList();
-                    for (int i = 0; i < partitionMetadata.partitions; i++) {
+                }
+                Map<Integer, TransactionCoordinatorStats> stats = new 
HashMap<>();
+                
FutureUtil.waitForAll(transactionMetadataStoreInfoFutures).whenComplete((result,
 e) -> {
+                    if (e != null) {
+                        asyncResponse.resume(new RestException(e));
+                        return;
+                    }
+
+                    for (int i = 0; i < 
transactionMetadataStoreInfoFutures.size(); i++) {
                         try {
-                            transactionMetadataStoreInfoFutures
-                                    
.add(pulsar().getAdminClient().transactions().getCoordinatorStatsByIdAsync(i));
-                        } catch (PulsarServerException e) {
-                            asyncResponse.resume(new RestException(e));
+                            stats.put(i, 
transactionMetadataStoreInfoFutures.get(i).get());
+                        } catch (Exception exception) {
+                            asyncResponse.resume(new 
RestException(exception.getCause()));
                             return;
                         }
                     }
-                    Map<Integer, TransactionCoordinatorStats> stats = new 
HashMap<>();
-                    
FutureUtil.waitForAll(transactionMetadataStoreInfoFutures).whenComplete((result,
 e) -> {
-                        if (e != null) {
-                            asyncResponse.resume(new RestException(e));
-                            return;
-                        }
 
-                        for (int i = 0; i < 
transactionMetadataStoreInfoFutures.size(); i++) {
-                            try {
-                                stats.put(i, 
transactionMetadataStoreInfoFutures.get(i).get());
-                            } catch (Exception exception) {
-                                asyncResponse.resume(new 
RestException(exception.getCause()));
-                                return;
-                            }
-                        }
-
-                        asyncResponse.resume(stats);
-                    });
-                }).exceptionally(ex -> {
-                    log.error("[{}] Failed to get transaction coordinator 
state.", clientAppId(), ex);
-                    resumeAsyncResponseExceptionally(asyncResponse, ex);
-                    return null;
+                    asyncResponse.resume(stats);
                 });
-            }
-        } else {
-            asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE,
-                    "This Broker is not configured with 
transactionCoordinatorEnabled=true."));
+            }).exceptionally(ex -> {
+                log.error("[{}] Failed to get transaction coordinator state.", 
clientAppId(), ex);
+                resumeAsyncResponseExceptionally(asyncResponse, ex);
+                return null;
+            });
         }
     }
 
-    protected void internalGetTransactionInPendingAckStats(AsyncResponse 
asyncResponse, boolean authoritative,
-                                                           long mostSigBits, 
long leastSigBits, String subName) {
-        if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
-            validateTopicOwnership(topicName, authoritative);
+    protected CompletableFuture<TransactionInPendingAckStats> 
internalGetTransactionInPendingAckStats(
+            boolean authoritative, long mostSigBits, long leastSigBits, String 
subName) {
+        return validateTopicOwnershipAsync(topicName, 
authoritative).thenCompose(__ -> {
             CompletableFuture<Optional<Topic>> topicFuture = 
pulsar().getBrokerService()
                     .getTopics().get(topicName.toString());
-            if (topicFuture != null) {
-                topicFuture.whenComplete((optionalTopic, e) -> {
-                    if (e != null) {
-                        asyncResponse.resume(new RestException(e));
-                        return;
-                    }
-                    if (!optionalTopic.isPresent()) {
-                        asyncResponse.resume(new 
RestException(TEMPORARY_REDIRECT,
-                                "Topic is not owned by this broker!"));
-                        return;
-                    }
-                    Topic topicObject = optionalTopic.get();
-                    if (topicObject instanceof PersistentTopic) {
-                        asyncResponse.resume(((PersistentTopic) topicObject)
-                                .getTransactionInPendingAckStats(new 
TxnID(mostSigBits, leastSigBits), subName));
-                    } else {
-                        asyncResponse.resume(new RestException(BAD_REQUEST, 
"Topic is not a persistent topic!"));
-                    }
-                });
-            } else {
-                asyncResponse.resume(new RestException(TEMPORARY_REDIRECT, 
"Topic is not owned by this broker!"));
+            if (topicFuture == null) {
+                return FutureUtil.failedFuture(new RestException(NOT_FOUND, 
"Topic not found"));
             }
-        } else {
-            asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE,
-                    "This Broker is not configured with 
transactionCoordinatorEnabled=true."));
-        }
+            return topicFuture.thenCompose(optionalTopic -> {
+                if (!optionalTopic.isPresent()) {
+                    return FutureUtil.failedFuture(new 
RestException(NOT_FOUND, "Topic not found"));
+                }
+                return CompletableFuture.completedFuture(((PersistentTopic) 
optionalTopic.get())

Review Comment:
   `PersistentTopic` check is removed here. Any reason?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/TransactionsBase.java:
##########
@@ -67,216 +67,144 @@
 
     protected void internalGetCoordinatorStats(AsyncResponse asyncResponse, 
boolean authoritative,
                                                Integer coordinatorId) {
-        if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
-            if (coordinatorId != null) {
-                
validateTopicOwnership(TopicName.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId),
-                        authoritative);
-                TransactionMetadataStore transactionMetadataStore =
-                        
pulsar().getTransactionMetadataStoreService().getStores()
-                                
.get(TransactionCoordinatorID.get(coordinatorId));
-                if (transactionMetadataStore == null) {
-                    asyncResponse.resume(new RestException(NOT_FOUND,
-                            "Transaction coordinator not found! coordinator id 
: " + coordinatorId));
+        if (coordinatorId != null) {
+            
validateTopicOwnership(TopicName.TRANSACTION_COORDINATOR_ASSIGN.getPartition(coordinatorId),
+                    authoritative);
+            TransactionMetadataStore transactionMetadataStore =
+                    pulsar().getTransactionMetadataStoreService().getStores()
+                            .get(TransactionCoordinatorID.get(coordinatorId));
+            if (transactionMetadataStore == null) {
+                asyncResponse.resume(new RestException(NOT_FOUND,
+                        "Transaction coordinator not found! coordinator id : " 
+ coordinatorId));
+                return;
+            }
+            
asyncResponse.resume(transactionMetadataStore.getCoordinatorStats());
+        } else {
+            
getPartitionedTopicMetadataAsync(TopicName.TRANSACTION_COORDINATOR_ASSIGN,
+                    false, false).thenAccept(partitionMetadata -> {
+                if (partitionMetadata.partitions == 0) {
+                    asyncResponse.resume(new 
RestException(Response.Status.NOT_FOUND,
+                            "Transaction coordinator not found"));
                     return;
                 }
-                
asyncResponse.resume(transactionMetadataStore.getCoordinatorStats());
-            } else {
-                
getPartitionedTopicMetadataAsync(TopicName.TRANSACTION_COORDINATOR_ASSIGN,
-                        false, false).thenAccept(partitionMetadata -> {
-                    if (partitionMetadata.partitions == 0) {
-                        asyncResponse.resume(new 
RestException(Response.Status.NOT_FOUND,
-                                "Transaction coordinator not found"));
+                List<CompletableFuture<TransactionCoordinatorStats>> 
transactionMetadataStoreInfoFutures =
+                        Lists.newArrayList();
+                for (int i = 0; i < partitionMetadata.partitions; i++) {
+                    try {
+                        transactionMetadataStoreInfoFutures
+                                
.add(pulsar().getAdminClient().transactions().getCoordinatorStatsByIdAsync(i));
+                    } catch (PulsarServerException e) {
+                        asyncResponse.resume(new RestException(e));
                         return;
                     }
-                    List<CompletableFuture<TransactionCoordinatorStats>> 
transactionMetadataStoreInfoFutures =
-                            Lists.newArrayList();
-                    for (int i = 0; i < partitionMetadata.partitions; i++) {
+                }
+                Map<Integer, TransactionCoordinatorStats> stats = new 
HashMap<>();
+                
FutureUtil.waitForAll(transactionMetadataStoreInfoFutures).whenComplete((result,
 e) -> {
+                    if (e != null) {
+                        asyncResponse.resume(new RestException(e));
+                        return;
+                    }
+
+                    for (int i = 0; i < 
transactionMetadataStoreInfoFutures.size(); i++) {
                         try {
-                            transactionMetadataStoreInfoFutures
-                                    
.add(pulsar().getAdminClient().transactions().getCoordinatorStatsByIdAsync(i));
-                        } catch (PulsarServerException e) {
-                            asyncResponse.resume(new RestException(e));
+                            stats.put(i, 
transactionMetadataStoreInfoFutures.get(i).get());
+                        } catch (Exception exception) {
+                            asyncResponse.resume(new 
RestException(exception.getCause()));
                             return;
                         }
                     }
-                    Map<Integer, TransactionCoordinatorStats> stats = new 
HashMap<>();
-                    
FutureUtil.waitForAll(transactionMetadataStoreInfoFutures).whenComplete((result,
 e) -> {
-                        if (e != null) {
-                            asyncResponse.resume(new RestException(e));
-                            return;
-                        }
 
-                        for (int i = 0; i < 
transactionMetadataStoreInfoFutures.size(); i++) {
-                            try {
-                                stats.put(i, 
transactionMetadataStoreInfoFutures.get(i).get());
-                            } catch (Exception exception) {
-                                asyncResponse.resume(new 
RestException(exception.getCause()));
-                                return;
-                            }
-                        }
-
-                        asyncResponse.resume(stats);
-                    });
-                }).exceptionally(ex -> {
-                    log.error("[{}] Failed to get transaction coordinator 
state.", clientAppId(), ex);
-                    resumeAsyncResponseExceptionally(asyncResponse, ex);
-                    return null;
+                    asyncResponse.resume(stats);
                 });
-            }
-        } else {
-            asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE,
-                    "This Broker is not configured with 
transactionCoordinatorEnabled=true."));
+            }).exceptionally(ex -> {
+                log.error("[{}] Failed to get transaction coordinator state.", 
clientAppId(), ex);
+                resumeAsyncResponseExceptionally(asyncResponse, ex);
+                return null;
+            });
         }
     }
 
-    protected void internalGetTransactionInPendingAckStats(AsyncResponse 
asyncResponse, boolean authoritative,
-                                                           long mostSigBits, 
long leastSigBits, String subName) {
-        if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
-            validateTopicOwnership(topicName, authoritative);
+    protected CompletableFuture<TransactionInPendingAckStats> 
internalGetTransactionInPendingAckStats(
+            boolean authoritative, long mostSigBits, long leastSigBits, String 
subName) {
+        return validateTopicOwnershipAsync(topicName, 
authoritative).thenCompose(__ -> {
             CompletableFuture<Optional<Topic>> topicFuture = 
pulsar().getBrokerService()
                     .getTopics().get(topicName.toString());
-            if (topicFuture != null) {
-                topicFuture.whenComplete((optionalTopic, e) -> {
-                    if (e != null) {
-                        asyncResponse.resume(new RestException(e));
-                        return;
-                    }
-                    if (!optionalTopic.isPresent()) {
-                        asyncResponse.resume(new 
RestException(TEMPORARY_REDIRECT,
-                                "Topic is not owned by this broker!"));
-                        return;
-                    }
-                    Topic topicObject = optionalTopic.get();
-                    if (topicObject instanceof PersistentTopic) {
-                        asyncResponse.resume(((PersistentTopic) topicObject)
-                                .getTransactionInPendingAckStats(new 
TxnID(mostSigBits, leastSigBits), subName));
-                    } else {
-                        asyncResponse.resume(new RestException(BAD_REQUEST, 
"Topic is not a persistent topic!"));
-                    }
-                });
-            } else {
-                asyncResponse.resume(new RestException(TEMPORARY_REDIRECT, 
"Topic is not owned by this broker!"));
+            if (topicFuture == null) {
+                return FutureUtil.failedFuture(new RestException(NOT_FOUND, 
"Topic not found"));
             }
-        } else {
-            asyncResponse.resume(new RestException(SERVICE_UNAVAILABLE,
-                    "This Broker is not configured with 
transactionCoordinatorEnabled=true."));
-        }
+            return topicFuture.thenCompose(optionalTopic -> {
+                if (!optionalTopic.isPresent()) {
+                    return FutureUtil.failedFuture(new 
RestException(NOT_FOUND, "Topic not found"));
+                }
+                return CompletableFuture.completedFuture(((PersistentTopic) 
optionalTopic.get())
+                        .getTransactionInPendingAckStats(new 
TxnID(mostSigBits, leastSigBits), subName));
+            });
+        });
     }
 
-    protected void internalGetTransactionInBufferStats(AsyncResponse 
asyncResponse, boolean authoritative,
-                                                       long mostSigBits, long 
leastSigBits) {
-        if (pulsar().getConfig().isTransactionCoordinatorEnabled()) {
-            validateTopicOwnership(topicName, authoritative);
+    protected CompletableFuture<TransactionInBufferStats> 
internalGetTransactionInBufferStats(
+            boolean authoritative, long mostSigBits, long leastSigBits) {
+        return validateTopicOwnershipAsync(topicName, 
authoritative).thenCompose(__ -> {

Review Comment:
   It seems L147-157 is duplicated in these methods. Better to use a common 
method like `getExistingPersistentTopicAsync()`



-- 
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: dev-unsubscr...@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to