Search in sources :

Example 6 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class ConnectionPool method scheduleCheck.

private void scheduleCheck(final ClientConnectionsEntry entry) {
    connectionManager.getConnectionEventsHub().fireDisconnect(entry.getClient().getAddr());
    connectionManager.newTimeout(new TimerTask() {

        @Override
        public void run(Timeout timeout) throws Exception {
            if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
                return;
            }
            RFuture<RedisConnection> connectionFuture = entry.getClient().connectAsync();
            connectionFuture.addListener(new FutureListener<RedisConnection>() {

                @Override
                public void operationComplete(Future<RedisConnection> future) throws Exception {
                    if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
                        return;
                    }
                    if (!future.isSuccess()) {
                        scheduleCheck(entry);
                        return;
                    }
                    final RedisConnection c = future.getNow();
                    if (!c.isActive()) {
                        c.closeAsync();
                        scheduleCheck(entry);
                        return;
                    }
                    final FutureListener<String> pingListener = new FutureListener<String>() {

                        @Override
                        public void operationComplete(Future<String> future) throws Exception {
                            try {
                                if (entry.getFreezeReason() != FreezeReason.RECONNECT || !entry.isFreezed()) {
                                    return;
                                }
                                if (future.isSuccess() && "PONG".equals(future.getNow())) {
                                    entry.resetFailedAttempts();
                                    RPromise<Void> promise = connectionManager.newPromise();
                                    promise.addListener(new FutureListener<Void>() {

                                        @Override
                                        public void operationComplete(Future<Void> future) throws Exception {
                                            if (entry.getNodeType() == NodeType.SLAVE) {
                                                masterSlaveEntry.slaveUp(entry.getClient().getAddr().getHostName(), entry.getClient().getAddr().getPort(), FreezeReason.RECONNECT);
                                                log.info("slave {} successfully reconnected", entry.getClient().getAddr());
                                            } else {
                                                synchronized (entry) {
                                                    if (entry.getFreezeReason() == FreezeReason.RECONNECT) {
                                                        entry.setFreezed(false);
                                                        entry.setFreezeReason(null);
                                                        log.info("host {} successfully reconnected", entry.getClient().getAddr());
                                                    }
                                                }
                                            }
                                        }
                                    });
                                    initConnections(entry, promise, false);
                                } else {
                                    scheduleCheck(entry);
                                }
                            } finally {
                                c.closeAsync();
                            }
                        }
                    };
                    if (entry.getConfig().getPassword() != null) {
                        RFuture<Void> temp = c.async(RedisCommands.AUTH, config.getPassword());
                        FutureListener<Void> listener = new FutureListener<Void>() {

                            @Override
                            public void operationComplete(Future<Void> future) throws Exception {
                                ping(c, pingListener);
                            }
                        };
                        temp.addListener(listener);
                    } else {
                        ping(c, pingListener);
                    }
                }
            });
        }
    }, config.getReconnectionTimeout(), TimeUnit.MILLISECONDS);
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) Timeout(io.netty.util.Timeout) RFuture(org.redisson.api.RFuture) RedisConnectionException(org.redisson.client.RedisConnectionException) TimerTask(io.netty.util.TimerTask) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future) RedisConnection(org.redisson.client.RedisConnection)

Example 7 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class ScheduledExecutorRemoteService method awaitResultAsync.

@Override
protected void awaitResultAsync(final RemoteInvocationOptions optionsCopy, final RemotePromise<Object> result, final RemoteServiceRequest request, final String responseName) {
    if (!optionsCopy.isResultExpected()) {
        return;
    }
    Long startTime = 0L;
    if (request != null && request.getArgs() != null && request.getArgs().length > 3) {
        startTime = (Long) request.getArgs()[3];
    }
    long delay = startTime - System.currentTimeMillis();
    if (delay > 0) {
        commandExecutor.getConnectionManager().newTimeout(new TimerTask() {

            @Override
            public void run(Timeout timeout) throws Exception {
                ScheduledExecutorRemoteService.super.awaitResultAsync(optionsCopy, result, request, responseName);
            }
        }, delay, TimeUnit.MILLISECONDS);
    } else {
        super.awaitResultAsync(optionsCopy, result, request, responseName);
    }
}
Also used : TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout)

Example 8 with TimerTask

use of io.netty.util.TimerTask 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 9 with TimerTask

use of io.netty.util.TimerTask 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 10 with TimerTask

use of io.netty.util.TimerTask in project redisson by redisson.

the class CommandBatchService method checkWriteFuture.

private void checkWriteFuture(final RPromise<Void> attemptPromise, AsyncDetails details, final RedisConnection connection, ChannelFuture future, boolean noResult) {
    if (attemptPromise.isDone() || future.isCancelled()) {
        return;
    }
    if (!future.isSuccess()) {
        details.setException(new WriteRedisConnectionException("Can't write command batch to channel: " + future.channel(), future.cause()));
    } else {
        details.getTimeout().cancel();
        TimerTask timeoutTask = new TimerTask() {

            @Override
            public void run(Timeout timeout) throws Exception {
                attemptPromise.tryFailure(new RedisTimeoutException("Redis server response timeout during command batch execution. Channel: " + connection.getChannel()));
            }
        };
        Timeout timeout = connectionManager.newTimeout(timeoutTask, connectionManager.getConfig().getTimeout(), TimeUnit.MILLISECONDS);
        details.setTimeout(timeout);
    }
}
Also used : WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) TimerTask(io.netty.util.TimerTask) RedisTimeoutException(org.redisson.client.RedisTimeoutException) Timeout(io.netty.util.Timeout)

Aggregations

Timeout (io.netty.util.Timeout)38 TimerTask (io.netty.util.TimerTask)38 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 RFuture (org.redisson.api.RFuture)8 RedisTimeoutException (org.redisson.client.RedisTimeoutException)7 FutureListener (io.netty.util.concurrent.FutureListener)6 ByteBuf (io.netty.buffer.ByteBuf)5 ChannelFuture (io.netty.channel.ChannelFuture)5 Future (io.netty.util.concurrent.Future)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 ArrayList (java.util.ArrayList)5 RedisException (org.redisson.client.RedisException)5 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)5 CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)5 RedisConnection (org.redisson.client.RedisConnection)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 IOException (java.io.IOException)3