Search in sources :

Example 1 with ConnectionPool

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

Example 2 with ConnectionPool

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

Example 3 with ConnectionPool

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

Example 4 with ConnectionPool

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;
}
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 5 with ConnectionPool

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

Aggregations

ConnectionPool (redis.clients.jedis.ConnectionPool)5 Connection (redis.clients.jedis.Connection)2 JedisException (redis.clients.jedis.exceptions.JedisException)2 JedisClusterOperationException (redis.clients.jedis.exceptions.JedisClusterOperationException)1