在分布式缓存或者存储系统中经常都会用到hash算法,最早的时候memeched开源客户端都用的简单取余的hash算法来做分布式缓存集群中的命中要缓存的机器,一致性hash算法的原理在这里就不用描述了网上很多资料,下面的图来回忆下原理。简单的说就是把所有的机器结点投放到0-2^32-1结点圆环上去。再对key做哈希运算找到环上的结点存储。因为把有限几个阶段分布到2^32个几点上去需要均匀命中,会在实际几点中间虚拟很多结点。n1和n2之间的虚拟结点命中之后,会在n2上存储。
最近看了下jedis里面对这个功能的实现,是有些变通的做法来实现,下满的方法initialize
参数是实际结点列表,nodes = new TreeMap<Long, S>();为hash圆环,每个结点通过hash之后都散落在nodes上,客户端端取实际要使用的结点是通过nodes.tailMap(algo.hash(key));取环上顺时针比当前key大的结点map,在通过 tail.get(tail.firstKey());取这个map上第一个key所在虚拟结点上的值,这个虚拟结点与顺时针第一个相邻的结点实际连接主机是一样。
下面这个算法是讲key 先md5然后取hash
private void initialize(List<S> shards) {
nodes = new TreeMap<Long, S>();
for (int i = 0; i != shards.size(); ++i) {
final S shardInfo = shards.get(i);
if (shardInfo.getName() == null)
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n),
shardInfo);
}
else
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
nodes.put(
this.algo.hash(shardInfo.getName() + "*"
+ shardInfo.getWeight() + n), shardInfo);
}
resources.put(shardInfo, shardInfo.createResource());
}
}
public R getShard(byte[] key) {
return resources.get(getShardInfo(key));
}
public R getShard(String key) {
return resources.get(getShardInfo(key));
}
public S getShardInfo(byte[] key) {
SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key));
if (tail.isEmpty()) {
return nodes.get(nodes.firstKey());
}
return tail.get(tail.firstKey());
}
通过算法看和我们在文章上看的hash算法还是有点变通,文章中图上实际节点和虚拟几点都会是连续,程序算法中,虚拟结点是随机(作了md5)分布到环上的实际对应的主机是一个主机。因为同一个虚拟结点随机分布在环上,取主机的时候只要取比hash(md5(key))大的结点上第一个结点对应的主机就可以,并不关心是node1 还是node2...