Merge JedisSlotBasedConnectionGuaranteedConnectionHandler to JedisSlotBasedConnectionHandler
This commit is contained in:
@@ -27,7 +27,7 @@ public class JedisCluster implements JedisCommands, BasicCommands {
|
||||
|
||||
public JedisCluster(Set<HostAndPort> jedisClusterNode, int timeout,
|
||||
int maxRedirections) {
|
||||
this.connectionHandler = new JedisSlotBasedConnectionGuaranteedConnectionHandler(
|
||||
this.connectionHandler = new JedisSlotBasedConnectionHandler(
|
||||
jedisClusterNode);
|
||||
this.timeout = timeout;
|
||||
this.maxRedirections = maxRedirections;
|
||||
|
||||
@@ -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<HostAndPort> 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<JedisPool> 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<JedisPool> getShuffledNodesPool() {
|
||||
List<JedisPool> pools = new ArrayList<JedisPool>();
|
||||
pools.addAll(nodes.values());
|
||||
Collections.shuffle(pools);
|
||||
return pools;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<JedisPool> 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<JedisPool> getShuffledNodesPool() {
|
||||
List<JedisPool> pools = new ArrayList<JedisPool>();
|
||||
pools.addAll(nodes.values());
|
||||
Collections.shuffle(pools);
|
||||
return pools;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user