Added a JUnit test for JedisSentinelPool.
This test will set up and get a master from a Redis master slave pair being watched by 2 Sentinels. It pings the master, segfaults it, asks the pool for another connection and makes sure it can ping it. This commit also restores the pom.xml file's scm information back to xetorthio and adds the default Sentinel port to the Procotol.
This commit is contained in:
@@ -231,9 +231,13 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
||||
}, "+switch-master");
|
||||
|
||||
} catch (JedisConnectionException e) {
|
||||
log.severe("Lost connection to Sentinel at " + host + ":" + port + ". Sleeping 5000ms and retrying.");
|
||||
|
||||
try { Thread.sleep(subscribeRetryWaitTimeMillis); } catch (InterruptedException e1) { e1.printStackTrace(); }
|
||||
if (running.get()) {
|
||||
log.severe("Lost connection to Sentinel at " + host + ":" + port + ". Sleeping 5000ms and retrying.");
|
||||
try { Thread.sleep(subscribeRetryWaitTimeMillis); } catch (InterruptedException e1) { e1.printStackTrace(); }
|
||||
} else {
|
||||
log.fine("Unsubscribing from Sentinel at " + host + ":" + port);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import redis.clients.util.SafeEncoder;
|
||||
public final class Protocol {
|
||||
|
||||
public static final int DEFAULT_PORT = 6379;
|
||||
public static final int DEFAULT_SENTINEL_PORT = 26379;
|
||||
public static final int DEFAULT_TIMEOUT = 2000;
|
||||
public static final int DEFAULT_DATABASE = 0;
|
||||
|
||||
|
||||
@@ -6,56 +6,104 @@ import java.util.List;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public class HostAndPortUtil {
|
||||
private static List<HostAndPort> hostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(
|
||||
2);
|
||||
private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>();
|
||||
private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>();
|
||||
|
||||
static {
|
||||
final HostAndPort defaulthnp1 = new HostAndPort();
|
||||
|
||||
HostAndPort defaulthnp1 = new HostAndPort();
|
||||
defaulthnp1.host = "localhost";
|
||||
defaulthnp1.port = Protocol.DEFAULT_PORT;
|
||||
hostAndPortList.add(defaulthnp1);
|
||||
defaulthnp1.port = Protocol.DEFAULT_PORT;
|
||||
redisHostAndPortList.add(defaulthnp1);
|
||||
|
||||
final HostAndPort defaulthnp2 = new HostAndPort();
|
||||
HostAndPort defaulthnp2 = new HostAndPort();
|
||||
defaulthnp2.host = "localhost";
|
||||
defaulthnp2.port = Protocol.DEFAULT_PORT + 1;
|
||||
hostAndPortList.add(defaulthnp2);
|
||||
redisHostAndPortList.add(defaulthnp2);
|
||||
|
||||
HostAndPort defaulthnp3 = new HostAndPort();
|
||||
defaulthnp3.host = "localhost";
|
||||
defaulthnp3.port = Protocol.DEFAULT_PORT + 2;
|
||||
redisHostAndPortList.add(defaulthnp3);
|
||||
|
||||
HostAndPort defaulthnp4 = new HostAndPort();
|
||||
defaulthnp4.host = "localhost";
|
||||
defaulthnp4.port = Protocol.DEFAULT_PORT + 3;
|
||||
redisHostAndPortList.add(defaulthnp4);
|
||||
|
||||
HostAndPort defaulthnp5 = new HostAndPort();
|
||||
defaulthnp5.host = "localhost";
|
||||
defaulthnp5.port = Protocol.DEFAULT_SENTINEL_PORT;
|
||||
sentinelHostAndPortList.add(defaulthnp5);
|
||||
|
||||
HostAndPort defaulthnp6 = new HostAndPort();
|
||||
defaulthnp6.host = "localhost";
|
||||
defaulthnp6.port = Protocol.DEFAULT_SENTINEL_PORT + 1;
|
||||
sentinelHostAndPortList.add(defaulthnp6);
|
||||
|
||||
HostAndPort defaulthnp7 = new HostAndPort();
|
||||
defaulthnp7.host = "localhost";
|
||||
defaulthnp7.port = Protocol.DEFAULT_SENTINEL_PORT + 2;
|
||||
sentinelHostAndPortList.add(defaulthnp7);
|
||||
|
||||
final String envHosts = System.getProperty("redis-hosts");
|
||||
if (null != envHosts && 0 < envHosts.length()) {
|
||||
final String[] hostDefs = envHosts.split(",");
|
||||
String envRedisHosts = System.getProperty("redis-hosts");
|
||||
String envSentinelHosts = System.getProperty("sentinel-hosts");
|
||||
|
||||
redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList);
|
||||
sentinelHostAndPortList = parseHosts(envSentinelHosts, sentinelHostAndPortList);
|
||||
}
|
||||
|
||||
public static List<HostAndPort> parseHosts(String envHosts, List<HostAndPort> existingHostsAndPorts) {
|
||||
|
||||
if (null != envHosts && 0 < envHosts.length()) {
|
||||
|
||||
String[] hostDefs = envHosts.split(",");
|
||||
|
||||
if (null != hostDefs && 2 <= hostDefs.length) {
|
||||
hostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(
|
||||
hostDefs.length);
|
||||
|
||||
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPortUtil.HostAndPort>(hostDefs.length);
|
||||
|
||||
for (String hostDef : hostDefs) {
|
||||
final String[] hostAndPort = hostDef.split(":");
|
||||
|
||||
String[] hostAndPort = hostDef.split(":");
|
||||
|
||||
if (null != hostAndPort && 2 == hostAndPort.length) {
|
||||
final HostAndPort hnp = new HostAndPort();
|
||||
|
||||
HostAndPort hnp = new HostAndPort();
|
||||
hnp.host = hostAndPort[0];
|
||||
|
||||
try {
|
||||
hnp.port = Integer.parseInt(hostAndPort[1]);
|
||||
} catch (final NumberFormatException nfe) {
|
||||
hnp.port = Protocol.DEFAULT_PORT;
|
||||
}
|
||||
hostAndPortList.add(hnp);
|
||||
}
|
||||
|
||||
envHostsAndPorts.add(hnp);
|
||||
}
|
||||
}
|
||||
|
||||
return envHostsAndPorts;
|
||||
}
|
||||
}
|
||||
final StringBuilder strb = new StringBuilder(
|
||||
"Redis hosts to be used : ");
|
||||
for (HostAndPort hnp : hostAndPortList) {
|
||||
strb.append('[').append(hnp.host).append(':').append(hnp.port)
|
||||
.append(']').append(' ');
|
||||
}
|
||||
System.out.println(strb);
|
||||
|
||||
return existingHostsAndPorts;
|
||||
}
|
||||
|
||||
|
||||
public static List<HostAndPort> getRedisServers() {
|
||||
return hostAndPortList;
|
||||
return redisHostAndPortList;
|
||||
}
|
||||
|
||||
public static List<HostAndPort> getSentinelServers() {
|
||||
return sentinelHostAndPortList;
|
||||
}
|
||||
|
||||
public static class HostAndPort {
|
||||
public String host;
|
||||
public int port;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return host + ":" + port;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.DebugParams;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisSentinelPool;
|
||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
||||
|
||||
public class JedisSentinelPoolTest extends JedisTestBase {
|
||||
|
||||
protected static HostAndPort master = HostAndPortUtil.getRedisServers().get(2);
|
||||
protected static HostAndPort slave1 = HostAndPortUtil.getRedisServers().get(3);
|
||||
protected static HostAndPort sentinel1 = HostAndPortUtil.getSentinelServers().get(1);
|
||||
protected static HostAndPort sentinel2 = HostAndPortUtil.getSentinelServers().get(2);
|
||||
|
||||
protected static Jedis masterJedis;
|
||||
protected static Jedis slaveJedis1;
|
||||
protected static Jedis sentinelJedis1;
|
||||
|
||||
protected Set<String> sentinels = new HashSet<String>();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
// set up master and slaves
|
||||
masterJedis = new Jedis(master.host, master.port);
|
||||
masterJedis.slaveofNoOne();
|
||||
|
||||
slaveJedis1 = new Jedis(slave1.host, slave1.port);
|
||||
slaveJedis1.slaveof(master.host, master.port);
|
||||
|
||||
sentinelJedis1 = new Jedis(sentinel1.host, sentinel1.port);
|
||||
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.
|
||||
Thread.sleep(10000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void segfaultMaster() throws InterruptedException {
|
||||
|
||||
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels);
|
||||
|
||||
Jedis jedis = pool.getResource();
|
||||
assertEquals("PONG", jedis.ping());
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ public class JedisSentinelTest {
|
||||
assertEquals("6379", masterHostAndPort.get(1));
|
||||
|
||||
List<Map<String, String>> slaves = j.sentinelSlaves(masterName);
|
||||
assertEquals(1, slaves.size());
|
||||
assertTrue(slaves.size() > 0);
|
||||
assertEquals("6379", slaves.get(0).get("master-port"));
|
||||
|
||||
List<? extends Object> isMasterDownByAddr = j
|
||||
|
||||
Reference in New Issue
Block a user