Skip to content

Instantly share code, notes, and snippets.

@diegows
Created September 21, 2018 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegows/9584da68e04f665f2924cf380c71d4bd to your computer and use it in GitHub Desktop.
Save diegows/9584da68e04f665f2924cf380c71d4bd to your computer and use it in GitHub Desktop.
Hash load balance to a sub set of nodes
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"))
@miguelgarcia
Copy link

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

import collections
import random
from hash_ring import HashRing

from pprint import pprint

nodes = range(10)
groups_dist = []

groups_count = 10
for i in range(groups_count):
    groups_dist.append("group %d" % i)

group_nodes = dict()
for i in range(groups_count):
    group_nodes["group %d" % i] = [random.choice(nodes) for j in range(3)]
    
weights = dict()
for i in range(len(nodes)):
    weights[groups_dist[i]] = 1
    
pprint(groups_dist)
pprint(weights)

ring = HashRing(groups_dist, weights)

for i in range(10):
    print random.choice(group_nodes[ring.get_node("test")])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment