Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SENTINEL commands

  • Loading branch information...
commit 69b5291f80762e737e2d30a5b3b87dbff1d81056 1 parent 2058231
@hamsterready hamsterready authored
View
5 src/main/java/redis/clients/jedis/BinaryClient.java
@@ -807,4 +807,9 @@ public void objectIdletime(byte[] key) {
public void objectEncoding(byte[] key) {
sendCommand(OBJECT, ENCODING.raw, key);
}
+
+ public void sentinel(final byte[]... args) {
+ sendCommand(SENTINEL, args);
+ }
+
}
View
14 src/main/java/redis/clients/jedis/Client.java
@@ -703,4 +703,18 @@ public void objectIdletime(String key) {
public void objectEncoding(String key) {
objectEncoding(SafeEncoder.encode(key));
}
+
+
+ public void sentinel(final String... args) {
+ final byte[][] arg = new byte[args.length][];
+ for (int i = 0; i < arg.length; i++) {
+ arg[i] = SafeEncoder.encode(args[i]);
+ }
+ sentinel(arg);
+ }
+
+ public void sentinel(final String cmd, String arg1, int arg2) {
+ sentinel(SafeEncoder.encode(cmd), SafeEncoder.encode(arg1), toByteArray(arg2));
+ }
+
}
View
139 src/main/java/redis/clients/jedis/Jedis.java
@@ -2,6 +2,7 @@
import java.net.URI;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
@@ -2873,4 +2874,142 @@ public Long objectIdletime(String string) {
client.objectIdletime(string);
return client.getIntegerReply();
}
+
+ /**
+ * <pre>
+ * redis 127.0.0.1:26381> sentinel masters
+ * 1) 1) "name"
+ * 2) "mymaster"
+ * 3) "ip"
+ * 4) "127.0.0.1"
+ * 5) "port"
+ * 6) "6379"
+ * 7) "runid"
+ * 8) "93d4d4e6e9c06d0eea36e27f31924ac26576081d"
+ * 9) "flags"
+ * 10) "master"
+ * 11) "pending-commands"
+ * 12) "0"
+ * 13) "last-ok-ping-reply"
+ * 14) "423"
+ * 15) "last-ping-reply"
+ * 16) "423"
+ * 17) "info-refresh"
+ * 18) "6107"
+ * 19) "num-slaves"
+ * 20) "1"
+ * 21) "num-other-sentinels"
+ * 22) "2"
+ * 23) "quorum"
+ * 24) "2"
+ *
+ * </pre>
+ *
+ * @return
+ */
+ public List<Map<String, String>> sentinelMasters() {
+ client.sentinel(Protocol.SENTINEL_MASTERS);
+ final List<Object> reply = client.getObjectMultiBulkReply();
+
+ final List<Map<String,String>> masters = new ArrayList<Map<String,String>>();
+ for (Object obj : reply) {
+ masters.add(BuilderFactory.STRING_MAP.build((List) obj));
+ }
+ return masters;
+ }
+
+
+ /**
+ * <pre>
+ * redis 127.0.0.1:26381> sentinel get-master-addr-by-name mymaster
+ * 1) "127.0.0.1"
+ * 2) "6379"
+ * </pre>
+ *
+ * @param masterName
+ * @return two elements list of strings : host and port.
+ */
+ public List<String> sentinelGetMasterAddrByName(String masterName) {
+ client.sentinel(Protocol.SENTINEL_GET_MASTER_ADDR_BY_NAME, masterName);
+ final List<Object> reply = client.getObjectMultiBulkReply();
+ return BuilderFactory.STRING_LIST.build(reply);
+ }
+
+ /**
+ * <pre>
+ * redis 127.0.0.1:26381> sentinel reset mymaster
+ * (integer) 1
+ * </pre>
+ *
+ * @param pattern
+ * @return
+ */
+ public Long sentinelReset(String pattern) {
+ client.sentinel(Protocol.SENTINEL_RESET, pattern);
+ return client.getIntegerReply();
+ }
+
+ /**
+ * <pre>
+ * redis 127.0.0.1:26381> sentinel slaves mymaster
+ * 1) 1) "name"
+ * 2) "127.0.0.1:6380"
+ * 3) "ip"
+ * 4) "127.0.0.1"
+ * 5) "port"
+ * 6) "6380"
+ * 7) "runid"
+ * 8) "d7f6c0ca7572df9d2f33713df0dbf8c72da7c039"
+ * 9) "flags"
+ * 10) "slave"
+ * 11) "pending-commands"
+ * 12) "0"
+ * 13) "last-ok-ping-reply"
+ * 14) "47"
+ * 15) "last-ping-reply"
+ * 16) "47"
+ * 17) "info-refresh"
+ * 18) "657"
+ * 19) "master-link-down-time"
+ * 20) "0"
+ * 21) "master-link-status"
+ * 22) "ok"
+ * 23) "master-host"
+ * 24) "localhost"
+ * 25) "master-port"
+ * 26) "6379"
+ * 27) "slave-priority"
+ * 28) "100"
+ * </pre>
+ *
+ * @param masterName
+ * @return
+ */
+ public List<Map<String, String>> sentinelSlaves(String masterName) {
+ client.sentinel(Protocol.SENTINEL_SLAVES, masterName);
+ final List<Object> reply = client.getObjectMultiBulkReply();
+
+ final List<Map<String,String>> slaves = new ArrayList<Map<String,String>>();
+ for (Object obj : reply) {
+ slaves.add(BuilderFactory.STRING_MAP.build((List) obj));
+ }
+ return slaves;
+ }
+
+ /**
+ * <pre>
+ * redis 127.0.0.1:26381> SENTINEL is-master-down-by-addr 127.0.0.1 1
+ * 1) (integer) 0
+ * 2) "?"
+ * redis 127.0.0.1:26381> SENTINEL is-master-down-by-addr 127.0.0.1 6379
+ * 1) (integer) 0
+ * 2) "aaef11fbb2712346a386078c7f9834e72ed51e96"
+ * </pre>
+ * @return Long followed by the String (runid)
+ */
+ public List<? extends Object> sentinelIsMasterDownByAddr(String host, int port) {
+ client.sentinel(Protocol.SENTINEL_IS_MASTER_DOWN_BY_ADDR, host, port);
+ final List<Object> reply = client.getObjectMultiBulkReply();
+ return Arrays.asList(BuilderFactory.LONG.build(reply.get(0)), BuilderFactory.STRING.build(reply.get(1)));
+ }
}
View
8 src/main/java/redis/clients/jedis/Protocol.java
@@ -24,6 +24,12 @@
public static final byte MINUS_BYTE = '-';
public static final byte COLON_BYTE = ':';
+ public static final String SENTINEL_MASTERS = "masters";
+ public static final String SENTINEL_GET_MASTER_ADDR_BY_NAME = "get-master-addr-by-name";
+ public static final String SENTINEL_RESET = "reset";
+ public static final String SENTINEL_SLAVES = "slaves";
+ public static final String SENTINEL_IS_MASTER_DOWN_BY_ADDR = "is-master-down-by-addr";
+
private Protocol() {
// this prevent the class from instantiation
}
@@ -144,7 +150,7 @@ public static Object read(final RedisInputStream is) {
}
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;
+ 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, SENTINEL;
public final byte[] raw;
View
47 src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java
@@ -0,0 +1,47 @@
+package redis.clients.jedis.tests;
+
+import static junit.framework.Assert.*;
+
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+
+import redis.clients.jedis.Jedis;
+
+public class JedisSentinelTest {
+
+ private static final String MASTER_NAME = "mymaster";
+
+ /**
+ * Based on redis/master/slave/sentinel configs from
+ * https://github.com/noise/redis-sentinel-tests.
+ */
+ @Test
+ public void sentinel() {
+ Jedis j = new Jedis("localhost", 26379);
+ List<Map<String, String>> masters = j.sentinelMasters();
+ final String masterName = masters.get(0).get("name");
+
+ assertEquals(MASTER_NAME, masterName);
+
+ List<String> masterHostAndPort = j.sentinelGetMasterAddrByName(masterName);
+ assertEquals("127.0.0.1", masterHostAndPort.get(0));
+ assertEquals("6379", masterHostAndPort.get(1));
+
+ List<Map<String, String>> slaves = j.sentinelSlaves(masterName);
+ assertEquals("6379", slaves.get(0).get("master-port"));
+
+ List<? extends Object> isMasterDownByAddr = j.sentinelIsMasterDownByAddr("127.0.0.1", 6379);
+ assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
+ assertFalse("?".equals(isMasterDownByAddr.get(1)));
+
+ isMasterDownByAddr = j.sentinelIsMasterDownByAddr("127.0.0.1", 1);
+ assertEquals(Long.valueOf(0), (Long) isMasterDownByAddr.get(0));
+ assertTrue("?".equals(isMasterDownByAddr.get(1)));
+
+ // DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
+ assertEquals(Long.valueOf(1), j.sentinelReset(masterName));
+ assertEquals(Long.valueOf(0), j.sentinelReset("woof" + masterName));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.