写点什么

第五周 实现一致性 hash 算法

用户头像
Geek_fabd84
关注
发布于: 2020 年 10 月 24 日



package com.demo;
import java.util.HashSet;
import java.util.Set;
/******************************
* 用途说明: 服务器节点
* 作者姓名: chenshijie
* 创建时间: 2020/10/24 02:02
******************************/
public class Node {
private int nodeId;
private Set<String> keys = new HashSet<String>();
public Node(int nodeId) {
this.nodeId = nodeId;
}
public void put(String key,String value){
keys.add(key);
}
public int getSize() {
return keys.size();
}
public int getNodeId() {
return nodeId;
}
public void setNodeId(int nodeId) {
this.nodeId = nodeId;
}
public Set<String> getKeys() {
return keys;
}
public void setKeys(Set<String> keys) {
this.keys = keys;
}
}



package com.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/******************************
* 用途说明: 节点管理器
* 作者姓名: chenshijie
* 创建时间: 2020/10/24 02:04
******************************/
public class NodeManager {
private static final Logger LOGGER = LoggerFactory.getLogger(NodeManager.class);
//虚拟节点数
private static final int VM_NODE = 150;
private static Map<Integer,Node> nodeMap = new HashMap<Integer, Node>();
private List<Node> nodeList = new ArrayList<Node>();
public void addNode(Node node) {
nodeList.add(node);
shuffer();
}
public Node getNodeByKey(String key) {
int i = key.hashCode() & Integer.MAX_VALUE;
int nodeIndex = i % VM_NODE;
Node node = nodeMap.get(nodeIndex);
LOGGER.debug("node:{}, nodeIndex:{}, i:{}",node,nodeIndex,i);
return node;
}
public int[] info() {
int[] rs = new int[nodeList.size()];
for(int i =0;i< nodeList.size();i++) {
Node node = nodeList.get(i);
rs[i] = node.getSize();
LOGGER.info("node id:{}, key size:{}",node.getNodeId(),node.getSize());
}
return rs;
}
private void shuffer() {
nodeMap.clear();
for(int i = 0; i < VM_NODE; i++) {
int nodeIndex = i % nodeList.size();
nodeMap.put(i,nodeList.get(nodeIndex));
}
LOGGER.debug("shuffer finish:map.size:{},map:{}",nodeMap.size(),nodeMap);
}
}



package com.demo;
/******************************
* 用途说明: 客户端
* 作者姓名: chenshijie
* 创建时间: 2020/10/24 02:03
******************************/
public class Client {
private NodeManager nodeManager;
public Client(NodeManager nodeManager) {
this.nodeManager = nodeManager;
}
public void put(String key,String value) {
Node node = nodeManager.getNodeByKey(key);
node.put(key,value);
}
}



package com.demo;
/******************************
* 用途说明: 标准差计算
* 作者姓名: chenshijie
* 创建时间: 2020/10/24 17:27
******************************/
public class StandardDiviation {
public static double StandardDiviation(int[] x) {
int m=x.length;
double sum=0;
for(int i=0;i<m;i++){//求和
sum+=x[i];
}
double dAve=sum/m;//求平均值
double dVar=0;
for(int i=0;i<m;i++){//求方差
dVar+=(x[i]-dAve)*(x[i]-dAve);
}
return Math.sqrt(dVar/m);
}
}



package com.demo;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.UUID;
/******************************
* 用途说明: 单元测试
* 作者姓名: chenshijie
* 创建时间: 2020/10/24 02:04
******************************/
public class ClientTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientTest.class);
@Test
public void put() {
NodeManager nodeManager = new NodeManager();
for(int i =1;i<=10;i++) {
Node node = new Node(i);
nodeManager.addNode(node);
}
Client client = new Client(nodeManager);
for(int i = 0; i<1000000; i++) {
String key = System.currentTimeMillis() + UUID.randomUUID().toString().replace("-", "");
client.put(key,null);
}
int[] info = nodeManager.info();
double v = StandardDiviation.StandardDiviation(info);
LOGGER.info("标准差={}",v);
}
}



运行结果:

2020-10-24 17:36:30.158 INFO com.demo.NodeManager - node id:1, key size:100086

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:2, key size:99729

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:3, key size:99602

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:4, key size:99794

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:5, key size:100051

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:6, key size:99978

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:7, key size:100040

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:8, key size:100326

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:9, key size:100406

2020-10-24 17:36:30.160 INFO com.demo.NodeManager - node id:10, key size:99988

2020-10-24 17:36:30.161 INFO com.demo.ClientTest - 标准差=236.13936562970605



多次运行,标准差均在200左右。



用户头像

Geek_fabd84

关注

还未添加个人签名 2019.06.30 加入

还未添加个人简介

评论

发布
暂无评论
第五周 实现一致性 hash 算法