Jedis中的一致性hash

本文仅供大家参考,不保证正确性,有问题请及时指出

一致性hash就不多说了,网上有很多说的很好的文章,这里说说Jedis中的Shard是如何使用一致性hash的,也为大家在实现一致性hash提供些思路。

首先是hash函数,在Jedis中有两种Hash算法可供选择,分别是MurMurHash和MD5. 按照Jedis的说法MurMur Hash更快,效果更好些。

MurmurHash.java

package redis.clients.util;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

publicclass MurmurHash implements Hashing {

publicstaticint hash(byte[] data, int seed) {

return hash(ByteBuffer.wrap(data), seed);

}

publicstaticint hash(byte[] data, int offset, int length, int seed) {

return hash(ByteBuffer.wrap(data, offset, length), seed);

}

publicstaticint hash(ByteBuffer buf, int seed) {

ByteOrder byteOrder = buf.order();

buf.order(ByteOrder.LITTLE_ENDIAN);

int m = 0x5bd1e995;

int r = 24;

int h = seed ^ buf.remaining();

int k;

while (buf.remaining() >= 4) {

k = buf.getInt();

k *= m;

k ^= k >>> r;

k *= m;

h *= m;

h ^= k;

}

if (buf.remaining() > 0) {

ByteBuffer finish = ByteBuffer.allocate(4).order(

ByteOrder.LITTLE_ENDIAN);

finish.put(buf).rewind();

h ^= finish.getInt();

h *= m;

}

h ^= h >>> 13;

h *= m;

h ^= h >>> 15;

buf.order(byteOrder);

return h;

}

publicstaticlong hash64A(byte[] data, int seed) {

return hash64A(ByteBuffer.wrap(data), seed);

}

publicstaticlong hash64A(byte[] data, int offset, int length, int seed) {

return hash64A(ByteBuffer.wrap(data, offset, length), seed);

}

publicstaticlong hash64A(ByteBuffer buf, int seed) {

ByteOrder byteOrder = buf.order();

buf.order(ByteOrder.LITTLE_ENDIAN);

long m = 0xc6a4a7935bd1e995L;

int r = 47;

long h = seed ^ (buf.remaining() * m);

long k;

while (buf.remaining() >= 8) {

k = buf.getLong();

k *= m;

k ^= k >>> r;

k *= m;

h ^= k;

h *= m;

}

if (buf.remaining() > 0) {

ByteBuffer finish = ByteBuffer.allocate(8).order(

ByteOrder.LITTLE_ENDIAN);

finish.put(buf).rewind();

h ^= finish.getLong();

h *= m;

}

h ^= h >>> r;

h *= m;

h ^= h >>> r;

buf.order(byteOrder);

return h;

}

publiclong hash(byte[] key) {

return hash64A(key, 0x1234ABCD);

}

publiclong hash(String key) {

return hash(SafeEncoder.encode(key));

}

}

而MD5从java提供的类中即可获得,但是需要将MD5值转换为long类型的常量。

publicinterface Hashing {

publicstaticfinal Hashing MURMUR_HASH = new MurmurHash();

public ThreadLocal<MessageDigest> md5Holder = new ThreadLocal<MessageDigest>();

publicstaticfinal Hashing MD5 = new Hashing() {

publiclong hash(String key) {

return hash(SafeEncoder.encode(key));

}

publiclong hash(byte[] key) {

try {

if (md5Holder.get() == null) {

md5Holder.set(MessageDigest.getInstance("MD5"));

}

} catch (NoSuchAlgorithmException e) {

thrownew IllegalStateException("++++ no md5 algorythm found");

}

MessageDigest md5 = md5Holder.get();

md5.reset();

md5.update(key);

byte[] bKey = md5.digest();

long res = ((long) (bKey[3] & 0xFF) << 24)

| ((long) (bKey[2] & 0xFF) << 16)

| ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF);

return res;

}

};

}

MD5中使用了ThreadLocal,这个就不多说了,网上也有很多相关的blog。或者查看我《如何写性能测试程序》那篇博客。

下面主要说说,Jedis中的一致性Hash部分。

Shard中定义了如下属性:

private TreeMap<Long, S> nodes;

privatefinal Hashing algo;

privatefinal Map<ShardInfo<R>, R> resources = new LinkedHashMap<ShardInfo<R>, R>();

nodes属性类型为TreeMap<Long,S>,表示主机映射。每个主机被Hash后,对应多个Long值,而nodes维护了这些Long值对应的主机。

algo是hash算法,是MurMur或者MD5.

resources 完成主机信息和实际链接(Jedis)间的映射。

初始化主机hash如下:

privatevoid initialize(List<S> shards) {

nodes = new TreeMap<Long, S>();

for (int i = 0; i != shards.size(); ++i) {

final S shardInfo = shards.get(i);

if (shardInfo.getName() == null)

for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {

nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo);

}

else

for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {

nodes.put(this.algo.hash(shardInfo.getName() + "*" + shardInfo.getWeight() + n), shardInfo);

}

resources.put(shardInfo, shardInfo.createResource());

}

}

在for循环中,遍历主机列表(shards.get(i)),之后对每个主机按照单权重160的比例计算shard值,将shard值和主机信息(shardInfo)放到nodes中,将主机信息(shardInfo)和其对应的链接资源(Jedis)映射放入到resources中。

