架构师训练营 week05 homework
发布于: 2020 年 07 月 08 日
code:
package consistHash;import java.util.*;/** * @since 2020/7/8 * <p> consistHash <p> */public class TestConsistHash { private SortedMap<Integer, String> sortedCircle = new TreeMap<>(); private static final int virtualCount = 100; private void mapVirtualNode(List<String> nodes) { Map<String, String> virtualNodes = new HashMap<>(); nodes.stream().forEach( n -> { for (int i = 0; i < virtualCount; i++) sortedCircle.put(hash(n+i), n); }); System.out.println(sortedCircle.size()); } private void addNode(String node) { for (int i = 0; i < virtualCount; i++) sortedCircle.put(hash(node + i), node); } private String findNode(String key) { SortedMap<Integer, String> subMap = sortedCircle.tailMap(hash(key)); return subMap.isEmpty() ? sortedCircle.get(sortedCircle.firstKey()) : subMap.get(subMap.firstKey()); } private void stat(Map<String, Object> data) { Map<String, Integer> result = new HashMap<>(); data.keySet().stream().forEach( key -> { String nodeName = findNode(key); if (result.get(nodeName) == null) { result.put(nodeName, 0); } result.put(nodeName, result.get(nodeName) + 1); }); for (String key : data.keySet()) { } System.out.println(result); System.out.println("标准差: " + stdDev(result, data.size())); } /** * 标准差计算 * * @param map * @param totalNums * @return */ private double stdDev(Map<String, Integer> map, long totalNums) { int n = map.size(); long avgNums = totalNums / map.size(); double sum = 0; for (String key : map.keySet()) { int value = map.get(key); sum += (value - avgNums) * (value - avgNums); } return Math.sqrt(sum / n); } /** */ public static int hash(String data) { final int p = 16777619; int hash = (int) 2166136261L; for (int i = 0; i < data.length(); i++) hash = (hash ^ data.charAt(i)) * p; hash += hash << 13; hash ^= hash >> 7; hash += hash << 3; hash ^= hash >> 17; hash += hash << 5; return hash; } public static void main(String[] args) { List<String> nodes = Arrays.asList("geek00", "geek01", "geek02", "geek03", "geek04", "geek05", "geek06", "geek07", "geek08", "geek09"); TestConsistHash hashCircle = new TestConsistHash(); hashCircle.mapVirtualNode(nodes); Map<String, Object> keyValus = new HashMap<>(); for (int i = 1; i <= 1000000; i++) { keyValus.put("key" + i, "value" + i); } hashCircle.stat(keyValus); hashCircle.addNode("geek10"); hashCircle.stat(keyValus); }}
result:
{geek01=102838, geek00=116363, geek03=90965, geek02=93704, geek05=95428, geek04=109234, geek07=105752, geek06=104185, geek09=87999, geek08=93532}
标准差: 8600.590375084725
{geek10=107300, geek01=100088, geek00=99767, geek03=79202, geek02=79320, geek05=85760, geek04=96465, geek07=94354, geek06=92415, geek09=84912, geek08=80417}
标准差: 9146.8093145295
划线
评论
复制
发布于: 2020 年 07 月 08 日 阅读数: 26
胡文强
关注
还未添加个人签名 2018.05.01 加入
还未添加个人简介
评论