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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user