Skip to content

Commit ffb1a5d

Browse files
authored
Expose max_concurrent_shard_requests in _msearch (#33016)
Today `_msearch` doesn't allow modifying the `max_concurrent_shard_requests` per sub search request. This change adds support for setting this parameter on all sub-search requests in an `_msearch`. Relates to #31877
1 parent 82d10b4 commit ffb1a5d

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

docs/reference/search/multi-search.asciidoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ The msearch's `max_concurrent_searches` request parameter can be used to control
8686
the maximum number of concurrent searches the multi search api will execute.
8787
This default is based on the number of data nodes and the default search thread pool size.
8888

89+
The request parameter `max_concurrent_shard_requests` can be used to control the
90+
maximum number of concurrent shard requests the each sub search request will execute.
91+
This parameter should be used to protect a single request from overloading a cluster
92+
(e.g., a default request will hit all indices in a cluster which could cause shard request rejections
93+
if the number of shards per node is high). This default is based on the number of
94+
data nodes in the cluster but at most `256`.In certain scenarios parallelism isn't achieved through
95+
concurrent request such that this protection will result in poor performance. For
96+
instance in an environment where only a very low number of concurrent search requests are expected
97+
it might help to increase this value to a higher number.
98+
8999
[float]
90100
[[msearch-security]]
91101
=== Security

rest-api-spec/src/main/resources/rest-api-spec/api/msearch.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
"type" : "number",
3434
"description" : "A threshold that enforces a pre-filter roundtrip to prefilter search shards based on query rewriting if the number of shards the search request expands to exceeds the threshold. This filter roundtrip can limit the number of shards significantly if for instance a shard can not match any documents based on it's rewrite method ie. if date filters are mandatory to match but the shard bounds and the query are disjoint.",
3535
"default" : 128
36+
},
37+
"max_concurrent_shard_requests" : {
38+
"type" : "number",
39+
"description" : "The number of concurrent shard requests each sub search executes concurrently. This value should be used to limit the impact of the search on the cluster in order to limit the number of concurrent shard requests",
40+
"default" : "The default grows with the number of nodes in the cluster but is at most 256."
3641
}
3742
}
3843
},

rest-api-spec/src/main/resources/rest-api-spec/test/msearch/10_basic.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,35 @@ setup:
6161
- match: { responses.3.error.root_cause.0.reason: "/no.such.index/" }
6262
- match: { responses.3.error.root_cause.0.index: index_3 }
6363
- match: { responses.4.hits.total: 4 }
64+
65+
---
66+
"Least impact smoke test":
67+
# only passing these parameters to make sure they are consumed
68+
- do:
69+
max_concurrent_shard_requests: 1
70+
max_concurrent_searches: 1
71+
msearch:
72+
body:
73+
- index: index_*
74+
- query:
75+
match: {foo: foo}
76+
- index: index_2
77+
- query:
78+
match_all: {}
79+
- index: index_1
80+
- query:
81+
match: {foo: foo}
82+
- index: index_3
83+
- query:
84+
match_all: {}
85+
- type: test
86+
- query:
87+
match_all: {}
88+
89+
- match: { responses.0.hits.total: 2 }
90+
- match: { responses.1.hits.total: 1 }
91+
- match: { responses.2.hits.total: 1 }
92+
- match: { responses.3.error.root_cause.0.type: index_not_found_exception }
93+
- match: { responses.3.error.root_cause.0.reason: "/no.such.index/" }
94+
- match: { responses.3.error.root_cause.0.index: index_3 }
95+
- match: { responses.4.hits.total: 4 }

server/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean a
8686

8787
int preFilterShardSize = restRequest.paramAsInt("pre_filter_shard_size", SearchRequest.DEFAULT_PRE_FILTER_SHARD_SIZE);
8888

89+
final Integer maxConcurrentShardRequests;
90+
if (restRequest.hasParam("max_concurrent_shard_requests")) {
91+
// only set if we have the parameter since we auto adjust the max concurrency on the coordinator
92+
// based on the number of nodes in the cluster
93+
maxConcurrentShardRequests = restRequest.paramAsInt("max_concurrent_shard_requests", Integer.MIN_VALUE);
94+
} else {
95+
maxConcurrentShardRequests = null;
96+
}
8997

9098
parseMultiLineRequest(restRequest, multiRequest.indicesOptions(), allowExplicitIndex, (searchRequest, parser) -> {
9199
searchRequest.source(SearchSourceBuilder.fromXContent(parser, false));
@@ -96,6 +104,9 @@ public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean a
96104
for (SearchRequest request : requests) {
97105
// preserve if it's set on the request
98106
request.setPreFilterShardSize(Math.min(preFilterShardSize, request.getPreFilterShardSize()));
107+
if (maxConcurrentShardRequests != null) {
108+
request.setMaxConcurrentShardRequests(maxConcurrentShardRequests);
109+
}
99110
}
100111
return multiRequest;
101112
}

0 commit comments

Comments
 (0)