Skip to content

Commit e7e2bfa

Browse files
author
Jonathan Leibiusky
committed
Launch 3 nodes on ports 7379 to 7381 with cluster enabled.
Add command CLUSTER NODES
1 parent 15891c4 commit e7e2bfa

File tree

8 files changed

+203
-70
lines changed

8 files changed

+203
-70
lines changed

Makefile

+47
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ save ""
6161
appendonly no
6262
endef
6363

64+
# SENTINELS
6465
define REDIS_SENTINEL1
6566
port 26379
6667
daemonize yes
@@ -97,6 +98,40 @@ pidfile /tmp/sentinel3.pid
9798
logfile /tmp/sentinel3.log
9899
endef
99100

101+
# CLUSTER REDIS NODES
102+
define REDIS_CLUSTER_NODE1_CONF
103+
daemonize yes
104+
port 7379
105+
pidfile /tmp/redis_cluster_node1.pid
106+
logfile /tmp/redis_cluster_node1.log
107+
save ""
108+
appendonly no
109+
cluster-enabled yes
110+
cluster-config-file /tmp/redis_cluster_node1.conf
111+
endef
112+
113+
define REDIS_CLUSTER_NODE2_CONF
114+
daemonize yes
115+
port 7380
116+
pidfile /tmp/redis_cluster_node2.pid
117+
logfile /tmp/redis_cluster_node2.log
118+
save ""
119+
appendonly no
120+
cluster-enabled yes
121+
cluster-config-file /tmp/redis_cluster_node2.conf
122+
endef
123+
124+
define REDIS_CLUSTER_NODE3_CONF
125+
daemonize yes
126+
port 7381
127+
pidfile /tmp/redis_cluster_node3.pid
128+
logfile /tmp/redis_cluster_node3.log
129+
save ""
130+
appendonly no
131+
cluster-enabled yes
132+
cluster-config-file /tmp/redis_cluster_node3.conf
133+
endef
134+
100135
export REDIS1_CONF
101136
export REDIS2_CONF
102137
export REDIS3_CONF
@@ -106,6 +141,9 @@ export REDIS6_CONF
106141
export REDIS_SENTINEL1
107142
export REDIS_SENTINEL2
108143
export REDIS_SENTINEL3
144+
export REDIS_CLUSTER_NODE1_CONF
145+
export REDIS_CLUSTER_NODE2_CONF
146+
export REDIS_CLUSTER_NODE3_CONF
109147

110148
start:
111149
echo "$$REDIS1_CONF" | redis-server -
@@ -119,6 +157,9 @@ start:
119157
echo "$$REDIS_SENTINEL2" > /tmp/sentinel2.conf && redis-server /tmp/sentinel2.conf --sentinel
120158
@sleep 0.5
121159
echo "$$REDIS_SENTINEL3" > /tmp/sentinel3.conf && redis-server /tmp/sentinel3.conf --sentinel
160+
echo "$$REDIS_CLUSTER_NODE1_CONF" | redis-server -
161+
echo "$$REDIS_CLUSTER_NODE2_CONF" | redis-server -
162+
echo "$$REDIS_CLUSTER_NODE3_CONF" | redis-server -
122163

123164
stop:
124165
kill `cat /tmp/redis1.pid`
@@ -131,6 +172,12 @@ stop:
131172
kill `cat /tmp/sentinel1.pid`
132173
kill `cat /tmp/sentinel2.pid`
133174
kill `cat /tmp/sentinel3.pid`
175+
kill `cat /tmp/redis_cluster_node1.pid` || true
176+
kill `cat /tmp/redis_cluster_node2.pid` || true
177+
kill `cat /tmp/redis_cluster_node3.pid` || true
178+
rm -f /tmp/redis_cluster_node1.conf
179+
rm -f /tmp/redis_cluster_node2.conf
180+
rm -f /tmp/redis_cluster_node3.conf
134181

135182
test:
136183
make start

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<properties>
4848
<redis-hosts>localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384</redis-hosts>
4949
<sentinel-hosts>localhost:26379,localhost:26380,localhost:26381</sentinel-hosts>
50+
<cluster-hosts>localhost:7379,localhost:7380,localhost:7381</cluster-hosts>
5051
<github.global.server>github</github.global.server>
5152
</properties>
5253

src/main/java/redis/clients/jedis/BinaryClient.java

+5
Original file line numberDiff line numberDiff line change
@@ -1100,4 +1100,9 @@ public void migrate(final byte[] host, final int port, final byte[] key, final i
11001100
public void hincrByFloat(final byte[] key, final byte[] field, double increment) {
11011101
sendCommand(HINCRBYFLOAT, key, field, toByteArray(increment));
11021102
}
1103+
1104+
public void cluster(final byte[]... args) {
1105+
sendCommand(CLUSTER, args);
1106+
}
1107+
11031108
}

src/main/java/redis/clients/jedis/Client.java

