Merge branch 'MasterSlaveConsistency' of https://github.com/TioBorracho/jedis into TioBorracho-MasterSlaveConsistency
This commit is contained in:
@@ -56,7 +56,7 @@ public class Sharded<R, S extends ShardInfo<R>> {
|
|||||||
for (int i = 0; i != shards.size(); ++i) {
|
for (int i = 0; i != shards.size(); ++i) {
|
||||||
final S shardInfo = shards.get(i);
|
final S shardInfo = shards.get(i);
|
||||||
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
|
for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {
|
||||||
nodes.put(this.algo.hash(shardInfo.toString() + n), shardInfo);
|
nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo);
|
||||||
}
|
}
|
||||||
resources.put(shardInfo, shardInfo.createResource());
|
resources.put(shardInfo, shardInfo.createResource());
|
||||||
}
|
}
|
||||||
@@ -108,3 +108,4 @@ public class Sharded<R, S extends ShardInfo<R>> {
|
|||||||
return Collections.unmodifiableCollection(resources.values());
|
return Collections.unmodifiableCollection(resources.values());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,14 +22,31 @@ public class ShardedJedisTest extends Assert {
|
|||||||
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers()
|
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers()
|
||||||
.get(1);
|
.get(1);
|
||||||
|
|
||||||
|
private List<String> getKeysDifferentShard(ShardedJedis jedis) {
|
||||||
|
List<String> ret = new ArrayList<String> ();
|
||||||
|
JedisShardInfo first = jedis.getShardInfo("a0");
|
||||||
|
ret.add("a0");
|
||||||
|
for (int i =1; i < 100; ++i) {
|
||||||
|
JedisShardInfo actual = jedis.getShardInfo("a" + i);
|
||||||
|
if (actual != first) {
|
||||||
|
ret.add("a" + i);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkSharding() {
|
public void checkSharding() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
||||||
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
JedisShardInfo s1 = jedis.getShardInfo("a1");
|
List<String> keys = getKeysDifferentShard(jedis);
|
||||||
JedisShardInfo s2 = jedis.getShardInfo("b2");
|
JedisShardInfo s1 = jedis.getShardInfo(keys.get(0));
|
||||||
|
JedisShardInfo s2 = jedis.getShardInfo(keys.get(1));
|
||||||
assertNotSame(s1, s2);
|
assertNotSame(s1, s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,8 +125,9 @@ public class ShardedJedisTest extends Assert {
|
|||||||
JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
|
JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
|
||||||
assertSame(s1, s2);
|
assertSame(s1, s2);
|
||||||
|
|
||||||
JedisShardInfo s3 = jedis.getShardInfo("a112");
|
List<String> keys = getKeysDifferentShard(jedis);
|
||||||
JedisShardInfo s4 = jedis.getShardInfo("b112");
|
JedisShardInfo s3 = jedis.getShardInfo(keys.get(0));
|
||||||
|
JedisShardInfo s4 = jedis.getShardInfo(keys.get(1));
|
||||||
assertNotSame(s3, s4);
|
assertNotSame(s3, s4);
|
||||||
|
|
||||||
ShardedJedis jedis2 = new ShardedJedis(shards);
|
ShardedJedis jedis2 = new ShardedJedis(shards);
|
||||||
@@ -117,8 +135,8 @@ public class ShardedJedisTest extends Assert {
|
|||||||
assertEquals(jedis2.getKeyTag("foo"), "foo");
|
assertEquals(jedis2.getKeyTag("foo"), "foo");
|
||||||
assertNotSame(jedis2.getKeyTag("foo{bar}"), "bar");
|
assertNotSame(jedis2.getKeyTag("foo{bar}"), "bar");
|
||||||
|
|
||||||
JedisShardInfo s5 = jedis2.getShardInfo("foo{bar}");
|
JedisShardInfo s5 = jedis2.getShardInfo(keys.get(0)+"{bar}");
|
||||||
JedisShardInfo s6 = jedis2.getShardInfo("abc{bar}");
|
JedisShardInfo s6 = jedis2.getShardInfo(keys.get(1)+"{bar}");
|
||||||
assertNotSame(s5, s6);
|
assertNotSame(s5, s6);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,15 +149,16 @@ public class ShardedJedisTest extends Assert {
|
|||||||
shards.get(1).setPassword("foobared");
|
shards.get(1).setPassword("foobared");
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
|
|
||||||
jedis.set("a112", "a");
|
final List<String> keys = getKeysDifferentShard(jedis);
|
||||||
jedis.set("b112", "b");
|
jedis.set(keys.get(0), "a");
|
||||||
|
jedis.set(keys.get(1), "b");
|
||||||
|
|
||||||
assertNotSame(jedis.getShard("a112"), jedis.getShard("b112"));
|
assertNotSame(jedis.getShard(keys.get(0)), jedis.getShard(keys.get(1)));
|
||||||
|
|
||||||
List<Object> results = jedis.pipelined(new ShardedJedisPipeline() {
|
List<Object> results = jedis.pipelined(new ShardedJedisPipeline() {
|
||||||
public void execute() {
|
public void execute() {
|
||||||
get("a112");
|
get(keys.get(0));
|
||||||
get("b112");
|
get(keys.get(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -222,3 +241,4 @@ public class ShardedJedisTest extends Assert {
|
|||||||
assertTrue(shard_6381 > 300 && shard_6381 < 400);
|
assertTrue(shard_6381 > 300 && shard_6381 < 400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user