From 5b1f8d4b37a7d7e800ff87a1bd1188916262b7da Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Wed, 26 Jan 2022 20:11:24 +0100 Subject: [PATCH] Make AllocationDeciders.canRemain more Efficient (#83171) The iteration before this change was over the value collection of a linked hash map, wrapped in unmodifiable. Given that this runs approximately #deciders * #shards iterations during a reroute it makes sense to optimize here. In practice the iteration did not inline well and showed up as about 25% of the overall cost of this method. --- .../routing/allocation/decider/AllocationDeciders.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDeciders.java b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDeciders.java index f6f43bd5c47c6..83d68724d0d0c 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDeciders.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDeciders.java @@ -17,7 +17,6 @@ import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import java.util.Collection; -import java.util.Collections; /** * A composite {@link AllocationDecider} combining the "decision" of multiple @@ -27,10 +26,10 @@ public class AllocationDeciders extends AllocationDecider { private static final Logger logger = LogManager.getLogger(AllocationDeciders.class); - private final Collection allocations; + private final AllocationDecider[] allocations; public AllocationDeciders(Collection allocations) { - this.allocations = Collections.unmodifiableCollection(allocations); + this.allocations = allocations.toArray(AllocationDecider[]::new); } @Override @@ -265,7 +264,7 @@ public Decision canAllocateReplicaWhenThereIsRetentionLease(ShardRouting shardRo return ret; } - private void addDecision(Decision.Multi ret, Decision decision, RoutingAllocation allocation) { + private static void addDecision(Decision.Multi ret, Decision decision, RoutingAllocation allocation) { // We never add ALWAYS decisions and only add YES decisions when requested by debug mode (since Multi default is YES). if (decision != Decision.ALWAYS && (allocation.getDebugMode() == RoutingAllocation.DebugMode.ON || decision.type() != Decision.Type.YES)) {