Weight是权重,用于调节单个主机被映射值个数,如果weight为1,那么当前主机将被映射为160个值,weight为2,当前主机将被映射为320个值,因此weight为2的节点被访问到的概率就会高一些。

获取某个key对应的主机链接:

public S getShardInfo(byte[] key) {

SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key));

if (tail.isEmpty()) {

returnnodes.get(nodes.firstKey());

}

return tail.get(tail.firstKey());

}

在获取key时,先根据key做hash(algo.hash(key)),然后获取node中值大于algo.hash(key)的子树,子树的头结点即为整个子树的最小值,因此选中这个节点做为key的映射节点,取出key值对应的value(hostInfo),之后根据hostInfo,通过resources,获取最终的链接。

public R getShard(byte[] key) {

returnresources.get(getShardInfo(key));

}

当然,一致性hash在存储时,一个key可以对应多个节点,一般这多个节点都是在nodes(MapTree)中连续的,因此,也可以改进这个算法,以便实现多节点存储同一个key的方式。

参考:jedis-2.1.0.jar

Jedis中的一致性hash的更多相关文章

  1. jedis中的一致性hash算法

    [http://my.oschina.net/u/866190/blog/192286] jredis是redis的java客户端,通过sharde实现负载路由,一直很好奇jredis的sharde如 ...

  2. 分布式缓存技术memcached学习(四)—— 一致性hash算法原理

    分布式一致性hash算法简介 当你看到“分布式一致性hash算法”这个词时,第一时间可能会问,什么是分布式,什么是一致性,hash又是什么.在分析分布式一致性hash算法原理之前,我们先来了解一下这几 ...

  3. Ceph剖析:数据分布之CRUSH算法与一致性Hash

    作者:吴香伟 发表于 2014/09/05 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 数据分布是分布式存储系统的一个重要部分,数据分布算法至少要考虑以下三个 ...

  4. 【转载】一致性hash算法释义

    http://www.cnblogs.com/haippy/archive/2011/12/10/2282943.html 一致性Hash算法背景 一致性哈希算法在1997年由麻省理工学院的Karge ...

  5. python -- 一致性Hash

    python有一个python模块--hash_ring,即python中的一致性hash,使用起来也挺简单. 可以参考下官方例子:https://pypi.python.org/pypi/hash_ ...

  6. 一致性Hash算法及使用场景

    一.问题产生背景      在使用分布式对数据进行存储时,经常会碰到需要新增节点来满足业务快速增长的需求.然而在新增节点时,如果处理不善会导致所有的数据重新分片,这对于某些系统来说可能是灾难性的. 那 ...

  7. Ceph之数据分布:CRUSH算法与一致性Hash

    转自于:http://www.cnblogs.com/shanno/p/3958298.html?utm_source=tuicool 数据分布是分布式存储系统的一个重要部分,数据分布算法至少要考虑以 ...

  8. 分布式缓存技术memcached学习系列(四)—— 一致性hash算法原理

    分布式一致性hash算法简介 当你看到"分布式一致性hash算法"这个词时,第一时间可能会问,什么是分布式,什么是一致性,hash又是什么.在分析分布式一致性hash算法原理之前, ...

  9. [转载] 一致性hash算法释义

    转载自http://www.cnblogs.com/haippy/archive/2011/12/10/2282943.html 一致性Hash算法背景 一致性哈希算法在1997年由麻省理工学院的Ka ...

随机推荐

  1. python学习之 dictionary 、list、tuple操作

    python 内置类型数据 有dictionary(字典).list(列表)和tuple(元组) 一.Dictionary Dictionary 是 Python 的内置数据类型之一,它定义了键和值之 ...

  2. IO库 8.2

    题目:编写一个测试函数,将cin作为参数传入. #include <iostream> using std::istream; istream& func(istream& ...

  3. IT工程师值得一看的书籍

    http://blog.csdn.net/chinahuyong/article/details/45060203

  4. ThinkPHP第二十四天(JQuery常用方法、TP自动验证)

    ---恢复内容开始--- 1.JQuery常用方法 A:JS中可以用json格式数据当做数组使用,如var validate={username:false,pwd:false,pwded:false ...

  5. codeforces 631C. Report

    题目链接 按题目给出的r, 维护一个递减的数列,然后在末尾补一个0. 比如样例给出的 4 21 2 4 32 31 2 递减的数列就是3 2 0, 操作的时候, 先变[3, 2), 然后变[2, 0) ...

  6. 有一个警告:Could not open/create prefs root node

    WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. 虽然程序也能正常运 ...

  7. 监控Informix-Url

    jdbc:informix-sqli://[{ip-address|host-name}:{port-number|service-name}][/dbname]: INFORMIXSERVER=se ...

  8. cocos2dx 字体BMFont,Atlas

    为了更加个性化,系统提供的字体,有时候没法满足我们的要求,所以cocos2dx提供了自定义字体控件. 分别是CCLabelBMFont和CCLabelAtlas,先看BMFont的效果 CCLabel ...

  9. VM Agent 和扩展程序

    VM Agent 和扩展程序 - 第 1 部分  Windows Azure基础结构服务最近宣布了一项新功能VM Agent.VMAgent是一个轻量级进程,用于启动由Microsoft或合作伙伴 ...

  10. Android面试笔试集锦

    前19题为常考题目 1. Android的四大组件是哪些,它们的作用? 答:Activity:Activity是Android程序与用户交互的窗口,是Android构造块中最基本的一种,它需要为保持各 ...