【题目描述】

在 Consistent Hashing I 中我们介绍了一个比较简单的一致性哈希算法,这个简单的版本有两个缺陷:

  1. 增加一台机器之后,数据全部从其中一台机器过来,这一台机器的读负载过大,对正常的服务会造成影响。
  2. 当增加到3台机器的时候,每台服务器的负载量不均衡,为1:1:2。

为了解决这个问题,引入了 micro-shards 的概念,一个更好的算法是这样:

  1. 将 360° 的区间分得更细。从 0~359 变为一个 0 ~ n-1 的区间,将这个区间首尾相接,连成一个圆。
  2. 当加入一台新的机器的时候,随机选择在圆周中撒 k 个点,代表这台机器的 k 个 micro-shards。
  3. 每个数据在圆周上也对应一个点,这个点通过一个 hash function 来计算。
  4. 一个数据该属于哪台机器负责管理,是按照该数据对应的圆周上的点在圆上顺时针碰到的第一个 micro-shard 点所属的机器来决定。

n 和 k在真实的 NoSQL 数据库中一般是 2^64 和 1000。

请实现这种引入了 micro-shard 的 consistent hashing 的方法。主要实现如下的三个函数:

  1. create(int n, int k)
  2. addMachine(int machine_id) // add a new machine, return a list of shard ids.
  3. getMachineIdByHashCode(int hashcode) // return machine id

在线评测地址: 

https://www.lintcode.com/problem/consistent-hashing-ii/?utm_source=sc-bky-zq

【样例】

样例 1:

输入:
create(, )
addMachine()
getMachineIdByHashCode()
addMachine()
getMachineIdByHashCode()
getMachineIdByHashCode()
输出:
[,,] [,,]

样例 2:

输入:
create(, )
addMachine()
getMachineIdByHashCode()
addMachine()
getMachineIdByHashCode()
getMachineIdByHashCode()
getMachineIdByHashCode()
getMachineIdByHashCode()
getMachineIdByHashCode()
getMachineIdByHashCode()
输出:
[,,,,] [,,,,]

【题解】

public class Solution {

    public int n, k;
public Set<Integer> ids = null;
public 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 here
Solution solution = new Solution();
solution.n = n;
solution.k = k;
solution.ids = new TreeSet<Integer>();
solution.machines = new HashMap<Integer, List<Integer>>();
return solution;
} // @param machine_id an integer
// @return a list of shard ids
public List<Integer> addMachine(int machine_id) {
// Write your code here
Random ra = new Random();
List<Integer> random_nums = new ArrayList<Integer>();
for (int i = 0; i < k; ++i) {
int index = ra.nextInt(n);
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
public int getMachineIdByHashCode(int hashcode) {
// Write your code here
int distance = n + 1;
int machine_id = 0;
for (Map.Entry<Integer, List<Integer>> entry : machines.entrySet()) {
int key = entry.getKey();
List<Integer> random_nums = entry.getValue();
for (Integer num : random_nums) {
int d = num - hashcode;
if (d < 0)
d += n;
if (d < distance) {
distance = d;
machine_id = key;
}
}
}
return machine_id;
}
}

【更多解法可参考】

https://www.jiuzhang.com/solution/longest-palindromic-substring/?utm_source=sc-bky-zq

[leetcode/lintcode 题解] 一致性哈希 II · Consistent Hashing II的更多相关文章

  1. (转)每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)

    背景:在redis集群中,有关于一致性哈希的使用. 一致性哈希:桶大小0~(2^32)-1 哈希指标:平衡性.单调性.分散性.负载性 为了提高平衡性,引入“虚拟节点” 每天进步一点点——五分钟理解一致 ...

  2. 一致性哈希算法(consistent hashing)(转)

    原文链接:每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)  一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网 ...

  3. 一致性哈希算法(Consistent Hashing Algorithm)

    一致性哈希算法(Consistent Hashing Algorithm) 浅谈一致性Hash原理及应用   在讲一致性Hash之前我们先来讨论一个问题. 问题:现在有亿级用户,每日产生千万级订单,如 ...

  4. 转 白话解析:一致性哈希算法 consistent hashing

    摘要: 本文首先以一个经典的分布式缓存的应用场景为铺垫,在了解了这个应用场景之后,生动而又不失风趣地介绍了一致性哈希算法,同时也明确给出了一致性哈希算法的优点.存在的问题及其解决办法. 声明与致谢: ...

  5. 白话解析:一致性哈希算法 consistent hashing【转】

    学习一致性哈希算法原理的时候看到博主朱双印的一片文章,看完就懂,大佬! 白话解析:一致性哈希算法 consistent hashing

  6. _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前.妳却感觉不到我的存在 技术方向: ...

  7. 一致性哈希算法(consistent hashing)PHP实现

    一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简单哈希 ...

  8. 五分钟理解一致性哈希算法(consistent hashing)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法 ...

  9. 每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

随机推荐

  1. MySQL按指定字符合并及拆分

    按照指定字符进行合并或拆分是经常碰到的场景,MySQL在合并的写法上比较简单,但是按指定字符拆分相对比较麻烦一点(也就是要多写一些字符).本文将举例演示如何进行按照指定字符合并及拆分. 1. 合并 M ...

  2. liunx中组合查询的命令

    今天无聊,把以前的liunx命令拿过练练,尤其是一些组合命令并带有逻辑的.这里的script是一个文件夹. 1.查看一个文件的最后3行的第一行. [root@localhost home]# tail ...

  3. Spring—容器外的Bean使用依赖注入

    认识AutowireCapableBeanFactory AutowireCapableBeanFactory是在BeanFactory的基础上实现对已存在实例的管理.可以使用这个接口集成其他框架,捆 ...

  4. 实时web应用方案——SignalR(.net core)

    何为实时 先从理论上解释一下两者的区别. 大多数传统的web应用是这样的:客户端发起http请求到服务端,服务端返回对应的结果.像这样: 也就是说,传统的web应用都是客户端主动发起请求到服务端. 那 ...

  5. iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 3306 -j DNAT --to-destination 172.17.0.2:3306 ! -i docker0: iptables: No chain/target/match by that name

    今天使用docker运行mysql时报错, 执行命令: docker run --restart=always --name mysql5.7 -p 3306:3306 -v /data/mysql/ ...

  6. 基础的markdown用法

    首先推荐一款在线的markdown编辑器:Editor.md 一篇博客里面就是文字与图片的集合.markdown其实就是编辑文字,插入图片的工具.对于大多数人来说,学习几个标签就可以了. 标题 #** ...

  7. dart快速入门教程 (7.2)

    7.4.抽离类为单独文件 新建一个文件,单独存放一个类,例如:Person类抽离到person.dart文件中 class Person { final String name; final num ...

  8. mfc 中unicode 字符和字符串的使用

    在MFC或SDK程序中,不需要进行任何关于unicode的设置,记住下面两个宏,保你程序一路畅通: 用TCHAR/TCHAR*代替char/char* 及wchar/wchar*用TEXT(" ...

  9. ant design pro: protable控件隐藏【收起】按钮

    [collapseRender:()=>false] [效果] [参考ProTable源码]

  10. MysqlException: max pool size was reached.

    2019-09-09 08:22:08.620 +00:00 [ERR] Connection id "0HLPKVK52H2OU", Request id "0HLPK ...