Search in sources :

Example 1 with RedisMovedException

use of org.redisson.client.RedisMovedException in project redisson by redisson.

the class CommandAsyncService method checkAttemptFuture.

private <R, V> void checkAttemptFuture(final NodeSource source, final AsyncDetails<V, R> details, Future<R> future) {
    details.getTimeout().cancel();
    if (future.isCancelled()) {
        return;
    }
    if (future.cause() instanceof RedisMovedException) {
        RedisMovedException ex = (RedisMovedException) future.cause();
        async(details.isReadOnlyMode(), new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.MOVED), details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt());
        AsyncDetails.release(details);
        return;
    }
    if (future.cause() instanceof RedisAskException) {
        RedisAskException ex = (RedisAskException) future.cause();
        async(details.isReadOnlyMode(), new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.ASK), details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt());
        AsyncDetails.release(details);
        return;
    }
    if (future.cause() instanceof RedisLoadingException) {
        async(details.isReadOnlyMode(), source, details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt());
        AsyncDetails.release(details);
        return;
    }
    if (future.cause() instanceof RedisTryAgainException) {
        connectionManager.newTimeout(new TimerTask() {

            @Override
            public void run(Timeout timeout) throws Exception {
                async(details.isReadOnlyMode(), source, details.getCodec(), details.getCommand(), details.getParams(), details.getMainPromise(), details.getAttempt());
            }
        }, 1, TimeUnit.SECONDS);
        AsyncDetails.release(details);
        return;
    }
    if (future.isSuccess()) {
        R res = future.getNow();
        if (res instanceof RedisClientResult) {
            InetSocketAddress addr = source.getAddr();
            if (addr == null) {
                addr = details.getConnectionFuture().getNow().getRedisClient().getAddr();
            }
            ((RedisClientResult) res).setRedisClient(addr);
        }
        if (isRedissonReferenceSupportEnabled()) {
            handleReference(details.getMainPromise(), res);
        } else {
            details.getMainPromise().trySuccess(res);
        }
    } else {
        details.getMainPromise().tryFailure(future.cause());
    }
    AsyncDetails.release(details);
}
Also used : NodeSource(org.redisson.connection.NodeSource) RedisMovedException(org.redisson.client.RedisMovedException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) InetSocketAddress(java.net.InetSocketAddress) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisClientResult(org.redisson.RedisClientResult) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisException(org.redisson.client.RedisException) RedisMovedException(org.redisson.client.RedisMovedException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedissonShutdownException(org.redisson.RedissonShutdownException)

Example 2 with RedisMovedException

use of org.redisson.client.RedisMovedException in project redisson by redisson.

the class CommandBatchService method execute.

private void execute(final Entry entry, final NodeSource source, final RPromise<Void> mainPromise, final AtomicInteger slots, final int attempt, final boolean noResult) {
    if (mainPromise.isCancelled()) {
        return;
    }
    if (!connectionManager.getShutdownLatch().acquire()) {
        mainPromise.tryFailure(new IllegalStateException("Redisson is shutdown"));
        return;
    }
    final RPromise<Void> attemptPromise = connectionManager.newPromise();
    final AsyncDetails details = new AsyncDetails();
    final RFuture<RedisConnection> connectionFuture;
    if (entry.isReadOnlyMode()) {
        connectionFuture = connectionManager.connectionReadOp(source, null);
    } else {
        connectionFuture = connectionManager.connectionWriteOp(source, null);
    }
    final TimerTask retryTimerTask = new TimerTask() {

        @Override
        public void run(Timeout timeout) throws Exception {
            if (attemptPromise.isDone()) {
                return;
            }
            if (connectionFuture.cancel(false)) {
                connectionManager.getShutdownLatch().release();
            } else {
                if (connectionFuture.isSuccess()) {
                    ChannelFuture writeFuture = details.getWriteFuture();
                    if (writeFuture != null && !writeFuture.cancel(false) && writeFuture.isSuccess()) {
                        return;
                    }
                }
            }
            if (mainPromise.isCancelled()) {
                attemptPromise.cancel(false);
                return;
            }
            if (attempt == connectionManager.getConfig().getRetryAttempts()) {
                if (details.getException() == null) {
                    details.setException(new RedisTimeoutException("Batch command execution timeout"));
                }
                attemptPromise.tryFailure(details.getException());
                return;
            }
            if (!attemptPromise.cancel(false)) {
                return;
            }
            int count = attempt + 1;
            execute(entry, source, mainPromise, slots, count, noResult);
        }
    };
    Timeout timeout = connectionManager.newTimeout(retryTimerTask, connectionManager.getConfig().getRetryInterval(), TimeUnit.MILLISECONDS);
    details.setTimeout(timeout);
    connectionFuture.addListener(new FutureListener<RedisConnection>() {

        @Override
        public void operationComplete(Future<RedisConnection> connFuture) throws Exception {
            checkConnectionFuture(entry, source, mainPromise, attemptPromise, details, connectionFuture, noResult);
        }
    });
    attemptPromise.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            details.getTimeout().cancel();
            if (future.isCancelled()) {
                return;
            }
            if (future.cause() instanceof RedisMovedException) {
                RedisMovedException ex = (RedisMovedException) future.cause();
                entry.clearErrors();
                execute(entry, new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.MOVED), mainPromise, slots, attempt, noResult);
                return;
            }
            if (future.cause() instanceof RedisAskException) {
                RedisAskException ex = (RedisAskException) future.cause();
                entry.clearErrors();
                execute(entry, new NodeSource(ex.getSlot(), ex.getAddr(), Redirect.ASK), mainPromise, slots, attempt, noResult);
                return;
            }
            if (future.cause() instanceof RedisLoadingException) {
                entry.clearErrors();
                execute(entry, source, mainPromise, slots, attempt, noResult);
                return;
            }
            if (future.cause() instanceof RedisTryAgainException) {
                entry.clearErrors();
                connectionManager.newTimeout(new TimerTask() {

                    @Override
                    public void run(Timeout timeout) throws Exception {
                        execute(entry, source, mainPromise, slots, attempt, noResult);
                    }
                }, 1, TimeUnit.SECONDS);
                return;
            }
            if (future.isSuccess()) {
                if (slots.decrementAndGet() == 0) {
                    mainPromise.trySuccess(future.getNow());
                }
            } else {
                mainPromise.tryFailure(future.cause());
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RedisMovedException(org.redisson.client.RedisMovedException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) Timeout(io.netty.util.Timeout) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisMovedException(org.redisson.client.RedisMovedException) RedisAskException(org.redisson.client.RedisAskException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) NodeSource(org.redisson.connection.NodeSource) TimerTask(io.netty.util.TimerTask) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisAskException(org.redisson.client.RedisAskException) RedisConnection(org.redisson.client.RedisConnection)

Example 3 with RedisMovedException

use of org.redisson.client.RedisMovedException in project redisson by redisson.

the class CommandDecoder method decode.

private void decode(ByteBuf in, CommandData<Object, Object> data, List<Object> parts, Channel channel) throws IOException {
    int code = in.readByte();
    if (code == '+') {
        ByteBuf rb = in.readBytes(in.bytesBefore((byte) '\r'));
        try {
            String result = rb.toString(CharsetUtil.UTF_8);
            in.skipBytes(2);
            handleResult(data, parts, result, false, channel);
        } finally {
            rb.release();
        }
    } else if (code == '-') {
        ByteBuf rb = in.readBytes(in.bytesBefore((byte) '\r'));
        try {
            String error = rb.toString(CharsetUtil.UTF_8);
            in.skipBytes(2);
            if (error.startsWith("MOVED")) {
                String[] errorParts = error.split(" ");
                int slot = Integer.valueOf(errorParts[1]);
                String addr = errorParts[2];
                data.tryFailure(new RedisMovedException(slot, addr));
            } else if (error.startsWith("ASK")) {
                String[] errorParts = error.split(" ");
                int slot = Integer.valueOf(errorParts[1]);
                String addr = errorParts[2];
                data.tryFailure(new RedisAskException(slot, addr));
            } else if (error.startsWith("TRYAGAIN")) {
                data.tryFailure(new RedisTryAgainException(error + ". channel: " + channel + " data: " + data));
            } else if (error.startsWith("LOADING")) {
                data.tryFailure(new RedisLoadingException(error + ". channel: " + channel + " data: " + data));
            } else if (error.startsWith("OOM")) {
                data.tryFailure(new RedisOutOfMemoryException(error.split("OOM ")[1] + ". channel: " + channel + " data: " + data));
            } else if (error.contains("-OOM ")) {
                data.tryFailure(new RedisOutOfMemoryException(error.split("-OOM ")[1] + ". channel: " + channel + " data: " + data));
            } else {
                if (data != null) {
                    data.tryFailure(new RedisException(error + ". channel: " + channel + " command: " + data));
                } else {
                    log.error("Error: {} channel: {} data: {}", error, channel, data);
                }
            }
        } finally {
            rb.release();
        }
    } else if (code == ':') {
        Long result = readLong(in);
        handleResult(data, parts, result, false, channel);
    } else if (code == '$') {
        ByteBuf buf = readBytes(in);
        Object result = null;
        if (buf != null) {
            Decoder<Object> decoder = selectDecoder(data, parts);
            result = decoder.decode(buf, state());
        }
        handleResult(data, parts, result, false, channel);
    } else if (code == '*') {
        int level = state().incLevel();
        long size = readLong(in);
        List<Object> respParts;
        if (state().getLevels().size() - 1 >= level) {
            StateLevel stateLevel = state().getLevels().get(level);
            respParts = stateLevel.getParts();
            size = stateLevel.getSize();
        } else {
            respParts = new ArrayList<Object>();
            if (state().isMakeCheckpoint()) {
                state().addLevel(new StateLevel(size, respParts));
            }
        }
        decodeList(in, data, parts, channel, size, respParts);
    } else {
        String dataStr = in.toString(0, in.writerIndex(), CharsetUtil.UTF_8);
        throw new IllegalStateException("Can't decode replay: " + dataStr);
    }
}
Also used : RedisMovedException(org.redisson.client.RedisMovedException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) ByteBuf(io.netty.buffer.ByteBuf) ReplayingDecoder(io.netty.handler.codec.ReplayingDecoder) Decoder(org.redisson.client.protocol.Decoder) ListMultiDecoder(org.redisson.client.protocol.decoder.ListMultiDecoder) SlotsDecoder(org.redisson.client.protocol.decoder.SlotsDecoder) NestedMultiDecoder(org.redisson.client.protocol.decoder.NestedMultiDecoder) MultiDecoder(org.redisson.client.protocol.decoder.MultiDecoder) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisException(org.redisson.client.RedisException) RedisAskException(org.redisson.client.RedisAskException) RedisOutOfMemoryException(org.redisson.client.RedisOutOfMemoryException)

Aggregations

RedisAskException (org.redisson.client.RedisAskException)3 RedisLoadingException (org.redisson.client.RedisLoadingException)3 RedisMovedException (org.redisson.client.RedisMovedException)3 RedisTryAgainException (org.redisson.client.RedisTryAgainException)3 Timeout (io.netty.util.Timeout)2 TimerTask (io.netty.util.TimerTask)2 RedisException (org.redisson.client.RedisException)2 RedisTimeoutException (org.redisson.client.RedisTimeoutException)2 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)2 NodeSource (org.redisson.connection.NodeSource)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ReplayingDecoder (io.netty.handler.codec.ReplayingDecoder)1 InetSocketAddress (java.net.InetSocketAddress)1 RedisClientResult (org.redisson.RedisClientResult)1 RedissonShutdownException (org.redisson.RedissonShutdownException)1 RedisConnection (org.redisson.client.RedisConnection)1 RedisOutOfMemoryException (org.redisson.client.RedisOutOfMemoryException)1 Decoder (org.redisson.client.protocol.Decoder)1 ListMultiDecoder (org.redisson.client.protocol.decoder.ListMultiDecoder)1