Search in sources :

Example 1 with JedisClusterMaxRedirectionsException

use of redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException in project cachecloud by sohutv.

the class JedisClusterCommand method runWithRetries.

private T runWithRetries(byte[] key, int redirections, boolean tryRandomNode, boolean asking) {
    if (redirections <= 0) {
        JedisClusterMaxRedirectionsException exception = new JedisClusterMaxRedirectionsException("Too many Cluster redirections? key=" + SafeEncoder.encode(key));
        //收集
        UsefulDataCollector.collectException(exception, "", System.currentTimeMillis(), ClientExceptionType.REDIS_CLUSTER);
        throw exception;
    }
    Jedis connection = null;
    try {
        if (asking) {
            // TODO: Pipeline asking with the original command to make it
            // faster....
            connection = askConnection.get();
            connection.asking();
            // if asking success, reset asking flag
            asking = false;
        } else {
            if (tryRandomNode) {
                connection = connectionHandler.getConnection();
            } else {
                connection = connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
            }
        }
        return execute(connection);
    } catch (JedisConnectionException jce) {
        if (tryRandomNode) {
            // maybe all connection is down
            throw jce;
        }
        // release current connection before recursion
        releaseConnection(connection);
        connection = null;
        // retry with random connection
        return runWithRetries(key, redirections - 1, true, asking);
    } catch (JedisRedirectionException jre) {
        // if MOVED redirection occurred,
        if (jre instanceof JedisMovedDataException) {
            // it rebuilds cluster's slot cache
            // recommended by Redis cluster specification
            this.connectionHandler.renewSlotCache(connection);
        }
        // release current connection before recursion or renewing
        releaseConnection(connection);
        connection = null;
        if (jre instanceof JedisAskDataException) {
            asking = true;
            askConnection.set(this.connectionHandler.getConnectionFromNode(jre.getTargetNode()));
        } else if (jre instanceof JedisMovedDataException) {
        } else {
            throw new JedisClusterException(jre);
        }
        return runWithRetries(key, redirections - 1, false, asking);
    } finally {
        releaseConnection(connection);
    }
}
Also used : JedisClusterException(redis.clients.jedis.exceptions.JedisClusterException) JedisClusterMaxRedirectionsException(redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException) JedisMovedDataException(redis.clients.jedis.exceptions.JedisMovedDataException) JedisAskDataException(redis.clients.jedis.exceptions.JedisAskDataException) JedisRedirectionException(redis.clients.jedis.exceptions.JedisRedirectionException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Aggregations

JedisAskDataException (redis.clients.jedis.exceptions.JedisAskDataException)1 JedisClusterException (redis.clients.jedis.exceptions.JedisClusterException)1 JedisClusterMaxRedirectionsException (redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException)1 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)1 JedisMovedDataException (redis.clients.jedis.exceptions.JedisMovedDataException)1 JedisRedirectionException (redis.clients.jedis.exceptions.JedisRedirectionException)1