make sure that JedisSentinelPool use timeout, password and db

This commit is contained in:
Jonathan Leibiusky
2013-09-15 15:33:22 -03:00
parent 4d9090827f
commit ad58f816b6
3 changed files with 332 additions and 263 deletions

View File

@@ -21,6 +21,7 @@ endef
define REDIS3_CONF
daemonize yes
port 6381
requirepass foobared
pidfile /tmp/redis3.pid
logfile /tmp/redis3.log
save ""
@@ -30,6 +31,8 @@ endef
define REDIS4_CONF
daemonize yes
port 6382
requirepass foobared
masterauth foobared
pidfile /tmp/redis4.pid
logfile /tmp/redis4.log
save ""
@@ -39,7 +42,7 @@ endef
define REDIS_SENTINEL1
port 26379
daemonize yes
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel monitor mymaster 127.0.0.1 6381 2
sentinel auth-pass mymaster foobared
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 900000
@@ -53,6 +56,7 @@ define REDIS_SENTINEL2
port 26380
daemonize yes
sentinel monitor mymaster 127.0.0.1 6381 2
sentinel auth-pass mymaster foobared
sentinel down-after-milliseconds mymaster 3000
sentinel can-failover mymaster yes
sentinel parallel-syncs mymaster 1
@@ -65,6 +69,7 @@ define REDIS_SENTINEL3
port 26381
daemonize yes
sentinel monitor mymaster 127.0.0.1 6381 2
sentinel auth-pass mymaster foobared
sentinel down-after-milliseconds mymaster 3000
sentinel can-failover mymaster yes
sentinel parallel-syncs mymaster 1

View File

