Race condition when switching masters in JedisSentinelPool

Instead of recreating GenericObjectPool, we change the
underlying factory destination host. When returning
objects to the pool we make sure they are pointing at
the correct master.
This commit is contained in:
Nelson Rodrigues
2014-07-25 18:06:33 -07:00
parent 4f693872b9
commit 75d2ba751b
2 changed files with 26 additions and 9 deletions

View File

@@ -74,6 +74,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
initPool(master);
}
private volatile JedisFactory factory;
private volatile HostAndPort currentHostMaster;
public void destroy() {
@@ -91,10 +92,15 @@ public class JedisSentinelPool extends Pool<Jedis> {
private void initPool(HostAndPort master) {
if (!master.equals(currentHostMaster)) {
currentHostMaster = master;
if (factory == null) {
factory = new JedisFactory(master.getHost(), master.getPort(),
timeout, password, database);
initPool(poolConfig, factory);
} else {
factory.setHostAndPort(currentHostMaster);
}
log.info("Created JedisPool to master at " + master);
initPool(poolConfig,
new JedisFactory(master.getHost(), master.getPort(),
timeout, password, database));
}
}