|
|
@ -16,9 +16,9 @@
|
|
|
|
package org.redisson.cluster;
|
|
|
|
package org.redisson.cluster;
|
|
|
|
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
|
|
|
import io.netty.util.Timeout;
|
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
import io.netty.util.concurrent.FutureListener;
|
|
|
|
import io.netty.util.concurrent.FutureListener;
|
|
|
|
import io.netty.util.concurrent.ScheduledFuture;
|
|
|
|
|
|
|
|
import org.redisson.api.NodeType;
|
|
|
|
import org.redisson.api.NodeType;
|
|
|
|
import org.redisson.api.RFuture;
|
|
|
|
import org.redisson.api.RFuture;
|
|
|
|
import org.redisson.client.*;
|
|
|
|
import org.redisson.client.*;
|
|
|
@ -55,7 +55,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
|
|
|
|
|
|
|
|
|
|
|
|
private final ConcurrentMap<Integer, ClusterPartition> lastPartitions = new ConcurrentHashMap<>();
|
|
|
|
private final ConcurrentMap<Integer, ClusterPartition> lastPartitions = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
private ScheduledFuture<?> monitorFuture;
|
|
|
|
private volatile Timeout monitorFuture;
|
|
|
|
|
|
|
|
|
|
|
|
private volatile RedisURI lastClusterNode;
|
|
|
|
private volatile RedisURI lastClusterNode;
|
|
|
|
|
|
|
|
|
|
|
@ -356,58 +356,54 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void scheduleClusterChangeCheck(ClusterServersConfig cfg) {
|
|
|
|
private void scheduleClusterChangeCheck(ClusterServersConfig cfg) {
|
|
|
|
monitorFuture = serviceManager.getGroup().schedule(new Runnable() {
|
|
|
|
monitorFuture = serviceManager.newTimeout(t -> {
|
|
|
|
@Override
|
|
|
|
if (configEndpointHostName != null) {
|
|
|
|
public void run() {
|
|
|
|
String address = cfg.getNodeAddresses().iterator().next();
|
|
|
|
if (configEndpointHostName != null) {
|
|
|
|
RedisURI uri = new RedisURI(address);
|
|
|
|
String address = cfg.getNodeAddresses().iterator().next();
|
|
|
|
Future<List<InetSocketAddress>> allNodes = serviceManager.resolveAll(uri);
|
|
|
|
RedisURI uri = new RedisURI(address);
|
|
|
|
allNodes.addListener(new FutureListener<List<InetSocketAddress>>() {
|
|
|
|
Future<List<InetSocketAddress>> allNodes = serviceManager.resolveAll(uri);
|
|
|
|
@Override
|
|
|
|
allNodes.addListener(new FutureListener<List<InetSocketAddress>>() {
|
|
|
|
public void operationComplete(Future<List<InetSocketAddress>> future) throws Exception {
|
|
|
|
@Override
|
|
|
|
AtomicReference<Throwable> lastException = new AtomicReference<Throwable>(future.cause());
|
|
|
|
public void operationComplete(Future<List<InetSocketAddress>> future) throws Exception {
|
|
|
|
if (!future.isSuccess()) {
|
|
|
|
AtomicReference<Throwable> lastException = new AtomicReference<Throwable>(future.cause());
|
|
|
|
checkClusterState(cfg, Collections.emptyIterator(), lastException);
|
|
|
|
if (!future.isSuccess()) {
|
|
|
|
return;
|
|
|
|
checkClusterState(cfg, Collections.emptyIterator(), lastException);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<RedisURI> nodes = new ArrayList<>();
|
|
|
|
|
|
|
|
for (InetSocketAddress addr : future.getNow()) {
|
|
|
|
|
|
|
|
RedisURI address = serviceManager.toURI(uri.getScheme(), addr.getAddress().getHostAddress(), "" + addr.getPort());
|
|
|
|
|
|
|
|
nodes.add(address);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Iterator<RedisURI> nodesIterator = nodes.iterator();
|
|
|
|
|
|
|
|
checkClusterState(cfg, nodesIterator, lastException);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
List<RedisURI> nodes = new ArrayList<>();
|
|
|
|
AtomicReference<Throwable> lastException = new AtomicReference<>();
|
|
|
|
for (InetSocketAddress addr : future.getNow()) {
|
|
|
|
List<RedisURI> nodes = new ArrayList<>();
|
|
|
|
RedisURI address = serviceManager.toURI(uri.getScheme(), addr.getAddress().getHostAddress(), "" + addr.getPort());
|
|
|
|
List<RedisURI> slaves = new ArrayList<>();
|
|
|
|
nodes.add(address);
|
|
|
|
|
|
|
|
|
|
|
|
for (ClusterPartition partition : getLastPartitions()) {
|
|
|
|
|
|
|
|
if (!partition.isMasterFail()) {
|
|
|
|
|
|
|
|
nodes.add(partition.getMasterAddress());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Set<RedisURI> partitionSlaves = new HashSet<>(partition.getSlaveAddresses());
|
|
|
|
Iterator<RedisURI> nodesIterator = nodes.iterator();
|
|
|
|
partitionSlaves.removeAll(partition.getFailedSlaveAddresses());
|
|
|
|
checkClusterState(cfg, nodesIterator, lastException);
|
|
|
|
slaves.addAll(partitionSlaves);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Collections.shuffle(nodes);
|
|
|
|
});
|
|
|
|
Collections.shuffle(slaves);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
AtomicReference<Throwable> lastException = new AtomicReference<>();
|
|
|
|
// master nodes first
|
|
|
|
List<RedisURI> nodes = new ArrayList<>();
|
|
|
|
nodes.addAll(slaves);
|
|
|
|
List<RedisURI> slaves = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
Iterator<RedisURI> nodesIterator = nodes.iterator();
|
|
|
|
for (ClusterPartition partition : getLastPartitions()) {
|
|
|
|
|
|
|
|
if (!partition.isMasterFail()) {
|
|
|
|
|
|
|
|
nodes.add(partition.getMasterAddress());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
checkClusterState(cfg, nodesIterator, lastException);
|
|
|
|
Set<RedisURI> partitionSlaves = new HashSet<>(partition.getSlaveAddresses());
|
|
|
|
|
|
|
|
partitionSlaves.removeAll(partition.getFailedSlaveAddresses());
|
|
|
|
|
|
|
|
slaves.addAll(partitionSlaves);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Collections.shuffle(nodes);
|
|
|
|
|
|
|
|
Collections.shuffle(slaves);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// master nodes first
|
|
|
|
|
|
|
|
nodes.addAll(slaves);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Iterator<RedisURI> nodesIterator = nodes.iterator();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checkClusterState(cfg, nodesIterator, lastException);
|
|
|
|
|
|
|
|
}
|
|
|
|
}, cfg.getScanInterval(), TimeUnit.MILLISECONDS);
|
|
|
|
}, cfg.getScanInterval(), TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -922,7 +918,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void shutdown(long quietPeriod, long timeout, TimeUnit unit) {
|
|
|
|
public void shutdown(long quietPeriod, long timeout, TimeUnit unit) {
|
|
|
|
if (monitorFuture != null) {
|
|
|
|
if (monitorFuture != null) {
|
|
|
|
monitorFuture.cancel(true);
|
|
|
|
monitorFuture.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
closeNodeConnections();
|
|
|
|
closeNodeConnections();
|
|
|
|