Speed up Sentinel related tests
* remove FIXME sleep codes for Sentinel related tests ** add functions for Sentinel tests (JedisSentinelTestUtil) *** waitForSentinelRecognizeRedisReplication *** waitForNewPromotedMaster *** waitForSentinelsRecognizeEachOthers **** TODO: there're no command for sentinel to list recognized sentinel **** sleep 5.5 sec (sentinel pings to master every 5 sec) * set HostAndPort class to public(no longer inner) class ** reason: We cannot know pool's current master if HostAndPort class is private inner class / HostAndPort classes are duplicated (main/test) ** make getter method and parameterized constructor *** set fields once, get anytime
This commit is contained in:
39
src/main/java/redis/clients/jedis/HostAndPort.java
Normal file
39
src/main/java/redis/clients/jedis/HostAndPort.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
public class HostAndPort {
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
public HostAndPort(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof HostAndPort) {
|
||||
HostAndPort hp = (HostAndPort) obj;
|
||||
|
||||
// localhost and 127.0.0.1 is same
|
||||
return port == hp.port &&
|
||||
(host.equals(hp.host) ||
|
||||
(host.equals("localhost") && hp.host.equals("127.0.0.1")) ||
|
||||
(host.equals("127.0.0.1") && hp.host.equals("localhost")) );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return host + ":" + port;
|
||||
}
|
||||
}
|
||||
@@ -81,25 +81,6 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
||||
returnResourceObject(resource);
|
||||
}
|
||||
|
||||
private class HostAndPort {
|
||||
String host;
|
||||
int port;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof HostAndPort) {
|
||||
HostAndPort hp = (HostAndPort) obj;
|
||||
return port == hp.port && host.equals(hp.host);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return host + ":" + port;
|
||||
}
|
||||
}
|
||||
|
||||
private volatile HostAndPort currentHostMaster;
|
||||
|
||||
public void destroy() {
|
||||
@@ -118,7 +99,7 @@ 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,
|
||||
initPool(poolConfig, new JedisFactory(master.getHost(), master.getPort(),
|
||||
timeout, password, database));
|
||||
}
|
||||
}
|
||||
@@ -141,7 +122,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
||||
log.fine("Connecting to Sentinel " + hap);
|
||||
|
||||
try {
|
||||
Jedis jedis = new Jedis(hap.host, hap.port);
|
||||
Jedis jedis = new Jedis(hap.getHost(), hap.getPort());
|
||||
|
||||
if (master == null) {
|
||||
master = toHostAndPort(jedis
|
||||
@@ -172,7 +153,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
||||
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel
|
||||
.split(":")));
|
||||
MasterListener masterListener = new MasterListener(masterName,
|
||||
hap.host, hap.port);
|
||||
hap.getHost(), hap.getPort());
|
||||
masterListeners.add(masterListener);
|
||||
masterListener.start();
|
||||
}
|
||||
@@ -181,10 +162,10 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
||||
}
|
||||
|
||||
private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) {
|
||||
final HostAndPort hap = new HostAndPort();
|
||||
hap.host = getMasterAddrByNameResult.get(0);
|
||||
hap.port = Integer.parseInt(getMasterAddrByNameResult.get(1));
|
||||
return hap;
|
||||
String host = getMasterAddrByNameResult.get(0);
|
||||
int port = Integer.parseInt(getMasterAddrByNameResult.get(1));
|
||||
|
||||
return new HostAndPort(host, port);
|
||||
}
|
||||
|
||||
protected class JedisPubSubAdapter extends JedisPubSub {
|
||||
|
||||
Reference in New Issue
Block a user