You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/11/02 21:41:11 UTC

[GitHub] sijie closed pull request #1778: [table service] add default port to service hosts

sijie closed pull request #1778: [table service] add default port to service hosts
URL: https://github.com/apache/bookkeeper/pull/1778
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/net/ServiceURI.java b/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/net/ServiceURI.java
index 4e2b158df4..3d419f3ccf 100644
--- a/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/net/ServiceURI.java
+++ b/bookkeeper-common/src/main/java/org/apache/bookkeeper/common/net/ServiceURI.java
@@ -27,6 +27,7 @@
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.net.URI;
 import java.util.List;
+import java.util.stream.Collectors;
 import lombok.AccessLevel;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
@@ -122,6 +123,7 @@
      * Service string for bookkeeper service.
      */
     public static final String SERVICE_BK = "bk";
+    public static final int SERVICE_BK_PORT = 4181;
 
     /**
      * The default local bk service uri.
@@ -160,7 +162,7 @@ public static ServiceURI create(String uriStr) {
     public static ServiceURI create(URI uri) {
         checkNotNull(uri, "service uri instance is null");
 
-        String serviceName = null;
+        String serviceName;
         String[] serviceInfos = new String[0];
         String scheme = uri.getScheme();
         if (null != scheme) {
@@ -175,6 +177,8 @@ public static ServiceURI create(URI uri) {
             serviceName = schemeParts[0];
             serviceInfos = new String[schemeParts.length - 1];
             System.arraycopy(schemeParts, 1, serviceInfos, 0, serviceInfos.length);
+        } else {
+            serviceName = null;
         }
 
         String userAndHostInformation = uri.getAuthority();
@@ -192,7 +196,10 @@ public static ServiceURI create(URI uri) {
             serviceUser = null;
             serviceHosts = splitter.splitToList(userAndHostInformation);
         }
-        serviceHosts.forEach(host -> validateHostName(host));
+        serviceHosts = serviceHosts
+            .stream()
+            .map(host -> validateHostName(serviceName, host))
+            .collect(Collectors.toList());
 
         String servicePath = uri.getPath();
         checkArgument(null != servicePath,
@@ -207,7 +214,7 @@ public static ServiceURI create(URI uri) {
             uri);
     }
 
-    private static void validateHostName(String hostname) {
+    private static String validateHostName(String serviceName, String hostname) {
         String[] parts = hostname.split(":");
         if (parts.length >= 3) {
             throw new IllegalArgumentException("Invalid hostname : " + hostname);
@@ -217,8 +224,12 @@ private static void validateHostName(String hostname) {
             } catch (NumberFormatException nfe) {
                 throw new IllegalArgumentException("Invalid hostname : " + hostname);
             }
+            return hostname;
+        } else if (parts.length == 1 && serviceName.toLowerCase().equals(SERVICE_BK)) {
+            return hostname + ":" + SERVICE_BK_PORT;
+        } else {
+            return hostname;
         }
-
     }
 
     private final String serviceName;
diff --git a/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/net/ServiceURITest.java b/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/net/ServiceURITest.java
index 9982c43a08..2ec83df32c 100644
--- a/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/net/ServiceURITest.java
+++ b/bookkeeper-common/src/test/java/org/apache/bookkeeper/common/net/ServiceURITest.java
@@ -154,7 +154,19 @@ public void testMultipleHostsWithoutPorts() {
             "bk",
             new String[0],
             null,
-            new String[] { "host1", "host2", "host3" },
+            new String[] { "host1:4181", "host2:4181", "host3:4181" },
+            "/path/to/namespace");
+    }
+
+    @Test
+    public void testMultipleHostsMixedPorts() {
+        String serviceUri = "bk://host1:3181,host2,host3:2181/path/to/namespace";
+        assertServiceUri(
+            serviceUri,
+            "bk",
+            new String[0],
+            null,
+            new String[] { "host1:3181", "host2:4181", "host3:2181" },
             "/path/to/namespace");
     }
 
@@ -166,7 +178,7 @@ public void testMultipleHostsMixed() {
             "bk",
             new String[0],
             null,
-            new String[] { "host1:2181", "host2", "host3:2181" },
+            new String[] { "host1:2181", "host2:4181", "host3:2181" },
             "/path/to/namespace");
     }
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services