1919package org .elasticsearch .search ;
2020
2121import com .carrotsearch .hppc .IntArrayList ;
22-
2322import org .apache .lucene .search .Query ;
2423import org .apache .lucene .store .AlreadyClosedException ;
2524import org .elasticsearch .ElasticsearchException ;
5150import org .elasticsearch .index .query .QueryRewriteContext ;
5251import org .elasticsearch .index .query .QueryShardContext ;
5352import org .elasticsearch .index .query .TermQueryBuilder ;
53+ import org .elasticsearch .index .search .stats .SearchStats ;
5454import org .elasticsearch .index .shard .IndexShard ;
5555import org .elasticsearch .index .shard .SearchOperationListener ;
5656import org .elasticsearch .index .shard .ShardId ;
@@ -226,6 +226,7 @@ public void testSearchWhileIndexDeleted() throws InterruptedException {
226226 AtomicBoolean running = new AtomicBoolean (true );
227227 CountDownLatch startGun = new CountDownLatch (1 );
228228 Semaphore semaphore = new Semaphore (Integer .MAX_VALUE );
229+
229230 final Thread thread = new Thread () {
230231 @ Override
231232 public void run () {
@@ -261,12 +262,16 @@ public void onFailure(Exception e) {
261262 try {
262263 final int rounds = scaledRandomIntBetween (100 , 10000 );
263264 SearchRequest searchRequest = new SearchRequest ().allowPartialSearchResults (true );
265+ SearchRequest scrollSearchRequest = new SearchRequest ().allowPartialSearchResults (true )
266+ .scroll (new Scroll (TimeValue .timeValueMinutes (1 )));
264267 for (int i = 0 ; i < rounds ; i ++) {
265268 try {
266269 try {
267270 PlainActionFuture <SearchPhaseResult > result = new PlainActionFuture <>();
271+ final boolean useScroll = randomBoolean ();
268272 service .executeQueryPhase (
269- new ShardSearchLocalRequest (searchRequest , indexShard .shardId (), 1 ,
273+ new ShardSearchLocalRequest (useScroll ? scrollSearchRequest : searchRequest ,
274+ indexShard .shardId (), 1 ,
270275 new AliasFilter (null , Strings .EMPTY_ARRAY ), 1.0f , -1 , null , null ),
271276 new SearchTask (123L , "" , "" , "" , null , Collections .emptyMap ()), result );
272277 SearchPhaseResult searchPhaseResult = result .get ();
@@ -276,6 +281,9 @@ public void onFailure(Exception e) {
276281 PlainActionFuture <FetchSearchResult > listener = new PlainActionFuture <>();
277282 service .executeFetchPhase (req , new SearchTask (123L , "" , "" , "" , null , Collections .emptyMap ()), listener );
278283 listener .get ();
284+ if (useScroll ) {
285+ service .freeContext (searchPhaseResult .getRequestId ());
286+ }
279287 } catch (ExecutionException ex ) {
280288 assertThat (ex .getCause (), instanceOf (RuntimeException .class ));
281289 throw ((RuntimeException )ex .getCause ());
@@ -293,6 +301,13 @@ public void onFailure(Exception e) {
293301 thread .join ();
294302 semaphore .acquire (Integer .MAX_VALUE );
295303 }
304+
305+ assertEquals (0 , service .getActiveContexts ());
306+
307+ SearchStats .Stats totalStats = indexShard .searchStats ().getTotal ();
308+ assertEquals (0 , totalStats .getQueryCurrent ());
309+ assertEquals (0 , totalStats .getScrollCurrent ());
310+ assertEquals (0 , totalStats .getFetchCurrent ());
296311 }
297312
298313 public void testTimeout () throws IOException {
0 commit comments