Jedis之ShardedJedis一致性哈希分析

时间:2015-03-25 19:44:29   收藏:0   阅读:193

Jedis之ShardedJedis一致性哈希分析


ShardedJedis通过一致性哈希实现的的分布式缓存。主要思路:


源码:

public class Sharded<R, S extends ShardInfo<R>> {

  public static final int DEFAULT_WEIGHT = 1;
  private TreeMap<Long, S> nodes;
  private final Hashing algo;
  private final Map<ShardInfo<R>, R> resources = new LinkedHashMap<ShardInfo<R>, R>();
  ........................
  ........................
}

这个类维护了一致性哈希后的物理机器和虚拟节点的映射关系,看一张图你会秒懂,

技术分享


TreeMap<Long, S> nodes,存储的是虚拟节点和key的映射关系。有了虚拟节点,还要找到真正的存储位置。

Map<ShardInfo<R>, R> resources维护了虚拟节点和真正的存储位置的映射关系。

也是说,hash(key) -> virtual node -> real node;


jedis划分虚拟节点的逻辑代码,在Sharded类中,方法是initialize。这是在实例化对象池ShardedJedisPool过程中执行的划分虚拟节点。

private void 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());
    }
}

以上代码就是划分虚拟节点的逻辑。


那么ShardedJedis客户端是如何执行set key value呢?

通过这里可以看出还是通过Jedis客户端执行的set key value。

public String set(String key, String value) {
  Jedis j = getShard(key);
  return j.set(key, value);
}

看一下代码中大体的逻辑,首先通过key得到ShardInfo,然后通过ShardInfo得到泛型Jedis客户端。

Sharded.java

public R getShard(byte[] key) {
  return resources.get(getShardInfo(key));
}

public S getShardInfo(byte[] key) {
  SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key));
  if (tail.isEmpty()) {
    return nodes.get(nodes.firstKey());
  }
  return tail.get(tail.firstKey());
}

来看一下ShardedJedis的继承关系吧,

技术分享

还有ShardInfo和JedisShardInfo继承关系,

技术分享

参考:http://yychao.iteye.com/blog/1751583

===================================END===================================


原文:http://my.oschina.net/xinxingegeya/blog/391713

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!