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 ;
5251import org .elasticsearch .index .query .QueryRewriteContext ;
5352import org .elasticsearch .index .query .QueryShardContext ;
5453import org .elasticsearch .index .query .TermQueryBuilder ;
54+ import org .elasticsearch .index .search .stats .SearchStats ;
5555import org .elasticsearch .index .shard .IndexShard ;
5656import org .elasticsearch .index .shard .SearchOperationListener ;
5757import org .elasticsearch .index .shard .ShardId ;
@@ -229,6 +229,7 @@ public void testSearchWhileIndexDeleted() throws InterruptedException {
229229 AtomicBoolean running = new AtomicBoolean (true );
230230 CountDownLatch startGun = new CountDownLatch (1 );
231231 Semaphore semaphore = new Semaphore (Integer .MAX_VALUE );
232+
232233 final Thread thread = new Thread () {
233234 @ Override
234235 public void run () {
@@ -267,10 +268,17 @@ public void onFailure(Exception e) {
267268 try {
268269 try {
269270 PlainActionFuture <SearchPhaseResult > result = new PlainActionFuture <>();
270- service .executeQueryPhase (
271- new ShardSearchLocalRequest (indexShard .shardId (), 1 , SearchType .DEFAULT ,
271+ final boolean useScroll = randomBoolean ();
272+ ShardSearchLocalRequest shardRequest ;
273+ if (useScroll ) {
274+ shardRequest = new ShardScrollRequestTest (indexShard .shardId ());
275+ } else {
276+ shardRequest = new ShardSearchLocalRequest (indexShard .shardId (), 1 , SearchType .DEFAULT ,
272277 new SearchSourceBuilder (), new String [0 ], false , new AliasFilter (null , Strings .EMPTY_ARRAY ), 1.0f ,
273- true , null , null ),
278+ true , null , null );
279+ }
280+ service .executeQueryPhase (
281+ shardRequest ,
274282 new SearchTask (123L , "" , "" , "" , null , Collections .emptyMap ()), result );
275283 SearchPhaseResult searchPhaseResult = result .get ();
276284 IntArrayList intCursors = new IntArrayList (1 );
@@ -279,6 +287,9 @@ public void onFailure(Exception e) {
279287 PlainActionFuture <FetchSearchResult > listener = new PlainActionFuture <>();
280288 service .executeFetchPhase (req , new SearchTask (123L , "" , "" , "" , null , Collections .emptyMap ()), listener );
281289 listener .get ();
290+ if (useScroll ) {
291+ service .freeContext (searchPhaseResult .getRequestId ());
292+ }
282293 } catch (ExecutionException ex ) {
283294 assertThat (ex .getCause (), instanceOf (RuntimeException .class ));
284295 throw ((RuntimeException )ex .getCause ());
@@ -296,6 +307,13 @@ public void onFailure(Exception e) {
296307 thread .join ();
297308 semaphore .acquire (Integer .MAX_VALUE );
298309 }
310+
311+ assertEquals (0 , service .getActiveContexts ());
312+
313+ SearchStats .Stats totalStats = indexShard .searchStats ().getTotal ();
314+ assertEquals (0 , totalStats .getQueryCurrent ());
315+ assertEquals (0 , totalStats .getScrollCurrent ());
316+ assertEquals (0 , totalStats .getFetchCurrent ());
299317 }
300318
301319 public void testTimeout () throws IOException {
0 commit comments