ASK Redirection don't update slot->node

This commit is contained in:
zhanghailei
2014-09-26 10:06:04 +08:00
parent 7836531ad7
commit 1578cae9fc
4 changed files with 144 additions and 12 deletions

View File

@@ -13,6 +13,7 @@ public abstract class JedisClusterCommand<T> {
private JedisClusterConnectionHandler connectionHandler;
private int commandTimeout;
private int redirections;
private ThreadLocal<Jedis> askConnection = new ThreadLocal<Jedis>();
public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler,
int timeout, int maxRedirections) {
@@ -41,20 +42,23 @@ public abstract class JedisClusterCommand<T> {
Jedis connection = null;
try {
if (tryRandomNode) {
connection = connectionHandler.getConnection();
} else {
connection = connectionHandler
.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
}
if (asking) {
// TODO: Pipeline asking with the original command to make it
// faster....
connection = askConnection.get();
connection.asking();
// if asking success, reset asking flag
asking = false;
}else{
if (tryRandomNode) {
connection = connectionHandler.getConnection();
} else {
connection = connectionHandler
.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
}
}
return execute(connection);
@@ -72,8 +76,7 @@ public abstract class JedisClusterCommand<T> {
} catch (JedisRedirectionException jre) {
if (jre instanceof JedisAskDataException) {
asking = true;
this.connectionHandler.assignSlotToNode(jre.getSlot(),
jre.getTargetNode());
askConnection.set(this.connectionHandler.getConnectionFromNode(jre.getTargetNode()));
} else if (jre instanceof JedisMovedDataException) {
// it rebuilds cluster's slot cache
// recommended by Redis cluster specification
@@ -102,4 +105,4 @@ public abstract class JedisClusterCommand<T> {
}
}
}
}

View File

@@ -26,6 +26,12 @@ public abstract class JedisClusterConnectionHandler {
abstract Jedis getConnectionFromSlot(int slot);
public Jedis getConnectionFromNode(HostAndPort node) {
cache.setNodeIfNotExist(node);
return cache.getNode(JedisClusterInfoCache.getNodeKey(node))
.getResource();
}
public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig) {
this.cache = new JedisClusterInfoCache(poolConfig);
initializeSlotsCache(nodes, poolConfig);