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/08/15 04:00:42 UTC

[GitHub] [pulsar] coderzc commented on a diff in pull request #17082: [fix][broker] Skip not connectable URL when get redirectionUrl

coderzc commented on code in PR #17082:
URL: https://github.com/apache/pulsar/pull/17082#discussion_r945411974


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/web/PulsarWebResource.java:
##########
@@ -493,13 +502,47 @@ private URI getRedirectionUrl(ClusterData differentClusterData) throws Malformed
             } else {
                 serviceNameResolver.updateServiceUrl(differentClusterData.getServiceUrl());
             }
-            URL webUrl = new URL(serviceNameResolver.resolveHostUri().toString());
-            return UriBuilder.fromUri(uri.getRequestUri()).host(webUrl.getHost()).port(webUrl.getPort()).build();
+            return getConnectableURI(serviceNameResolver);
         } catch (PulsarClientException.InvalidServiceURL exception) {
             throw new MalformedURLException(exception.getMessage());
         }
     }
 
+    private CompletableFuture<URI> getConnectableURI(PulsarServiceNameResolver serviceNameResolver) {
+        return CompletableFuture.supplyAsync(() -> {
+            String hostUri = serviceNameResolver.resolveHostUri().toString();
+            String firstHostUri = hostUri;
+            boolean connectable = false;
+            URL webUrl = null;
+            do {
+                try {
+                    webUrl = new URL(hostUri);
+                    Socket socket = new Socket();
+                    socket.connect(new InetSocketAddress(webUrl.getHost(), webUrl.getPort()), 5000);
+                    socket.close();
+                    connectable = true;
+                } catch (Exception e) {
+                    log.warn("Unable connect to url:{}", hostUri);
+                    hostUri = serviceNameResolver.resolveHostUri().toString();
+
+                    if (firstHostUri.equals(hostUri)) {
+                        break;
+                    }
+                }
+            } while (!connectable);
+
+            if (webUrl == null) {
+                try {
+                    webUrl = new URL(hostUri);
+                } catch (MalformedURLException e) {
+                    throw new RestException(e);
+                }
+            }
+
+            return UriBuilder.fromUri(uri.getRequestUri()).host(webUrl.getHost()).port(webUrl.getPort()).build();
+        }, pulsar.getExecutor());
+    }
+

Review Comment:
   > We may add this logic to PulsarServiceNameResolver
   
   This seems odd, since the method also needs to verify URI is an HTTP protocol



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