diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index c1a34ce..121bde8 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -27,7 +27,7 @@ public class JedisCluster implements JedisCommands, BasicCommands { public JedisCluster(Set jedisClusterNode, int timeout, int maxRedirections) { - this.connectionHandler = new JedisSlotBasedConnectionGuaranteedConnectionHandler( + this.connectionHandler = new JedisSlotBasedConnectionHandler( jedisClusterNode); this.timeout = timeout; this.maxRedirections = maxRedirections; diff --git a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionGuaranteedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionGuaranteedConnectionHandler.java deleted file mode 100644 index 0fe2cec..0000000 --- a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionGuaranteedConnectionHandler.java +++ /dev/null @@ -1,68 +0,0 @@ -package redis.clients.jedis; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -import redis.clients.jedis.exceptions.JedisConnectionException; - -public class JedisSlotBasedConnectionGuaranteedConnectionHandler extends - JedisSlotBasedConnectionHandler { - - public JedisSlotBasedConnectionGuaranteedConnectionHandler( - Set nodes) { - super(nodes); - } - - public Jedis getConnection() { - // 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; - - pool.returnBrokenResource(jedis); - } catch (JedisConnectionException ex) { - if (jedis != null) { - pool.returnBrokenResource(jedis); - } - } - } - - throw new JedisConnectionException("no reachable node in cluster"); - } - - @Override - public Jedis getConnectionFromSlot(int slot) { - JedisPool connectionPool = slots.get(slot); - if (connectionPool != null) { - // It can't guaranteed to get valid connection because of node assignment - return connectionPool.getResource(); - } else { - return getConnection(); - } - } - - 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/JedisSlotBasedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java index 18aa424..4cd4fc7 100644 --- a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java @@ -1,7 +1,12 @@ package redis.clients.jedis; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Set; +import redis.clients.jedis.exceptions.JedisConnectionException; + public class JedisSlotBasedConnectionHandler extends JedisClusterConnectionHandler { @@ -10,7 +15,35 @@ public class JedisSlotBasedConnectionHandler extends } public Jedis getConnection() { - return 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; + + pool.returnBrokenResource(jedis); + } catch (JedisConnectionException ex) { + if (jedis != null) { + pool.returnBrokenResource(jedis); + } + } + } + + throw new JedisConnectionException("no reachable node in cluster"); } @Override @@ -21,10 +54,19 @@ public class JedisSlotBasedConnectionHandler extends @Override public Jedis getConnectionFromSlot(int slot) { 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(); } - return connectionPool.getResource(); + } + + private List getShuffledNodesPool() { + List pools = new ArrayList(); + pools.addAll(nodes.values()); + Collections.shuffle(pools); + return pools; } }