From fcfec4534d4fd5c9b1f308217b51788b85400507 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Thu, 13 Aug 2020 10:21:40 +0300 Subject: [PATCH] refactoring --- .../cluster/ClusterConnectionManager.java | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java b/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java index effc95cc8..311ec1523 100644 --- a/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java +++ b/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java @@ -160,7 +160,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { } } - scheduleClusterChangeCheck(cfg, null); + scheduleClusterChangeCheck(cfg); } @Override @@ -350,7 +350,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { return result; } - private void scheduleClusterChangeCheck(ClusterServersConfig cfg, Iterator iterator) { + private void scheduleClusterChangeCheck(ClusterServersConfig cfg) { monitorFuture = group.schedule(new Runnable() { @Override public void run() { @@ -364,7 +364,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { public void operationComplete(Future> future) throws Exception { AtomicReference lastException = new AtomicReference(future.cause()); if (!future.isSuccess()) { - checkClusterState(cfg, Collections.emptyList().iterator(), lastException); + checkClusterState(cfg, Collections.emptyIterator(), lastException); return; } @@ -381,26 +381,23 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { }); } else { AtomicReference lastException = new AtomicReference(); - Iterator nodesIterator = iterator; - if (nodesIterator == null) { - List nodes = new ArrayList<>(); - List slaves = new ArrayList<>(); - - for (ClusterPartition partition : getLastPartitions()) { - if (!partition.isMasterFail()) { - nodes.add(partition.getMasterAddress()); - } - - Set partitionSlaves = new HashSet<>(partition.getSlaveAddresses()); - partitionSlaves.removeAll(partition.getFailedSlaveAddresses()); - slaves.addAll(partitionSlaves); + List nodes = new ArrayList<>(); + List slaves = new ArrayList<>(); + + for (ClusterPartition partition : getLastPartitions()) { + if (!partition.isMasterFail()) { + nodes.add(partition.getMasterAddress()); } - // master nodes first - nodes.addAll(slaves); - - nodesIterator = nodes.iterator(); + + Set partitionSlaves = new HashSet<>(partition.getSlaveAddresses()); + partitionSlaves.removeAll(partition.getFailedSlaveAddresses()); + slaves.addAll(partitionSlaves); } - + // master nodes first + nodes.addAll(slaves); + + Iterator nodesIterator = nodes.iterator(); + checkClusterState(cfg, nodesIterator, lastException); } } @@ -413,7 +410,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { if (lastException.get() != null) { log.error("Can't update cluster state", lastException.get()); } - scheduleClusterChangeCheck(cfg, null); + scheduleClusterChangeCheck(cfg); return; } if (!getShutdownLatch().acquire()) { @@ -461,9 +458,9 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { checkSlaveNodesChange(newPartitions); masterFuture.onComplete((res, ex) -> { checkSlotsMigration(newPartitions); - checkSlotsChange(cfg, newPartitions); + checkSlotsChange(newPartitions); getShutdownLatch().release(); - scheduleClusterChangeCheck(cfg, null); + scheduleClusterChangeCheck(cfg); }); }); } @@ -617,7 +614,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { return result; } - private void checkSlotsChange(ClusterServersConfig cfg, Collection newPartitions) { + private void checkSlotsChange(Collection newPartitions) { int newSlotsAmount = slotsAmount(newPartitions); if (newSlotsAmount == lastPartitions.size() && lastPartitions.size() == MAX_SLOT) { return;