From 74c4dc140eaa5fdaf12eb45a6682136d9db075f6 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 14 Apr 2025 21:19:33 +0200 Subject: [PATCH] Avoid walking the complete list of search contexts on shard creation (#123855) This I found in the many-shards benchmark during some manual testing. Creating indices slows down measurably when there's concurrent searches going on. Interestingly enough, the bulk of the cost is coming from this hook. This makes sense to some extend because the map can quickly grow to a massive size as it scales as O(shards_searched_on_average * concurrent_searches) and a CHM generally is anything but cheap to iterate over. --- .../src/main/java/org/elasticsearch/search/SearchService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/search/SearchService.java b/server/src/main/java/org/elasticsearch/search/SearchService.java index 3ea467fa67b41..2358287bf5f31 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchService.java +++ b/server/src/main/java/org/elasticsearch/search/SearchService.java @@ -30,6 +30,7 @@ import org.elasticsearch.action.support.TransportActions; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.ResolvedExpression; +import org.elasticsearch.cluster.routing.RecoverySource; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.CheckedSupplier; @@ -511,7 +512,7 @@ public void beforeIndexShardCreated(ShardRouting routing, Settings indexSettings // to stop searches to restore full availability as fast as possible. A known scenario here is that we lost connection to master // or master(s) were restarted. assert routing.initializing(); - if (routing.isRelocationTarget() == false) { + if (routing.isRelocationTarget() == false && routing.recoverySource() != RecoverySource.EmptyStoreRecoverySource.INSTANCE) { freeAllContextsForShard(routing.shardId()); } }