Hash Ring

Hash Ring
  Tooz提供了一个连续哈希环(consitent hash ring)的实现。连续哈希环用来将对象映射到一个或者多个节点上。当节点变化时(添加节点或者节点被删除),可以重新平衡对象的分布,并且减少对象迁移。
  下面的例子展示了连续哈希环的基本使用。

from tooz import hashring

hashring = hashring.HashRing({'node1', 'node2', 'node3'})

nodes_for_foo = hashring[b'foo']
print('nodes_for_foo: %r' % nodes_for_foo)


nodes_for_foo = hashring.get_nodes(b'foo')
print('nodes_for_foo: %r' % nodes_for_foo)

nodes_for_foo_with_replicas = hashring.get_nodes(b'foo',
                                                 replicas=2)
print('nodes_for_foo_with_replicas: %r' % nodes_for_foo_with_replicas)

nodes_for_foo_with_replicas_ignore = hashring.get_nodes(b'foo',
                                                 replicas=2,
                                                 ignore_nodes={'node2'})
print('nodes_for_foo_with_replicas_ignore: %r' % nodes_for_foo_with_replicas_ignore)

  程序输出:

nodes_for_foo: set(['node2'])
nodes_for_foo: set(['node2'])
nodes_for_foo_with_replicas: set(['node3', 'node2'])
nodes_for_foo_with_replicas_ignore: set(['node1', 'node3'])

参考资料:
  Hash ring