diff --git a/Makefile b/Makefile
index a6e9912..cf4751f 100644
--- a/Makefile
+++ b/Makefile
@@ -65,6 +65,7 @@ appendonly no
slaveof localhost 6379
endef
+# SENTINELS
define REDIS_SENTINEL1
port 26379
daemonize yes
@@ -101,6 +102,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
@@ -110,8 +145,11 @@ 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:
+start: cleanup
echo "$$REDIS1_CONF" | redis-server -
echo "$$REDIS2_CONF" | redis-server -
echo "$$REDIS3_CONF" | redis-server -
@@ -123,6 +161,12 @@ 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 -
+
+cleanup:
+ rm -vf /tmp/redis_cluster_node*.conf
stop:
kill `cat /tmp/redis1.pid`
@@ -135,10 +179,16 @@ 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
- mvn clean compile test
+ mvn -Dtest=${TEST} clean compile test
make stop
deploy:
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 dff1e76..f1b167a 100644
--- a/src/main/java/redis/clients/jedis/BinaryClient.java
+++ b/src/main/java/redis/clients/jedis/BinaryClient.java
@@ -1135,4 +1135,11 @@ public class BinaryClient extends Connection {
public void waitReplicas(int replicas, long timeout) {
sendCommand(WAIT, toByteArray(replicas), toByteArray(timeout));
}
+
+ public void cluster(final byte[]... args) {
+ sendCommand(CLUSTER, args);
+ }
+ public void asking() {
+ sendCommand(Command.ASKING);
+ }
}
diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java
index 92a99a2..f08633f 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/Client.java b/src/main/java/redis/clients/jedis/Client.java
index c5a072b..1353a1b 100644
--- a/src/main/java/redis/clients/jedis/Client.java
+++ b/src/main/java/redis/clients/jedis/Client.java
@@ -820,4 +820,65 @@ public class Client extends BinaryClient implements Commands {
public void zscan(final String key, int cursor, final ScanParams params) {
zscan(SafeEncoder.encode(key), cursor, params);
}
+
+ 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);
+ }
+
+ 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);
+ }
}
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 4ef5c4f..f5f6bdd 100644
--- a/src/main/java/redis/clients/jedis/Jedis.java
+++ b/src/main/java/redis/clients/jedis/Jedis.java
@@ -13,8 +13,10 @@ import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.SafeEncoder;
import redis.clients.util.Slowlog;
+import java.net.URI;
+import java.util.*;
-public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands, BasicCommands {
+public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands, BasicCommands, ClusterCommands {
public Jedis(final String host) {
super(host);
}
@@ -3153,4 +3155,63 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand
}
return new ScanResult(newcursor, results);
}
+ public String clusterNodes() {
+ checkIsInMulti();
+ client.clusterNodes();
+ return client.getBulkReply();
+ }
+
+ public String clusterMeet(final String ip, final int port) {
+ checkIsInMulti();
+ client.clusterMeet(ip, port);
+ return client.getStatusCodeReply();
+ }
+
+ public String clusterAddSlots(final int ...slots) {
+ checkIsInMulti();
+ client.clusterAddSlots(slots);
+ return client.getStatusCodeReply();
+ }
+
+ public String clusterDelSlots(final int ...slots) {
+ checkIsInMulti();
+ client.clusterDelSlots(slots);
+ return client.getStatusCodeReply();
+ }
+
+ public String clusterInfo() {
+ checkIsInMulti();
+ client.clusterInfo();
+ return client.getStatusCodeReply();
+ }
+
+ public List clusterGetKeysInSlot(final int slot, final int count) {
+ checkIsInMulti();
+ client.clusterGetKeysInSlot(slot, count);
+ return client.getMultiBulkReply();
+ }
+
+ public String clusterSetSlotNode(final int slot, final String nodeId) {
+ checkIsInMulti();
+ client.clusterSetSlotNode(slot, nodeId);
+ return client.getStatusCodeReply();
+ }
+
+ public String clusterSetSlotMigrating(final int slot, final String nodeId) {
+ checkIsInMulti();
+ client.clusterSetSlotMigrating(slot, nodeId);
+ return client.getStatusCodeReply();
+ }
+
+ public String clusterSetSlotImporting(final int slot, final String nodeId) {
+ checkIsInMulti();
+ 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
new file mode 100644
index 0000000..ed60b5a
--- /dev/null
+++ b/src/main/java/redis/clients/jedis/JedisCluster.java
@@ -0,0 +1,1432 @@
+package redis.clients.jedis;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import redis.clients.jedis.BinaryClient.LIST_POSITION;
+
+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) {
+ 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, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().set(key, value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String get(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().get(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Boolean exists(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Boolean execute() {
+ return connectionHandler.getConnection().exists(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long persist(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().persist(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String type(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().type(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long expire(final String key, final int seconds) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().expire(key, seconds);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long expireAt(final String key, final long unixTime) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection()
+ .expireAt(key, unixTime);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long ttl(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().ttl(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Boolean setbit(final String key, final long offset,
+ final boolean value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Boolean execute() {
+ return connectionHandler.getConnection().setbit(key, offset,
+ value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Boolean setbit(final String key, final long offset,
+ final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Boolean execute() {
+ return connectionHandler.getConnection().setbit(key, offset,
+ value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Boolean getbit(final String key, final long offset) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Boolean execute() {
+ return connectionHandler.getConnection().getbit(key, offset);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long setrange(final String key, final long offset, final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().setrange(key, offset,
+ value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String getrange(final String key, final long startOffset,
+ final long endOffset) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().getrange(key,
+ startOffset, endOffset);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String getSet(final String key, final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().getSet(key, value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long setnx(final String key, final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().setnx(key, value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String setex(final String key, final int seconds, final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().setex(key, seconds,
+ value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long decrBy(final String key, final long integer) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().decrBy(key, integer);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long decr(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().decr(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long incrBy(final String key, final long integer) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().incrBy(key, integer);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long incr(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().incr(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long append(final String key, final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().append(key, value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String substr(final String key, final int start, final int end) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection()
+ .substr(key, start, end);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long hset(final String key, final String field, final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection()
+ .hset(key, field, value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String hget(final String key, final String field) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().hget(key, field);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long hsetnx(final String key, final String field, final String value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().hsetnx(key, field,
+ value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public String hmset(final String key, final Map hash) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public String execute() {
+ return connectionHandler.getConnection().hmset(key, hash);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public List hmget(final String key, final String... fields) {
+ return new JedisClusterCommand>(connectionHandler,
+ timeout, maxRedirections) {
+ @Override
+ public List execute() {
+ return connectionHandler.getConnection().hmget(key, fields);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long hincrBy(final String key, final String field, final long value) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().hincrBy(key, field,
+ value);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Boolean hexists(final String key, final String field) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Boolean execute() {
+ return connectionHandler.getConnection().hexists(key, field);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long hdel(final String key, final String... field) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().hdel(key, field);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Long hlen(final String key) {
+ return new JedisClusterCommand(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Long execute() {
+ return connectionHandler.getConnection().hdel(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Set hkeys(final String key) {
+ return new JedisClusterCommand>(connectionHandler, timeout,
+ maxRedirections) {
+ @Override
+ public Set execute() {
+ return connectionHandler.getConnection().hkeys(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public List hvals(final String key) {
+ return new JedisClusterCommand>(connectionHandler,
+ timeout, maxRedirections) {
+ @Override
+ public List execute() {
+ return connectionHandler.getConnection().hvals(key);
+ }
+ }.run(key);
+ }
+
+ @Override
+ public Map hgetAll(final String key) {
+ return new JedisClusterCommand