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 .FsDirectoryFactory ;
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 ;
84
89
85
90
import java .io .IOException ;
86
91
import java .util .Collections ;
92
+ import java .util .HashSet ;
87
93
import java .util .Map ;
94
+ import java .util .Set ;
88
95
import java .util .concurrent .TimeUnit ;
89
96
import java .util .concurrent .atomic .AtomicBoolean ;
90
97
91
98
import static java .util .Collections .emptyMap ;
99
+ import static java .util .Collections .singletonMap ;
92
100
import static org .elasticsearch .index .IndexService .IndexCreationContext .CREATE_INDEX ;
93
101
import static org .hamcrest .Matchers .containsString ;
102
+ import static org .hamcrest .Matchers .empty ;
94
103
import static org .hamcrest .Matchers .hasToString ;
95
104
import static org .hamcrest .Matchers .instanceOf ;
96
105
@@ -174,7 +183,7 @@ public void testRegisterIndexStore() throws IOException {
174
183
.put (IndexModule .INDEX_STORE_TYPE_SETTING .getKey (), "foo_store" )
175
184
.build ();
176
185
final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings (index , settings );
177
- final Map <String , IndexStorePlugin .DirectoryFactory > indexStoreFactories = Collections . singletonMap (
186
+ final Map <String , IndexStorePlugin .DirectoryFactory > indexStoreFactories = singletonMap (
178
187
"foo_store" , new FooFunction ());
179
188
final IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), indexStoreFactories );
180
189
@@ -354,11 +363,19 @@ public void testForceCustomQueryCache() throws IOException {
354
363
.put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
355
364
final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
356
365
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 ()));
366
+ final Set <CustomQueryCache > liveQueryCaches = new HashSet <>();
367
+ module .forceQueryCacheProvider ((a , b ) -> {
368
+ final CustomQueryCache customQueryCache = new CustomQueryCache (liveQueryCaches );
369
+ liveQueryCaches .add (customQueryCache );
370
+ return customQueryCache ;
371
+ });
372
+ expectThrows (AlreadySetException .class , () -> module .forceQueryCacheProvider ((a , b ) -> {
373
+ throw new AssertionError ("never called" );
374
+ }));
359
375
IndexService indexService = newIndexService (module );
360
376
assertTrue (indexService .cache ().query () instanceof CustomQueryCache );
361
377
indexService .close ("simon says" , false );
378
+ assertThat (liveQueryCaches , empty ());
362
379
}
363
380
364
381
public void testDefaultQueryCacheImplIsSelected () throws IOException {
@@ -379,12 +396,73 @@ public void testDisableQueryCacheHasPrecedenceOverForceQueryCache() throws IOExc
379
396
.put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
380
397
final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
381
398
IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
382
- module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache ());
399
+ module .forceQueryCacheProvider ((a , b ) -> new CustomQueryCache (null ));
383
400
IndexService indexService = newIndexService (module );
384
401
assertTrue (indexService .cache ().query () instanceof DisabledQueryCache );
385
402
indexService .close ("simon says" , false );
386
403
}
387
404
405
+ public void testCustomQueryCacheCleanedUpIfIndexServiceCreationFails () {
406
+ Settings settings = Settings .builder ()
407
+ .put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ().toString ())
408
+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
409
+ final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
410
+ IndexModule module = new IndexModule (indexSettings , emptyAnalysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
411
+ final Set <CustomQueryCache > liveQueryCaches = new HashSet <>();
412
+ module .forceQueryCacheProvider ((a , b ) -> {
413
+ final CustomQueryCache customQueryCache = new CustomQueryCache (liveQueryCaches );
414
+ liveQueryCaches .add (customQueryCache );
415
+ return customQueryCache ;
416
+ });
417
+ threadPool .shutdown (); // causes index service creation to fail
418
+ expectThrows (EsRejectedExecutionException .class , () -> newIndexService (module ));
419
+ assertThat (liveQueryCaches , empty ());
420
+ }
421
+
422
+ public void testIndexAnalyzersCleanedUpIfIndexServiceCreationFails () {
423
+ Settings settings = Settings .builder ()
424
+ .put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ().toString ())
425
+ .put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT ).build ();
426
+ final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings ("foo" , settings );
427
+
428
+ final HashSet <Analyzer > openAnalyzers = new HashSet <>();
429
+ final AnalysisModule .AnalysisProvider <AnalyzerProvider <?>> analysisProvider = (i ,e ,n ,s ) -> new AnalyzerProvider <>() {
430
+ @ Override
431
+ public String name () {
432
+ return "test" ;
433
+ }
434
+
435
+ @ Override
436
+ public AnalyzerScope scope () {
437
+ return AnalyzerScope .INDEX ;
438
+ }
439
+
440
+ @ Override
441
+ public Analyzer get () {
442
+ final Analyzer analyzer = new Analyzer () {
443
+ @ Override
444
+ protected TokenStreamComponents createComponents (String fieldName ) {
445
+ throw new AssertionError ("should not be here" );
446
+ }
447
+
448
+ @ Override
449
+ public void close () {
450
+ super .close ();
451
+ openAnalyzers .remove (this );
452
+ }
453
+ };
454
+ openAnalyzers .add (analyzer );
455
+ return analyzer ;
456
+ }
457
+ };
458
+ final AnalysisRegistry analysisRegistry = new AnalysisRegistry (environment , emptyMap (), emptyMap (), emptyMap (),
459
+ singletonMap ("test" , analysisProvider ), emptyMap (), emptyMap (), emptyMap (), emptyMap (), emptyMap ());
460
+ IndexModule module = new IndexModule (indexSettings , analysisRegistry , new InternalEngineFactory (), Collections .emptyMap ());
461
+ threadPool .shutdown (); // causes index service creation to fail
462
+ expectThrows (EsRejectedExecutionException .class , () -> newIndexService (module ));
463
+ assertThat (openAnalyzers , empty ());
464
+ }
465
+
388
466
public void testMmapNotAllowed () {
389
467
String storeType = randomFrom (IndexModule .Type .HYBRIDFS .getSettingsKey (), IndexModule .Type .MMAPFS .getSettingsKey ());
390
468
final Settings settings = Settings .builder ()
@@ -403,12 +481,19 @@ public void testMmapNotAllowed() {
403
481
404
482
class CustomQueryCache implements QueryCache {
405
483
484
+ private final Set <CustomQueryCache > liveQueryCaches ;
485
+
486
+ CustomQueryCache (Set <CustomQueryCache > liveQueryCaches ) {
487
+ this .liveQueryCaches = liveQueryCaches ;
488
+ }
489
+
406
490
@ Override
407
491
public void clear (String reason ) {
408
492
}
409
493
410
494
@ Override
411
- public void close () throws IOException {
495
+ public void close () {
496
+ assertTrue (liveQueryCaches == null || liveQueryCaches .remove (this ));
412
497
}
413
498
414
499
@ Override
0 commit comments