Skip to content

Commit 91c240b

Browse files
committed
Fix assertion
1 parent 123a0b9 commit 91c240b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceReconcilerTests.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.elasticsearch.threadpool.ThreadPool;
7575
import org.junit.BeforeClass;
7676

77+
import java.util.Arrays;
7778
import java.util.Comparator;
7879
import java.util.HashMap;
7980
import java.util.HashSet;
@@ -1028,11 +1029,16 @@ public void testShardsAreRelocatedEvenly() {
10281029
logger.info("State after shutdowns: {}", shardCounts(clusterState));
10291030

10301031
Map<String, Integer> allocationCounts = shardCounts(clusterState);
1031-
// All allocations should be on the remaining nodes
1032+
1033+
// Only the remaining nodes should have allocations
10321034
assertTrue(allocationCounts.keySet().stream().allMatch(nodeId -> nodeOrdinal.apply(nodeId) < numToRemain));
1033-
// Allocations should be spread evenly amongst them
1034-
int minimumAllocationCount = allocationCounts.values().stream().min(Integer::compareTo).orElse(0);
1035-
int maximumAllocationCount = allocationCounts.values().stream().max(Integer::compareTo).orElse(Integer.MAX_VALUE);
1035+
1036+
// ... and the shards should be spread as evenly as possible over them
1037+
int[] remainingNodeShardCounts = IntStream.range(0, numToRemain - 1)
1038+
.map(ordinal -> allocationCounts.getOrDefault("node-" + ordinal, 0))
1039+
.toArray();
1040+
int minimumAllocationCount = Arrays.stream(remainingNodeShardCounts).min().orElse(0);
1041+
int maximumAllocationCount = Arrays.stream(remainingNodeShardCounts).max().orElse(Integer.MAX_VALUE);
10361042
assertThat(maximumAllocationCount - minimumAllocationCount, lessThanOrEqualTo(1));
10371043
}
10381044

0 commit comments

Comments
 (0)