You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by si...@apache.org on 2018/11/03 00:47:42 UTC

[bookkeeper] branch master updated: [TABLE SERVICE] [PYTHON] add default port to service hosts

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 911a08f  [TABLE SERVICE] [PYTHON] add default port to service hosts
911a08f is described below

commit 911a08f7a3bcb82688f9d1a5b6ef7b86a615a2f9
Author: Sijie Guo <gu...@gmail.com>
AuthorDate: Fri Nov 2 17:47:37 2018 -0700

    [TABLE SERVICE] [PYTHON] add default port to service hosts
    
    Descriptions of the changes in this PR:
    
    *Motivation*
    
    Similar as #1778, add default port to service hosts if port is missing.
    
    *Changes*
    
    add default port to service hosts if they miss ports
    
    Related Issue: #1778
    
    
    
    
    Reviewers: Jia Zhai <None>
    
    This closes #1780 from sijie/fix_python_service_uri
---
 .../python/bookkeeper/common/service_uri.py        | 18 +++----
 stream/clients/python/bookkeeper/common/util.py    |  8 ++++
 .../unit/bookkeeper/common/test_service_uri.py     | 55 ++++++++++++++++++++++
 .../unit/bookkeeper/common/test_util.py}           | 15 ++----
 4 files changed, 78 insertions(+), 18 deletions(-)

diff --git a/stream/clients/python/bookkeeper/common/service_uri.py b/stream/clients/python/bookkeeper/common/service_uri.py
index ee8cd93..7cf7820 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 @@ class ServiceURI(object):
         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 54f11d9..cb47176 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 0000000..bcb0ca8
--- /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/bookkeeper/common/util.py b/stream/clients/python/tests/unit/bookkeeper/common/test_util.py
similarity index 65%
copy from stream/clients/python/bookkeeper/common/util.py
copy to stream/clients/python/tests/unit/bookkeeper/common/test_util.py
index 54f11d9..214d3f1 100644
--- a/stream/clients/python/bookkeeper/common/util.py
+++ b/stream/clients/python/tests/unit/bookkeeper/common/test_util.py
@@ -12,15 +12,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import sys
+from bookkeeper.common import util
 
-__PYTHON3__ = sys.version_info >= (3, 0)
 
-
-def to_bytes(n, length, endianess='big'):
-    if __PYTHON3__:
-        return n.to_bytes(length, endianess)
-    else:
-        h = '%x' % n
-        s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
-        return s if endianess == 'big' else s[::-1]
+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)