来看看维奇百科上哈希函数的定义。
散列函数(英语:Hash function)又称散列算法、哈希函数,是一种从任何一种数据中创建小的数字“指纹”的方法。散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来。该函数将数据打乱混合,重新创建一个叫做散列值(hash values,hash codes,hash sums,或hashes)的指纹。散列值通常用一个短的随机字母和数字组成的字符串来代表。
比如 MD5算法,把字符串映射到固定的16字节(128位)的二进制。16字节对应32个16进制的数(0 -9 A - E),每个字节有8位,一个字节可以用来表示2个16进制的数,所以 MD5 算法会映射出32个数字和字母的随机组合。
如何散列函数和散列表结合使用,那么最后求出的映射的数就不需要再转义为对应的 ASCII 码了,直接变成数字就好。
一致性哈希 II
在 Consistent Hashing I 中我们介绍了一个比较简单的一致性哈希算法,这个简单的版本有两个缺陷:
- 增加一台机器之后,数据全部从其中一台机器过来,这一台机器的读负载过大,对正常的服务会造成影响。
- 当增加到3台机器的时候,每台服务器的负载量不均衡,为1:1:2。
为了解决这个问题,引入了 micro-shards 的概念,一个更好的算法是这样:
- 将 360° 的区间分得更细。从 0~359 变为一个 0 ~ n-1 的区间,将这个区间首尾相接,连成一个圆。
- 当加入一台新的机器的时候,随机选择在圆周中撒 k 个点,代表这台机器的 k 个 micro-shards。
- 每个数据在圆周上也对应一个点,这个点通过一个 hash function 来计算。
- 一个数据该属于哪台机器负责管理,是按照该数据对应的圆周上的点在圆上顺时针碰到的第一个 micro-shard 点所属的机器来决定。
n 和 k在真实的 NoSQL 数据库中一般是 2^64 和 1000。
请实现这种引入了 micro-shard 的 consistent hashing 的方法。主要实现如下的三个函数:
create(int n, int k);addMachine(int machine_id)// 添加新机器,返回碎片ID列表。getMachineIdByHashCode(int hashcode)// 返回机器id
样例 1:
输入:create(100, 3)addMachine(1)getMachineIdByHashCode(4)addMachine(2)getMachineIdByHashCode(61)getMachineIdByHashCode(91)输出:[77,83,86]1[15,35,93]12
输入:create(10, 5)addMachine(1)getMachineIdByHashCode(4)addMachine(2)getMachineIdByHashCode(0)getMachineIdByHashCode(1)getMachineIdByHashCode(2)getMachineIdByHashCode(3)getMachineIdByHashCode(4)getMachineIdByHashCode(5)输出:[2,3,5,6,7]1[0,1,4,8,9]221121
代码:
public class Solution {public int n, k;// 所有机器的 micro-shards_id 的集合,用于防止重复public Set<Integer> ids = null;// 保存机器的 id号 和其对应的 micro-shards_idpublic Map<Integer, List<Integer>> machines = null;// @param n a positive integer// @param k a positive integer// @return a Solution object// 初始化,初始化一些重要参数public static Solution create(int n, int k) {// Write your code hereSolution solution = new Solution();solution.n = n;solution.k = k;// 用红黑树的 Set,便于排序solution.ids = new TreeSet<Integer>();solution.machines = new HashMap<Integer, List<Integer>>();return solution;}// @param machine_id an integer// @return a list of shard idspublic List<Integer> addMachine(int machine_id) {// Write your code hereRandom ra = new Random();List<Integer> random_nums = new ArrayList<Integer>();for (int i = 0; i < k; ++i) {int index = ra.nextInt(n);// 发现这个 micro-shards_id 已经存在了,那就得重新生成while (ids.contains(index))index = ra.nextInt(n);ids.add(index);random_nums.add(index);}Collections.sort(random_nums);machines.put(machine_id, random_nums);return random_nums;}// @param hashcode an integer// @return a machine id// 根据 hashcode 来获得对应的 machineIdpublic int getMachineIdByHashCode(int hashcode) {// Write your code here// 假设 n = 100, 那么 distance = 101int distance = n + 1;int machine_id = 0;// 循环遍历每一个 hashMapfor (Map.Entry<Integer, List<Integer>> entry : machines.entrySet()) {int key = entry.getKey();List<Integer> random_nums = entry.getValue();for (Integer num : random_nums) {// 假设 num = 16, hashcode = 50;int d = num - hashcode;// 如果小于0,说明 hashcode 在 num 的逆时针// 那么从 hashcode 顺时针到 num 的距离为 d += n// 比如 d= num - hashcode = -34, d += n = -34 + 101 = 67if (d < 0)d += n;// 统计最短距离,即看哪个 num 离 hashcode 最近if (d < distance) {distance = d;machine_id = key;}}}return machine_id;}}
怎么Sharding 取决于我们怎么去取数据。
