From 15891c411734e78dafe61262f7052b0d1936e286 Mon Sep 17 00:00:00 2001 From: Jungtaek Lim Date: Mon, 2 Dec 2013 22:11:52 +0900 Subject: [PATCH] make jedis unit tests pass to Redis 2.8.1 * increase sentinel instance to test JedisSentinelTest ** clear() called, slave promoted to master (slave of no one), New Sentinel force to restore it (demote) -> slave is not reusable * ipv6 applied at Redis 2.8 -> localhost / 127.0.0.1 / ::1 is now all same * Makefile: sleep some time for launch each sentinel (workaround to sentinel's issue) ** issue to sentinel leader vote: https://github.com/antirez/redis/issues/1419 *** sentinel may confused to vote with sentinels launched approximately same time --- Makefile | 79 ++++++------------- pom.xml | 6 +- .../java/redis/clients/jedis/HostAndPort.java | 21 +++-- .../clients/jedis/tests/HostAndPortUtil.java | 11 ++- .../jedis/tests/JedisSentinelTest.java | 19 ++--- 5 files changed, 58 insertions(+), 78 deletions(-) diff --git a/Makefile b/Makefile index 30a8b04..21eaa2b 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,17 @@ save "" appendonly no endef +define REDIS6_CONF +daemonize yes +port 6384 +requirepass foobared +masterauth foobared +pidfile /tmp/redis6.pid +logfile /tmp/redis6.log +save "" +appendonly no +endef + define REDIS_SENTINEL1 port 26379 daemonize yes @@ -91,6 +102,7 @@ export REDIS2_CONF export REDIS3_CONF export REDIS4_CONF export REDIS5_CONF +export REDIS6_CONF export REDIS_SENTINEL1 export REDIS_SENTINEL2 export REDIS_SENTINEL3 @@ -101,8 +113,11 @@ start: echo "$$REDIS3_CONF" | redis-server - echo "$$REDIS4_CONF" | redis-server - echo "$$REDIS5_CONF" | redis-server - + echo "$$REDIS6_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 stop: @@ -112,76 +127,26 @@ stop: kill `cat /tmp/redis3.pid` || true kill `cat /tmp/redis4.pid` || true kill `cat /tmp/redis5.pid` || true + kill `cat /tmp/redis6.pid` || true kill `cat /tmp/sentinel1.pid` kill `cat /tmp/sentinel2.pid` kill `cat /tmp/sentinel3.pid` test: - echo "$$REDIS1_CONF" | redis-server - - echo "$$REDIS2_CONF" | redis-server - - echo "$$REDIS3_CONF" | redis-server - - echo "$$REDIS4_CONF" | redis-server - - echo "$$REDIS5_CONF" | redis-server - - echo "$$REDIS_SENTINEL1" | redis-server - --sentinel - echo "$$REDIS_SENTINEL2" | redis-server - --sentinel - echo "$$REDIS_SENTINEL3" | redis-server - --sentinel - + make start mvn clean compile test - - kill `cat /tmp/redis1.pid` - kill `cat /tmp/redis2.pid` - # this get's segfaulted by the tests - kill `cat /tmp/redis3.pid` || true - kill `cat /tmp/redis4.pid` || true - kill `cat /tmp/redis5.pid` || true - kill `cat /tmp/sentinel1.pid` - kill `cat /tmp/sentinel2.pid` - kill `cat /tmp/sentinel3.pid` + make stop deploy: - echo "$$REDIS1_CONF" | redis-server - - echo "$$REDIS2_CONF" | redis-server - - echo "$$REDIS3_CONF" | redis-server - - echo "$$REDIS4_CONF" | redis-server - - echo "$$REDIS5_CONF" | redis-server - - echo "$$REDIS_SENTINEL1" | redis-server - --sentinel - echo "$$REDIS_SENTINEL2" | redis-server - --sentinel - echo "$$REDIS_SENTINEL3" | redis-server - --sentinel - + make start mvn clean deploy - - kill `cat /tmp/redis1.pid` - kill `cat /tmp/redis2.pid` - # this get's segfaulted by the tests - kill `cat /tmp/redis3.pid` || true - kill `cat /tmp/redis4.pid` || true - kill `cat /tmp/redis5.pid` || true - kill `cat /tmp/sentinel1.pid` - kill `cat /tmp/sentinel2.pid` - kill `cat /tmp/sentinel3.pid` + make stop release: - echo "$$REDIS1_CONF" | redis-server - - echo "$$REDIS2_CONF" | redis-server - - echo "$$REDIS3_CONF" | redis-server - - echo "$$REDIS4_CONF" | redis-server - - echo "$$REDIS5_CONF" | redis-server - - echo "$$REDIS_SENTINEL1" | redis-server - --sentinel - echo "$$REDIS_SENTINEL2" | redis-server - --sentinel - echo "$$REDIS_SENTINEL3" | redis-server - --sentinel - + make start mvn release:clean mvn release:prepare mvn release:perform - - kill `cat /tmp/redis1.pid` - kill `cat /tmp/redis2.pid` - # this get's segfaulted by the tests - kill `cat /tmp/redis3.pid` || true - kill `cat /tmp/redis4.pid` || true - kill `cat /tmp/redis5.pid` || true - kill `cat /tmp/sentinel1.pid` - kill `cat /tmp/sentinel2.pid` - kill `cat /tmp/sentinel3.pid` + make stop .PHONY: test diff --git a/pom.xml b/pom.xml index 8649428..9409ec9 100644 --- a/pom.xml +++ b/pom.xml @@ -45,9 +45,9 @@ - localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383 - localhost:26379,localhost:26380 - github + localhost:6379,localhost:6380,localhost:6381,localhost:6382,localhost:6383,localhost:6384 + localhost:26379,localhost:26380,localhost:26381 + github diff --git a/src/main/java/redis/clients/jedis/HostAndPort.java b/src/main/java/redis/clients/jedis/HostAndPort.java index 488eac3..33d1467 100644 --- a/src/main/java/redis/clients/jedis/HostAndPort.java +++ b/src/main/java/redis/clients/jedis/HostAndPort.java @@ -1,6 +1,8 @@ package redis.clients.jedis; public class HostAndPort { + public static final String LOCALHOST_STR = "localhost"; + private String host; private int port; @@ -22,11 +24,11 @@ public class HostAndPort { if (obj instanceof HostAndPort) { HostAndPort hp = (HostAndPort) obj; - // localhost and 127.0.0.1 is same - return port == hp.port && - (host.equals(hp.host) || - (host.equals("localhost") && hp.host.equals("127.0.0.1")) || - (host.equals("127.0.0.1") && hp.host.equals("localhost")) ); + String thisHost = convertHost(host); + String hpHost = convertHost(hp.host); + return port == hp.port && + thisHost.equals(hpHost); + } return false; @@ -36,4 +38,13 @@ public class HostAndPort { public String toString() { return host + ":" + port; } + + private String convertHost(String host) { + if (host.equals("127.0.0.1")) + return LOCALHOST_STR; + else if (host.equals("::1")) + return LOCALHOST_STR; + + return host; + } } diff --git a/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java b/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java index 03e421e..be64c53 100644 --- a/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java +++ b/src/test/java/redis/clients/jedis/tests/HostAndPortUtil.java @@ -27,14 +27,17 @@ public class HostAndPortUtil { HostAndPort defaulthnp5 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4); redisHostAndPortList.add(defaulthnp5); - HostAndPort defaulthnp6 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT); - sentinelHostAndPortList.add(defaulthnp6); + HostAndPort defaulthnp6 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 5); + redisHostAndPortList.add(defaulthnp6); - HostAndPort defaulthnp7 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1); + HostAndPort defaulthnp7 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT); sentinelHostAndPortList.add(defaulthnp7); - HostAndPort defaulthnp8 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 2); + 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); String envRedisHosts = System.getProperty("redis-hosts"); String envSentinelHosts = System.getProperty("sentinel-hosts"); diff --git a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java index 3e5cfdf..8912a1c 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisSentinelTest.java @@ -17,8 +17,8 @@ public class JedisSentinelTest extends JedisTestBase { protected static HostAndPort master = HostAndPortUtil.getRedisServers() .get(0); - protected static HostAndPort slave = HostAndPortUtil.getRedisServers().get( - 1); + protected static HostAndPort slave = HostAndPortUtil.getRedisServers() + .get(5); protected static HostAndPort sentinel = HostAndPortUtil .getSentinelServers().get(0); @@ -44,14 +44,14 @@ public class JedisSentinelTest extends JedisTestBase { @After public void clear() throws InterruptedException { - Jedis j = new Jedis("localhost", 6380); - j.auth("foobared"); - j.slaveofNoOne(); + // New Sentinel (after 2.8.1) + // when slave promoted to master (slave of no one), New Sentinel force to restore it (demote) + // so, promote(slaveof) slave to master has no effect, not same to old Sentinel's behavior } @Test public void sentinel() { - Jedis j = new Jedis("localhost", 26379); + Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort()); List> masters = j.sentinelMasters(); final String masterName = masters.get(0).get("name"); @@ -59,12 +59,13 @@ public class JedisSentinelTest extends JedisTestBase { List masterHostAndPort = j .sentinelGetMasterAddrByName(masterName); - assertEquals("127.0.0.1", masterHostAndPort.get(0)); - assertEquals("6379", masterHostAndPort.get(1)); + HostAndPort masterFromSentinel = new HostAndPort(masterHostAndPort.get(0), + Integer.parseInt(masterHostAndPort.get(1))); + assertEquals(master, masterFromSentinel); List> slaves = j.sentinelSlaves(masterName); assertTrue(slaves.size() > 0); - assertEquals("6379", 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));