From a50cf3b15e36c78023baf85894c721fc612f041e Mon Sep 17 00:00:00 2001 From: Jungtaek Lim Date: Wed, 22 Jan 2014 00:23:40 +0900 Subject: [PATCH 1/5] Apply Sentinel runtime configuration API introduced on Redis 2.8.4 * Implements new sentinel commands (failover, monitor, remove, set) * unit test included ** added 2 redis-server and 1 sentinel for failover test * with some refactoring ** SentinelCommands : refactor to have interface ** HostAndPortUtil : same format to cluster setup --- Makefile | 50 +++++++ pom.xml | 4 +- src/main/java/redis/clients/jedis/Jedis.java | 38 ++++- .../java/redis/clients/jedis/Protocol.java | 4 + .../redis/clients/jedis/SentinelCommands.java | 22 +++ .../clients/jedis/tests/HostAndPortUtil.java | 48 ++----- .../jedis/tests/JedisSentinelTest.java | 136 +++++++++++++++--- .../tests/utils/JedisSentinelTestUtil.java | 7 +- 8 files changed, 244 insertions(+), 65 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/SentinelCommands.java diff --git a/Makefile b/Makefile index e00fb83160..b1d4507f8b 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,29 @@ appendonly no slaveof localhost 6379 endef +define REDIS7_CONF +daemonize yes +port 6385 +requirepass foobared +masterauth foobared +pidfile /tmp/redis7.pid +logfile /tmp/redis7.log +save "" +appendonly no +endef + +define REDIS8_CONF +daemonize yes +port 6386 +requirepass foobared +masterauth foobared +pidfile /tmp/redis8.pid +logfile /tmp/redis8.log +save "" +appendonly no +slaveof localhost 6385 +endef + # SENTINELS define REDIS_SENTINEL1 port 26379 @@ -102,6 +125,18 @@ pidfile /tmp/sentinel3.pid logfile /tmp/sentinel3.log endef +define REDIS_SENTINEL4 +port 26382 +daemonize yes +sentinel monitor mymasterfailover 127.0.0.1 6385 1 +sentinel auth-pass mymasterfailover foobared +sentinel down-after-milliseconds mymasterfailover 3000 +sentinel failover-timeout mymasterfailover 900000 +sentinel parallel-syncs mymasterfailover 1 +pidfile /tmp/sentinel4.pid +logfile /tmp/sentinel4.log +endef + # CLUSTER REDIS NODES define REDIS_CLUSTER_NODE1_CONF daemonize yes @@ -142,9 +177,12 @@ export REDIS3_CONF export REDIS4_CONF export REDIS5_CONF export REDIS6_CONF +export REDIS7_CONF +export REDIS8_CONF export REDIS_SENTINEL1 export REDIS_SENTINEL2 export REDIS_SENTINEL3 +export REDIS_SENTINEL4 export REDIS_CLUSTER_NODE1_CONF export REDIS_CLUSTER_NODE2_CONF export REDIS_CLUSTER_NODE3_CONF @@ -156,17 +194,22 @@ start: cleanup echo "$$REDIS4_CONF" | redis-server - echo "$$REDIS5_CONF" | redis-server - echo "$$REDIS6_CONF" | redis-server - + echo "$$REDIS7_CONF" | redis-server - + echo "$$REDIS8_CONF" | redis-server - echo "$$REDIS_SENTINEL1" > /tmp/sentinel1.conf && redis-server /tmp/sentinel1.conf --sentinel @sleep 0.5 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 + @sleep 0.5 + echo "$$REDIS_SENTINEL4" > /tmp/sentinel4.conf && redis-server /tmp/sentinel4.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 + rm -vf /tmp/sentinel*.conf stop: kill `cat /tmp/redis1.pid` @@ -176,12 +219,19 @@ stop: kill `cat /tmp/redis4.pid` || true kill `cat /tmp/redis5.pid` || true kill `cat /tmp/redis6.pid` || true + kill `cat /tmp/redis7.pid` + kill `cat /tmp/redis8.pid` kill `cat /tmp/sentinel1.pid` kill `cat /tmp/sentinel2.pid` kill `cat /tmp/sentinel3.pid` + kill `cat /tmp/sentinel4.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/sentinel1.conf + rm -f /tmp/sentinel2.conf + rm -f /tmp/sentinel3.conf + rm -f /tmp/sentinel4.conf rm -f /tmp/redis_cluster_node1.conf rm -f /tmp/redis_cluster_node2.conf rm -f /tmp/redis_cluster_node3.conf diff --git a/pom.xml b/pom.xml index f6575ebc58..58c250dd55 100644 --- a/pom.xml +++ b/pom.xml @@ -45,8 +45,8 @@ - localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384 - localhost:26379,localhost:26380,localhost:26381 + localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384,localhost:6385,localhost:6386 + localhost:26379,localhost:26380,localhost:26381,localhost:26382 localhost:7379,localhost:7380,localhost:7381 github diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index f5f6bddbcd..abfeaf91a3 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -8,15 +8,14 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; 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, ClusterCommands { +public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands, BasicCommands, ClusterCommands, SentinelCommands { public Jedis(final String host) { super(host); } @@ -3006,6 +3005,38 @@ public List> sentinelSlaves(String masterName) { return slaves; } + public String sentinelFailover(String masterName) { + client.sentinel(Protocol.SENTINEL_FAILOVER, masterName); + return client.getStatusCodeReply(); + } + + public String sentinelMonitor(String masterName, String ip, int port, + int quorum) { + client.sentinel(Protocol.SENTINEL_MONITOR, masterName, ip, String.valueOf(port), String.valueOf(quorum)); + return client.getStatusCodeReply(); + } + + public String sentinelRemove(String masterName) { + client.sentinel(Protocol.SENTINEL_REMOVE, masterName); + return client.getStatusCodeReply(); + } + + public String sentinelSet(String masterName, Map parameterMap) { + int index = 0; + int paramsLength = parameterMap.size() * 2 + 2; + String[] params = new String[paramsLength]; + + params[index++] = Protocol.SENTINEL_SET; + params[index++] = masterName; + for (Entry entry : parameterMap.entrySet()) { + params[index++] = entry.getKey(); + params[index++] = entry.getValue(); + } + + client.sentinel(params); + return client.getStatusCodeReply(); + } + public byte[] dump(final String key) { checkIsInMulti(); client.dump(key); @@ -3214,4 +3245,5 @@ public String asking() { client.asking(); return client.getStatusCodeReply(); } + } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index a5e08c6a77..c1cb39c5ac 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -33,6 +33,10 @@ public final class Protocol { 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_FAILOVER = "failover"; + public static final String SENTINEL_MONITOR = "monitor"; + public static final String SENTINEL_REMOVE = "remove"; + public static final String SENTINEL_SET = "set"; public static final String CLUSTER_NODES = "nodes"; public static final String CLUSTER_MEET = "meet"; diff --git a/src/main/java/redis/clients/jedis/SentinelCommands.java b/src/main/java/redis/clients/jedis/SentinelCommands.java new file mode 100644 index 0000000000..21ef3f968e --- /dev/null +++ b/src/main/java/redis/clients/jedis/SentinelCommands.java @@ -0,0 +1,22 @@ +package redis.clients.jedis; + +import java.util.List; +import java.util.Map; + +public interface SentinelCommands { + public List> sentinelMasters(); + + public List sentinelGetMasterAddrByName(String masterName); + + public Long sentinelReset(String pattern); + + public List> sentinelSlaves(String masterName); + + public String sentinelFailover(String masterName); + + public String sentinelMonitor(String masterName, String ip, int port, int quorum); + + public String sentinelRemove(String masterName); + + public String sentinelSet(String masterName, Map parameterMap); +} diff --git a/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java b/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java index c7be599561..491f0a354b 100644 --- a/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java +++ b/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java @@ -12,42 +12,18 @@ public class HostAndPortUtil { 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); + redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT)); + redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 1)); + redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 2)); + redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 3)); + redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4)); + redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 5)); + redisHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_PORT + 6)); + + sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT)); + sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1)); + sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 2)); + sentinelHostAndPortList.add(new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 3)); clusterHostAndPortList.add(new HostAndPort("localhost", 7379)); clusterHostAndPortList.add(new HostAndPort("localhost", 7380)); diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java index f8267c8e54..ad709544a5 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java @@ -1,5 +1,6 @@ package redis.clients.jedis.tests; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -9,24 +10,28 @@ import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; +import redis.clients.jedis.exceptions.JedisDataException; +import redis.clients.jedis.tests.utils.JedisSentinelTestUtil; public class JedisSentinelTest extends JedisTestBase { private static final String MASTER_NAME = "mymaster"; + private static final String MONITOR_MASTER_NAME = "mymastermonitor"; + private static final String REMOVE_MASTER_NAME = "mymasterremove"; + private static final String FAILOVER_MASTER_NAME = "mymasterfailover"; + private static final String MASTER_IP = "127.0.0.1"; protected static HostAndPort master = HostAndPortUtil.getRedisServers() .get(0); - protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get( - 5); + protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get(5); protected static HostAndPort sentinel = HostAndPortUtil .getSentinelServers().get(0); - - protected static Jedis masterJedis; - protected static Jedis slaveJedis; - protected static Jedis sentinelJedis; - + + protected static HostAndPort sentinelForFailover = HostAndPortUtil.getSentinelServers() + .get(3); + protected static HostAndPort masterForFailover = HostAndPortUtil.getRedisServers().get(6); + @Before public void setup() throws InterruptedException { - } @After @@ -36,30 +41,117 @@ public void clear() throws InterruptedException { // to restore it (demote) // so, promote(slaveof) slave to master has no effect, not same to old // Sentinel's behavior + ensureRemoved(MONITOR_MASTER_NAME); + ensureRemoved(REMOVE_MASTER_NAME); } @Test public void sentinel() { - Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); List> masters = j.sentinelMasters(); - final String masterName = masters.get(0).get("name"); - - assertEquals(MASTER_NAME, masterName); - - List masterHostAndPort = j - .sentinelGetMasterAddrByName(masterName); + + boolean inMasters = false; + for (Map master : masters) + if (MASTER_NAME.equals(master.get("name"))) + inMasters = true; + + assertTrue(inMasters); + + List masterHostAndPort = j.sentinelGetMasterAddrByName(MASTER_NAME); HostAndPort masterFromSentinel = new HostAndPort( masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort .get(1))); assertEquals(master, masterFromSentinel); - - List> slaves = j.sentinelSlaves(masterName); + + List> slaves = j.sentinelSlaves(MASTER_NAME); assertTrue(slaves.size() > 0); - assertEquals(master.getPort(), - Integer.parseInt(slaves.get(0).get("master-port"))); - + assertEquals(master.getPort(), Integer.parseInt(slaves.get(0).get("master-port"))); + // 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)); + assertEquals(Long.valueOf(1), j.sentinelReset(MASTER_NAME)); + assertEquals(Long.valueOf(0), j.sentinelReset("woof" + MASTER_NAME)); + } + + @Test + public void sentinelFailover() throws InterruptedException { + Jedis j = new Jedis(sentinelForFailover.getHost(), sentinelForFailover.getPort()); + + HostAndPort currentMaster = new HostAndPort(masterForFailover.getHost(), masterForFailover.getPort()); + + List masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME); + String result = j.sentinelFailover(FAILOVER_MASTER_NAME); + assertEquals("OK", result); + + JedisSentinelTestUtil.waitForNewPromotedMaster(sentinelForFailover, FAILOVER_MASTER_NAME, currentMaster); + + masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME); + HostAndPort newMaster = new HostAndPort(masterHostAndPort.get(0), + Integer.parseInt(masterHostAndPort.get(1))); + + assertNotEquals(newMaster, currentMaster); } + + @Test + public void sentinelMonitor() { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + + // monitor new master + String result = j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1); + assertEquals("OK", result); + + // already monitored + try { + j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1); + fail(); + } catch (JedisDataException e) { + // pass + } + } + + @Test + public void sentinelRemove() { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + + ensureMonitored(sentinel, REMOVE_MASTER_NAME, MASTER_IP, master.getPort(), 1); + + String result = j.sentinelRemove(REMOVE_MASTER_NAME); + assertEquals("OK", result); + + // not exist + try { + result = j.sentinelRemove(REMOVE_MASTER_NAME); + assertNotEquals("OK", result); + fail(); + } catch (JedisDataException e) { + // pass + } + } + + @Test + public void sentinelSet() { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + + Map parameterMap = new HashMap(); + parameterMap.put("down-after-milliseconds", String.valueOf(3000)); + parameterMap.put("parallel-syncs", String.valueOf(1)); + j.sentinelSet(MASTER_NAME, parameterMap); + + // cannot test "sentinel set" because there is no command "sentinel get" + } + + private void ensureMonitored(HostAndPort sentinel, String masterName, String ip, int port, int quorum) { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + try { + j.sentinelMonitor(masterName, ip, port, quorum); + } catch (JedisDataException e) { + } + } + + private void ensureRemoved(String masterName) { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + try { + j.sentinelRemove(masterName); + } catch (JedisDataException e) { + } + } } diff --git a/src/test/java/redis/clients/jedis/tests/utils/JedisSentinelTestUtil.java b/src/test/java/redis/clients/jedis/tests/utils/JedisSentinelTestUtil.java index cd8fdc63ff..9ac4a85ca2 100644 --- a/src/test/java/redis/clients/jedis/tests/utils/JedisSentinelTestUtil.java +++ b/src/test/java/redis/clients/jedis/tests/utils/JedisSentinelTestUtil.java @@ -46,8 +46,11 @@ public static HostAndPort waitForNewPromotedMaster(HostAndPort sentinel, List sentinelMasterInfos = sentinelJedis .sentinelGetMasterAddrByName(masterName); - if (sentinelMasterInfos == null) - continue; + if (sentinelMasterInfos == null) { + System.out + .println("Cannot retrieve Sentinel's master address info, sleep..."); + continue; + } newMaster = new HostAndPort(sentinelMasterInfos.get(0), Integer.parseInt(sentinelMasterInfos.get(1))); From 68356eb8022ebbe54342dc32e567b78951a79635 Mon Sep 17 00:00:00 2001 From: Jungtaek Lim Date: Wed, 22 Jan 2014 00:40:37 +0900 Subject: [PATCH 2/5] make sentinel set command unit test stronger --- .../redis/clients/jedis/tests/JedisSentinelTest.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java index ad709544a5..833151e757 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java @@ -132,11 +132,17 @@ public void sentinelSet() { Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); Map parameterMap = new HashMap(); - parameterMap.put("down-after-milliseconds", String.valueOf(3000)); - parameterMap.put("parallel-syncs", String.valueOf(1)); + parameterMap.put("down-after-milliseconds", String.valueOf(1234)); + parameterMap.put("parallel-syncs", String.valueOf(3)); j.sentinelSet(MASTER_NAME, parameterMap); - // cannot test "sentinel set" because there is no command "sentinel get" + List> masters = j.sentinelMasters(); + for (Map master : masters) { + if (master.get("name").equals(MASTER_NAME)) { + assertEquals(1234, Integer.parseInt(master.get("down-after-milliseconds"))); + assertEquals(3, Integer.parseInt(master.get("parallel-syncs"))); + } + } } private void ensureMonitored(HostAndPort sentinel, String masterName, String ip, int port, int quorum) { From 13f0c32f7cbccc12cd00eb42dd1551d001eebcd5 Mon Sep 17 00:00:00 2001 From: Jungtaek Lim Date: Wed, 5 Feb 2014 00:41:34 +0900 Subject: [PATCH 3/5] Add unit test for "sentinel set quorum" introduced by Redis 2.8.5 --- .../java/redis/clients/jedis/tests/JedisSentinelTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java index 833151e757..da0d5af08b 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java @@ -134,6 +134,7 @@ public void sentinelSet() { Map parameterMap = new HashMap(); parameterMap.put("down-after-milliseconds", String.valueOf(1234)); parameterMap.put("parallel-syncs", String.valueOf(3)); + parameterMap.put("quorum", String.valueOf(2)); j.sentinelSet(MASTER_NAME, parameterMap); List> masters = j.sentinelMasters(); @@ -141,8 +142,12 @@ public void sentinelSet() { if (master.get("name").equals(MASTER_NAME)) { assertEquals(1234, Integer.parseInt(master.get("down-after-milliseconds"))); assertEquals(3, Integer.parseInt(master.get("parallel-syncs"))); + assertEquals(2, Integer.parseInt(master.get("quorum"))); } } + + parameterMap.put("quorum", String.valueOf(1)); + j.sentinelSet(MASTER_NAME, parameterMap); } private void ensureMonitored(HostAndPort sentinel, String masterName, String ip, int port, int quorum) { From dd62e360fa714c6c93722f362772a71179a0ddda Mon Sep 17 00:00:00 2001 From: Jungtaek Lim Date: Wed, 5 Feb 2014 00:45:48 +0900 Subject: [PATCH 4/5] Apply "Java Convention" to met Jedis's preferred source code Convention --- .../jedis/tests/JedisSentinelTest.java | 221 +++++++++--------- 1 file changed, 117 insertions(+), 104 deletions(-) diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java index da0d5af08b..4445072baf 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java @@ -22,14 +22,16 @@ public class JedisSentinelTest extends JedisTestBase { protected static HostAndPort master = HostAndPortUtil.getRedisServers() .get(0); - protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get(5); + protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get( + 5); protected static HostAndPort sentinel = HostAndPortUtil .getSentinelServers().get(0); - - protected static HostAndPort sentinelForFailover = HostAndPortUtil.getSentinelServers() - .get(3); - protected static HostAndPort masterForFailover = HostAndPortUtil.getRedisServers().get(6); - + + protected static HostAndPort sentinelForFailover = HostAndPortUtil + .getSentinelServers().get(3); + protected static HostAndPort masterForFailover = HostAndPortUtil + .getRedisServers().get(6); + @Before public void setup() throws InterruptedException { } @@ -41,128 +43,139 @@ public void clear() throws InterruptedException { // to restore it (demote) // so, promote(slaveof) slave to master has no effect, not same to old // Sentinel's behavior - ensureRemoved(MONITOR_MASTER_NAME); - ensureRemoved(REMOVE_MASTER_NAME); + ensureRemoved(MONITOR_MASTER_NAME); + ensureRemoved(REMOVE_MASTER_NAME); } @Test public void sentinel() { - Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); List> masters = j.sentinelMasters(); - + boolean inMasters = false; for (Map master : masters) - if (MASTER_NAME.equals(master.get("name"))) - inMasters = true; - + if (MASTER_NAME.equals(master.get("name"))) + inMasters = true; + assertTrue(inMasters); - - List masterHostAndPort = j.sentinelGetMasterAddrByName(MASTER_NAME); + + List masterHostAndPort = j + .sentinelGetMasterAddrByName(MASTER_NAME); HostAndPort masterFromSentinel = new HostAndPort( masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort .get(1))); assertEquals(master, masterFromSentinel); - + List> slaves = j.sentinelSlaves(MASTER_NAME); assertTrue(slaves.size() > 0); - assertEquals(master.getPort(), Integer.parseInt(slaves.get(0).get("master-port"))); - + assertEquals(master.getPort(), + Integer.parseInt(slaves.get(0).get("master-port"))); + // DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET assertEquals(Long.valueOf(1), j.sentinelReset(MASTER_NAME)); assertEquals(Long.valueOf(0), j.sentinelReset("woof" + MASTER_NAME)); } @Test - public void sentinelFailover() throws InterruptedException { - Jedis j = new Jedis(sentinelForFailover.getHost(), sentinelForFailover.getPort()); - - HostAndPort currentMaster = new HostAndPort(masterForFailover.getHost(), masterForFailover.getPort()); - - List masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME); - String result = j.sentinelFailover(FAILOVER_MASTER_NAME); - assertEquals("OK", result); - - JedisSentinelTestUtil.waitForNewPromotedMaster(sentinelForFailover, FAILOVER_MASTER_NAME, currentMaster); - - masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME); - HostAndPort newMaster = new HostAndPort(masterHostAndPort.get(0), - Integer.parseInt(masterHostAndPort.get(1))); - - assertNotEquals(newMaster, currentMaster); + public void sentinelFailover() throws InterruptedException { + Jedis j = new Jedis(sentinelForFailover.getHost(), + sentinelForFailover.getPort()); + + HostAndPort currentMaster = new HostAndPort( + masterForFailover.getHost(), masterForFailover.getPort()); + + List masterHostAndPort = j + .sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME); + String result = j.sentinelFailover(FAILOVER_MASTER_NAME); + assertEquals("OK", result); + + JedisSentinelTestUtil.waitForNewPromotedMaster(sentinelForFailover, + FAILOVER_MASTER_NAME, currentMaster); + + masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME); + HostAndPort newMaster = new HostAndPort(masterHostAndPort.get(0), + Integer.parseInt(masterHostAndPort.get(1))); + + assertNotEquals(newMaster, currentMaster); } - - @Test - public void sentinelMonitor() { - Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); - - // monitor new master - String result = j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1); - assertEquals("OK", result); - - // already monitored - try { - j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1); - fail(); - } catch (JedisDataException e) { - // pass - } + + @Test + public void sentinelMonitor() { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + + // monitor new master + String result = j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, + master.getPort(), 1); + assertEquals("OK", result); + + // already monitored + try { + j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), + 1); + fail(); + } catch (JedisDataException e) { + // pass } - - @Test - public void sentinelRemove() { - Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); - - ensureMonitored(sentinel, REMOVE_MASTER_NAME, MASTER_IP, master.getPort(), 1); - - String result = j.sentinelRemove(REMOVE_MASTER_NAME); - assertEquals("OK", result); - - // not exist - try { - result = j.sentinelRemove(REMOVE_MASTER_NAME); - assertNotEquals("OK", result); - fail(); - } catch (JedisDataException e) { - // pass - } + } + + @Test + public void sentinelRemove() { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + + ensureMonitored(sentinel, REMOVE_MASTER_NAME, MASTER_IP, + master.getPort(), 1); + + String result = j.sentinelRemove(REMOVE_MASTER_NAME); + assertEquals("OK", result); + + // not exist + try { + result = j.sentinelRemove(REMOVE_MASTER_NAME); + assertNotEquals("OK", result); + fail(); + } catch (JedisDataException e) { + // pass } - - @Test - public void sentinelSet() { - Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); - - Map parameterMap = new HashMap(); - parameterMap.put("down-after-milliseconds", String.valueOf(1234)); - parameterMap.put("parallel-syncs", String.valueOf(3)); - parameterMap.put("quorum", String.valueOf(2)); - j.sentinelSet(MASTER_NAME, parameterMap); - - List> masters = j.sentinelMasters(); - for (Map master : masters) { - if (master.get("name").equals(MASTER_NAME)) { - assertEquals(1234, Integer.parseInt(master.get("down-after-milliseconds"))); - assertEquals(3, Integer.parseInt(master.get("parallel-syncs"))); - assertEquals(2, Integer.parseInt(master.get("quorum"))); - } - } - - parameterMap.put("quorum", String.valueOf(1)); - j.sentinelSet(MASTER_NAME, parameterMap); + } + + @Test + public void sentinelSet() { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + + Map parameterMap = new HashMap(); + parameterMap.put("down-after-milliseconds", String.valueOf(1234)); + parameterMap.put("parallel-syncs", String.valueOf(3)); + parameterMap.put("quorum", String.valueOf(2)); + j.sentinelSet(MASTER_NAME, parameterMap); + + List> masters = j.sentinelMasters(); + for (Map master : masters) { + if (master.get("name").equals(MASTER_NAME)) { + assertEquals(1234, + Integer.parseInt(master.get("down-after-milliseconds"))); + assertEquals(3, Integer.parseInt(master.get("parallel-syncs"))); + assertEquals(2, Integer.parseInt(master.get("quorum"))); + } } - - private void ensureMonitored(HostAndPort sentinel, String masterName, String ip, int port, int quorum) { - Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); - try { - j.sentinelMonitor(masterName, ip, port, quorum); - } catch (JedisDataException e) { - } + + parameterMap.put("quorum", String.valueOf(1)); + j.sentinelSet(MASTER_NAME, parameterMap); + } + + private void ensureMonitored(HostAndPort sentinel, String masterName, + String ip, int port, int quorum) { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + try { + j.sentinelMonitor(masterName, ip, port, quorum); + } catch (JedisDataException e) { } - - private void ensureRemoved(String masterName) { - Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); - try { - j.sentinelRemove(masterName); - } catch (JedisDataException e) { - } + } + + private void ensureRemoved(String masterName) { + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); + try { + j.sentinelRemove(masterName); + } catch (JedisDataException e) { } + } } From a5363582a6831cca4ec00f2428222cbeffb088d1 Mon Sep 17 00:00:00 2001 From: Jungtaek Lim Date: Wed, 5 Feb 2014 07:52:53 +0900 Subject: [PATCH 5/5] Apply "Java Convention" to met Jedis's preferred source code Convention --- src/main/java/redis/clients/jedis/Jedis.java | 62 ++++++++++---------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index ec3929a230..6e06fef2af 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3012,38 +3012,40 @@ public List> sentinelSlaves(String masterName) { return slaves; } - public String sentinelFailover(String masterName) { - client.sentinel(Protocol.SENTINEL_FAILOVER, masterName); - return client.getStatusCodeReply(); - } + public String sentinelFailover(String masterName) { + client.sentinel(Protocol.SENTINEL_FAILOVER, masterName); + return client.getStatusCodeReply(); + } - public String sentinelMonitor(String masterName, String ip, int port, - int quorum) { - client.sentinel(Protocol.SENTINEL_MONITOR, masterName, ip, String.valueOf(port), String.valueOf(quorum)); - return client.getStatusCodeReply(); - } + public String sentinelMonitor(String masterName, String ip, int port, + int quorum) { + client.sentinel(Protocol.SENTINEL_MONITOR, masterName, ip, + String.valueOf(port), String.valueOf(quorum)); + return client.getStatusCodeReply(); + } - public String sentinelRemove(String masterName) { - client.sentinel(Protocol.SENTINEL_REMOVE, masterName); - return client.getStatusCodeReply(); - } + public String sentinelRemove(String masterName) { + client.sentinel(Protocol.SENTINEL_REMOVE, masterName); + return client.getStatusCodeReply(); + } + + public String sentinelSet(String masterName, + Map parameterMap) { + int index = 0; + int paramsLength = parameterMap.size() * 2 + 2; + String[] params = new String[paramsLength]; - public String sentinelSet(String masterName, Map parameterMap) { - int index = 0; - int paramsLength = parameterMap.size() * 2 + 2; - String[] params = new String[paramsLength]; - - params[index++] = Protocol.SENTINEL_SET; - params[index++] = masterName; - for (Entry entry : parameterMap.entrySet()) { - params[index++] = entry.getKey(); - params[index++] = entry.getValue(); - } - - client.sentinel(params); - return client.getStatusCodeReply(); + params[index++] = Protocol.SENTINEL_SET; + params[index++] = masterName; + for (Entry entry : parameterMap.entrySet()) { + params[index++] = entry.getKey(); + params[index++] = entry.getValue(); } + client.sentinel(params); + return client.getStatusCodeReply(); + } + public byte[] dump(final String key) { checkIsInMulti(); client.dump(key); @@ -3280,8 +3282,8 @@ public Long pubsubNumPat() { public Map pubsubNumSub(String... channels) { checkIsInMulti(); - client.pubsubNumSub(channels); - return BuilderFactory.STRING_MAP - .build(client.getBinaryMultiBulkReply()); + client.pubsubNumSub(channels); + return BuilderFactory.STRING_MAP + .build(client.getBinaryMultiBulkReply()); } }