Skip to content

Instantly share code, notes, and snippets.

@yuokada
Created November 5, 2011 18:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuokada/1341846 to your computer and use it in GitHub Desktop.
Save yuokada/1341846 to your computer and use it in GitHub Desktop.
consistent-hashing in python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import binascii
import md5
def search(nodelist, key):
max_len = len(nodelist)
keys = sorted( nodelist.keys() )
for i , nodekey in enumerate(keys):
if keys[i-1] < key <= keys[i]:
return nodelist[keys[i]]
return nodelist[keys[0]]
number_of_node = 128
crc = binascii.crc32
hash = md5.new()
node = ["n1","n2", "n3", "n4","n5", "n6", "n7"]
circle = {}
for no in node:
for i in range(number_of_node):
hash.update(no + '_%d' % i)
circle[hash.hexdigest()] = no
#print circle
for s in range(ord('A'), ord('Z')):
store = crc(chr(s)) % len(node)
#print chr(s), " : ", store
#print store , " : " , chr(s)
print chr(s) , " : ",
hash.update(chr(s))
print search(circle, hash.hexdigest() )
# http://8-p.info/consistent-hashing/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment