diff --git a/README.md b/README.md index bbefbe1..450c574 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Or use it as a maven dependency: redis.clients jedis - 2.2.1 + 2.4.2 jar compile diff --git a/pom.xml b/pom.xml index a55806e..1be71dd 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ jar redis.clients jedis - 2.4.2-SNAPSHOT + 2.5.0-SNAPSHOT Jedis Jedis is a blazingly small and sane Redis java client. https://github.com/xetorthio/jedis diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 72f72c0..9870429 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -55,8 +55,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().set(key, value); + public String execute(Jedis connection) { + return connection.set(key, value); } }.run(key); } @@ -66,8 +66,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().get(key); + public String execute(Jedis connection) { + return connection.get(key); } }.run(key); } @@ -77,8 +77,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Boolean execute() { - return connectionHandler.getConnection().exists(key); + public Boolean execute(Jedis connection) { + return connection.exists(key); } }.run(key); } @@ -88,8 +88,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().persist(key); + public Long execute(Jedis connection) { + return connection.persist(key); } }.run(key); } @@ -99,8 +99,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().type(key); + public String execute(Jedis connection) { + return connection.type(key); } }.run(key); } @@ -110,8 +110,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().expire(key, seconds); + public Long execute(Jedis connection) { + return connection.expire(key, seconds); } }.run(key); } @@ -121,8 +121,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection() + public Long execute(Jedis connection) { + return connection .expireAt(key, unixTime); } }.run(key); @@ -133,8 +133,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().ttl(key); + public Long execute(Jedis connection) { + return connection.ttl(key); } }.run(key); } @@ -145,8 +145,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Boolean execute() { - return connectionHandler.getConnection().setbit(key, offset, + public Boolean execute(Jedis connection) { + return connection.setbit(key, offset, value); } }.run(key); @@ -158,8 +158,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Boolean execute() { - return connectionHandler.getConnection().setbit(key, offset, + public Boolean execute(Jedis connection) { + return connection.setbit(key, offset, value); } }.run(key); @@ -170,8 +170,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Boolean execute() { - return connectionHandler.getConnection().getbit(key, offset); + public Boolean execute(Jedis connection) { + return connection.getbit(key, offset); } }.run(key); } @@ -181,8 +181,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().setrange(key, offset, + public Long execute(Jedis connection) { + return connection.setrange(key, offset, value); } }.run(key); @@ -194,8 +194,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().getrange(key, + public String execute(Jedis connection) { + return connection.getrange(key, startOffset, endOffset); } }.run(key); @@ -206,8 +206,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().getSet(key, value); + public String execute(Jedis connection) { + return connection.getSet(key, value); } }.run(key); } @@ -217,8 +217,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().setnx(key, value); + public Long execute(Jedis connection) { + return connection.setnx(key, value); } }.run(key); } @@ -228,8 +228,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().setex(key, seconds, + public String execute(Jedis connection) { + return connection.setex(key, seconds, value); } }.run(key); @@ -240,8 +240,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().decrBy(key, integer); + public Long execute(Jedis connection) { + return connection.decrBy(key, integer); } }.run(key); } @@ -251,8 +251,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().decr(key); + public Long execute(Jedis connection) { + return connection.decr(key); } }.run(key); } @@ -262,8 +262,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().incrBy(key, integer); + public Long execute(Jedis connection) { + return connection.incrBy(key, integer); } }.run(key); } @@ -273,8 +273,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().incr(key); + public Long execute(Jedis connection) { + return connection.incr(key); } }.run(key); } @@ -284,8 +284,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().append(key, value); + public Long execute(Jedis connection) { + return connection.append(key, value); } }.run(key); } @@ -295,8 +295,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection() + public String execute(Jedis connection) { + return connection .substr(key, start, end); } }.run(key); @@ -307,8 +307,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection() + public Long execute(Jedis connection) { + return connection .hset(key, field, value); } }.run(key); @@ -319,8 +319,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().hget(key, field); + public String execute(Jedis connection) { + return connection.hget(key, field); } }.run(key); } @@ -330,8 +330,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().hsetnx(key, field, + public Long execute(Jedis connection) { + return connection.hsetnx(key, field, value); } }.run(key); @@ -342,8 +342,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().hmset(key, hash); + public String execute(Jedis connection) { + return connection.hmset(key, hash); } }.run(key); } @@ -353,8 +353,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public List execute() { - return connectionHandler.getConnection().hmget(key, fields); + public List execute(Jedis connection) { + return connection.hmget(key, fields); } }.run(key); } @@ -364,8 +364,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().hincrBy(key, field, + public Long execute(Jedis connection) { + return connection.hincrBy(key, field, value); } }.run(key); @@ -376,8 +376,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Boolean execute() { - return connectionHandler.getConnection().hexists(key, field); + public Boolean execute(Jedis connection) { + return connection.hexists(key, field); } }.run(key); } @@ -387,8 +387,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().hdel(key, field); + public Long execute(Jedis connection) { + return connection.hdel(key, field); } }.run(key); } @@ -398,8 +398,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().hdel(key); + public Long execute(Jedis connection) { + return connection.hdel(key); } }.run(key); } @@ -409,8 +409,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().hkeys(key); + public Set execute(Jedis connection) { + return connection.hkeys(key); } }.run(key); } @@ -420,8 +420,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public List execute() { - return connectionHandler.getConnection().hvals(key); + public List execute(Jedis connection) { + return connection.hvals(key); } }.run(key); } @@ -431,8 +431,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Map execute() { - return connectionHandler.getConnection().hgetAll(key); + public Map execute(Jedis connection) { + return connection.hgetAll(key); } }.run(key); } @@ -442,8 +442,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().rpush(key, string); + public Long execute(Jedis connection) { + return connection.rpush(key, string); } }.run(key); } @@ -453,8 +453,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().lpush(key, string); + public Long execute(Jedis connection) { + return connection.lpush(key, string); } }.run(key); } @@ -464,8 +464,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().llen(key); + public Long execute(Jedis connection) { + return connection.llen(key); } }.run(key); } @@ -476,8 +476,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public List execute() { - return connectionHandler.getConnection() + public List execute(Jedis connection) { + return connection .lrange(key, start, end); } }.run(key); @@ -488,8 +488,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().ltrim(key, start, end); + public String execute(Jedis connection) { + return connection.ltrim(key, start, end); } }.run(key); } @@ -499,8 +499,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().lindex(key, index); + public String execute(Jedis connection) { + return connection.lindex(key, index); } }.run(key); } @@ -510,8 +510,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection() + public String execute(Jedis connection) { + return connection .lset(key, index, value); } }.run(key); @@ -522,8 +522,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection() + public Long execute(Jedis connection) { + return connection .lrem(key, count, value); } }.run(key); @@ -534,8 +534,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().lpop(key); + public String execute(Jedis connection) { + return connection.lpop(key); } }.run(key); } @@ -545,8 +545,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().rpop(key); + public String execute(Jedis connection) { + return connection.rpop(key); } }.run(key); } @@ -556,8 +556,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().sadd(key, member); + public Long execute(Jedis connection) { + return connection.sadd(key, member); } }.run(key); } @@ -567,8 +567,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().smembers(key); + public Set execute(Jedis connection) { + return connection.smembers(key); } }.run(key); } @@ -578,8 +578,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().srem(key, member); + public Long execute(Jedis connection) { + return connection.srem(key, member); } }.run(key); } @@ -589,8 +589,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().spop(key); + public String execute(Jedis connection) { + return connection.spop(key); } }.run(key); } @@ -600,8 +600,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().scard(key); + public Long execute(Jedis connection) { + return connection.scard(key); } }.run(key); } @@ -611,8 +611,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Boolean execute() { - return connectionHandler.getConnection().sismember(key, member); + public Boolean execute(Jedis connection) { + return connection.sismember(key, member); } }.run(key); } @@ -622,8 +622,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().srandmember(key); + public String execute(Jedis connection) { + return connection.srandmember(key); } }.run(key); } @@ -633,8 +633,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().strlen(key); + public Long execute(Jedis connection) { + return connection.strlen(key); } }.run(key); } @@ -644,8 +644,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zadd(key, score, + public Long execute(Jedis connection) { + return connection.zadd(key, score, member); } }.run(key); @@ -656,8 +656,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection() + public Long execute(Jedis connection) { + return connection .zadd(key, scoreMembers); } }.run(key); @@ -668,8 +668,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrange(key, start, end); } }.run(key); @@ -680,8 +680,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zrem(key, member); + public Long execute(Jedis connection) { + return connection.zrem(key, member); } }.run(key); } @@ -692,8 +692,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Double execute() { - return connectionHandler.getConnection().zincrby(key, score, + public Double execute(Jedis connection) { + return connection.zincrby(key, score, member); } }.run(key); @@ -704,8 +704,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zrank(key, member); + public Long execute(Jedis connection) { + return connection.zrank(key, member); } }.run(key); } @@ -715,8 +715,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zrevrank(key, member); + public Long execute(Jedis connection) { + return connection.zrevrank(key, member); } }.run(key); } @@ -727,8 +727,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrevrange(key, start, + public Set execute(Jedis connection) { + return connection.zrevrange(key, start, end); } }.run(key); @@ -740,8 +740,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrangeWithScores(key, + public Set execute(Jedis connection) { + return connection.zrangeWithScores(key, start, end); } }.run(key); @@ -753,8 +753,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrevrangeWithScores( + public Set execute(Jedis connection) { + return connection.zrevrangeWithScores( key, start, end); } }.run(key); @@ -765,8 +765,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zcard(key); + public Long execute(Jedis connection) { + return connection.zcard(key); } }.run(key); } @@ -776,8 +776,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Double execute() { - return connectionHandler.getConnection().zscore(key, member); + public Double execute(Jedis connection) { + return connection.zscore(key, member); } }.run(key); } @@ -787,8 +787,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public List execute() { - return connectionHandler.getConnection().sort(key); + public List execute(Jedis connection) { + return connection.sort(key); } }.run(key); } @@ -799,8 +799,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public List execute() { - return connectionHandler.getConnection().sort(key, + public List execute(Jedis connection) { + return connection.sort(key, sortingParameters); } }.run(key); @@ -811,8 +811,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zcount(key, min, max); + public Long execute(Jedis connection) { + return connection.zcount(key, min, max); } }.run(key); } @@ -822,8 +822,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zcount(key, min, max); + public Long execute(Jedis connection) { + return connection.zcount(key, min, max); } }.run(key); } @@ -834,8 +834,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrangeByScore(key, min, max); } }.run(key); @@ -847,8 +847,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrangeByScore(key, min, max); } }.run(key); @@ -860,8 +860,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrevrangeByScore(key, min, max); } }.run(key); @@ -873,8 +873,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrangeByScore(key, min, max, offset, count); } }.run(key); @@ -886,8 +886,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrevrangeByScore(key, min, max); } }.run(key); @@ -899,8 +899,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrangeByScore(key, min, max, offset, count); } }.run(key); @@ -912,8 +912,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrevrangeByScore(key, min, max, offset, count); } }.run(key); @@ -925,8 +925,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrangeByScoreWithScores(key, min, max); } }.run(key); @@ -938,8 +938,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrevrangeByScoreWithScores(key, min, max); } }.run(key); @@ -952,8 +952,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrangeByScoreWithScores(key, min, max, offset, count); } }.run(key); @@ -965,8 +965,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection().zrevrangeByScore(key, + public Set execute(Jedis connection) { + return connection.zrevrangeByScore(key, min, max, offset, count); } }.run(key); @@ -978,8 +978,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrangeByScoreWithScores(key, min, max); } }.run(key); @@ -991,8 +991,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrevrangeByScoreWithScores(key, min, max); } }.run(key); @@ -1005,8 +1005,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrangeByScoreWithScores(key, min, max, offset, count); } }.run(key); @@ -1019,8 +1019,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrevrangeByScoreWithScores(key, max, min, offset, count); } @@ -1034,8 +1034,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public Set execute() { - return connectionHandler.getConnection() + public Set execute(Jedis connection) { + return connection .zrevrangeByScoreWithScores(key, max, min, offset, count); } @@ -1048,8 +1048,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zremrangeByRank(key, + public Long execute(Jedis connection) { + return connection.zremrangeByRank(key, start, end); } }.run(key); @@ -1061,8 +1061,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zremrangeByScore(key, + public Long execute(Jedis connection) { + return connection.zremrangeByScore(key, start, end); } }.run(key); @@ -1074,8 +1074,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().zremrangeByScore(key, + public Long execute(Jedis connection) { + return connection.zremrangeByScore(key, start, end); } }.run(key); @@ -1087,8 +1087,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().linsert(key, where, + public Long execute(Jedis connection) { + return connection.linsert(key, where, pivot, value); } }.run(key); @@ -1099,8 +1099,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().lpushx(key, string); + public Long execute(Jedis connection) { + return connection.lpushx(key, string); } }.run(key); } @@ -1110,8 +1110,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().rpushx(key, string); + public Long execute(Jedis connection) { + return connection.rpushx(key, string); } }.run(key); } @@ -1121,8 +1121,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public List execute() { - return connectionHandler.getConnection().blpop(arg); + public List execute(Jedis connection) { + return connection.blpop(arg); } }.run(null); } @@ -1132,8 +1132,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public List execute() { - return connectionHandler.getConnection().brpop(arg); + public List execute(Jedis connection) { + return connection.brpop(arg); } }.run(null); } @@ -1143,10 +1143,10 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().del(key); + public Long execute(Jedis connection) { + return connection.del(key); } - }.run(null); + }.run(key); } @Override @@ -1154,8 +1154,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().echo(string); + public String execute(Jedis connection) { + return connection.echo(string); } }.run(null); } @@ -1165,8 +1165,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().move(key, dbIndex); + public Long execute(Jedis connection) { + return connection.move(key, dbIndex); } }.run(key); } @@ -1176,8 +1176,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().bitcount(key); + public Long execute(Jedis connection) { + return connection.bitcount(key); } }.run(key); } @@ -1187,8 +1187,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().bitcount(key, start, + public Long execute(Jedis connection) { + return connection.bitcount(key, start, end); } }.run(key); @@ -1199,8 +1199,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().ping(); + public String execute(Jedis connection) { + return connection.ping(); } }.run(null); } @@ -1210,8 +1210,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().quit(); + public String execute(Jedis connection) { + return connection.quit(); } }.run(null); } @@ -1221,8 +1221,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().flushDB(); + public String execute(Jedis connection) { + return connection.flushDB(); } }.run(null); } @@ -1232,8 +1232,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().dbSize(); + public Long execute(Jedis connection) { + return connection.dbSize(); } }.run(null); } @@ -1243,8 +1243,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().select(index); + public String execute(Jedis connection) { + return connection.select(index); } }.run(null); } @@ -1254,8 +1254,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().flushAll(); + public String execute(Jedis connection) { + return connection.flushAll(); } }.run(null); } @@ -1265,8 +1265,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().auth(password); + public String execute(Jedis connection) { + return connection.auth(password); } }.run(null); } @@ -1276,8 +1276,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().save(); + public String execute(Jedis connection) { + return connection.save(); } }.run(null); } @@ -1287,8 +1287,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().bgsave(); + public String execute(Jedis connection) { + return connection.bgsave(); } }.run(null); } @@ -1298,8 +1298,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().bgrewriteaof(); + public String execute(Jedis connection) { + return connection.bgrewriteaof(); } }.run(null); } @@ -1309,8 +1309,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().lastsave(); + public Long execute(Jedis connection) { + return connection.lastsave(); } }.run(null); } @@ -1320,8 +1320,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().shutdown(); + public String execute(Jedis connection) { + return connection.shutdown(); } }.run(null); } @@ -1331,8 +1331,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().info(); + public String execute(Jedis connection) { + return connection.info(); } }.run(null); } @@ -1342,8 +1342,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().info(section); + public String execute(Jedis connection) { + return connection.info(section); } }.run(null); } @@ -1353,8 +1353,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().slaveof(host, port); + public String execute(Jedis connection) { + return connection.slaveof(host, port); } }.run(null); } @@ -1364,8 +1364,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().slaveofNoOne(); + public String execute(Jedis connection) { + return connection.slaveofNoOne(); } }.run(null); } @@ -1375,8 +1375,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public Long execute() { - return connectionHandler.getConnection().getDB(); + public Long execute(Jedis connection) { + return connection.getDB(); } }.run(null); } @@ -1386,8 +1386,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().debug(params); + public String execute(Jedis connection) { + return connection.debug(params); } }.run(null); } @@ -1397,8 +1397,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand(connectionHandler, timeout, maxRedirections) { @Override - public String execute() { - return connectionHandler.getConnection().configResetStat(); + public String execute(Jedis connection) { + return connection.configResetStat(); } }.run(null); } @@ -1425,8 +1425,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>>( connectionHandler, timeout, maxRedirections) { @Override - public ScanResult> execute() { - return connectionHandler.getConnection().hscan(key, cursor); + public ScanResult> execute(Jedis connection) { + return connection.hscan(key, cursor); } }.run(null); } @@ -1442,8 +1442,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public ScanResult execute() { - return connectionHandler.getConnection().sscan(key, cursor); + public ScanResult execute(Jedis connection) { + return connection.sscan(key, cursor); } }.run(null); } @@ -1459,8 +1459,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public ScanResult execute() { - return connectionHandler.getConnection().zscan(key, cursor); + public ScanResult execute(Jedis connection) { + return connection.zscan(key, cursor); } }.run(null); } @@ -1471,8 +1471,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>>( connectionHandler, timeout, maxRedirections) { @Override - public ScanResult> execute() { - return connectionHandler.getConnection().hscan(key, cursor); + public ScanResult> execute(Jedis connection) { + return connection.hscan(key, cursor); } }.run(null); } @@ -1482,8 +1482,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public ScanResult execute() { - return connectionHandler.getConnection().sscan(key, cursor); + public ScanResult execute(Jedis connection) { + return connection.sscan(key, cursor); } }.run(null); } @@ -1493,8 +1493,8 @@ public class JedisCluster implements JedisCommands, BasicCommands { return new JedisClusterCommand>(connectionHandler, timeout, maxRedirections) { @Override - public ScanResult execute() { - return connectionHandler.getConnection().zscan(key, cursor); + public ScanResult execute(Jedis connection) { + return connection.zscan(key, cursor); } }.run(null); } diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java index 41087a7..c1be912 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterCommand.java +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -3,19 +3,18 @@ package redis.clients.jedis; import redis.clients.jedis.exceptions.JedisAskDataException; import redis.clients.jedis.exceptions.JedisClusterException; import redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException; +import redis.clients.jedis.exceptions.JedisConnectionException; +import redis.clients.jedis.exceptions.JedisException; +import redis.clients.jedis.exceptions.JedisMovedDataException; import redis.clients.jedis.exceptions.JedisRedirectionException; import redis.clients.util.JedisClusterCRC16; public abstract class JedisClusterCommand { - private boolean asking = false; - private JedisClusterConnectionHandler connectionHandler; private int commandTimeout; private int redirections; - // private boolean asking = false; - public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler, int timeout, int maxRedirections) { this.connectionHandler = connectionHandler; @@ -23,38 +22,83 @@ public abstract class JedisClusterCommand { this.redirections = maxRedirections; } - public abstract T execute(); + public abstract T execute(Jedis connection); public T run(String key) { - try { + if (key == null) { + throw new JedisClusterException( + "No way to dispatch this command to Redis Cluster."); + } - if (key == null) { - throw new JedisClusterException( - "No way to dispatch this command to Redis Cluster."); - } else if (redirections == 0) { - throw new JedisClusterMaxRedirectionsException( - "Too many Cluster redirections?"); + return runWithRetries(key, this.redirections, false, false); + } + + private T runWithRetries(String key, int redirections, + boolean tryRandomNode, boolean asking) { + if (redirections <= 0) { + throw new JedisClusterMaxRedirectionsException( + "Too many Cluster redirections?"); + } + + Jedis connection = null; + try { + if (tryRandomNode) { + connection = connectionHandler.getConnection(); + } else { + connection = connectionHandler + .getConnectionFromSlot(JedisClusterCRC16.getSlot(key)); } - connectionHandler.getConnectionFromSlot(JedisClusterCRC16 - .getSlot(key)); + if (asking) { // TODO: Pipeline asking with the original command to make it // faster.... - connectionHandler.getConnection().asking(); + connection.asking(); + + // if asking success, reset asking flag + asking = false; } - return execute(); + + return execute(connection); + } catch (JedisConnectionException jce) { + if (tryRandomNode) { + // maybe all connection is down + throw jce; + } + + releaseConnection(connection, true); + connection = null; + + // retry with random connection + return runWithRetries(key, redirections--, true, asking); } catch (JedisRedirectionException jre) { - return handleRedirection(jre, key); + if (jre instanceof JedisAskDataException) { + asking = true; + } else if (jre instanceof JedisMovedDataException) { + // TODO : In antirez's redis-rb-cluster implementation, + // it rebuilds cluster's slot and node cache + } + + this.connectionHandler.assignSlotToNode(jre.getSlot(), + jre.getTargetNode()); + + releaseConnection(connection, false); + connection = null; + + return runWithRetries(key, redirections - 1, false, asking); + } finally { + releaseConnection(connection, false); + } + + } + + private void releaseConnection(Jedis connection, boolean broken) { + if (connection != null) { + if (broken) { + connectionHandler.returnBrokenConnection(connection); + } else { + connectionHandler.returnConnection(connection); + } } } - private T handleRedirection(JedisRedirectionException jre, String key) { - if (jre instanceof JedisAskDataException) { - asking = true; - } - redirections--; - this.connectionHandler.assignSlotToNode(jre.getSlot(), - jre.getTargetNode()); - return run(key); - } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index 24f4349..29fab51 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -7,6 +7,8 @@ import java.util.Map; import java.util.Random; import java.util.Set; +import redis.clients.jedis.exceptions.JedisConnectionException; + public abstract class JedisClusterConnectionHandler { protected Map nodes = new HashMap(); @@ -15,6 +17,16 @@ public abstract class JedisClusterConnectionHandler { abstract Jedis getConnection(); + protected void returnConnection(Jedis connection) { + nodes.get(getNodeKey(connection.getClient())) + .returnResource(connection); + } + + public void returnBrokenConnection(Jedis connection) { + nodes.get(getNodeKey(connection.getClient())).returnBrokenResource( + connection); + } + abstract Jedis getConnectionFromSlot(int slot); public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig) { @@ -26,29 +38,57 @@ public abstract class JedisClusterConnectionHandler { return nodes; } - private void initializeSlotsCache(Set nodes) { - for (HostAndPort hostAndPort : nodes) { + private void initializeSlotsCache(Set startNodes) { + for (HostAndPort hostAndPort : startNodes) { JedisPool jp = new JedisPool(poolConfig, hostAndPort.getHost(), hostAndPort.getPort()); - this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); - Jedis jedis = jp.getResource(); + + this.nodes.clear(); + this.slots.clear(); + + Jedis jedis = null; try { + jedis = jp.getResource(); discoverClusterNodesAndSlots(jedis); + break; + } catch (JedisConnectionException e) { + if (jedis != null) { + jp.returnBrokenResource(jedis); + jedis = null; + } + + // try next nodes } finally { - jp.returnResource(jedis); + if (jedis != null) { + jp.returnResource(jedis); + } } } - } + for (HostAndPort node : startNodes) { + setNodeIfNotExist(node); + } + } + private void discoverClusterNodesAndSlots(Jedis jedis) { String localNodes = jedis.clusterNodes(); for (String nodeInfo : localNodes.split("\n")) { HostAndPort node = getHostAndPortFromNodeLine(nodeInfo, jedis); - JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort()); - this.nodes.put(node.getHost() + node.getPort(), nodePool); + setNodeIfNotExist(node); + + JedisPool nodePool = nodes.get(getNodeKey(node)); populateNodeSlots(nodeInfo, nodePool); } } + + private void setNodeIfNotExist(HostAndPort node) { + String nodeKey = getNodeKey(node); + if (nodes.containsKey(nodeKey)) + return; + + JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort()); + nodes.put(nodeKey, nodePool); + } private void populateNodeSlots(String nodeInfo, JedisPool nodePool) { String[] nodeInfoArray = nodeInfo.split(" "); @@ -71,7 +111,8 @@ public abstract class JedisClusterConnectionHandler { } } - private HostAndPort getHostAndPortFromNodeLine(String nodeInfo, Jedis currentConnection) { + private HostAndPort getHostAndPortFromNodeLine(String nodeInfo, + Jedis currentConnection) { String stringHostAndPort = nodeInfo.split(" ", 3)[1]; if (":0".equals(stringHostAndPort)) { return new HostAndPort(currentConnection.getClient().getHost(), @@ -83,9 +124,16 @@ public abstract class JedisClusterConnectionHandler { } public void assignSlotToNode(int slot, HostAndPort targetNode) { - JedisPool targetPool = nodes.get(targetNode.getHost() - + targetNode.getPort()); - slots.put(slot, targetPool); + JedisPool targetPool = nodes.get(getNodeKey(targetNode)); + + if (targetPool != null) { + slots.put(slot, targetPool); + } else { + setNodeIfNotExist(targetNode); + + targetPool = nodes.get(getNodeKey(targetNode)); + slots.put(slot, targetPool); + } } protected JedisPool getRandomConnection() { @@ -93,4 +141,11 @@ public abstract class JedisClusterConnectionHandler { return (JedisPool) (nodeArray[new Random().nextInt(nodeArray.length)]); } + protected String getNodeKey(HostAndPort hnp) { + return hnp.getHost() + ":" + hnp.getPort(); + } + + protected String getNodeKey(Client client) { + return client.getHost() + ":" + client.getPort(); + } } diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 6b2c80c..8e34d19 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -80,11 +80,15 @@ public class JedisPool extends Pool { } public void returnBrokenResource(final Jedis resource) { - returnBrokenResourceObject(resource); + if (resource != null) { + returnBrokenResourceObject(resource); + } } public void returnResource(final Jedis resource) { - resource.resetState(); - returnResourceObject(resource); + if (resource != null) { + resource.resetState(); + returnResourceObject(resource); + } } } diff --git a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java index 35674fa..b0fb3b1 100644 --- a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java @@ -1,49 +1,74 @@ package redis.clients.jedis; +import redis.clients.jedis.exceptions.JedisConnectionException; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Set; public class JedisSlotBasedConnectionHandler extends JedisClusterConnectionHandler { - private Jedis currentConnection; - public JedisSlotBasedConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig) { super(nodes, poolConfig); } public Jedis getConnection() { - return currentConnection != null ? currentConnection - : getRandomConnection().getResource(); - } + // In antirez's redis-rb-cluster implementation, + // getRandomConnection always return valid connection (able to ping-pong) + // or exception if all connections are invalid + + List pools = getShuffledNodesPool(); + + for (JedisPool pool : pools) { + Jedis jedis = null; + try { + jedis = pool.getResource(); + + if (jedis == null) { + continue; + } + + String result = jedis.ping(); + + if (result.equalsIgnoreCase("pong")) + return jedis; - private void returnCurrentConnection() { - if (currentConnection != null) { - nodes.get( - currentConnection.getClient().getHost() - + currentConnection.getClient().getPort()) - .returnResource(currentConnection); + pool.returnBrokenResource(jedis); + } catch (JedisConnectionException ex) { + if (jedis != null) { + pool.returnBrokenResource(jedis); + } + } } - + + throw new JedisConnectionException("no reachable node in cluster"); } @Override public void assignSlotToNode(int slot, HostAndPort targetNode) { super.assignSlotToNode(slot, targetNode); - getConnectionFromSlot(slot); } @Override public Jedis getConnectionFromSlot(int slot) { - returnCurrentConnection(); JedisPool connectionPool = slots.get(slot); - if (connectionPool == null) { - connectionPool = getRandomConnection(); + if (connectionPool != null) { + // It can't guaranteed to get valid connection because of node assignment + return connectionPool.getResource(); + } else { + return getConnection(); } - currentConnection = connectionPool.getResource(); - return currentConnection; + } + + private List getShuffledNodesPool() { + List pools = new ArrayList(); + pools.addAll(nodes.values()); + Collections.shuffle(pools); + return pools; } } diff --git a/src/main/java/redis/clients/jedis/Pipeline.java b/src/main/java/redis/clients/jedis/Pipeline.java index 97f856b..2687f84 100755 --- a/src/main/java/redis/clients/jedis/Pipeline.java +++ b/src/main/java/redis/clients/jedis/Pipeline.java @@ -31,6 +31,12 @@ public class Pipeline extends MultiKeyPipelineBase { return values; } + public void setResponseDependency(Response dependency) { + for (Response response : responses) { + response.setDependency(dependency); + } + } + public void addResponse(Response response) { responses.add(response); } @@ -106,6 +112,7 @@ public class Pipeline extends MultiKeyPipelineBase { public Response> exec() { client.exec(); Response> response = super.getResponse(currentMulti); + currentMulti.setResponseDependency(response); currentMulti = null; return response; } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index a753f96..681bc56 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.List; import redis.clients.jedis.exceptions.JedisAskDataException; +import redis.clients.jedis.exceptions.JedisClusterException; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisMovedDataException; @@ -16,6 +17,7 @@ public final class Protocol { private static final String ASK_RESPONSE = "ASK"; private static final String MOVED_RESPONSE = "MOVED"; + private static final String CLUSTERDOWN_RESPONSE = "CLUSTERDOWN"; public static final int DEFAULT_PORT = 6379; public static final int DEFAULT_SENTINEL_PORT = 26379; public static final int DEFAULT_TIMEOUT = 2000; @@ -96,6 +98,8 @@ public final class Protocol { throw new JedisAskDataException(message, new HostAndPort( askInfo[1], Integer.valueOf(askInfo[2])), Integer.valueOf(askInfo[0])); + } else if (message.startsWith(CLUSTERDOWN_RESPONSE)) { + throw new JedisClusterException(message); } throw new JedisDataException(message); } diff --git a/src/main/java/redis/clients/jedis/Response.java b/src/main/java/redis/clients/jedis/Response.java index b17f314..955277a 100644 --- a/src/main/java/redis/clients/jedis/Response.java +++ b/src/main/java/redis/clients/jedis/Response.java @@ -8,6 +8,8 @@ public class Response { private boolean set = false; private Builder builder; private Object data; + private Response dependency = null; + private boolean requestDependencyBuild = false; public Response(Builder b) { this.builder = b; @@ -19,23 +21,39 @@ public class Response { } public T get() { + // if response has dependency response and dependency is not built, + // build it first and no more!! + if (!requestDependencyBuild && dependency != null && dependency.set + && !dependency.built) { + requestDependencyBuild = true; + dependency.build(); + } if (!set) { throw new JedisDataException( "Please close pipeline or multi block before calling this method."); } if (!built) { - if (data != null) { - if (data instanceof JedisDataException) { - throw new JedisDataException((JedisDataException) data); - } - response = builder.build(data); - } - this.data = null; - built = true; + build(); } return response; } + public void setDependency(Response dependency) { + this.dependency = dependency; + this.requestDependencyBuild = false; + } + + private void build() { + if (data != null) { + if (data instanceof JedisDataException) { + throw new JedisDataException((JedisDataException) data); + } + response = builder.build(data); + } + data = null; + built = true; + } + public String toString() { return "Response " + builder.toString(); } diff --git a/src/main/java/redis/clients/util/JedisClusterCRC16.java b/src/main/java/redis/clients/util/JedisClusterCRC16.java index 3c5c9b5..270dbf0 100644 --- a/src/main/java/redis/clients/util/JedisClusterCRC16.java +++ b/src/main/java/redis/clients/util/JedisClusterCRC16.java @@ -2,7 +2,6 @@ package redis.clients.util; public class JedisClusterCRC16 { public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1 - static int crc; public static int getSlot(String key) { @@ -17,7 +16,7 @@ public class JedisClusterCRC16 { } private static int getCRC16(String key) { - crc = 0x0000; + int crc = 0x0000; for (byte b : key.getBytes()) { for (int i = 0; i < 8; i++) { boolean bit = ((b >> (7 - i) & 1) == 1); diff --git a/src/main/java/redis/clients/util/Pool.java b/src/main/java/redis/clients/util/Pool.java index 09d8ebb..659c731 100644 --- a/src/main/java/redis/clients/util/Pool.java +++ b/src/main/java/redis/clients/util/Pool.java @@ -45,6 +45,9 @@ public abstract class Pool { } public void returnResourceObject(final T resource) { + if (resource == null) { + return; + } try { internalPool.returnObject(resource); } catch (Exception e) { @@ -54,11 +57,15 @@ public abstract class Pool { } public void returnBrokenResource(final T resource) { - returnBrokenResourceObject(resource); + if (resource != null) { + returnBrokenResourceObject(resource); + } } public void returnResource(final T resource) { - returnResourceObject(resource); + if (resource != null) { + returnResourceObject(resource); + } } public void destroy() { @@ -81,4 +88,4 @@ public abstract class Pool { throw new JedisException("Could not destroy the pool", e); } } -} \ No newline at end of file +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java index a501024..514d3b5 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java @@ -198,4 +198,14 @@ public class JedisPoolTest extends Assert { pool.returnResource(jedis2); pool.destroy(); } + + @Test + public void returnNullObjectShouldNotFail() { + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), + hnp.getPort(), 2000, "foobared", 0, "my_shiny_client_name"); + + pool.returnBrokenResource(null); + pool.returnResource(null); + pool.returnResourceObject(null); + } } diff --git a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java index aed67dc..d3cddd0 100755 --- a/src/test/java/redis/clients/jedis/tests/PipeliningTest.java +++ b/src/test/java/redis/clients/jedis/tests/PipeliningTest.java @@ -251,6 +251,27 @@ public class PipeliningTest extends Assert { } + @Test + public void multiWithSync() { + jedis.set("foo", "314"); + jedis.set("bar", "foo"); + jedis.set("hello", "world"); + Pipeline p = jedis.pipelined(); + Response r1 = p.get("bar"); + p.multi(); + Response r2 = p.get("foo"); + p.exec(); + Response r3 = p.get("hello"); + p.sync(); + + // before multi + assertEquals("foo", r1.get()); + // It should be readable whether exec's response was built or not + assertEquals("314", r2.get()); + // after multi + assertEquals("world", r3.get()); + } + @Test public void testDiscardInPipeline() { Pipeline pipeline = jedis.pipelined();