make JedisSentinelPool fast
This commit is contained in:
2
Makefile
2
Makefile
@@ -38,6 +38,7 @@ pidfile /tmp/redis4.pid
|
|||||||
logfile /tmp/redis4.log
|
logfile /tmp/redis4.log
|
||||||
save ""
|
save ""
|
||||||
appendonly no
|
appendonly no
|
||||||
|
slaveof localhost 6381
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define REDIS5_CONF
|
define REDIS5_CONF
|
||||||
@@ -49,6 +50,7 @@ pidfile /tmp/redis5.pid
|
|||||||
logfile /tmp/redis5.log
|
logfile /tmp/redis5.log
|
||||||
save ""
|
save ""
|
||||||
appendonly no
|
appendonly no
|
||||||
|
slaveof localhost 6381
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define REDIS6_CONF
|
define REDIS6_CONF
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package redis.clients.jedis.tests;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -12,8 +11,8 @@ import org.junit.Test;
|
|||||||
import redis.clients.jedis.DebugParams;
|
import redis.clients.jedis.DebugParams;
|
||||||
import redis.clients.jedis.HostAndPort;
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisPubSub;
|
||||||
import redis.clients.jedis.JedisSentinelPool;
|
import redis.clients.jedis.JedisSentinelPool;
|
||||||
import redis.clients.jedis.tests.utils.JedisSentinelTestUtil;
|
|
||||||
|
|
||||||
public class JedisSentinelPoolTest extends JedisTestBase {
|
public class JedisSentinelPoolTest extends JedisTestBase {
|
||||||
private static final String MASTER_NAME = "mymaster";
|
private static final String MASTER_NAME = "mymaster";
|
||||||
@@ -29,43 +28,16 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
protected static HostAndPort sentinel2 = HostAndPortUtil
|
protected static HostAndPort sentinel2 = HostAndPortUtil
|
||||||
.getSentinelServers().get(2);
|
.getSentinelServers().get(2);
|
||||||
|
|
||||||
protected static Jedis masterJedis;
|
|
||||||
protected static Jedis slaveJedis1;
|
|
||||||
protected static Jedis slaveJedis2;
|
|
||||||
protected static Jedis sentinelJedis1;
|
protected static Jedis sentinelJedis1;
|
||||||
protected static Jedis sentinelJedis2;
|
|
||||||
|
|
||||||
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
|
|
||||||
masterJedis = new Jedis(master.getHost(), master.getPort());
|
|
||||||
masterJedis.auth("foobared");
|
|
||||||
masterJedis.slaveofNoOne();
|
|
||||||
|
|
||||||
slaveJedis1 = new Jedis(slave1.getHost(), slave1.getPort());
|
|
||||||
slaveJedis1.auth("foobared");
|
|
||||||
slaveJedis1.slaveof(master.getHost(), master.getPort());
|
|
||||||
|
|
||||||
slaveJedis2 = new Jedis(slave2.getHost(), slave2.getPort());
|
|
||||||
slaveJedis2.auth("foobared");
|
|
||||||
slaveJedis2.slaveof(master.getHost(), master.getPort());
|
|
||||||
|
|
||||||
sentinels.add(sentinel1.toString());
|
sentinels.add(sentinel1.toString());
|
||||||
sentinels.add(sentinel2.toString());
|
sentinels.add(sentinel2.toString());
|
||||||
|
|
||||||
List<HostAndPort> slaves = new ArrayList<HostAndPort>();
|
sentinelJedis1 = new Jedis(sentinel1.getHost(), sentinel1.getPort());
|
||||||
slaves.add(slave1);
|
|
||||||
slaves.add(slave2);
|
|
||||||
|
|
||||||
JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(
|
|
||||||
sentinel1, MASTER_NAME, master, slaves);
|
|
||||||
JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(
|
|
||||||
sentinel2, MASTER_NAME, master, slaves);
|
|
||||||
|
|
||||||
// No need to wait for sentinels to recognize each other
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -106,17 +78,62 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
|
|
||||||
private void waitForFailover(JedisSentinelPool pool, HostAndPort oldMaster)
|
private void waitForFailover(JedisSentinelPool pool, HostAndPort oldMaster)
|
||||||
throws InterruptedException {
|
throws InterruptedException {
|
||||||
HostAndPort newMaster = JedisSentinelTestUtil.waitForNewPromotedMaster(
|
waitForJedisSentinelPoolRecognizeNewMaster(pool);
|
||||||
sentinel1, MASTER_NAME, oldMaster);
|
|
||||||
JedisSentinelTestUtil.waitForNewPromotedMaster(sentinel2, MASTER_NAME,
|
|
||||||
oldMaster);
|
|
||||||
JedisSentinelTestUtil.waitForSentinelsRecognizeEachOthers();
|
|
||||||
waitForJedisSentinelPoolRecognizeNewMaster(pool, newMaster);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void waitForJedisSentinelPoolRecognizeNewMaster(
|
private void waitForJedisSentinelPoolRecognizeNewMaster(
|
||||||
JedisSentinelPool pool, HostAndPort newMaster)
|
JedisSentinelPool pool) throws InterruptedException {
|
||||||
throws InterruptedException {
|
|
||||||
|
final AtomicReference<String> newmaster = new AtomicReference<String>(
|
||||||
|
"");
|
||||||
|
|
||||||
|
sentinelJedis1.psubscribe(new JedisPubSub() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(String channel, String message) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPMessage(String pattern, String channel,
|
||||||
|
String message) {
|
||||||
|
if (channel.equals("+switch-master")) {
|
||||||
|
newmaster.set(message);
|
||||||
|
punsubscribe();
|
||||||
|
}
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(String channel, int subscribedChannels) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUnsubscribe(String channel, int subscribedChannels) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPUnsubscribe(String pattern, int subscribedChannels) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPSubscribe(String pattern, int subscribedChannels) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
}, "*");
|
||||||
|
|
||||||
|
String[] chunks = newmaster.get().split(" ");
|
||||||
|
HostAndPort newMaster = new HostAndPort(chunks[3],
|
||||||
|
Integer.parseInt(chunks[4]));
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
String host = pool.getCurrentHostMaster().getHost();
|
String host = pool.getCurrentHostMaster().getHost();
|
||||||
@@ -128,7 +145,7 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
System.out
|
System.out
|
||||||
.println("JedisSentinelPool's master is not yet changed, sleep...");
|
.println("JedisSentinelPool's master is not yet changed, sleep...");
|
||||||
|
|
||||||
Thread.sleep(1000);
|
Thread.sleep(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user