+8
Original file line numberDiff line numberDiff line change
@@ -807,4 +807,12 @@ public void migrate(final String host, final int port, final String key, final i
807807
public void hincrByFloat(final String key, final String field, double increment) {
808808
hincrByFloat(SafeEncoder.encode(key), SafeEncoder.encode(field), increment);
809809
}
810+
811+
public void cluster(final String... args) {
812+
final byte[][] arg = new byte[args.length][];
813+
for (int i = 0; i < arg.length; i++) {
814+
arg[i] = SafeEncoder.encode(args[i]);
815+
}
816+
cluster(arg);
817+
}
810818
}

src/main/java/redis/clients/jedis/Jedis.java

+6
Original file line numberDiff line numberDiff line change
@@ -3076,4 +3076,10 @@ public Double hincrByFloat(final String key, final String field, double incremen
30763076
String relpy = client.getBulkReply();
30773077
return (relpy != null ? new Double(relpy) : null);
30783078
}
3079+
3080+
public String clusterNodes() {
3081+
checkIsInMulti();
3082+
client.cluster(Protocol.CLUSTER_NODES);
3083+
return client.getBulkReply();
3084+
}
30793085
}

src/main/java/redis/clients/jedis/Protocol.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public final class Protocol {
3030
public static final String SENTINEL_RESET = "reset";
3131
public static final String SENTINEL_SLAVES = "slaves";
3232

33+
public static final String CLUSTER_NODES = "nodes";
34+
3335
private Protocol() {
3436
// this prevent the class from instantiation
3537
}
@@ -155,7 +157,7 @@ public static final byte[] toByteArray(final double value) {
155157

156158
public static enum Command {
157159
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,
158-
DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT;
160+
DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT, PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT, CLUSTER;
159161

160162
public final byte[] raw;
161163

src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java

+92-69
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,106 @@
99
public class HostAndPortUtil {
1010
private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPort>();
1111
private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPort>();
12+
private static List<HostAndPort> clusterHostAndPortList = new ArrayList<HostAndPort>();
1213

1314
static {
14-
15-
HostAndPort defaulthnp1 = new HostAndPort("localhost", Protocol.DEFAULT_PORT);
16-
redisHostAndPortList.add(defaulthnp1);
17-
18-
HostAndPort defaulthnp2 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 1);
19-
redisHostAndPortList.add(defaulthnp2);
20-
21-
HostAndPort defaulthnp3 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 2);
22-
redisHostAndPortList.add(defaulthnp3);
23-
24-
HostAndPort defaulthnp4 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 3);
25-
redisHostAndPortList.add(defaulthnp4);
26-
27-
HostAndPort defaulthnp5 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4);
28-
redisHostAndPortList.add(defaulthnp5);
29-
30-
HostAndPort defaulthnp6 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 5);
31-
redisHostAndPortList.add(defaulthnp6);
32-
33-
HostAndPort defaulthnp7 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT);
34-
sentinelHostAndPortList.add(defaulthnp7);
35-
36-
HostAndPort defaulthnp8 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1);
37-
sentinelHostAndPortList.add(defaulthnp8);
38-
39-
HostAndPort defaulthnp9 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 2);
40-
sentinelHostAndPortList.add(defaulthnp9);
41-
42-
String envRedisHosts = System.getProperty("redis-hosts");
43-
String envSentinelHosts = System.getProperty("sentinel-hosts");
44-
45-
redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList);
46-
sentinelHostAndPortList = parseHosts(envSentinelHosts, sentinelHostAndPortList);
15+
16+
HostAndPort defaulthnp1 = new HostAndPort("localhost",
17+
Protocol.DEFAULT_PORT);
18+
redisHostAndPortList.add(defaulthnp1);
19+
20+
HostAndPort defaulthnp2 = new HostAndPort("localhost",
21+
Protocol.DEFAULT_PORT + 1);
22+
redisHostAndPortList.add(defaulthnp2);
23+
24+
HostAndPort defaulthnp3 = new HostAndPort("localhost",
25+
Protocol.DEFAULT_PORT + 2);
26+
redisHostAndPortList.add(defaulthnp3);
27+
28+
HostAndPort defaulthnp4 = new HostAndPort("localhost",
29+
Protocol.DEFAULT_PORT + 3);
30+
redisHostAndPortList.add(defaulthnp4);
31+
32+
HostAndPort defaulthnp5 = new HostAndPort("localhost",
33+
Protocol.DEFAULT_PORT + 4);
34+
redisHostAndPortList.add(defaulthnp5);
35+
36+
HostAndPort defaulthnp6 = new HostAndPort("localhost",
37+
Protocol.DEFAULT_PORT + 5);
38+
redisHostAndPortList.add(defaulthnp6);
39+
40+
HostAndPort defaulthnp7 = new HostAndPort("localhost",
41+
Protocol.DEFAULT_SENTINEL_PORT);
42+
sentinelHostAndPortList.add(defaulthnp7);
43+
44+
HostAndPort defaulthnp8 = new HostAndPort("localhost",
45+
Protocol.DEFAULT_SENTINEL_PORT + 1);
46+
sentinelHostAndPortList.add(defaulthnp8);
47+
48+
HostAndPort defaulthnp9 = new HostAndPort("localhost",
49+
Protocol.DEFAULT_SENTINEL_PORT + 2);
50+
sentinelHostAndPortList.add(defaulthnp9);
51+
52+
clusterHostAndPortList.add(new HostAndPort("localhost", 7379));
53+
clusterHostAndPortList.add(new HostAndPort("localhost", 7380));
54+
clusterHostAndPortList.add(new HostAndPort("localhost", 7381));
55+
56+
String envRedisHosts = System.getProperty("redis-hosts");
57+
String envSentinelHosts = System.getProperty("sentinel-hosts");
58+
String envClusterHosts = System.getProperty("cluster-hosts");
59+
60+
redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList);
61+
sentinelHostAndPortList = parseHosts(envSentinelHosts,
62+
sentinelHostAndPortList);
63+
clusterHostAndPortList = parseHosts(envClusterHosts,
64+
clusterHostAndPortList);
4765
}
4866

