Skip to content

Commit 749c3ec

Browse files
author
David Roberts
authored
Remove the single argument Environment constructor (#27235)
Only tests should use the single argument Environment constructor. To enforce this the single arg Environment constructor has been replaced with a test framework factory method. Production code (beyond initial Bootstrap) should always use the same Environment object that Node.getEnvironment() returns. This Environment is also available via dependency injection.
1 parent 964016e commit 749c3ec

File tree

28 files changed

+129
-72
lines changed

28 files changed

+129
-72
lines changed

core/src/main/java/org/elasticsearch/env/Environment.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ public class Environment {
8585
/** Path to the temporary file directory used by the JDK */
8686
private final Path tmpFile = PathUtils.get(System.getProperty("java.io.tmpdir"));
8787

88-
public Environment(Settings settings) {
89-
this(settings, null);
90-
}
91-
9288
public Environment(final Settings settings, final Path configPath) {
9389
final Path homeFile;
9490
if (PATH_HOME_SETTING.exists(settings)) {

core/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.common.UUIDs;
2929
import org.elasticsearch.common.settings.Settings;
3030
import org.elasticsearch.env.Environment;
31+
import org.elasticsearch.env.TestEnvironment;
3132
import org.elasticsearch.index.IndexSettings;
3233
import org.elasticsearch.index.analysis.AbstractCharFilterFactory;
3334
import org.elasticsearch.index.analysis.AbstractTokenFilterFactory;
@@ -74,7 +75,7 @@ public void setUp() throws Exception {
7475
.put("index.analysis.normalizer.my_normalizer.type", "custom")
7576
.putList("index.analysis.normalizer.my_normalizer.filter", "lowercase").build();
7677
IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings);
77-
environment = new Environment(settings);
78+
environment = TestEnvironment.newEnvironment(settings);
7879
AnalysisPlugin plugin = new AnalysisPlugin() {
7980
class MockFactory extends AbstractTokenFilterFactory {
8081
MockFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) {

core/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.nio.file.FileSystem;
25-
import java.nio.file.FileSystems;
2625
import java.nio.file.Files;
2726
import java.nio.file.Path;
2827
import java.util.ArrayList;
@@ -35,6 +34,7 @@
3534
import org.elasticsearch.cli.CommandTestCase;
3635
import org.elasticsearch.common.io.PathUtilsForTesting;
3736
import org.elasticsearch.env.Environment;
37+
import org.elasticsearch.env.TestEnvironment;
3838
import org.junit.After;
3939
import org.junit.Before;
4040

@@ -70,7 +70,7 @@ public static Environment setupEnv(boolean posix, List<FileSystem> fileSystems)
7070
PathUtilsForTesting.installMock(fs); // restored by restoreFileSystem in ESTestCase
7171
Path home = fs.getPath("/", "test-home");
7272
Files.createDirectories(home.resolve("config"));
73-
return new Environment(Settings.builder().put("path.home", home).build());
73+
return TestEnvironment.newEnvironment(Settings.builder().put("path.home", home).build());
7474
}
7575

7676
KeyStoreWrapper createKeystore(String password, String... settings) throws Exception {

core/src/test/java/org/elasticsearch/env/EnvironmentTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Environment newEnvironment(Settings settings) throws IOException {
4343
.put(settings)
4444
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath())
4545
.putList(Environment.PATH_DATA_SETTING.getKey(), tmpPaths()).build();
46-
return new Environment(build);
46+
return new Environment(build, null);
4747
}
4848

4949
public void testRepositoryResolution() throws IOException {
@@ -76,21 +76,21 @@ public void testRepositoryResolution() throws IOException {
7676
public void testPathDataWhenNotSet() {
7777
final Path pathHome = createTempDir().toAbsolutePath();
7878
final Settings settings = Settings.builder().put("path.home", pathHome).build();
79-
final Environment environment = new Environment(settings);
79+
final Environment environment = new Environment(settings, null);
8080
assertThat(environment.dataFiles(), equalTo(new Path[]{pathHome.resolve("data")}));
8181
}
8282

8383
public void testPathDataNotSetInEnvironmentIfNotSet() {
8484
final Settings settings = Settings.builder().put("path.home", createTempDir().toAbsolutePath()).build();
8585
assertFalse(Environment.PATH_DATA_SETTING.exists(settings));
86-
final Environment environment = new Environment(settings);
86+
final Environment environment = new Environment(settings, null);
8787
assertFalse(Environment.PATH_DATA_SETTING.exists(environment.settings()));
8888
}
8989

9090
public void testPathLogsWhenNotSet() {
9191
final Path pathHome = createTempDir().toAbsolutePath();
9292
final Settings settings = Settings.builder().put("path.home", pathHome).build();
93-
final Environment environment = new Environment(settings);
93+
final Environment environment = new Environment(settings, null);
9494
assertThat(environment.logsFile(), equalTo(pathHome.resolve("logs")));
9595
}
9696

@@ -111,7 +111,7 @@ public void testConfigPath() {
111111
public void testConfigPathWhenNotSet() {
112112
final Path pathHome = createTempDir().toAbsolutePath();
113113
final Settings settings = Settings.builder().put("path.home", pathHome).build();
114-
final Environment environment = new Environment(settings);
114+
final Environment environment = new Environment(settings, null);
115115
assertThat(environment.configFile(), equalTo(pathHome.resolve("config")));
116116
}
117117

core/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public void testNodeLockSingleEnvironment() throws IOException {
8080

8181
// Reuse the same location and attempt to lock again
8282
IllegalStateException ex =
83-
expectThrows(IllegalStateException.class, () -> new NodeEnvironment(settings, new Environment(settings)));
83+
expectThrows(IllegalStateException.class, () -> new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings)));
8484
assertThat(ex.getMessage(), containsString("failed to obtain node lock"));
8585

8686
// Close the environment that holds the lock and make sure we can get the lock after release
8787
env.close();
88-
env = new NodeEnvironment(settings, new Environment(settings));
88+
env = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
8989
assertThat(env.nodeDataPaths(), arrayWithSize(dataPaths.size()));
9090

9191
for (int i = 0; i < dataPaths.size(); i++) {
@@ -120,7 +120,7 @@ public void testNodeLockMultipleEnvironment() throws IOException {
120120
final Settings settings = buildEnvSettings(Settings.builder().put("node.max_local_storage_nodes", 2).build());
121121
final NodeEnvironment first = newNodeEnvironment(settings);
122122
List<String> dataPaths = Environment.PATH_DATA_SETTING.get(settings);
123-
NodeEnvironment second = new NodeEnvironment(settings, new Environment(settings));
123+
NodeEnvironment second = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
124124
assertEquals(first.nodeDataPaths().length, dataPaths.size());
125125
assertEquals(second.nodeDataPaths().length, dataPaths.size());
126126
for (int i = 0; i < dataPaths.size(); i++) {
@@ -477,7 +477,7 @@ public NodeEnvironment newNodeEnvironment() throws IOException {
477477
@Override
478478
public NodeEnvironment newNodeEnvironment(Settings settings) throws IOException {
479479
Settings build = buildEnvSettings(settings);
480-
return new NodeEnvironment(build, new Environment(build));
480+
return new NodeEnvironment(build, TestEnvironment.newEnvironment(build));
481481
}
482482

483483
public Settings buildEnvSettings(Settings settings) {
@@ -492,7 +492,7 @@ public NodeEnvironment newNodeEnvironment(String[] dataPaths, Settings settings)
492492
.put(settings)
493493
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString())
494494
.putList(Environment.PATH_DATA_SETTING.getKey(), dataPaths).build();
495-
return new NodeEnvironment(build, new Environment(build));
495+
return new NodeEnvironment(build, TestEnvironment.newEnvironment(build));
496496
}
497497

498498
public NodeEnvironment newNodeEnvironment(String[] dataPaths, String sharedDataPath, Settings settings) throws IOException {
@@ -501,6 +501,6 @@ public NodeEnvironment newNodeEnvironment(String[] dataPaths, String sharedDataP
501501
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString())
502502
.put(Environment.PATH_SHARED_DATA_SETTING.getKey(), sharedDataPath)
503503
.putList(Environment.PATH_DATA_SETTING.getKey(), dataPaths).build();
504-
return new NodeEnvironment(build, new Environment(build));
504+
return new NodeEnvironment(build, TestEnvironment.newEnvironment(build));
505505
}
506506
}

core/src/test/java/org/elasticsearch/index/IndexModuleTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.env.Environment;
4343
import org.elasticsearch.env.NodeEnvironment;
4444
import org.elasticsearch.env.ShardLock;
45+
import org.elasticsearch.env.TestEnvironment;
4546
import org.elasticsearch.index.analysis.AnalysisRegistry;
4647
import org.elasticsearch.index.cache.query.DisabledQueryCache;
4748
import org.elasticsearch.index.cache.query.IndexQueryCache;
@@ -118,7 +119,7 @@ public void setUp() throws Exception {
118119
indicesQueryCache = new IndicesQueryCache(settings);
119120
indexSettings = IndexSettingsModule.newIndexSettings("foo", settings);
120121
index = indexSettings.getIndex();
121-
environment = new Environment(settings);
122+
environment = TestEnvironment.newEnvironment(settings);
122123
emptyAnalysisRegistry = new AnalysisRegistry(environment, emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(),
123124
emptyMap(), emptyMap(), emptyMap());
124125
threadPool = new TestThreadPool("test");

core/src/test/java/org/elasticsearch/index/analysis/AnalysisRegistryTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.cluster.metadata.IndexMetaData;
3131
import org.elasticsearch.common.settings.Settings;
3232
import org.elasticsearch.env.Environment;
33+
import org.elasticsearch.env.TestEnvironment;
3334
import org.elasticsearch.index.IndexSettings;
3435
import org.elasticsearch.indices.analysis.AnalysisModule;
3536
import org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider;
@@ -56,8 +57,8 @@ private static AnalyzerProvider<?> analyzerProvider(final String name) {
5657
}
5758

5859
private static AnalysisRegistry emptyAnalysisRegistry(Settings settings) {
59-
return new AnalysisRegistry(new Environment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(),
60-
emptyMap(), emptyMap());
60+
return new AnalysisRegistry(TestEnvironment.newEnvironment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(),
61+
emptyMap(), emptyMap(), emptyMap());
6162
}
6263

6364
private static IndexSettings indexSettingsOfCurrentVersion(Settings.Builder settings) {
@@ -157,8 +158,8 @@ public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
157158
return singletonMap("mock", MockFactory::new);
158159
}
159160
};
160-
IndexAnalyzers indexAnalyzers = new AnalysisModule(new Environment(settings), singletonList(plugin)).getAnalysisRegistry()
161-
.build(idxSettings);
161+
IndexAnalyzers indexAnalyzers = new AnalysisModule(TestEnvironment.newEnvironment(settings),
162+
singletonList(plugin)).getAnalysisRegistry().build(idxSettings);
162163

163164
// This shouldn't contain English stopwords
164165
try (NamedAnalyzer custom_analyser = indexAnalyzers.get("custom_analyzer_with_camel_case")) {

core/src/test/java/org/elasticsearch/index/analysis/AnalysisTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.analysis.CharArraySet;
2323
import org.elasticsearch.common.settings.Settings;
2424
import org.elasticsearch.env.Environment;
25+
import org.elasticsearch.env.TestEnvironment;
2526
import org.elasticsearch.test.ESTestCase;
2627

2728
import java.io.BufferedWriter;
@@ -61,7 +62,7 @@ public void testParseNonExistingFile() {
6162
Settings nodeSettings = Settings.builder()
6263
.put("foo.bar_path", tempDir.resolve("foo.dict"))
6364
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir).build();
64-
Environment env = new Environment(nodeSettings);
65+
Environment env = TestEnvironment.newEnvironment(nodeSettings);
6566
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
6667
() -> Analysis.getWordList(env, nodeSettings, "foo.bar"));
6768
assertEquals("IOException while reading foo.bar_path: " + tempDir.resolve("foo.dict").toString(), ex.getMessage());
@@ -80,7 +81,7 @@ public void testParseFalseEncodedFile() throws IOException {
8081
writer.write(new byte[]{(byte) 0xff, 0x00, 0x00}); // some invalid UTF-8
8182
writer.write('\n');
8283
}
83-
Environment env = new Environment(nodeSettings);
84+
Environment env = TestEnvironment.newEnvironment(nodeSettings);
8485
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
8586
() -> Analysis.getWordList(env, nodeSettings, "foo.bar"));
8687
assertEquals("Unsupported character encoding detected while reading foo.bar_path: " + tempDir.resolve("foo.dict").toString()
@@ -101,7 +102,7 @@ public void testParseWordList() throws IOException {
101102
writer.write("world");
102103
writer.write('\n');
103104
}
104-
Environment env = new Environment(nodeSettings);
105+
Environment env = TestEnvironment.newEnvironment(nodeSettings);
105106
List<String> wordList = Analysis.getWordList(env, nodeSettings, "foo.bar");
106107
assertEquals(Arrays.asList("hello", "world"), wordList);
107108

core/src/test/java/org/elasticsearch/index/shard/NewPathForShardTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
import org.elasticsearch.env.Environment;
2828
import org.elasticsearch.env.NodeEnvironment;
2929
import org.elasticsearch.env.NodeEnvironment.NodePath;
30+
import org.elasticsearch.env.TestEnvironment;
3031
import org.elasticsearch.index.IndexSettings;
3132
import org.elasticsearch.test.ESTestCase;
3233
import org.elasticsearch.test.IndexSettingsModule;
3334
import org.junit.AfterClass;
3435
import org.junit.BeforeClass;
3536

3637
import java.io.IOException;
37-
import java.math.BigInteger;
3838
import java.nio.file.FileStore;
3939
import java.nio.file.FileSystem;
4040
import java.nio.file.Files;
@@ -178,7 +178,7 @@ public void testSelectNewPathForShard() throws Exception {
178178
Settings settings = Settings.builder()
179179
.put(Environment.PATH_HOME_SETTING.getKey(), path)
180180
.putList(Environment.PATH_DATA_SETTING.getKey(), paths).build();
181-
NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings));
181+
NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
182182

183183
// Make sure all our mocking above actually worked:
184184
NodePath[] nodePaths = nodeEnv.nodePaths();
@@ -233,7 +233,7 @@ public void testSelectNewPathForShardEvenly() throws Exception {
233233
Settings settings = Settings.builder()
234234
.put(Environment.PATH_HOME_SETTING.getKey(), path)
235235
.putList(Environment.PATH_DATA_SETTING.getKey(), paths).build();
236-
NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings));
236+
NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
237237

238238
// Make sure all our mocking above actually worked:
239239
NodePath[] nodePaths = nodeEnv.nodePaths();
@@ -290,7 +290,7 @@ public void testGettingPathWithMostFreeSpace() throws Exception {
290290
Settings settings = Settings.builder()
291291
.put(Environment.PATH_HOME_SETTING.getKey(), path)
292292
.putList(Environment.PATH_DATA_SETTING.getKey(), paths).build();
293-
NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings));
293+
NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
294294

295295
aFileStore.usableSpace = 100000;
296296
bFileStore.usableSpace = 1000;
@@ -315,7 +315,7 @@ public void testTieBreakWithMostShards() throws Exception {
315315
Settings settings = Settings.builder()
316316
.put(Environment.PATH_HOME_SETTING.getKey(), path)
317317
.putList(Environment.PATH_DATA_SETTING.getKey(), paths).build();
318-
NodeEnvironment nodeEnv = new NodeEnvironment(settings, new Environment(settings));
318+
NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
319319

320320
// Make sure all our mocking above actually worked:
321321
NodePath[] nodePaths = nodeEnv.nodePaths();

0 commit comments

Comments
 (0)