Merge branch 'speed-up-unit-test' of github.com:HeartSaVioR/jedis into upgrade_libs
Conflicts: src/test/java/redis/clients/jedis/tests/JedisPoolTest.java src/test/java/redis/clients/jedis/tests/JedisSentinelPoolTest.java src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java src/test/java/redis/clients/jedis/tests/ShardedJedisPipelineTest.java src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java src/test/java/redis/clients/jedis/tests/commands/TransactionCommandsTest.java
This commit is contained in:
6
Makefile
6
Makefile
@@ -101,9 +101,9 @@ start:
|
|||||||
echo "$$REDIS3_CONF" | redis-server -
|
echo "$$REDIS3_CONF" | redis-server -
|
||||||
echo "$$REDIS4_CONF" | redis-server -
|
echo "$$REDIS4_CONF" | redis-server -
|
||||||
echo "$$REDIS5_CONF" | redis-server -
|
echo "$$REDIS5_CONF" | redis-server -
|
||||||
echo "$$REDIS_SENTINEL1" | redis-server - --sentinel
|
echo "$$REDIS_SENTINEL1" > /tmp/sentinel1.conf && redis-server /tmp/sentinel1.conf --sentinel
|
||||||
echo "$$REDIS_SENTINEL2" | redis-server - --sentinel
|
echo "$$REDIS_SENTINEL2" > /tmp/sentinel2.conf && redis-server /tmp/sentinel2.conf --sentinel
|
||||||
echo "$$REDIS_SENTINEL3" | redis-server - --sentinel
|
echo "$$REDIS_SENTINEL3" > /tmp/sentinel3.conf && redis-server /tmp/sentinel3.conf --sentinel
|
||||||
|
|
||||||
stop:
|
stop:
|
||||||
kill `cat /tmp/redis1.pid`
|
kill `cat /tmp/redis1.pid`
|
||||||
|
|||||||
@@ -752,11 +752,6 @@ public class Client extends BinaryClient implements Commands {
|
|||||||
sentinel(arg);
|
sentinel(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sentinel(final String cmd, String arg1, int arg2) {
|
|
||||||
sentinel(SafeEncoder.encode(cmd), SafeEncoder.encode(arg1),
|
|
||||||
toByteArray(arg2));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dump(final String key) {
|
public void dump(final String key) {
|
||||||
dump(SafeEncoder.encode(key));
|
dump(SafeEncoder.encode(key));
|
||||||
}
|
}
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2997,27 +2997,6 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand
|
|||||||
return slaves;
|
return slaves;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* <pre>
|
|
||||||
* redis 127.0.0.1:26381> SENTINEL is-master-down-by-addr 127.0.0.1 1
|
|
||||||
* 1) (integer) 0
|
|
||||||
* 2) "?"
|
|
||||||
* redis 127.0.0.1:26381> SENTINEL is-master-down-by-addr 127.0.0.1 6379
|
|
||||||
* 1) (integer) 0
|
|
||||||
* 2) "aaef11fbb2712346a386078c7f9834e72ed51e96"
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @return Long followed by the String (runid)
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public List<? extends Object> sentinelIsMasterDownByAddr(String host,
|
|
||||||
int port) {
|
|
||||||
client.sentinel(Protocol.SENTINEL_IS_MASTER_DOWN_BY_ADDR, host, port);
|
|
||||||
final List<Object> reply = client.getObjectMultiBulkReply();
|
|
||||||
return Arrays.asList(BuilderFactory.LONG.build(reply.get(0)),
|
|
||||||
BuilderFactory.STRING.build(reply.get(1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] dump(final String key) {
|
public byte[] dump(final String key) {
|
||||||
checkIsInMulti();
|
checkIsInMulti();
|
||||||
client.dump(key);
|
client.dump(key);
|
||||||
|
|||||||
@@ -82,25 +82,6 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
|||||||
returnResourceObject(resource);
|
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;
|
private volatile HostAndPort currentHostMaster;
|
||||||
|
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
@@ -119,7 +100,7 @@ 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,
|
initPool(poolConfig, new JedisFactory(master.getHost(), master.getPort(),
|
||||||
timeout, password, database));
|
timeout, password, database));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +123,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
|||||||
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.getHost(), hap.getPort());
|
||||||
|
|
||||||
if (master == null) {
|
if (master == null) {
|
||||||
master = toHostAndPort(jedis
|
master = toHostAndPort(jedis
|
||||||
@@ -173,7 +154,7 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
|||||||
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel
|
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel
|
||||||
.split(":")));
|
.split(":")));
|
||||||
MasterListener masterListener = new MasterListener(masterName,
|
MasterListener masterListener = new MasterListener(masterName,
|
||||||
hap.host, hap.port);
|
hap.getHost(), hap.getPort());
|
||||||
masterListeners.add(masterListener);
|
masterListeners.add(masterListener);
|
||||||
masterListener.start();
|
masterListener.start();
|
||||||
}
|
}
|
||||||
@@ -182,10 +163,10 @@ public class JedisSentinelPool extends Pool<Jedis> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) {
|
private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) {
|
||||||
final HostAndPort hap = new HostAndPort();
|
String host = getMasterAddrByNameResult.get(0);
|
||||||
hap.host = getMasterAddrByNameResult.get(0);
|
int port = Integer.parseInt(getMasterAddrByNameResult.get(1));
|
||||||
hap.port = Integer.parseInt(getMasterAddrByNameResult.get(1));
|
|
||||||
return hap;
|
return new HostAndPort(host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class JedisPubSubAdapter extends JedisPubSub {
|
protected class JedisPubSubAdapter extends JedisPubSub {
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ public final class Protocol {
|
|||||||
public static final String SENTINEL_GET_MASTER_ADDR_BY_NAME = "get-master-addr-by-name";
|
public static final String SENTINEL_GET_MASTER_ADDR_BY_NAME = "get-master-addr-by-name";
|
||||||
public static final String SENTINEL_RESET = "reset";
|
public static final String SENTINEL_RESET = "reset";
|
||||||
public static final String SENTINEL_SLAVES = "slaves";
|
public static final String SENTINEL_SLAVES = "slaves";
|
||||||
public static final String SENTINEL_IS_MASTER_DOWN_BY_ADDR = "is-master-down-by-addr";
|
|
||||||
|
|
||||||
private Protocol() {
|
private Protocol() {
|
||||||
// this prevent the class from instantiation
|
// this prevent the class from instantiation
|
||||||
|
|||||||
@@ -3,52 +3,37 @@ package redis.clients.jedis.tests;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Protocol;
|
import redis.clients.jedis.Protocol;
|
||||||
|
|
||||||
public class HostAndPortUtil {
|
public class HostAndPortUtil {
|
||||||
private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>();
|
private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPort>();
|
||||||
private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>();
|
private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPort>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
||||||
HostAndPort defaulthnp1 = new HostAndPort();
|
HostAndPort defaulthnp1 = new HostAndPort("localhost", Protocol.DEFAULT_PORT);
|
||||||
defaulthnp1.host = "localhost";
|
|
||||||
defaulthnp1.port = Protocol.DEFAULT_PORT;
|
|
||||||
redisHostAndPortList.add(defaulthnp1);
|
redisHostAndPortList.add(defaulthnp1);
|
||||||
|
|
||||||
HostAndPort defaulthnp2 = new HostAndPort();
|
HostAndPort defaulthnp2 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 1);
|
||||||
defaulthnp2.host = "localhost";
|
|
||||||
defaulthnp2.port = Protocol.DEFAULT_PORT + 1;
|
|
||||||
redisHostAndPortList.add(defaulthnp2);
|
redisHostAndPortList.add(defaulthnp2);
|
||||||
|
|
||||||
HostAndPort defaulthnp3 = new HostAndPort();
|
HostAndPort defaulthnp3 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 2);
|
||||||
defaulthnp3.host = "localhost";
|
|
||||||
defaulthnp3.port = Protocol.DEFAULT_PORT + 2;
|
|
||||||
redisHostAndPortList.add(defaulthnp3);
|
redisHostAndPortList.add(defaulthnp3);
|
||||||
|
|
||||||
HostAndPort defaulthnp4 = new HostAndPort();
|
HostAndPort defaulthnp4 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 3);
|
||||||
defaulthnp4.host = "localhost";
|
|
||||||
defaulthnp4.port = Protocol.DEFAULT_PORT + 3;
|
|
||||||
redisHostAndPortList.add(defaulthnp4);
|
redisHostAndPortList.add(defaulthnp4);
|
||||||
|
|
||||||
HostAndPort defaulthnp5 = new HostAndPort();
|
HostAndPort defaulthnp5 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4);
|
||||||
defaulthnp5.host = "localhost";
|
|
||||||
defaulthnp5.port = Protocol.DEFAULT_PORT + 4;
|
|
||||||
redisHostAndPortList.add(defaulthnp5);
|
redisHostAndPortList.add(defaulthnp5);
|
||||||
|
|
||||||
HostAndPort defaulthnp6 = new HostAndPort();
|
HostAndPort defaulthnp6 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT);
|
||||||
defaulthnp6.host = "localhost";
|
|
||||||
defaulthnp6.port = Protocol.DEFAULT_SENTINEL_PORT;
|
|
||||||
sentinelHostAndPortList.add(defaulthnp6);
|
sentinelHostAndPortList.add(defaulthnp6);
|
||||||
|
|
||||||
HostAndPort defaulthnp7 = new HostAndPort();
|
HostAndPort defaulthnp7 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1);
|
||||||
defaulthnp7.host = "localhost";
|
|
||||||
defaulthnp7.port = Protocol.DEFAULT_SENTINEL_PORT + 1;
|
|
||||||
sentinelHostAndPortList.add(defaulthnp7);
|
sentinelHostAndPortList.add(defaulthnp7);
|
||||||
|
|
||||||
HostAndPort defaulthnp8 = new HostAndPort();
|
HostAndPort defaulthnp8 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 2);
|
||||||
defaulthnp8.host = "localhost";
|
|
||||||
defaulthnp8.port = Protocol.DEFAULT_SENTINEL_PORT + 2;
|
|
||||||
sentinelHostAndPortList.add(defaulthnp8);
|
sentinelHostAndPortList.add(defaulthnp8);
|
||||||
|
|
||||||
String envRedisHosts = System.getProperty("redis-hosts");
|
String envRedisHosts = System.getProperty("redis-hosts");
|
||||||
@@ -66,24 +51,22 @@ public class HostAndPortUtil {
|
|||||||
|
|
||||||
if (null != hostDefs && 2 <= hostDefs.length) {
|
if (null != hostDefs && 2 <= hostDefs.length) {
|
||||||
|
|
||||||
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPortUtil.HostAndPort>(hostDefs.length);
|
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPort>(hostDefs.length);
|
||||||
|
|
||||||
for (String hostDef : hostDefs) {
|
for (String hostDef : hostDefs) {
|
||||||
|
|
||||||
String[] hostAndPort = hostDef.split(":");
|
String[] hostAndPort = hostDef.split(":");
|
||||||
|
|
||||||
if (null != hostAndPort && 2 == hostAndPort.length) {
|
if (null != hostAndPort && 2 == hostAndPort.length) {
|
||||||
|
String host = hostAndPort[0];
|
||||||
|
int port = Protocol.DEFAULT_PORT;
|
||||||
|
|
||||||
HostAndPort hnp = new HostAndPort();
|
try {
|
||||||
hnp.host = hostAndPort[0];
|
port = Integer.parseInt(hostAndPort[1]);
|
||||||
|
|
||||||
try {
|
|
||||||
hnp.port = Integer.parseInt(hostAndPort[1]);
|
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final NumberFormatException nfe) {
|
||||||
hnp.port = Protocol.DEFAULT_PORT;
|
}
|
||||||
}
|
|
||||||
|
envHostsAndPorts.add(new HostAndPort(host, port));
|
||||||
envHostsAndPorts.add(hnp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,13 +85,4 @@ public class HostAndPortUtil {
|
|||||||
return sentinelHostAndPortList;
|
return sentinelHostAndPortList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class HostAndPort {
|
|
||||||
public String host;
|
|
||||||
public int port;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return host + ":" + port;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,69 +7,70 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
import redis.clients.jedis.JedisPoolConfig;
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
public class JedisPoolTest extends Assert {
|
public class JedisPoolTest extends Assert {
|
||||||
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkConnections() {
|
public void checkConnections() {
|
||||||
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host,
|
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.port, 2000);
|
hnp.getPort(), 2000);
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
assertEquals("bar", jedis.get("foo"));
|
assertEquals("bar", jedis.get("foo"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkConnectionWithDefaultPort() {
|
public void checkConnectionWithDefaultPort() {
|
||||||
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host,
|
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.port);
|
hnp.getPort());
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
assertEquals("bar", jedis.get("foo"));
|
assertEquals("bar", jedis.get("foo"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkJedisIsReusedWhenReturned() {
|
public void checkJedisIsReusedWhenReturned() {
|
||||||
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host,
|
|
||||||
hnp.port);
|
|
||||||
Jedis jedis = pool.getResource();
|
|
||||||
jedis.auth("foobared");
|
|
||||||
jedis.set("foo", "0");
|
|
||||||
pool.returnResource(jedis);
|
|
||||||
|
|
||||||
jedis = pool.getResource();
|
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
jedis.auth("foobared");
|
hnp.getPort());
|
||||||
jedis.incr("foo");
|
Jedis jedis = pool.getResource();
|
||||||
pool.returnResource(jedis);
|
jedis.auth("foobared");
|
||||||
pool.destroy();
|
jedis.set("foo", "0");
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
|
||||||
|
jedis = pool.getResource();
|
||||||
|
jedis.auth("foobared");
|
||||||
|
jedis.incr("foo");
|
||||||
|
pool.returnResource(jedis);
|
||||||
|
pool.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkPoolRepairedWhenJedisIsBroken() {
|
public void checkPoolRepairedWhenJedisIsBroken() {
|
||||||
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host,
|
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.port);
|
hnp.getPort());
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.quit();
|
jedis.quit();
|
||||||
pool.returnBrokenResource(jedis);
|
pool.returnBrokenResource(jedis);
|
||||||
|
|
||||||
jedis = pool.getResource();
|
jedis = pool.getResource();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.incr("foo");
|
jedis.incr("foo");
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = JedisConnectionException.class)
|
@Test(expected = JedisConnectionException.class)
|
||||||
@@ -77,7 +78,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||||
config.setMaxTotal(1);
|
config.setMaxTotal(1);
|
||||||
config.setBlockWhenExhausted(false);
|
config.setBlockWhenExhausted(false);
|
||||||
JedisPool pool = new JedisPool(config, hnp.host, hnp.port);
|
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort());
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.set("foo", "0");
|
jedis.set("foo", "0");
|
||||||
@@ -91,7 +92,7 @@ public class JedisPoolTest extends Assert {
|
|||||||
public void securePool() {
|
public void securePool() {
|
||||||
JedisPoolConfig config = new JedisPoolConfig();
|
JedisPoolConfig config = new JedisPoolConfig();
|
||||||
config.setTestOnBorrow(true);
|
config.setTestOnBorrow(true);
|
||||||
JedisPool pool = new JedisPool(config, hnp.host, hnp.port, 2000,
|
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000,
|
||||||
"foobared");
|
"foobared");
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
@@ -101,16 +102,16 @@ public class JedisPoolTest extends Assert {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nonDefaultDatabase() {
|
public void nonDefaultDatabase() {
|
||||||
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host,
|
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.port, 2000, "foobared");
|
hnp.getPort(), 2000, "foobared");
|
||||||
Jedis jedis0 = pool0.getResource();
|
Jedis jedis0 = pool0.getResource();
|
||||||
jedis0.set("foo", "bar");
|
jedis0.set("foo", "bar");
|
||||||
assertEquals("bar", jedis0.get("foo"));
|
assertEquals("bar", jedis0.get("foo"));
|
||||||
pool0.returnResource(jedis0);
|
pool0.returnResource(jedis0);
|
||||||
pool0.destroy();
|
pool0.destroy();
|
||||||
|
|
||||||
JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.host,
|
JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.port, 2000, "foobared", 1);
|
hnp.getPort(), 2000, "foobared", 1);
|
||||||
Jedis jedis1 = pool1.getResource();
|
Jedis jedis1 = pool1.getResource();
|
||||||
assertNull(jedis1.get("foo"));
|
assertNull(jedis1.get("foo"));
|
||||||
pool1.returnResource(jedis1);
|
pool1.returnResource(jedis1);
|
||||||
@@ -144,8 +145,8 @@ public class JedisPoolTest extends Assert {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void selectDatabaseOnActivation() {
|
public void selectDatabaseOnActivation() {
|
||||||
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.host,
|
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.port, 2000, "foobared");
|
hnp.getPort(), 2000, "foobared");
|
||||||
|
|
||||||
Jedis jedis0 = pool.getResource();
|
Jedis jedis0 = pool.getResource();
|
||||||
assertEquals(0L, jedis0.getDB().longValue());
|
assertEquals(0L, jedis0.getDB().longValue());
|
||||||
@@ -165,8 +166,8 @@ public class JedisPoolTest extends Assert {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void customClientName() {
|
public void customClientName() {
|
||||||
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host,
|
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
hnp.port, 2000, "foobared", 0, "my_shiny_client_name");
|
hnp.getPort(), 2000, "foobared", 0, "my_shiny_client_name");
|
||||||
|
|
||||||
Jedis jedis = pool0.getResource();
|
Jedis jedis = pool0.getResource();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +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 org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
@@ -8,11 +10,13 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import redis.clients.jedis.DebugParams;
|
import redis.clients.jedis.DebugParams;
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisSentinelPool;
|
import redis.clients.jedis.JedisSentinelPool;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
import redis.clients.jedis.tests.utils.JedisSentinelTestUtil;
|
||||||
|
|
||||||
public class JedisSentinelPoolTest extends JedisTestBase {
|
public class JedisSentinelPoolTest extends JedisTestBase {
|
||||||
|
private static final String MASTER_NAME = "mymaster";
|
||||||
|
|
||||||
protected static HostAndPort master = HostAndPortUtil.getRedisServers()
|
protected static HostAndPort master = HostAndPortUtil.getRedisServers()
|
||||||
.get(2);
|
.get(2);
|
||||||
@@ -28,8 +32,8 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
protected static Jedis masterJedis;
|
protected static Jedis masterJedis;
|
||||||
protected static Jedis slaveJedis1;
|
protected static Jedis slaveJedis1;
|
||||||
protected static Jedis slaveJedis2;
|
protected static Jedis slaveJedis2;
|
||||||
|
protected static Jedis sentinelJedis1;
|
||||||
protected static int slaveCount = 0;
|
protected static Jedis sentinelJedis2;
|
||||||
|
|
||||||
protected Set<String> sentinels = new HashSet<String>();
|
protected Set<String> sentinels = new HashSet<String>();
|
||||||
|
|
||||||
@@ -37,32 +41,36 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
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.getHost(), master.getPort());
|
||||||
masterJedis.auth("foobared");
|
masterJedis.auth("foobared");
|
||||||
masterJedis.slaveofNoOne();
|
masterJedis.slaveofNoOne();
|
||||||
|
|
||||||
slaveJedis1 = new Jedis(slave1.host, slave1.port);
|
slaveJedis1 = new Jedis(slave1.getHost(), slave1.getPort());
|
||||||
slaveJedis1.auth("foobared");
|
slaveJedis1.auth("foobared");
|
||||||
slaveJedis1.slaveof(master.host, master.port);
|
slaveJedis1.slaveof(master.getHost(), master.getPort());
|
||||||
slaveCount++;
|
|
||||||
|
|
||||||
slaveJedis2 = new Jedis(slave2.host, slave2.port);
|
slaveJedis2 = new Jedis(slave2.getHost(), slave2.getPort());
|
||||||
slaveJedis2.auth("foobared");
|
slaveJedis2.auth("foobared");
|
||||||
slaveJedis2.slaveof(master.host, master.port);
|
slaveJedis2.slaveof(master.getHost(), master.getPort());
|
||||||
slaveCount++;
|
|
||||||
|
|
||||||
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
|
List<HostAndPort> slaves = new ArrayList<HostAndPort>();
|
||||||
// be established, and let sentinels know about this relationship.
|
slaves.add(slave1);
|
||||||
// We can do this more elegantly.
|
slaves.add(slave2);
|
||||||
Thread.sleep(10000);
|
|
||||||
|
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
|
||||||
public void ensureSafeTwiceFailover() throws InterruptedException {
|
public void ensureSafeTwiceFailover() throws InterruptedException {
|
||||||
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,
|
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels,
|
||||||
new GenericObjectPoolConfig(), 1000, "foobared", 2);
|
new GenericObjectPoolConfig(), 1000, "foobared", 2);
|
||||||
|
|
||||||
// perform failover
|
// perform failover
|
||||||
@@ -77,6 +85,8 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
|
|
||||||
private void doSegFaultMaster(JedisSentinelPool pool)
|
private void doSegFaultMaster(JedisSentinelPool pool)
|
||||||
throws InterruptedException {
|
throws InterruptedException {
|
||||||
|
HostAndPort oldMaster = pool.getCurrentHostMaster();
|
||||||
|
|
||||||
// jedis connection should be master
|
// jedis connection should be master
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
assertEquals("PONG", jedis.ping());
|
assertEquals("PONG", jedis.ping());
|
||||||
@@ -86,14 +96,40 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for the sentinel to promote a master
|
waitForFailover(pool, oldMaster);
|
||||||
// FIXME: we can query the sentinel and sleep
|
|
||||||
// right until the master is promoted
|
|
||||||
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("foobared", jedis.configGet("requirepass").get(1));
|
||||||
assertEquals(2, jedis.getDB().intValue());
|
assertEquals(2, jedis.getDB().intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void waitForFailover(JedisSentinelPool pool, HostAndPort oldMaster)
|
||||||
|
throws InterruptedException {
|
||||||
|
HostAndPort newMaster = JedisSentinelTestUtil.waitForNewPromotedMaster(
|
||||||
|
sentinel1, MASTER_NAME, oldMaster);
|
||||||
|
JedisSentinelTestUtil.waitForNewPromotedMaster(sentinel2, MASTER_NAME,
|
||||||
|
oldMaster);
|
||||||
|
JedisSentinelTestUtil.waitForSentinelsRecognizeEachOthers();
|
||||||
|
waitForJedisSentinelPoolRecognizeNewMaster(pool, newMaster);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitForJedisSentinelPoolRecognizeNewMaster(
|
||||||
|
JedisSentinelPool pool, HostAndPort newMaster)
|
||||||
|
throws InterruptedException {
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
String host = pool.getCurrentHostMaster().getHost();
|
||||||
|
int port = pool.getCurrentHostMaster().getPort();
|
||||||
|
|
||||||
|
if (host.equals(newMaster.getHost()) && port == newMaster.getPort())
|
||||||
|
break;
|
||||||
|
|
||||||
|
System.out
|
||||||
|
.println("JedisSentinelPool's master is not yet changed, sleep...");
|
||||||
|
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package redis.clients.jedis.tests;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import java.util.ArrayList;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -11,25 +8,42 @@ import org.junit.After;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.tests.utils.JedisSentinelTestUtil;
|
||||||
|
|
||||||
public class JedisSentinelTest {
|
public class JedisSentinelTest extends JedisTestBase {
|
||||||
private static final String MASTER_NAME = "mymaster";
|
private static final String MASTER_NAME = "mymaster";
|
||||||
|
|
||||||
|
protected static HostAndPort master = HostAndPortUtil.getRedisServers()
|
||||||
|
.get(0);
|
||||||
|
protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get(
|
||||||
|
1);
|
||||||
|
protected static HostAndPort sentinel = HostAndPortUtil
|
||||||
|
.getSentinelServers().get(0);
|
||||||
|
|
||||||
|
protected static Jedis masterJedis;
|
||||||
|
protected static Jedis slaveJedis;
|
||||||
|
protected static Jedis sentinelJedis;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() throws InterruptedException {
|
public void setup() throws InterruptedException {
|
||||||
Jedis j = new Jedis("localhost", 6380);
|
masterJedis = new Jedis(master.getHost(), master.getPort());
|
||||||
j.auth("foobared");
|
|
||||||
j.configSet("masterauth", "foobared");
|
slaveJedis = new Jedis(slave.getHost(), slave.getPort());
|
||||||
j.slaveof("localhost", 6379);
|
slaveJedis.auth("foobared");
|
||||||
// TODO: The sleep is to give time to the slave to synchronize with the
|
slaveJedis.configSet("masterauth", "foobared");
|
||||||
// master and also let know the sentinels about this new topology. We
|
slaveJedis.slaveof(master.getHost(), master.getPort());
|
||||||
// should find a better way to do this.
|
|
||||||
Thread.sleep(10000);
|
List<HostAndPort> slaves = new ArrayList<HostAndPort>();
|
||||||
|
slaves.add(slave);
|
||||||
|
|
||||||
|
JedisSentinelTestUtil.waitForSentinelRecognizeRedisReplication(
|
||||||
|
sentinel, MASTER_NAME, master, slaves);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void clear() {
|
public void clear() throws InterruptedException {
|
||||||
Jedis j = new Jedis("localhost", 6380);
|
Jedis j = new Jedis("localhost", 6380);
|
||||||
j.auth("foobared");
|
j.auth("foobared");
|
||||||
j.slaveofNoOne();
|
j.slaveofNoOne();
|
||||||
@@ -52,18 +66,8 @@ public class JedisSentinelTest {
|
|||||||
assertTrue(slaves.size() > 0);
|
assertTrue(slaves.size() > 0);
|
||||||
assertEquals("6379", slaves.get(0).get("master-port"));
|
assertEquals("6379", slaves.get(0).get("master-port"));
|
||||||
|
|
||||||
List<? extends Object> isMasterDownByAddr = j
|
|
||||||
.sentinelIsMasterDownByAddr("127.0.0.1", 6379);
|
|
||||||
assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
|
|
||||||
assertFalse("?".equals(isMasterDownByAddr.get(1)));
|
|
||||||
|
|
||||||
isMasterDownByAddr = j.sentinelIsMasterDownByAddr("127.0.0.1", 1);
|
|
||||||
assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
|
|
||||||
assertTrue("?".equals(isMasterDownByAddr.get(1)));
|
|
||||||
|
|
||||||
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
|
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
|
||||||
assertEquals(Long.valueOf(1), j.sentinelReset(masterName));
|
assertEquals(Long.valueOf(1), j.sentinelReset(masterName));
|
||||||
assertEquals(Long.valueOf(0), j.sentinelReset("woof" + masterName));
|
assertEquals(Long.valueOf(0), j.sentinelReset("woof" + masterName));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,24 @@
|
|||||||
package redis.clients.jedis.tests;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import redis.clients.jedis.*;
|
|
||||||
import redis.clients.jedis.exceptions.JedisDataException;
|
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import redis.clients.jedis.HostAndPort;
|
||||||
import java.util.*;
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.Pipeline;
|
||||||
|
import redis.clients.jedis.PipelineBlock;
|
||||||
|
import redis.clients.jedis.Response;
|
||||||
|
import redis.clients.jedis.Tuple;
|
||||||
|
import redis.clients.jedis.exceptions.JedisDataException;
|
||||||
|
|
||||||
public class PipeliningTest extends Assert {
|
public class PipeliningTest extends Assert {
|
||||||
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||||
@@ -17,7 +27,7 @@ public class PipeliningTest extends Assert {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
jedis = new Jedis(hnp.host, hnp.port, 500);
|
jedis = new Jedis(hnp.getHost(), hnp.getPort(), 500);
|
||||||
jedis.connect();
|
jedis.connect();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.flushAll();
|
jedis.flushAll();
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import java.util.UUID;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisShardInfo;
|
import redis.clients.jedis.JedisShardInfo;
|
||||||
import redis.clients.jedis.Response;
|
import redis.clients.jedis.Response;
|
||||||
@@ -24,32 +25,36 @@ import redis.clients.jedis.Tuple;
|
|||||||
import redis.clients.jedis.exceptions.JedisDataException;
|
import redis.clients.jedis.exceptions.JedisDataException;
|
||||||
|
|
||||||
public class ShardedJedisPipelineTest {
|
public class ShardedJedisPipelineTest {
|
||||||
private static HostAndPortUtil.HostAndPort redis1 = HostAndPortUtil
|
|
||||||
.getRedisServers().get(0);
|
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers()
|
||||||
private static HostAndPortUtil.HostAndPort redis2 = HostAndPortUtil
|
.get(0);
|
||||||
.getRedisServers().get(1);
|
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers()
|
||||||
|
.get(1);
|
||||||
|
|
||||||
private ShardedJedis jedis;
|
private ShardedJedis jedis;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
Jedis jedis = new Jedis(redis1.host, redis1.port);
|
Jedis jedis = new Jedis(redis1.getHost(), redis1.getPort());
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.flushAll();
|
jedis.flushAll();
|
||||||
jedis.disconnect();
|
jedis.disconnect();
|
||||||
jedis = new Jedis(redis2.host, redis2.port);
|
jedis = new Jedis(redis2.getHost(), redis2.getPort());
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.flushAll();
|
jedis.flushAll();
|
||||||
jedis.disconnect();
|
jedis.disconnect();
|
||||||
|
|
||||||
JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.host, redis1.port);
|
JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.getHost(),
|
||||||
JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.host, redis2.port);
|
redis1.getPort());
|
||||||
|
JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.getHost(),
|
||||||
|
redis2.getPort());
|
||||||
shardInfo1.setPassword("foobared");
|
shardInfo1.setPassword("foobared");
|
||||||
shardInfo2.setPassword("foobared");
|
shardInfo2.setPassword("foobared");
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
shards.add(shardInfo1);
|
shards.add(shardInfo1);
|
||||||
shards.add(shardInfo2);
|
shards.add(shardInfo2);
|
||||||
this.jedis = new ShardedJedis(shards);
|
this.jedis = new ShardedJedis(shards);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisShardInfo;
|
import redis.clients.jedis.JedisShardInfo;
|
||||||
import redis.clients.jedis.ShardedJedis;
|
import redis.clients.jedis.ShardedJedis;
|
||||||
import redis.clients.jedis.ShardedJedisPool;
|
import redis.clients.jedis.ShardedJedisPool;
|
||||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
public class ShardedJedisPoolTest extends Assert {
|
public class ShardedJedisPoolTest extends Assert {
|
||||||
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers()
|
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers()
|
||||||
@@ -28,8 +28,8 @@ public class ShardedJedisPoolTest extends Assert {
|
|||||||
@Before
|
@Before
|
||||||
public void startUp() {
|
public void startUp() {
|
||||||
shards = new ArrayList<JedisShardInfo>();
|
shards = new ArrayList<JedisShardInfo>();
|
||||||
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
|
||||||
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
|
||||||
shards.get(0).setPassword("foobared");
|
shards.get(0).setPassword("foobared");
|
||||||
shards.get(1).setPassword("foobared");
|
shards.get(1).setPassword("foobared");
|
||||||
Jedis j = new Jedis(shards.get(0));
|
Jedis j = new Jedis(shards.get(0));
|
||||||
@@ -40,6 +40,7 @@ public class ShardedJedisPoolTest extends Assert {
|
|||||||
j.connect();
|
j.connect();
|
||||||
j.flushAll();
|
j.flushAll();
|
||||||
j.disconnect();
|
j.disconnect();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -97,7 +98,7 @@ public class ShardedJedisPoolTest extends Assert {
|
|||||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||||
config.setMaxTotal(1);
|
config.setMaxTotal(1);
|
||||||
config.setBlockWhenExhausted(false);
|
config.setBlockWhenExhausted(false);
|
||||||
|
|
||||||
ShardedJedisPool pool = new ShardedJedisPool(config, shards);
|
ShardedJedisPool pool = new ShardedJedisPool(config, shards);
|
||||||
|
|
||||||
ShardedJedis jedis = pool.getResource();
|
ShardedJedis jedis = pool.getResource();
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import java.util.List;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisShardInfo;
|
import redis.clients.jedis.JedisShardInfo;
|
||||||
import redis.clients.jedis.Protocol;
|
import redis.clients.jedis.Protocol;
|
||||||
import redis.clients.jedis.ShardedJedis;
|
import redis.clients.jedis.ShardedJedis;
|
||||||
import redis.clients.jedis.ShardedJedisPipeline;
|
import redis.clients.jedis.ShardedJedisPipeline;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
import redis.clients.util.Hashing;
|
import redis.clients.util.Hashing;
|
||||||
import redis.clients.util.SafeEncoder;
|
import redis.clients.util.SafeEncoder;
|
||||||
import redis.clients.util.Sharded;
|
import redis.clients.util.Sharded;
|
||||||
@@ -41,8 +41,8 @@ public class ShardedJedisTest extends Assert {
|
|||||||
@Test
|
@Test
|
||||||
public void checkSharding() {
|
public void checkSharding() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
|
||||||
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
List<String> keys = getKeysDifferentShard(jedis);
|
List<String> keys = getKeysDifferentShard(jedis);
|
||||||
JedisShardInfo s1 = jedis.getShardInfo(keys.get(0));
|
JedisShardInfo s1 = jedis.getShardInfo(keys.get(0));
|
||||||
@@ -53,10 +53,10 @@ public class ShardedJedisTest extends Assert {
|
|||||||
@Test
|
@Test
|
||||||
public void trySharding() {
|
public void trySharding() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
JedisShardInfo si = new JedisShardInfo(redis1.host, redis1.port);
|
JedisShardInfo si = new JedisShardInfo(redis1.getHost(), redis1.getPort());
|
||||||
si.setPassword("foobared");
|
si.setPassword("foobared");
|
||||||
shards.add(si);
|
shards.add(si);
|
||||||
si = new JedisShardInfo(redis2.host, redis2.port);
|
si = new JedisShardInfo(redis2.getHost(), redis2.getPort());
|
||||||
si.setPassword("foobared");
|
si.setPassword("foobared");
|
||||||
shards.add(si);
|
shards.add(si);
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
@@ -80,10 +80,10 @@ public class ShardedJedisTest extends Assert {
|
|||||||
@Test
|
@Test
|
||||||
public void tryShardingWithMurmure() {
|
public void tryShardingWithMurmure() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
JedisShardInfo si = new JedisShardInfo(redis1.host, redis1.port);
|
JedisShardInfo si = new JedisShardInfo(redis1.getHost(), redis1.getPort());
|
||||||
si.setPassword("foobared");
|
si.setPassword("foobared");
|
||||||
shards.add(si);
|
shards.add(si);
|
||||||
si = new JedisShardInfo(redis2.host, redis2.port);
|
si = new JedisShardInfo(redis2.getHost(), redis2.getPort());
|
||||||
si.setPassword("foobared");
|
si.setPassword("foobared");
|
||||||
shards.add(si);
|
shards.add(si);
|
||||||
ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMUR_HASH);
|
ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMUR_HASH);
|
||||||
@@ -107,8 +107,8 @@ public class ShardedJedisTest extends Assert {
|
|||||||
@Test
|
@Test
|
||||||
public void checkKeyTags() {
|
public void checkKeyTags() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
|
||||||
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
|
||||||
ShardedJedis jedis = new ShardedJedis(shards,
|
ShardedJedis jedis = new ShardedJedis(shards,
|
||||||
ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
|
ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
|
||||||
|
|
||||||
@@ -143,8 +143,8 @@ public class ShardedJedisTest extends Assert {
|
|||||||
@Test
|
@Test
|
||||||
public void shardedPipeline() {
|
public void shardedPipeline() {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
shards.add(new JedisShardInfo(redis1.host, redis1.port));
|
shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
|
||||||
shards.add(new JedisShardInfo(redis2.host, redis2.port));
|
shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
|
||||||
shards.get(0).setPassword("foobared");
|
shards.get(0).setPassword("foobared");
|
||||||
shards.get(1).setPassword("foobared");
|
shards.get(1).setPassword("foobared");
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import java.io.IOException;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
public class GetSetBenchmark {
|
public class GetSetBenchmark {
|
||||||
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||||
@@ -14,7 +14,7 @@ public class GetSetBenchmark {
|
|||||||
|
|
||||||
public static void main(String[] args) throws UnknownHostException,
|
public static void main(String[] args) throws UnknownHostException,
|
||||||
IOException {
|
IOException {
|
||||||
Jedis jedis = new Jedis(hnp.host, hnp.port);
|
Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
|
||||||
jedis.connect();
|
jedis.connect();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.flushAll();
|
jedis.flushAll();
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import java.util.Calendar;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisShardInfo;
|
import redis.clients.jedis.JedisShardInfo;
|
||||||
import redis.clients.jedis.ShardedJedis;
|
import redis.clients.jedis.ShardedJedis;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
public class HashingBenchmark {
|
public class HashingBenchmark {
|
||||||
private static HostAndPort hnp1 = HostAndPortUtil.getRedisServers().get(0);
|
private static HostAndPort hnp1 = HostAndPortUtil.getRedisServers().get(0);
|
||||||
@@ -21,10 +21,10 @@ public class HashingBenchmark {
|
|||||||
public static void main(String[] args) throws UnknownHostException,
|
public static void main(String[] args) throws UnknownHostException,
|
||||||
IOException {
|
IOException {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
JedisShardInfo shard = new JedisShardInfo(hnp1.host, hnp1.port);
|
JedisShardInfo shard = new JedisShardInfo(hnp1.getHost(), hnp1.getPort());
|
||||||
shard.setPassword("foobared");
|
shard.setPassword("foobared");
|
||||||
shards.add(shard);
|
shards.add(shard);
|
||||||
shard = new JedisShardInfo(hnp2.host, hnp2.port);
|
shard = new JedisShardInfo(hnp2.getHost(), hnp2.getPort());
|
||||||
shard.setPassword("foobared");
|
shard.setPassword("foobared");
|
||||||
shards.add(shard);
|
shards.add(shard);
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import java.io.IOException;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.Pipeline;
|
import redis.clients.jedis.Pipeline;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
public class PipelinedGetSetBenchmark {
|
public class PipelinedGetSetBenchmark {
|
||||||
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||||
@@ -15,7 +15,7 @@ public class PipelinedGetSetBenchmark {
|
|||||||
|
|
||||||
public static void main(String[] args) throws UnknownHostException,
|
public static void main(String[] args) throws UnknownHostException,
|
||||||
IOException {
|
IOException {
|
||||||
Jedis jedis = new Jedis(hnp.host, hnp.port);
|
Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
|
||||||
jedis.connect();
|
jedis.connect();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.flushAll();
|
jedis.flushAll();
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
public class PoolBenchmark {
|
public class PoolBenchmark {
|
||||||
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||||
private static final int TOTAL_OPERATIONS = 100000;
|
private static final int TOTAL_OPERATIONS = 100000;
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Jedis j = new Jedis(hnp.host, hnp.port);
|
Jedis j = new Jedis(hnp.getHost(), hnp.getPort());
|
||||||
j.connect();
|
j.connect();
|
||||||
j.auth("foobared");
|
j.auth("foobared");
|
||||||
j.flushAll();
|
j.flushAll();
|
||||||
@@ -31,7 +31,7 @@ public class PoolBenchmark {
|
|||||||
|
|
||||||
private static void withPool() throws Exception {
|
private static void withPool() throws Exception {
|
||||||
final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(),
|
final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(),
|
||||||
hnp.host, hnp.port, 2000, "foobared");
|
hnp.getHost(), hnp.getPort(), 2000, "foobared");
|
||||||
List<Thread> tds = new ArrayList<Thread>();
|
List<Thread> tds = new ArrayList<Thread>();
|
||||||
|
|
||||||
final AtomicInteger ind = new AtomicInteger();
|
final AtomicInteger ind = new AtomicInteger();
|
||||||
@@ -59,5 +59,6 @@ public class PoolBenchmark {
|
|||||||
t.join();
|
t.join();
|
||||||
|
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,8 @@ package redis.clients.jedis.tests.commands;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import redis.clients.jedis.BinaryJedis;
|
import redis.clients.jedis.BinaryJedis;
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
|
|
||||||
public class ConnectionHandlingCommandsTest extends JedisCommandTestBase {
|
public class ConnectionHandlingCommandsTest extends JedisCommandTestBase {
|
||||||
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||||
@@ -16,7 +16,7 @@ public class ConnectionHandlingCommandsTest extends JedisCommandTestBase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void binary_quit() {
|
public void binary_quit() {
|
||||||
BinaryJedis bj = new BinaryJedis(hnp.host);
|
BinaryJedis bj = new BinaryJedis(hnp.getHost());
|
||||||
assertEquals("OK", bj.quit());
|
assertEquals("OK", bj.quit());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,19 @@
|
|||||||
package redis.clients.jedis.tests.commands;
|
package redis.clients.jedis.tests.commands;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.ComparisonFailure;
|
|
||||||
import redis.clients.jedis.Jedis;
|
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
|
||||||
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
|
|
||||||
import redis.clients.jedis.tests.JedisTestBase;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.ComparisonFailure;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||||
|
import redis.clients.jedis.tests.JedisTestBase;
|
||||||
|
|
||||||
public abstract class JedisCommandTestBase extends JedisTestBase {
|
public abstract class JedisCommandTestBase extends JedisTestBase {
|
||||||
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ public abstract class JedisCommandTestBase extends JedisTestBase {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
jedis = new Jedis(hnp.host, hnp.port, 500);
|
jedis = new Jedis(hnp.getHost(), hnp.getPort(), 500);
|
||||||
jedis.connect();
|
jedis.connect();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.configSet("timeout", "300");
|
jedis.configSet("timeout", "300");
|
||||||
@@ -37,7 +38,7 @@ public abstract class JedisCommandTestBase extends JedisTestBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Jedis createJedis() {
|
protected Jedis createJedis() {
|
||||||
Jedis j = new Jedis(hnp.host, hnp.port);
|
Jedis j = new Jedis(hnp.getHost(), hnp.getPort());
|
||||||
j.connect();
|
j.connect();
|
||||||
j.auth("foobared");
|
j.auth("foobared");
|
||||||
j.flushAll();
|
j.flushAll();
|
||||||
|
|||||||
@@ -475,7 +475,7 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
|||||||
@Test @Ignore
|
@Test @Ignore
|
||||||
public void subscribeWithoutConnecting() {
|
public void subscribeWithoutConnecting() {
|
||||||
try {
|
try {
|
||||||
Jedis jedis = new Jedis(hnp.host, hnp.port);
|
Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
|
||||||
jedis.subscribe(new JedisPubSub() {
|
jedis.subscribe(new JedisPubSub() {
|
||||||
public void onMessage(String channel, String message) {
|
public void onMessage(String channel, String message) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
|
|||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
nj = new Jedis(hnp.host, hnp.port, 500);
|
nj = new Jedis(hnp.getHost(), hnp.getPort(), 500);
|
||||||
nj.connect();
|
nj.connect();
|
||||||
nj.auth("foobared");
|
nj.auth("foobared");
|
||||||
nj.flushAll();
|
nj.flushAll();
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package redis.clients.jedis.tests.utils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import redis.clients.jedis.HostAndPort;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
|
||||||
|
public class JedisSentinelTestUtil {
|
||||||
|
|
||||||
|
public static void waitForSentinelRecognizeRedisReplication(
|
||||||
|
HostAndPort sentinel, String masterName, HostAndPort master,
|
||||||
|
List<HostAndPort> slaves) throws InterruptedException {
|
||||||
|
Jedis sentinelJedis = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
|
while (true) {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
if (!isMasterRecognized(sentinelJedis, masterName, master)) {
|
||||||
|
System.out.println("Master not recognized by Sentinel "
|
||||||
|
+ sentinel.getHost() + ":" + sentinel.getPort()
|
||||||
|
+ ", sleep...");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isSlavesRecognized(sentinelJedis, masterName, slaves)) {
|
||||||
|
System.out.println("Slaves not recognized by Sentinel "
|
||||||
|
+ sentinel.getHost() + ":" + sentinel.getPort()
|
||||||
|
+ ", sleep...");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// all recognized
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HostAndPort waitForNewPromotedMaster(HostAndPort sentinel,
|
||||||
|
String masterName, HostAndPort oldMaster)
|
||||||
|
throws InterruptedException {
|
||||||
|
Jedis sentinelJedis = new Jedis(sentinel.getHost(), sentinel.getPort());
|
||||||
|
|
||||||
|
HostAndPort newMaster = null;
|
||||||
|
while (true) {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
List<String> sentinelMasterInfos = sentinelJedis
|
||||||
|
.sentinelGetMasterAddrByName(masterName);
|
||||||
|
if (sentinelMasterInfos == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
newMaster = new HostAndPort(sentinelMasterInfos.get(0),
|
||||||
|
Integer.parseInt(sentinelMasterInfos.get(1)));
|
||||||
|
|
||||||
|
if (!newMaster.equals(oldMaster))
|
||||||
|
break;
|
||||||
|
|
||||||
|
System.out
|
||||||
|
.println("Sentinel's master is not yet changed, sleep...");
|
||||||
|
}
|
||||||
|
|
||||||
|
return newMaster;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void waitForSentinelsRecognizeEachOthers()
|
||||||
|
throws InterruptedException {
|
||||||
|
// During failover, master has been changed
|
||||||
|
// It means that sentinels need to recognize other sentinels from new
|
||||||
|
// master's hello channel
|
||||||
|
// Without recognizing, Sentinels cannot run failover
|
||||||
|
|
||||||
|
// Sentinels need to take some time to recognize each other...
|
||||||
|
// http://redis.io/topics/sentinel
|
||||||
|
// Sentinel Rule #8: Every Sentinel publishes a message to every
|
||||||
|
// monitored master
|
||||||
|
// Pub/Sub channel __sentinel__:hello, every five seconds, blabla...
|
||||||
|
|
||||||
|
// FIXME There're no command for sentinel to list recognized sentinels
|
||||||
|
// so sleep wisely (channel's hello message interval + margin)
|
||||||
|
Thread.sleep(5000 + 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isMasterRecognized(Jedis sentinelJedis,
|
||||||
|
String masterName, HostAndPort master) {
|
||||||
|
List<String> sentinelMasterInfos = sentinelJedis
|
||||||
|
.sentinelGetMasterAddrByName(masterName);
|
||||||
|
if (sentinelMasterInfos == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
HostAndPort sentinelMaster = new HostAndPort(
|
||||||
|
sentinelMasterInfos.get(0),
|
||||||
|
Integer.parseInt(sentinelMasterInfos.get(1)));
|
||||||
|
|
||||||
|
return sentinelMaster.equals(master);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isSlavesRecognized(Jedis sentinelJedis,
|
||||||
|
String masterName, List<HostAndPort> slaves) {
|
||||||
|
List<Map<String, String>> slavesMap = sentinelJedis
|
||||||
|
.sentinelSlaves(masterName);
|
||||||
|
|
||||||
|
if (slavesMap.size() != slaves.size())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int slavesRecognized = 0;
|
||||||
|
|
||||||
|
for (HostAndPort slave : slaves) {
|
||||||
|
if (isSlaveFoundInSlavesMap(slavesMap, slave))
|
||||||
|
slavesRecognized++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return slavesRecognized == slaves.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isSlaveFoundInSlavesMap(
|
||||||
|
List<Map<String, String>> slavesMap, HostAndPort slave) {
|
||||||
|
for (Map<String, String> slaveMap : slavesMap) {
|
||||||
|
HostAndPort sentinelSlave = new HostAndPort(slaveMap.get("ip"),
|
||||||
|
Integer.parseInt(slaveMap.get("port")));
|
||||||
|
|
||||||
|
if (sentinelSlave.equals(slave))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user