第五周作业
发布于: 2020 年 11 月 15 日
作业一(2 选 1):
用你熟悉的编程语言实现一致性 hash 算法。
编写测试用例测试这个算法,测试 100 万 KV 数据,10 个服务器节点的情况下,计算这些 KV 数据在服务器上分布数量的标准差,以评估算法的存储负载不均衡性。
package HashWork;
import java.util.*;
public class HashWork {
static String[] servers = {
"192.168.0.1:100",
"192.168.0.1:101",
"192.168.0.1:102",
"192.168.0.1:103",
"192.168.0.1:104",
"192.168.0.1:105",
"192.168.0.1:106",
"192.168.0.1:107",
"192.168.0.1:108",
"192.168.0.1:109",
"192.168.0.1:110"
};
private static SortedMap<Integer, String> integerStringTreeMap = new TreeMap<Integer, String>();
static {
for (int i=0; i<servers.length; i++) {
int hash = getHash(servers[i]);
System.out.println("[" + servers[i] + "]Join, Hash is" + hash);
integerStringTreeMap.put(hash, servers[i]);
}
System.out.println();
}
private static String getServer(String key) {
int hash = getHash(key);
SortedMap<Integer, String> subMap = integerStringTreeMap.tailMap(hash);
if(subMap.isEmpty()){
Integer i = integerStringTreeMap.firstKey();
return integerStringTreeMap.get(i);
}else{
Integer i = subMap.firstKey();
return subMap.get(i);
}
}
private static int getHash(String str) {
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;
}
public static void main(String[] args) {
HashMap<String,Integer> serVerCountMap =new HashMap<>();
for (String serverId :servers){
serVerCountMap.put(serverId,0);
}
Random rd=new Random();
for (int i=0 ;i<1000000;i++){
String key=i+ "" +rd.nextGaussian();
String serName = getServer(key);
Integer val=Integer.parseUnsignedInt(serVerCountMap.get(serName).toString())+1;
serVerCountMap.put(serName,val);
}
for (String serverId :servers){
System.out.printf("%s count is %s \r\n",serverId,serVerCountMap.get(serverId));
}
}
}
复制代码
结果
/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=55857:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/lib/tools.jar:/Users/siweichen/sstudry/java/Http/target/classes HashWork.HashWork
[192.168.0.1:100]Join, Hash is1487223403
[192.168.0.1:101]Join, Hash is1702253343
[192.168.0.1:102]Join, Hash is768187498
[192.168.0.1:103]Join, Hash is1379281766
[192.168.0.1:104]Join, Hash is1385108307
[192.168.0.1:105]Join, Hash is1938442671
[192.168.0.1:106]Join, Hash is765430537
[192.168.0.1:107]Join, Hash is1862988068
[192.168.0.1:108]Join, Hash is2002040801
[192.168.0.1:109]Join, Hash is598304130
[192.168.0.1:110]Join, Hash is1630529759
192.168.0.1:100 count is 47965
192.168.0.1:101 count is 33471
192.168.0.1:102 count is 1316
192.168.0.1:103 count is 284706
192.168.0.1:104 count is 2683
192.168.0.1:105 count is 35082
192.168.0.1:106 count is 77941
192.168.0.1:107 count is 74526
192.168.0.1:108 count is 29645
192.168.0.1:109 count is 346130
192.168.0.1:110 count is 66535
复制代码
貌似并不均衡..暂时记录
划线
评论
复制
发布于: 2020 年 11 月 15 日阅读数: 31
willson
关注
还未添加个人签名 2018.03.08 加入
还未添加个人简介
评论