ShardedJedis部分源码 redis分布式直连同步方式

ShardedJedis redis分布式直连同步

==============================================================


Sharded类中4个属性,2个重要方法:

4个属性 

public static final int DEFAULT_WEIGHT = 1;//权重
private final Hashing algo;//哈希
private TreeMap<Long, S> nodes;//虚拟节点
private final Map<ShardInfo<R>, R> resources = new LinkedHashMap<ShardInfo<R>, R>();//真实节点
两个重要方法
1.new JedisShardInfo()时调用,算是初始化
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());//shardInfo.createResource() = new Jedis()
    }
  }
160为虚拟节点基数,shardInfo.getWeight()权重越大,虚拟节点数越多,越容易存取到该节点
nodes和resources结合使用,找到虚拟节点对应的真实节点

2.get()/set()方法时调用,获取存取数据到的具体Jedis
public R getShard(String 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());
  }
TreeMap、nodes.tailMap()、SortedMap
实现一致性哈希旋转概念(找到最近的节点)


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

public String get(String key) {
    Jedis j = getShard(key);
    return j.get(key);
  }
......
-----------------------------------------------------

BinaryShardedJedis.java
-----------------------------------------------------
public String set(byte[] key, byte[] value) {
    Jedis j = getShard(key);
    return j.set(key, value);
  }

public byte[] get(byte[] key) {
    Jedis j = getShard(key);
    return j.get(key);
  }
......
-----------------------------------------------------


TreeMap、treeMap.tailMap()、SortedMap示例:

public static void main(String[] args) {
		// creating maps
		TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();

		// populating tree map
		treemap.put(2, "two");
		treemap.put(1, "one");
		treemap.put(3, "three");
		treemap.put(6, "six");
		treemap.put(5, "five");

		SortedMap<Integer, String> treemapincl = treemap.tailMap(3);//该方法调用返回此映射,其键大于或等于fromKey(这里是3)的值
		System.out.println("Tail map values: " + treemapincl);
		// 执行结果: Tail map values: {3=three, 5=five, 6=six}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值