Skip to content

Commit 235867c

Browse files
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.
1 parent ff42cb6 commit 235867c

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

server/src/main/java/org/elasticsearch/search/SearchService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.action.support.TransportActions;
3131
import org.elasticsearch.cluster.ProjectState;
3232
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.ResolvedExpression;
33+
import org.elasticsearch.cluster.routing.RecoverySource;
3334
import org.elasticsearch.cluster.routing.ShardRouting;
3435
import org.elasticsearch.cluster.service.ClusterService;
3536
import org.elasticsearch.common.CheckedSupplier;
@@ -531,7 +532,7 @@ public void beforeIndexShardCreated(ShardRouting routing, Settings indexSettings
531532
// to stop searches to restore full availability as fast as possible. A known scenario here is that we lost connection to master
532533
// or master(s) were restarted.
533534
assert routing.initializing();
534-
if (routing.isRelocationTarget() == false) {
535+
if (routing.isRelocationTarget() == false && routing.recoverySource() != RecoverySource.EmptyStoreRecoverySource.INSTANCE) {
535536
freeAllContextsForShard(routing.shardId());
536537
}
537538
}

0 commit comments

Comments
 (0)