You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/12/14 13:00:22 UTC

[GitHub] [pulsar] liudezhi2098 commented on a diff in pull request #18838: [fix][client] Best effort resolve to reachable host

liudezhi2098 commented on code in PR #18838:
URL: https://github.com/apache/pulsar/pull/18838#discussion_r1048439630


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarServiceNameResolver.java:
##########
@@ -37,25 +39,42 @@ public class PulsarServiceNameResolver implements ServiceNameResolver {
 
     private volatile ServiceURI serviceUri;
     private volatile String serviceUrl;
-    private static final AtomicIntegerFieldUpdater<PulsarServiceNameResolver> CURRENT_INDEX_UPDATER =
-            AtomicIntegerFieldUpdater.newUpdater(PulsarServiceNameResolver.class, "currentIndex");
-    private volatile int currentIndex;
     private volatile List<InetSocketAddress> addressList;
 
+    private final Predicate<InetSocketAddress> predicate;
+
+    public PulsarServiceNameResolver() {
+        this(address -> {
+            final InetSocketAddress resolved = new InetSocketAddress(address.getHostName(), address.getPort());
+            return !resolved.isUnresolved();
+        });
+    }
+
+    @VisibleForTesting
+    public PulsarServiceNameResolver(Predicate<InetSocketAddress> predicate) {
+        this.predicate = predicate;
+    }
+
     @Override
     public InetSocketAddress resolveHost() {
-        List<InetSocketAddress> list = addressList;
-        checkState(
-            list != null, "No service url is provided yet");
-        checkState(
-            !list.isEmpty(), "No hosts found for service url : " + serviceUrl);
-        if (list.size() == 1) {
-            return list.get(0);
-        } else {
-            CURRENT_INDEX_UPDATER.getAndUpdate(this, last -> (last + 1) % list.size());
-            return list.get(currentIndex);
+        final List<InetSocketAddress> list = addressList;
+        final String url = serviceUrl;
+        checkState(list != null, "No service url is provided yet");
+        checkState(!list.isEmpty(), "No hosts found for service url : " + serviceUrl);
 
+        Collections.shuffle(list);

Review Comment:
   agree



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org