murong00 commented on a change in pull request #6637: [pulsar-broker] Implement 
AutoSubscriptionCreation by namespace override
URL: https://github.com/apache/pulsar/pull/6637#discussion_r405912642
 
 

 ##########
 File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/NamespacesBase.java
 ##########
 @@ -571,23 +572,80 @@ protected void 
internalSetAutoTopicCreation(AsyncResponse asyncResponse, AutoTop
                         try {
                             // Write back the new policies into zookeeper
                             globalZk().setData(path(POLICIES, 
namespaceName.toString()),
-                                    
jsonMapper().writeValueAsBytes(policiesNode.getKey()), 
policiesNode.getValue().getVersion());
-                            policiesCache().invalidate(path(POLICIES, 
namespaceName.toString()));
+                                    
jsonMapper().writeValueAsBytes(policiesNode.getKey()), 
policiesNode.getValue().getVersion(),
+                                    (rc, path1, ctx, stat) -> {
+                                        if (rc == 
KeeperException.Code.OK.intValue()) {
+                                            
policiesCache().invalidate(path(POLICIES, namespaceName.toString()));
+                                        } else {
+                                            String errorMsg = String.format(
+                                                    "[%s] Failed to modify 
autoTopicCreation status for namespace %s",
+                                                    clientAppId(), 
namespaceName);
+                                            if (rc == 
KeeperException.Code.NONODE.intValue()) {
+                                                log.warn("{} : does not 
exist", errorMsg);
+                                                asyncResponse.resume(new 
RestException(Status.NOT_FOUND, "Namespace does not exist"));
+                                            } else if (rc == 
KeeperException.Code.BADVERSION.intValue()) {
+                                                log.warn("{} : concurrent 
modification", errorMsg);
+                                                asyncResponse.resume(new 
RestException(Status.CONFLICT, "Concurrent modification"));
+                                            } else {
+                                                
asyncResponse.resume(KeeperException.create(KeeperException.Code.get(rc), 
errorMsg));
+                                            }
+                                        }
+                                    }, null);
+                            String autoOverride = 
autoTopicCreationOverride.allowAutoTopicCreation ? "enabled" : "disabled";
+                            log.info("[{}] Successfully {} autoTopicCreation 
on namespace {}", clientAppId(), autoOverride, namespaceName);
                             asyncResponse.resume(Response.noContent().build());
-                            log.info("[{}] Successfully {} on namespace {}", 
clientAppId(),
-                                    
autoTopicCreationOverride.allowAutoTopicCreation ? "enabled" : "disabled", 
namespaceName);
                             return null;
-                        } catch (KeeperException.NoNodeException e) {
-                            log.error("[{}] Failed to modify autoTopicCreation 
status for namespace {}: does not exist", clientAppId(),
-                                    namespaceName);
-                            asyncResponse.resume(new 
RestException(Status.NOT_FOUND, "Namespace does not exist"));
+                        } catch (Exception e) {
+                            log.error("[{}] Failed to modify autoTopicCreation 
status on namespace {}", clientAppId(), namespaceName, e);
+                            asyncResponse.resume(new RestException(e));
                             return null;
-                        } catch (KeeperException.BadVersionException e) {
-                            log.error(
-                                    "[{}] Failed to modify autoTopicCreation 
status on namespace {} expected policy node version={} : concurrent 
modification",
-                                    clientAppId(), namespaceName, 
policiesNode.getValue().getVersion());
+                        }
+                    } else {
+                        asyncResponse.resume(new 
RestException(Status.NOT_FOUND, "Namespace " + namespaceName + " does not 
exist"));
+                        return null;
+                    }
+                }
+        ).exceptionally(e -> {
+            log.error("[{}] Failed to modify autoTopicCreation status on 
namespace {}", clientAppId(), namespaceName, e);
+            asyncResponse.resume(new RestException(e));
+            return null;
+        });
+    }
+
+    protected void internalRemoveAutoTopicCreation(AsyncResponse 
asyncResponse) {
+        validateAdminAccessForTenant(namespaceName.getTenant());
+        validatePoliciesReadOnlyAccess();
 
-                            asyncResponse.resume(new 
RestException(Status.CONFLICT, "Concurrent modification"));
+        // Force to read the data s.t. the watch to the cache content is setup.
+        policiesCache().getWithStatAsync(path(POLICIES, 
namespaceName.toString())).thenApply(
+                policies -> {
+                    if (policies.isPresent()) {
+                        Entry<Policies, Stat> policiesNode = policies.get();
+                        policiesNode.getKey().autoTopicCreationOverride = null;
+                        try {
+                            // Write back the new policies into zookeeper
+                            globalZk().setData(path(POLICIES, 
namespaceName.toString()),
+                                    
jsonMapper().writeValueAsBytes(policiesNode.getKey()), 
policiesNode.getValue().getVersion(),
+                                    (rc, path1, ctx, stat) -> {
+                                        if (rc == 
KeeperException.Code.OK.intValue()) {
+                                            
policiesCache().invalidate(path(POLICIES, namespaceName.toString()));
+                                        } else {
+                                            String errorMsg = String.format(
+                                                    "[%s] Failed to modify 
autoTopicCreation status for namespace %s",
+                                                    clientAppId(), 
namespaceName);
+                                            if (rc == 
KeeperException.Code.NONODE.intValue()) {
+                                                log.warn("{} : does not 
exist", errorMsg);
+                                                asyncResponse.resume(new 
RestException(Status.NOT_FOUND, "Namespace does not exist"));
+                                            } else if (rc == 
KeeperException.Code.BADVERSION.intValue()) {
+                                                log.warn("{} : concurrent 
modification", errorMsg);
+                                                asyncResponse.resume(new 
RestException(Status.CONFLICT, "Concurrent modification"));
+                                            } else {
+                                                
asyncResponse.resume(KeeperException.create(KeeperException.Code.get(rc), 
errorMsg));
+                                            }
+                                        }
+                                    }, null);
+                            log.info("[{}] Successfully removed 
autoTopicCreation override on namespace {}", clientAppId(), namespaceName);
+                            asyncResponse.resume(Response.noContent().build());
 
 Review comment:
   @codelipenghui Thanks for pointing out this, have fixed.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to