|
|
@@ -21,7 +21,6 @@ package org.elasticsearch.env;
|
|
|
import org.apache.lucene.store.LockObtainFailedException;
|
|
|
import org.apache.lucene.util.IOUtils;
|
|
|
import org.elasticsearch.ElasticsearchIllegalStateException;
|
|
|
-import org.elasticsearch.cluster.metadata.IndexMetaData;
|
|
|
import org.elasticsearch.common.settings.ImmutableSettings;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.index.Index;
|
|
|
@@ -39,13 +38,8 @@ import java.util.Set;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
-import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
|
|
|
-import static org.hamcrest.CoreMatchers.equalTo;
|
|
|
-
|
|
|
public class NodeEnvironmentTests extends ElasticsearchTestCase {
|
|
|
|
|
|
- private final Settings settings = ImmutableSettings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).build();
|
|
|
-
|
|
|
@Test
|
|
|
public void testNodeLockSingleEnvironment() throws IOException {
|
|
|
NodeEnvironment env = newNodeEnvironment(ImmutableSettings.builder()
|
|
|
@@ -99,7 +93,7 @@ public class NodeEnvironmentTests extends ElasticsearchTestCase {
|
|
|
} catch (LockObtainFailedException ex) {
|
|
|
// expected
|
|
|
}
|
|
|
- for (Path path : env.indexPaths(new Index("foo"), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo"))) {
|
|
|
Files.createDirectories(path.resolve("1"));
|
|
|
Files.createDirectories(path.resolve("2"));
|
|
|
}
|
|
|
@@ -132,7 +126,7 @@ public class NodeEnvironmentTests extends ElasticsearchTestCase {
|
|
|
final NodeEnvironment env = newNodeEnvironment();
|
|
|
final int numIndices = randomIntBetween(1, 10);
|
|
|
for (int i = 0; i < numIndices; i++) {
|
|
|
- for (Path path : env.indexPaths(new Index("foo" + i), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo" + i))) {
|
|
|
Files.createDirectories(path);
|
|
|
}
|
|
|
}
|
|
|
@@ -152,47 +146,46 @@ public class NodeEnvironmentTests extends ElasticsearchTestCase {
|
|
|
assertEquals(new ShardId("foo", 1), fooLock.getShardId());
|
|
|
|
|
|
|
|
|
- for (Path path : env.indexPaths(new Index("foo"), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo"))) {
|
|
|
Files.createDirectories(path.resolve("1"));
|
|
|
Files.createDirectories(path.resolve("2"));
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- env.deleteShardDirectorySafe(new ShardId("foo", 1), settings);
|
|
|
+ env.deleteShardDirectorySafe(new ShardId("foo", 1));
|
|
|
fail("shard is locked");
|
|
|
} catch (LockObtainFailedException ex) {
|
|
|
// expected
|
|
|
}
|
|
|
|
|
|
- for (Path path : env.indexPaths(new Index("foo"), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo"))) {
|
|
|
assertTrue(Files.exists(path.resolve("1")));
|
|
|
assertTrue(Files.exists(path.resolve("2")));
|
|
|
|
|
|
}
|
|
|
|
|
|
- env.deleteShardDirectorySafe(new ShardId("foo", 2), settings);
|
|
|
+ env.deleteShardDirectorySafe(new ShardId("foo", 2));
|
|
|
|
|
|
- for (Path path : env.indexPaths(new Index("foo"), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo"))) {
|
|
|
assertTrue(Files.exists(path.resolve("1")));
|
|
|
assertFalse(Files.exists(path.resolve("2")));
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
-
|
|
|
- env.deleteIndexDirectorySafe(new Index("foo"), settings);
|
|
|
+ env.deleteIndexDirectorySafe(new Index("foo"));
|
|
|
fail("shard is locked");
|
|
|
} catch (LockObtainFailedException ex) {
|
|
|
// expected
|
|
|
}
|
|
|
fooLock.close();
|
|
|
|
|
|
- for (Path path : env.indexPaths(new Index("foo"), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo"))) {
|
|
|
assertTrue(Files.exists(path));
|
|
|
}
|
|
|
|
|
|
- env.deleteIndexDirectorySafe(new Index("foo"), settings);
|
|
|
+ env.deleteIndexDirectorySafe(new Index("foo"));
|
|
|
|
|
|
- for (Path path : env.indexPaths(new Index("foo"), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo"))) {
|
|
|
assertFalse(Files.exists(path));
|
|
|
}
|
|
|
assertTrue("LockedShards: " + env.lockedShards(), env.lockedShards().isEmpty());
|
|
|
@@ -205,7 +198,7 @@ public class NodeEnvironmentTests extends ElasticsearchTestCase {
|
|
|
final int numIndices = randomIntBetween(1, 10);
|
|
|
final Set<ShardId> createdShards = new HashSet<>();
|
|
|
for (int i = 0; i < numIndices; i++) {
|
|
|
- for (Path path : env.indexPaths(new Index("foo" + i), settings)) {
|
|
|
+ for (Path path : env.indexPaths(new Index("foo" + i))) {
|
|
|
final int numShards = randomIntBetween(1, 10);
|
|
|
for (int j = 0; j < numShards; j++) {
|
|
|
Files.createDirectories(path.resolve(Integer.toString(j)));
|
|
|
@@ -290,104 +283,4 @@ public class NodeEnvironmentTests extends ElasticsearchTestCase {
|
|
|
}
|
|
|
env.close();
|
|
|
}
|
|
|
-
|
|
|
- @Test
|
|
|
- public void testCustomDataPaths() throws Exception {
|
|
|
- String[] dataPaths = tmpPaths();
|
|
|
- NodeEnvironment env = newNodeEnvironment(dataPaths, ImmutableSettings.EMPTY);
|
|
|
-
|
|
|
- Settings s1 = ImmutableSettings.EMPTY;
|
|
|
- Settings s2 = ImmutableSettings.builder().put(IndexMetaData.SETTING_DATA_PATH, "/tmp/foo").build();
|
|
|
- ShardId sid = new ShardId("myindex", 0);
|
|
|
- Index i = new Index("myindex");
|
|
|
-
|
|
|
- assertFalse("no settings should mean no custom data path", NodeEnvironment.hasCustomDataPath(s1));
|
|
|
- assertTrue("settings with path_data should have a custom data path", NodeEnvironment.hasCustomDataPath(s2));
|
|
|
-
|
|
|
- assertThat(env.shardDataPaths(sid, s1), equalTo(env.shardPaths(sid, s1)));
|
|
|
- assertThat(env.shardDataPaths(sid, s2), equalTo(new Path[] {Paths.get("/tmp/foo/0/myindex/0")}));
|
|
|
-
|
|
|
- assertThat("shard paths with a custom data_path should contain regular paths and custom path",
|
|
|
- env.shardPaths(sid, s2),
|
|
|
- equalTo(addPaths(stringsToPaths(dataPaths, "elasticsearch/nodes/0/indices/myindex/0"),
|
|
|
- new String[] {"/tmp/foo/0/myindex/0"})));
|
|
|
-
|
|
|
- assertThat("index paths with no custom settings uses the regular template",
|
|
|
- env.indexPaths(i, s1), equalTo(stringsToPaths(dataPaths, "elasticsearch/nodes/0/indices/myindex")));
|
|
|
- assertThat("index paths with custom data_path setting is the same as the data_path",
|
|
|
- env.indexPaths(i, s2),
|
|
|
- equalTo(addPaths(stringsToPaths(dataPaths, "elasticsearch/nodes/0/indices/myindex"),
|
|
|
- new String[] {"/tmp/foo/0"})));
|
|
|
-
|
|
|
- env.close();
|
|
|
- NodeEnvironment env2 = newNodeEnvironment(dataPaths,
|
|
|
- ImmutableSettings.builder().put(NodeEnvironment.ADD_NODE_ID_TO_CUSTOM_PATH, false).build());
|
|
|
-
|
|
|
- assertThat(env2.shardDataPaths(sid, s1), equalTo(env2.shardPaths(sid, s1)));
|
|
|
- assertThat(env2.shardDataPaths(sid, s2), equalTo(new Path[] {Paths.get("/tmp/foo/myindex/0")}));
|
|
|
-
|
|
|
- assertThat("shard paths with a custom data_path should contain regular paths and custom path",
|
|
|
- env2.shardPaths(sid, s2),
|
|
|
- equalTo(addPaths(stringsToPaths(dataPaths, "elasticsearch/nodes/0/indices/myindex/0"),
|
|
|
- new String[] {"/tmp/foo/myindex/0"})));
|
|
|
-
|
|
|
- assertThat("index paths with no custom settings uses the regular template",
|
|
|
- env2.indexPaths(i, s1), equalTo(stringsToPaths(dataPaths, "elasticsearch/nodes/0/indices/myindex")));
|
|
|
- assertThat("index paths with custom data_path setting is the same as the data_path",
|
|
|
- env2.indexPaths(i, s2),
|
|
|
- equalTo(addPaths(stringsToPaths(dataPaths, "elasticsearch/nodes/0/indices/myindex"),
|
|
|
- new String[] {"/tmp/foo/"})));
|
|
|
- }
|
|
|
-
|
|
|
- /** Converts an array of Strings to an array of Paths, adding an additional child if specified */
|
|
|
- private Path[] stringsToPaths(String[] strings, String additional) {
|
|
|
- Path[] locations = new Path[strings.length];
|
|
|
- for (int i = 0; i < strings.length; i++) {
|
|
|
- locations[i] = Paths.get(strings[i], additional);
|
|
|
- }
|
|
|
- return locations;
|
|
|
- }
|
|
|
-
|
|
|
- /** Adds the {@code pathsToAdd} string array to the given paths list and returns it */
|
|
|
- private Path[] addPaths(Path[] paths, String[] pathsToAdd) {
|
|
|
- Path[] locations = new Path[paths.length + pathsToAdd.length];
|
|
|
- for (int i = 0; i < paths.length; i++) {
|
|
|
- locations[i] = paths[i];
|
|
|
- }
|
|
|
- for (int i = paths.length; i < (paths.length + pathsToAdd.length); i++) {
|
|
|
- locations[i] = Paths.get(pathsToAdd[i - paths.length]);
|
|
|
- }
|
|
|
- return locations;
|
|
|
- }
|
|
|
-
|
|
|
- public String[] tmpPaths() {
|
|
|
- final int numPaths = randomIntBetween(1, 3);
|
|
|
- final String[] absPaths = new String[numPaths];
|
|
|
- for (int i = 0; i < numPaths; i++) {
|
|
|
- absPaths[i] = newTempDirPath().toAbsolutePath().toString();
|
|
|
- }
|
|
|
- return absPaths;
|
|
|
- }
|
|
|
-
|
|
|
- public NodeEnvironment newNodeEnvironment() throws IOException {
|
|
|
- return newNodeEnvironment(ImmutableSettings.EMPTY);
|
|
|
- }
|
|
|
-
|
|
|
- public NodeEnvironment newNodeEnvironment(Settings settings) throws IOException {
|
|
|
- Settings build = ImmutableSettings.builder()
|
|
|
- .put(settings)
|
|
|
- .put("path.home", newTempDirPath().toAbsolutePath().toString())
|
|
|
- .put(NodeEnvironment.SETTING_CUSTOM_DATA_PATH_ENABLED, true)
|
|
|
- .putArray("path.data", tmpPaths()).build();
|
|
|
- return new NodeEnvironment(build, new Environment(build));
|
|
|
- }
|
|
|
-
|
|
|
- public NodeEnvironment newNodeEnvironment(String[] dataPaths, Settings settings) throws IOException {
|
|
|
- Settings build = ImmutableSettings.builder()
|
|
|
- .put(settings)
|
|
|
- .put("path.home", newTempDirPath().toAbsolutePath().toString())
|
|
|
- .put(NodeEnvironment.SETTING_CUSTOM_DATA_PATH_ENABLED, true)
|
|
|
- .putArray("path.data", dataPaths).build();
|
|
|
- return new NodeEnvironment(build, new Environment(build));
|
|
|
- }
|
|
|
}
|