简介
现在一致性hash算法在分布式系统中也得到了广泛应用,研究过memcached缓存数据库的人都知道,memcached服务器端本身不提供分布式cache的一致性,而是由客户端来提供,具体在计算一致性hash时采用如下步骤:
首先求出memcached服务器(节点)的哈希值,并将其配置到0~2^32的圆(continuum)上。
采用同样的方法求出存储数据的键的哈希值,并映射到相同的圆上。
从数据映射到的位置开始顺时针查找,将数据保存到找到的第一个服务器上。如果超过2^32仍然找不到服务器,就会保存到第一台memcached服务器上。
从上图的状态中添加一台memcached服务器。余数分布式算法由于保存键的服务器会发生巨大变化而影响缓存的命中率,但Consistent Hashing中,只有在圆(continuum)上增加服务器的地点逆时针方向的第一台服务器上的键会受到影响,如下图所示:
一致性Hash性质
良好的分布式cahce系统中的一致性hash算法应该满足以下几个方面:
- 平衡性(Balance)
- 单调性(Monotonicity)
- 分散性(Spread)
- 负载(Load)
- 平滑性(Smoothness)
原理
基本概念
一致性哈希算法(Consistent Hashing)最早在论文《Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web》中被提出。
简单来说,一致性哈希将整个哈希值空间组织成一个虚拟的圆环,如假设某哈希函数H的值空间为0-2^32-1(即哈希值是一个32位无符号整形),整个哈希空间环如下:
整个空间按顺时针方向组织。0和2^32-1在零点中方向重合。
下一步将各个服务器使用Hash进行一个哈希,具体可以选择服务器的ip或主机名作为关键字进行哈希,这样每台机器就能确定其在哈希环上的位置,这里假设将上文中四台服务器使用ip地址哈希后在环空间的位置如下:
接下来使用如下算法定位数据访问到相应服务器:将数据key使用相同的函数Hash计算出哈希值,并确定此数据在环上的位置,从此位置沿环顺时针“行走”,第一台遇到的服务器就是其应该定位到的服务器。
例如我们有Object A、Object B、Object C、Object D四个数据对象,经过哈希计算后,在环空间上的位置如下:
根据一致性哈希算法,数据A会被定为到Node A上,B被定为到Node B上,C被定为到Node C上,D被定为到Node D上。
容错性
现假设Node C不幸宕机,可以看到此时对象A、B、D不会受到影响,只有C对象被重定位到Node D。
一般的,在一致性哈希算法中,如果一台服务器不可用,则受影响的数据仅仅是此服务器到其环空间中前一台服务器(即沿着逆时针方向行走遇到的第一台服务器)之间数据,其它不会受到影响。
可扩展性
如果在系统中增加一台服务器Node X,如下图所示:
此时对象Object A、B、D不受影响,只有对象C需要重定位到新的Node X 。
一般的,在一致性哈希算法中,如果增加一台服务器,则受影响的数据仅仅是新服务器到其环空间中前一台服务器(即沿着逆时针方向行走遇到的第一台服务器)之间数据,其它数据也不会受到影响。
综上所述,一致性哈希算法对于节点的增减都只需重定位环空间中的一小部分数据,具有较好的容错性和可扩展性。
数据倾斜问题
另外,一致性哈希算法在服务节点太少时,容易因为节点分部不均匀而造成数据倾斜问题。例如系统中只有两台服务器,其环分布如下:
此时必然造成大量数据集中到Node A上,而只有极少量会定位到Node B上。
为了解决这种数据倾斜问题,一致性哈希算法引入了虚拟节点机制,即对每一个服务节点计算多个哈希,每个计算结果位置都放置一个此服务节点,称为虚拟节点。
具体做法可以在服务器ip或主机名的后面增加编号来实现。例如上面的情况,可以为每台服务器计算三个虚拟节点,于是可以分别计算 “Node A#1”、“Node A#2”、“Node A#3”、“Node B#1”、“Node B#2”、“Node B#3”的哈希值,于是形成六个虚拟节点:
同时数据定位算法不变,只是多了一步虚拟节点到实际节点的映射,例如定位到“Node A#1”、“Node A#2”、“Node A#3”三个虚拟节点的数据均定位到Node A上。这样就解决了服务节点少时数据倾斜的问题。
在实际应用中,通常将虚拟节点数设置为32甚至更大,因此即使很少的服务节点也能做到相对均匀的数据分布。
代码测试
一致性Hash模拟类:
package com.example.demo.hash;
import java.util.*;
/**
* 一致性Hash
*
* @author gaochen
* @date 2019/5/29
*/
public class ConsistentHash<T> {
/**
* 节点的复制因子,实际节点个数 * numberOfReplicas
*/
private final int numberOfReplicas;
/**
* 虚拟节点个数,存储虚拟节点的hash值到真实节点的映射
*/
private final SortedMap<Integer, T> circle = new TreeMap<>();
public ConsistentHash(int numberOfReplicas, Collection<T> nodes) {
this.numberOfReplicas = numberOfReplicas;
for (T node : nodes) {
add(node);
}
}
/**
* 模拟添加一个节点
* <p>
* 对于一个实际机器节点 node, 对应 numberOfReplicas 个虚拟节点
* 不同的虚拟节点(i不同)有不同的hash值,但都对应同一个实际机器node
* 虚拟node一般是均衡分布在环上的,数据存储在顺时针方向的虚拟node上
* </P>
*
* @param node 哈希环节点
*/
public void add(T node) {
for (int i = 0; i < numberOfReplicas; i++) {
String nodestr = node.toString() + i;
int hashcode = nodestr.hashCode();
System.out.println("hashcode:" + hashcode);
circle.put(hashcode, node);
}
}
/**
* 删除一个节点
*
* @param node 待删除节点
*/
public void remove(T node) {
for (int i = 0; i < numberOfReplicas; i++) {
circle.remove((node.toString() + i).hashCode());
}
}
/**
* 获得一个最近的顺时针节点,根据给定的key 取Hash
* 然后再取得顺时针方向上最近的一个虚拟节点对应的实际节点
* 再从实际节点中取得 数据
*
* @param key 模拟缓存Key
*/
public T get(Object key) {
if (circle.isEmpty()) {
return null;
}
// node 用String来表示,获得node在哈希环中的hashCode
int hash = key.hashCode();
System.out.println("hashcode----->:" + hash);
//数据映射在两台虚拟机器所在环之间,就需要按顺时针方向寻找机器
if (!circle.containsKey(hash)) {
SortedMap<Integer, T> tailMap = circle.tailMap(hash);
hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
}
return circle.get(hash);
}
/**
* 获取当前哈希环节点数
*
* @return 哈希环节点数
*/
public long getSize() {
return circle.size();
}
/**
* 查看表示整个哈希环中各个虚拟节点位置
*/
public void showBalance() {
//获得TreeMap中所有的Key
Set<Integer> sets = circle.keySet();
//将获得的Key集合排序
SortedSet<Integer> sortedSets = new TreeSet<Integer>(sets);
for (Integer hashCode : sortedSets) {
System.out.println(hashCode);
}
System.out.println("----each location 's distance are follows: ----");
//查看相邻两个hashCode的差值
Iterator<Integer> it = sortedSets.iterator();
Iterator<Integer> it2 = sortedSets.iterator();
if (it2.hasNext()) {
it2.next();
}
long keyPre, keyAfter;
while (it.hasNext() && it2.hasNext()) {
keyPre = it.next();
keyAfter = it2.next();
System.out.println(keyAfter - keyPre);
}
}
}
测试代码:
package com.example.demo.hash;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* TODO
*
* @author gaochen
* @date 2019/5/29
*/
public class ConsistentHashTest {
private static ConsistentHash<String> consistentHash;
@Before
public void initHash() {
Set<String> nodes = new HashSet<>();
consistentHash = new ConsistentHash<>(2, nodes);
}
@Test
public void testBalance() {
// 分配三个节点
consistentHash.add("A1");
consistentHash.add("C1");
consistentHash.add("D1");
System.out.println("hash circle size: " + consistentHash.getSize());
System.out.println("location of each node are follows: ");
// consistentHash.showBalance();
// hash值在当前哈希环内
final String key1 = "A31";
// hash值超出了当前哈希环
final String key2 = "Apple";
final List<String> keys = Arrays.asList(key1, key2);
// 模拟节点分配
showAllocate(keys);
// 模拟增加节点, A31被分配到更近的B1节点
consistentHash.add("B1");
System.out.println("增加节点B1");
showAllocate(keys);
System.out.println("-------------------------------------");
// 模拟删除节点, A31被分配到更近的C1节点
consistentHash.remove("B1");
System.out.println("删除节点B1");
showAllocate(keys);
}
/**
* 模拟缓存分配
*
* @param keys 缓存键
*/
private void showAllocate(List<String> keys) {
keys.forEach(key -> {
String node = consistentHash.get(key);
// A31被分配到更近的C1节点
System.out.println(String.format("key %s is allocated to node %s", key, node));
});
}
}
控制台输出:
hashcode:64032
hashcode:64033
hashcode:65954
hashcode:65955
hashcode:66915
hashcode:66916
hash circle size: 6
location of each node are follows:
hashcode----->:64095
key A31 is allocated to node C1
hashcode----->:63476538
key Apple is allocated to node A1
hashcode:64993
hashcode:64994
增加节点B1
hashcode----->:64095
key A31 is allocated to node B1
hashcode----->:63476538
key Apple is allocated to node A1
-------------------------------------
删除节点B1
hashcode----->:64095
key A31 is allocated to node C1
hashcode----->:63476538
key Apple is allocated to node A1
可以看出,增加或删除节点,只会影响到节点与上一个节点之间的元素,所以一致性Hash算法在容错性和可扩展性上面较普通Hash是有巨大提升的。