Several changes have been added to this commit:
- Add asking to cluster commands - Make jedis cluster return connection to original pool - Add tests for MOVED and ASK cluster responses - Refactor connection handler to recalculate connections based on slots This commit makes the first usable version of Jedis along with Redis Cluster
This commit is contained in:
@@ -1,27 +1,41 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import redis.clients.jedis.exceptions.JedisAskDataException;
|
||||
import redis.clients.jedis.exceptions.JedisClusterException;
|
||||
import redis.clients.jedis.exceptions.JedisMovedDataException;
|
||||
import redis.clients.jedis.tests.utils.JedisClusterCRC16;
|
||||
|
||||
public abstract class JedisClusterCommand<T> {
|
||||
|
||||
private JedisClusterConnectionHandler connectionHandler;
|
||||
private boolean asking = false;
|
||||
|
||||
private JedisClusterConnectionHandler connectionHandler;
|
||||
// private boolean asking = false;
|
||||
|
||||
public JedisClusterCommand(JedisClusterConnectionHandler connectionHandler) {
|
||||
this.connectionHandler = connectionHandler;
|
||||
}
|
||||
|
||||
public abstract T execute();
|
||||
|
||||
public T run() {
|
||||
public T run(String key) {
|
||||
try {
|
||||
if (key == null) {
|
||||
throw new JedisClusterException("No way to dispatch this command to Redis Cluster.");
|
||||
}
|
||||
connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
|
||||
if (asking) {
|
||||
//TODO: Pipeline asking with the original command to make it faster....
|
||||
connectionHandler.getConnection().asking();
|
||||
}
|
||||
return execute();
|
||||
} catch (JedisMovedDataException jme) {
|
||||
this.connectionHandler.assignSlotToNode(jme.getSlot(), jme.getTargetNode());
|
||||
return execute();
|
||||
return run(key);
|
||||
} catch (JedisAskDataException jae) {
|
||||
throw jae;
|
||||
asking = true;
|
||||
this.connectionHandler.assignSlotToNode(jae.getSlot(), jae.getTargetNode());
|
||||
return run(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user