use of redis.clients.jedis.Connection in project jedis by xetorthio.
the class RetryableCommandExecutor method executeCommand.
@Override
public final <T> T executeCommand(CommandObject<T> commandObject) {
Instant deadline = Instant.now().plus(maxTotalRetriesDuration);
int consecutiveConnectionFailures = 0;
JedisException lastException = null;
for (int attemptsLeft = this.maxAttempts; attemptsLeft > 0; attemptsLeft--) {
Connection connection = null;
try {
connection = provider.getConnection(commandObject.getArguments());
return connection.executeCommand(commandObject);
} catch (JedisConnectionException jce) {
lastException = jce;
++consecutiveConnectionFailures;
log.debug("Failed connecting to Redis: {}", connection, jce);
// "- 1" because we just did one, but the attemptsLeft counter hasn't been decremented yet
boolean reset = handleConnectionProblem(attemptsLeft - 1, consecutiveConnectionFailures, deadline);
if (reset) {
consecutiveConnectionFailures = 0;
}
} finally {
if (connection != null) {
connection.close();
}
}
if (Instant.now().isAfter(deadline)) {
throw new JedisException("Cluster retry deadline exceeded.");
}
}
JedisException maxAttemptsException = new JedisException("No more cluster attempts left.");
maxAttemptsException.addSuppressed(lastException);
throw maxAttemptsException;
}
use of redis.clients.jedis.Connection 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.Connection in project jedis by xetorthio.
the class ClusterConnectionProvider method initializeSlotsCache.
private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig clientConfig) {
ArrayList<HostAndPort> startNodeList = new ArrayList<>(startNodes);
Collections.shuffle(startNodeList);
for (HostAndPort hostAndPort : startNodeList) {
try (Connection jedis = new Connection(hostAndPort, clientConfig)) {
cache.discoverClusterNodesAndSlots(jedis);
return;
} catch (JedisConnectionException e) {
// try next nodes
}
}
throw new JedisClusterOperationException("Could not initialize cluster slots cache.");
}
use of redis.clients.jedis.Connection in project weicoder by wdcode.
the class JedisBuilder method buildCluster.
/**
* 构建Jedis集群
*
* @param name 名称
* @return Jedis集群
*/
public static JedisCluster buildCluster(String name) {
// 实例化Jedis配置
GenericObjectPoolConfig<Connection> config = new GenericObjectPoolConfig<Connection>();
// 设置属性
config.setMaxTotal(RedisParams.getMaxTotal(name));
config.setMaxIdle(RedisParams.getMaxIdle(name));
config.setMaxWait(Duration.ofMillis(RedisParams.getMaxWait(name)));
// 服务器节点
Set<HostAndPort> nodes = Sets.newSet();
for (String server : RedisParams.getCluster(name)) {
String[] s = StringUtil.split(server, StringConstants.COLON);
nodes.add(new HostAndPort(s[0], W.C.toInt(s[1])));
}
// 生成JedisCluster
Logs.info("redis init cluster nodes={}", nodes);
return new JedisCluster(nodes, RedisParams.getTimeOut(name), RedisParams.getTimeOut(name), 5, RedisParams.getPassword(name), config);
}
Aggregations