@@ -26,36 +26,49 @@ public class JedisSentinelPool extends Pool<Jedis> {
protected Logger log = Logger.getLogger(getClass().getName());
public JedisSentinelPool(String masterName, Set<String> sentinels, final Config poolConfig) {
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig) {
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels) {
this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT,
null, Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels, String password) {
this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT, password);
public JedisSentinelPool(String masterName, Set<String> sentinels,
String password) {
this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT,
password);
}
public JedisSentinelPool(String masterName, Set<String> sentinels, final Config poolConfig, int timeout, final String password) {
this(masterName, sentinels, poolConfig, timeout, password, Protocol.DEFAULT_DATABASE);
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, int timeout, final String password) {
this(masterName, sentinels, poolConfig, timeout, password,
Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels, final Config poolConfig, final int timeout) {
this(masterName, sentinels, poolConfig, timeout, null, Protocol.DEFAULT_DATABASE);
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, final int timeout) {
this(masterName, sentinels, poolConfig, timeout, null,
Protocol.DEFAULT_DATABASE);
}
public JedisSentinelPool(String masterName, Set<String> sentinels, final Config poolConfig, final String password) {
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, password);
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, final String password) {
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT,
password);
}
public JedisSentinelPool(String masterName, Set<String> sentinels, final Config poolConfig, int timeout, final String password,
public JedisSentinelPool(String masterName, Set<String> sentinels,
final Config poolConfig, int timeout, final String password,
final int database) {
this.poolConfig = poolConfig;
this.timeout = timeout;
this.password = password;
this.database = database;
HostAndPort master = initSentinels(sentinels, masterName);
initPool(master);
}
@@ -105,11 +118,13 @@ public class JedisSentinelPool extends Pool<Jedis> {
if (!master.equals(currentHostMaster)) {
currentHostMaster = master;
log.info("Created JedisPool to master at " + master);
initPool(poolConfig, new JedisFactory(master.host, master.port, timeout, password, database));
initPool(poolConfig, new JedisFactory(master.host, master.port,
timeout, password, database));
}
}
private HostAndPort initSentinels(Set<String> sentinels, final String masterName) {
private HostAndPort initSentinels(Set<String> sentinels,
final String masterName) {
HostAndPort master = null;
boolean running = true;
@@ -120,7 +135,8 @@ public class JedisSentinelPool extends Pool<Jedis> {
for (String sentinel : sentinels) {
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel
.split(":")));
log.fine("Connecting to Sentinel " + hap);
@@ -128,29 +144,35 @@ public class JedisSentinelPool extends Pool<Jedis> {
Jedis jedis = new Jedis(hap.host, hap.port);
if (master == null) {
master = toHostAndPort(jedis.sentinelGetMasterAddrByName(masterName));
master = toHostAndPort(jedis
.sentinelGetMasterAddrByName(masterName));
log.fine("Found Redis master at " + master);
jedis.disconnect();
break outer;
}
} catch (JedisConnectionException e) {
log.warning("Cannot connect to sentinel running @ " + hap + ". Trying next one.");
log.warning("Cannot connect to sentinel running @ " + hap
+ ". Trying next one.");
}
}
try {
log.severe("All sentinels down, cannot determine where is " + masterName + " master is running... sleeping 1000ms.");
log.severe("All sentinels down, cannot determine where is "
+ masterName + " master is running... sleeping 1000ms.");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info("Redis master running at " + master + ", starting Sentinel listeners...");
log.info("Redis master running at " + master
+ ", starting Sentinel listeners...");
for (String sentinel : sentinels) {
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));
MasterListener masterListener = new MasterListener(masterName, hap.host, hap.port);
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel
.split(":")));
MasterListener masterListener = new MasterListener(masterName,
hap.host, hap.port);
masterListeners.add(masterListener);
masterListener.start();
}
@@ -167,17 +189,28 @@ public class JedisSentinelPool extends Pool<Jedis> {
protected class JedisPubSubAdapter extends JedisPubSub {
@Override
public void onMessage(String channel, String message) {}
public void onMessage(String channel, String message) {
}
@Override
public void onPMessage(String pattern, String channel, String message) {}
public void onPMessage(String pattern, String channel, String message) {
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {}
public void onPSubscribe(String pattern, int subscribedChannels) {
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {}
public void onPUnsubscribe(String pattern, int subscribedChannels) {
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {}
public void onSubscribe(String channel, int subscribedChannels) {
}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {}
public void onUnsubscribe(String channel, int subscribedChannels) {
}
}
protected class MasterListener extends Thread {
@@ -189,7 +222,8 @@ public class JedisSentinelPool extends Pool<Jedis> {
protected Jedis j;
protected AtomicBoolean running = new AtomicBoolean(false);
protected MasterListener() {}
protected MasterListener() {
}
public MasterListener(String masterName, String host, int port) {
this.masterName = masterName;
@@ -197,7 +231,8 @@ public class JedisSentinelPool extends Pool<Jedis> {
this.port = port;
}
public MasterListener(String masterName, String host, int port, long subscribeRetryWaitTimeMillis) {
public MasterListener(String masterName, String host, int port,
long subscribeRetryWaitTimeMillis) {
this(masterName, host, port);
this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis;
}
@@ -214,21 +249,31 @@ public class JedisSentinelPool extends Pool<Jedis> {
j.subscribe(new JedisPubSubAdapter() {
@Override
public void onMessage(String channel, String message) {
log.fine("Sentinel " + host + ":" + port + " published: " + message + ".");
log.fine("Sentinel " + host + ":" + port
+ " published: " + message + ".");
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3) {
if (masterName.equals(switchMasterMsg[0])) {
initPool(toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
initPool(toHostAndPort(Arrays.asList(
switchMasterMsg[3],
switchMasterMsg[4])));
} else {
log.fine("Ignoring message on +switch-master for master name " + switchMasterMsg[0] + ", our master name is " + masterName);
log.fine("Ignoring message on +switch-master for master name "
+ switchMasterMsg[0]
+ ", our master name is "
+ masterName);
}
} else {
log.severe("Invalid message received on Sentinel " + host + ":" + port + " on channel +switch-master: " + message);
log.severe("Invalid message received on Sentinel "
+ host
+ ":"
+ port
+ " on channel +switch-master: "
+ message);
}
}
}, "+switch-master");
@@ -236,10 +281,17 @@ public class JedisSentinelPool extends Pool<Jedis> {
} catch (JedisConnectionException e) {
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(); }
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);
log.fine("Unsubscribing from Sentinel at " + host + ":"
+ port);
}
}
}
@@ -252,7 +304,8 @@ public class JedisSentinelPool extends Pool<Jedis> {
// This isn't good, the Jedis object is not thread safe
j.disconnect();
} catch (Exception e) {
log.severe("Caught exception while shutting down: " + e.getMessage());
log.severe("Caught exception while shutting down: "
+ e.getMessage());
}
}
}

View File

@@ -3,6 +3,7 @@ package redis.clients.jedis.tests;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.junit.Before;
import org.junit.Test;
@@ -13,14 +14,17 @@ 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 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>();
@@ -29,12 +33,13 @@ public class JedisSentinelPoolTest extends JedisTestBase {
// set up master and slaves
masterJedis = new Jedis(master.host, master.port);
masterJedis.auth("foobared");
masterJedis.slaveofNoOne();
slaveJedis1 = new Jedis(slave1.host, slave1.port);
slaveJedis1.auth("foobared");
slaveJedis1.slaveof(master.host, master.port);
sentinelJedis1 = new Jedis(sentinel1.host, sentinel1.port);
sentinels.add(sentinel1.toString());
sentinels.add(sentinel2.toString());
@@ -46,12 +51,16 @@ public class JedisSentinelPoolTest extends JedisTestBase {
@Test
public void segfaultMaster() throws InterruptedException {
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels);
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,
new Config(), 1000, "foobared", 2);
Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
try { masterJedis.debug(DebugParams.SEGFAULT()); } catch (Exception e) {}
try {
masterJedis.debug(DebugParams.SEGFAULT());
} catch (Exception e) {
}
// wait for the sentinel to promote a master
// FIXME: we can query the sentinel and sleep
@@ -60,5 +69,7 @@ public class JedisSentinelPoolTest extends JedisTestBase {
jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("foobared", jedis.configGet("requirepass").get(1));
assertEquals(2, jedis.getDB().intValue());
}
}