40
40
import org .elasticsearch .common .settings .Settings ;
41
41
import org .elasticsearch .common .util .BigArrays ;
42
42
import org .elasticsearch .common .util .PageCacheRecycler ;
43
+ import org .elasticsearch .common .util .concurrent .EsRejectedExecutionException ;
43
44
import org .elasticsearch .core .internal .io .IOUtils ;
44
45
import org .elasticsearch .env .Environment ;
45
46
import org .elasticsearch .env .NodeEnvironment ;
84
85
85
86
import java .io .IOException ;
86
87
import java .util .Collections ;
88
+ import java .util .HashSet ;
87
89
import java .util .Map ;
90
+ import java .util .Set ;
88
91
import java .util .concurrent .TimeUnit ;
89
92
import java .util .concurrent .atomic .AtomicBoolean ;
90
93
91
94
import static java .util .Collections .emptyMap ;
92
95
import static org .elasticsearch .index .IndexService .IndexCreationContext .CREATE_INDEX ;
93
96
import static org .hamcrest .Matchers .containsString ;
97
+ import static org .hamcrest .Matchers .empty ;
94
98
import static org .hamcrest .Matchers .hasToString ;
95
99
import static org .hamcrest .Matchers .instanceOf ;
96
100
@@ -354,11 +358,19 @@ public void testForceCustomQueryCache() throws IOException {
354
358
.put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
355
359
final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
356
360
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
+ }));
359
370
IndexService indexService = newIndexService (module );
360
371
assertTrue (indexService .cache ().query () instanceof CustomQueryCache );
361
372
indexService .close ("simon says" , false );
373
+ assertThat (liveQueryCaches , empty ());
362
374
}
363
375
364
376
public void testDefaultQueryCacheImplIsSelected () throws IOException {
@@ -379,12 +391,29 @@ public void testDisableQueryCacheHasPrecedenceOverForceQueryCache() throws IOExc
379
391
.put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
380
392
final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
381
393
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 ));
383
395
IndexService indexService = newIndexService (module );
384
396
assertTrue (indexService .cache ().query () instanceof DisabledQueryCache );
385
397
indexService .close ("simon says" , false );
386
398
}
387
399
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
+
388
417
public void testMmapNotAllowed () {
389
418
String storeType = randomFrom (IndexModule .Type .HYBRIDFS .getSettingsKey (), IndexModule .Type .MMAPFS .getSettingsKey ());
390
419
final Settings settings = Settings .builder ()
@@ -403,12 +432,19 @@ public void testMmapNotAllowed() {
403
432
404
433
class CustomQueryCache implements QueryCache {
405
434
435
+ private final Set <CustomQueryCache > liveQueryCaches ;
436
+
437
+ CustomQueryCache (Set <CustomQueryCache > liveQueryCaches ) {
438
+ this .liveQueryCaches = liveQueryCaches ;
439
+ }
440
+
406
441
@ Override
407
442
public void clear (String reason ) {
408
443
}
409
444
410
445
@ Override
411
- public void close () throws IOException {
446
+ public void close () {
447
+ assertTrue (liveQueryCaches == null || liveQueryCaches .remove (this ));
412
448
}
413
449
414
450
@ Override
0 commit comments