This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new c2a8283  Support to specify multi ipv6 hosts in brokerServiceUrl 
(#8120)
c2a8283 is described below

commit c2a828324b72274f121a496a5082766501c4ae08
Author: WangJialing <[email protected]>
AuthorDate: Wed Sep 30 11:27:27 2020 +0800

    Support to specify multi ipv6 hosts in brokerServiceUrl (#8120)
    
    ### Motivation
    Fixes #8092
    
    ### Modifications
    
    split url string by comma before create URI object when handling multi ipv6 
hosts
    
    ### Verifying this change
    
    This change added tests and can be verified as follows:
    
      - add testMultiIpv6Uri() in ServiceURI
---
 .../org/apache/pulsar/common/net/ServiceURI.java   | 34 ++++++++++++++++++++++
 .../apache/pulsar/common/net/ServiceURITest.java   | 24 +++++++++++++++
 2 files changed, 58 insertions(+)

diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/net/ServiceURI.java 
b/pulsar-common/src/main/java/org/apache/pulsar/common/net/ServiceURI.java
index 81a2966..6053c97 100644
--- a/pulsar-common/src/main/java/org/apache/pulsar/common/net/ServiceURI.java
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/net/ServiceURI.java
@@ -26,6 +26,7 @@ import com.google.common.base.CharMatcher;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -67,6 +68,39 @@ public class ServiceURI {
     public static ServiceURI create(String uriStr) {
         checkNotNull(uriStr, "service uri string is null");
 
+        if (uriStr.contains("[") && uriStr.contains("]")) {
+            // deal with ipv6 address
+            Splitter splitter = Splitter.on(CharMatcher.anyOf(",;"));
+            List<String> hosts = splitter.splitToList(uriStr);
+
+            if (hosts.size() > 1) {
+                // deal with multi ipv6 hosts
+                String firstHost = hosts.get(0);
+                String lastHost = hosts.get(hosts.size() - 1);
+                boolean hasPath = lastHost.contains("/");
+                String path = hasPath ? 
lastHost.substring(lastHost.indexOf("/")) : "";
+                firstHost += path;
+
+                URI uri = URI.create(firstHost);
+                ServiceURI serviceURI = create(uri);
+
+                List<String> multiHosts = new ArrayList<>();
+                multiHosts.add(serviceURI.getServiceHosts()[0]);
+                multiHosts.addAll(hosts.subList(1, hosts.size()));
+                multiHosts = multiHosts
+                        .stream()
+                        .map(host -> 
validateHostName(serviceURI.getServiceName(), serviceURI.getServiceInfos(), 
host))
+                        .collect(Collectors.toList());
+                return new ServiceURI(
+                        serviceURI.getServiceName(),
+                        serviceURI.getServiceInfos(),
+                        serviceURI.getServiceUser(),
+                        multiHosts.toArray(new String[multiHosts.size()]),
+                        serviceURI.getServicePath(),
+                        serviceURI.getUri());
+            }
+        }
+
         // a service uri first should be a valid java.net.URI
         URI uri = URI.create(uriStr);
 
diff --git 
a/pulsar-common/src/test/java/org/apache/pulsar/common/net/ServiceURITest.java 
b/pulsar-common/src/test/java/org/apache/pulsar/common/net/ServiceURITest.java
index 05afcf3..1935c88 100644
--- 
a/pulsar-common/src/test/java/org/apache/pulsar/common/net/ServiceURITest.java
+++ 
b/pulsar-common/src/test/java/org/apache/pulsar/common/net/ServiceURITest.java
@@ -147,6 +147,30 @@ public class ServiceURITest {
     }
 
     @Test
+    public void testMultiIpv6Uri() {
+        String serviceUri = 
"pulsar://pulsaruser@[fec0:0:0:ffff::1]:6650,[fec0:0:0:ffff::2]:6650;[fec0:0:0:ffff::3]:6650/path/to/namespace";
+        assertServiceUri(
+                serviceUri,
+                "pulsar",
+                new String[0],
+                "pulsaruser",
+                new String[] { "[fec0:0:0:ffff::1]:6650", 
"[fec0:0:0:ffff::2]:6650", "[fec0:0:0:ffff::3]:6650" },
+                "/path/to/namespace");
+    }
+
+    @Test
+    public void testMultiIpv6UriWithoutPulsarPort() {
+        String serviceUri = 
"pulsar://pulsaruser@[fec0:0:0:ffff::1],[fec0:0:0:ffff::2];[fec0:0:0:ffff::3]/path/to/namespace";
+        assertServiceUri(
+                serviceUri,
+                "pulsar",
+                new String[0],
+                "pulsaruser",
+                new String[] { "[fec0:0:0:ffff::1]:6650", 
"[fec0:0:0:ffff::2]:6650", "[fec0:0:0:ffff::3]:6650" },
+                "/path/to/namespace");
+    }
+
+    @Test
     public void testMultipleHostsSemiColon() {
         String serviceUri = 
"pulsar://host1:6650;host2:6650;host3:6650/path/to/namespace";
         assertServiceUri(

Reply via email to