@@ -489,6 +489,52 @@ public void testMaxOpenScrollContexts() throws RuntimeException, IOException {
489489 }
490490 }
491491
492+ public void testOpenScrollContextsConcurrently () throws Exception {
493+ final int maxScrollContexts = randomIntBetween (50 , 200 );
494+ assertAcked (client ().admin ().cluster ().prepareUpdateSettings ()
495+ .setTransientSettings (Settings .builder ().put (SearchService .MAX_OPEN_SCROLL_CONTEXT .getKey (), maxScrollContexts )));
496+ try {
497+ createIndex ("index" );
498+ final IndicesService indicesService = getInstanceFromNode (IndicesService .class );
499+ final IndexShard indexShard = indicesService .indexServiceSafe (resolveIndex ("index" )).getShard (0 );
500+
501+ final SearchService searchService = getInstanceFromNode (SearchService .class );
502+ Thread [] threads = new Thread [randomIntBetween (2 , 8 )];
503+ CountDownLatch latch = new CountDownLatch (threads .length );
504+ for (int i = 0 ; i < threads .length ; i ++) {
505+ threads [i ] = new Thread (() -> {
506+ latch .countDown ();
507+ try {
508+ latch .await ();
509+ for (; ; ) {
510+ try {
511+ searchService .createAndPutContext (new ShardScrollRequestTest (indexShard .shardId ()));
512+ } catch (ElasticsearchException e ) {
513+ assertThat (e .getMessage (), equalTo (
514+ "Trying to create too many scroll contexts. Must be less than or equal to: " +
515+ "[" + maxScrollContexts + "]. " +
516+ "This limit can be set by changing the [search.max_open_scroll_context] setting." ));
517+ return ;
518+ }
519+ }
520+ } catch (Exception e ) {
521+ throw new AssertionError (e );
522+ }
523+ });
524+ threads [i ].setName ("elasticsearch[node_s_0][search]" );
525+ threads [i ].start ();
526+ }
527+ for (Thread thread : threads ) {
528+ thread .join ();
529+ }
530+ assertThat (searchService .getActiveContexts (), equalTo (maxScrollContexts ));
531+ searchService .freeAllScrollContexts ();
532+ } finally {
533+ assertAcked (client ().admin ().cluster ().prepareUpdateSettings ()
534+ .setTransientSettings (Settings .builder ().putNull (SearchService .MAX_OPEN_SCROLL_CONTEXT .getKey ())));
535+ }
536+ }
537+
492538 public static class FailOnRewriteQueryPlugin extends Plugin implements SearchPlugin {
493539 @ Override
494540 public List <QuerySpec <?>> getQueries () {
0 commit comments