simplify hashring node lookup

current code uses a nested loop which makes additional _get_node
calls needlessly when replicas > 1. this patch replaces it with a
simpler single loop.

Change-Id: I6bb5753ba1cef489648d7d1a75374cbb63ac5656
This commit is contained in:
gord chung 2017-04-01 18:37:17 +00:00 committed by gordon chung
parent 9f87cf158d
commit 1917f5ea27
1 changed files with 5 additions and 8 deletions

View File

@ -127,15 +127,12 @@ class HashRing(object):
replicas = min(replicas, len(candidates))
nodes = set()
for replica in six.moves.range(0, replicas):
while len(nodes) < replicas:
node = self._get_node(partition)
while node in nodes or node in ignore_nodes:
partition += 1
if partition >= len(self._partitions):
partition = 0
node = self._get_node(partition)
nodes.add(node)
if node not in ignore_nodes:
nodes.add(node)
partition = (partition + 1
if partition + 1 < len(self._partitions) else 0)
return nodes
def __getitem__(self, key):