Created
September 21, 2018 02:38
-
-
Save diegows/9584da68e04f665f2924cf380c71d4bd to your computer and use it in GitHub Desktop.
Hash load balance to a sub set of nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import random | |
from hash_ring import HashRing | |
from pprint import pprint | |
nodes = range(10) | |
nodes_dist = [] | |
for i in range(len(nodes)): | |
c = tuple(random.choice(nodes) for j in range(3)) | |
nodes_dist.append(c) | |
weights = dict() | |
for i in range(len(nodes)): | |
weights[nodes_dist[i]] = 1 | |
pprint(nodes_dist) | |
pprint(weights) | |
ring = HashRing(nodes_dist, weights) | |
for i in range(10): | |
print random.choice(ring.get_node("test")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool, after looking at hash_ring I think it uses the string representation of each nodes_dist entry to compute its key. So if you later add or remove a node from a group then its key could change and the list of sorted keys
_sorted_keys
could change. It may be more convenient to pass to HashRing a list of group names and keep a separate dict of group_name -> [nodes].