Search in sources :

Example 1 with JedisRedirectionException

use of redis.clients.jedis.exceptions.JedisRedirectionException 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)

Example 2 with JedisRedirectionException

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

the class PipelineClusterCommand method checkException.

protected boolean checkException(Object obj) {
    if (obj instanceof Exception) {
        Exception e = (Exception) obj;
        if (e instanceof JedisRedirectionException) {
            //重定向slot 映射.
            if (e instanceof JedisMovedDataException) {
                // it rebuilds cluster's slot cache
                // recommended by Redis cluster specification
                this.connectionHandler.renewSlotCache();
                logger.warn("JedisMovedDataException:" + e.getMessage(), e);
            } else {
                logger.error("pipeline-error:" + e.getMessage(), e);
            }
        } else {
            logger.error(e.getMessage(), e);
        }
        return true;
    }
    return false;
}
Also used : JedisMovedDataException(redis.clients.jedis.exceptions.JedisMovedDataException) JedisRedirectionException(redis.clients.jedis.exceptions.JedisRedirectionException) JedisMovedDataException(redis.clients.jedis.exceptions.JedisMovedDataException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) JedisRedirectionException(redis.clients.jedis.exceptions.JedisRedirectionException)

Aggregations

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