315157973 commented on a change in pull request #12215:
URL: https://github.com/apache/pulsar/pull/12215#discussion_r735314825



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
##########
@@ -978,21 +981,76 @@ public void unloadNamespaceBundlesGracefully() {
         }
 
         CompletableFuture<Void> future = new CompletableFuture<>();
-        managedLedgerFactory.asyncDelete(tn.getPersistenceNamingEncoding(), 
new DeleteLedgerCallback() {
-            @Override
-            public void deleteLedgerComplete(Object ctx) {
-                future.complete(null);
-            }
 
-            @Override
-            public void deleteLedgerFailed(ManagedLedgerException exception, 
Object ctx) {
-                future.completeExceptionally(exception);
+        CompletableFuture<Void> deleteTopicAuthenticationFuture = new 
CompletableFuture<>();
+        deleteTopicAuthenticationWithRetry(topic, 
deleteTopicAuthenticationFuture, 5);
+        deleteTopicAuthenticationFuture.whenComplete((v, ex) -> {
+            if (ex != null) {
+                future.completeExceptionally(ex);
+                return;
             }
-        }, null);
+            
managedLedgerFactory.asyncDelete(tn.getPersistenceNamingEncoding(), new 
DeleteLedgerCallback() {
+                @Override
+                public void deleteLedgerComplete(Object ctx) {
+                    future.complete(null);
+                }
+
+                @Override
+                public void deleteLedgerFailed(ManagedLedgerException 
exception, Object ctx) {
+                    future.completeExceptionally(exception);
+                }
+            }, null);
+        });
+
 
         return future;
     }
 
+    public void deleteTopicAuthenticationWithRetry(String topic, 
CompletableFuture<Void> future, int count) {
+        if (count == 0) {
+            log.error("The number of retries has exhausted for topic {}", 
topic);
+            future.completeExceptionally(new MetadataStoreException("The 
number of retries has exhausted"));
+            return;
+        }
+        NamespaceName namespaceName = 
TopicName.get(topic).getNamespaceObject();
+        // Check whether there are auth policies for the topic
+        
pulsar.getPulsarResources().getNamespaceResources().getPoliciesAsync(namespaceName).thenAccept(optPolicies
 -> {
+            if (!optPolicies.isPresent() || 
!optPolicies.get().auth_policies.getTopicAuthentication()
+                    .containsKey(topic)) {
+                // if there is no auth policy for the topic, just complete and 
return
+                if (log.isDebugEnabled()) {
+                    log.debug("Authentication policies not found for topic 
{}", topic);
+                }
+                future.complete(null);
+                return;
+            }
+            pulsar.getPulsarResources().getNamespaceResources()
+                    
.setPoliciesAsync(TopicName.get(topic).getNamespaceObject(), p -> {
+                        p.auth_policies.getTopicAuthentication().remove(topic);
+                        return p;
+                    }).thenAccept(v -> {
+                        log.info("Successfully delete authentication policies 
for topic {}", topic);
+                        future.complete(null);
+                    }).exceptionally(ex1 -> {
+                        if (ex1.getCause() instanceof 
MetadataStoreException.BadVersionException) {
+                            log.warn(
+                                    "Failed to delete authentication policies 
because of bad version. "
+                                            + "Retry to delete authentication 
policies for topic {}",
+                                    topic);
+                            deleteTopicAuthenticationWithRetry(topic, future, 
count - 1);
+                        } else {
+                            log.error("Failed to delete authentication 
policies for topic {}", topic, ex1);
+                            future.completeExceptionally(ex1);
+                        }
+                        return null;

Review comment:
       It seems that there are duplicate codes in two places, can we abstract 
into one method?




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