Skip to content

Commit a0b654b

Browse files
committed
Discard excess tracking if the limit is reduced
1 parent 0af8050 commit a0b654b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/UndesiredAllocationsTracker.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
import org.elasticsearch.logging.LogManager;
2323
import org.elasticsearch.logging.Logger;
2424

25+
import java.util.Spliterator;
26+
import java.util.Spliterators;
27+
import java.util.stream.Collectors;
28+
import java.util.stream.StreamSupport;
29+
2530
/**
2631
* Keeps track of a limited number of shards that are currently in undesired allocations. If the
2732
* shards remain in undesired allocations for longer than a configurable threshold, it will log
@@ -114,6 +119,7 @@ public void cleanup(RoutingNodes routingNodes) {
114119
return true;
115120
}
116121
});
122+
shrinkIfOversized();
117123
}
118124

119125
/**
@@ -187,4 +193,22 @@ private void logUndesiredShardDetails(
187193
allocation.setDebugMode(originalDebugMode);
188194
}
189195
}
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+
}
190214
}

0 commit comments

Comments
 (0)