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/03 00:47:39 UTC

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

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

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/stream/clients/python/bookkeeper/common/service_uri.py b/stream/clients/python/bookkeeper/common/service_uri.py
index ee8cd93070..7cf782086b 100644
--- a/stream/clients/python/bookkeeper/common/service_uri.py
+++ b/stream/clients/python/bookkeeper/common/service_uri.py
@@ -10,14 +10,13 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import sys
+from bookkeeper.common.util import __PYTHON3__
+from bookkeeper.common.util import new_hostname_with_port
 
-if sys.version_info[0] < 3:
-    USE_PYTHON3 = False
-    from urlparse import urlparse
-else:
-    USE_PYTHON3 = True
+if __PYTHON3__:
     from urllib.parse import urlparse
+else:
+    from urlparse import urlparse
 
 
 class ServiceURI(object):
@@ -27,5 +26,8 @@ def __init__(self, service_uri):
         self.service_name = self.uri.scheme
         self.service_user = self.uri.username
         self.service_path = self.uri.path
-        self.service_location = self.uri.netloc
-        self.service_hosts = self.uri.netloc.split(',')
+        if __PYTHON3__:
+            self.service_hosts = list(map(lambda x: new_hostname_with_port(x), self.uri.netloc.split(',')))
+        else:
+            self.service_hosts = map(lambda x: new_hostname_with_port(x), self.uri.netloc.split(','))
+        self.service_location = ','.join(self.service_hosts)
diff --git a/stream/clients/python/bookkeeper/common/util.py b/stream/clients/python/bookkeeper/common/util.py
index 54f11d9621..cb47176406 100644
--- a/stream/clients/python/bookkeeper/common/util.py
+++ b/stream/clients/python/bookkeeper/common/util.py
@@ -24,3 +24,11 @@ def to_bytes(n, length, endianess='big'):
         h = '%x' % n
         s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
         return s if endianess == 'big' else s[::-1]
+
+
+def new_hostname_with_port(hostname, default_port=4181):
+    host_parts = hostname.split(':')
+    if len(host_parts) > 1:
+        return hostname
+    else:
+        return "%s:%d" % (hostname, default_port)
diff --git a/stream/clients/python/tests/unit/bookkeeper/common/test_service_uri.py b/stream/clients/python/tests/unit/bookkeeper/common/test_service_uri.py
new file mode 100644
index 0000000000..bcb0ca876a
--- /dev/null
+++ b/stream/clients/python/tests/unit/bookkeeper/common/test_service_uri.py
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from bookkeeper.common.service_uri import ServiceURI
+
+
+def test_service_uri_one_host_without_port():
+    uri = ServiceURI("bk://127.0.0.1")
+    assert "bk" == uri.service_name
+    assert uri.service_user is None
+    assert "127.0.0.1:4181" == uri.service_location
+    assert ["127.0.0.1:4181"] == uri.service_hosts
+
+
+def test_service_uri_one_host_with_port():
+    uri = ServiceURI("bk://127.0.0.1:3181")
+    assert "bk" == uri.service_name
+    assert uri.service_user is None
+    assert "127.0.0.1:3181" == uri.service_location
+    assert ["127.0.0.1:3181"] == uri.service_hosts
+
+
+def test_service_uri_multiple_hosts_with_port():
+    uri = ServiceURI("bk://127.0.0.1:3181,127.0.0.2:4181,127.0.0.3:5181")
+    assert "bk" == uri.service_name
+    assert uri.service_user is None
+    assert "127.0.0.1:3181,127.0.0.2:4181,127.0.0.3:5181" == uri.service_location
+    assert ["127.0.0.1:3181", "127.0.0.2:4181", "127.0.0.3:5181"] == uri.service_hosts
+
+
+def test_service_uri_multiple_hosts_without_port():
+    uri = ServiceURI("bk://127.0.0.1,127.0.0.2,127.0.0.3")
+    assert "bk" == uri.service_name
+    assert uri.service_user is None
+    assert "127.0.0.1:4181,127.0.0.2:4181,127.0.0.3:4181" == uri.service_location
+    assert ["127.0.0.1:4181", "127.0.0.2:4181", "127.0.0.3:4181"] == uri.service_hosts
+
+
+def test_service_uri_multiple_hosts_mixed_with_and_without_port():
+    uri = ServiceURI("bk://127.0.0.1:3181,127.0.0.2,127.0.0.3:5181")
+    assert "bk" == uri.service_name
+    assert uri.service_user is None
+    assert "127.0.0.1:3181,127.0.0.2:4181,127.0.0.3:5181" == uri.service_location
+    assert ["127.0.0.1:3181", "127.0.0.2:4181", "127.0.0.3:5181"] == uri.service_hosts
diff --git a/stream/clients/python/tests/unit/bookkeeper/common/test_util.py b/stream/clients/python/tests/unit/bookkeeper/common/test_util.py
new file mode 100644
index 0000000000..214d3f19d1
--- /dev/null
+++ b/stream/clients/python/tests/unit/bookkeeper/common/test_util.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from bookkeeper.common import util
+
+
+def test_new_hostname_with_port():
+    assert "127.0.0.1:3181" == util.new_hostname_with_port("127.0.0.1:3181")
+    assert "127.0.0.1:4181" == util.new_hostname_with_port("127.0.0.1")
+    assert "127.0.0.1:2181" == util.new_hostname_with_port("127.0.0.1", 2181)


 

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