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);
}
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);
}
}
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();
}
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);
}
});
}
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();
}
}
Aggregations