Search in sources :

Example 6 with RedisConnection

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

the class ReplicatedConnectionManager method scheduleMasterChangeCheck.

private void scheduleMasterChangeCheck(final ReplicatedServersConfig cfg) {
    monitorFuture = GlobalEventExecutor.INSTANCE.schedule(new Runnable() {

        @Override
        public void run() {
            final URL master = currentMaster.get();
            log.debug("Current master: {}", master);
            final AtomicInteger count = new AtomicInteger(cfg.getNodeAddresses().size());
            for (final URL addr : cfg.getNodeAddresses()) {
                if (isShuttingDown()) {
                    return;
                }
                RFuture<RedisConnection> connectionFuture = connect(cfg, addr);
                connectionFuture.addListener(new FutureListener<RedisConnection>() {

                    @Override
                    public void operationComplete(Future<RedisConnection> future) throws Exception {
                        if (!future.isSuccess()) {
                            log.error(future.cause().getMessage(), future.cause());
                            if (count.decrementAndGet() == 0) {
                                scheduleMasterChangeCheck(cfg);
                            }
                            return;
                        }
                        if (isShuttingDown()) {
                            return;
                        }
                        RedisConnection connection = future.getNow();
                        RFuture<Map<String, String>> result = connection.async(RedisCommands.INFO_REPLICATION);
                        result.addListener(new FutureListener<Map<String, String>>() {

                            @Override
                            public void operationComplete(Future<Map<String, String>> future) throws Exception {
                                if (!future.isSuccess()) {
                                    log.error(future.cause().getMessage(), future.cause());
                                    if (count.decrementAndGet() == 0) {
                                        scheduleMasterChangeCheck(cfg);
                                    }
                                    return;
                                }
                                Role role = Role.valueOf(future.getNow().get(ROLE_KEY));
                                if (Role.master.equals(role)) {
                                    if (master.equals(addr)) {
                                        log.debug("Current master {} unchanged", master);
                                    } else if (currentMaster.compareAndSet(master, addr)) {
                                        log.info("Master has changed from {} to {}", master, addr);
                                        changeMaster(singleSlotRange.getStartSlot(), addr.getHost(), addr.getPort());
                                    }
                                }
                                if (count.decrementAndGet() == 0) {
                                    scheduleMasterChangeCheck(cfg);
                                }
                            }
                        });
                    }
                });
            }
        }
    }, cfg.getScanInterval(), TimeUnit.MILLISECONDS);
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) RFuture(org.redisson.api.RFuture) URL(java.net.URL) RedisException(org.redisson.client.RedisException) RedisConnectionException(org.redisson.client.RedisConnectionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future) RedisConnection(org.redisson.client.RedisConnection)

Example 7 with RedisConnection

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

the class ConnectionWatchdog method reconnect.

private void reconnect(final RedisConnection connection, final Channel channel) {
    if (connection.getReconnectListener() != null) {
        // new connection used only for channel init
        RedisConnection rc = new RedisConnection(connection.getRedisClient(), channel);
        RPromise<RedisConnection> connectionFuture = new RedissonPromise<RedisConnection>();
        connection.getReconnectListener().onReconnect(rc, connectionFuture);
        connectionFuture.addListener(new FutureListener<RedisConnection>() {

            @Override
            public void operationComplete(Future<RedisConnection> future) throws Exception {
                if (future.isSuccess()) {
                    refresh(connection, channel);
                }
            }
        });
    } else {
        refresh(connection, channel);
    }
}
Also used : RedissonPromise(org.redisson.misc.RedissonPromise) RedisException(org.redisson.client.RedisException) RedisConnection(org.redisson.client.RedisConnection)

Example 8 with RedisConnection

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

the class ConnectionWatchdog method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    RedisConnection connection = RedisConnection.getFrom(ctx.channel());
    if (connection != null) {
        connection.onDisconnect();
        if (!connection.isClosed()) {
            if (connection.isFastReconnect()) {
                tryReconnect(connection, 1);
                connection.clearFastReconnect();
            } else {
                reconnect(connection, 1);
            }
        }
    }
    ctx.fireChannelInactive();
}
Also used : RedisConnection(org.redisson.client.RedisConnection)

Example 9 with RedisConnection

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

the class ClusterConnectionManager method checkClusterState.

private void checkClusterState(final ClusterServersConfig cfg, final Iterator<URL> iterator, final AtomicReference<Throwable> lastException) {
    if (!iterator.hasNext()) {
        log.error("Can't update cluster state", lastException.get());
        scheduleClusterChangeCheck(cfg, null);
        return;
    }
    if (!getShutdownLatch().acquire()) {
        return;
    }
    final URL uri = iterator.next();
    RFuture<RedisConnection> connectionFuture = connect(cfg, uri);
    connectionFuture.addListener(new FutureListener<RedisConnection>() {

        @Override
        public void operationComplete(Future<RedisConnection> future) throws Exception {
            if (!future.isSuccess()) {
                lastException.set(future.cause());
                getShutdownLatch().release();
                checkClusterState(cfg, iterator, lastException);
                return;
            }
            RedisConnection connection = future.getNow();
            updateClusterState(cfg, connection, iterator, uri);
        }
    });
}
Also used : URL(java.net.URL) RedisException(org.redisson.client.RedisException) RedisConnectionException(org.redisson.client.RedisConnectionException) RedisConnection(org.redisson.client.RedisConnection)

Example 10 with RedisConnection

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

the class ClusterConnectionManager method shutdown.

@Override
public void shutdown() {
    monitorFuture.cancel(true);
    super.shutdown();
    for (RedisConnection connection : nodeConnections.values()) {
        connection.getRedisClient().shutdown();
    }
}
Also used : RedisConnection(org.redisson.client.RedisConnection)

Aggregations

RedisConnection (org.redisson.client.RedisConnection)23 FutureListener (io.netty.util.concurrent.FutureListener)7 ArrayList (java.util.ArrayList)7 RFuture (org.redisson.api.RFuture)7 RedisClient (org.redisson.client.RedisClient)7 RedisException (org.redisson.client.RedisException)7 Future (io.netty.util.concurrent.Future)6 Test (org.junit.Test)6 RedisConnectionException (org.redisson.client.RedisConnectionException)6 RPromise (org.redisson.misc.RPromise)5 ChannelFuture (io.netty.channel.ChannelFuture)4 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)4 CommandData (org.redisson.client.protocol.CommandData)4 CommandsData (org.redisson.client.protocol.CommandsData)4 RedissonPromise (org.redisson.misc.RedissonPromise)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 Timeout (io.netty.util.Timeout)3 TimerTask (io.netty.util.TimerTask)3 RedisAskException (org.redisson.client.RedisAskException)3 RedisLoadingException (org.redisson.client.RedisLoadingException)3