use of redis.clients.jedis.ConnectionPool in project jedis by xetorthio.
the class ClusterConnectionProvider method getConnectionFromSlot.
public Connection getConnectionFromSlot(int slot) {
ConnectionPool connectionPool = cache.getSlotPool(slot);
if (connectionPool != null) {
// It can't guaranteed to get valid connection because of node assignment
return connectionPool.getResource();
} else {
// It's abnormal situation for cluster mode that we have just nothing for slot.
// Try to rediscover state
renewSlotCache();
connectionPool = cache.getSlotPool(slot);
if (connectionPool != null) {
return connectionPool.getResource();
} else {
// no choice, fallback to new connection to random node
return getConnection();
}
}
}
use of redis.clients.jedis.ConnectionPool in project jedis by xetorthio.
the class ShardedConnectionProvider method getConnection.
@Override
public Connection getConnection() {
List<ConnectionPool> pools = getShuffledNodesPool();
JedisException suppressed = null;
for (ConnectionPool pool : pools) {
Connection jedis = null;
try {
jedis = pool.getResource();
if (jedis == null) {
continue;
}
jedis.ping();
return jedis;
} catch (JedisException ex) {
if (suppressed == null) {
// remembering first suppressed exception
suppressed = ex;
}
if (jedis != null) {
jedis.close();
}
}
}
JedisException noReachableNode = new JedisException("No reachable shard.");
if (suppressed != null) {
noReachableNode.addSuppressed(suppressed);
}
throw noReachableNode;
}
use of redis.clients.jedis.ConnectionPool in project jedis by xetorthio.
the class ShardedConnectionProvider method reset.
private void reset() {
for (ConnectionPool pool : resources.values()) {
try {
if (pool != null) {
pool.destroy();
}
} catch (RuntimeException e) {
// pass
}
}
resources.clear();
nodes.clear();
}
use of redis.clients.jedis.ConnectionPool in project jedis by xetorthio.
the class ClusterConnectionProvider method getConnection.
@Override
public Connection 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<ConnectionPool> pools = cache.getShuffledNodesPool();
JedisException suppressed = null;
for (ConnectionPool pool : pools) {
Connection jedis = null;
try {
jedis = pool.getResource();
if (jedis == null) {
continue;
}
jedis.ping();
return jedis;
} catch (JedisException ex) {
if (suppressed == null) {
// remembering first suppressed exception
suppressed = ex;
}
if (jedis != null) {
jedis.close();
}
}
}
JedisClusterOperationException noReachableNode = new JedisClusterOperationException("No reachable node in cluster.");
if (suppressed != null) {
noReachableNode.addSuppressed(suppressed);
}
throw noReachableNode;
}
use of redis.clients.jedis.ConnectionPool in project jedis by xetorthio.
the class ShardedConnectionProvider method setupNodeIfNotExist.
private ConnectionPool setupNodeIfNotExist(final HostAndPort node) {
String nodeKey = node.toString();
ConnectionPool existingPool = resources.get(nodeKey);
if (existingPool != null)
return existingPool;
ConnectionPool nodePool = new ConnectionPool(node, clientConfig, poolConfig);
resources.put(nodeKey, nodePool);
return nodePool;
}
Aggregations