-
Notifications
You must be signed in to change notification settings - Fork 3.6k
ElasticSearch: Add timeout parameter to _search #28935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,6 +122,7 @@ public class ElasticsearchClient | |
| private final BackpressureRestHighLevelClient client; | ||
| private final int scrollSize; | ||
| private final Duration scrollTimeout; | ||
| private final Duration requestTimeout; | ||
|
|
||
| private final AtomicReference<Set<ElasticsearchNode>> nodes = new AtomicReference<>(ImmutableSet.of()); | ||
| private final ScheduledExecutorService executor = newSingleThreadScheduledExecutor(daemonThreadsNamed("NodeRefresher")); | ||
|
|
@@ -146,6 +147,7 @@ public ElasticsearchClient( | |
| this.ignorePublishAddress = config.isIgnorePublishAddress(); | ||
| this.scrollSize = config.getScrollSize(); | ||
| this.scrollTimeout = config.getScrollTimeout(); | ||
| this.requestTimeout = config.getRequestTimeout(); | ||
| this.refreshInterval = config.getNodeRefreshInterval(); | ||
| this.tlsEnabled = config.isTlsEnabled(); | ||
| } | ||
|
|
@@ -580,7 +582,8 @@ public String executeQuery(String index, String query) | |
| public SearchResponse beginSearch(String index, int shard, QueryBuilder query, Optional<List<String>> fields, List<String> documentFields, Optional<String> sort, OptionalLong limit) | ||
| { | ||
| SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource() | ||
| .query(query); | ||
| .query(query) | ||
| .timeout(new TimeValue(requestTimeout.toMillis())); | ||
|
|
||
| if (limit.isPresent() && limit.getAsLong() < scrollSize) { | ||
| // Safe to cast it to int because scrollSize is int. | ||
|
|
@@ -612,7 +615,11 @@ public SearchResponse beginSearch(String index, int shard, QueryBuilder query, O | |
|
|
||
| long start = System.nanoTime(); | ||
| try { | ||
| return client.search(request); | ||
| SearchResponse response = client.search(request); | ||
| if (response.isTimedOut()) { | ||
| throw new TrinoException(ELASTICSEARCH_CONNECTION_ERROR, "Elasticsearch query timed out"); | ||
| } | ||
| return response; | ||
| } | ||
| catch (IOException e) { | ||
| throw new TrinoException(ELASTICSEARCH_CONNECTION_ERROR, e); | ||
|
|
@@ -642,7 +649,11 @@ public SearchResponse nextPage(String scrollId) | |
|
|
||
| long start = System.nanoTime(); | ||
| try { | ||
| return client.searchScroll(request); | ||
| SearchResponse response = client.searchScroll(request); | ||
| if (response.isTimedOut()) { | ||
| throw new TrinoException(ELASTICSEARCH_CONNECTION_ERROR, "Elasticsearch query timed out"); | ||
| } | ||
|
Comment on lines
+653
to
+655
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Throwing immediately on Useful? React with 👍 / 👎. |
||
| return response; | ||
| } | ||
| catch (IOException e) { | ||
| throw new TrinoException(ELASTICSEARCH_CONNECTION_ERROR, e); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
client.search(request)returnstimed_out=true, this branch throws before returning theSearchResponse. In thebeginSearchpath, that happens beforeScanQueryPageSource.SearchHitIteratoris constructed, so no caller can read the returned_scroll_idand invokeclearScroll. When timed-out scroll searches return a scroll ID, those contexts are left open untilelasticsearch.scroll-timeoutexpires, which can accumulate under repeated timeouts and waste Elasticsearch resources.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codex This is a good remark, but I think it's a separate issue. Whether we want to clear scrolls and when is not specific to the timeout case.
If your human masters want me to specifically add a clearScroll call before the throw here I'll do it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To use Codex here, create an environment for this repo.