18
18
*/
19
19
package org .elasticsearch .index ;
20
20
21
+ import org .apache .lucene .analysis .Analyzer ;
21
22
import org .apache .lucene .index .AssertingDirectoryReader ;
22
23
import org .apache .lucene .index .DirectoryReader ;
23
24
import org .apache .lucene .index .FieldInvertState ;
40
41
import org .elasticsearch .common .settings .Settings ;
41
42
import org .elasticsearch .common .util .BigArrays ;
42
43
import org .elasticsearch .common .util .PageCacheRecycler ;
44
+ import org .elasticsearch .common .util .concurrent .EsRejectedExecutionException ;
43
45
import org .elasticsearch .core .internal .io .IOUtils ;
44
46
import org .elasticsearch .env .Environment ;
45
47
import org .elasticsearch .env .NodeEnvironment ;
46
48
import org .elasticsearch .env .ShardLock ;
47
49
import org .elasticsearch .env .TestEnvironment ;
48
50
import org .elasticsearch .index .analysis .AnalysisRegistry ;
51
+ import org .elasticsearch .index .analysis .AnalyzerProvider ;
52
+ import org .elasticsearch .index .analysis .AnalyzerScope ;
49
53
import org .elasticsearch .index .cache .query .DisabledQueryCache ;
50
54
import org .elasticsearch .index .cache .query .IndexQueryCache ;
51
55
import org .elasticsearch .index .cache .query .QueryCache ;
65
69
import org .elasticsearch .index .store .IndexStore ;
66
70
import org .elasticsearch .indices .IndicesModule ;
67
71
import org .elasticsearch .indices .IndicesQueryCache ;
72
+ import org .elasticsearch .indices .analysis .AnalysisModule ;
68
73
import org .elasticsearch .indices .breaker .CircuitBreakerService ;
69
74
import org .elasticsearch .indices .breaker .NoneCircuitBreakerService ;
70
75
import org .elasticsearch .indices .cluster .IndicesClusterStateService .AllocatedIndices .IndexRemovalReason ;
83
88
84
89
import java .io .IOException ;
85
90
import java .util .Collections ;
91
+ import java .util .HashSet ;
86
92
import java .util .Map ;
93
+ import java .util .Set ;
87
94
import java .util .concurrent .TimeUnit ;
88
95
import java .util .concurrent .atomic .AtomicBoolean ;
89
96
import java .util .function .Function ;
90
97
91
98
import static java .util .Collections .emptyMap ;
99
+ import static java .util .Collections .singletonMap ;
92
100
import static org .hamcrest .Matchers .containsString ;
101
+ import static org .hamcrest .Matchers .empty ;
93
102
import static org .hamcrest .Matchers .hasToString ;
94
103
import static org .hamcrest .Matchers .instanceOf ;
95
104
@@ -351,11 +360,19 @@ public void testForceCustomQueryCache() throws IOException {
351
360
.put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
352
361
final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
353
362
IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
354
- module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache ());
355
- expectThrows (AlreadySetException .class , () -> module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache ()));
363
+ final Set <CustomQueryCache > liveQueryCaches = new HashSet <>();
364
+ module .forceQueryCacheProvider ((a , b ) -> {
365
+ final CustomQueryCache customQueryCache = new CustomQueryCache (liveQueryCaches );
366
+ liveQueryCaches .add (customQueryCache );
367
+ return customQueryCache ;
368
+ });
369
+ expectThrows (AlreadySetException .class , () -> module .forceQueryCacheProvider ((a , b ) -> {
370
+ throw new AssertionError ("never called" );
371
+ }));
356
372
IndexService indexService = newIndexService (module );
357
373
assertTrue (indexService .cache ().query () instanceof CustomQueryCache );
358
374
indexService .close ("simon says" , false );
375
+ assertThat (liveQueryCaches , empty ());
359
376
}
360
377
361
378
public void testDefaultQueryCacheImplIsSelected () throws IOException {
@@ -376,12 +393,73 @@ public void testDisableQueryCacheHasPrecedenceOverForceQueryCache() throws IOExc
376
393
.put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
377
394
final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
378
395
IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
379
- module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache ());
396
+ module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache (null ));
380
397
IndexService indexService = newIndexService (module );
381
398
assertTrue (indexService .cache ().query () instanceof DisabledQueryCache );
382
399
indexService .close ("simon says" , false );
383
400
}
384
401
402
+ public void testCustomQueryCacheCleanedUpIfIndexServiceCreationFails () {
403
+ Settings settings = Settings .builder ()
404
+ .put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ().toString ())
405
+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
406
+ final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
407
+ IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
408
+ final Set <CustomQueryCache > liveQueryCaches = new HashSet <>();
409
+ module .forceQueryCacheProvider ((a , b ) -> {
410
+ final CustomQueryCache customQueryCache = new CustomQueryCache (liveQueryCaches );
411
+ liveQueryCaches .add (customQueryCache );
412
+ return customQueryCache ;
413
+ });
414
+ threadPool .shutdown (); // causes index service creation to fail
415
+ expectThrows (EsRejectedExecutionException .class , () -> newIndexService (module ));
416
+ assertThat (liveQueryCaches , empty ());
417
+ }
418
+
419
+ public void testIndexAnalyzersCleanedUpIfIndexServiceCreationFails () {
420
+ Settings settings = Settings .builder ()
421
+ .put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ().toString ())
422
+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
423
+ final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
424
+
425
+ final HashSet <Analyzer > openAnalyzers = new HashSet <>();
426
+ final AnalysisModule .AnalysisProvider <AnalyzerProvider <?>> analysisProvider = (i ,e ,n ,s ) -> new AnalyzerProvider <Analyzer >() {
427
+ @ Override
428
+ public String name () {
429
+ return "test" ;
430
+ }
431
+
432
+ @ Override
433
+ public AnalyzerScope scope () {
434
+ return AnalyzerScope .INDEX ;
435
+ }
436
+
437
+ @ Override
438
+ public Analyzer get () {
439
+ final Analyzer analyzer = new Analyzer () {
440
+ @ Override
441
+ protected TokenStreamComponents createComponents (String fieldName ) {
442
+ throw new AssertionError ("should not be here" );
443
+ }
444
+
445
+ @ Override
446
+ public void close () {
447
+ super .close ();
448
+ openAnalyzers .remove (this );
449
+ }
450
+ };
451
+ openAnalyzers .add (analyzer );
452
+ return analyzer ;
453
+ }
454
+ };
455
+ final AnalysisRegistry analysisRegistry = new AnalysisRegistry (environment , emptyMap (), emptyMap (), emptyMap (),
456
+ singletonMap ("test" , analysisProvider ), emptyMap (), emptyMap (), emptyMap (), emptyMap (), emptyMap ());
457
+ IndexModule module = new IndexModule (indexSettings , analysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
458
+ threadPool .shutdown (); // causes index service creation to fail
459
+ expectThrows (EsRejectedExecutionException .class , () -> newIndexService (module ));
460
+ assertThat (openAnalyzers , empty ());
461
+ }
462
+
385
463
public void testMmapNotAllowed () {
386
464
String storeType = randomFrom (IndexModule .Type .HYBRIDFS .getSettingsKey (), IndexModule .Type .MMAPFS .getSettingsKey ());
387
465
final Settings settings = Settings .builder ()
@@ -400,12 +478,19 @@ public void testMmapNotAllowed() {
400
478
401
479
class CustomQueryCache implements QueryCache {
402
480
481
+ private final Set <CustomQueryCache > liveQueryCaches ;
482
+
483
+ CustomQueryCache (Set <CustomQueryCache > liveQueryCaches ) {
484
+ this .liveQueryCaches = liveQueryCaches ;
485
+ }
486
+
403
487
@ Override
404
488
public void clear (String reason ) {
405
489
}
406
490
407
491
@ Override
408
- public void close () throws IOException {
492
+ public void close () {
493
+ assertTrue (liveQueryCaches == null || liveQueryCaches .remove (this ));
409
494
}
410
495
411
496
@ Override
0 commit comments