poorbarcode commented on code in PR #25312:
URL: https://github.com/apache/pulsar/pull/25312#discussion_r2937846345


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java:
##########
@@ -3458,6 +3478,79 @@ private CompletableFuture<PartitionedTopicMetadata> 
createDefaultPartitionedTopi
                 });
     }
 
+    private CompletableFuture<TopicExistsInfo> 
getRemotePartitionedTopicMetadataForAutoCreation(
+            TopicName topicName, Optional<Policies> policies) {
+        if (!pulsar.getConfig().isCreateTopicToRemoteClusterForReplication()) {
+            return 
CompletableFuture.completedFuture(TopicExistsInfo.newTopicNotExists());
+        }
+        if (topicName.isPartitioned() || !topicName.isPersistent() || 
policies.isEmpty()) {
+            return 
CompletableFuture.completedFuture(TopicExistsInfo.newTopicNotExists());
+        }
+        Set<String> replicationClusters = policies.get().replication_clusters;
+        if (replicationClusters == null || replicationClusters.isEmpty()) {
+            return 
CompletableFuture.completedFuture(TopicExistsInfo.newTopicNotExists());
+        }
+        String localCluster = pulsar.getConfiguration().getClusterName();
+        if (!replicationClusters.contains(localCluster) || 
replicationClusters.size() <= 1) {
+            return 
CompletableFuture.completedFuture(TopicExistsInfo.newTopicNotExists());
+        }
+        List<String> remoteClusters = replicationClusters.stream()
+                .filter(cluster -> !cluster.equals(localCluster))
+                .sorted()
+                .toList();
+        return findRemoteTopicMetadataForAutoCreation(topicName, 
remoteClusters, 0, null);
+    }
+
+    private CompletableFuture<TopicExistsInfo> 
findRemoteTopicMetadataForAutoCreation(
+            TopicName topicName, List<String> remoteClusters, int index, 
Throwable errOccurred) {
+        if (index >= remoteClusters.size()) {
+            if (errOccurred != null) {
+                log.error("[{}] Failed to check remote topic partitioned 
metadata on cluster {}. Fallback to "
+                    + "default auto topic creation policy.",
+                    topicName, remoteClusters, errOccurred);
+            }
+            return 
CompletableFuture.completedFuture(TopicExistsInfo.newTopicNotExists());
+        }
+        final String remoteCluster = remoteClusters.get(index);
+        return 
pulsar.getPulsarResources().getClusterResources().getClusterAsync(remoteCluster)
+            .thenCompose(clusterData -> {
+                if (clusterData.isEmpty()) {
+                    log.warn("[{}] Skip checking remote cluster {} because 
cluster data is missing",
+                            topicName, remoteCluster);
+                    return findRemoteTopicMetadataForAutoCreation(topicName, 
remoteClusters, index + 1, null);
+                }
+                PulsarClient client = getReplicationClient(remoteCluster, 
clusterData);
+                CompletableFuture<TopicExistsInfo> future = new 
CompletableFuture<>();
+                client.getPartitionsForTopic(topicName.toString(), 
false).handle((topics, t) -> {
+                    if (t != null) {
+                        Throwable actEx = 
FutureUtil.unwrapCompletionException(t);
+                        if (actEx instanceof 
PulsarClientException.NotFoundException
+                            | actEx instanceof 
PulsarClientException.TopicDoesNotExistException
+                            | actEx instanceof 
PulsarAdminException.NotFoundException) {
+                            
future.complete(TopicExistsInfo.newTopicNotExists());

Review Comment:
   Yes, take the first one as the standard. Since the second cluster has 
already been compared with the first cluster when creating the topic, we can 
assume that the old cluster is consistent



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