codelipenghui commented on a change in pull request #4963: Add more config for 
auto-topic-creation
URL: https://github.com/apache/pulsar/pull/4963#discussion_r321629989
 
 

 ##########
 File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java
 ##########
 @@ -566,6 +574,78 @@ public PartitionedTopicMetadata deserialize(String key, 
byte[] content) throws E
         return metadataFuture;
     }
 
+    protected static PartitionedTopicMetadata 
fetchPartitionedTopicMetadataCheckAllowAutoCreation(
+            PulsarService pulsar, String path, TopicName topicName) {
+        try {
+            return 
fetchPartitionedTopicMetadataCheckAllowAutoCreationAsync(pulsar, path, 
topicName)
+                    .get();
+        } catch (Exception e) {
+            if (e.getCause() instanceof RestException) {
+                throw (RestException) e;
+            }
+            throw new RestException(e);
+        }
+    }
+
+    protected static CompletableFuture<PartitionedTopicMetadata> 
fetchPartitionedTopicMetadataCheckAllowAutoCreationAsync(
+            PulsarService pulsar, String path, TopicName topicName) {
+        CompletableFuture<PartitionedTopicMetadata> metadataFuture = new 
CompletableFuture<>();
+        try {
+            boolean allowAutoTopicCreation = 
pulsar.getConfiguration().isAllowAutoTopicCreation();
+            String topicType = 
pulsar.getConfiguration().getAllowAutoTopicCreationType();
+            boolean topicExist;
+            try {
+                topicExist = pulsar.getNamespaceService()
+                        .getListOfTopics(topicName.getNamespaceObject(), 
PulsarApi.CommandGetTopicsOfNamespace.Mode.ALL)
+                        .contains(topicName.toString());
+            } catch (Exception e) {
+                log.warn("Unexpected error while getting list of topics. 
topic={}. Error: {}",
+                        topicName, e.getMessage(), e);
+                throw new RestException(e);
+            }
+            fetchPartitionedTopicMetadataAsync(pulsar, 
path).whenCompleteAsync((metadata, ex) -> {
+                if (ex != null) {
+                    metadataFuture.completeExceptionally(ex);
+                    // If topic is already exist, creating partitioned topic 
is not allowed.
+                } else if (metadata.partitions == 0 && !topicExist && 
allowAutoTopicCreation &&
+                        TopicType.PARTITIONED.toString().equals(topicType)) {
+                    createDefaultPartitionedTopicAsync(pulsar, 
path).whenComplete((defaultMetadata, e) -> {
+                        if (e == null) {
+                            metadataFuture.complete(defaultMetadata);
 
 Review comment:
   Whether need to determine if the exception is partitioned topic has been 
created? 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to