一致性Hash
来源:互联网 发布:类似简图的软件 编辑:程序博客网 时间:2024/06/04 20:54
理解一致性哈希算法(consistent hashing)
关于TreeMap和一致性hash
某分布式应用实践一致性哈希的一些问题
public interface HashFunction {int hash(String hash);}
import java.util.Collection;import java.util.SortedMap;import java.util.TreeMap;public class ConsistentHash<T> { private final HashFunction hashFunction;//hash算法接口,可使用MD5 SHA1 SHA256,MurmurHash等算法 private final int numberOfReplicas;// 每个机器节点关联的虚拟节点个数 private final SortedMap<Integer, T> circle = new TreeMap<Integer, T>();// 虚拟节点 public ConsistentHash(HashFunction hashFunction, int numberOfReplicas, Collection<T> nodes) { this.hashFunction = hashFunction; this.numberOfReplicas = numberOfReplicas; for (T node : nodes) { add(node); } } //添加新的机器节点 public void add(T node) { for (int i = 0; i < numberOfReplicas; i++) { circle.put(hashFunction.hash(node.toString() + i), node); } } //删除某机器节点 public void remove(T node) { for (int i = 0; i < numberOfReplicas; i++) { circle.remove(hashFunction.hash(node.toString() + i)); } } //查找待存储key对应的机器节点 public T get(Object key) { if (circle.isEmpty()) { return null; } int hash = hashFunction.hash(key.toString()); if (!circle.containsKey(hash)) { // 沿环的顺时针找到一个虚拟节点,环中不存在使用环中的第一个key SortedMap<Integer, T> tailMap = circle.tailMap(hash); hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); } return circle.get(hash); } }
0 0
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性 hash
- 一致性Hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- 一致性hash
- STL_deque
- 关于C和C++一些大神们的讨论
- IOS-52-UITableView中cell分割线左对齐(解决iOS8版本适配问题)
- 什么时候应该使用$scope.$apply()
- GDKOI2016 day 2 Problem 4. 小学生数学题 - 数学题
- 一致性Hash
- 2014年 Thinking of Hardware Design
- tarjan求强联通分量
- Universal-Image-Loader(UIL)图片加载框架使用简单介绍
- 大型分布式网站架构技术总结
- Tomcat服务器配置二
- python 验证码识别
- swift学习日记(十二) 泛型
- OpenCv库函数(types_c.h)持续更新