本文主要简介一下jump Consistent hash。

jump consistent hash

jump consistent hash是一致性哈希的一种实现,论文见A Fast, Minimal Memory, Consistent Hash Algorithm
经典的一致性哈希算法来自Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web
jump consistent hash与之的主要区别是节点可以扩容,但是不会移除节点。

算法代码

int32_t JumpConsistentHash(uint64_t key, int32_t num_buckets) {int64_t b = -1, j = 0;while (j < num_buckets) {b = j;key = key * 2862933555777941757ULL + 1;j = (b + 1) * (double(1LL << 31) / double((key >> 33) + 1));}return b;
}

java实现

guava里头有个现成的实现
guava-22.0-sources.jar!/com/google/common/hash/Hashing.java

/*** Assigns to {@code hashCode} a "bucket" in the range {@code [0, buckets)}, in a uniform manner* that minimizes the need for remapping as {@code buckets} grows. That is, {@code* consistentHash(h, n)} equals:** <ul>* <li>{@code n - 1}, with approximate probability {@code 1/n}* <li>{@code consistentHash(h, n - 1)}, otherwise (probability {@code 1 - 1/n})* </ul>** <p>This method is suitable for the common use case of dividing work among buckets that meet the* following conditions:** <ul>* <li>You want to assign the same fraction of inputs to each bucket.* <li>When you reduce the number of buckets, you can accept that the most recently added buckets* will be removed first. More concretely, if you are dividing traffic among tasks, you can* decrease the number of tasks from 15 and 10, killing off the final 5 tasks, and {@code* consistentHash} will handle it. If, however, you are dividing traffic among servers {@code* alpha}, {@code bravo}, and {@code charlie} and you occasionally need to take each of the* servers offline, {@code consistentHash} will be a poor fit: It provides no way for you to* specify which of the three buckets is disappearing. Thus, if your buckets change from {@code* [alpha, bravo, charlie]} to {@code [bravo, charlie]}, it will assign all the old {@code alpha}* traffic to {@code bravo} and all the old {@code bravo} traffic to {@code charlie}, rather than* letting {@code bravo} keep its traffic.* </ul>*** <p>See the <a href="http://en.wikipedia.org/wiki/Consistent_hashing">Wikipedia article on* consistent hashing</a> for more information.*/public static int consistentHash(HashCode hashCode, int buckets) {return consistentHash(hashCode.padToLong(), buckets);}/*** Assigns to {@code input} a "bucket" in the range {@code [0, buckets)}, in a uniform manner that* minimizes the need for remapping as {@code buckets} grows. That is, {@code consistentHash(h,* n)} equals:** <ul>* <li>{@code n - 1}, with approximate probability {@code 1/n}* <li>{@code consistentHash(h, n - 1)}, otherwise (probability {@code 1 - 1/n})* </ul>** <p>This method is suitable for the common use case of dividing work among buckets that meet the* following conditions:** <ul>* <li>You want to assign the same fraction of inputs to each bucket.* <li>When you reduce the number of buckets, you can accept that the most recently added buckets* will be removed first. More concretely, if you are dividing traffic among tasks, you can* decrease the number of tasks from 15 and 10, killing off the final 5 tasks, and {@code* consistentHash} will handle it. If, however, you are dividing traffic among servers {@code* alpha}, {@code bravo}, and {@code charlie} and you occasionally need to take each of the* servers offline, {@code consistentHash} will be a poor fit: It provides no way for you to* specify which of the three buckets is disappearing. Thus, if your buckets change from {@code* [alpha, bravo, charlie]} to {@code [bravo, charlie]}, it will assign all the old {@code alpha}* traffic to {@code bravo} and all the old {@code bravo} traffic to {@code charlie}, rather than* letting {@code bravo} keep its traffic.* </ul>*** <p>See the <a href="http://en.wikipedia.org/wiki/Consistent_hashing">Wikipedia article on* consistent hashing</a> for more information.*/public static int consistentHash(long input, int buckets) {checkArgument(buckets > 0, "buckets must be positive: %s", buckets);LinearCongruentialGenerator generator = new LinearCongruentialGenerator(input);int candidate = 0;int next;// Jump from bucket to bucket until we go out of rangewhile (true) {next = (int) ((candidate + 1) / generator.nextDouble());if (next >= 0 && next < buckets) {candidate = next;} else {return candidate;}}}/*** Linear CongruentialGenerator to use for consistent hashing. See* http://en.wikipedia.org/wiki/Linear_congruential_generator*/private static final class LinearCongruentialGenerator {private long state;public LinearCongruentialGenerator(long seed) {this.state = seed;}public double nextDouble() {state = 2862933555777941757L * state + 1;return ((double) ((int) (state >>> 33) + 1)) / (0x1.0p31);}}

使用实例

    @Testpublic void testJumpHash(){List<String> nodes = Arrays.asList("ins1","ins2","ins3","ins4");List<String> keys = Arrays.asList("key1","key2","key3","key4");keys.stream().forEach(e -> {int bucket = Hashing.consistentHash(Hashing.md5().hashString(e, Charsets.UTF_8), nodes.size());String node = nodes.get(bucket);System.out.println(e + " >> " + node);});}

doc

  • A Fast, Minimal Memory, Consistent Hash Algorithm

  • 一个速度快内存占用小的一致性哈希算法

  • jump Consistent hash:零内存消耗,均匀,快速,简洁,来自Google的一致性哈希算法

聊聊jump consistent hash相关推荐

  1. Google Jump Consistent Hash 一致性哈希算法

    接触到这个一致性哈希算法是在腾讯音乐的讲座中,用于在线扩容 如图中的例子,本来只有group0和group1,现在要增加一个group2用于推送新的数据,如果使用不满足单调性要求的hash方法,首先向 ...

  2. 关于consistent hash的思考及改进方案

    这里默认读者已经知道了一致性hash算法的原理. 1. 为什么在某台机器宕机之后consistent hash算法能够避免所有或者大部分key重新hash? 首先需要弄清的是,如果某一台机器宕机之后, ...

  3. 主从mysql replication 集群的sharding memcache集群使用consistent hash

    sharding 实现跨越DB的分区与扩展功能 consistent hash 一致哈希实现memcache的扩展 http://cache.baidu.com/c?m=9f65cb4a8c8507e ...

  4. haproxy Consistent Hash浅析

    http://blog.sina.com.cn/s/blog_502c8cc40100kfz2.html Haproxy实现了Map-based 和consistent hash算法,来完成通过哈希值 ...

  5. dapr的consistent hash

    序 本文主要研究一下dapr的consistent hash consistent_hash dapr/pkg/placement/hashing/consistent_hash.go var rep ...

  6. Upstream Consistent Hash

    介绍 https://www.nginx.com/resources/wiki/modules/consistent_hash/地址 ngx_http_upstream_consistent_hash ...

  7. 2016 -Nginx的负载均衡 - 一致性哈希 (Consistent Hash)

    Nginx版本:1.9.1 算法介绍 当后端是缓存服务器时,经常使用一致性哈希算法来进行负载均衡. 使用一致性哈希的好处在于,增减集群的缓存服务器时,只有少量的缓存会失效,回源量较小. 在nginx+ ...

  8. consistent hash

    https://www.youtube.com/watch?v=ffE1mQWxyKM https://www.youtube.com/watch?v=zaRkONvyGr8

  9. 分布式存储系统设计(2)—— 数据分片

    在分布式存储系统中,数据需要分散存储在多台设备上,数据分片(Sharding)就是用来确定数据在多台存储设备上分布的技术.数据分片要达到三个目的: 分布均匀,即每台设备上的数据量要尽可能相近: 负载均 ...

最新文章

  1. safari post 请求接收不到_我是谁?我在哪?我要到哪去?——HTTP请求头
  2. git 添加未跟踪的文件
  3. C++虚析构和纯虚析构
  4. python3 装饰器参数_Learn Python 3:装饰器
  5. 如何关闭借呗订阅开通通知_支付宝花呗借呗隐藏规则,芝麻分600以上,花呗3.6万,借呗12万!...
  6. 为何 Canvas 内元素动画总是在颤抖?
  7. android中如何通过代码检测是否有root权限?
  8. 马云:不能把孩子放在温室里,光给孩子知识是不够的
  9. 30分钟快速上手Docker,看这篇就对了!
  10. 加密托管机构Komainu与英国当局合作存储没收的加密货币
  11. .Net混淆研究(一)---基本原理和利弊
  12. 网络工程师的人生之路是这样的开始的!
  13. UGUI Scrollbar控件
  14. [2018.10.11 T1] 锻造
  15. 有限元分析简单实例之平面矩形薄板(matlab)
  16. 开源中国众包平台 —— 为什么我们需要托管赏金
  17. python 标签云_Python中文标签云之pytagcloud
  18. C/C++输入未知组数据的方法,多行输入介绍
  19. mxGraph Tutorial
  20. 理解选择排序的不稳定性

热门文章

  1. 漫画:如何给女朋友解释什么是删库跑路?
  2. 微软为什么从 C/C++ 转向了 Rust?
  3. ClearCanvas DICOM 开发系列 一
  4. 【朝花夕拾】Android性能篇之(二)Java内存分配
  5. rsync同步服务实验讲解
  6. CentOS 6.3下rsync服务器的安装与配置
  7. python操作Mysql基础
  8. AM335X can驱动移植
  9. Repository 设计模式介绍
  10. 利用Wireshark和OSS的API文档简单实现上传和下载