From 063a6d047cd264ed435c4cee6eeaf7866f86ba08 Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Wed, 9 Sep 2020 14:45:45 -0400 Subject: [PATCH] Release search context when scroll keep_alive is too large (#62179) Previously, we close related search contexts if the keep_alive of a scroll is too large. But we accidentally change this behavior in #62061. --- .../elasticsearch/search/SearchService.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/search/SearchService.java b/server/src/main/java/org/elasticsearch/search/SearchService.java index 13a17bdeb6d..aa46fcc9b65 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchService.java +++ b/server/src/main/java/org/elasticsearch/search/SearchService.java @@ -471,7 +471,14 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv SearchShardTask task, ActionListener listener) { final LegacyReaderContext readerContext = (LegacyReaderContext) findReaderContext(request.contextId(), request); - final Releasable markAsUsed = readerContext.markAsUsed(getScrollKeepAlive(request.scroll())); + final Releasable markAsUsed; + try { + markAsUsed = readerContext.markAsUsed(getScrollKeepAlive(request.scroll())); + } catch (Exception e) { + // We need to release the reader context of the scroll when we hit any exception (here the keep_alive can be too large) + freeReaderContext(readerContext.id()); + throw e; + } runAsync(getExecutor(readerContext.indexShard()), () -> { final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(null); try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, false); @@ -536,7 +543,14 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv public void executeFetchPhase(InternalScrollSearchRequest request, SearchShardTask task, ActionListener listener) { final LegacyReaderContext readerContext = (LegacyReaderContext) findReaderContext(request.contextId(), request); - final Releasable markAsUsed = readerContext.markAsUsed(getScrollKeepAlive(request.scroll())); + final Releasable markAsUsed; + try { + markAsUsed = readerContext.markAsUsed(getScrollKeepAlive(request.scroll())); + } catch (Exception e) { + // We need to release the reader context of the scroll when we hit any exception (here the keep_alive can be too large) + freeReaderContext(readerContext.id()); + throw e; + } runAsync(getExecutor(readerContext.indexShard()), () -> { final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(null); try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, false);