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_r325001966
########## File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java ########## @@ -566,6 +574,96 @@ 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); + } else if (e instanceof KeeperException) { + try { + Thread.sleep(PARTITIONED_TOPIC_WAIT_SYNC_TIME_MS); + if (!pulsar.getGlobalZkCache().exists(path)){ + metadataFuture.completeExceptionally(e); + return; + } + } catch (InterruptedException | KeeperException exc) { + metadataFuture.completeExceptionally(exc); + return; + } + fetchPartitionedTopicMetadataAsync(pulsar, path).whenComplete((metadata2, ex2) -> { + if (ex2 != null) { + metadataFuture.completeExceptionally(ex2); + } else { + metadataFuture.complete(metadata2); + } + }); + } else { + metadataFuture.completeExceptionally(e); + } + }); + } else { + metadataFuture.complete(metadata); + } + }); + } catch (Exception e) { + metadataFuture.completeExceptionally(e); + } + return metadataFuture; + } + + protected static CompletableFuture<PartitionedTopicMetadata> createDefaultPartitionedTopicAsync( Review comment: Since this method is no longer an asynchronous method, it's better to rename it to createDefaultPartitionedTopic() ---------------------------------------------------------------- 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