From e7e2bfaedf6328151f7b516fd9b9ae68d263ebef Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Mon, 2 Dec 2013 09:53:40 -0500 Subject: [PATCH 01/23] Launch 3 nodes on ports 7379 to 7381 with cluster enabled. Add command CLUSTER NODES --- Makefile | 47 +++++ pom.xml | 1 + .../redis/clients/jedis/BinaryClient.java | 5 + src/main/java/redis/clients/jedis/Client.java | 8 + src/main/java/redis/clients/jedis/Jedis.java | 6 + .../java/redis/clients/jedis/Protocol.java | 4 +- .../clients/jedis/tests/HostAndPortUtil.java | 161 ++++++++++-------- .../tests/commands/ClusterCommandsTest.java | 41 +++++ 8 files changed, 203 insertions(+), 70 deletions(-) create mode 100644 src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java diff --git a/Makefile b/Makefile index 21eaa2b..e0189f2 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,7 @@ save "" appendonly no endef +# SENTINELS define REDIS_SENTINEL1 port 26379 daemonize yes @@ -97,6 +98,40 @@ pidfile /tmp/sentinel3.pid logfile /tmp/sentinel3.log endef +# CLUSTER REDIS NODES +define REDIS_CLUSTER_NODE1_CONF +daemonize yes +port 7379 +pidfile /tmp/redis_cluster_node1.pid +logfile /tmp/redis_cluster_node1.log +save "" +appendonly no +cluster-enabled yes +cluster-config-file /tmp/redis_cluster_node1.conf +endef + +define REDIS_CLUSTER_NODE2_CONF +daemonize yes +port 7380 +pidfile /tmp/redis_cluster_node2.pid +logfile /tmp/redis_cluster_node2.log +save "" +appendonly no +cluster-enabled yes +cluster-config-file /tmp/redis_cluster_node2.conf +endef + +define REDIS_CLUSTER_NODE3_CONF +daemonize yes +port 7381 +pidfile /tmp/redis_cluster_node3.pid +logfile /tmp/redis_cluster_node3.log +save "" +appendonly no +cluster-enabled yes +cluster-config-file /tmp/redis_cluster_node3.conf +endef + export REDIS1_CONF export REDIS2_CONF export REDIS3_CONF @@ -106,6 +141,9 @@ export REDIS6_CONF export REDIS_SENTINEL1 export REDIS_SENTINEL2 export REDIS_SENTINEL3 +export REDIS_CLUSTER_NODE1_CONF +export REDIS_CLUSTER_NODE2_CONF +export REDIS_CLUSTER_NODE3_CONF start: echo "$$REDIS1_CONF" | redis-server - @@ -119,6 +157,9 @@ start: echo "$$REDIS_SENTINEL2" > /tmp/sentinel2.conf && redis-server /tmp/sentinel2.conf --sentinel @sleep 0.5 echo "$$REDIS_SENTINEL3" > /tmp/sentinel3.conf && redis-server /tmp/sentinel3.conf --sentinel + echo "$$REDIS_CLUSTER_NODE1_CONF" | redis-server - + echo "$$REDIS_CLUSTER_NODE2_CONF" | redis-server - + echo "$$REDIS_CLUSTER_NODE3_CONF" | redis-server - stop: kill `cat /tmp/redis1.pid` @@ -131,6 +172,12 @@ stop: kill `cat /tmp/sentinel1.pid` kill `cat /tmp/sentinel2.pid` kill `cat /tmp/sentinel3.pid` + kill `cat /tmp/redis_cluster_node1.pid` || true + kill `cat /tmp/redis_cluster_node2.pid` || true + kill `cat /tmp/redis_cluster_node3.pid` || true + rm -f /tmp/redis_cluster_node1.conf + rm -f /tmp/redis_cluster_node2.conf + rm -f /tmp/redis_cluster_node3.conf test: make start diff --git a/pom.xml b/pom.xml index 9409ec9..f6575eb 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384 localhost:26379,localhost:26380,localhost:26381 + localhost:7379,localhost:7380,localhost:7381 github diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index acb120b..1b9db49 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -1100,4 +1100,9 @@ public class BinaryClient extends Connection { public void hincrByFloat(final byte[] key, final byte[] field, double increment) { sendCommand(HINCRBYFLOAT, key, field, toByteArray(increment)); } + + public void cluster(final byte[]... args) { + sendCommand(CLUSTER, args); + } + } diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 9c97e38..b726821 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -807,4 +807,12 @@ public class Client extends BinaryClient implements Commands { public void hincrByFloat(final String key, final String field, double increment) { hincrByFloat(SafeEncoder.encode(key), SafeEncoder.encode(field), increment); } + + public void cluster(final String... args) { + final byte[][] arg = new byte[args.length][]; + for (int i = 0; i < arg.length; i++) { + arg[i] = SafeEncoder.encode(args[i]); + } + cluster(arg); + } } diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 23cfd3d..d2daca5 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3076,4 +3076,10 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand String relpy = client.getBulkReply(); return (relpy != null ? new Double(relpy) : null); } + + public String clusterNodes() { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_NODES); + return client.getBulkReply(); + } } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index f85c324..e4a9126 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -30,6 +30,8 @@ public final class Protocol { public static final String SENTINEL_RESET = "reset"; public static final String SENTINEL_SLAVES = "slaves"; + public static final String CLUSTER_NODES = "nodes"; + private Protocol() { // this prevent the class from instantiation } @@ -155,7 +157,7 @@ public final class Protocol { public static enum Command { PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP, SENTINEL, - DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT; + DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT, CLUSTER; public final byte[] raw; diff --git a/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java b/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java index be64c53..c7be599 100644 --- a/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java +++ b/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java @@ -9,83 +9,106 @@ import redis.clients.jedis.Protocol; public class HostAndPortUtil { private static List redisHostAndPortList = new ArrayList(); private static List sentinelHostAndPortList = new ArrayList(); + private static List clusterHostAndPortList = new ArrayList(); static { - - HostAndPort defaulthnp1 = new HostAndPort("localhost", Protocol.DEFAULT_PORT); - redisHostAndPortList.add(defaulthnp1); - HostAndPort defaulthnp2 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 1); - redisHostAndPortList.add(defaulthnp2); - - HostAndPort defaulthnp3 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 2); - redisHostAndPortList.add(defaulthnp3); - - HostAndPort defaulthnp4 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 3); - redisHostAndPortList.add(defaulthnp4); - - HostAndPort defaulthnp5 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4); - redisHostAndPortList.add(defaulthnp5); - - HostAndPort defaulthnp6 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 5); - redisHostAndPortList.add(defaulthnp6); - - HostAndPort defaulthnp7 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT); - sentinelHostAndPortList.add(defaulthnp7); - - HostAndPort defaulthnp8 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1); - sentinelHostAndPortList.add(defaulthnp8); - - HostAndPort defaulthnp9 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 2); - sentinelHostAndPortList.add(defaulthnp9); + HostAndPort defaulthnp1 = new HostAndPort("localhost", + Protocol.DEFAULT_PORT); + redisHostAndPortList.add(defaulthnp1); - String envRedisHosts = System.getProperty("redis-hosts"); - String envSentinelHosts = System.getProperty("sentinel-hosts"); - - redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList); - sentinelHostAndPortList = parseHosts(envSentinelHosts, sentinelHostAndPortList); + HostAndPort defaulthnp2 = new HostAndPort("localhost", + Protocol.DEFAULT_PORT + 1); + redisHostAndPortList.add(defaulthnp2); + + HostAndPort defaulthnp3 = new HostAndPort("localhost", + Protocol.DEFAULT_PORT + 2); + redisHostAndPortList.add(defaulthnp3); + + HostAndPort defaulthnp4 = new HostAndPort("localhost", + Protocol.DEFAULT_PORT + 3); + redisHostAndPortList.add(defaulthnp4); + + HostAndPort defaulthnp5 = new HostAndPort("localhost", + Protocol.DEFAULT_PORT + 4); + redisHostAndPortList.add(defaulthnp5); + + HostAndPort defaulthnp6 = new HostAndPort("localhost", + Protocol.DEFAULT_PORT + 5); + redisHostAndPortList.add(defaulthnp6); + + HostAndPort defaulthnp7 = new HostAndPort("localhost", + Protocol.DEFAULT_SENTINEL_PORT); + sentinelHostAndPortList.add(defaulthnp7); + + HostAndPort defaulthnp8 = new HostAndPort("localhost", + Protocol.DEFAULT_SENTINEL_PORT + 1); + sentinelHostAndPortList.add(defaulthnp8); + + HostAndPort defaulthnp9 = new HostAndPort("localhost", + Protocol.DEFAULT_SENTINEL_PORT + 2); + sentinelHostAndPortList.add(defaulthnp9); + + clusterHostAndPortList.add(new HostAndPort("localhost", 7379)); + clusterHostAndPortList.add(new HostAndPort("localhost", 7380)); + clusterHostAndPortList.add(new HostAndPort("localhost", 7381)); + + String envRedisHosts = System.getProperty("redis-hosts"); + String envSentinelHosts = System.getProperty("sentinel-hosts"); + String envClusterHosts = System.getProperty("cluster-hosts"); + + redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList); + sentinelHostAndPortList = parseHosts(envSentinelHosts, + sentinelHostAndPortList); + clusterHostAndPortList = parseHosts(envClusterHosts, + clusterHostAndPortList); } - public static List parseHosts(String envHosts, List existingHostsAndPorts) { - - if (null != envHosts && 0 < envHosts.length()) { - - String[] hostDefs = envHosts.split(","); - - if (null != hostDefs && 2 <= hostDefs.length) { - - List envHostsAndPorts = new ArrayList(hostDefs.length); - - for (String hostDef : hostDefs) { - - String[] hostAndPort = hostDef.split(":"); - - if (null != hostAndPort && 2 == hostAndPort.length) { - String host = hostAndPort[0]; - int port = Protocol.DEFAULT_PORT; - - try { - port = Integer.parseInt(hostAndPort[1]); - } catch (final NumberFormatException nfe) { - } - - envHostsAndPorts.add(new HostAndPort(host, port)); - } - } - - return envHostsAndPorts; - } - } - - return existingHostsAndPorts; + public static List parseHosts(String envHosts, + List existingHostsAndPorts) { + + if (null != envHosts && 0 < envHosts.length()) { + + String[] hostDefs = envHosts.split(","); + + if (null != hostDefs && 2 <= hostDefs.length) { + + List envHostsAndPorts = new ArrayList( + hostDefs.length); + + for (String hostDef : hostDefs) { + + String[] hostAndPort = hostDef.split(":"); + + if (null != hostAndPort && 2 == hostAndPort.length) { + String host = hostAndPort[0]; + int port = Protocol.DEFAULT_PORT; + + try { + port = Integer.parseInt(hostAndPort[1]); + } catch (final NumberFormatException nfe) { + } + + envHostsAndPorts.add(new HostAndPort(host, port)); + } + } + + return envHostsAndPorts; + } + } + + return existingHostsAndPorts; } - + public static List getRedisServers() { - return redisHostAndPortList; - } - - public static List getSentinelServers() { - return sentinelHostAndPortList; + return redisHostAndPortList; } + public static List getSentinelServers() { + return sentinelHostAndPortList; + } + + public static List getClusterServers() { + return clusterHostAndPortList; + } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java new file mode 100644 index 0000000..1599b02 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -0,0 +1,41 @@ +package redis.clients.jedis.tests.commands; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.tests.HostAndPortUtil; +import redis.clients.jedis.tests.JedisTestBase; + +public class ClusterCommandsTest extends JedisTestBase { + private Jedis node1; + private Jedis node2; + + @Before + public void setUp() throws Exception { + HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0); + HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1); + + node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); + node1.connect(); + node1.flushAll(); + + node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); + node2.connect(); + node2.flushAll(); + } + + @After + public void tearDown() { + node1.disconnect(); + node2.disconnect(); + } + + @Test + public void clusterNodes() { + String nodes = node1.clusterNodes(); + assertEquals(1, nodes.split("\n").length); + } +} \ No newline at end of file From 403f2b292cb9e3f4f1cf71c2f0e46d5d014d1dd2 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Mon, 2 Dec 2013 10:01:19 -0500 Subject: [PATCH 02/23] Add CLUSTER MEET command --- src/main/java/redis/clients/jedis/Client.java | 4 ++-- src/main/java/redis/clients/jedis/Jedis.java | 6 ++++++ src/main/java/redis/clients/jedis/Protocol.java | 1 + .../jedis/tests/commands/ClusterCommandsTest.java | 13 ++++++++++--- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index b726821..c4e131e 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -808,10 +808,10 @@ public class Client extends BinaryClient implements Commands { hincrByFloat(SafeEncoder.encode(key), SafeEncoder.encode(field), increment); } - public void cluster(final String... args) { + public void cluster(final Object... args) { final byte[][] arg = new byte[args.length][]; for (int i = 0; i < arg.length; i++) { - arg[i] = SafeEncoder.encode(args[i]); + arg[i] = SafeEncoder.encode(args[i].toString()); } cluster(arg); } diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index d2daca5..ef952a7 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3082,4 +3082,10 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand client.cluster(Protocol.CLUSTER_NODES); return client.getBulkReply(); } + + public String clusterMeet(final String ip, final int port) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_MEET, ip, port); + return client.getStatusCodeReply(); + } } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index e4a9126..22479a5 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -31,6 +31,7 @@ public final class Protocol { public static final String SENTINEL_SLAVES = "slaves"; public static final String CLUSTER_NODES = "nodes"; + public static final String CLUSTER_MEET = "meet"; private Protocol() { // this prevent the class from instantiation diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java index 1599b02..0fe04f1 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -13,10 +13,11 @@ public class ClusterCommandsTest extends JedisTestBase { private Jedis node1; private Jedis node2; + private HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0); + private HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1); + @Before public void setUp() throws Exception { - HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0); - HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1); node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); node1.connect(); @@ -36,6 +37,12 @@ public class ClusterCommandsTest extends JedisTestBase { @Test public void clusterNodes() { String nodes = node1.clusterNodes(); - assertEquals(1, nodes.split("\n").length); + assertTrue(nodes.split("\n").length > 0); + } + + @Test + public void clusterMeet() { + String status = node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); + assertEquals("OK", status); } } \ No newline at end of file From dde278152f391238fb12bacbdd5ed609a06e717f Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Mon, 2 Dec 2013 11:36:55 -0500 Subject: [PATCH 03/23] Add CLUSTER ADDSLOTS and CLUSTER DELSLOTS commands --- src/main/java/redis/clients/jedis/Client.java | 26 +++++++++++++++---- src/main/java/redis/clients/jedis/Jedis.java | 14 +++++++++- .../java/redis/clients/jedis/Protocol.java | 2 ++ .../tests/commands/ClusterCommandsTest.java | 13 ++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index c4e131e..90621f3 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -808,11 +808,27 @@ public class Client extends BinaryClient implements Commands { hincrByFloat(SafeEncoder.encode(key), SafeEncoder.encode(field), increment); } - public void cluster(final Object... args) { - final byte[][] arg = new byte[args.length][]; - for (int i = 0; i < arg.length; i++) { - arg[i] = SafeEncoder.encode(args[i].toString()); + public void cluster(final String subcommand, final int... args) { + final byte[][] arg = new byte[args.length+1][]; + for (int i = 1; i < arg.length; i++) { + arg[i] = toByteArray(args[i-1]); } + arg[0] = SafeEncoder.encode(subcommand); cluster(arg); } -} + + public void cluster(final String subcommand, final String... args) { + final byte[][] arg = new byte[args.length+1][]; + for (int i = 1; i < arg.length; i++) { + arg[i] = SafeEncoder.encode(args[i-1]); + } + arg[0] = SafeEncoder.encode(subcommand); + cluster(arg); + } + + public void cluster(final String subcommand) { + final byte[][] arg = new byte[1][]; + arg[0] = SafeEncoder.encode(subcommand); + cluster(arg); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index ef952a7..b4db590 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3085,7 +3085,19 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand public String clusterMeet(final String ip, final int port) { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_MEET, ip, port); + client.cluster(Protocol.CLUSTER_MEET, ip, String.valueOf(port)); + return client.getStatusCodeReply(); + } + + public String clusterAddSlots(final int ...slots) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_ADDSLOTS, slots); + return client.getStatusCodeReply(); + } + + public String clusterDelSlots(final int ...slots) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_DELSLOTS, slots); return client.getStatusCodeReply(); } } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 22479a5..1bbdc06 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -32,6 +32,8 @@ public final class Protocol { public static final String CLUSTER_NODES = "nodes"; public static final String CLUSTER_MEET = "meet"; + public static final String CLUSTER_ADDSLOTS = "addslots"; + public static final String CLUSTER_DELSLOTS = "delslots"; private Protocol() { // this prevent the class from instantiation diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java index 0fe04f1..6b11b09 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -45,4 +45,17 @@ public class ClusterCommandsTest extends JedisTestBase { String status = node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); assertEquals("OK", status); } + + @Test + public void clusterAddSlots() { + String status = node1.clusterAddSlots(1, 2, 3, 4, 5); + assertEquals("OK", status); + } + + @Test + public void clusterDelSlots() { + node1.clusterAddSlots(900); + String status = node1.clusterDelSlots(900); + assertEquals("OK", status); + } } \ No newline at end of file From 77d244b96a17b3f9a193e857b1164c2ae70d5b2a Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Mon, 2 Dec 2013 14:04:37 -0500 Subject: [PATCH 04/23] Add CLUSTER INFO, CLUSTER SETSLOT and CLUSTER GETKEYSINSLOT commands --- src/main/java/redis/clients/jedis/Jedis.java | 31 +++++++++++++ .../java/redis/clients/jedis/Protocol.java | 6 +++ .../tests/commands/ClusterCommandsTest.java | 45 ++++++++++++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index b4db590..6451f30 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3100,4 +3100,35 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand client.cluster(Protocol.CLUSTER_DELSLOTS, slots); return client.getStatusCodeReply(); } + + public String clusterInfo() { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_INFO); + return client.getStatusCodeReply(); + } + + public List clusterGetKeysInSlot(final int slot, final int count) { + checkIsInMulti(); + final int[] args = new int[]{ slot, count }; + client.cluster(Protocol.CLUSTER_GETKEYSINSLOT, args); + return client.getMultiBulkReply(); + } + + public String clusterSetSlotNode(final int slot, final String nodeId) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_NODE, nodeId); + return client.getStatusCodeReply(); + } + + public String clusterSetSlotMigrating(final int slot, final String nodeId) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_MIGRATING, nodeId); + return client.getStatusCodeReply(); + } + + public String clusterSetSlotImporting(final int slot, final String nodeId) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_IMPORTING, nodeId); + return client.getStatusCodeReply(); + } } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 1bbdc06..a3eacd6 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -34,6 +34,12 @@ public final class Protocol { public static final String CLUSTER_MEET = "meet"; public static final String CLUSTER_ADDSLOTS = "addslots"; public static final String CLUSTER_DELSLOTS = "delslots"; + public static final String CLUSTER_INFO = "info"; + public static final String CLUSTER_GETKEYSINSLOT = "getkeysinslot"; + public static final String CLUSTER_SETSLOT = "setslot"; + public static final String CLUSTER_SETSLOT_NODE = "node"; + public static final String CLUSTER_SETSLOT_MIGRATING = "migrating"; + public static final String CLUSTER_SETSLOT_IMPORTING = "importing"; private Protocol() { // this prevent the class from instantiation diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java index 6b11b09..f4abd11 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -1,5 +1,7 @@ package redis.clients.jedis.tests.commands; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -45,17 +47,56 @@ public class ClusterCommandsTest extends JedisTestBase { String status = node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); assertEquals("OK", status); } - + @Test public void clusterAddSlots() { String status = node1.clusterAddSlots(1, 2, 3, 4, 5); assertEquals("OK", status); } - + @Test public void clusterDelSlots() { node1.clusterAddSlots(900); String status = node1.clusterDelSlots(900); assertEquals("OK", status); } + + @Test + public void clusterInfo() { + String info = node1.clusterInfo(); + assertNotNull(info); + } + + @Test + public void clusterGetKeysInSlot() { + node1.clusterAddSlots(500); + List keys = node1.clusterGetKeysInSlot(500, 1); + assertEquals(0, keys.size()); + } + + @Test + public void clusterSetSlotNode() { + String[] nodes = node1.clusterNodes().split("\n"); + String nodeId = nodes[0].split(" ")[0]; + String status = node1.clusterSetSlotNode(10000, nodeId); + assertEquals("OK", status); + } + + @Test + public void clusterSetSlotMigrating() { + node1.clusterAddSlots(5000); + String[] nodes = node1.clusterNodes().split("\n"); + String nodeId = nodes[0].split(" ")[0]; + String status = node1.clusterSetSlotMigrating(5000, nodeId); + assertEquals("OK", status); + } + + @Test + public void clusterSetSlotImporting() { + node2.clusterAddSlots(6000); + String[] nodes = node1.clusterNodes().split("\n"); + String nodeId = nodes[0].split(" ")[0]; + String status = node1.clusterSetSlotImporting(6000, nodeId); + assertEquals("OK", status); + } } \ No newline at end of file From 5b15d48803078397b3af487bdad289dc9a0ba21a Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Mon, 2 Dec 2013 14:34:05 -0500 Subject: [PATCH 05/23] Refactor to have interfaces. Add cluster pipeline commands. --- src/main/java/redis/clients/jedis/Client.java | 37 ++++++++++++++ .../redis/clients/jedis/ClusterCommands.java | 23 +++++++++ .../redis/clients/jedis/ClusterPipeline.java | 23 +++++++++ src/main/java/redis/clients/jedis/Jedis.java | 21 ++++---- .../clients/jedis/MultiKeyPipelineBase.java | 48 ++++++++++++++++++- 5 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/ClusterCommands.java create mode 100644 src/main/java/redis/clients/jedis/ClusterPipeline.java diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 90621f3..bbe7045 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -831,4 +831,41 @@ public class Client extends BinaryClient implements Commands { arg[0] = SafeEncoder.encode(subcommand); cluster(arg); } + + public void clusterNodes() { + cluster(Protocol.CLUSTER_NODES); + } + + public void clusterMeet(final String ip, final int port) { + cluster(Protocol.CLUSTER_MEET, ip, String.valueOf(port)); + } + + public void clusterAddSlots(final int ...slots) { + cluster(Protocol.CLUSTER_ADDSLOTS, slots); + } + + public void clusterDelSlots(final int ...slots) { + cluster(Protocol.CLUSTER_DELSLOTS, slots); + } + + public void clusterInfo() { + cluster(Protocol.CLUSTER_INFO); + } + + public void clusterGetKeysInSlot(final int slot, final int count) { + final int[] args = new int[]{ slot, count }; + cluster(Protocol.CLUSTER_GETKEYSINSLOT, args); + } + + public void clusterSetSlotNode(final int slot, final String nodeId) { + cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_NODE, nodeId); + } + + public void clusterSetSlotMigrating(final int slot, final String nodeId) { + cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_MIGRATING, nodeId); + } + + public void clusterSetSlotImporting(final int slot, final String nodeId) { + cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_IMPORTING, nodeId); + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/ClusterCommands.java b/src/main/java/redis/clients/jedis/ClusterCommands.java new file mode 100644 index 0000000..fff4533 --- /dev/null +++ b/src/main/java/redis/clients/jedis/ClusterCommands.java @@ -0,0 +1,23 @@ +package redis.clients.jedis; + +import java.util.List; + +public interface ClusterCommands { + String clusterNodes(); + + String clusterMeet(final String ip, final int port); + + String clusterAddSlots(final int... slots); + + String clusterDelSlots(final int... slots); + + String clusterInfo(); + + List clusterGetKeysInSlot(final int slot, final int count); + + String clusterSetSlotNode(final int slot, final String nodeId); + + String clusterSetSlotMigrating(final int slot, final String nodeId); + + String clusterSetSlotImporting(final int slot, final String nodeId); +} diff --git a/src/main/java/redis/clients/jedis/ClusterPipeline.java b/src/main/java/redis/clients/jedis/ClusterPipeline.java new file mode 100644 index 0000000..73330d4 --- /dev/null +++ b/src/main/java/redis/clients/jedis/ClusterPipeline.java @@ -0,0 +1,23 @@ +package redis.clients.jedis; + +import java.util.List; + +public interface ClusterPipeline { + Response clusterNodes(); + + Response clusterMeet(final String ip, final int port); + + Response clusterAddSlots(final int... slots); + + Response clusterDelSlots(final int... slots); + + Response clusterInfo(); + + Response> clusterGetKeysInSlot(final int slot, final int count); + + Response clusterSetSlotNode(final int slot, final String nodeId); + + Response clusterSetSlotMigrating(final int slot, final String nodeId); + + Response clusterSetSlotImporting(final int slot, final String nodeId); +} diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 6451f30..c6add77 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -7,7 +7,7 @@ import redis.clients.util.Slowlog; import java.net.URI; import java.util.*; -public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands { +public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands, ClusterCommands { public Jedis(final String host) { super(host); } @@ -3079,56 +3079,55 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand public String clusterNodes() { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_NODES); + client.clusterNodes(); return client.getBulkReply(); } public String clusterMeet(final String ip, final int port) { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_MEET, ip, String.valueOf(port)); + client.clusterMeet(ip, port); return client.getStatusCodeReply(); } public String clusterAddSlots(final int ...slots) { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_ADDSLOTS, slots); + client.clusterAddSlots(slots); return client.getStatusCodeReply(); } public String clusterDelSlots(final int ...slots) { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_DELSLOTS, slots); + client.clusterDelSlots(slots); return client.getStatusCodeReply(); } public String clusterInfo() { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_INFO); + client.clusterInfo(); return client.getStatusCodeReply(); } public List clusterGetKeysInSlot(final int slot, final int count) { checkIsInMulti(); - final int[] args = new int[]{ slot, count }; - client.cluster(Protocol.CLUSTER_GETKEYSINSLOT, args); + client.clusterGetKeysInSlot(slot, count); return client.getMultiBulkReply(); } public String clusterSetSlotNode(final int slot, final String nodeId) { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_NODE, nodeId); + client.clusterSetSlotNode(slot, nodeId); return client.getStatusCodeReply(); } public String clusterSetSlotMigrating(final int slot, final String nodeId) { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_MIGRATING, nodeId); + client.clusterSetSlotMigrating(slot, nodeId); return client.getStatusCodeReply(); } public String clusterSetSlotImporting(final int slot, final String nodeId) { checkIsInMulti(); - client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_IMPORTING, nodeId); + client.clusterSetSlotImporting(slot, nodeId); return client.getStatusCodeReply(); } } diff --git a/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java index be9d89a..04bdc80 100644 --- a/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java +++ b/src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java @@ -7,7 +7,8 @@ import java.util.Set; abstract class MultiKeyPipelineBase extends PipelineBase implements BasicRedisPipeline, MultiKeyBinaryRedisPipeline, - MultiKeyCommandsPipeline { + MultiKeyCommandsPipeline, + ClusterPipeline { protected Client client = null; @@ -398,4 +399,49 @@ abstract class MultiKeyPipelineBase extends PipelineBase implements client.bitop(op, destKey, srcKeys); return getResponse(BuilderFactory.LONG); } + + public Response clusterNodes() { + client.clusterNodes(); + return getResponse(BuilderFactory.STRING); + } + + public Response clusterMeet(final String ip, final int port) { + client.clusterMeet(ip, port); + return getResponse(BuilderFactory.STRING); + } + + public Response clusterAddSlots(final int... slots) { + client.clusterAddSlots(slots); + return getResponse(BuilderFactory.STRING); + } + + public Response clusterDelSlots(final int... slots) { + client.clusterDelSlots(slots); + return getResponse(BuilderFactory.STRING); + } + + public Response clusterInfo() { + client.clusterInfo(); + return getResponse(BuilderFactory.STRING); + } + + public Response> clusterGetKeysInSlot(final int slot, final int count) { + client.clusterGetKeysInSlot(slot, count); + return getResponse(BuilderFactory.STRING_LIST); + } + + public Response clusterSetSlotNode(final int slot, final String nodeId) { + client.clusterSetSlotNode(slot, nodeId); + return getResponse(BuilderFactory.STRING); + } + + public Response clusterSetSlotMigrating(final int slot, final String nodeId) { + client.clusterSetSlotMigrating(slot, nodeId); + return getResponse(BuilderFactory.STRING); + } + + public Response clusterSetSlotImporting(final int slot, final String nodeId) { + client.clusterSetSlotImporting(slot, nodeId); + return getResponse(BuilderFactory.STRING); + } } From 46966fb89f6fa9383bef7009f1949d3002594438 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Mon, 2 Dec 2013 19:27:12 -0500 Subject: [PATCH 06/23] Prepare redis cluster for testing. The cluster will always be in a deterministic state to be able to reproduce -MOVED and -ASK --- .../redis/clients/jedis/JedisCluster.java | 5 ++ .../clients/jedis/tests/JedisClusterTest.java | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/main/java/redis/clients/jedis/JedisCluster.java create mode 100644 src/test/java/redis/clients/jedis/tests/JedisClusterTest.java diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java new file mode 100644 index 0000000..7b5613b --- /dev/null +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -0,0 +1,5 @@ +package redis.clients.jedis; + +public class JedisCluster { + public static final int HASH_SLOTS = 16384; +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java new file mode 100644 index 0000000..ca90362 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -0,0 +1,70 @@ +package redis.clients.jedis.tests; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Pipeline; + +public class JedisClusterTest extends Assert { + private Jedis node1; + private Jedis node2; + private Jedis node3; + + private HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0); + private HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1); + private HostAndPort nodeInfo3 = HostAndPortUtil.getClusterServers().get(2); + + @Before + public void setUp() { + node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); + node1.connect(); + node1.flushAll(); + + node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); + node2.connect(); + node2.flushAll(); + + node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort()); + node3.connect(); + node3.flushAll(); + + // ---- configure cluster + + // add nodes to cluster + node1.clusterMeet("127.0.0.1", nodeInfo1.getPort()); + node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); + node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); + + // add all slots to node1 + Pipeline pipelined = node1.pipelined(); + for (int i = 0; i < JedisCluster.HASH_SLOTS; i++) { + pipelined.clusterAddSlots(i); + } + pipelined.sync(); + } + + @After + public void tearDown() { + // clear all slots of node1 + Pipeline pipelined = node1.pipelined(); + for (int i = 0; i < JedisCluster.HASH_SLOTS; i++) { + pipelined.clusterDelSlots(i); + } + pipelined.sync(); + } + + @Test + public void moved() { + //TODO: needs to implement + } + + @Test + public void ask() { + //TODO: needs to implement + } +} \ No newline at end of file From a8987ed865e1034f83d1e6a3ef681582ccb8a614 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Sat, 7 Dec 2013 17:55:17 -0300 Subject: [PATCH 07/23] Add first test to thorw MovedDataException when receiving MOVED from a cluster node --- Makefile | 2 +- .../java/redis/clients/jedis/BinaryJedis.java | 2 +- .../redis/clients/jedis/JedisCluster.java | 738 +++++++++++++++++- .../java/redis/clients/jedis/JedisPool.java | 2 +- .../java/redis/clients/jedis/Protocol.java | 12 +- .../exceptions/JedisMovedDataException.java | 17 + .../clients/jedis/tests/JedisClusterTest.java | 15 +- 7 files changed, 775 insertions(+), 13 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java diff --git a/Makefile b/Makefile index e0189f2..8e45570 100644 --- a/Makefile +++ b/Makefile @@ -181,7 +181,7 @@ stop: test: make start - mvn clean compile test + mvn -Dtest=${TEST} clean compile test make stop deploy: diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 385b1c0..2d8dead 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -1697,7 +1697,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKey protected void checkIsInMulti() { if (client.isInMulti()) { - throw new JedisDataException( + throw new JedisDataException( "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); } } diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 7b5613b..4586b8f 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -1,5 +1,739 @@ package redis.clients.jedis; -public class JedisCluster { - public static final int HASH_SLOTS = 16384; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.util.Pool; + +public class JedisCluster implements JedisCommands, BasicCommands { + + public static final short HASHSLOTS = 16384; + private static final int DEFAULT_TIMEOUT = 1; + + private Map nodes = new HashMap(); + + public JedisCluster(Set nodes, int timeout) { + initializeSlotsCache(nodes); + } + + private void initializeSlotsCache(Set nodes) { + for (HostAndPort hostAndPort : nodes) { + JedisPool jp = new JedisPool(hostAndPort.getHost(), hostAndPort.getPort()); + this.nodes.put(hostAndPort.getHost()+hostAndPort.getPort(), jp); + } + + } + + public JedisCluster(Set nodes) { + this(nodes, DEFAULT_TIMEOUT); + } + + + @Override + public String set(String key, String value) { + return getRandomConnection().set(key, value); + } + + @Override + public String get(String key) { + return getRandomConnection().get(key); + } + + @Override + public Boolean exists(String key) { + return getRandomConnection().exists(key); + } + + @Override + public Long persist(String key) { + return getRandomConnection().persist(key); + } + + @Override + public String type(String key) { + return getRandomConnection().type(key); + } + + @Override + public Long expire(String key, int seconds) { + return getRandomConnection().expire(key, seconds); + } + + @Override + public Long expireAt(String key, long unixTime) { + return getRandomConnection().expireAt(key, unixTime); + } + + @Override + public Long ttl(String key) { + return getRandomConnection().ttl(key); + } + + @Override + public Boolean setbit(String key, long offset, boolean value) { + return getRandomConnection().setbit(key, offset, value); + } + + @Override + public Boolean setbit(String key, long offset, String value) { + return getRandomConnection().setbit(key, offset, value); + } + + @Override + public Boolean getbit(String key, long offset) { + return getRandomConnection().getbit(key, offset); + } + + @Override + public Long setrange(String key, long offset, String value) { + return getRandomConnection().setrange(key, offset, value); + } + + @Override + public String getrange(String key, long startOffset, long endOffset) { + return getRandomConnection().getrange(key, startOffset, endOffset); + } + + @Override + public String getSet(String key, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long setnx(String key, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String setex(String key, int seconds, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long decrBy(String key, long integer) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long decr(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long incrBy(String key, long integer) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long incr(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long append(String key, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String substr(String key, int start, int end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long hset(String key, String field, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String hget(String key, String field) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long hsetnx(String key, String field, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String hmset(String key, Map hash) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List hmget(String key, String... fields) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long hincrBy(String key, String field, long value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Boolean hexists(String key, String field) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long hdel(String key, String... field) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long hlen(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set hkeys(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List hvals(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Map hgetAll(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long rpush(String key, String... string) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long lpush(String key, String... string) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long llen(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List lrange(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String ltrim(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String lindex(String key, long index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String lset(String key, long index, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long lrem(String key, long count, String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String lpop(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String rpop(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long sadd(String key, String... member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set smembers(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long srem(String key, String... member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String spop(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long scard(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Boolean sismember(String key, String member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String srandmember(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long strlen(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zadd(String key, double score, String member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zadd(String key, Map scoreMembers) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrange(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zrem(String key, String... member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Double zincrby(String key, double score, String member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zrank(String key, String member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zrevrank(String key, String member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrange(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeWithScores(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeWithScores(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zcard(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Double zscore(String key, String member) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List sort(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List sort(String key, SortingParams sortingParameters) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zcount(String key, double min, double max) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zcount(String key, String min, String max) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScore(String key, double min, double max) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScore(String key, String min, String max) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScore(String key, double max, double min) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScore(String key, double min, double max, + int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScore(String key, String max, String min) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScore(String key, String min, String max, + int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScore(String key, double max, double min, + int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScoreWithScores(String key, double min, double max) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScoreWithScores(String key, double max, + double min) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScoreWithScores(String key, double min, + double max, int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScore(String key, String max, String min, + int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScoreWithScores(String key, String min, String max) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScoreWithScores(String key, String max, + String min) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrangeByScoreWithScores(String key, String min, + String max, int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScoreWithScores(String key, double max, + double min, int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set zrevrangeByScoreWithScores(String key, String max, + String min, int offset, int count) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zremrangeByRank(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zremrangeByScore(String key, double start, double end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long zremrangeByScore(String key, String start, String end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long linsert(String key, LIST_POSITION where, String pivot, + String value) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long lpushx(String key, String... string) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long rpushx(String key, String... string) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List blpop(String arg) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List brpop(String arg) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long del(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String echo(String string) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long move(String key, int dbIndex) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long bitcount(String key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long bitcount(String key, long start, long end) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String ping() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String quit() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String flushDB() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long dbSize() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String select(int index) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String flushAll() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String auth(String password) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String save() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String bgsave() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String bgrewriteaof() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long lastsave() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String shutdown() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String info() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String info(String section) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String slaveof(String host, int port) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String slaveofNoOne() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Long getDB() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String debug(DebugParams params) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String configResetStat() { + // TODO Auto-generated method stub + return null; + } + + + @SuppressWarnings("unchecked") + private Jedis getRandomConnection() { + Object[] nodeArray = nodes.values().toArray(); + return ((Pool) nodeArray[new Random().nextInt(nodeArray.length)]).getResource(); + } + + + } diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 58212ba..6511207 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -18,7 +18,7 @@ public class JedisPool extends Pool { this(new GenericObjectPoolConfig(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null); } - + public JedisPool(final String host) { URI uri = URI.create(host); if (uri.getScheme() != null && uri.getScheme().equals("redis")) { diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index a3eacd6..123edc4 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -6,13 +6,15 @@ import java.util.List; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; +import redis.clients.jedis.exceptions.JedisMovedDataException; import redis.clients.util.RedisInputStream; import redis.clients.util.RedisOutputStream; import redis.clients.util.SafeEncoder; public final class Protocol { - public static final int DEFAULT_PORT = 6379; + private static final String MOVED_RESPONSE = "MOVED"; + public static final int DEFAULT_PORT = 6379; public static final int DEFAULT_SENTINEL_PORT = 26379; public static final int DEFAULT_TIMEOUT = 2000; public static final int DEFAULT_DATABASE = 0; @@ -72,8 +74,12 @@ public final class Protocol { } private static void processError(final RedisInputStream is) { - String message = is.readLine(); - throw new JedisDataException(message); + String message = is.readLine(); + //TODO: Read only first 5 bytes? + if (message.contains(MOVED_RESPONSE)) { + throw new JedisMovedDataException(message); + } + throw new JedisDataException(message); } private static Object process(final RedisInputStream is) { diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java b/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java new file mode 100644 index 0000000..78e0a4b --- /dev/null +++ b/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java @@ -0,0 +1,17 @@ +package redis.clients.jedis.exceptions; + +public class JedisMovedDataException extends JedisDataException { + private static final long serialVersionUID = 3878126572474819403L; + + public JedisMovedDataException(String message) { + super(message); + } + + public JedisMovedDataException(Throwable cause) { + super(cause); + } + + public JedisMovedDataException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index ca90362..68ad4eb 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -1,5 +1,7 @@ package redis.clients.jedis.tests; +import java.util.HashSet; + import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -9,6 +11,7 @@ import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Pipeline; +import redis.clients.jedis.exceptions.JedisMovedDataException; public class JedisClusterTest extends Assert { private Jedis node1; @@ -42,7 +45,7 @@ public class JedisClusterTest extends Assert { // add all slots to node1 Pipeline pipelined = node1.pipelined(); - for (int i = 0; i < JedisCluster.HASH_SLOTS; i++) { + for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { pipelined.clusterAddSlots(i); } pipelined.sync(); @@ -52,15 +55,17 @@ public class JedisClusterTest extends Assert { public void tearDown() { // clear all slots of node1 Pipeline pipelined = node1.pipelined(); - for (int i = 0; i < JedisCluster.HASH_SLOTS; i++) { + for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { pipelined.clusterDelSlots(i); } pipelined.sync(); } - @Test - public void moved() { - //TODO: needs to implement + @Test(expected=JedisMovedDataException.class) + public void throwMovedExceptionTest() { + JedisCluster jc = new JedisCluster(new HashSet(HostAndPortUtil.getClusterServers())); + jc.set("foo", "bar"); + jc.get("foo"); } @Test From 726c3151b67c194d84c22e2e80fd3099249eecb7 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Sun, 8 Dec 2013 01:08:14 -0300 Subject: [PATCH 08/23] Add RedisSlot helper to calculate key slot --- .../clients/jedis/tests/JedisClusterTest.java | 21 +++++++++++++--- .../clients/jedis/tests/utils/RedisSlot.java | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 68ad4eb..6e4a7cf 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -12,6 +12,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Pipeline; import redis.clients.jedis.exceptions.JedisMovedDataException; +import redis.clients.jedis.tests.utils.RedisSlot; public class JedisClusterTest extends Assert { private Jedis node1; @@ -68,8 +69,22 @@ public class JedisClusterTest extends Assert { jc.get("foo"); } - @Test - public void ask() { - //TODO: needs to implement +// @Test +// public void ask() { +// JedisCluster jc = new JedisCluster(new HashSet(HostAndPortUtil.getClusterServers())); +// jc.set("foo", "bar"); +// int keySlot = RedisSlot.getSlot("foo"); +// String node2Id = getNodeId(node2.clusterNodes()); +// node1.clusterSetSlotMigrating(keySlot, node2Id); +// node1.get("foo"); +// } + + private String getNodeId(String infoOutput) { + for (String infoLine : infoOutput.split("\n")) { + if (infoLine.contains("myself")) { + return infoLine.split(" ")[0]; + } + } + return ""; } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java b/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java new file mode 100644 index 0000000..1dd76c3 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java @@ -0,0 +1,25 @@ +package redis.clients.jedis.tests.utils; + +public class RedisSlot { + public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1 + static int crc; + + public RedisSlot(){ + crc = 0x0000; + } + + public static int getSlot(String key) { + for (byte b : key.getBytes()) { + for (int i = 0; i < 8; i++) { + boolean bit = ((b >> (7-i) & 1) == 1); + boolean c15 = ((crc >> 15 & 1) == 1); + crc <<= 1; + // If coefficient of bit and remainder polynomial = 1 xor crc with polynomial + if (c15 ^ bit) crc ^= polynomial; + } + } + + return crc &= 0xffff % 16384; + } + +} \ No newline at end of file From 0ebbf02c944308fdb367dfd4e0373534b0975838 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Mon, 9 Dec 2013 15:17:13 -0300 Subject: [PATCH 09/23] Change Moved cluster test and add confirmation for jedis cluster tests --- .../exceptions/JedisAskDataException.java | 17 ++++ .../clients/jedis/tests/JedisClusterTest.java | 85 ++++++++++--------- 2 files changed, 62 insertions(+), 40 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java new file mode 100644 index 0000000..158256d --- /dev/null +++ b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java @@ -0,0 +1,17 @@ +package redis.clients.jedis.exceptions; + +public class JedisAskDataException extends JedisDataException { + private static final long serialVersionUID = 3878126572474819403L; + + public JedisAskDataException(String message) { + super(message); + } + + public JedisAskDataException(Throwable cause) { + super(cause); + } + + public JedisAskDataException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 6e4a7cf..b465be8 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -1,7 +1,5 @@ package redis.clients.jedis.tests; -import java.util.HashSet; - import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -11,6 +9,7 @@ import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Pipeline; +import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisMovedDataException; import redis.clients.jedis.tests.utils.RedisSlot; @@ -24,55 +23,61 @@ public class JedisClusterTest extends Assert { private HostAndPort nodeInfo3 = HostAndPortUtil.getClusterServers().get(2); @Before - public void setUp() { - node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); - node1.connect(); - node1.flushAll(); - - node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); - node2.connect(); - node2.flushAll(); - - node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort()); - node3.connect(); - node3.flushAll(); - - // ---- configure cluster - - // add nodes to cluster - node1.clusterMeet("127.0.0.1", nodeInfo1.getPort()); - node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); - node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); - - // add all slots to node1 - Pipeline pipelined = node1.pipelined(); - for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { - pipelined.clusterAddSlots(i); - } - pipelined.sync(); + public void setUp() throws InterruptedException { + node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); + node1.connect(); + node1.flushAll(); + + node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); + node2.connect(); + node2.flushAll(); + + node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort()); + node3.connect(); + node3.flushAll(); + + // ---- configure cluster + + // add nodes to cluster + node1.clusterMeet("127.0.0.1", nodeInfo1.getPort()); + node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); + node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); + + // add all slots to node1 + Pipeline pipelined = node1.pipelined(); + for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { + pipelined.clusterAddSlots(i); + } + pipelined.sync(); + + boolean clusterOk = false; + while (!clusterOk) { + if (node1.clusterInfo().split("\n")[0].contains("ok")) { + clusterOk = true; + } + Thread.sleep(100); + } } @After public void tearDown() { - // clear all slots of node1 - Pipeline pipelined = node1.pipelined(); - for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { - pipelined.clusterDelSlots(i); - } - pipelined.sync(); + // clear all slots of node1 + Pipeline pipelined = node1.pipelined(); + for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { + pipelined.clusterDelSlots(i); + } + pipelined.sync(); } @Test(expected=JedisMovedDataException.class) public void throwMovedExceptionTest() { - JedisCluster jc = new JedisCluster(new HashSet(HostAndPortUtil.getClusterServers())); - jc.set("foo", "bar"); - jc.get("foo"); + node1.set("foo", "bar"); + node2.get("foo"); } -// @Test +// @Test(expected=JedisAskDataException.class) // public void ask() { -// JedisCluster jc = new JedisCluster(new HashSet(HostAndPortUtil.getClusterServers())); -// jc.set("foo", "bar"); +// node1.set("foo", "bar"); // int keySlot = RedisSlot.getSlot("foo"); // String node2Id = getNodeId(node2.clusterNodes()); // node1.clusterSetSlotMigrating(keySlot, node2Id); From c00807004592abf6868b4cc61f1ccaa690c0fe13 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Tue, 10 Dec 2013 10:25:41 -0300 Subject: [PATCH 10/23] Add JedisClusterCommand and updated code to use it respectively --- .../redis/clients/jedis/JedisCluster.java | 140 ++++++++++++------ .../clients/jedis/JedisClusterCommand.java | 23 +++ .../jedis/JedisClusterConnectionHandler.java | 33 +++++ .../clients/jedis/tests/JedisClusterTest.java | 4 +- 4 files changed, 156 insertions(+), 44 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/JedisClusterCommand.java create mode 100644 src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 4586b8f..a3b6f22 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -1,32 +1,25 @@ package redis.clients.jedis; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.Set; import redis.clients.jedis.BinaryClient.LIST_POSITION; -import redis.clients.util.Pool; public class JedisCluster implements JedisCommands, BasicCommands { public static final short HASHSLOTS = 16384; private static final int DEFAULT_TIMEOUT = 1; - private Map nodes = new HashMap(); + + private JedisClusterConnectionHandler connectionHandler; public JedisCluster(Set nodes, int timeout) { - initializeSlotsCache(nodes); - } - - private void initializeSlotsCache(Set nodes) { - for (HostAndPort hostAndPort : nodes) { - JedisPool jp = new JedisPool(hostAndPort.getHost(), hostAndPort.getPort()); - this.nodes.put(hostAndPort.getHost()+hostAndPort.getPort(), jp); - } + connectionHandler = new JedisClusterConnectionHandler(nodes); } + + public JedisCluster(Set nodes) { this(nodes, DEFAULT_TIMEOUT); @@ -34,68 +27,133 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override - public String set(String key, String value) { - return getRandomConnection().set(key, value); + public String set(final String key, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getRandomConnection().set(key, value); + } + }.run(); } @Override - public String get(String key) { - return getRandomConnection().get(key); + public String get(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getRandomConnection().get(key); + } + }.run(); } @Override - public Boolean exists(String key) { - return getRandomConnection().exists(key); + public Boolean exists(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Boolean execute() { + return connectionHandler.getRandomConnection().exists(key); + } + }.run(); } @Override - public Long persist(String key) { - return getRandomConnection().persist(key); + public Long persist(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getRandomConnection().persist(key); + } + }.run(); } @Override - public String type(String key) { - return getRandomConnection().type(key); + public String type(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getRandomConnection().type(key); + } + }.run(); } @Override - public Long expire(String key, int seconds) { - return getRandomConnection().expire(key, seconds); + public Long expire(final String key, final int seconds) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getRandomConnection().expire(key, seconds); + } + }.run(); } @Override - public Long expireAt(String key, long unixTime) { - return getRandomConnection().expireAt(key, unixTime); + public Long expireAt(final String key, final long unixTime) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getRandomConnection().expireAt(key, unixTime); + } + }.run(); } @Override - public Long ttl(String key) { - return getRandomConnection().ttl(key); + public Long ttl(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getRandomConnection().ttl(key); + } + }.run(); } @Override - public Boolean setbit(String key, long offset, boolean value) { - return getRandomConnection().setbit(key, offset, value); + public Boolean setbit(final String key, final long offset, final boolean value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Boolean execute() { + return connectionHandler.getRandomConnection().setbit(key, offset, value); + } + }.run(); } @Override - public Boolean setbit(String key, long offset, String value) { - return getRandomConnection().setbit(key, offset, value); + public Boolean setbit(final String key, final long offset, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Boolean execute() { + return connectionHandler.getRandomConnection().setbit(key, offset, value); + } + }.run(); } @Override - public Boolean getbit(String key, long offset) { - return getRandomConnection().getbit(key, offset); + public Boolean getbit(final String key, final long offset) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Boolean execute() { + return connectionHandler.getRandomConnection().getbit(key, offset); + } + }.run(); } @Override - public Long setrange(String key, long offset, String value) { - return getRandomConnection().setrange(key, offset, value); + public Long setrange(final String key, final long offset, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getRandomConnection().setrange(key, offset, value); + } + }.run(); } @Override - public String getrange(String key, long startOffset, long endOffset) { - return getRandomConnection().getrange(key, startOffset, endOffset); + public String getrange(final String key, final long startOffset, final long endOffset) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getRandomConnection().getrange(key, startOffset, endOffset); + } + }.run(); } @Override @@ -728,11 +786,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { } - @SuppressWarnings("unchecked") - private Jedis getRandomConnection() { - Object[] nodeArray = nodes.values().toArray(); - return ((Pool) nodeArray[new Random().nextInt(nodeArray.length)]).getResource(); - } + diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java new file mode 100644 index 0000000..55c9f29 --- /dev/null +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -0,0 +1,23 @@ +package redis.clients.jedis; + +import redis.clients.jedis.exceptions.JedisMovedDataException; + +public abstract class JedisClusterCommand { + + private JedisClusterConnectionHandler connectionHandler; + + public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler) { + this.connectionHandler = connectionHandler; + } + + public abstract T execute(); + + public T run() { + try { + return execute(); + } catch (JedisMovedDataException e) { + //TODO: Retry here + } + return null; + } +} diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java new file mode 100644 index 0000000..d942723 --- /dev/null +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -0,0 +1,33 @@ +package redis.clients.jedis; + +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import redis.clients.util.Pool; + +public class JedisClusterConnectionHandler { + + private Map nodes = new HashMap(); + + public JedisClusterConnectionHandler(Set nodes) { + initializeSlotsCache(nodes); + } + + private void initializeSlotsCache(Set nodes) { + for (HostAndPort hostAndPort : nodes) { + JedisPool jp = new JedisPool(hostAndPort.getHost(), + hostAndPort.getPort()); + this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); + } + + } + + @SuppressWarnings("unchecked") + public Jedis getRandomConnection() { + Object[] nodeArray = nodes.values().toArray(); + return ((Pool) nodeArray[new Random().nextInt(nodeArray.length)]).getResource(); + } + +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index b465be8..204b4f8 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -52,7 +52,9 @@ public class JedisClusterTest extends Assert { boolean clusterOk = false; while (!clusterOk) { - if (node1.clusterInfo().split("\n")[0].contains("ok")) { + if (node1.clusterInfo().split("\n")[0].contains("ok") && + node2.clusterInfo().split("\n")[0].contains("ok") && + node3.clusterInfo().split("\n")[0].contains("ok") ) { clusterOk = true; } Thread.sleep(100); From 0bc27ac3a298b34d72aabe1afa985fd816a71fa0 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Fri, 20 Dec 2013 15:28:32 -0300 Subject: [PATCH 11/23] Refactor redis cluster connection handler and add JedisAskDataException handling --- .../redis/clients/jedis/JedisCluster.java | 28 ++++++++-------- .../jedis/JedisClusterConnectionHandler.java | 29 ++-------------- .../jedis/JedisRandomConnectionHandler.java | 33 +++++++++++++++++++ .../java/redis/clients/jedis/Protocol.java | 7 +++- .../clients/jedis/tests/JedisClusterTest.java | 15 ++++----- .../clients/jedis/tests/utils/RedisSlot.java | 4 +++ 6 files changed, 66 insertions(+), 50 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index a3b6f22..3c82d8c 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -15,7 +15,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { private JedisClusterConnectionHandler connectionHandler; public JedisCluster(Set nodes, int timeout) { - connectionHandler = new JedisClusterConnectionHandler(nodes); + connectionHandler = new JedisRandomConnectionHandler(nodes); } @@ -31,7 +31,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getRandomConnection().set(key, value); + return connectionHandler.getConnection().set(key, value); } }.run(); } @@ -41,7 +41,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getRandomConnection().get(key); + return connectionHandler.getConnection().get(key); } }.run(); } @@ -51,7 +51,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getRandomConnection().exists(key); + return connectionHandler.getConnection().exists(key); } }.run(); } @@ -61,7 +61,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getRandomConnection().persist(key); + return connectionHandler.getConnection().persist(key); } }.run(); } @@ -71,7 +71,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getRandomConnection().type(key); + return connectionHandler.getConnection().type(key); } }.run(); } @@ -81,7 +81,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getRandomConnection().expire(key, seconds); + return connectionHandler.getConnection().expire(key, seconds); } }.run(); } @@ -91,7 +91,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getRandomConnection().expireAt(key, unixTime); + return connectionHandler.getConnection().expireAt(key, unixTime); } }.run(); } @@ -101,7 +101,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getRandomConnection().ttl(key); + return connectionHandler.getConnection().ttl(key); } }.run(); } @@ -111,7 +111,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getRandomConnection().setbit(key, offset, value); + return connectionHandler.getConnection().setbit(key, offset, value); } }.run(); } @@ -121,7 +121,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getRandomConnection().setbit(key, offset, value); + return connectionHandler.getConnection().setbit(key, offset, value); } }.run(); } @@ -131,7 +131,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getRandomConnection().getbit(key, offset); + return connectionHandler.getConnection().getbit(key, offset); } }.run(); } @@ -141,7 +141,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getRandomConnection().setrange(key, offset, value); + return connectionHandler.getConnection().setrange(key, offset, value); } }.run(); } @@ -151,7 +151,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getRandomConnection().getrange(key, startOffset, endOffset); + return connectionHandler.getConnection().getrange(key, startOffset, endOffset); } }.run(); } diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index d942723..d5aaf51 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -1,33 +1,8 @@ package redis.clients.jedis; -import java.util.HashMap; -import java.util.Map; -import java.util.Random; -import java.util.Set; -import redis.clients.util.Pool; - -public class JedisClusterConnectionHandler { - - private Map nodes = new HashMap(); +public interface JedisClusterConnectionHandler { - public JedisClusterConnectionHandler(Set nodes) { - initializeSlotsCache(nodes); - } - - private void initializeSlotsCache(Set nodes) { - for (HostAndPort hostAndPort : nodes) { - JedisPool jp = new JedisPool(hostAndPort.getHost(), - hostAndPort.getPort()); - this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); - } - - } - - @SuppressWarnings("unchecked") - public Jedis getRandomConnection() { - Object[] nodeArray = nodes.values().toArray(); - return ((Pool) nodeArray[new Random().nextInt(nodeArray.length)]).getResource(); - } + Jedis getConnection(); } diff --git a/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java new file mode 100644 index 0000000..f3da675 --- /dev/null +++ b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java @@ -0,0 +1,33 @@ +package redis.clients.jedis; + +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import redis.clients.util.Pool; + +public class JedisRandomConnectionHandler implements JedisClusterConnectionHandler { + + private Map nodes = new HashMap(); + + public JedisRandomConnectionHandler(Set nodes) { + initializeSlotsCache(nodes); + } + + private void initializeSlotsCache(Set nodes) { + for (HostAndPort hostAndPort : nodes) { + JedisPool jp = new JedisPool(hostAndPort.getHost(), + hostAndPort.getPort()); + this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); + } + + } + + @SuppressWarnings("unchecked") + public Jedis getConnection() { + Object[] nodeArray = nodes.values().toArray(); + return ((Pool) nodeArray[new Random().nextInt(nodeArray.length)]).getResource(); + } + +} diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 123edc4..7eecd25 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisMovedDataException; @@ -13,6 +14,7 @@ import redis.clients.util.SafeEncoder; public final class Protocol { + private static final String ASK_RESPONSE = "ASK"; private static final String MOVED_RESPONSE = "MOVED"; public static final int DEFAULT_PORT = 6379; public static final int DEFAULT_SENTINEL_PORT = 26379; @@ -75,9 +77,12 @@ public final class Protocol { private static void processError(final RedisInputStream is) { String message = is.readLine(); - //TODO: Read only first 5 bytes? + //TODO: I'm not sure if this is the best way to do this. + //Maybe Read only first 5 bytes instead? if (message.contains(MOVED_RESPONSE)) { throw new JedisMovedDataException(message); + } else if (message.contains(ASK_RESPONSE)) { + throw new JedisAskDataException(message); } throw new JedisDataException(message); } diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 204b4f8..0f7c06c 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -77,14 +77,13 @@ public class JedisClusterTest extends Assert { node2.get("foo"); } -// @Test(expected=JedisAskDataException.class) -// public void ask() { -// node1.set("foo", "bar"); -// int keySlot = RedisSlot.getSlot("foo"); -// String node2Id = getNodeId(node2.clusterNodes()); -// node1.clusterSetSlotMigrating(keySlot, node2Id); -// node1.get("foo"); -// } + @Test(expected=JedisAskDataException.class) + public void ask() { + int keySlot = RedisSlot.getSlot("test"); + String node2Id = getNodeId(node2.clusterNodes()); + node1.clusterSetSlotMigrating(keySlot, node2Id); + node1.get("test"); + } private String getNodeId(String infoOutput) { for (String infoLine : infoOutput.split("\n")) { diff --git a/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java b/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java index 1dd76c3..61a98dd 100644 --- a/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java +++ b/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java @@ -22,4 +22,8 @@ public class RedisSlot { return crc &= 0xffff % 16384; } + public static void main(String[] args) { + System.out.println(getSlot("test")); + } + } \ No newline at end of file From af72248c221a6844b3e42e739d5bebc791de7940 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Fri, 20 Dec 2013 16:56:29 -0300 Subject: [PATCH 12/23] Implement all Jedis cluster commands and rename RedisSlot class --- .../redis/clients/jedis/JedisCluster.java | 1030 ++++++++++++----- .../clients/jedis/JedisClusterCommand.java | 7 +- .../clients/jedis/tests/JedisClusterTest.java | 6 +- ...{RedisSlot.java => JedisClusterCRC16.java} | 4 +- 4 files changed, 726 insertions(+), 321 deletions(-) rename src/test/java/redis/clients/jedis/tests/utils/{RedisSlot.java => JedisClusterCRC16.java} (92%) diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 3c82d8c..460e032 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -18,8 +18,6 @@ public class JedisCluster implements JedisCommands, BasicCommands { connectionHandler = new JedisRandomConnectionHandler(nodes); } - - public JedisCluster(Set nodes) { this(nodes, DEFAULT_TIMEOUT); @@ -157,637 +155,1041 @@ public class JedisCluster implements JedisCommands, BasicCommands { } @Override - public String getSet(String key, String value) { - // TODO Auto-generated method stub - return null; + public String getSet(final String key, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().getSet(key, value); + } + }.run(); } @Override - public Long setnx(String key, String value) { - // TODO Auto-generated method stub - return null; + public Long setnx(final String key, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().setnx(key, value); + } + }.run(); } @Override - public String setex(String key, int seconds, String value) { - // TODO Auto-generated method stub - return null; + public String setex(final String key, final int seconds, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().setex(key, seconds, value); + } + }.run(); } @Override - public Long decrBy(String key, long integer) { - // TODO Auto-generated method stub - return null; + public Long decrBy(final String key, final long integer) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().decrBy(key, integer); + } + }.run(); } @Override - public Long decr(String key) { - // TODO Auto-generated method stub - return null; + public Long decr(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().decr(key); + } + }.run(); } @Override - public Long incrBy(String key, long integer) { - // TODO Auto-generated method stub - return null; + public Long incrBy(final String key, final long integer) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().incrBy(key, integer); + } + }.run(); } @Override - public Long incr(String key) { - // TODO Auto-generated method stub - return null; + public Long incr(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().incr(key); + } + }.run(); } @Override - public Long append(String key, String value) { - // TODO Auto-generated method stub - return null; + public Long append(final String key, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().append(key, value); + } + }.run(); } @Override - public String substr(String key, int start, int end) { - // TODO Auto-generated method stub - return null; + public String substr(final String key, final int start, final int end) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().substr(key, start, end); + } + }.run(); } @Override - public Long hset(String key, String field, String value) { - // TODO Auto-generated method stub - return null; + public Long hset(final String key, final String field, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().hset(key, field, value); + } + }.run(); } @Override - public String hget(String key, String field) { - // TODO Auto-generated method stub - return null; + public String hget(final String key, final String field) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().hget(key, field); + } + }.run(); } @Override - public Long hsetnx(String key, String field, String value) { - // TODO Auto-generated method stub - return null; + public Long hsetnx(final String key, final String field, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().hsetnx(key, field, value); + } + }.run(); } @Override - public String hmset(String key, Map hash) { - // TODO Auto-generated method stub - return null; + public String hmset(final String key, final Map hash) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().hmset(key, hash); + } + }.run(); } @Override - public List hmget(String key, String... fields) { - // TODO Auto-generated method stub - return null; + public List hmget(final String key, final String... fields) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public List execute() { + return connectionHandler.getConnection().hmget(key, fields); + } + }.run(); } @Override - public Long hincrBy(String key, String field, long value) { - // TODO Auto-generated method stub - return null; + public Long hincrBy(final String key, final String field, final long value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().hincrBy(key, field, value); + } + }.run(); } @Override - public Boolean hexists(String key, String field) { - // TODO Auto-generated method stub - return null; + public Boolean hexists(final String key, final String field) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Boolean execute() { + return connectionHandler.getConnection().hexists(key, field); + } + }.run(); } @Override - public Long hdel(String key, String... field) { - // TODO Auto-generated method stub - return null; + public Long hdel(final String key, final String... field) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().hdel(key, field); + } + }.run(); } @Override - public Long hlen(String key) { - // TODO Auto-generated method stub - return null; + public Long hlen(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().hdel(key); + } + }.run(); } @Override - public Set hkeys(String key) { - // TODO Auto-generated method stub - return null; + public Set hkeys(final String key) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().hkeys(key); + } + }.run(); } @Override - public List hvals(String key) { - // TODO Auto-generated method stub - return null; + public List hvals(final String key) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public List execute() { + return connectionHandler.getConnection().hvals(key); + } + }.run(); } @Override - public Map hgetAll(String key) { - // TODO Auto-generated method stub - return null; + public Map hgetAll(final String key) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Map execute() { + return connectionHandler.getConnection().hgetAll(key); + } + }.run(); } @Override - public Long rpush(String key, String... string) { - // TODO Auto-generated method stub - return null; + public Long rpush(final String key, final String... string) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().rpush(key, string); + } + }.run(); } @Override - public Long lpush(String key, String... string) { - // TODO Auto-generated method stub - return null; + public Long lpush(final String key, final String... string) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().lpush(key, string); + } + }.run(); } @Override - public Long llen(String key) { - // TODO Auto-generated method stub - return null; + public Long llen(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().llen(key); + } + }.run(); } @Override - public List lrange(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public List lrange(final String key, final long start, final long end) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public List execute() { + return connectionHandler.getConnection().lrange(key, start, end); + } + }.run(); } @Override - public String ltrim(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public String ltrim(final String key, final long start, final long end) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().ltrim(key, start, end); + } + }.run(); } @Override - public String lindex(String key, long index) { - // TODO Auto-generated method stub - return null; + public String lindex(final String key, final long index) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().lindex(key, index); + } + }.run(); } @Override - public String lset(String key, long index, String value) { - // TODO Auto-generated method stub - return null; + public String lset(final String key, final long index, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().lset(key, index, value); + } + }.run(); } @Override - public Long lrem(String key, long count, String value) { - // TODO Auto-generated method stub - return null; + public Long lrem(final String key, final long count, final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().lrem(key, count, value); + } + }.run(); } @Override - public String lpop(String key) { - // TODO Auto-generated method stub - return null; + public String lpop(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().lpop(key); + } + }.run(); } @Override - public String rpop(String key) { - // TODO Auto-generated method stub - return null; + public String rpop(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().rpop(key); + } + }.run(); } @Override - public Long sadd(String key, String... member) { - // TODO Auto-generated method stub - return null; + public Long sadd(final String key, final String... member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().sadd(key, member); + } + }.run(); } @Override - public Set smembers(String key) { - // TODO Auto-generated method stub - return null; + public Set smembers(final String key) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().smembers(key); + } + }.run(); } @Override - public Long srem(String key, String... member) { - // TODO Auto-generated method stub - return null; + public Long srem(final String key, final String... member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().srem(key, member); + } + }.run(); } @Override - public String spop(String key) { - // TODO Auto-generated method stub - return null; + public String spop(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().spop(key); + } + }.run(); } @Override - public Long scard(String key) { - // TODO Auto-generated method stub - return null; + public Long scard(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().scard(key); + } + }.run(); } @Override - public Boolean sismember(String key, String member) { - // TODO Auto-generated method stub - return null; + public Boolean sismember(final String key, final String member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Boolean execute() { + return connectionHandler.getConnection().sismember(key, member); + } + }.run(); } @Override - public String srandmember(String key) { - // TODO Auto-generated method stub - return null; + public String srandmember(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().srandmember(key); + } + }.run(); } @Override - public Long strlen(String key) { - // TODO Auto-generated method stub - return null; + public Long strlen(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().strlen(key); + } + }.run(); } @Override - public Long zadd(String key, double score, String member) { - // TODO Auto-generated method stub - return null; + public Long zadd(final String key, final double score, final String member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zadd(key, score, member); + } + }.run(); } @Override - public Long zadd(String key, Map scoreMembers) { - // TODO Auto-generated method stub - return null; + public Long zadd(final String key, final Map scoreMembers) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zadd(key, scoreMembers); + } + }.run(); } @Override - public Set zrange(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public Set zrange(final String key, final long start, final long end) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrange(key, start, end); + } + }.run(); } @Override - public Long zrem(String key, String... member) { - // TODO Auto-generated method stub - return null; + public Long zrem(final String key, final String... member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zrem(key, member); + } + }.run(); } @Override - public Double zincrby(String key, double score, String member) { - // TODO Auto-generated method stub - return null; + public Double zincrby(final String key, final double score, final String member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Double execute() { + return connectionHandler.getConnection().zincrby(key, score, member); + } + }.run(); } @Override - public Long zrank(String key, String member) { - // TODO Auto-generated method stub - return null; + public Long zrank(final String key, final String member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zrank(key, member); + } + }.run(); } @Override - public Long zrevrank(String key, String member) { - // TODO Auto-generated method stub - return null; + public Long zrevrank(final String key, final String member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zrevrank(key, member); + } + }.run(); } @Override - public Set zrevrange(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public Set zrevrange(final String key, final long start, final long end) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrange(key, start, end); + } + }.run(); } @Override - public Set zrangeWithScores(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public Set zrangeWithScores(final String key, final long start, final long end) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeWithScores(key, start, end); + } + }.run(); } @Override - public Set zrevrangeWithScores(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeWithScores(final String key, final long start, final long end) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeWithScores(key, start, end); + } + }.run(); } @Override - public Long zcard(String key) { - // TODO Auto-generated method stub - return null; + public Long zcard(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zcard(key); + } + }.run(); } @Override - public Double zscore(String key, String member) { - // TODO Auto-generated method stub - return null; + public Double zscore(final String key, final String member) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Double execute() { + return connectionHandler.getConnection().zscore(key, member); + } + }.run(); } @Override - public List sort(String key) { - // TODO Auto-generated method stub - return null; + public List sort(final String key) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public List execute() { + return connectionHandler.getConnection().sort(key); + } + }.run(); } @Override - public List sort(String key, SortingParams sortingParameters) { - // TODO Auto-generated method stub - return null; + public List sort(final String key, final SortingParams sortingParameters) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public List execute() { + return connectionHandler.getConnection().sort(key, sortingParameters); + } + }.run(); } @Override - public Long zcount(String key, double min, double max) { - // TODO Auto-generated method stub - return null; + public Long zcount(final String key, final double min, final double max) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zcount(key, min, max); + } + }.run(); } @Override - public Long zcount(String key, String min, String max) { - // TODO Auto-generated method stub - return null; + public Long zcount(final String key, final String min, final String max) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zcount(key, min, max); + } + }.run(); } @Override - public Set zrangeByScore(String key, double min, double max) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScore(final String key, final double min, final double max) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScore(key, min, max); + } + }.run(); } @Override - public Set zrangeByScore(String key, String min, String max) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScore(final String key, final String min, final String max) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScore(key, min, max); + } + }.run(); } @Override - public Set zrevrangeByScore(String key, double max, double min) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScore(final String key, final double max, final double min) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScore(key, min, max); + } + }.run(); } @Override - public Set zrangeByScore(String key, double min, double max, - int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScore(final String key, final double min, final double max, + final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); + } + }.run(); } @Override - public Set zrevrangeByScore(String key, String max, String min) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScore(final String key, final String max, final String min) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScore(key, min, max); + } + }.run(); } @Override - public Set zrangeByScore(String key, String min, String max, - int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScore(final String key, final String min, final String max, + final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); + } + }.run(); } @Override - public Set zrevrangeByScore(String key, double max, double min, - int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScore(final String key, final double max, final double min, + final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); + } + }.run(); } @Override - public Set zrangeByScoreWithScores(String key, double min, double max) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScoreWithScores(final String key, final double min, final double max) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); + } + }.run(); } @Override - public Set zrevrangeByScoreWithScores(String key, double max, - double min) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScoreWithScores(final String key, final double max, final double min) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); + } + }.run(); } @Override - public Set zrangeByScoreWithScores(String key, double min, - double max, int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); + } + }.run(); } @Override - public Set zrevrangeByScore(String key, String max, String min, - int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScore(final String key, final String max, final String min, + final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); + } + }.run(); } @Override - public Set zrangeByScoreWithScores(String key, String min, String max) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScoreWithScores(final String key, final String min, final String max) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); + } + }.run(); } @Override - public Set zrevrangeByScoreWithScores(String key, String max, - String min) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScoreWithScores(final String key, final String max, + final String min) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); + } + }.run(); } @Override - public Set zrangeByScoreWithScores(String key, String min, - String max, int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrangeByScoreWithScores(final String key, final String min, + final String max, final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); + } + }.run(); } @Override - public Set zrevrangeByScoreWithScores(String key, double max, - double min, int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScoreWithScores(final String key, final double max, + final double min, final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); + } + }.run(); } @Override - public Set zrevrangeByScoreWithScores(String key, String max, - String min, int offset, int count) { - // TODO Auto-generated method stub - return null; + public Set zrevrangeByScoreWithScores(final String key, final String max, + final String min, final int offset, final int count) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public Set execute() { + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); + } + }.run(); } @Override - public Long zremrangeByRank(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public Long zremrangeByRank(final String key, final long start, final long end) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zremrangeByRank(key, start, end); + } + }.run(); } @Override - public Long zremrangeByScore(String key, double start, double end) { - // TODO Auto-generated method stub - return null; + public Long zremrangeByScore(final String key, final double start, final double end) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zremrangeByScore(key, start, end); + } + }.run(); } @Override - public Long zremrangeByScore(String key, String start, String end) { - // TODO Auto-generated method stub - return null; + public Long zremrangeByScore(final String key, final String start, final String end) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().zremrangeByScore(key, start, end); + } + }.run(); } @Override - public Long linsert(String key, LIST_POSITION where, String pivot, - String value) { - // TODO Auto-generated method stub - return null; + public Long linsert(final String key, final LIST_POSITION where, final String pivot, + final String value) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().linsert(key, where, pivot, value); + } + }.run(); } @Override - public Long lpushx(String key, String... string) { - // TODO Auto-generated method stub - return null; + public Long lpushx(final String key, final String... string) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().lpushx(key, string); + } + }.run(); } @Override - public Long rpushx(String key, String... string) { - // TODO Auto-generated method stub - return null; + public Long rpushx(final String key, final String... string) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().rpushx(key, string); + } + }.run(); } @Override - public List blpop(String arg) { - // TODO Auto-generated method stub - return null; + public List blpop(final String arg) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public List execute() { + return connectionHandler.getConnection().blpop(arg); + } + }.run(); } @Override - public List brpop(String arg) { - // TODO Auto-generated method stub - return null; + public List brpop(final String arg) { + return new JedisClusterCommand>(connectionHandler) { + @Override + public List execute() { + return connectionHandler.getConnection().brpop(arg); + } + }.run(); } @Override - public Long del(String key) { - // TODO Auto-generated method stub - return null; + public Long del(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().del(key); + } + }.run(); } @Override - public String echo(String string) { - // TODO Auto-generated method stub - return null; + public String echo(final String string) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().echo(string); + } + }.run(); } @Override - public Long move(String key, int dbIndex) { - // TODO Auto-generated method stub - return null; + public Long move(final String key, final int dbIndex) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().move(key, dbIndex); + } + }.run(); } @Override - public Long bitcount(String key) { - // TODO Auto-generated method stub - return null; + public Long bitcount(final String key) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().bitcount(key); + } + }.run(); } @Override - public Long bitcount(String key, long start, long end) { - // TODO Auto-generated method stub - return null; + public Long bitcount(final String key, final long start, final long end) { + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().bitcount(key, start, end); + } + }.run(); } @Override public String ping() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().ping(); + } + }.run(); } @Override public String quit() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().quit(); + } + }.run(); } @Override public String flushDB() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().flushDB(); + } + }.run(); } @Override public Long dbSize() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().dbSize(); + } + }.run(); } @Override - public String select(int index) { - // TODO Auto-generated method stub - return null; + public String select(final int index) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().select(index); + } + }.run(); } @Override public String flushAll() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().flushAll(); + } + }.run(); } @Override - public String auth(String password) { - // TODO Auto-generated method stub - return null; + public String auth(final String password) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().auth(password); + } + }.run(); } @Override public String save() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().save(); + } + }.run(); } @Override public String bgsave() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().bgsave(); + } + }.run(); } @Override public String bgrewriteaof() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().bgrewriteaof(); + } + }.run(); } @Override public Long lastsave() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().lastsave(); + } + }.run(); } @Override public String shutdown() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().shutdown(); + } + }.run(); } @Override public String info() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().info(); + } + }.run(); } @Override - public String info(String section) { - // TODO Auto-generated method stub - return null; + public String info(final String section) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().info(section); + } + }.run(); } @Override - public String slaveof(String host, int port) { - // TODO Auto-generated method stub - return null; + public String slaveof(final String host, final int port) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().slaveof(host, port); + } + }.run(); } @Override public String slaveofNoOne() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().slaveofNoOne(); + } + }.run(); } @Override public Long getDB() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public Long execute() { + return connectionHandler.getConnection().getDB(); + } + }.run(); } @Override - public String debug(DebugParams params) { - // TODO Auto-generated method stub - return null; + public String debug(final DebugParams params) { + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().debug(params); + } + }.run(); } @Override public String configResetStat() { - // TODO Auto-generated method stub - return null; + return new JedisClusterCommand(connectionHandler) { + @Override + public String execute() { + return connectionHandler.getConnection().configResetStat(); + } + }.run(); } - - - - - - } diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java index 55c9f29..1d9aece 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterCommand.java +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -1,5 +1,6 @@ package redis.clients.jedis; +import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisMovedDataException; public abstract class JedisClusterCommand { @@ -15,8 +16,10 @@ public abstract class JedisClusterCommand { public T run() { try { return execute(); - } catch (JedisMovedDataException e) { - //TODO: Retry here + } catch (JedisMovedDataException jme) { + //TODO: Do Retry here + } catch (JedisAskDataException jae) { + //TODO: Do ASK here } return null; } diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 0f7c06c..29f275d 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -11,7 +11,7 @@ import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Pipeline; import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisMovedDataException; -import redis.clients.jedis.tests.utils.RedisSlot; +import redis.clients.jedis.tests.utils.JedisClusterCRC16; public class JedisClusterTest extends Assert { private Jedis node1; @@ -78,8 +78,8 @@ public class JedisClusterTest extends Assert { } @Test(expected=JedisAskDataException.class) - public void ask() { - int keySlot = RedisSlot.getSlot("test"); + public void throwAskExceptionTest() { + int keySlot = JedisClusterCRC16.getSlot("test"); String node2Id = getNodeId(node2.clusterNodes()); node1.clusterSetSlotMigrating(keySlot, node2Id); node1.get("test"); diff --git a/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java b/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java similarity index 92% rename from src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java rename to src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java index 61a98dd..edf7e2c 100644 --- a/src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java +++ b/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java @@ -1,10 +1,10 @@ package redis.clients.jedis.tests.utils; -public class RedisSlot { +public class JedisClusterCRC16 { public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1 static int crc; - public RedisSlot(){ + public JedisClusterCRC16(){ crc = 0x0000; } From 9f767a084859fad71a6ab972973d2f73f3ae1098 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Fri, 27 Dec 2013 23:09:44 -0300 Subject: [PATCH 13/23] Add automatic discovery of cluster nodes --- .../redis/clients/jedis/JedisCluster.java | 6 +++ .../jedis/JedisClusterConnectionHandler.java | 43 ++++++++++++++++++- .../jedis/JedisRandomConnectionHandler.java | 15 +------ .../clients/jedis/tests/JedisClusterTest.java | 42 ++++++++++++++---- 4 files changed, 82 insertions(+), 24 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 460e032..9753815 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -1,5 +1,7 @@ package redis.clients.jedis; +import java.util.HashSet; +import java.util.IllegalFormatException; import java.util.List; import java.util.Map; import java.util.Set; @@ -1192,4 +1194,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { } }.run(); } + + public Map getClusterNodes() { + return connectionHandler.getNodes(); + } } diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index d5aaf51..a28a23c 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -1,8 +1,47 @@ package redis.clients.jedis; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; -public interface JedisClusterConnectionHandler { + +public abstract class JedisClusterConnectionHandler { - Jedis getConnection(); + protected Map nodes = new HashMap(); + + abstract Jedis getConnection(); + + public JedisClusterConnectionHandler(Set nodes) { + initializeSlotsCache(nodes); + } + + public Map getNodes() { + return nodes; + } + + private void initializeSlotsCache(Set nodes) { + for (HostAndPort hostAndPort : nodes) { + JedisPool jp = new JedisPool(hostAndPort.getHost(), hostAndPort.getPort()); + this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); + this.nodes.putAll(discoverClusterNodes(jp)); + } + } + + private Map discoverClusterNodes(JedisPool jp) { + Map discoveredNodes = new HashMap(); + String localNodes = jp.getResource().clusterNodes(); + for (String nodeInfo : localNodes.split("\n")) { + HostAndPort node = getHostAndPortFromNodeLine(nodeInfo); + JedisPool nodePool = new JedisPool(node.getHost(), node.getPort()); + discoveredNodes.put(node.getHost() + node.getPort(), nodePool); + } + return discoveredNodes; + } + + private HostAndPort getHostAndPortFromNodeLine(String nodeInfo) { + String stringHostAndPort = nodeInfo.split(" ",3)[1]; + String[] arrayHostAndPort = stringHostAndPort.split(":"); + return new HostAndPort(arrayHostAndPort[0], Integer.valueOf(arrayHostAndPort[1])); + } } diff --git a/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java index f3da675..fc8226b 100644 --- a/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java @@ -1,28 +1,17 @@ package redis.clients.jedis; -import java.util.HashMap; -import java.util.Map; import java.util.Random; import java.util.Set; import redis.clients.util.Pool; -public class JedisRandomConnectionHandler implements JedisClusterConnectionHandler { +public class JedisRandomConnectionHandler extends JedisClusterConnectionHandler { - private Map nodes = new HashMap(); public JedisRandomConnectionHandler(Set nodes) { - initializeSlotsCache(nodes); + super(nodes); } - private void initializeSlotsCache(Set nodes) { - for (HostAndPort hostAndPort : nodes) { - JedisPool jp = new JedisPool(hostAndPort.getHost(), - hostAndPort.getPort()); - this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); - } - - } @SuppressWarnings("unchecked") public Jedis getConnection() { diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 29f275d..5162635 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -1,5 +1,8 @@ package redis.clients.jedis.tests; +import java.util.HashSet; +import java.util.Set; + import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -43,12 +46,24 @@ public class JedisClusterTest extends Assert { node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); - // add all slots to node1 - Pipeline pipelined = node1.pipelined(); + // split available slots across the three nodes + int slotsPerNode = JedisCluster.HASHSLOTS / 3; + Pipeline pipeline1 = node1.pipelined(); + Pipeline pipeline2 = node2.pipelined(); + Pipeline pipeline3 = node3.pipelined(); for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { - pipelined.clusterAddSlots(i); + if (i < slotsPerNode) { + pipeline1.clusterAddSlots(i); + } else if (i > slotsPerNode * 2) { + pipeline3.clusterAddSlots(i); + } else { + pipeline2.clusterAddSlots(i); + } } - pipelined.sync(); + pipeline1.sync(); + pipeline2.sync(); + pipeline3.sync(); + boolean clusterOk = false; while (!clusterOk) { @@ -72,19 +87,28 @@ public class JedisClusterTest extends Assert { } @Test(expected=JedisMovedDataException.class) - public void throwMovedExceptionTest() { + public void testThrowMovedException() { node1.set("foo", "bar"); node2.get("foo"); } @Test(expected=JedisAskDataException.class) - public void throwAskExceptionTest() { + public void testThrowAskException() { int keySlot = JedisClusterCRC16.getSlot("test"); - String node2Id = getNodeId(node2.clusterNodes()); - node1.clusterSetSlotMigrating(keySlot, node2Id); - node1.get("test"); + String node3Id = getNodeId(node3.clusterNodes()); + node2.clusterSetSlotMigrating(keySlot, node3Id); + node2.get("test"); } + @Test + public void testDiscoverNodesAutomatically() { + Set jedisClusterNode = new HashSet(); + jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); + JedisCluster jc = new JedisCluster(jedisClusterNode); + assertEquals(jc.getClusterNodes().size(), 3); + } + + private String getNodeId(String infoOutput) { for (String infoLine : infoOutput.split("\n")) { if (infoLine.contains("myself")) { From b2d22e206037eee2bebd5e4ec0dbe57692f8a57f Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Sat, 28 Dec 2013 00:59:35 -0300 Subject: [PATCH 14/23] Add slot based connection which routes requests according to key slot --- .../redis/clients/jedis/JedisCluster.java | 236 +++++++++--------- .../jedis/JedisClusterConnectionHandler.java | 32 ++- .../jedis/JedisRandomConnectionHandler.java | 2 +- .../JedisSlotBasedConnectionHandler.java | 30 +++ .../clients/jedis/tests/JedisClusterTest.java | 12 +- .../jedis/tests/utils/JedisClusterCRC16.java | 10 +- 6 files changed, 186 insertions(+), 136 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 9753815..b654be7 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -1,7 +1,5 @@ package redis.clients.jedis; -import java.util.HashSet; -import java.util.IllegalFormatException; import java.util.List; import java.util.Map; import java.util.Set; @@ -17,7 +15,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { private JedisClusterConnectionHandler connectionHandler; public JedisCluster(Set nodes, int timeout) { - connectionHandler = new JedisRandomConnectionHandler(nodes); + connectionHandler = new JedisSlotBasedConnectionHandler(nodes); } @@ -31,7 +29,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().set(key, value); + return connectionHandler.getConnection(key).set(key, value); } }.run(); } @@ -41,7 +39,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().get(key); + return connectionHandler.getConnection(key).get(key); } }.run(); } @@ -51,7 +49,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection().exists(key); + return connectionHandler.getConnection(key).exists(key); } }.run(); } @@ -61,7 +59,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().persist(key); + return connectionHandler.getConnection(key).persist(key); } }.run(); } @@ -71,7 +69,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().type(key); + return connectionHandler.getConnection(key).type(key); } }.run(); } @@ -81,7 +79,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().expire(key, seconds); + return connectionHandler.getConnection(key).expire(key, seconds); } }.run(); } @@ -91,7 +89,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().expireAt(key, unixTime); + return connectionHandler.getConnection(key).expireAt(key, unixTime); } }.run(); } @@ -101,7 +99,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().ttl(key); + return connectionHandler.getConnection(key).ttl(key); } }.run(); } @@ -111,7 +109,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection().setbit(key, offset, value); + return connectionHandler.getConnection(key).setbit(key, offset, value); } }.run(); } @@ -121,7 +119,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection().setbit(key, offset, value); + return connectionHandler.getConnection(key).setbit(key, offset, value); } }.run(); } @@ -131,7 +129,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection().getbit(key, offset); + return connectionHandler.getConnection(key).getbit(key, offset); } }.run(); } @@ -141,7 +139,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().setrange(key, offset, value); + return connectionHandler.getConnection(key).setrange(key, offset, value); } }.run(); } @@ -151,7 +149,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().getrange(key, startOffset, endOffset); + return connectionHandler.getConnection(key).getrange(key, startOffset, endOffset); } }.run(); } @@ -161,7 +159,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().getSet(key, value); + return connectionHandler.getConnection(key).getSet(key, value); } }.run(); } @@ -171,7 +169,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().setnx(key, value); + return connectionHandler.getConnection(key).setnx(key, value); } }.run(); } @@ -181,7 +179,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().setex(key, seconds, value); + return connectionHandler.getConnection(key).setex(key, seconds, value); } }.run(); } @@ -191,7 +189,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().decrBy(key, integer); + return connectionHandler.getConnection(key).decrBy(key, integer); } }.run(); } @@ -201,7 +199,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().decr(key); + return connectionHandler.getConnection(key).decr(key); } }.run(); } @@ -211,7 +209,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().incrBy(key, integer); + return connectionHandler.getConnection(key).incrBy(key, integer); } }.run(); } @@ -221,7 +219,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().incr(key); + return connectionHandler.getConnection(key).incr(key); } }.run(); } @@ -231,7 +229,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().append(key, value); + return connectionHandler.getConnection(key).append(key, value); } }.run(); } @@ -241,7 +239,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().substr(key, start, end); + return connectionHandler.getConnection(key).substr(key, start, end); } }.run(); } @@ -251,7 +249,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().hset(key, field, value); + return connectionHandler.getConnection(key).hset(key, field, value); } }.run(); } @@ -261,7 +259,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().hget(key, field); + return connectionHandler.getConnection(key).hget(key, field); } }.run(); } @@ -271,7 +269,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().hsetnx(key, field, value); + return connectionHandler.getConnection(key).hsetnx(key, field, value); } }.run(); } @@ -281,7 +279,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().hmset(key, hash); + return connectionHandler.getConnection(key).hmset(key, hash); } }.run(); } @@ -291,7 +289,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection().hmget(key, fields); + return connectionHandler.getConnection(key).hmget(key, fields); } }.run(); } @@ -301,7 +299,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().hincrBy(key, field, value); + return connectionHandler.getConnection(key).hincrBy(key, field, value); } }.run(); } @@ -311,7 +309,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection().hexists(key, field); + return connectionHandler.getConnection(key).hexists(key, field); } }.run(); } @@ -321,7 +319,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().hdel(key, field); + return connectionHandler.getConnection(key).hdel(key, field); } }.run(); } @@ -331,7 +329,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().hdel(key); + return connectionHandler.getConnection(key).hdel(key); } }.run(); } @@ -341,7 +339,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().hkeys(key); + return connectionHandler.getConnection(key).hkeys(key); } }.run(); } @@ -351,7 +349,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection().hvals(key); + return connectionHandler.getConnection(key).hvals(key); } }.run(); } @@ -361,7 +359,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Map execute() { - return connectionHandler.getConnection().hgetAll(key); + return connectionHandler.getConnection(key).hgetAll(key); } }.run(); } @@ -371,7 +369,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().rpush(key, string); + return connectionHandler.getConnection(key).rpush(key, string); } }.run(); } @@ -381,7 +379,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().lpush(key, string); + return connectionHandler.getConnection(key).lpush(key, string); } }.run(); } @@ -391,7 +389,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().llen(key); + return connectionHandler.getConnection(key).llen(key); } }.run(); } @@ -401,7 +399,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection().lrange(key, start, end); + return connectionHandler.getConnection(key).lrange(key, start, end); } }.run(); } @@ -411,7 +409,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().ltrim(key, start, end); + return connectionHandler.getConnection(key).ltrim(key, start, end); } }.run(); } @@ -421,7 +419,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().lindex(key, index); + return connectionHandler.getConnection(key).lindex(key, index); } }.run(); } @@ -431,7 +429,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().lset(key, index, value); + return connectionHandler.getConnection(key).lset(key, index, value); } }.run(); } @@ -441,7 +439,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().lrem(key, count, value); + return connectionHandler.getConnection(key).lrem(key, count, value); } }.run(); } @@ -451,7 +449,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().lpop(key); + return connectionHandler.getConnection(key).lpop(key); } }.run(); } @@ -461,7 +459,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().rpop(key); + return connectionHandler.getConnection(key).rpop(key); } }.run(); } @@ -471,7 +469,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().sadd(key, member); + return connectionHandler.getConnection(key).sadd(key, member); } }.run(); } @@ -481,7 +479,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().smembers(key); + return connectionHandler.getConnection(key).smembers(key); } }.run(); } @@ -491,7 +489,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().srem(key, member); + return connectionHandler.getConnection(key).srem(key, member); } }.run(); } @@ -501,7 +499,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().spop(key); + return connectionHandler.getConnection(key).spop(key); } }.run(); } @@ -511,7 +509,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().scard(key); + return connectionHandler.getConnection(key).scard(key); } }.run(); } @@ -521,7 +519,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection().sismember(key, member); + return connectionHandler.getConnection(key).sismember(key, member); } }.run(); } @@ -531,7 +529,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().srandmember(key); + return connectionHandler.getConnection(key).srandmember(key); } }.run(); } @@ -541,7 +539,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().strlen(key); + return connectionHandler.getConnection(key).strlen(key); } }.run(); } @@ -551,7 +549,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zadd(key, score, member); + return connectionHandler.getConnection(key).zadd(key, score, member); } }.run(); } @@ -561,7 +559,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zadd(key, scoreMembers); + return connectionHandler.getConnection(key).zadd(key, scoreMembers); } }.run(); } @@ -571,7 +569,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrange(key, start, end); + return connectionHandler.getConnection(key).zrange(key, start, end); } }.run(); } @@ -581,7 +579,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zrem(key, member); + return connectionHandler.getConnection(key).zrem(key, member); } }.run(); } @@ -591,7 +589,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Double execute() { - return connectionHandler.getConnection().zincrby(key, score, member); + return connectionHandler.getConnection(key).zincrby(key, score, member); } }.run(); } @@ -601,7 +599,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zrank(key, member); + return connectionHandler.getConnection(key).zrank(key, member); } }.run(); } @@ -611,7 +609,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zrevrank(key, member); + return connectionHandler.getConnection(key).zrevrank(key, member); } }.run(); } @@ -621,7 +619,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrange(key, start, end); + return connectionHandler.getConnection(key).zrevrange(key, start, end); } }.run(); } @@ -631,7 +629,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeWithScores(key, start, end); + return connectionHandler.getConnection(key).zrangeWithScores(key, start, end); } }.run(); } @@ -641,7 +639,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeWithScores(key, start, end); + return connectionHandler.getConnection(key).zrevrangeWithScores(key, start, end); } }.run(); } @@ -651,7 +649,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zcard(key); + return connectionHandler.getConnection(key).zcard(key); } }.run(); } @@ -661,7 +659,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Double execute() { - return connectionHandler.getConnection().zscore(key, member); + return connectionHandler.getConnection(key).zscore(key, member); } }.run(); } @@ -671,7 +669,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection().sort(key); + return connectionHandler.getConnection(key).sort(key); } }.run(); } @@ -681,7 +679,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection().sort(key, sortingParameters); + return connectionHandler.getConnection(key).sort(key, sortingParameters); } }.run(); } @@ -691,7 +689,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zcount(key, min, max); + return connectionHandler.getConnection(key).zcount(key, min, max); } }.run(); } @@ -701,7 +699,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zcount(key, min, max); + return connectionHandler.getConnection(key).zcount(key, min, max); } }.run(); } @@ -711,7 +709,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, min, max); + return connectionHandler.getConnection(key).zrangeByScore(key, min, max); } }.run(); } @@ -721,7 +719,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, min, max); + return connectionHandler.getConnection(key).zrangeByScore(key, min, max); } }.run(); } @@ -731,7 +729,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, min, max); + return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max); } }.run(); } @@ -742,7 +740,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection(key).zrangeByScore(key, min, max, offset, count); } }.run(); } @@ -752,7 +750,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, min, max); + return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max); } }.run(); } @@ -763,7 +761,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection(key).zrangeByScore(key, min, max, offset, count); } }.run(); } @@ -774,7 +772,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max, offset, count); } }.run(); } @@ -784,7 +782,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max); } }.run(); } @@ -794,7 +792,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, min, max); } }.run(); } @@ -804,7 +802,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); + return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max, offset, count); } }.run(); } @@ -815,7 +813,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max, offset, count); } }.run(); } @@ -825,7 +823,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max); } }.run(); } @@ -836,7 +834,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, min, max); } }.run(); } @@ -847,7 +845,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); + return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max, offset, count); } }.run(); } @@ -858,7 +856,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); + return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, max, min, offset, count); } }.run(); } @@ -869,7 +867,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); + return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, max, min, offset, count); } }.run(); } @@ -879,7 +877,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zremrangeByRank(key, start, end); + return connectionHandler.getConnection(key).zremrangeByRank(key, start, end); } }.run(); } @@ -889,7 +887,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zremrangeByScore(key, start, end); + return connectionHandler.getConnection(key).zremrangeByScore(key, start, end); } }.run(); } @@ -899,7 +897,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().zremrangeByScore(key, start, end); + return connectionHandler.getConnection(key).zremrangeByScore(key, start, end); } }.run(); } @@ -910,7 +908,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().linsert(key, where, pivot, value); + return connectionHandler.getConnection(key).linsert(key, where, pivot, value); } }.run(); } @@ -920,7 +918,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().lpushx(key, string); + return connectionHandler.getConnection(key).lpushx(key, string); } }.run(); } @@ -930,7 +928,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().rpushx(key, string); + return connectionHandler.getConnection(key).rpushx(key, string); } }.run(); } @@ -940,7 +938,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection().blpop(arg); + return connectionHandler.getConnection(null).blpop(arg); } }.run(); } @@ -950,7 +948,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection().brpop(arg); + return connectionHandler.getConnection(null).brpop(arg); } }.run(); } @@ -960,7 +958,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().del(key); + return connectionHandler.getConnection(key).del(key); } }.run(); } @@ -970,7 +968,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().echo(string); + return connectionHandler.getConnection(null).echo(string); } }.run(); } @@ -980,7 +978,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().move(key, dbIndex); + return connectionHandler.getConnection(key).move(key, dbIndex); } }.run(); } @@ -990,7 +988,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().bitcount(key); + return connectionHandler.getConnection(key).bitcount(key); } }.run(); } @@ -1000,7 +998,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().bitcount(key, start, end); + return connectionHandler.getConnection(key).bitcount(key, start, end); } }.run(); } @@ -1010,7 +1008,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().ping(); + return connectionHandler.getConnection(null).ping(); } }.run(); } @@ -1020,7 +1018,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().quit(); + return connectionHandler.getConnection(null).quit(); } }.run(); } @@ -1030,7 +1028,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().flushDB(); + return connectionHandler.getConnection(null).flushDB(); } }.run(); } @@ -1040,7 +1038,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().dbSize(); + return connectionHandler.getConnection(null).dbSize(); } }.run(); } @@ -1050,7 +1048,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().select(index); + return connectionHandler.getConnection(null).select(index); } }.run(); } @@ -1060,7 +1058,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().flushAll(); + return connectionHandler.getConnection(null).flushAll(); } }.run(); } @@ -1070,7 +1068,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().auth(password); + return connectionHandler.getConnection(null).auth(password); } }.run(); } @@ -1080,7 +1078,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().save(); + return connectionHandler.getConnection(null).save(); } }.run(); } @@ -1090,7 +1088,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().bgsave(); + return connectionHandler.getConnection(null).bgsave(); } }.run(); } @@ -1100,7 +1098,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().bgrewriteaof(); + return connectionHandler.getConnection(null).bgrewriteaof(); } }.run(); } @@ -1110,7 +1108,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().lastsave(); + return connectionHandler.getConnection(null).lastsave(); } }.run(); } @@ -1120,7 +1118,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().shutdown(); + return connectionHandler.getConnection(null).shutdown(); } }.run(); } @@ -1130,7 +1128,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().info(); + return connectionHandler.getConnection(null).info(); } }.run(); } @@ -1140,7 +1138,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().info(section); + return connectionHandler.getConnection(null).info(section); } }.run(); } @@ -1150,7 +1148,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().slaveof(host, port); + return connectionHandler.getConnection(null).slaveof(host, port); } }.run(); } @@ -1160,7 +1158,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().slaveofNoOne(); + return connectionHandler.getConnection(null).slaveofNoOne(); } }.run(); } @@ -1170,7 +1168,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection().getDB(); + return connectionHandler.getConnection(null).getDB(); } }.run(); } @@ -1180,7 +1178,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().debug(params); + return connectionHandler.getConnection(null).debug(params); } }.run(); } @@ -1190,7 +1188,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection().configResetStat(); + return connectionHandler.getConnection(null).configResetStat(); } }.run(); } diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index a28a23c..24cf77f 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -8,8 +8,9 @@ import java.util.Set; public abstract class JedisClusterConnectionHandler { protected Map nodes = new HashMap(); + protected Map slots = new HashMap(); - abstract Jedis getConnection(); + abstract Jedis getConnection(String key); public JedisClusterConnectionHandler(Set nodes) { initializeSlotsCache(nodes); @@ -23,20 +24,39 @@ public abstract class JedisClusterConnectionHandler { for (HostAndPort hostAndPort : nodes) { JedisPool jp = new JedisPool(hostAndPort.getHost(), hostAndPort.getPort()); this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); - this.nodes.putAll(discoverClusterNodes(jp)); + discoverClusterNodesAndSlots(jp); } } - private Map discoverClusterNodes(JedisPool jp) { - Map discoveredNodes = new HashMap(); + private void discoverClusterNodesAndSlots(JedisPool jp) { String localNodes = jp.getResource().clusterNodes(); for (String nodeInfo : localNodes.split("\n")) { HostAndPort node = getHostAndPortFromNodeLine(nodeInfo); JedisPool nodePool = new JedisPool(node.getHost(), node.getPort()); - discoveredNodes.put(node.getHost() + node.getPort(), nodePool); + this.nodes.put(node.getHost() + node.getPort(), nodePool); + populateNodeSlots(nodeInfo, nodePool); + } + } + + private void populateNodeSlots(String nodeInfo, JedisPool nodePool) { + String[] nodeInfoArray = nodeInfo.split(" "); + if (nodeInfoArray.length > 7) { + for (int i = 8; i < nodeInfoArray.length; i++) { + processSlot(nodeInfoArray[i], nodePool); + } + } + } + + private void processSlot(String slot, JedisPool nodePool) { + if (slot.contains("-")) { + String[] slotRange = slot.split("-"); + for (int i = Integer.valueOf(slotRange[0]); i <= Integer.valueOf(slotRange[1]); i++) { + slots.put(i, nodePool); + } + } else { + slots.put(Integer.valueOf(slot), nodePool); } - return discoveredNodes; } private HostAndPort getHostAndPortFromNodeLine(String nodeInfo) { diff --git a/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java index fc8226b..f96854e 100644 --- a/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java @@ -14,7 +14,7 @@ public class JedisRandomConnectionHandler extends JedisClusterConnectionHandler @SuppressWarnings("unchecked") - public Jedis getConnection() { + public Jedis getConnection(String key) { Object[] nodeArray = nodes.values().toArray(); return ((Pool) nodeArray[new Random().nextInt(nodeArray.length)]).getResource(); } diff --git a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java new file mode 100644 index 0000000..c8961a5 --- /dev/null +++ b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java @@ -0,0 +1,30 @@ +package redis.clients.jedis; + +import java.util.Random; +import java.util.Set; + +import redis.clients.jedis.tests.utils.JedisClusterCRC16; + +public class JedisSlotBasedConnectionHandler extends JedisClusterConnectionHandler { + + + public JedisSlotBasedConnectionHandler(Set nodes) { + super(nodes); + } + + + public Jedis getConnection(String key) { + int keySlot = JedisClusterCRC16.getSlot(key); + JedisPool connectionPool = slots.get(keySlot); + if (connectionPool == null) { + connectionPool = getRandomConnection(); + } + return connectionPool.getResource(); + } + + private JedisPool getRandomConnection() { + Object[] nodeArray = nodes.values().toArray(); + return (JedisPool) (nodeArray[new Random().nextInt(nodeArray.length)]); + } + +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 5162635..bcd8458 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -89,7 +89,6 @@ public class JedisClusterTest extends Assert { @Test(expected=JedisMovedDataException.class) public void testThrowMovedException() { node1.set("foo", "bar"); - node2.get("foo"); } @Test(expected=JedisAskDataException.class) @@ -108,6 +107,17 @@ public class JedisClusterTest extends Assert { assertEquals(jc.getClusterNodes().size(), 3); } + @Test + public void testCalculateConnectionPerSlot() { + Set jedisClusterNode = new HashSet(); + jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); + JedisCluster jc = new JedisCluster(jedisClusterNode); + jc.set("foo", "bar"); + jc.set("test", "test"); + assertEquals("bar",node3.get("foo")); + assertEquals("test",node2.get("test")); + + } private String getNodeId(String infoOutput) { for (String infoLine : infoOutput.split("\n")) { diff --git a/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java b/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java index edf7e2c..24f3e3e 100644 --- a/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java +++ b/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java @@ -4,11 +4,8 @@ public class JedisClusterCRC16 { public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1 static int crc; - public JedisClusterCRC16(){ - crc = 0x0000; - } - public static int getSlot(String key) { + crc = 0x0000; for (byte b : key.getBytes()) { for (int i = 0; i < 8; i++) { boolean bit = ((b >> (7-i) & 1) == 1); @@ -21,9 +18,4 @@ public class JedisClusterCRC16 { return crc &= 0xffff % 16384; } - - public static void main(String[] args) { - System.out.println(getSlot("test")); - } - } \ No newline at end of file From 1b268157999abf69367fd2b6de7c7aed7b649896 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Thu, 2 Jan 2014 20:52:17 -0300 Subject: [PATCH 15/23] Add functionality to recalculate slots when receiving MOVED response from node. Add test to check for ASK responses (implementation missing) --- .../clients/jedis/JedisClusterCommand.java | 7 +- .../jedis/JedisClusterConnectionHandler.java | 5 ++ .../java/redis/clients/jedis/Protocol.java | 4 +- .../exceptions/JedisMovedDataException.java | 28 ++++++- .../clients/jedis/tests/JedisClusterTest.java | 74 +++++++++++++++---- 5 files changed, 94 insertions(+), 24 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java index 1d9aece..0f2b68f 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterCommand.java +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -6,6 +6,7 @@ import redis.clients.jedis.exceptions.JedisMovedDataException; public abstract class JedisClusterCommand { private JedisClusterConnectionHandler connectionHandler; + private boolean asking = false; public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler) { this.connectionHandler = connectionHandler; @@ -17,10 +18,10 @@ public abstract class JedisClusterCommand { try { return execute(); } catch (JedisMovedDataException jme) { - //TODO: Do Retry here + this.connectionHandler.assignSlotToNode(jme.getSlot(), jme.getTargetNode()); + return execute(); } catch (JedisAskDataException jae) { - //TODO: Do ASK here + throw jae; } - return null; } } diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index 24cf77f..e005ded 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -64,4 +64,9 @@ public abstract class JedisClusterConnectionHandler { String[] arrayHostAndPort = stringHostAndPort.split(":"); return new HostAndPort(arrayHostAndPort[0], Integer.valueOf(arrayHostAndPort[1])); } + + public void assignSlotToNode(int slot, HostAndPort targetNode) { + JedisPool targetPool = nodes.get(targetNode.getHost() + targetNode.getPort()); + slots.put(slot, targetPool); + } } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 7eecd25..bff80dd 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -80,7 +80,9 @@ public final class Protocol { //TODO: I'm not sure if this is the best way to do this. //Maybe Read only first 5 bytes instead? if (message.contains(MOVED_RESPONSE)) { - throw new JedisMovedDataException(message); + String[] movedInfo = message.split(" "); + String[] targetHostAndPort = movedInfo[2].split(":"); + throw new JedisMovedDataException(message, new HostAndPort(targetHostAndPort[0], Integer.valueOf(targetHostAndPort[1])), Integer.valueOf(movedInfo[1])); } else if (message.contains(ASK_RESPONSE)) { throw new JedisAskDataException(message); } diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java b/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java index 78e0a4b..c2b5565 100644 --- a/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java +++ b/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java @@ -1,17 +1,37 @@ package redis.clients.jedis.exceptions; +import redis.clients.jedis.HostAndPort; + + public class JedisMovedDataException extends JedisDataException { private static final long serialVersionUID = 3878126572474819403L; + + private HostAndPort targetNode; + private int slot; - public JedisMovedDataException(String message) { - super(message); + public JedisMovedDataException(String message, HostAndPort targetNode, int slot) { + super(message); + this.targetNode = targetNode; + this.slot = slot; } - public JedisMovedDataException(Throwable cause) { + public JedisMovedDataException(Throwable cause, HostAndPort targetNode, int slot) { super(cause); + this.targetNode = targetNode; + this.slot = slot; } - public JedisMovedDataException(String message, Throwable cause) { + public JedisMovedDataException(String message, Throwable cause, HostAndPort targetNode, int slot) { super(message, cause); + this.targetNode = targetNode; + this.slot = slot; } + + public HostAndPort getTargetNode() { + return targetNode; + } + + public int getSlot() { + return slot; + } } diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index bcd8458..256a807 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -65,31 +65,36 @@ public class JedisClusterTest extends Assert { pipeline3.sync(); - boolean clusterOk = false; - while (!clusterOk) { - if (node1.clusterInfo().split("\n")[0].contains("ok") && - node2.clusterInfo().split("\n")[0].contains("ok") && - node3.clusterInfo().split("\n")[0].contains("ok") ) { - clusterOk = true; - } - Thread.sleep(100); - } + waitForClusterReady(); } - @After + + @After public void tearDown() { - // clear all slots of node1 - Pipeline pipelined = node1.pipelined(); - for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { - pipelined.clusterDelSlots(i); + // clear all slots + int[] slotsToDelete = new int[JedisCluster.HASHSLOTS]; + for (int i = 0; i < JedisCluster.HASHSLOTS; i++) { + slotsToDelete[i] = i; } - pipelined.sync(); + node1.clusterDelSlots(slotsToDelete); + node2.clusterDelSlots(slotsToDelete); + node3.clusterDelSlots(slotsToDelete); } @Test(expected=JedisMovedDataException.class) public void testThrowMovedException() { node1.set("foo", "bar"); } + + @Test + public void testMovedExceptionParameters() { + try { + node1.set("foo", "bar"); + } catch (JedisMovedDataException jme) { + assertEquals(12182, jme.getSlot()); + assertEquals(new HostAndPort("127.0.0.1", 7381), jme.getTargetNode()); + } + } @Test(expected=JedisAskDataException.class) public void testThrowAskException() { @@ -116,7 +121,31 @@ public class JedisClusterTest extends Assert { jc.set("test", "test"); assertEquals("bar",node3.get("foo")); assertEquals("test",node2.get("test")); - + } + + @Test + public void testRecalculateSlotsWhenMoved() throws InterruptedException { + Set jedisClusterNode = new HashSet(); + jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); + JedisCluster jc = new JedisCluster(jedisClusterNode); + node2.clusterDelSlots(JedisClusterCRC16.getSlot("51")); + //TODO: We shouldn't need to issue DELSLOTS in node3, but due to redis-cluster bug we need to. + node3.clusterDelSlots(JedisClusterCRC16.getSlot("51")); + node3.clusterAddSlots(JedisClusterCRC16.getSlot("51")); + waitForClusterReady(); + jc.set("51", "foo"); + assertEquals("foo", jc.get("51")); + } + + @Test + public void testAskResponse() throws InterruptedException { + Set jedisClusterNode = new HashSet(); + jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); + JedisCluster jc = new JedisCluster(jedisClusterNode); + node3.clusterSetSlotImporting(JedisClusterCRC16.getSlot("51"), getNodeId(node2.clusterNodes())); + node2.clusterSetSlotMigrating(JedisClusterCRC16.getSlot("51"), getNodeId(node3.clusterNodes())); + jc.set("51", "foo"); + assertEquals("foo", jc.get("51")); } private String getNodeId(String infoOutput) { @@ -127,4 +156,17 @@ public class JedisClusterTest extends Assert { } return ""; } + + private void waitForClusterReady() throws InterruptedException { + boolean clusterOk = false; + while (!clusterOk) { + if (node1.clusterInfo().split("\n")[0].contains("ok") && + node2.clusterInfo().split("\n")[0].contains("ok") && + node3.clusterInfo().split("\n")[0].contains("ok") ) { + clusterOk = true; + } + Thread.sleep(100); + } + } + } \ No newline at end of file From dd0bbdaf9182f5d32c43ce0902b1a99f366c238e Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Fri, 3 Jan 2014 16:42:21 -0300 Subject: [PATCH 16/23] Several changes have been added to this commit: - Add asking to cluster commands - Make jedis cluster return connection to original pool - Add tests for MOVED and ASK cluster responses - Refactor connection handler to recalculate connections based on slots This commit makes the first usable version of Jedis along with Redis Cluster --- .../redis/clients/jedis/BinaryClient.java | 3 + src/main/java/redis/clients/jedis/Jedis.java | 6 + .../redis/clients/jedis/JedisCluster.java | 464 +++++++++--------- .../clients/jedis/JedisClusterCommand.java | 22 +- .../jedis/JedisClusterConnectionHandler.java | 12 +- .../jedis/JedisRandomConnectionHandler.java | 14 +- .../JedisSlotBasedConnectionHandler.java | 38 +- .../java/redis/clients/jedis/Protocol.java | 21 +- .../exceptions/JedisAskDataException.java | 23 +- .../clients/jedis/tests/JedisClusterTest.java | 11 +- 10 files changed, 349 insertions(+), 265 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index 1b9db49..fd10d38 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -1104,5 +1104,8 @@ public class BinaryClient extends Connection { public void cluster(final byte[]... args) { sendCommand(CLUSTER, args); } + public void asking() { + sendCommand(Command.ASKING); + } } diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index c6add77..83a4a1b 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3130,4 +3130,10 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand client.clusterSetSlotImporting(slot, nodeId); return client.getStatusCodeReply(); } + + public String asking() { + checkIsInMulti(); + client.asking(); + return client.getStatusCodeReply(); + } } diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index b654be7..be33412 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -29,9 +29,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).set(key, value); + return connectionHandler.getConnection().set(key, value); } - }.run(); + }.run(key); } @Override @@ -39,9 +39,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).get(key); + return connectionHandler.getConnection().get(key); } - }.run(); + }.run(key); } @Override @@ -49,9 +49,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection(key).exists(key); + return connectionHandler.getConnection().exists(key); } - }.run(); + }.run(key); } @Override @@ -59,9 +59,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).persist(key); + return connectionHandler.getConnection().persist(key); } - }.run(); + }.run(key); } @Override @@ -69,9 +69,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).type(key); + return connectionHandler.getConnection().type(key); } - }.run(); + }.run(key); } @Override @@ -79,9 +79,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).expire(key, seconds); + return connectionHandler.getConnection().expire(key, seconds); } - }.run(); + }.run(key); } @Override @@ -89,9 +89,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).expireAt(key, unixTime); + return connectionHandler.getConnection().expireAt(key, unixTime); } - }.run(); + }.run(key); } @Override @@ -99,9 +99,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).ttl(key); + return connectionHandler.getConnection().ttl(key); } - }.run(); + }.run(key); } @Override @@ -109,9 +109,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection(key).setbit(key, offset, value); + return connectionHandler.getConnection().setbit(key, offset, value); } - }.run(); + }.run(key); } @Override @@ -119,9 +119,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection(key).setbit(key, offset, value); + return connectionHandler.getConnection().setbit(key, offset, value); } - }.run(); + }.run(key); } @Override @@ -129,9 +129,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection(key).getbit(key, offset); + return connectionHandler.getConnection().getbit(key, offset); } - }.run(); + }.run(key); } @Override @@ -139,9 +139,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).setrange(key, offset, value); + return connectionHandler.getConnection().setrange(key, offset, value); } - }.run(); + }.run(key); } @Override @@ -149,9 +149,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).getrange(key, startOffset, endOffset); + return connectionHandler.getConnection().getrange(key, startOffset, endOffset); } - }.run(); + }.run(key); } @Override @@ -159,9 +159,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).getSet(key, value); + return connectionHandler.getConnection().getSet(key, value); } - }.run(); + }.run(key); } @Override @@ -169,9 +169,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).setnx(key, value); + return connectionHandler.getConnection().setnx(key, value); } - }.run(); + }.run(key); } @Override @@ -179,9 +179,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).setex(key, seconds, value); + return connectionHandler.getConnection().setex(key, seconds, value); } - }.run(); + }.run(key); } @Override @@ -189,9 +189,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).decrBy(key, integer); + return connectionHandler.getConnection().decrBy(key, integer); } - }.run(); + }.run(key); } @Override @@ -199,9 +199,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).decr(key); + return connectionHandler.getConnection().decr(key); } - }.run(); + }.run(key); } @Override @@ -209,9 +209,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).incrBy(key, integer); + return connectionHandler.getConnection().incrBy(key, integer); } - }.run(); + }.run(key); } @Override @@ -219,9 +219,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).incr(key); + return connectionHandler.getConnection().incr(key); } - }.run(); + }.run(key); } @Override @@ -229,9 +229,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).append(key, value); + return connectionHandler.getConnection().append(key, value); } - }.run(); + }.run(key); } @Override @@ -239,9 +239,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).substr(key, start, end); + return connectionHandler.getConnection().substr(key, start, end); } - }.run(); + }.run(key); } @Override @@ -249,9 +249,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).hset(key, field, value); + return connectionHandler.getConnection().hset(key, field, value); } - }.run(); + }.run(key); } @Override @@ -259,9 +259,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).hget(key, field); + return connectionHandler.getConnection().hget(key, field); } - }.run(); + }.run(key); } @Override @@ -269,9 +269,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).hsetnx(key, field, value); + return connectionHandler.getConnection().hsetnx(key, field, value); } - }.run(); + }.run(key); } @Override @@ -279,9 +279,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).hmset(key, hash); + return connectionHandler.getConnection().hmset(key, hash); } - }.run(); + }.run(key); } @Override @@ -289,9 +289,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection(key).hmget(key, fields); + return connectionHandler.getConnection().hmget(key, fields); } - }.run(); + }.run(key); } @Override @@ -299,9 +299,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).hincrBy(key, field, value); + return connectionHandler.getConnection().hincrBy(key, field, value); } - }.run(); + }.run(key); } @Override @@ -309,9 +309,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection(key).hexists(key, field); + return connectionHandler.getConnection().hexists(key, field); } - }.run(); + }.run(key); } @Override @@ -319,9 +319,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).hdel(key, field); + return connectionHandler.getConnection().hdel(key, field); } - }.run(); + }.run(key); } @Override @@ -329,9 +329,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).hdel(key); + return connectionHandler.getConnection().hdel(key); } - }.run(); + }.run(key); } @Override @@ -339,9 +339,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).hkeys(key); + return connectionHandler.getConnection().hkeys(key); } - }.run(); + }.run(key); } @Override @@ -349,9 +349,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection(key).hvals(key); + return connectionHandler.getConnection().hvals(key); } - }.run(); + }.run(key); } @Override @@ -359,9 +359,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Map execute() { - return connectionHandler.getConnection(key).hgetAll(key); + return connectionHandler.getConnection().hgetAll(key); } - }.run(); + }.run(key); } @Override @@ -369,9 +369,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).rpush(key, string); + return connectionHandler.getConnection().rpush(key, string); } - }.run(); + }.run(key); } @Override @@ -379,9 +379,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).lpush(key, string); + return connectionHandler.getConnection().lpush(key, string); } - }.run(); + }.run(key); } @Override @@ -389,9 +389,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).llen(key); + return connectionHandler.getConnection().llen(key); } - }.run(); + }.run(key); } @Override @@ -399,9 +399,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection(key).lrange(key, start, end); + return connectionHandler.getConnection().lrange(key, start, end); } - }.run(); + }.run(key); } @Override @@ -409,9 +409,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).ltrim(key, start, end); + return connectionHandler.getConnection().ltrim(key, start, end); } - }.run(); + }.run(key); } @Override @@ -419,9 +419,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).lindex(key, index); + return connectionHandler.getConnection().lindex(key, index); } - }.run(); + }.run(key); } @Override @@ -429,9 +429,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).lset(key, index, value); + return connectionHandler.getConnection().lset(key, index, value); } - }.run(); + }.run(key); } @Override @@ -439,9 +439,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).lrem(key, count, value); + return connectionHandler.getConnection().lrem(key, count, value); } - }.run(); + }.run(key); } @Override @@ -449,9 +449,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).lpop(key); + return connectionHandler.getConnection().lpop(key); } - }.run(); + }.run(key); } @Override @@ -459,9 +459,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).rpop(key); + return connectionHandler.getConnection().rpop(key); } - }.run(); + }.run(key); } @Override @@ -469,9 +469,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).sadd(key, member); + return connectionHandler.getConnection().sadd(key, member); } - }.run(); + }.run(key); } @Override @@ -479,9 +479,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).smembers(key); + return connectionHandler.getConnection().smembers(key); } - }.run(); + }.run(key); } @Override @@ -489,9 +489,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).srem(key, member); + return connectionHandler.getConnection().srem(key, member); } - }.run(); + }.run(key); } @Override @@ -499,9 +499,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).spop(key); + return connectionHandler.getConnection().spop(key); } - }.run(); + }.run(key); } @Override @@ -509,9 +509,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).scard(key); + return connectionHandler.getConnection().scard(key); } - }.run(); + }.run(key); } @Override @@ -519,9 +519,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Boolean execute() { - return connectionHandler.getConnection(key).sismember(key, member); + return connectionHandler.getConnection().sismember(key, member); } - }.run(); + }.run(key); } @Override @@ -529,9 +529,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(key).srandmember(key); + return connectionHandler.getConnection().srandmember(key); } - }.run(); + }.run(key); } @Override @@ -539,9 +539,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).strlen(key); + return connectionHandler.getConnection().strlen(key); } - }.run(); + }.run(key); } @Override @@ -549,9 +549,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zadd(key, score, member); + return connectionHandler.getConnection().zadd(key, score, member); } - }.run(); + }.run(key); } @Override @@ -559,9 +559,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zadd(key, scoreMembers); + return connectionHandler.getConnection().zadd(key, scoreMembers); } - }.run(); + }.run(key); } @Override @@ -569,9 +569,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrange(key, start, end); + return connectionHandler.getConnection().zrange(key, start, end); } - }.run(); + }.run(key); } @Override @@ -579,9 +579,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zrem(key, member); + return connectionHandler.getConnection().zrem(key, member); } - }.run(); + }.run(key); } @Override @@ -589,9 +589,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Double execute() { - return connectionHandler.getConnection(key).zincrby(key, score, member); + return connectionHandler.getConnection().zincrby(key, score, member); } - }.run(); + }.run(key); } @Override @@ -599,9 +599,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zrank(key, member); + return connectionHandler.getConnection().zrank(key, member); } - }.run(); + }.run(key); } @Override @@ -609,9 +609,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zrevrank(key, member); + return connectionHandler.getConnection().zrevrank(key, member); } - }.run(); + }.run(key); } @Override @@ -619,9 +619,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrange(key, start, end); + return connectionHandler.getConnection().zrevrange(key, start, end); } - }.run(); + }.run(key); } @Override @@ -629,9 +629,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeWithScores(key, start, end); + return connectionHandler.getConnection().zrangeWithScores(key, start, end); } - }.run(); + }.run(key); } @Override @@ -639,9 +639,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeWithScores(key, start, end); + return connectionHandler.getConnection().zrevrangeWithScores(key, start, end); } - }.run(); + }.run(key); } @Override @@ -649,9 +649,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zcard(key); + return connectionHandler.getConnection().zcard(key); } - }.run(); + }.run(key); } @Override @@ -659,9 +659,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Double execute() { - return connectionHandler.getConnection(key).zscore(key, member); + return connectionHandler.getConnection().zscore(key, member); } - }.run(); + }.run(key); } @Override @@ -669,9 +669,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection(key).sort(key); + return connectionHandler.getConnection().sort(key); } - }.run(); + }.run(key); } @Override @@ -679,9 +679,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection(key).sort(key, sortingParameters); + return connectionHandler.getConnection().sort(key, sortingParameters); } - }.run(); + }.run(key); } @Override @@ -689,9 +689,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zcount(key, min, max); + return connectionHandler.getConnection().zcount(key, min, max); } - }.run(); + }.run(key); } @Override @@ -699,9 +699,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zcount(key, min, max); + return connectionHandler.getConnection().zcount(key, min, max); } - }.run(); + }.run(key); } @Override @@ -709,9 +709,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScore(key, min, max); + return connectionHandler.getConnection().zrangeByScore(key, min, max); } - }.run(); + }.run(key); } @Override @@ -719,9 +719,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScore(key, min, max); + return connectionHandler.getConnection().zrangeByScore(key, min, max); } - }.run(); + }.run(key); } @Override @@ -729,9 +729,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max); + return connectionHandler.getConnection().zrevrangeByScore(key, min, max); } - }.run(); + }.run(key); } @Override @@ -740,9 +740,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); } - }.run(); + }.run(key); } @Override @@ -750,9 +750,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max); + return connectionHandler.getConnection().zrevrangeByScore(key, min, max); } - }.run(); + }.run(key); } @Override @@ -761,9 +761,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); } - }.run(); + }.run(key); } @Override @@ -772,9 +772,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); } - }.run(); + }.run(key); } @Override @@ -782,9 +782,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); } - }.run(); + }.run(key); } @Override @@ -792,9 +792,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); } - }.run(); + }.run(key); } @Override @@ -802,9 +802,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max, offset, count); + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); } - }.run(); + }.run(key); } @Override @@ -813,9 +813,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScore(key, min, max, offset, count); + return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); } - }.run(); + }.run(key); } @Override @@ -823,9 +823,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); } - }.run(); + }.run(key); } @Override @@ -834,9 +834,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, min, max); + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); } - }.run(); + }.run(key); } @Override @@ -845,9 +845,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrangeByScoreWithScores(key, min, max, offset, count); + return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); } - }.run(); + }.run(key); } @Override @@ -856,9 +856,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, max, min, offset, count); + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); } - }.run(); + }.run(key); } @Override @@ -867,9 +867,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public Set execute() { - return connectionHandler.getConnection(key).zrevrangeByScoreWithScores(key, max, min, offset, count); + return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); } - }.run(); + }.run(key); } @Override @@ -877,9 +877,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zremrangeByRank(key, start, end); + return connectionHandler.getConnection().zremrangeByRank(key, start, end); } - }.run(); + }.run(key); } @Override @@ -887,9 +887,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zremrangeByScore(key, start, end); + return connectionHandler.getConnection().zremrangeByScore(key, start, end); } - }.run(); + }.run(key); } @Override @@ -897,9 +897,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).zremrangeByScore(key, start, end); + return connectionHandler.getConnection().zremrangeByScore(key, start, end); } - }.run(); + }.run(key); } @Override @@ -908,9 +908,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).linsert(key, where, pivot, value); + return connectionHandler.getConnection().linsert(key, where, pivot, value); } - }.run(); + }.run(key); } @Override @@ -918,9 +918,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).lpushx(key, string); + return connectionHandler.getConnection().lpushx(key, string); } - }.run(); + }.run(key); } @Override @@ -928,9 +928,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).rpushx(key, string); + return connectionHandler.getConnection().rpushx(key, string); } - }.run(); + }.run(key); } @Override @@ -938,9 +938,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection(null).blpop(arg); + return connectionHandler.getConnection().blpop(arg); } - }.run(); + }.run(null); } @Override @@ -948,9 +948,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler) { @Override public List execute() { - return connectionHandler.getConnection(null).brpop(arg); + return connectionHandler.getConnection().brpop(arg); } - }.run(); + }.run(null); } @Override @@ -958,9 +958,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).del(key); + return connectionHandler.getConnection().del(key); } - }.run(); + }.run(null); } @Override @@ -968,9 +968,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).echo(string); + return connectionHandler.getConnection().echo(string); } - }.run(); + }.run(null); } @Override @@ -978,9 +978,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).move(key, dbIndex); + return connectionHandler.getConnection().move(key, dbIndex); } - }.run(); + }.run(key); } @Override @@ -988,9 +988,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).bitcount(key); + return connectionHandler.getConnection().bitcount(key); } - }.run(); + }.run(key); } @Override @@ -998,9 +998,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(key).bitcount(key, start, end); + return connectionHandler.getConnection().bitcount(key, start, end); } - }.run(); + }.run(key); } @Override @@ -1008,9 +1008,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).ping(); + return connectionHandler.getConnection().ping(); } - }.run(); + }.run(null); } @Override @@ -1018,9 +1018,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).quit(); + return connectionHandler.getConnection().quit(); } - }.run(); + }.run(null); } @Override @@ -1028,9 +1028,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).flushDB(); + return connectionHandler.getConnection().flushDB(); } - }.run(); + }.run(null); } @Override @@ -1038,9 +1038,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(null).dbSize(); + return connectionHandler.getConnection().dbSize(); } - }.run(); + }.run(null); } @Override @@ -1048,9 +1048,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).select(index); + return connectionHandler.getConnection().select(index); } - }.run(); + }.run(null); } @Override @@ -1058,9 +1058,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).flushAll(); + return connectionHandler.getConnection().flushAll(); } - }.run(); + }.run(null); } @Override @@ -1068,9 +1068,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).auth(password); + return connectionHandler.getConnection().auth(password); } - }.run(); + }.run(null); } @Override @@ -1078,9 +1078,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).save(); + return connectionHandler.getConnection().save(); } - }.run(); + }.run(null); } @Override @@ -1088,9 +1088,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).bgsave(); + return connectionHandler.getConnection().bgsave(); } - }.run(); + }.run(null); } @Override @@ -1098,9 +1098,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).bgrewriteaof(); + return connectionHandler.getConnection().bgrewriteaof(); } - }.run(); + }.run(null); } @Override @@ -1108,9 +1108,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(null).lastsave(); + return connectionHandler.getConnection().lastsave(); } - }.run(); + }.run(null); } @Override @@ -1118,9 +1118,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).shutdown(); + return connectionHandler.getConnection().shutdown(); } - }.run(); + }.run(null); } @Override @@ -1128,9 +1128,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).info(); + return connectionHandler.getConnection().info(); } - }.run(); + }.run(null); } @Override @@ -1138,9 +1138,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).info(section); + return connectionHandler.getConnection().info(section); } - }.run(); + }.run(null); } @Override @@ -1148,9 +1148,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).slaveof(host, port); + return connectionHandler.getConnection().slaveof(host, port); } - }.run(); + }.run(null); } @Override @@ -1158,9 +1158,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).slaveofNoOne(); + return connectionHandler.getConnection().slaveofNoOne(); } - }.run(); + }.run(null); } @Override @@ -1168,9 +1168,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public Long execute() { - return connectionHandler.getConnection(null).getDB(); + return connectionHandler.getConnection().getDB(); } - }.run(); + }.run(null); } @Override @@ -1178,9 +1178,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).debug(params); + return connectionHandler.getConnection().debug(params); } - }.run(); + }.run(null); } @Override @@ -1188,9 +1188,9 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler) { @Override public String execute() { - return connectionHandler.getConnection(null).configResetStat(); + return connectionHandler.getConnection().configResetStat(); } - }.run(); + }.run(null); } public Map getClusterNodes() { diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java index 0f2b68f..61e4d3a 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterCommand.java +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -1,27 +1,41 @@ package redis.clients.jedis; import redis.clients.jedis.exceptions.JedisAskDataException; +import redis.clients.jedis.exceptions.JedisClusterException; import redis.clients.jedis.exceptions.JedisMovedDataException; +import redis.clients.jedis.tests.utils.JedisClusterCRC16; public abstract class JedisClusterCommand { - private JedisClusterConnectionHandler connectionHandler; private boolean asking = false; + private JedisClusterConnectionHandler connectionHandler; +// private boolean asking = false; + public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler) { this.connectionHandler = connectionHandler; } public abstract T execute(); - public T run() { + public T run(String key) { try { + if (key == null) { + throw new JedisClusterException("No way to dispatch this command to Redis Cluster."); + } + connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key)); + if (asking) { + //TODO: Pipeline asking with the original command to make it faster.... + connectionHandler.getConnection().asking(); + } return execute(); } catch (JedisMovedDataException jme) { this.connectionHandler.assignSlotToNode(jme.getSlot(), jme.getTargetNode()); - return execute(); + return run(key); } catch (JedisAskDataException jae) { - throw jae; + asking = true; + this.connectionHandler.assignSlotToNode(jae.getSlot(), jae.getTargetNode()); + return run(key); } } } diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index e005ded..765b9a2 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -2,6 +2,7 @@ package redis.clients.jedis; import java.util.HashMap; import java.util.Map; +import java.util.Random; import java.util.Set; @@ -10,7 +11,8 @@ public abstract class JedisClusterConnectionHandler { protected Map nodes = new HashMap(); protected Map slots = new HashMap(); - abstract Jedis getConnection(String key); + abstract Jedis getConnection(); + abstract Jedis getConnectionFromSlot(int slot); public JedisClusterConnectionHandler(Set nodes) { initializeSlotsCache(nodes); @@ -69,4 +71,12 @@ public abstract class JedisClusterConnectionHandler { JedisPool targetPool = nodes.get(targetNode.getHost() + targetNode.getPort()); slots.put(slot, targetPool); } + + + protected JedisPool getRandomConnection() { + Object[] nodeArray = nodes.values().toArray(); + return (JedisPool) (nodeArray[new Random().nextInt(nodeArray.length)]); + } + + } diff --git a/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java index f96854e..40d3f04 100644 --- a/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisRandomConnectionHandler.java @@ -1,10 +1,7 @@ package redis.clients.jedis; -import java.util.Random; import java.util.Set; -import redis.clients.util.Pool; - public class JedisRandomConnectionHandler extends JedisClusterConnectionHandler { @@ -12,11 +9,12 @@ public class JedisRandomConnectionHandler extends JedisClusterConnectionHandler super(nodes); } - - @SuppressWarnings("unchecked") - public Jedis getConnection(String key) { - Object[] nodeArray = nodes.values().toArray(); - return ((Pool) nodeArray[new Random().nextInt(nodeArray.length)]).getResource(); + public Jedis getConnection() { + return getRandomConnection().getResource(); } + @Override + Jedis getConnectionFromSlot(int slot) { + return getRandomConnection().getResource(); + } } diff --git a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java index c8961a5..4f3ea5d 100644 --- a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java @@ -1,30 +1,48 @@ package redis.clients.jedis; -import java.util.Random; import java.util.Set; -import redis.clients.jedis.tests.utils.JedisClusterCRC16; - public class JedisSlotBasedConnectionHandler extends JedisClusterConnectionHandler { + private Jedis currentConnection; public JedisSlotBasedConnectionHandler(Set nodes) { super(nodes); } - public Jedis getConnection(String key) { - int keySlot = JedisClusterCRC16.getSlot(key); - JedisPool connectionPool = slots.get(keySlot); + public Jedis getConnection() { + return currentConnection != null ? currentConnection : getRandomConnection().getResource(); + } + + + + + private void returnCurrentConnection() { + if (currentConnection != null) { + nodes.get(currentConnection.getClient().getHost()+currentConnection.getClient().getPort()).returnResource(currentConnection); + } + + } + + + @Override + public void assignSlotToNode(int slot, HostAndPort targetNode) { + super.assignSlotToNode(slot, targetNode); + getConnectionFromSlot(slot); + } + + @Override + public Jedis getConnectionFromSlot(int slot) { + returnCurrentConnection(); + JedisPool connectionPool = slots.get(slot); if (connectionPool == null) { connectionPool = getRandomConnection(); } + currentConnection = connectionPool.getResource(); return connectionPool.getResource(); } - private JedisPool getRandomConnection() { - Object[] nodeArray = nodes.values().toArray(); - return (JedisPool) (nodeArray[new Random().nextInt(nodeArray.length)]); - } + } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index bff80dd..18e453f 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -79,15 +79,26 @@ public final class Protocol { String message = is.readLine(); //TODO: I'm not sure if this is the best way to do this. //Maybe Read only first 5 bytes instead? + // if (message.contains(MOVED_RESPONSE)) { - String[] movedInfo = message.split(" "); - String[] targetHostAndPort = movedInfo[2].split(":"); - throw new JedisMovedDataException(message, new HostAndPort(targetHostAndPort[0], Integer.valueOf(targetHostAndPort[1])), Integer.valueOf(movedInfo[1])); + String[] movedInfo = parseTargetHostAndSlot(message); + throw new JedisMovedDataException(message, new HostAndPort(movedInfo[1], Integer.valueOf(movedInfo[2])), Integer.valueOf(movedInfo[0])); } else if (message.contains(ASK_RESPONSE)) { - throw new JedisAskDataException(message); + String[] askInfo = parseTargetHostAndSlot(message); + throw new JedisAskDataException(message, new HostAndPort(askInfo[1], Integer.valueOf(askInfo[2])), Integer.valueOf(askInfo[0])); } throw new JedisDataException(message); } + + private static String[] parseTargetHostAndSlot(String clusterRedirectResponse) { + String[] response = new String[3]; + String[] messageInfo = clusterRedirectResponse.split(" "); + String[] targetHostAndPort = messageInfo[2].split(":"); + response[0] = messageInfo[1]; + response[1] = targetHostAndPort[0]; + response[2] = targetHostAndPort[1]; + return response; + } private static Object process(final RedisInputStream is) { try { @@ -179,7 +190,7 @@ public final class Protocol { public static enum Command { PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP, SENTINEL, - DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT, CLUSTER; + DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT, CLUSTER, ASKING; public final byte[] raw; diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java index 158256d..645d5c2 100644 --- a/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java +++ b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java @@ -1,11 +1,12 @@ package redis.clients.jedis.exceptions; +import redis.clients.jedis.HostAndPort; + public class JedisAskDataException extends JedisDataException { private static final long serialVersionUID = 3878126572474819403L; - - public JedisAskDataException(String message) { - super(message); - } + + private HostAndPort targetNode; + private int slot; public JedisAskDataException(Throwable cause) { super(cause); @@ -14,4 +15,18 @@ public class JedisAskDataException extends JedisDataException { public JedisAskDataException(String message, Throwable cause) { super(message, cause); } + + public JedisAskDataException(String message, HostAndPort targetHost, int slot) { + super(message); + this.targetNode = targetHost; + this.slot = slot; + } + + public HostAndPort getTargetNode() { + return targetNode; + } + + public int getSlot() { + return slot; + } } diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 256a807..b13c559 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -13,6 +13,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Pipeline; import redis.clients.jedis.exceptions.JedisAskDataException; +import redis.clients.jedis.exceptions.JedisClusterException; import redis.clients.jedis.exceptions.JedisMovedDataException; import redis.clients.jedis.tests.utils.JedisClusterCRC16; @@ -39,7 +40,7 @@ public class JedisClusterTest extends Assert { node3.connect(); node3.flushAll(); - // ---- configure cluster + // ---- configure cluster // add nodes to cluster node1.clusterMeet("127.0.0.1", nodeInfo1.getPort()); @@ -148,6 +149,14 @@ public class JedisClusterTest extends Assert { assertEquals("foo", jc.get("51")); } + @Test(expected=JedisClusterException.class) + public void testThrowExceptionWithoutKey() { + Set jedisClusterNode = new HashSet(); + jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); + JedisCluster jc = new JedisCluster(jedisClusterNode); + jc.ping(); + } + private String getNodeId(String infoOutput) { for (String infoLine : infoOutput.split("\n")) { if (infoLine.contains("myself")) { From 33716e237c239a12dd6320bcd04251a328c6d24b Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Fri, 3 Jan 2014 16:45:22 -0300 Subject: [PATCH 17/23] Add JedisClusterException file --- .../exceptions/JedisClusterException.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/redis/clients/jedis/exceptions/JedisClusterException.java diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisClusterException.java b/src/main/java/redis/clients/jedis/exceptions/JedisClusterException.java new file mode 100644 index 0000000..e20d5a7 --- /dev/null +++ b/src/main/java/redis/clients/jedis/exceptions/JedisClusterException.java @@ -0,0 +1,18 @@ +package redis.clients.jedis.exceptions; + + +public class JedisClusterException extends JedisDataException { + private static final long serialVersionUID = 3878126572474819403L; + + public JedisClusterException(Throwable cause) { + super(cause); + } + + public JedisClusterException(String message, Throwable cause) { + super(message, cause); + } + + public JedisClusterException(String message) { + super(message); + } +} From 4ab8ea2ef7f349349dbd0c00e89e5c20a967f87d Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Tue, 14 Jan 2014 15:57:53 -0300 Subject: [PATCH 18/23] Move Jedis CRC16 util as it's being used in the project. Changed Makefile to cleanup redis cluster node configuration before running tests Add cleanup to ClusterCommandTest. --- Makefile | 5 ++- .../clients/jedis/JedisClusterCommand.java | 2 +- .../clients/util}/JedisClusterCRC16.java | 2 +- .../clients/jedis/tests/JedisClusterTest.java | 4 +- .../tests/commands/ClusterCommandsTest.java | 40 +++++++++++++++++-- 5 files changed, 45 insertions(+), 8 deletions(-) rename src/{test/java/redis/clients/jedis/tests/utils => main/java/redis/clients/util}/JedisClusterCRC16.java (93%) diff --git a/Makefile b/Makefile index 8e45570..16c1dc6 100644 --- a/Makefile +++ b/Makefile @@ -145,7 +145,7 @@ export REDIS_CLUSTER_NODE1_CONF export REDIS_CLUSTER_NODE2_CONF export REDIS_CLUSTER_NODE3_CONF -start: +start: cleanup echo "$$REDIS1_CONF" | redis-server - echo "$$REDIS2_CONF" | redis-server - echo "$$REDIS3_CONF" | redis-server - @@ -161,6 +161,9 @@ start: echo "$$REDIS_CLUSTER_NODE2_CONF" | redis-server - echo "$$REDIS_CLUSTER_NODE3_CONF" | redis-server - +cleanup: + rm -vf /tmp/redis_cluster_node*.conf + stop: kill `cat /tmp/redis1.pid` kill `cat /tmp/redis2.pid` diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java index 61e4d3a..163b7c4 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterCommand.java +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -3,7 +3,7 @@ package redis.clients.jedis; import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisClusterException; import redis.clients.jedis.exceptions.JedisMovedDataException; -import redis.clients.jedis.tests.utils.JedisClusterCRC16; +import redis.clients.util.JedisClusterCRC16; public abstract class JedisClusterCommand { diff --git a/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java b/src/main/java/redis/clients/util/JedisClusterCRC16.java similarity index 93% rename from src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java rename to src/main/java/redis/clients/util/JedisClusterCRC16.java index 24f3e3e..044e003 100644 --- a/src/test/java/redis/clients/jedis/tests/utils/JedisClusterCRC16.java +++ b/src/main/java/redis/clients/util/JedisClusterCRC16.java @@ -1,4 +1,4 @@ -package redis.clients.jedis.tests.utils; +package redis.clients.util; public class JedisClusterCRC16 { public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1 diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index b13c559..d612b5e 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -15,7 +15,7 @@ import redis.clients.jedis.Pipeline; import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisClusterException; import redis.clients.jedis.exceptions.JedisMovedDataException; -import redis.clients.jedis.tests.utils.JedisClusterCRC16; +import redis.clients.util.JedisClusterCRC16; public class JedisClusterTest extends Assert { private Jedis node1; @@ -174,7 +174,7 @@ public class JedisClusterTest extends Assert { node3.clusterInfo().split("\n")[0].contains("ok") ) { clusterOk = true; } - Thread.sleep(100); + Thread.sleep(50); } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java index f4abd11..91a0d78 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -3,6 +3,7 @@ package redis.clients.jedis.tests.commands; import java.util.List; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; @@ -12,8 +13,8 @@ import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.JedisTestBase; public class ClusterCommandsTest extends JedisTestBase { - private Jedis node1; - private Jedis node2; + private static Jedis node1; + private static Jedis node2; private HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0); private HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1); @@ -35,8 +36,41 @@ public class ClusterCommandsTest extends JedisTestBase { node1.disconnect(); node2.disconnect(); } + + @AfterClass + public static void removeSlots() throws InterruptedException { + //This is to wait for gossip to replicate data. + waitForEqualClusterSize(); + System.out.println(node1.clusterInfo()); + System.out.println(node2.clusterInfo()); + String[] nodes = node1.clusterNodes().split("\n"); + String node1Id = nodes[0].split(" ")[0]; + node1.clusterDelSlots(1,2,3,4,5,500); + node1.clusterSetSlotNode(5000, node1Id); + node1.clusterDelSlots(5000, 10000); + node1.clusterDelSlots(6000); + node2.clusterDelSlots(6000,1,2,3,4,5,500,5000); + System.out.println(node1.clusterNodes()); + System.out.println(node2.clusterNodes()); + } - @Test + private static void waitForEqualClusterSize() throws InterruptedException { + boolean notEqualSize = true; + while (notEqualSize) { + notEqualSize = getClusterAttribute(node1.clusterInfo(), "cluster_known_nodes") == getClusterAttribute(node2.clusterInfo(), "cluster_size") ? false : true; + } + } + + private static int getClusterAttribute(String clusterInfo, String attributeName) { + for (String infoElement: clusterInfo.split("\n")) { + if (infoElement.contains(attributeName)) { + return Integer.valueOf(infoElement.split(":")[1].trim()); + } + } + return 0; + } + + @Test public void clusterNodes() { String nodes = node1.clusterNodes(); assertTrue(nodes.split("\n").length > 0); From a09a682f0910add7492924c0f0bde314f23f9a1a Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Tue, 14 Jan 2014 16:00:22 -0300 Subject: [PATCH 19/23] Remove sysout prints from test --- .../clients/jedis/tests/commands/ClusterCommandsTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java index 91a0d78..8de59a7 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -41,8 +41,6 @@ public class ClusterCommandsTest extends JedisTestBase { public static void removeSlots() throws InterruptedException { //This is to wait for gossip to replicate data. waitForEqualClusterSize(); - System.out.println(node1.clusterInfo()); - System.out.println(node2.clusterInfo()); String[] nodes = node1.clusterNodes().split("\n"); String node1Id = nodes[0].split(" ")[0]; node1.clusterDelSlots(1,2,3,4,5,500); @@ -50,8 +48,6 @@ public class ClusterCommandsTest extends JedisTestBase { node1.clusterDelSlots(5000, 10000); node1.clusterDelSlots(6000); node2.clusterDelSlots(6000,1,2,3,4,5,500,5000); - System.out.println(node1.clusterNodes()); - System.out.println(node2.clusterNodes()); } private static void waitForEqualClusterSize() throws InterruptedException { From ccf93714e67057a1b8951713ec1986d236e4fe06 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Tue, 14 Jan 2014 17:58:21 -0300 Subject: [PATCH 20/23] Fix ClusterCommand tearDown as gossip may take some time to send data to nodes --- .../clients/jedis/tests/commands/ClusterCommandsTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java index 8de59a7..a7045a6 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; +import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.tests.HostAndPortUtil; import redis.clients.jedis.tests.JedisTestBase; @@ -48,6 +49,11 @@ public class ClusterCommandsTest extends JedisTestBase { node1.clusterDelSlots(5000, 10000); node1.clusterDelSlots(6000); node2.clusterDelSlots(6000,1,2,3,4,5,500,5000); + try { + node2.clusterDelSlots(10000); + } catch (JedisDataException jde) { + //Do nothing, slot may or may not be assigned depending on gossip + } } private static void waitForEqualClusterSize() throws InterruptedException { From 2f9564e1d331a2cfad400eea17fdf4058e98dd48 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Tue, 14 Jan 2014 21:20:33 -0300 Subject: [PATCH 21/23] Variable refactor in JedisClusterTest --- .../redis/clients/jedis/tests/JedisClusterTest.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index d612b5e..08ce912 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -129,10 +129,11 @@ public class JedisClusterTest extends Assert { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode); - node2.clusterDelSlots(JedisClusterCRC16.getSlot("51")); + int slot51 = JedisClusterCRC16.getSlot("51"); + node2.clusterDelSlots(slot51); //TODO: We shouldn't need to issue DELSLOTS in node3, but due to redis-cluster bug we need to. - node3.clusterDelSlots(JedisClusterCRC16.getSlot("51")); - node3.clusterAddSlots(JedisClusterCRC16.getSlot("51")); + node3.clusterDelSlots(slot51); + node3.clusterAddSlots(slot51); waitForClusterReady(); jc.set("51", "foo"); assertEquals("foo", jc.get("51")); @@ -143,8 +144,9 @@ public class JedisClusterTest extends Assert { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode); - node3.clusterSetSlotImporting(JedisClusterCRC16.getSlot("51"), getNodeId(node2.clusterNodes())); - node2.clusterSetSlotMigrating(JedisClusterCRC16.getSlot("51"), getNodeId(node3.clusterNodes())); + int slot51 = JedisClusterCRC16.getSlot("51"); + node3.clusterSetSlotImporting(slot51, getNodeId(node2.clusterNodes())); + node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes())); jc.set("51", "foo"); assertEquals("foo", jc.get("51")); } From 46733c5d5a081d3397e05e3027e89435b7859035 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Thu, 16 Jan 2014 18:04:27 -0300 Subject: [PATCH 22/23] Add test for redis cluster max redirections and refactor JedisClusterCommand exception handling --- .../redis/clients/jedis/JedisCluster.java | 247 +++++++++--------- .../clients/jedis/JedisClusterCommand.java | 32 ++- .../exceptions/JedisAskDataException.java | 24 +- .../JedisClusterMaxRedirectionsException.java | 18 ++ .../exceptions/JedisMovedDataException.java | 24 +- .../exceptions/JedisRedirectionException.java | 37 +++ .../clients/jedis/tests/JedisClusterTest.java | 14 +- 7 files changed, 228 insertions(+), 168 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/exceptions/JedisClusterMaxRedirectionsException.java create mode 100644 src/main/java/redis/clients/jedis/exceptions/JedisRedirectionException.java diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index be33412..e9f4bcd 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -10,23 +10,31 @@ public class JedisCluster implements JedisCommands, BasicCommands { public static final short HASHSLOTS = 16384; private static final int DEFAULT_TIMEOUT = 1; + private static final int DEFAULT_MAX_REDIRECTIONS = 5; + + private int timeout; + private int maxRedirections; private JedisClusterConnectionHandler connectionHandler; public JedisCluster(Set nodes, int timeout) { - connectionHandler = new JedisSlotBasedConnectionHandler(nodes); - + this(nodes, timeout, DEFAULT_MAX_REDIRECTIONS); } public JedisCluster(Set nodes) { this(nodes, DEFAULT_TIMEOUT); } - + + public JedisCluster(Set jedisClusterNode, int timeout, int maxRedirections) { + this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode); + this.timeout = timeout; + this.maxRedirections = maxRedirections; + } @Override public String set(final String key, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().set(key, value); @@ -36,7 +44,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String get(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().get(key); @@ -46,7 +54,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Boolean exists(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Boolean execute() { return connectionHandler.getConnection().exists(key); @@ -56,7 +64,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long persist(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().persist(key); @@ -66,7 +74,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String type(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().type(key); @@ -76,7 +84,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long expire(final String key, final int seconds) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().expire(key, seconds); @@ -86,7 +94,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long expireAt(final String key, final long unixTime) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().expireAt(key, unixTime); @@ -96,7 +104,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long ttl(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().ttl(key); @@ -106,7 +114,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Boolean setbit(final String key, final long offset, final boolean value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Boolean execute() { return connectionHandler.getConnection().setbit(key, offset, value); @@ -116,7 +124,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Boolean setbit(final String key, final long offset, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Boolean execute() { return connectionHandler.getConnection().setbit(key, offset, value); @@ -126,7 +134,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Boolean getbit(final String key, final long offset) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Boolean execute() { return connectionHandler.getConnection().getbit(key, offset); @@ -136,7 +144,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long setrange(final String key, final long offset, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().setrange(key, offset, value); @@ -146,7 +154,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String getrange(final String key, final long startOffset, final long endOffset) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().getrange(key, startOffset, endOffset); @@ -156,7 +164,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String getSet(final String key, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().getSet(key, value); @@ -166,7 +174,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long setnx(final String key, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().setnx(key, value); @@ -176,7 +184,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String setex(final String key, final int seconds, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().setex(key, seconds, value); @@ -186,7 +194,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long decrBy(final String key, final long integer) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().decrBy(key, integer); @@ -196,7 +204,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long decr(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().decr(key); @@ -206,7 +214,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long incrBy(final String key, final long integer) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().incrBy(key, integer); @@ -216,7 +224,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long incr(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().incr(key); @@ -226,7 +234,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long append(final String key, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().append(key, value); @@ -236,7 +244,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String substr(final String key, final int start, final int end) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().substr(key, start, end); @@ -246,7 +254,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long hset(final String key, final String field, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().hset(key, field, value); @@ -256,7 +264,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String hget(final String key, final String field) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().hget(key, field); @@ -266,7 +274,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long hsetnx(final String key, final String field, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().hsetnx(key, field, value); @@ -276,7 +284,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String hmset(final String key, final Map hash) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().hmset(key, hash); @@ -286,7 +294,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public List hmget(final String key, final String... fields) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public List execute() { return connectionHandler.getConnection().hmget(key, fields); @@ -296,7 +304,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long hincrBy(final String key, final String field, final long value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().hincrBy(key, field, value); @@ -306,7 +314,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Boolean hexists(final String key, final String field) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Boolean execute() { return connectionHandler.getConnection().hexists(key, field); @@ -316,7 +324,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long hdel(final String key, final String... field) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().hdel(key, field); @@ -326,7 +334,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long hlen(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().hdel(key); @@ -336,7 +344,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set hkeys(final String key) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().hkeys(key); @@ -346,7 +354,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public List hvals(final String key) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public List execute() { return connectionHandler.getConnection().hvals(key); @@ -356,7 +364,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Map hgetAll(final String key) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Map execute() { return connectionHandler.getConnection().hgetAll(key); @@ -366,7 +374,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long rpush(final String key, final String... string) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().rpush(key, string); @@ -376,7 +384,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long lpush(final String key, final String... string) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().lpush(key, string); @@ -386,7 +394,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long llen(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().llen(key); @@ -396,7 +404,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public List lrange(final String key, final long start, final long end) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public List execute() { return connectionHandler.getConnection().lrange(key, start, end); @@ -406,7 +414,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String ltrim(final String key, final long start, final long end) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().ltrim(key, start, end); @@ -416,7 +424,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String lindex(final String key, final long index) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().lindex(key, index); @@ -426,7 +434,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String lset(final String key, final long index, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().lset(key, index, value); @@ -436,7 +444,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long lrem(final String key, final long count, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().lrem(key, count, value); @@ -446,7 +454,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String lpop(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().lpop(key); @@ -456,7 +464,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String rpop(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().rpop(key); @@ -466,7 +474,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long sadd(final String key, final String... member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().sadd(key, member); @@ -476,7 +484,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set smembers(final String key) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().smembers(key); @@ -486,7 +494,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long srem(final String key, final String... member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().srem(key, member); @@ -496,7 +504,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String spop(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().spop(key); @@ -506,7 +514,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long scard(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().scard(key); @@ -516,7 +524,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Boolean sismember(final String key, final String member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Boolean execute() { return connectionHandler.getConnection().sismember(key, member); @@ -526,7 +534,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String srandmember(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().srandmember(key); @@ -536,7 +544,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long strlen(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().strlen(key); @@ -546,7 +554,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zadd(final String key, final double score, final String member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zadd(key, score, member); @@ -556,7 +564,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zadd(final String key, final Map scoreMembers) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zadd(key, scoreMembers); @@ -566,7 +574,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrange(final String key, final long start, final long end) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrange(key, start, end); @@ -576,7 +584,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zrem(final String key, final String... member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zrem(key, member); @@ -586,7 +594,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Double zincrby(final String key, final double score, final String member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Double execute() { return connectionHandler.getConnection().zincrby(key, score, member); @@ -596,7 +604,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zrank(final String key, final String member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zrank(key, member); @@ -606,7 +614,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zrevrank(final String key, final String member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zrevrank(key, member); @@ -616,7 +624,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrange(final String key, final long start, final long end) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrange(key, start, end); @@ -626,7 +634,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeWithScores(final String key, final long start, final long end) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeWithScores(key, start, end); @@ -636,7 +644,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeWithScores(final String key, final long start, final long end) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeWithScores(key, start, end); @@ -646,7 +654,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zcard(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zcard(key); @@ -656,7 +664,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Double zscore(final String key, final String member) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Double execute() { return connectionHandler.getConnection().zscore(key, member); @@ -666,7 +674,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public List sort(final String key) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public List execute() { return connectionHandler.getConnection().sort(key); @@ -676,7 +684,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public List sort(final String key, final SortingParams sortingParameters) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public List execute() { return connectionHandler.getConnection().sort(key, sortingParameters); @@ -686,7 +694,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zcount(final String key, final double min, final double max) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zcount(key, min, max); @@ -696,7 +704,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zcount(final String key, final String min, final String max) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zcount(key, min, max); @@ -706,7 +714,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScore(final String key, final double min, final double max) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScore(key, min, max); @@ -716,7 +724,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScore(final String key, final String min, final String max) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScore(key, min, max); @@ -726,7 +734,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScore(final String key, final double max, final double min) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScore(key, min, max); @@ -737,7 +745,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScore(final String key, final double min, final double max, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); @@ -747,7 +755,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScore(final String key, final String max, final String min) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScore(key, min, max); @@ -758,7 +766,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScore(final String key, final String min, final String max, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScore(key, min, max, offset, count); @@ -769,7 +777,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScore(final String key, final double max, final double min, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); @@ -779,7 +787,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScoreWithScores(final String key, final double min, final double max) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); @@ -789,7 +797,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScoreWithScores(final String key, final double max, final double min) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); @@ -799,7 +807,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScoreWithScores(final String key, final double min, final double max, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); @@ -810,7 +818,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScore(final String key, final String max, final String min, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScore(key, min, max, offset, count); @@ -820,7 +828,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScoreWithScores(final String key, final String min, final String max) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max); @@ -831,7 +839,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScoreWithScores(final String key, final String max, final String min) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, min, max); @@ -842,7 +850,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrangeByScoreWithScores(final String key, final String min, final String max, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrangeByScoreWithScores(key, min, max, offset, count); @@ -853,7 +861,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScoreWithScores(final String key, final double max, final double min, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); @@ -864,7 +872,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Set zrevrangeByScoreWithScores(final String key, final String max, final String min, final int offset, final int count) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public Set execute() { return connectionHandler.getConnection().zrevrangeByScoreWithScores(key, max, min, offset, count); @@ -874,7 +882,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zremrangeByRank(final String key, final long start, final long end) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zremrangeByRank(key, start, end); @@ -884,7 +892,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zremrangeByScore(final String key, final double start, final double end) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zremrangeByScore(key, start, end); @@ -894,7 +902,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long zremrangeByScore(final String key, final String start, final String end) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().zremrangeByScore(key, start, end); @@ -905,7 +913,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long linsert(final String key, final LIST_POSITION where, final String pivot, final String value) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().linsert(key, where, pivot, value); @@ -915,7 +923,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long lpushx(final String key, final String... string) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().lpushx(key, string); @@ -925,7 +933,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long rpushx(final String key, final String... string) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().rpushx(key, string); @@ -935,7 +943,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public List blpop(final String arg) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public List execute() { return connectionHandler.getConnection().blpop(arg); @@ -945,7 +953,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public List brpop(final String arg) { - return new JedisClusterCommand>(connectionHandler) { + return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override public List execute() { return connectionHandler.getConnection().brpop(arg); @@ -955,7 +963,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long del(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().del(key); @@ -965,7 +973,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String echo(final String string) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().echo(string); @@ -975,7 +983,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long move(final String key, final int dbIndex) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().move(key, dbIndex); @@ -985,7 +993,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long bitcount(final String key) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().bitcount(key); @@ -995,7 +1003,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long bitcount(final String key, final long start, final long end) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().bitcount(key, start, end); @@ -1005,7 +1013,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String ping() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().ping(); @@ -1015,7 +1023,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String quit() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().quit(); @@ -1025,7 +1033,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String flushDB() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().flushDB(); @@ -1035,7 +1043,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long dbSize() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().dbSize(); @@ -1045,7 +1053,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String select(final int index) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().select(index); @@ -1055,7 +1063,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String flushAll() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().flushAll(); @@ -1065,7 +1073,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String auth(final String password) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().auth(password); @@ -1075,7 +1083,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String save() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().save(); @@ -1085,7 +1093,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String bgsave() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().bgsave(); @@ -1095,7 +1103,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String bgrewriteaof() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().bgrewriteaof(); @@ -1105,7 +1113,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long lastsave() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().lastsave(); @@ -1115,7 +1123,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String shutdown() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().shutdown(); @@ -1125,7 +1133,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String info() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().info(); @@ -1135,7 +1143,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String info(final String section) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().info(section); @@ -1145,7 +1153,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String slaveof(final String host, final int port) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().slaveof(host, port); @@ -1155,7 +1163,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String slaveofNoOne() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().slaveofNoOne(); @@ -1165,7 +1173,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public Long getDB() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public Long execute() { return connectionHandler.getConnection().getDB(); @@ -1175,7 +1183,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String debug(final DebugParams params) { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().debug(params); @@ -1185,7 +1193,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { @Override public String configResetStat() { - return new JedisClusterCommand(connectionHandler) { + return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override public String execute() { return connectionHandler.getConnection().configResetStat(); @@ -1197,3 +1205,4 @@ public class JedisCluster implements JedisCommands, BasicCommands { return connectionHandler.getNodes(); } } + diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java index 163b7c4..8596971 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterCommand.java +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -2,7 +2,8 @@ package redis.clients.jedis; import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisClusterException; -import redis.clients.jedis.exceptions.JedisMovedDataException; +import redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException; +import redis.clients.jedis.exceptions.JedisRedirectionException; import redis.clients.util.JedisClusterCRC16; public abstract class JedisClusterCommand { @@ -10,18 +11,25 @@ public abstract class JedisClusterCommand { private boolean asking = false; private JedisClusterConnectionHandler connectionHandler; + private int commandTimeout; + private int redirections; // private boolean asking = false; - public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler) { + public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler, int timeout, int maxRedirections) { this.connectionHandler = connectionHandler; + this.commandTimeout = timeout; + this.redirections = maxRedirections; } public abstract T execute(); public T run(String key) { try { + if (key == null) { throw new JedisClusterException("No way to dispatch this command to Redis Cluster."); + } else if (redirections == 0) { + throw new JedisClusterMaxRedirectionsException("Too many Cluster redirections?"); } connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key)); if (asking) { @@ -29,13 +37,17 @@ public abstract class JedisClusterCommand { connectionHandler.getConnection().asking(); } return execute(); - } catch (JedisMovedDataException jme) { - this.connectionHandler.assignSlotToNode(jme.getSlot(), jme.getTargetNode()); - return run(key); - } catch (JedisAskDataException jae) { - asking = true; - this.connectionHandler.assignSlotToNode(jae.getSlot(), jae.getTargetNode()); - return run(key); + } catch (JedisRedirectionException jre) { + return handleRedirection(jre, key); } } -} + + private T handleRedirection(JedisRedirectionException jre, String key) { + if (jre instanceof JedisAskDataException) { + asking = true; + } + redirections--; + this.connectionHandler.assignSlotToNode(jre.getSlot(), jre.getTargetNode()); + return run(key); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java index 645d5c2..599a779 100644 --- a/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java +++ b/src/main/java/redis/clients/jedis/exceptions/JedisAskDataException.java @@ -2,31 +2,19 @@ package redis.clients.jedis.exceptions; import redis.clients.jedis.HostAndPort; -public class JedisAskDataException extends JedisDataException { +public class JedisAskDataException extends JedisRedirectionException { private static final long serialVersionUID = 3878126572474819403L; - private HostAndPort targetNode; - private int slot; - - public JedisAskDataException(Throwable cause) { - super(cause); + public JedisAskDataException(Throwable cause, HostAndPort targetHost, int slot) { + super(cause, targetHost, slot); } - public JedisAskDataException(String message, Throwable cause) { - super(message, cause); + public JedisAskDataException(String message, Throwable cause, HostAndPort targetHost, int slot) { + super(message, cause, targetHost, slot); } public JedisAskDataException(String message, HostAndPort targetHost, int slot) { - super(message); - this.targetNode = targetHost; - this.slot = slot; + super(message, targetHost, slot); } - public HostAndPort getTargetNode() { - return targetNode; - } - - public int getSlot() { - return slot; - } } diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisClusterMaxRedirectionsException.java b/src/main/java/redis/clients/jedis/exceptions/JedisClusterMaxRedirectionsException.java new file mode 100644 index 0000000..519188b --- /dev/null +++ b/src/main/java/redis/clients/jedis/exceptions/JedisClusterMaxRedirectionsException.java @@ -0,0 +1,18 @@ +package redis.clients.jedis.exceptions; + + +public class JedisClusterMaxRedirectionsException extends JedisDataException { + private static final long serialVersionUID = 3878126572474819403L; + + public JedisClusterMaxRedirectionsException(Throwable cause) { + super(cause); + } + + public JedisClusterMaxRedirectionsException(String message, Throwable cause) { + super(message, cause); + } + + public JedisClusterMaxRedirectionsException(String message) { + super(message); + } +} diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java b/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java index c2b5565..c7a0873 100644 --- a/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java +++ b/src/main/java/redis/clients/jedis/exceptions/JedisMovedDataException.java @@ -3,35 +3,19 @@ package redis.clients.jedis.exceptions; import redis.clients.jedis.HostAndPort; -public class JedisMovedDataException extends JedisDataException { +public class JedisMovedDataException extends JedisRedirectionException { private static final long serialVersionUID = 3878126572474819403L; - private HostAndPort targetNode; - private int slot; public JedisMovedDataException(String message, HostAndPort targetNode, int slot) { - super(message); - this.targetNode = targetNode; - this.slot = slot; + super(message, targetNode, slot); } public JedisMovedDataException(Throwable cause, HostAndPort targetNode, int slot) { - super(cause); - this.targetNode = targetNode; - this.slot = slot; + super(cause, targetNode, slot); } public JedisMovedDataException(String message, Throwable cause, HostAndPort targetNode, int slot) { - super(message, cause); - this.targetNode = targetNode; - this.slot = slot; + super(message, cause, targetNode, slot); } - - public HostAndPort getTargetNode() { - return targetNode; - } - - public int getSlot() { - return slot; - } } diff --git a/src/main/java/redis/clients/jedis/exceptions/JedisRedirectionException.java b/src/main/java/redis/clients/jedis/exceptions/JedisRedirectionException.java new file mode 100644 index 0000000..65969f3 --- /dev/null +++ b/src/main/java/redis/clients/jedis/exceptions/JedisRedirectionException.java @@ -0,0 +1,37 @@ +package redis.clients.jedis.exceptions; + +import redis.clients.jedis.HostAndPort; + + +public class JedisRedirectionException extends JedisDataException { + private static final long serialVersionUID = 3878126572474819403L; + + private HostAndPort targetNode; + private int slot; + + public JedisRedirectionException(String message, HostAndPort targetNode, int slot) { + super(message); + this.targetNode = targetNode; + this.slot = slot; + } + + public JedisRedirectionException(Throwable cause, HostAndPort targetNode, int slot) { + super(cause); + this.targetNode = targetNode; + this.slot = slot; + } + + public JedisRedirectionException(String message, Throwable cause, HostAndPort targetNode, int slot) { + super(message, cause); + this.targetNode = targetNode; + this.slot = slot; + } + + public HostAndPort getTargetNode() { + return targetNode; + } + + public int getSlot() { + return slot; + } +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 08ce912..794f624 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -14,6 +14,7 @@ import redis.clients.jedis.JedisCluster; import redis.clients.jedis.Pipeline; import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisClusterException; +import redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException; import redis.clients.jedis.exceptions.JedisMovedDataException; import redis.clients.util.JedisClusterCRC16; @@ -131,9 +132,9 @@ public class JedisClusterTest extends Assert { JedisCluster jc = new JedisCluster(jedisClusterNode); int slot51 = JedisClusterCRC16.getSlot("51"); node2.clusterDelSlots(slot51); - //TODO: We shouldn't need to issue DELSLOTS in node3, but due to redis-cluster bug we need to. node3.clusterDelSlots(slot51); node3.clusterAddSlots(slot51); + waitForClusterReady(); jc.set("51", "foo"); assertEquals("foo", jc.get("51")); @@ -159,6 +160,17 @@ public class JedisClusterTest extends Assert { jc.ping(); } + @Test(expected=JedisClusterMaxRedirectionsException.class) + public void testRedisClusterMaxRedirections() { + Set jedisClusterNode = new HashSet(); + jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); + JedisCluster jc = new JedisCluster(jedisClusterNode); + int slot51 = JedisClusterCRC16.getSlot("51"); + //This will cause an infinite redirection loop + node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes())); + jc.set("51", "foo"); + } + private String getNodeId(String infoOutput) { for (String infoLine : infoOutput.split("\n")) { if (infoLine.contains("myself")) { From 6c8d7a5045e3e8b34944067fe769cacde3be2604 Mon Sep 17 00:00:00 2001 From: Marcos Nils Date: Thu, 16 Jan 2014 18:14:55 -0300 Subject: [PATCH 23/23] Add fail in case test doesn't throw any exception --- src/test/java/redis/clients/jedis/tests/JedisClusterTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index 794f624..ef48a21 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -96,6 +96,7 @@ public class JedisClusterTest extends Assert { assertEquals(12182, jme.getSlot()); assertEquals(new HostAndPort("127.0.0.1", 7381), jme.getTargetNode()); } + fail(); } @Test(expected=JedisAskDataException.class)