|
74 | 74 | import org.elasticsearch.threadpool.ThreadPool; |
75 | 75 | import org.junit.BeforeClass; |
76 | 76 |
|
| 77 | +import java.util.Arrays; |
77 | 78 | import java.util.Comparator; |
78 | 79 | import java.util.HashMap; |
79 | 80 | import java.util.HashSet; |
@@ -1028,11 +1029,16 @@ public void testShardsAreRelocatedEvenly() { |
1028 | 1029 | logger.info("State after shutdowns: {}", shardCounts(clusterState)); |
1029 | 1030 |
|
1030 | 1031 | Map<String, Integer> allocationCounts = shardCounts(clusterState); |
1031 | | - // All allocations should be on the remaining nodes |
| 1032 | + |
| 1033 | + // Only the remaining nodes should have allocations |
1032 | 1034 | 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); |
1036 | 1042 | assertThat(maximumAllocationCount - minimumAllocationCount, lessThanOrEqualTo(1)); |
1037 | 1043 | } |
1038 | 1044 |
|
|
0 commit comments