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:
Marcos Nils
2014-01-03 16:42:21 -03:00
parent 1b26815799
commit dd0bbdaf91
10 changed files with 349 additions and 265 deletions

View File

@@ -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);
}
}
}