consistent-hash
consistent-hash copied to clipboard
不同的node生成的key有重叠的话,在删除时其中一个node后会导致keys和key_node数据不一致
在使用这个代码时,发现
len(._consistent_hash.keys) 13920
len(._consistent_hash.key_node) 13919
在get_node时,出现KeyError错误 def get_node(self, string_key): """Given a string key a corresponding node in the hash ring is returned.
If the hash ring is empty, `None` is returned.
"""
pos = self.get_node_pos(string_key)
if pos is None:
return None
return self.key_node[self.keys[pos]]
怀疑点 m = hashlib.md5()有冲突的概率
解决方案 def del_nodes(self, nodes): """ Deletes nodes from the ring.
Nodes is expected to be a list of nodes already
present in the ring.
"""
try:
if not isinstance(nodes, list):
raise TypeError("The arguments of nodes must be list.")
except TypeError:
traceback.print_exc(file=sys.stdout)
# Delete nodes from the ring.
for node in nodes:
if node not in self.nodes:
continue
for key in self._node_keys(node):
self.keys.remove(key)
del self.key_node[key]
self.index -= 1
self.nodes.remove(node)
在 self.keys.remove(key)和del self.key_node[key]时,是不是把self.keys.remove(key)都清理了。