Technoboy- commented on code in PR #20551:
URL: https://github.com/apache/pulsar/pull/20551#discussion_r1226313332


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/ExtensibleLoadManagerImpl.java:
##########
@@ -342,56 +344,101 @@ public CompletableFuture<Optional<BrokerLookupData>> 
assign(Optional<ServiceUnit
 
         final String bundle = serviceUnit.toString();
 
-        CompletableFuture<Optional<BrokerLookupData>> future = 
lookupRequests.computeIfAbsent(bundle, k -> {
+        return dedupeLookupRequest(bundle, k -> {
             final CompletableFuture<Optional<String>> owner;
             // Assign the bundle to channel owner if is internal topic, to 
avoid circular references.
             if (topic.isPresent() && isInternalTopic(topic.get().toString())) {
                 owner = serviceUnitStateChannel.getChannelOwnerAsync();
             } else {
-                owner = 
serviceUnitStateChannel.getOwnerAsync(bundle).thenCompose(broker -> {
-                    // If the bundle not assign yet, select and publish assign 
event to channel.
-                    if (broker.isEmpty()) {
-                        return 
this.selectAsync(serviceUnit).thenCompose(brokerOpt -> {
-                            if (brokerOpt.isPresent()) {
-                                assignCounter.incrementSuccess();
-                                log.info("Selected new owner broker: {} for 
bundle: {}.", brokerOpt.get(), bundle);
-                                return 
serviceUnitStateChannel.publishAssignEventAsync(bundle, brokerOpt.get())
-                                        .thenApply(Optional::of);
-                            } else {
-                                throw new IllegalStateException(
-                                        "Failed to select the new owner broker 
for bundle: " + bundle);
-                            }
-                        });
+                owner = getOwnerAsync(serviceUnit, bundle, false);
+            }
+            return getBrokerLookupData(owner, bundle);
+        });
+    }
+
+    private CompletableFuture<Optional<String>> getOwnerAsync(
+            ServiceUnitId serviceUnit, String bundle, boolean 
ownByLocalBrokerIfAbsent) {
+        return 
serviceUnitStateChannel.getOwnerAsync(bundle).thenCompose(broker -> {
+            // If the bundle not assign yet, select and publish assign event 
to channel.
+            if (broker.isEmpty()) {
+                CompletableFuture<Optional<String>> selectedBroker;
+                if (ownByLocalBrokerIfAbsent){
+                    String brokerId = this.brokerRegistry.getBrokerId();
+                    selectedBroker = 
CompletableFuture.completedFuture(Optional.of(brokerId));
+                } else {
+                    selectedBroker = this.selectAsync(serviceUnit);
+                }
+                return selectedBroker.thenCompose(brokerOpt -> {
+                    if (brokerOpt.isPresent()) {
+                        assignCounter.incrementSuccess();
+                        log.info("Selected new owner broker: {} for bundle: 
{}.", brokerOpt.get(), bundle);
+                        return 
serviceUnitStateChannel.publishAssignEventAsync(bundle, brokerOpt.get())
+                                .thenApply(Optional::of);
+                    } else {
+                        throw new IllegalStateException(
+                                "Failed to select the new owner broker for 
bundle: " + bundle);
                     }
-                    assignCounter.incrementSkip();
-                    // Already assigned, return it.
-                    return CompletableFuture.completedFuture(broker);
                 });
             }
+            assignCounter.incrementSkip();
+            // Already assigned, return it.
+            return CompletableFuture.completedFuture(broker);
+        });
+    }
 
-            return owner.thenCompose(broker -> {
-                if (broker.isEmpty()) {
-                    String errorMsg = String.format(
-                            "Failed to get or assign the owner for bundle:%s", 
bundle);
-                    log.error(errorMsg);
-                    throw new IllegalStateException(errorMsg);
-                }
-                return CompletableFuture.completedFuture(broker.get());
-            }).thenCompose(broker -> 
this.getBrokerRegistry().lookupAsync(broker).thenCompose(brokerLookupData -> {
-                if (brokerLookupData.isEmpty()) {
-                    String errorMsg = String.format(
-                            "Failed to look up a broker registry:%s for 
bundle:%s", broker, bundle);
-                    log.error(errorMsg);
-                    throw new IllegalStateException(errorMsg);
-                }
-                return CompletableFuture.completedFuture(brokerLookupData);
-            }));
+    private CompletableFuture<Optional<BrokerLookupData>> getBrokerLookupData(

Review Comment:
   yes, the same idea with @mattisonchao 



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