|
22 | 22 | import org.elasticsearch.logging.LogManager; |
23 | 23 | import org.elasticsearch.logging.Logger; |
24 | 24 |
|
| 25 | +import java.util.Spliterator; |
| 26 | +import java.util.Spliterators; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.StreamSupport; |
| 29 | + |
25 | 30 | /** |
26 | 31 | * Keeps track of a limited number of shards that are currently in undesired allocations. If the |
27 | 32 | * shards remain in undesired allocations for longer than a configurable threshold, it will log |
@@ -114,6 +119,7 @@ public void cleanup(RoutingNodes routingNodes) { |
114 | 119 | return true; |
115 | 120 | } |
116 | 121 | }); |
| 122 | + shrinkIfOversized(); |
117 | 123 | } |
118 | 124 |
|
119 | 125 | /** |
@@ -187,4 +193,22 @@ private void logUndesiredShardDetails( |
187 | 193 | allocation.setDebugMode(originalDebugMode); |
188 | 194 | } |
189 | 195 | } |
| 196 | + |
| 197 | + /** |
| 198 | + * If the maximum to track was reduced, and we are tracking more than the new maximum, purge the most recent entries |
| 199 | + * to bring us under the new limit |
| 200 | + */ |
| 201 | + private void shrinkIfOversized() { |
| 202 | + if (undesiredAllocations.size() > maxUndesiredAllocationsToTrack) { |
| 203 | + final var newestExcessValues = StreamSupport.stream( |
| 204 | + Spliterators.spliteratorUnknownSize(undesiredAllocations.iterator(), Spliterator.ORDERED), |
| 205 | + false |
| 206 | + ) |
| 207 | + .sorted((a, b) -> Long.compare(b.value, a.value)) |
| 208 | + .limit(undesiredAllocations.size() - maxUndesiredAllocationsToTrack) |
| 209 | + .map(olc -> olc.key) |
| 210 | + .collect(Collectors.toSet()); |
| 211 | + undesiredAllocations.removeAll(newestExcessValues::contains); |
| 212 | + } |
| 213 | + } |
190 | 214 | } |
0 commit comments