support unicode node name

hashring fails if node name is unicode in python2

Change-Id: I12cdd455ea233233bbcd6e3c46db3d8d6077c6af
This commit is contained in:
gord chung 2017-02-14 18:07:02 +00:00
parent 634ee67dde
commit fbfa1c1162
2 changed files with 12 additions and 2 deletions

View File

@ -68,7 +68,7 @@ class HashRing(object):
will each handle 1/6, 1/3 and 1/2 of the resources, respectively.
"""
for node in nodes:
key = str(node).encode('utf-8')
key = six.text_type(node).encode('utf-8')
key_hash = hashlib.md5(key)
for r in six.moves.range(self._partition_number * weight):
key_hash.update(key)
@ -90,7 +90,7 @@ class HashRing(object):
except KeyError:
raise UnknownNode(node)
key = str(node).encode('utf-8')
key = six.text_type(node).encode('utf-8')
key_hash = hashlib.md5(key)
for r in six.moves.range(self._partition_number * weight):
key_hash.update(key)

View File

@ -61,6 +61,16 @@ class HashRingTestCase(testcase.TestCase):
self.assertEqual(nodes, set(ring.nodes.keys()))
self.assertEqual(2 ** 5 * len(nodes), len(ring))
def test_add_node_unicode(self):
nodes = {'foo', 'bar'}
ring = hashring.HashRing(nodes)
self.assertEqual(nodes, set(ring.nodes.keys()))
self.assertEqual(2 ** 5 * len(nodes), len(ring))
nodes.add(u'\u0634\u0628\u06a9\u0647')
ring.add_node(u'\u0634\u0628\u06a9\u0647')
self.assertEqual(nodes, set(ring.nodes.keys()))
self.assertEqual(2 ** 5 * len(nodes), len(ring))
def test_add_node_weight(self):
nodes = {'foo', 'bar'}
ring = hashring.HashRing(nodes)