Skip to content

Commit 655e352

Browse files
committed
Use ordered map to quickly determine earliest entry
1 parent 566f5d5 commit 655e352

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.elasticsearch.logging.LogManager;
2222
import org.elasticsearch.logging.Logger;
2323

24-
import java.util.HashMap;
24+
import java.util.LinkedHashMap;
2525
import java.util.Map;
2626
import java.util.stream.Collectors;
2727

@@ -76,7 +76,7 @@ public class UndesiredAllocationsTracker {
7676
);
7777

7878
private final TimeProvider timeProvider;
79-
private final Map<String, UndesiredAllocation> undesiredAllocations = new HashMap<>();
79+
private final LinkedHashMap<String, UndesiredAllocation> undesiredAllocations = new LinkedHashMap<>();
8080
private final FrequencyCappedAction undesiredAllocationDurationLogInterval;
8181
private volatile TimeValue undesiredAllocationDurationLoggingThreshold;
8282
private volatile int maxUndesiredAllocationsToTrack;
@@ -148,18 +148,15 @@ public void maybeLogUndesiredShardsWarning(
148148
DesiredBalance desiredBalance
149149
) {
150150
final long currentTimeMillis = timeProvider.relativeTimeInMillis();
151-
long earliestUndesiredTimestamp = Long.MAX_VALUE;
152-
for (var undesiredAllocation : undesiredAllocations.values()) {
153-
if (undesiredAllocation.undesiredSince() < earliestUndesiredTimestamp) {
154-
earliestUndesiredTimestamp = undesiredAllocation.undesiredSince();
151+
if (undesiredAllocations.isEmpty() == false) {
152+
final long earliestUndesiredTimestamp = undesiredAllocations.firstEntry().getValue().undesiredSince();
153+
if (earliestUndesiredTimestamp < currentTimeMillis
154+
&& currentTimeMillis - earliestUndesiredTimestamp > undesiredAllocationDurationLoggingThreshold.millis()) {
155+
undesiredAllocationDurationLogInterval.maybeExecute(
156+
() -> logDecisionsForUndesiredShardsOverThreshold(routingNodes, routingAllocation, desiredBalance)
157+
);
155158
}
156159
}
157-
if (earliestUndesiredTimestamp < currentTimeMillis
158-
&& currentTimeMillis - earliestUndesiredTimestamp > undesiredAllocationDurationLoggingThreshold.millis()) {
159-
undesiredAllocationDurationLogInterval.maybeExecute(
160-
() -> logDecisionsForUndesiredShardsOverThreshold(routingNodes, routingAllocation, desiredBalance)
161-
);
162-
}
163160
}
164161

165162
private void logDecisionsForUndesiredShardsOverThreshold(

0 commit comments

Comments
 (0)