code cleanup
This commit is contained in:
@@ -116,71 +116,55 @@ public class ShardedJedisPoolTest extends Assert {
|
|||||||
assertNotSame(j1.getShard("foo"), j2.getShard("foo"));
|
assertNotSame(j1.getShard("foo"), j2.getShard("foo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkConnectionsWithNoServers() {
|
@Test
|
||||||
shards = new ArrayList<JedisShardInfo>();
|
public void checkFailedJedisServer() {
|
||||||
shards.add(new JedisShardInfo("localhost", 6379, "ssa"));
|
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
|
||||||
shards.add(new JedisShardInfo("localhost", 6380, "ssa"));
|
ShardedJedis jedis = pool.getResource();
|
||||||
Config redisConfig = new Config();
|
jedis.incr("foo");
|
||||||
redisConfig.testOnBorrow = false; // deactivated for now
|
pool.returnResource(jedis);
|
||||||
redisConfig.testOnReturn = true;
|
pool.destroy();
|
||||||
redisConfig.maxActive = 200; // nro threads + margen de seguridad?
|
}
|
||||||
redisConfig.minIdle = 200;
|
|
||||||
ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
|
|
||||||
ShardedJedis jedis = pool.getResource();
|
|
||||||
pool.returnResource(jedis);
|
|
||||||
pool.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void checkFailedJedisServer() {
|
|
||||||
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
|
|
||||||
ShardedJedis jedis = pool.getResource();
|
|
||||||
jedis.incr("foo");
|
|
||||||
pool.returnResource(jedis);
|
|
||||||
pool.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldReturnActiveShardsWhenOneGoesOffline() {
|
|
||||||
Config redisConfig = new Config();
|
|
||||||
redisConfig.testOnBorrow = false;
|
|
||||||
ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
|
|
||||||
ShardedJedis jedis = pool.getResource();
|
|
||||||
// fill the shards
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
|
||||||
jedis.set("a-test-" + i, "0");
|
|
||||||
}
|
|
||||||
pool.returnResource(jedis);
|
|
||||||
// check quantity for each shard
|
|
||||||
Jedis j = new Jedis(shards.get(0));
|
|
||||||
j.connect();
|
|
||||||
Long c1 = j.dbSize();
|
|
||||||
j.disconnect();
|
|
||||||
j = new Jedis(shards.get(1));
|
|
||||||
j.connect();
|
|
||||||
Long c2 = j.dbSize();
|
|
||||||
j.disconnect();
|
|
||||||
// shutdown shard 2 and check thay the pool returns an instance with c1
|
|
||||||
// items on one shard
|
|
||||||
// alter shard 1 and recreate pool
|
|
||||||
pool.destroy();
|
|
||||||
shards.set(1, new JedisShardInfo("nohost", 1234));
|
|
||||||
pool = new ShardedJedisPool(redisConfig, shards);
|
|
||||||
jedis = pool.getResource();
|
|
||||||
Long actual = new Long(0);
|
|
||||||
Long fails = new Long(0);
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
|
||||||
try {
|
|
||||||
jedis.get("a-test-" + i);
|
|
||||||
actual++;
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
fails++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pool.returnResource(jedis);
|
|
||||||
pool.destroy();
|
|
||||||
assertEquals(actual, c1);
|
|
||||||
assertEquals(fails, c2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnActiveShardsWhenOneGoesOffline() {
|
||||||
|
Config redisConfig = new Config();
|
||||||
|
redisConfig.testOnBorrow = false;
|
||||||
|
ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
|
||||||
|
ShardedJedis jedis = pool.getResource();
|
||||||
|
// fill the shards
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
jedis.set("a-test-" + i, "0");
|
||||||
|
}
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
// check quantity for each shard
|
||||||
|
Jedis j = new Jedis(shards.get(0));
|
||||||
|
j.connect();
|
||||||
|
Long c1 = j.dbSize();
|
||||||
|
j.disconnect();
|
||||||
|
j = new Jedis(shards.get(1));
|
||||||
|
j.connect();
|
||||||
|
Long c2 = j.dbSize();
|
||||||
|
j.disconnect();
|
||||||
|
// shutdown shard 2 and check thay the pool returns an instance with c1
|
||||||
|
// items on one shard
|
||||||
|
// alter shard 1 and recreate pool
|
||||||
|
pool.destroy();
|
||||||
|
shards.set(1, new JedisShardInfo("nohost", 1234));
|
||||||
|
pool = new ShardedJedisPool(redisConfig, shards);
|
||||||
|
jedis = pool.getResource();
|
||||||
|
Long actual = new Long(0);
|
||||||
|
Long fails = new Long(0);
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
try {
|
||||||
|
jedis.get("a-test-" + i);
|
||||||
|
actual++;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
fails++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
pool.destroy();
|
||||||
|
assertEquals(actual, c1);
|
||||||
|
assertEquals(fails, c2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -23,10 +23,10 @@ public class ShardedJedisTest extends Assert {
|
|||||||
.get(1);
|
.get(1);
|
||||||
|
|
||||||
private List<String> getKeysDifferentShard(ShardedJedis jedis) {
|
private List<String> getKeysDifferentShard(ShardedJedis jedis) {
|
||||||
List<String> ret = new ArrayList<String> ();
|
List<String> ret = new ArrayList<String>();
|
||||||
JedisShardInfo first = jedis.getShardInfo("a0");
|
JedisShardInfo first = jedis.getShardInfo("a0");
|
||||||
ret.add("a0");
|
ret.add("a0");
|
||||||
for (int i =1; i < 100; ++i) {
|
for (int i = 1; i < 100; ++i) {
|
||||||
JedisShardInfo actual = jedis.getShardInfo("a" + i);
|
JedisShardInfo actual = jedis.getShardInfo("a" + i);
|
||||||
if (actual != first) {
|
if (actual != first) {
|
||||||
ret.add("a" + i);
|
ret.add("a" + i);
|
||||||
@@ -135,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(keys.get(0)+"{bar}");
|
JedisShardInfo s5 = jedis2.getShardInfo(keys.get(0) + "{bar}");
|
||||||
JedisShardInfo s6 = jedis2.getShardInfo(keys.get(1)+"{bar}");
|
JedisShardInfo s6 = jedis2.getShardInfo(keys.get(1) + "{bar}");
|
||||||
assertNotSame(s5, s6);
|
assertNotSame(s5, s6);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,62 +241,63 @@ public class ShardedJedisTest extends Assert {
|
|||||||
assertTrue(shard_6381 > 300 && shard_6381 < 400);
|
assertTrue(shard_6381 > 300 && shard_6381 < 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMasterSlaveShardingConsistency() {
|
public void testMasterSlaveShardingConsistency() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
|
||||||
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
|
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT));
|
||||||
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
|
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1));
|
||||||
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
|
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2));
|
||||||
Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(
|
Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(
|
||||||
shards, Hashing.MURMUR_HASH);
|
shards, Hashing.MURMUR_HASH);
|
||||||
|
|
||||||
List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
|
List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
|
||||||
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
|
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT));
|
||||||
otherShards.add(new JedisShardInfo("otherhost",
|
otherShards.add(new JedisShardInfo("otherhost",
|
||||||
Protocol.DEFAULT_PORT + 1));
|
Protocol.DEFAULT_PORT + 1));
|
||||||
otherShards.add(new JedisShardInfo("otherhost",
|
otherShards.add(new JedisShardInfo("otherhost",
|
||||||
Protocol.DEFAULT_PORT + 2));
|
Protocol.DEFAULT_PORT + 2));
|
||||||
Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(
|
Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(
|
||||||
otherShards, Hashing.MURMUR_HASH);
|
otherShards, Hashing.MURMUR_HASH);
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer
|
JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer
|
||||||
.toString(i));
|
.toString(i));
|
||||||
JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer
|
JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer
|
||||||
.toString(i));
|
.toString(i));
|
||||||
assertEquals(shards.indexOf(jedisShardInfo),
|
assertEquals(shards.indexOf(jedisShardInfo), otherShards
|
||||||
otherShards.indexOf(jedisShardInfo2));
|
.indexOf(jedisShardInfo2));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@Test
|
|
||||||
public void testMasterSlaveShardingConsistencyWithShardNaming() {
|
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
|
|
||||||
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234"));
|
|
||||||
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1,"HOST2:1234"));
|
|
||||||
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234"));
|
|
||||||
Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(
|
|
||||||
shards, Hashing.MURMUR_HASH);
|
|
||||||
|
|
||||||
List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
|
@Test
|
||||||
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234"));
|
public void testMasterSlaveShardingConsistencyWithShardNaming() {
|
||||||
otherShards.add(new JedisShardInfo("otherhost",
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3);
|
||||||
Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
|
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT,
|
||||||
otherShards.add(new JedisShardInfo("otherhost",
|
"HOST1:1234"));
|
||||||
Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
|
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1,
|
||||||
Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(
|
"HOST2:1234"));
|
||||||
otherShards, Hashing.MURMUR_HASH);
|
shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2,
|
||||||
|
"HOST3:1234"));
|
||||||
|
Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(
|
||||||
|
shards, Hashing.MURMUR_HASH);
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3);
|
||||||
JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer
|
otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT,
|
||||||
.toString(i));
|
"HOST2:1234"));
|
||||||
JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer
|
otherShards.add(new JedisShardInfo("otherhost",
|
||||||
.toString(i));
|
Protocol.DEFAULT_PORT + 1, "HOST3:1234"));
|
||||||
assertEquals(jedisShardInfo.getName(),
|
otherShards.add(new JedisShardInfo("otherhost",
|
||||||
jedisShardInfo2.getName());
|
Protocol.DEFAULT_PORT + 2, "HOST1:1234"));
|
||||||
}
|
Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(
|
||||||
|
otherShards, Hashing.MURMUR_HASH);
|
||||||
}
|
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer
|
||||||
|
.toString(i));
|
||||||
|
JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer
|
||||||
|
.toString(i));
|
||||||
|
assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user