49-
public static List<HostAndPort> parseHosts(String envHosts, List<HostAndPort> existingHostsAndPorts) {
50-
51-
if (null != envHosts && 0 < envHosts.length()) {
52-
53-
String[] hostDefs = envHosts.split(",");
54-
55-
if (null != hostDefs && 2 <= hostDefs.length) {
56-
57-
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPort>(hostDefs.length);
58-
59-
for (String hostDef : hostDefs) {
60-
61-
String[] hostAndPort = hostDef.split(":");
62-
63-
if (null != hostAndPort && 2 == hostAndPort.length) {
64-
String host = hostAndPort[0];
65-
int port = Protocol.DEFAULT_PORT;
66-
67-
try {
68-
port = Integer.parseInt(hostAndPort[1]);
69-
} catch (final NumberFormatException nfe) {
70-
}
71-
72-
envHostsAndPorts.add(new HostAndPort(host, port));
73-
}
74-
}
75-
76-
return envHostsAndPorts;
77-
}
78-
}
79-
80-
return existingHostsAndPorts;
67+
public static List<HostAndPort> parseHosts(String envHosts,
68+
List<HostAndPort> existingHostsAndPorts) {
69+
70+
if (null != envHosts && 0 < envHosts.length()) {
71+
72+
String[] hostDefs = envHosts.split(",");
73+
74+
if (null != hostDefs && 2 <= hostDefs.length) {
75+
76+
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPort>(
77+
hostDefs.length);
78+
79+
for (String hostDef : hostDefs) {
80+
81+
String[] hostAndPort = hostDef.split(":");
82+
83+
if (null != hostAndPort && 2 == hostAndPort.length) {
84+
String host = hostAndPort[0];
85+
int port = Protocol.DEFAULT_PORT;
86+
87+
try {
88+
port = Integer.parseInt(hostAndPort[1]);
89+
} catch (final NumberFormatException nfe) {
90+
}
91+
92+
envHostsAndPorts.add(new HostAndPort(host, port));
93+
}
94+
}
95+
96+
return envHostsAndPorts;
97+
}
98+
}
99+
100+
return existingHostsAndPorts;
81101
}
82-
102+
83103
public static List<HostAndPort> getRedisServers() {
84-
return redisHostAndPortList;
104+
return redisHostAndPortList;
85105
}
86-
106+
87107
public static List<HostAndPort> getSentinelServers() {
88-
return sentinelHostAndPortList;
108+
return sentinelHostAndPortList;
89109
}
90110

111+
public static List<HostAndPort> getClusterServers() {
112+
return clusterHostAndPortList;
113+
}
91114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package redis.clients.jedis.tests.commands;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import redis.clients.jedis.HostAndPort;
8+
import redis.clients.jedis.Jedis;
9+
import redis.clients.jedis.tests.HostAndPortUtil;
10+
import redis.clients.jedis.tests.JedisTestBase;
11+
12+
public class ClusterCommandsTest extends JedisTestBase {
13+
private Jedis node1;
14+
private Jedis node2;
15+
16+
@Before
17+
public void setUp() throws Exception {
18+
HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0);
19+
HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1);
20+
21+
node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
22+
node1.connect();
23+
node1.flushAll();
24+
25+
node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
26+
node2.connect();
27+
node2.flushAll();
28+
}
29+
30+
@After
31+
public void tearDown() {
32+
node1.disconnect();
33+
node2.disconnect();
34+
}
35+
36+
@Test
37+
public void clusterNodes() {
38+
String nodes = node1.clusterNodes();
39+
assertEquals(1, nodes.split("\n").length);
40+
}
41+
}

0 commit comments

Comments
 (0)