美文网首页
一致性hash.md

一致性hash.md

作者: jey恒 | 来源:发表于2016-08-03 17:22 被阅读92次

jedis中sharejedis一致性hash实现

  • Redis服务器节点划分:将每台服务器节点采用hash算法划分为160个虚拟节点(可以配置划分权重)
  • 将划分虚拟节点采用TreeMap存储
  • 对每个Redis服务器的物理连接采用LinkedHashMap存储
  • 对Key or KeyTag 采用同样的hash算法,然后从TreeMap获取大于等于键hash值得节点,取最邻近节点存储;当* * key的hash值大于虚拟节点hash值得最大值时,存入第一个虚拟节点
  • sharded采用的hash算法:MD5 和 MurmurHash两种;默认采用64位的MurmurHash算法;有兴趣的可以 研究下,MurmurHash是一种高效,低碰撞的hash算法;参考地址:

http://blog.csdn.net/yfkiss/article/details/7337382
https://sites.google.com/site/murmurhash/


public static final int DEFAULT_WEIGHT = 1;

private TreeMap<Long, S> nodes; 
private final Hashing algo;// hash算法
//shardinfo对应的jedis连接
private final Map<ShardInfo<R>, R> resources = new LinkedHashMap<ShardInfo<R>, R>();


//shards 所有的服务器节点列表

private void initialize(List<S> shards) {

  // TreeMap的hash环
  nodes = new TreeMap<Long, S>();
  
  // 遍历节点
  for (int i = 0; i != shards.size(); ++i) {
    final S shardInfo = shards.get(i);
     //每个节点创建160个虚拟节点
    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());
  }
}

// 获取一个节点
public S getShardInfo(byte[] key) {
  //返回大于key的部分子map
  SortedMap<Long, S> tail = nodes.tailMap(algo.hash(key));
  if (tail.isEmpty()) {
    return nodes.get(nodes.firstKey());
  }
  
  //返回map中key值最小的一个
  return tail.get(tail.firstKey());
}

// 获得一个jedis
public R getShard(String key) {
  return resources.get(getShardInfo(key));
}

public S getShardInfo(String key) {
  return getShardInfo(SafeEncoder.encode(getKeyTag(key)));
}

相关文章

  • 一致性hash.md

    jedis中sharejedis一致性hash实现 Redis服务器节点划分:将每台服务器节点采用hash算法划分...

  • 分布式服务框架

    分布式事务的一致性? 一致性Hash算法的原理? 强一致性、弱一致性、最终一致性? zookeeper如何实现数据...

  • 一致性算法(Paxos、Raft、ZAB)

    一致性算法(Paxos、Raft、ZAB) 什么是一致性 1、弱一致性a、最终一致性i、DNS(Domain Na...

  • 分布式存储中的CAP理论

    参考: 强一致性、弱一致性、最终一致性 维基百科

  • 一致性

    事务一致性 最终一致性

  • [六]分布式系统

    分布式基本原则 cap原则 C 一致性 A 可用性 P 分区容错性 一致性分类 强一致性 弱一致性 最终一致性 B...

  • SpringCloud系列2-服务治理技术选型

    1.CAP定理 分布式系统不可避免的会遇到CAP定理。 C就是指一致性,一致性分为强一致性和弱一致性,强一致性就是...

  • 分布式事务

    CAP定理 Consistency 一致性 (强一致性) 强一致性,要求更新过的数据能被后续的访问都能看到若一致性...

  • 读书分享日更(3/311)

    人类误判心理学 五 避免不一致性倾向 这种一致性体现在两个方面:时间一致性和身心一致性。 一.时间一致性 人们有思...

  • 分布式系统的一致性

    一、什么是一致性 二、关于一致性的案例 三、如何解决一致性问题 3.1 酸碱平衡理论 3.2 分布式一致性协议...

网友评论

      本文标题:一致性hash.md

      本文链接:https://www.haomeiwen.com/subject/luoqsttx.html