|
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; |
|
47 | 48 | import org.elasticsearch.env.ShardLock; |
48 | 49 | import org.elasticsearch.env.TestEnvironment; |
49 | 50 | import org.elasticsearch.index.analysis.AnalysisRegistry; |
| 51 | +import org.elasticsearch.index.analysis.AnalyzerProvider; |
| 52 | +import org.elasticsearch.index.analysis.AnalyzerScope; |
50 | 53 | import org.elasticsearch.index.cache.query.DisabledQueryCache; |
51 | 54 | import org.elasticsearch.index.cache.query.IndexQueryCache; |
52 | 55 | import org.elasticsearch.index.cache.query.QueryCache; |
|
66 | 69 | import org.elasticsearch.index.store.FsDirectoryFactory; |
67 | 70 | import org.elasticsearch.indices.IndicesModule; |
68 | 71 | import org.elasticsearch.indices.IndicesQueryCache; |
| 72 | +import org.elasticsearch.indices.analysis.AnalysisModule; |
69 | 73 | import org.elasticsearch.indices.breaker.CircuitBreakerService; |
70 | 74 | import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; |
71 | 75 | import org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason; |
|
92 | 96 | import java.util.concurrent.atomic.AtomicBoolean; |
93 | 97 |
|
94 | 98 | import static java.util.Collections.emptyMap; |
| 99 | +import static java.util.Collections.singletonMap; |
95 | 100 | import static org.elasticsearch.index.IndexService.IndexCreationContext.CREATE_INDEX; |
96 | 101 | import static org.hamcrest.Matchers.containsString; |
97 | 102 | import static org.hamcrest.Matchers.empty; |
@@ -178,7 +183,7 @@ public void testRegisterIndexStore() throws IOException { |
178 | 183 | .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "foo_store") |
179 | 184 | .build(); |
180 | 185 | final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings); |
181 | | - final Map<String, IndexStorePlugin.DirectoryFactory> indexStoreFactories = Collections.singletonMap( |
| 186 | + final Map<String, IndexStorePlugin.DirectoryFactory> indexStoreFactories = singletonMap( |
182 | 187 | "foo_store", new FooFunction()); |
183 | 188 | final IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), indexStoreFactories); |
184 | 189 |
|
@@ -414,6 +419,50 @@ public void testCustomQueryCacheCleanedUpIfIndexServiceCreationFails() { |
414 | 419 | assertThat(liveQueryCaches, empty()); |
415 | 420 | } |
416 | 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 | + |
417 | 466 | public void testMmapNotAllowed() { |
418 | 467 | String storeType = randomFrom(IndexModule.Type.HYBRIDFS.getSettingsKey(), IndexModule.Type.MMAPFS.getSettingsKey()); |
419 | 468 | final Settings settings = Settings.builder() |
|
0 commit comments