A clone of the last published version (version 1.3.1) of Python hash_ring from PyPi, written by Amir Salihefendic. I created this clone since we needed to continue using the original project in Python 3.x, and the original had stopped being maintained, and was stuck at Python 2.x.
The only modifications I have made is to port this to Python 3.8, and remove the
memcache_ring
module, since we never use it, and it was not practical for me to test
it out.
Currently, I have no plans to support this long-term, since this is a temporary measure to port our own code to Python 3.x as painlessly as possible; in future, we might move to using a different consistent hashing implementation in Python, at which time I will drop support for this.
The following description is mostly from the original package.
Implements consistent hashing that can be used when the number of server nodes can increase or decrease (like in memcached). The hashing ring is built using the same algorithm as libketama.
Consistent hashing is a scheme that provides a hash table functionality in a way that the adding or removing of one slot does not significantly change the mapping of keys to slots.
More information about consistent hashing can be read in these articles:
Basic example of usage:
servers = ['192.168.0.246:11212',
'192.168.0.247:11212',
'192.168.0.249:11212']
ring = HashRing(servers)
server = ring.get_node('my_key')
Example using weights:
servers = ['192.168.0.246:11212',
'192.168.0.247:11212',
'192.168.0.249:11212']
weights = {
'192.168.0.246:11212': 1,
'192.168.0.247:11212': 2,
'192.168.0.249:11212': 1
}
ring = HashRing(servers, weights)
server = ring.get_node('my_key')
Assuming you are on a Unix-like system:
$ git clone https://github.com/apramanick/hash_ring.git
$ cd hash_ring
$ pip3.8 install .
The original hash_ring project had no unit tests, and this one doesn't have any either. However, I have added a simple, quick sanity check to gain some amount of confidence that the Python 3.8-ported code is functionally equivalent to the original Python 2.x code. To run this (assuming you are on a Unix-like system):
cd tests
./test_port.sh <path-to-your-python2-interpreter> <path-to-your-python3-interpreter>