diff --git a/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java b/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java index dd5585e50..083407c32 100644 --- a/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java +++ b/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java @@ -408,8 +408,9 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { } private void checkSlaveNodesChange(Collection newPartitions) { + Set lastPartitions = getLastPartitions(); for (ClusterPartition newPart : newPartitions) { - for (ClusterPartition currentPart : getLastPartitions()) { + for (ClusterPartition currentPart : lastPartitions) { if (!newPart.getMasterAddress().equals(currentPart.getMasterAddress())) { continue; } @@ -479,10 +480,10 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { return addedSlaves; } - private Collection slots(Collection partitions) { - Set result = new HashSet(MAX_SLOT); + private int slotsAmount(Collection partitions) { + int result = 0; for (ClusterPartition clusterPartition : partitions) { - result.addAll(clusterPartition.getSlots()); + result += clusterPartition.getSlots().size(); } return result; } @@ -500,9 +501,10 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { private RFuture checkMasterNodesChange(ClusterServersConfig cfg, Collection newPartitions) { List newMasters = new ArrayList(); + Set lastPartitions = getLastPartitions(); for (final ClusterPartition newPart : newPartitions) { boolean masterFound = false; - for (ClusterPartition currentPart : getLastPartitions()) { + for (ClusterPartition currentPart : lastPartitions) { if (!newPart.getMasterAddress().equals(currentPart.getMasterAddress())) { continue; } @@ -567,13 +569,24 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { } private void checkSlotsChange(ClusterServersConfig cfg, Collection newPartitions) { - Collection newPartitionsSlots = slots(newPartitions); - if (newPartitionsSlots.size() == lastPartitions.size() && lastPartitions.size() == MAX_SLOT) { + int newSlotsAmount = slotsAmount(newPartitions); + if (newSlotsAmount == lastPartitions.size() && lastPartitions.size() == MAX_SLOT) { return; } - Set removedSlots = new HashSet(lastPartitions.keySet()); - removedSlots.removeAll(newPartitionsSlots); + Set removedSlots = new HashSet(); + for (Integer slot : lastPartitions.keySet()) { + boolean found = false; + for (ClusterPartition clusterPartition : newPartitions) { + if (clusterPartition.getSlots().contains(slot)) { + found = true; + break; + } + } + if (!found) { + removedSlots.add(slot); + } + } lastPartitions.keySet().removeAll(removedSlots); if (!removedSlots.isEmpty()) { log.info("{} slots found to remove", removedSlots.size()); @@ -587,9 +600,14 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { } } - - Set addedSlots = new HashSet(newPartitionsSlots); - addedSlots.removeAll(lastPartitions.keySet()); + Set addedSlots = new HashSet(); + for (ClusterPartition clusterPartition : newPartitions) { + for (Integer slot : clusterPartition.getSlots()) { + if (!lastPartitions.containsKey(slot)) { + addedSlots.add(slot); + } + } + } if (!addedSlots.isEmpty()) { log.info("{} slots found to add", addedSlots.size()); } @@ -611,8 +629,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { } private void checkSlotsMigration(Collection newPartitions) { - Set currentPartitions = getLastPartitions(); - for (ClusterPartition currentPartition : currentPartitions) { + for (ClusterPartition currentPartition : getLastPartitions()) { for (ClusterPartition newPartition : newPartitions) { if (!currentPartition.getNodeId().equals(newPartition.getNodeId()) // skip master change case @@ -750,7 +767,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { super.shutdown(); } - private HashSet getLastPartitions() { + private Set getLastPartitions() { return new HashSet(lastPartitions.values()); }