Search in sources :

Example 6 with Connection

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;
}
Also used : JedisException(redis.clients.jedis.exceptions.JedisException) Instant(java.time.Instant) Connection(redis.clients.jedis.Connection) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 7 with Connection

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;
}
Also used : ConnectionPool(redis.clients.jedis.ConnectionPool) JedisClusterOperationException(redis.clients.jedis.exceptions.JedisClusterOperationException) JedisException(redis.clients.jedis.exceptions.JedisException) Connection(redis.clients.jedis.Connection)

Example 8 with Connection

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.");
}
Also used : JedisClusterOperationException(redis.clients.jedis.exceptions.JedisClusterOperationException) HostAndPort(redis.clients.jedis.HostAndPort) ArrayList(java.util.ArrayList) Connection(redis.clients.jedis.Connection) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 9 with Connection

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);
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisCluster(redis.clients.jedis.JedisCluster) Connection(redis.clients.jedis.Connection)

Aggregations

Connection (redis.clients.jedis.Connection)9 HashMap (java.util.HashMap)3 List (java.util.List)3 JSONObject (org.json.JSONObject)3 Test (org.junit.Test)3 Pipeline (redis.clients.jedis.Pipeline)3 JedisException (redis.clients.jedis.exceptions.JedisException)3 IRLObject (redis.clients.jedis.modules.json.JsonObjects.IRLObject)3 Instant (java.time.Instant)2 Map (java.util.Map)2 ConnectionPool (redis.clients.jedis.ConnectionPool)2 HostAndPort (redis.clients.jedis.HostAndPort)2 JedisClusterOperationException (redis.clients.jedis.exceptions.JedisClusterOperationException)2 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)2 JsonSetParams (redis.clients.jedis.json.JsonSetParams)2 RediSearchUtil.toStringMap (redis.clients.jedis.search.RediSearchUtil.toStringMap)2 ArrayList (java.util.ArrayList)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 BeforeClass (org.junit.BeforeClass)1 JedisCluster (redis.clients.jedis.JedisCluster)1