第五周作业
代码:
package dfdf;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
public class ConsistentHashingWithVirtualNode {
//待添加入Hash环的服务器列表
private static String[] servers = {"192.168.0.0:111", "192.168.0.1:111", "192.168.0.2:111",
"192.168.0.3:111", "192.168.0.4:111", "192.168.0.5:111", "192.168.0.6:111", "192.168.0.7:111"
, "192.168.0.8:111", "192.168.0.9:111"};
//真实结点列表,考虑到服务器上线、下线的场景,即添加、删除的场景会比较频繁,这里使用LinkedList会更好
private static List<String> realNodes = new LinkedList<String>();
//虚拟节点,key表示虚拟节点的hash值,value表示虚拟节点的名称
private static SortedMap<Integer, String> virtualNodes = new TreeMap<Integer, String>();
//虚拟节点的数目,这里写死,为了演示需要,一个真实结点对应5个虚拟节点,分别测试5 50 150 200
private static final int VIRTUAL_NODES = 5;
//计数Map
private static Map<String,Integer> countMap = new HashMap<>();
static{
//先把原始的服务器添加到真实结点列表中
for(int i=0; i<servers.length; i++) {
realNodes.add(servers[i]);
countMap.put(servers[i], 0);
}
//再添加虚拟节点,遍历LinkedList使用foreach循环效率会比较高
for (String str : realNodes){
for(int i=0; i<VIRTUAL_NODES; i++){
String virtualNodeName = str + "&&VN" + String.valueOf(i);
int hash = getHash(virtualNodeName);
// System.out.println("虚拟节点[" + virtualNodeName + "]被添加, hash值为" + hash);
virtualNodes.put(hash, virtualNodeName);
}
}
System.out.println();
}
//使用FNV132HASH算法计算服务器的Hash值,这里不使用重写hashCode的方法,最终效果没区别
private static int getHash(String str){
final int p = 16777619;
int hash = (int)2166136261L;
for (int i = 0; i < str.length(); i++)
hash = (hash ^ str.charAt(i)) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
// 如果算出来的值为负数则取其绝对值
if (hash < 0)
hash = Math.abs(hash);
return hash;
}
//得到应当路由到的结点
private static String getServer(String key){
//得到该key的hash值
int hash = getHash(key);
// 得到大于该Hash值的所有Map
SortedMap<Integer, String> subMap = virtualNodes.tailMap(hash);
String virtualNode;
if(subMap.isEmpty()){
//如果没有比该key的hash值大的,则从第一个node开始
Integer i = virtualNodes.firstKey();
//返回对应的服务器
virtualNode = virtualNodes.get(i);
}else{
//第一个Key就是顺时针过去离node最近的那个结点
Integer i = subMap.firstKey();
//返回对应的服务器
virtualNode = subMap.get(i);
}
//virtualNode虚拟节点名称要截取一下
if(virtualNode!=null&&!virtualNode.equals("")){
return virtualNode.substring(0, virtualNode.indexOf("&&"));
}
return null;
}
public static void main(String[] args){
String[] keys = new String[1000000];
for (int i = 0; i < 1000000; i++) {
keys[i]=UUID.randomUUID().toString();
}
for(int i=0; i<keys.length; i++) {
String serverHit = getServer(keys[i]);
Integer hits = countMap.get(serverHit);
hits+=1;
countMap.put(serverHit, hits);
}
System.out.println(countMap);
}
}
100万 key=UUID 10个节点 每个节点分别测试5,50,150,200个虚拟节点,测试每种情况中10个节点中有多少key
5个虚拟节点
{192.168.0.2:111=132902, 192.168.0.8:111=135276, 192.168.0.7:111=70688, 192.168.0.6:111=57043, 192.168.0.1:111=119179, 192.168.0.4:111=78256, 192.168.0.3:111=119075, 192.168.0.5:111=84464, 192.168.0.0:111=148564, 192.168.0.9:111=54553}
标准差 = 33016.94
50个虚拟节点
{192.168.0.2:111=115959, 192.168.0.8:111=119115, 192.168.0.7:111=108113, 192.168.0.6:111=89620, 192.168.0.1:111=89393, 192.168.0.4:111=87844, 192.168.0.3:111=74527, 192.168.0.5:111=107041, 192.168.0.0:111=101530, 192.168.0.9:111=106858}
标准差 =13422.88
150个虚拟节点
{192.168.0.2:111=108739, 192.168.0.8:111=88274, 192.168.0.7:111=98887, 192.168.0.6:111=90914, 192.168.0.1:111=111984, 192.168.0.4:111=115909, 192.168.0.3:111=86926, 192.168.0.5:111=100060, 192.168.0.0:111=104619, 192.168.0.9:111=93688}
标准差 =9625.412
200个虚拟节点
{192.168.0.2:111=90750, 192.168.0.8:111=83137, 192.168.0.7:111=109141, 192.168.0.6:111=87268, 192.168.0.1:111=111385, 192.168.0.4:111=112254, 192.168.0.3:111=94694, 192.168.0.5:111=107166, 192.168.0.0:111=102554, 192.168.0.9:111=101651}
标准差 =9920.27
标准差使用excel STDEV.P 公式计算
5个时数据分布很不均衡,50个时比5个有很大改善,150-200个基本分布均衡
评论