From f62548931caa8352675c65e3232812c90e975d95 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Wed, 10 Nov 2010 13:41:41 -0300 Subject: [PATCH] Add a pool for sharded jedis --- .../java/redis/clients/jedis/Connection.java | 7 - src/main/java/redis/clients/jedis/Jedis.java | 219 +++++++++--------- .../redis/clients/jedis/ShardedJedis.java | 5 +- .../redis/clients/jedis/ShardedJedisPool.java | 82 +++++++ .../clients/jedis/tests/JedisPoolTest.java | 138 +++++------ .../jedis/tests/ShardedJedisPoolTest.java | 115 +++++++++ 6 files changed, 382 insertions(+), 184 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/ShardedJedisPool.java create mode 100644 src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 96bf728..a9c9cd6 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -50,13 +50,6 @@ public class Connection { } protected Connection sendCommand(String name, String... args) { - try { - connect(); - } catch (UnknownHostException e) { - throw new JedisException("Could not connect to redis-server", e); - } catch (IOException e) { - throw new JedisException("Could not connect to redis-server", e); - } protocol.sendCommand(outputStream, name, args); pipelinedCommands++; return this; diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 6feaf31..d70c2ac 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -14,6 +14,7 @@ import redis.clients.jedis.Client.LIST_POSITION; public class Jedis implements JedisCommands { private Client client = null; + private String password = null; public Jedis(String host) { client = new Client(host); @@ -31,13 +32,11 @@ public class Jedis implements JedisCommands { public Jedis(JedisShardInfo shardInfo) { client = new Client(shardInfo.getHost(), shardInfo.getPort()); client.setTimeout(shardInfo.getTimeout()); - if (shardInfo.getPassword() != null) { - this.auth(shardInfo.getPassword()); - } + this.password = shardInfo.getPassword(); } public String ping() { - checkIsInMulti(); + runChecks(); client.ping(); return client.getStatusCodeReply(); } @@ -53,7 +52,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String set(String key, String value) { - checkIsInMulti(); + runChecks(); client.set(key, value); return client.getStatusCodeReply(); } @@ -69,7 +68,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String get(String key) { - checkIsInMulti(); + runChecks(); client.sendCommand("GET", key); return client.getBulkReply(); } @@ -78,7 +77,7 @@ public class Jedis implements JedisCommands { * Ask the server to silently close the connection. */ public void quit() { - checkIsInMulti(); + runChecks(); client.quit(); } @@ -93,7 +92,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, "0" if the key exists, otherwise "1" */ public Integer exists(String key) { - checkIsInMulti(); + runChecks(); client.exists(key); return client.getIntegerReply(); } @@ -109,7 +108,7 @@ public class Jedis implements JedisCommands { * more keys were removed 0 if none of the specified key existed */ public Integer del(String... keys) { - checkIsInMulti(); + runChecks(); client.del(keys); return client.getIntegerReply(); } @@ -129,7 +128,7 @@ public class Jedis implements JedisCommands { * contains a Hash value */ public String type(String key) { - checkIsInMulti(); + runChecks(); client.type(key); return client.getStatusCodeReply(); } @@ -141,7 +140,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String flushDB() { - checkIsInMulti(); + runChecks(); client.flushDB(); return client.getStatusCodeReply(); } @@ -178,7 +177,7 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply */ public List keys(String pattern) { - checkIsInMulti(); + runChecks(); client.keys(pattern); return client.getMultiBulkReply(); } @@ -192,7 +191,7 @@ public class Jedis implements JedisCommands { * empty string is the database is empty */ public String randomKey() { - checkIsInMulti(); + runChecks(); client.randomKey(); return client.getBulkReply(); } @@ -209,7 +208,7 @@ public class Jedis implements JedisCommands { * @return Status code repy */ public String rename(String oldkey, String newkey) { - checkIsInMulti(); + runChecks(); client.rename(oldkey, newkey); return client.getStatusCodeReply(); } @@ -226,7 +225,7 @@ public class Jedis implements JedisCommands { * target key already exist */ public Integer renamenx(String oldkey, String newkey) { - checkIsInMulti(); + runChecks(); client.renamenx(oldkey, newkey); return client.getIntegerReply(); } @@ -237,7 +236,7 @@ public class Jedis implements JedisCommands { * @return Integer reply */ public Integer dbSize() { - checkIsInMulti(); + runChecks(); client.dbSize(); return client.getIntegerReply(); } @@ -271,7 +270,7 @@ public class Jedis implements JedisCommands { * exist. */ public Integer expire(String key, int seconds) { - checkIsInMulti(); + runChecks(); client.expire(key, seconds); return client.getIntegerReply(); } @@ -307,7 +306,7 @@ public class Jedis implements JedisCommands { * exist. */ public Integer expireAt(String key, long unixTime) { - checkIsInMulti(); + runChecks(); client.expireAt(key, unixTime); return client.getIntegerReply(); } @@ -324,7 +323,7 @@ public class Jedis implements JedisCommands { * have an associated expire, -1 is returned. */ public Integer ttl(String key) { - checkIsInMulti(); + runChecks(); client.ttl(key); return client.getIntegerReply(); } @@ -337,7 +336,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String select(int index) { - checkIsInMulti(); + runChecks(); client.select(index); return client.getStatusCodeReply(); } @@ -356,7 +355,7 @@ public class Jedis implements JedisCommands { * found in the current DB. */ public Integer move(String key, int dbIndex) { - checkIsInMulti(); + runChecks(); client.move(key, dbIndex); return client.getIntegerReply(); } @@ -368,7 +367,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String flushAll() { - checkIsInMulti(); + runChecks(); client.flushAll(); return client.getStatusCodeReply(); } @@ -385,7 +384,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String getSet(String key, String value) { - checkIsInMulti(); + runChecks(); client.getSet(key, value); return client.getBulkReply(); } @@ -401,7 +400,7 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply */ public List mget(String... keys) { - checkIsInMulti(); + runChecks(); client.mget(keys); return client.getMultiBulkReply(); } @@ -419,7 +418,7 @@ public class Jedis implements JedisCommands { * was not set */ public Integer setnx(String key, String value) { - checkIsInMulti(); + runChecks(); client.setnx(key, value); return client.getIntegerReply(); } @@ -437,7 +436,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String setex(String key, int seconds, String value) { - checkIsInMulti(); + runChecks(); client.setex(key, seconds, value); return client.getStatusCodeReply(); } @@ -463,7 +462,7 @@ public class Jedis implements JedisCommands { * @return Status code reply Basically +OK as MSET can't fail */ public String mset(String... keysvalues) { - checkIsInMulti(); + runChecks(); client.mset(keysvalues); return client.getStatusCodeReply(); } @@ -490,7 +489,7 @@ public class Jedis implements JedisCommands { * no key was set (at least one key already existed) */ public Integer msetnx(String... keysvalues) { - checkIsInMulti(); + runChecks(); client.msetnx(keysvalues); return client.getIntegerReply(); } @@ -518,7 +517,7 @@ public class Jedis implements JedisCommands { * after the increment. */ public Integer decrBy(String key, int integer) { - checkIsInMulti(); + runChecks(); client.decrBy(key, integer); return client.getIntegerReply(); } @@ -546,7 +545,7 @@ public class Jedis implements JedisCommands { * after the increment. */ public Integer decr(String key) { - checkIsInMulti(); + runChecks(); client.decr(key); return client.getIntegerReply(); } @@ -574,7 +573,7 @@ public class Jedis implements JedisCommands { * after the increment. */ public Integer incrBy(String key, int integer) { - checkIsInMulti(); + runChecks(); client.incrBy(key, integer); return client.getIntegerReply(); } @@ -602,7 +601,7 @@ public class Jedis implements JedisCommands { * after the increment. */ public Integer incr(String key) { - checkIsInMulti(); + runChecks(); client.incr(key); return client.getIntegerReply(); } @@ -624,7 +623,7 @@ public class Jedis implements JedisCommands { * the append operation. */ public Integer append(String key, String value) { - checkIsInMulti(); + runChecks(); client.append(key, value); return client.getIntegerReply(); } @@ -648,7 +647,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String substr(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.substr(key, start, end); return client.getBulkReply(); } @@ -669,7 +668,7 @@ public class Jedis implements JedisCommands { * 1 is returned. */ public Integer hset(String key, String field, String value) { - checkIsInMulti(); + runChecks(); client.hset(key, field, value); return client.getIntegerReply(); } @@ -688,7 +687,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String hget(String key, String field) { - checkIsInMulti(); + runChecks(); client.hget(key, field); return client.getBulkReply(); } @@ -705,7 +704,7 @@ public class Jedis implements JedisCommands { * field is created 1 is returned. */ public Integer hsetnx(String key, String field, String value) { - checkIsInMulti(); + runChecks(); client.hsetnx(key, field, value); return client.getIntegerReply(); } @@ -723,7 +722,7 @@ public class Jedis implements JedisCommands { * @return Always OK because HMSET can't fail */ public String hmset(String key, Map hash) { - checkIsInMulti(); + runChecks(); client.hmset(key, hash); return client.getStatusCodeReply(); } @@ -742,7 +741,7 @@ public class Jedis implements JedisCommands { * with the specified fields, in the same order of the request. */ public List hmget(String key, String... fields) { - checkIsInMulti(); + runChecks(); client.hmget(key, fields); return client.getMultiBulkReply(); } @@ -766,7 +765,7 @@ public class Jedis implements JedisCommands { * operation. */ public Integer hincrBy(String key, String field, int value) { - checkIsInMulti(); + runChecks(); client.hincrBy(key, field, value); return client.getIntegerReply(); } @@ -782,7 +781,7 @@ public class Jedis implements JedisCommands { * Return 0 if the key is not found or the field is not present. */ public Integer hexists(String key, String field) { - checkIsInMulti(); + runChecks(); client.hexists(key, field); return client.getIntegerReply(); } @@ -798,7 +797,7 @@ public class Jedis implements JedisCommands { * returned, otherwise 0 is returned and no operation is performed. */ public Integer hdel(String key, String field) { - checkIsInMulti(); + runChecks(); client.hdel(key, field); return client.getIntegerReply(); } @@ -814,7 +813,7 @@ public class Jedis implements JedisCommands { * an empty hash. */ public Integer hlen(String key) { - checkIsInMulti(); + runChecks(); client.hlen(key); return client.getIntegerReply(); } @@ -828,7 +827,7 @@ public class Jedis implements JedisCommands { * @return All the fields names contained into a hash. */ public List hkeys(String key) { - checkIsInMulti(); + runChecks(); client.hkeys(key); return client.getMultiBulkReply(); } @@ -842,7 +841,7 @@ public class Jedis implements JedisCommands { * @return All the fields values contained into a hash. */ public List hvals(String key) { - checkIsInMulti(); + runChecks(); client.hvals(key); return client.getMultiBulkReply(); } @@ -856,7 +855,7 @@ public class Jedis implements JedisCommands { * @return All the fields and values contained into a hash. */ public Map hgetAll(String key) { - checkIsInMulti(); + runChecks(); client.hgetAll(key); List flatHash = client.getMultiBulkReply(); Map hash = new HashMap(); @@ -884,7 +883,7 @@ public class Jedis implements JedisCommands { * list after the push operation. */ public Integer rpush(String key, String string) { - checkIsInMulti(); + runChecks(); client.rpush(key, string); return client.getIntegerReply(); } @@ -905,7 +904,7 @@ public class Jedis implements JedisCommands { * list after the push operation. */ public Integer lpush(String key, String string) { - checkIsInMulti(); + runChecks(); client.lpush(key, string); return client.getIntegerReply(); } @@ -921,7 +920,7 @@ public class Jedis implements JedisCommands { * @return The length of the list. */ public Integer llen(String key) { - checkIsInMulti(); + runChecks(); client.llen(key); return client.getIntegerReply(); } @@ -965,7 +964,7 @@ public class Jedis implements JedisCommands { * specified range. */ public List lrange(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.lrange(key, start, end); return client.getMultiBulkReply(); } @@ -1005,7 +1004,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String ltrim(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.ltrim(key, start, end); return client.getStatusCodeReply(); } @@ -1029,7 +1028,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply, specifically the requested element */ public String lindex(String key, int index) { - checkIsInMulti(); + runChecks(); client.lindex(key, index); return client.getBulkReply(); } @@ -1056,7 +1055,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String lset(String key, int index, String value) { - checkIsInMulti(); + runChecks(); client.lset(key, index, value); return client.getStatusCodeReply(); } @@ -1081,7 +1080,7 @@ public class Jedis implements JedisCommands { * the operation succeeded */ public Integer lrem(String key, int count, String value) { - checkIsInMulti(); + runChecks(); client.lrem(key, count, value); return client.getIntegerReply(); } @@ -1100,7 +1099,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String lpop(String key) { - checkIsInMulti(); + runChecks(); client.lpop(key); return client.getBulkReply(); } @@ -1119,7 +1118,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String rpop(String key) { - checkIsInMulti(); + runChecks(); client.rpop(key); return client.getBulkReply(); } @@ -1143,7 +1142,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String rpoplpush(String srckey, String dstkey) { - checkIsInMulti(); + runChecks(); client.rpoplpush(srckey, dstkey); return client.getBulkReply(); } @@ -1162,7 +1161,7 @@ public class Jedis implements JedisCommands { * the element was already a member of the set */ public Integer sadd(String key, String member) { - checkIsInMulti(); + runChecks(); client.sadd(key, member); return client.getIntegerReply(); } @@ -1177,7 +1176,7 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply */ public Set smembers(String key) { - checkIsInMulti(); + runChecks(); client.smembers(key); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); @@ -1196,7 +1195,7 @@ public class Jedis implements JedisCommands { * if the new element was not a member of the set */ public Integer srem(String key, String member) { - checkIsInMulti(); + runChecks(); client.srem(key, member); return client.getIntegerReply(); } @@ -1214,7 +1213,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String spop(String key) { - checkIsInMulti(); + runChecks(); client.spop(key); return client.getBulkReply(); } @@ -1243,7 +1242,7 @@ public class Jedis implements JedisCommands { * performed */ public Integer smove(String srckey, String dstkey, String member) { - checkIsInMulti(); + runChecks(); client.smove(srckey, dstkey, member); return client.getIntegerReply(); } @@ -1257,7 +1256,7 @@ public class Jedis implements JedisCommands { * of the set as an integer. */ public Integer scard(String key) { - checkIsInMulti(); + runChecks(); client.scard(key); return client.getIntegerReply(); } @@ -1275,7 +1274,7 @@ public class Jedis implements JedisCommands { * does not exist */ public Integer sismember(String key, String member) { - checkIsInMulti(); + runChecks(); client.sismember(key, member); return client.getIntegerReply(); } @@ -1300,7 +1299,7 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sinter(String... keys) { - checkIsInMulti(); + runChecks(); client.sinter(keys); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); @@ -1318,7 +1317,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public Integer sinterstore(String dstkey, String... keys) { - checkIsInMulti(); + runChecks(); client.sinterstore(dstkey, keys); return client.getIntegerReply(); } @@ -1340,7 +1339,7 @@ public class Jedis implements JedisCommands { * @return Multi bulk reply, specifically the list of common elements. */ public Set sunion(String... keys) { - checkIsInMulti(); + runChecks(); client.sunion(keys); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); @@ -1359,7 +1358,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public Integer sunionstore(String dstkey, String... keys) { - checkIsInMulti(); + runChecks(); client.sunionstore(dstkey, keys); return client.getIntegerReply(); } @@ -1388,7 +1387,7 @@ public class Jedis implements JedisCommands { * the first set provided and all the successive sets. */ public Set sdiff(String... keys) { - checkIsInMulti(); + runChecks(); client.sdiff(keys); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); @@ -1403,7 +1402,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public Integer sdiffstore(String dstkey, String... keys) { - checkIsInMulti(); + runChecks(); client.sdiffstore(dstkey, keys); return client.getIntegerReply(); } @@ -1421,7 +1420,7 @@ public class Jedis implements JedisCommands { * @return Bulk reply */ public String srandmember(String key) { - checkIsInMulti(); + runChecks(); client.srandmember(key); return client.getBulkReply(); } @@ -1448,13 +1447,13 @@ public class Jedis implements JedisCommands { * was updated */ public Integer zadd(String key, double score, String member) { - checkIsInMulti(); + runChecks(); client.zadd(key, score, member); return client.getIntegerReply(); } public Set zrange(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.zrange(key, start, end); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); @@ -1476,7 +1475,7 @@ public class Jedis implements JedisCommands { * if the new element was not a member of the set */ public Integer zrem(String key, String member) { - checkIsInMulti(); + runChecks(); client.zrem(key, member); return client.getIntegerReply(); } @@ -1506,7 +1505,7 @@ public class Jedis implements JedisCommands { * @return The new score */ public Double zincrby(String key, double score, String member) { - checkIsInMulti(); + runChecks(); client.zincrby(key, score, member); String newscore = client.getBulkReply(); return Double.valueOf(newscore); @@ -1533,7 +1532,7 @@ public class Jedis implements JedisCommands { * reply if there is no such element. */ public Integer zrank(String key, String member) { - checkIsInMulti(); + runChecks(); client.zrank(key, member); return client.getIntegerReply(); } @@ -1559,27 +1558,27 @@ public class Jedis implements JedisCommands { * reply if there is no such element. */ public Integer zrevrank(String key, String member) { - checkIsInMulti(); + runChecks(); client.zrevrank(key, member); return client.getIntegerReply(); } public Set zrevrange(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.zrevrange(key, start, end); List members = client.getMultiBulkReply(); return new LinkedHashSet(members); } public Set zrangeWithScores(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.zrangeWithScores(key, start, end); Set set = getTupledSet(); return set; } public Set zrevrangeWithScores(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.zrevrangeWithScores(key, start, end); Set set = getTupledSet(); return set; @@ -1595,7 +1594,7 @@ public class Jedis implements JedisCommands { * @return the cardinality (number of elements) of the set as an integer. */ public Integer zcard(String key) { - checkIsInMulti(); + runChecks(); client.zcard(key); return client.getIntegerReply(); } @@ -1612,7 +1611,7 @@ public class Jedis implements JedisCommands { * @return the score */ public Double zscore(String key, String member) { - checkIsInMulti(); + runChecks(); client.zscore(key, member); String score = client.getBulkReply(); return (score != null ? new Double(score) : null); @@ -1637,15 +1636,27 @@ public class Jedis implements JedisCommands { return results; } - private void checkIsInMulti() { + private void runChecks() { if (client.isInMulti()) { throw new JedisException( "Cannot use Jedis when in Multi. Please use JedisTransaction instead."); } + try { + this.connect(); + } catch (UnknownHostException e) { + throw new JedisException(e); + } catch (IOException e) { + throw new JedisException(e); + } } public void connect() throws UnknownHostException, IOException { - client.connect(); + if (!client.isConnected()) { + client.connect(); + if (this.password != null) { + this.auth(this.password); + } + } } public void disconnect() throws IOException { @@ -1680,7 +1691,7 @@ public class Jedis implements JedisCommands { * smallest to the biggest number. */ public List sort(String key) { - checkIsInMulti(); + runChecks(); client.sort(key); return client.getMultiBulkReply(); } @@ -1762,7 +1773,7 @@ public class Jedis implements JedisCommands { * @return a list of sorted elements. */ public List sort(String key, SortingParams sortingParameters) { - checkIsInMulti(); + runChecks(); client.sort(key, sortingParameters); return client.getMultiBulkReply(); } @@ -1840,7 +1851,7 @@ public class Jedis implements JedisCommands { * programming language used. */ public List blpop(int timeout, String... keys) { - checkIsInMulti(); + runChecks(); List args = new ArrayList(); for (String arg : keys) { args.add(arg); @@ -1869,7 +1880,7 @@ public class Jedis implements JedisCommands { */ public Integer sort(String key, SortingParams sortingParameters, String dstkey) { - checkIsInMulti(); + runChecks(); client.sort(key, sortingParameters, dstkey); return client.getIntegerReply(); } @@ -1891,7 +1902,7 @@ public class Jedis implements JedisCommands { * @return The number of elements of the list at dstkey. */ public Integer sort(String key, String dstkey) { - checkIsInMulti(); + runChecks(); client.sort(key, dstkey); return client.getIntegerReply(); } @@ -1969,7 +1980,7 @@ public class Jedis implements JedisCommands { * programming language used. */ public List brpop(int timeout, String... keys) { - checkIsInMulti(); + runChecks(); List args = new ArrayList(); for (String arg : keys) { args.add(arg); @@ -2000,7 +2011,7 @@ public class Jedis implements JedisCommands { * @return Status code reply */ public String auth(String password) { - checkIsInMulti(); + runChecks(); client.auth(password); return client.getStatusCodeReply(); } @@ -2029,7 +2040,7 @@ public class Jedis implements JedisCommands { } public Integer zcount(String key, double min, double max) { - checkIsInMulti(); + runChecks(); client.zcount(key, min, max); return client.getIntegerReply(); } @@ -2092,13 +2103,13 @@ public class Jedis implements JedisCommands { * score range. */ public Set zrangeByScore(String key, double min, double max) { - checkIsInMulti(); + runChecks(); client.zrangeByScore(key, min, max); return new LinkedHashSet(client.getMultiBulkReply()); } public Set zrangeByScore(String key, String min, String max) { - checkIsInMulti(); + runChecks(); client.zrangeByScore(key, min, max); return new LinkedHashSet(client.getMultiBulkReply()); } @@ -2161,7 +2172,7 @@ public class Jedis implements JedisCommands { */ public Set zrangeByScore(String key, double min, double max, int offset, int count) { - checkIsInMulti(); + runChecks(); client.zrangeByScore(key, min, max, offset, count); return new LinkedHashSet(client.getMultiBulkReply()); } @@ -2223,7 +2234,7 @@ public class Jedis implements JedisCommands { * score range. */ public Set zrangeByScoreWithScores(String key, double min, double max) { - checkIsInMulti(); + runChecks(); client.zrangeByScoreWithScores(key, min, max); Set set = getTupledSet(); return set; @@ -2287,14 +2298,14 @@ public class Jedis implements JedisCommands { */ public Set zrangeByScoreWithScores(String key, double min, double max, int offset, int count) { - checkIsInMulti(); + runChecks(); client.zrangeByScoreWithScores(key, min, max, offset, count); Set set = getTupledSet(); return set; } private Set getTupledSet() { - checkIsInMulti(); + runChecks(); List membersWithScores = client.getMultiBulkReply(); Set set = new LinkedHashSet(); Iterator iterator = membersWithScores.iterator(); @@ -2320,7 +2331,7 @@ public class Jedis implements JedisCommands { * */ public Integer zremrangeByRank(String key, int start, int end) { - checkIsInMulti(); + runChecks(); client.zremrangeByRank(key, start, end); return client.getIntegerReply(); } @@ -2340,7 +2351,7 @@ public class Jedis implements JedisCommands { * @return Integer reply, specifically the number of elements removed. */ public Integer zremrangeByScore(String key, double start, double end) { - checkIsInMulti(); + runChecks(); client.zremrangeByScore(key, start, end); return client.getIntegerReply(); } @@ -2384,7 +2395,7 @@ public class Jedis implements JedisCommands { * set at dstkey */ public Integer zunionstore(String dstkey, String... sets) { - checkIsInMulti(); + runChecks(); client.zunionstore(dstkey, sets); return client.getIntegerReply(); } @@ -2429,7 +2440,7 @@ public class Jedis implements JedisCommands { * set at dstkey */ public Integer zunionstore(String dstkey, ZParams params, String... sets) { - checkIsInMulti(); + runChecks(); client.zunionstore(dstkey, params, sets); return client.getIntegerReply(); } @@ -2473,7 +2484,7 @@ public class Jedis implements JedisCommands { * set at dstkey */ public Integer zinterstore(String dstkey, String... sets) { - checkIsInMulti(); + runChecks(); client.zinterstore(dstkey, sets); return client.getIntegerReply(); } @@ -2518,7 +2529,7 @@ public class Jedis implements JedisCommands { * set at dstkey */ public Integer zinterstore(String dstkey, ZParams params, String... sets) { - checkIsInMulti(); + runChecks(); client.zinterstore(dstkey, params, sets); return client.getIntegerReply(); } diff --git a/src/main/java/redis/clients/jedis/ShardedJedis.java b/src/main/java/redis/clients/jedis/ShardedJedis.java index a56f7dc..a14b0a8 100644 --- a/src/main/java/redis/clients/jedis/ShardedJedis.java +++ b/src/main/java/redis/clients/jedis/ShardedJedis.java @@ -31,14 +31,11 @@ public class ShardedJedis extends Sharded implements public void disconnect() throws IOException { for (JedisShardInfo jedis : getAllShards()) { + jedis.getResource().quit(); jedis.getResource().disconnect(); } } - protected Jedis create(JedisShardInfo shard) { - return new Jedis(shard); - } - public String set(String key, String value) { Jedis j = getShard(key); return j.set(key, value); diff --git a/src/main/java/redis/clients/jedis/ShardedJedisPool.java b/src/main/java/redis/clients/jedis/ShardedJedisPool.java new file mode 100644 index 0000000..20045ef --- /dev/null +++ b/src/main/java/redis/clients/jedis/ShardedJedisPool.java @@ -0,0 +1,82 @@ +package redis.clients.jedis; + +import java.util.List; +import java.util.regex.Pattern; + +import redis.clients.util.FixedResourcePool; +import redis.clients.util.Hashing; + +public class ShardedJedisPool extends FixedResourcePool { + private List shards; + private Hashing algo = Hashing.MD5; + private Pattern keyTagPattern; + + public ShardedJedisPool(List shards) { + this.shards = shards; + } + + public ShardedJedisPool(List shards, Hashing algo) { + this.shards = shards; + this.algo = algo; + } + + public ShardedJedisPool(List shards, Pattern keyTagPattern) { + this.shards = shards; + this.keyTagPattern = keyTagPattern; + } + + public ShardedJedisPool(List shards, Hashing algo, + Pattern keyTagPattern) { + this.shards = shards; + this.algo = algo; + this.keyTagPattern = keyTagPattern; + } + + @Override + protected ShardedJedis createResource() { + ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern); + boolean done = false; + while (!done) { + try { + for (JedisShardInfo shard : jedis.getAllShards()) { + if (!shard.getResource().isConnected()) { + shard.getResource().connect(); + } + } + done = true; + } catch (Exception e) { + try { + Thread.sleep(100); + } catch (InterruptedException e1) { + } + } + } + return jedis; + } + + @Override + protected void destroyResource(ShardedJedis jedis) { + if (jedis != null) { + try { + jedis.disconnect(); + } catch (Exception e) { + + } + } + } + + @Override + protected boolean isResourceValid(ShardedJedis jedis) { + try { + for (JedisShardInfo shard : jedis.getAllShards()) { + if (!shard.getResource().isConnected() + || !shard.getResource().ping().equals("PONG")) { + return false; + } + } + return true; + } catch (Exception ex) { + return false; + } + } +} diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java index b7eb7f8..e106499 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java @@ -11,85 +11,85 @@ import redis.clients.jedis.JedisPool; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; public class JedisPoolTest extends Assert { - private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); - - @Test - public void checkConnections() throws TimeoutException { - JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000); - pool.setResourcesNumber(10); - pool.init(); + private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); - Jedis jedis = pool.getResource(200); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - pool.returnResource(jedis); - pool.destroy(); - } + @Test + public void checkConnections() throws TimeoutException { + JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000); + pool.setResourcesNumber(10); + pool.init(); - @Test - public void checkConnectionWithDefaultPort() throws TimeoutException { - JedisPool pool = new JedisPool(hnp.host, hnp.port); - pool.setResourcesNumber(10); - pool.init(); + Jedis jedis = pool.getResource(200); + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); + } - Jedis jedis = pool.getResource(200); - jedis.auth("foobared"); - jedis.set("foo", "bar"); - assertEquals("bar", jedis.get("foo")); - pool.returnResource(jedis); - pool.destroy(); - } + @Test + public void checkConnectionWithDefaultPort() throws TimeoutException { + JedisPool pool = new JedisPool(hnp.host, hnp.port); + pool.setResourcesNumber(10); + pool.init(); - @Test - public void checkJedisIsReusedWhenReturned() throws TimeoutException { - JedisPool pool = new JedisPool(hnp.host, hnp.port); - pool.setResourcesNumber(1); - pool.init(); + Jedis jedis = pool.getResource(200); + jedis.auth("foobared"); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); + } - Jedis jedis = pool.getResource(200); - jedis.auth("foobared"); - jedis.set("foo", "0"); - pool.returnResource(jedis); + @Test + public void checkJedisIsReusedWhenReturned() throws TimeoutException { + JedisPool pool = new JedisPool(hnp.host, hnp.port); + pool.setResourcesNumber(1); + pool.init(); - jedis = pool.getResource(200); - jedis.auth("foobared"); - jedis.incr("foo"); - pool.returnResource(jedis); - pool.destroy(); - } + Jedis jedis = pool.getResource(200); + jedis.auth("foobared"); + jedis.set("foo", "0"); + pool.returnResource(jedis); - @Test - public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException, - IOException { - JedisPool pool = new JedisPool(hnp.host, hnp.port); - pool.setResourcesNumber(1); - pool.init(); + jedis = pool.getResource(200); + jedis.auth("foobared"); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); + } - Jedis jedis = pool.getResource(200); - jedis.auth("foobared"); - jedis.quit(); - pool.returnBrokenResource(jedis); + @Test + public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException, + IOException { + JedisPool pool = new JedisPool(hnp.host, hnp.port); + pool.setResourcesNumber(1); + pool.init(); - jedis = pool.getResource(200); - jedis.auth("foobared"); - jedis.incr("foo"); - pool.returnResource(jedis); - pool.destroy(); - } + Jedis jedis = pool.getResource(200); + jedis.auth("foobared"); + jedis.quit(); + pool.returnBrokenResource(jedis); - @Test(expected = TimeoutException.class) - public void checkPoolOverflow() throws TimeoutException { - JedisPool pool = new JedisPool(hnp.host, hnp.port); - pool.setResourcesNumber(1); - pool.init(); + jedis = pool.getResource(200); + jedis.auth("foobared"); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); + } - Jedis jedis = pool.getResource(200); - jedis.auth("foobared"); - jedis.set("foo", "0"); + @Test(expected = TimeoutException.class) + public void checkPoolOverflow() throws TimeoutException { + JedisPool pool = new JedisPool(hnp.host, hnp.port); + pool.setResourcesNumber(1); + pool.init(); - Jedis newJedis = pool.getResource(200); - newJedis.auth("foobared"); - newJedis.incr("foo"); - } + Jedis jedis = pool.getResource(200); + jedis.auth("foobared"); + jedis.set("foo", "0"); + + Jedis newJedis = pool.getResource(200); + newJedis.auth("foobared"); + newJedis.incr("foo"); + } } \ No newline at end of file diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java new file mode 100644 index 0000000..fca100d --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java @@ -0,0 +1,115 @@ +package redis.clients.jedis.tests; + +import java.io.IOException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeoutException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisShardInfo; +import redis.clients.jedis.ShardedJedis; +import redis.clients.jedis.ShardedJedisPool; +import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; + +public class ShardedJedisPoolTest extends Assert { + private static HostAndPort redis1 = HostAndPortUtil.getRedisServers() + .get(0); + private static HostAndPort redis2 = HostAndPortUtil.getRedisServers() + .get(1); + + private List shards; + + @Before + public void startUp() throws UnknownHostException, IOException { + shards = new ArrayList(); + shards.add(new JedisShardInfo(redis1.host, redis1.port)); + shards.add(new JedisShardInfo(redis2.host, redis2.port)); + shards.get(0).setPassword("foobared"); + shards.get(1).setPassword("foobared"); + Jedis j = new Jedis(shards.get(0)); + j.connect(); + j.flushAll(); + j.disconnect(); + j = new Jedis(shards.get(1)); + j.connect(); + j.flushAll(); + j.disconnect(); + } + + @Test + public void checkConnections() throws TimeoutException { + ShardedJedisPool pool = new ShardedJedisPool(shards); + pool.setResourcesNumber(10); + pool.init(); + + ShardedJedis jedis = pool.getResource(200); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); + } + + @Test + public void checkConnectionWithDefaultPort() throws TimeoutException { + ShardedJedisPool pool = new ShardedJedisPool(shards); + pool.setResourcesNumber(10); + pool.init(); + + ShardedJedis jedis = pool.getResource(200); + jedis.set("foo", "bar"); + assertEquals("bar", jedis.get("foo")); + pool.returnResource(jedis); + pool.destroy(); + } + + @Test + public void checkJedisIsReusedWhenReturned() throws TimeoutException { + ShardedJedisPool pool = new ShardedJedisPool(shards); + pool.setResourcesNumber(1); + pool.init(); + + ShardedJedis jedis = pool.getResource(200); + jedis.set("foo", "0"); + pool.returnResource(jedis); + + jedis = pool.getResource(200); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); + } + + @Test + public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException, + IOException { + ShardedJedisPool pool = new ShardedJedisPool(shards); + pool.setResourcesNumber(1); + pool.init(); + + ShardedJedis jedis = pool.getResource(200); + jedis.disconnect(); + pool.returnBrokenResource(jedis); + + jedis = pool.getResource(200); + jedis.incr("foo"); + pool.returnResource(jedis); + pool.destroy(); + } + + @Test(expected = TimeoutException.class) + public void checkPoolOverflow() throws TimeoutException { + ShardedJedisPool pool = new ShardedJedisPool(shards); + pool.setResourcesNumber(1); + pool.init(); + + ShardedJedis jedis = pool.getResource(200); + jedis.set("foo", "0"); + + ShardedJedis newJedis = pool.getResource(200); + newJedis.incr("foo"); + } +} \ No newline at end of file