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

View File

@@ -26,36 +26,49 @@ public class JedisSentinelPool extends Pool<Jedis> {
protected Logger log = Logger.getLogger(getClass().getName()); protected Logger log = Logger.getLogger(getClass().getName());
public JedisSentinelPool(String masterName, Set<String> sentinels, final Config poolConfig) { public JedisSentinelPool(String masterName, Set<String> sentinels,
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); final Config poolConfig) {
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null,
Protocol.DEFAULT_DATABASE);
} }
public JedisSentinelPool(String masterName, Set<String> sentinels) { 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) { public JedisSentinelPool(String masterName, Set<String> sentinels,
this(masterName, sentinels, new Config(), Protocol.DEFAULT_TIMEOUT, password); 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) { public JedisSentinelPool(String masterName, Set<String> sentinels,
this(masterName, sentinels, poolConfig, timeout, password, Protocol.DEFAULT_DATABASE); 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) { public JedisSentinelPool(String masterName, Set<String> sentinels,
this(masterName, sentinels, poolConfig, timeout, null, Protocol.DEFAULT_DATABASE); 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) { public JedisSentinelPool(String masterName, Set<String> sentinels,
this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, password); 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) { final int database) {
this.poolConfig = poolConfig; this.poolConfig = poolConfig;
this.timeout = timeout; this.timeout = timeout;
this.password = password; this.password = password;
this.database = database; this.database = database;
HostAndPort master = initSentinels(sentinels, masterName); HostAndPort master = initSentinels(sentinels, masterName);
initPool(master); initPool(master);
} }
@@ -105,11 +118,13 @@ public class JedisSentinelPool extends Pool<Jedis> {
if (!master.equals(currentHostMaster)) { if (!master.equals(currentHostMaster)) {
currentHostMaster = master; currentHostMaster = master;
log.info("Created JedisPool to master at " + 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; HostAndPort master = null;
boolean running = true; boolean running = true;
@@ -120,7 +135,8 @@ public class JedisSentinelPool extends Pool<Jedis> {
for (String sentinel : sentinels) { 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); log.fine("Connecting to Sentinel " + hap);
@@ -128,29 +144,35 @@ public class JedisSentinelPool extends Pool<Jedis> {
Jedis jedis = new Jedis(hap.host, hap.port); Jedis jedis = new Jedis(hap.host, hap.port);
if (master == null) { if (master == null) {
master = toHostAndPort(jedis.sentinelGetMasterAddrByName(masterName)); master = toHostAndPort(jedis
.sentinelGetMasterAddrByName(masterName));
log.fine("Found Redis master at " + master); log.fine("Found Redis master at " + master);
jedis.disconnect(); jedis.disconnect();
break outer; break outer;
} }
} catch (JedisConnectionException e) { } 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 { 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); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); 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) { for (String sentinel : sentinels) {
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":"))); final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel
MasterListener masterListener = new MasterListener(masterName, hap.host, hap.port); .split(":")));
MasterListener masterListener = new MasterListener(masterName,
hap.host, hap.port);
masterListeners.add(masterListener); masterListeners.add(masterListener);
masterListener.start(); masterListener.start();
} }
@@ -167,17 +189,28 @@ public class JedisSentinelPool extends Pool<Jedis> {
protected class JedisPubSubAdapter extends JedisPubSub { protected class JedisPubSubAdapter extends JedisPubSub {
@Override @Override
public void onMessage(String channel, String message) {} public void onMessage(String channel, String message) {
}
@Override @Override
public void onPMessage(String pattern, String channel, String message) {} public void onPMessage(String pattern, String channel, String message) {
}
@Override @Override
public void onPSubscribe(String pattern, int subscribedChannels) {} public void onPSubscribe(String pattern, int subscribedChannels) {
}
@Override @Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {} public void onPUnsubscribe(String pattern, int subscribedChannels) {
}
@Override @Override
public void onSubscribe(String channel, int subscribedChannels) {} public void onSubscribe(String channel, int subscribedChannels) {
}
@Override @Override
public void onUnsubscribe(String channel, int subscribedChannels) {} public void onUnsubscribe(String channel, int subscribedChannels) {
}
} }
protected class MasterListener extends Thread { protected class MasterListener extends Thread {
@@ -189,7 +222,8 @@ public class JedisSentinelPool extends Pool<Jedis> {
protected Jedis j; protected Jedis j;
protected AtomicBoolean running = new AtomicBoolean(false); protected AtomicBoolean running = new AtomicBoolean(false);
protected MasterListener() {} protected MasterListener() {
}
public MasterListener(String masterName, String host, int port) { public MasterListener(String masterName, String host, int port) {
this.masterName = masterName; this.masterName = masterName;
@@ -197,7 +231,8 @@ public class JedisSentinelPool extends Pool<Jedis> {
this.port = port; 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(masterName, host, port);
this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis; this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis;
} }
@@ -214,21 +249,31 @@ public class JedisSentinelPool extends Pool<Jedis> {
j.subscribe(new JedisPubSubAdapter() { j.subscribe(new JedisPubSubAdapter() {
@Override @Override
public void onMessage(String channel, String message) { public void onMessage(String channel, String message) {
log.fine("Sentinel " + host + ":" + port
log.fine("Sentinel " + host + ":" + port + " published: " + message + "."); + " published: " + message + ".");
String[] switchMasterMsg = message.split(" "); String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3) { if (switchMasterMsg.length > 3) {
if (masterName.equals(switchMasterMsg[0])) { if (masterName.equals(switchMasterMsg[0])) {
initPool(toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4]))); initPool(toHostAndPort(Arrays.asList(
switchMasterMsg[3],
switchMasterMsg[4])));
} else { } 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 { } 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"); }, "+switch-master");
@@ -236,10 +281,17 @@ public class JedisSentinelPool extends Pool<Jedis> {
} catch (JedisConnectionException e) { } catch (JedisConnectionException e) {
if (running.get()) { if (running.get()) {
log.severe("Lost connection to Sentinel at " + host + ":" + port + ". Sleeping 5000ms and retrying."); log.severe("Lost connection to Sentinel at " + host
try { Thread.sleep(subscribeRetryWaitTimeMillis); } catch (InterruptedException e1) { e1.printStackTrace(); } + ":" + port
+ ". Sleeping 5000ms and retrying.");
try {
Thread.sleep(subscribeRetryWaitTimeMillis);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} else { } 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 // This isn't good, the Jedis object is not thread safe
j.disconnect(); j.disconnect();
} catch (Exception e) { } 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.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -13,14 +14,17 @@ import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class JedisSentinelPoolTest extends JedisTestBase { public class JedisSentinelPoolTest extends JedisTestBase {
protected static HostAndPort master = HostAndPortUtil.getRedisServers().get(2); protected static HostAndPort master = HostAndPortUtil.getRedisServers()
protected static HostAndPort slave1 = HostAndPortUtil.getRedisServers().get(3); .get(2);
protected static HostAndPort sentinel1 = HostAndPortUtil.getSentinelServers().get(1); protected static HostAndPort slave1 = HostAndPortUtil.getRedisServers()
protected static HostAndPort sentinel2 = HostAndPortUtil.getSentinelServers().get(2); .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 masterJedis;
protected static Jedis slaveJedis1; protected static Jedis slaveJedis1;
protected static Jedis sentinelJedis1;
protected Set<String> sentinels = new HashSet<String>(); protected Set<String> sentinels = new HashSet<String>();
@@ -29,12 +33,13 @@ public class JedisSentinelPoolTest extends JedisTestBase {
// set up master and slaves // set up master and slaves
masterJedis = new Jedis(master.host, master.port); masterJedis = new Jedis(master.host, master.port);
masterJedis.auth("foobared");
masterJedis.slaveofNoOne(); masterJedis.slaveofNoOne();
slaveJedis1 = new Jedis(slave1.host, slave1.port); slaveJedis1 = new Jedis(slave1.host, slave1.port);
slaveJedis1.auth("foobared");
slaveJedis1.slaveof(master.host, master.port); slaveJedis1.slaveof(master.host, master.port);
sentinelJedis1 = new Jedis(sentinel1.host, sentinel1.port);
sentinels.add(sentinel1.toString()); sentinels.add(sentinel1.toString());
sentinels.add(sentinel2.toString()); sentinels.add(sentinel2.toString());
@@ -46,12 +51,16 @@ public class JedisSentinelPoolTest extends JedisTestBase {
@Test @Test
public void segfaultMaster() throws InterruptedException { 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(); Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping()); 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 // wait for the sentinel to promote a master
// FIXME: we can query the sentinel and sleep // FIXME: we can query the sentinel and sleep
@@ -60,5 +69,7 @@ public class JedisSentinelPoolTest extends JedisTestBase {
jedis = pool.getResource(); jedis = pool.getResource();
assertEquals("PONG", jedis.ping()); assertEquals("PONG", jedis.ping());
assertEquals("foobared", jedis.configGet("requirepass").get(1));
assertEquals(2, jedis.getDB().intValue());
} }
} }