JedisSentinelPool now takes care of multi failover

* Now Pool.initPool() call closeInternalPool(), instead of destroy()
** calling destroy() in Pool.initPool() may have side effect, and JedisSentinelPool did
* modify unit test to test failover twice (needs +1 slave)
** modify configurations for additional slave
This commit is contained in:
Jungtaek Lim
2013-09-26 10:48:17 +09:00
parent 08e9b0c7e7
commit 35a2dfd4c0
5 changed files with 97 additions and 45 deletions

View File

@@ -33,18 +33,23 @@ public class HostAndPortUtil {
HostAndPort defaulthnp5 = new HostAndPort();
defaulthnp5.host = "localhost";
defaulthnp5.port = Protocol.DEFAULT_SENTINEL_PORT;
sentinelHostAndPortList.add(defaulthnp5);
defaulthnp5.port = Protocol.DEFAULT_PORT + 4;
redisHostAndPortList.add(defaulthnp5);
HostAndPort defaulthnp6 = new HostAndPort();
defaulthnp6.host = "localhost";
defaulthnp6.port = Protocol.DEFAULT_SENTINEL_PORT + 1;
defaulthnp6.port = Protocol.DEFAULT_SENTINEL_PORT;
sentinelHostAndPortList.add(defaulthnp6);
HostAndPort defaulthnp7 = new HostAndPort();
defaulthnp7.host = "localhost";
defaulthnp7.port = Protocol.DEFAULT_SENTINEL_PORT + 2;
defaulthnp7.port = Protocol.DEFAULT_SENTINEL_PORT + 1;
sentinelHostAndPortList.add(defaulthnp7);
HostAndPort defaulthnp8 = new HostAndPort();
defaulthnp8.host = "localhost";
defaulthnp8.port = Protocol.DEFAULT_SENTINEL_PORT + 2;
sentinelHostAndPortList.add(defaulthnp8);
String envRedisHosts = System.getProperty("redis-hosts");
String envSentinelHosts = System.getProperty("sentinel-hosts");

View File

@@ -18,6 +18,8 @@ public class JedisSentinelPoolTest extends JedisTestBase {
.get(2);
protected static HostAndPort slave1 = HostAndPortUtil.getRedisServers()
.get(3);
protected static HostAndPort slave2 = HostAndPortUtil.getRedisServers()
.get(4);
protected static HostAndPort sentinel1 = HostAndPortUtil
.getSentinelServers().get(1);
protected static HostAndPort sentinel2 = HostAndPortUtil
@@ -25,6 +27,9 @@ public class JedisSentinelPoolTest extends JedisTestBase {
protected static Jedis masterJedis;
protected static Jedis slaveJedis1;
protected static Jedis slaveJedis2;
protected static int slaveCount = 0;
protected Set<String> sentinels = new HashSet<String>();
@@ -39,37 +44,55 @@ public class JedisSentinelPoolTest extends JedisTestBase {
slaveJedis1 = new Jedis(slave1.host, slave1.port);
slaveJedis1.auth("foobared");
slaveJedis1.slaveof(master.host, master.port);
slaveCount++;
slaveJedis2 = new Jedis(slave2.host, slave2.port);
slaveJedis2.auth("foobared");
slaveJedis2.slaveof(master.host, master.port);
slaveCount++;
sentinels.add(sentinel1.toString());
sentinels.add(sentinel2.toString());
// FIXME: The following allows the master/slave relationship to
// be established. We can do this more elegantly.
// be established, and let sentinels know about this relationship.
// We can do this more elegantly.
Thread.sleep(10000);
}
@Test
public void segfaultMaster() throws InterruptedException {
public void ensureSafeTwiceFailover() throws InterruptedException {
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,
new Config(), 1000, "foobared", 2);
// perform failover
doSegFaultMaster(pool);
// perform failover once again
doSegFaultMaster(pool);
// you can test failover as much as possible
// but you need to prepare additional slave per failover
}
private void doSegFaultMaster(JedisSentinelPool pool) throws InterruptedException {
// jedis connection should be master
Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,
new Config(), 1000, "foobared", 2);
try {
jedis.debug(DebugParams.SEGFAULT());
} catch (Exception e) {
}
Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
// wait for the sentinel to promote a master
// FIXME: we can query the sentinel and sleep
// right until the master is promoted
Thread.sleep(35000);
try {
masterJedis.debug(DebugParams.SEGFAULT());
} catch (Exception e) {
}
// wait for the sentinel to promote a master
// FIXME: we can query the sentinel and sleep
// right until the master is promoted
Thread.sleep(35000);
jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("foobared", jedis.configGet("requirepass").get(1));
assertEquals(2, jedis.getDB().intValue());
jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("foobared", jedis.configGet("requirepass").get(1));
assertEquals(2, jedis.getDB().intValue());
}
}