4040import org .elasticsearch .common .settings .Settings ;
4141import org .elasticsearch .common .util .BigArrays ;
4242import org .elasticsearch .common .util .PageCacheRecycler ;
43+ import org .elasticsearch .common .util .concurrent .EsRejectedExecutionException ;
4344import org .elasticsearch .core .internal .io .IOUtils ;
4445import org .elasticsearch .env .Environment ;
4546import org .elasticsearch .env .NodeEnvironment ;
8485
8586import java .io .IOException ;
8687import java .util .Collections ;
88+ import java .util .HashSet ;
8789import java .util .Map ;
90+ import java .util .Set ;
8891import java .util .concurrent .TimeUnit ;
8992import java .util .concurrent .atomic .AtomicBoolean ;
9093
9194import static java .util .Collections .emptyMap ;
9295import static org .elasticsearch .index .IndexService .IndexCreationContext .CREATE_INDEX ;
9396import static org .hamcrest .Matchers .containsString ;
97+ import static org .hamcrest .Matchers .empty ;
9498import static org .hamcrest .Matchers .hasToString ;
9599import static org .hamcrest .Matchers .instanceOf ;
96100
@@ -354,11 +358,19 @@ public void testForceCustomQueryCache() throws IOException {
354358 .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
355359 final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
356360 IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
357- module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache ());
358- expectThrows (AlreadySetException .class , () -> module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache ()));
361+ final Set <CustomQueryCache > liveQueryCaches = new HashSet <>();
362+ module .forceQueryCacheProvider ((a , b ) -> {
363+ final CustomQueryCache customQueryCache = new CustomQueryCache (liveQueryCaches );
364+ liveQueryCaches .add (customQueryCache );
365+ return customQueryCache ;
366+ });
367+ expectThrows (AlreadySetException .class , () -> module .forceQueryCacheProvider ((a , b ) -> {
368+ throw new AssertionError ("never called" );
369+ }));
359370 IndexService indexService = newIndexService (module );
360371 assertTrue (indexService .cache ().query () instanceof CustomQueryCache );
361372 indexService .close ("simon says" , false );
373+ assertThat (liveQueryCaches , empty ());
362374 }
363375
364376 public void testDefaultQueryCacheImplIsSelected () throws IOException {
@@ -379,12 +391,29 @@ public void testDisableQueryCacheHasPrecedenceOverForceQueryCache() throws IOExc
379391 .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
380392 final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
381393 IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
382- module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache ());
394+ module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache (null ));
383395 IndexService indexService = newIndexService (module );
384396 assertTrue (indexService .cache ().query () instanceof DisabledQueryCache );
385397 indexService .close ("simon says" , false );
386398 }
387399
400+ public void testCustomQueryCacheCleanedUpIfIndexServiceCreationFails () {
401+ Settings settings = Settings .builder ()
402+ .put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ().toString ())
403+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
404+ final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
405+ IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
406+ final Set <CustomQueryCache > liveQueryCaches = new HashSet <>();
407+ module .forceQueryCacheProvider ((a , b ) -> {
408+ final CustomQueryCache customQueryCache = new CustomQueryCache (liveQueryCaches );
409+ liveQueryCaches .add (customQueryCache );
410+ return customQueryCache ;
411+ });
412+ threadPool .shutdown (); // causes index service creation to fail
413+ expectThrows (EsRejectedExecutionException .class , () -> newIndexService (module ));
414+ assertThat (liveQueryCaches , empty ());
415+ }
416+
388417 public void testMmapNotAllowed () {
389418 String storeType = randomFrom (IndexModule .Type .HYBRIDFS .getSettingsKey (), IndexModule .Type .MMAPFS .getSettingsKey ());
390419 final Settings settings = Settings .builder ()
@@ -403,12 +432,19 @@ public void testMmapNotAllowed() {
403432
404433 class CustomQueryCache implements QueryCache {
405434
435+ private final Set <CustomQueryCache > liveQueryCaches ;
436+
437+ CustomQueryCache (Set <CustomQueryCache > liveQueryCaches ) {
438+ this .liveQueryCaches = liveQueryCaches ;
439+ }
440+
406441 @ Override
407442 public void clear (String reason ) {
408443 }
409444
410445 @ Override
411- public void close () throws IOException {
446+ public void close () {
447+ assertTrue (liveQueryCaches == null || liveQueryCaches .remove (this ));
412448 }
413449
414450 @ Override
0 commit comments