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,234 +26,287 @@ 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 int database) { final Config poolConfig, int timeout, final String password,
this.poolConfig = poolConfig; final int database) {
this.timeout = timeout; this.poolConfig = poolConfig;
this.password = password; this.timeout = timeout;
this.database = database; this.password = password;
HostAndPort master = initSentinels(sentinels, masterName); this.database = database;
initPool(master);
HostAndPort master = initSentinels(sentinels, masterName);
initPool(master);
} }
public void returnBrokenResource(final BinaryJedis resource) { public void returnBrokenResource(final BinaryJedis resource) {
returnBrokenResourceObject(resource); returnBrokenResourceObject(resource);
} }
public void returnResource(final BinaryJedis resource) { public void returnResource(final BinaryJedis resource) {
returnResourceObject(resource); returnResourceObject(resource);
} }
private class HostAndPort { private class HostAndPort {
String host; String host;
int port; int port;
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof HostAndPort) { if (obj instanceof HostAndPort) {
HostAndPort hp = (HostAndPort) obj; HostAndPort hp = (HostAndPort) obj;
return port == hp.port && host.equals(hp.host); return port == hp.port && host.equals(hp.host);
} }
return false; return false;
} }
@Override @Override
public String toString() { public String toString() {
return host + ":" + port; return host + ":" + port;
} }
} }
private volatile HostAndPort currentHostMaster; private volatile HostAndPort currentHostMaster;
public void destroy() { public void destroy() {
for (MasterListener m : masterListeners) { for (MasterListener m : masterListeners) {
m.shutdown(); m.shutdown();
} }
super.destroy(); super.destroy();
} }
public HostAndPort getCurrentHostMaster() { public HostAndPort getCurrentHostMaster() {
return currentHostMaster; return currentHostMaster;
} }
private void initPool(HostAndPort master) { private void initPool(HostAndPort master) {
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;
outer: while (running) { outer: while (running) {
log.info("Trying to find master from available Sentinels..."); log.info("Trying to find master from available Sentinels...");
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);
try { try {
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
log.fine("Found Redis master at " + master); .sentinelGetMasterAddrByName(masterName));
jedis.disconnect(); log.fine("Found Redis master at " + master);
break outer; jedis.disconnect();
} break outer;
} catch (JedisConnectionException e) { }
log.warning("Cannot connect to sentinel running @ " + hap + ". Trying next one."); } catch (JedisConnectionException e) {
} 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 "
Thread.sleep(1000); + masterName + " master is running... sleeping 1000ms.");
} catch (InterruptedException e) { Thread.sleep(1000);
e.printStackTrace(); } 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) { 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(":")));
masterListeners.add(masterListener); MasterListener masterListener = new MasterListener(masterName,
masterListener.start(); hap.host, hap.port);
} masterListeners.add(masterListener);
masterListener.start();
}
return master; return master;
} }
private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) { private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) {
final HostAndPort hap = new HostAndPort(); final HostAndPort hap = new HostAndPort();
hap.host = getMasterAddrByNameResult.get(0); hap.host = getMasterAddrByNameResult.get(0);
hap.port = Integer.parseInt(getMasterAddrByNameResult.get(1)); hap.port = Integer.parseInt(getMasterAddrByNameResult.get(1));
return hap; return hap;
} }
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 }
public void onPMessage(String pattern, String channel, String message) {}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {}
@Override
public void onSubscribe(String channel, int subscribedChannels) {}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {}
}
protected class MasterListener extends Thread { @Override
public void onPMessage(String pattern, String channel, String message) {
}
protected String masterName; @Override
protected String host; public void onPSubscribe(String pattern, int subscribedChannels) {
protected int port; }
protected long subscribeRetryWaitTimeMillis = 5000;
protected Jedis j;
protected AtomicBoolean running = new AtomicBoolean(false);
protected MasterListener() {} @Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
}
public MasterListener(String masterName, String host, int port) { @Override
this.masterName = masterName; public void onSubscribe(String channel, int subscribedChannels) {
this.host = host; }
this.port = port;
}
public MasterListener(String masterName, String host, int port, long subscribeRetryWaitTimeMillis) { @Override
this(masterName, host, port); public void onUnsubscribe(String channel, int subscribedChannels) {
this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis; }
} }
public void run() { protected class MasterListener extends Thread {
running.set(true); protected String masterName;
protected String host;
protected int port;
protected long subscribeRetryWaitTimeMillis = 5000;
protected Jedis j;
protected AtomicBoolean running = new AtomicBoolean(false);
while (running.get()) { protected MasterListener() {
}
j = new Jedis(host, port); public MasterListener(String masterName, String host, int port) {
this.masterName = masterName;
this.host = host;
this.port = port;
}
try { public MasterListener(String masterName, String host, int port,
j.subscribe(new JedisPubSubAdapter() { long subscribeRetryWaitTimeMillis) {
@Override this(masterName, host, port);
public void onMessage(String channel, String message) { this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis;
}
log.fine("Sentinel " + host + ":" + port + " published: " + message + "."); public void run() {
String[] switchMasterMsg = message.split(" "); running.set(true);
if (switchMasterMsg.length > 3) { while (running.get()) {
if (masterName.equals(switchMasterMsg[0])) { j = new Jedis(host, port);
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);
}
} else { try {
log.severe("Invalid message received on Sentinel " + host + ":" + port + " on channel +switch-master: " + message); j.subscribe(new JedisPubSubAdapter() {
} @Override
} public void onMessage(String channel, String message) {
}, "+switch-master"); log.fine("Sentinel " + host + ":" + port
+ " published: " + message + ".");
} catch (JedisConnectionException e) { String[] switchMasterMsg = message.split(" ");
if (running.get()) { if (switchMasterMsg.length > 3) {
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);
}
}
}
}
public void shutdown() { if (masterName.equals(switchMasterMsg[0])) {
try { initPool(toHostAndPort(Arrays.asList(
log.fine("Shutting down listener on " + host + ":" + port); switchMasterMsg[3],
running.set(false); switchMasterMsg[4])));
// This isn't good, the Jedis object is not thread safe } else {
j.disconnect(); log.fine("Ignoring message on +switch-master for master name "
} catch (Exception e) { + switchMasterMsg[0]
log.severe("Caught exception while shutting down: " + e.getMessage()); + ", our master name is "
} + masterName);
} }
}
} else {
log.severe("Invalid message received on Sentinel "
+ host
+ ":"
+ port
+ " on channel +switch-master: "
+ message);
}
}
}, "+switch-master");
} 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();
}
} else {
log.fine("Unsubscribing from Sentinel at " + host + ":"
+ port);
}
}
}
}
public void shutdown() {
try {
log.fine("Shutting down listener on " + host + ":" + port);
running.set(false);
// 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());
}
}
}
} }

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,52 +14,62 @@ 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>();
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// 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.slaveofNoOne(); masterJedis.auth("foobared");
masterJedis.slaveofNoOne();
slaveJedis1 = new Jedis(slave1.host, slave1.port); slaveJedis1 = new Jedis(slave1.host, slave1.port);
slaveJedis1.slaveof(master.host, master.port); slaveJedis1.auth("foobared");
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());
// FIXME: The following allows the master/slave relationship to // FIXME: The following allows the master/slave relationship to
// be established. We can do this more elegantly. // be established. We can do this more elegantly.
Thread.sleep(10000); Thread.sleep(10000);
} }
@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
// right until the master is promoted // right until the master is promoted
Thread.sleep(35000); Thread.sleep(35000);
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());
} }
} }