Search in sources :

Example 6 with NodeEnvironment

use of org.opensearch.env.NodeEnvironment in project OpenSearch by opensearch-project.

the class IndexShardIT method testLockTryingToDelete.

public void testLockTryingToDelete() throws Exception {
    createIndex("test");
    ensureGreen();
    NodeEnvironment env = getInstanceFromNode(NodeEnvironment.class);
    ClusterService cs = getInstanceFromNode(ClusterService.class);
    final Index index = cs.state().metadata().index("test").getIndex();
    Path[] shardPaths = env.availableShardPaths(new ShardId(index, 0));
    logger.info("--> paths: [{}]", (Object) shardPaths);
    // Should not be able to acquire the lock because it's already open
    try {
        NodeEnvironment.acquireFSLockForPaths(IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), shardPaths);
        fail("should not have been able to acquire the lock");
    } catch (LockObtainFailedException e) {
        assertTrue("msg: " + e.getMessage(), e.getMessage().contains("unable to acquire write.lock"));
    }
    // Test without the regular shard lock to assume we can acquire it
    // (worst case, meaning that the shard lock could be acquired and
    // we're green to delete the shard's directory)
    ShardLock sLock = new DummyShardLock(new ShardId(index, 0));
    try {
        env.deleteShardDirectoryUnderLock(sLock, IndexSettingsModule.newIndexSettings("test", Settings.EMPTY));
        fail("should not have been able to delete the directory");
    } catch (LockObtainFailedException e) {
        assertTrue("msg: " + e.getMessage(), e.getMessage().contains("unable to acquire write.lock"));
    }
}
Also used : Path(java.nio.file.Path) ClusterService(org.opensearch.cluster.service.ClusterService) NodeEnvironment(org.opensearch.env.NodeEnvironment) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) Index(org.opensearch.index.Index) DummyShardLock(org.opensearch.test.DummyShardLock) ShardLock(org.opensearch.env.ShardLock) DummyShardLock(org.opensearch.test.DummyShardLock)

Example 7 with NodeEnvironment

use of org.opensearch.env.NodeEnvironment in project OpenSearch by opensearch-project.

the class IndicesStoreIntegrationIT method shardDirectory.

private Path shardDirectory(String server, Index index, int shard) {
    NodeEnvironment env = internalCluster().getInstance(NodeEnvironment.class, server);
    final Path[] paths = env.availableShardPaths(new ShardId(index, shard));
    assert paths.length == 1;
    return paths[0];
}
Also used : Path(java.nio.file.Path) ShardId(org.opensearch.index.shard.ShardId) NodeEnvironment(org.opensearch.env.NodeEnvironment)

Example 8 with NodeEnvironment

use of org.opensearch.env.NodeEnvironment in project OpenSearch by opensearch-project.

the class IndicesStoreIntegrationIT method indexDirectory.

private Path indexDirectory(String server, Index index) {
    NodeEnvironment env = internalCluster().getInstance(NodeEnvironment.class, server);
    final Path[] paths = env.indexPaths(index);
    assert paths.length == 1;
    return paths[0];
}
Also used : Path(java.nio.file.Path) NodeEnvironment(org.opensearch.env.NodeEnvironment)

Example 9 with NodeEnvironment

use of org.opensearch.env.NodeEnvironment in project OpenSearch by opensearch-project.

the class NewPathForShardTests method testTieBreakWithMostShards.

public void testTieBreakWithMostShards() throws Exception {
    Path path = PathUtils.get(createTempDir().toString());
    // Use 2 data paths:
    String[] paths = new String[] { path.resolve("a").toString(), path.resolve("b").toString() };
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), path).putList(Environment.PATH_DATA_SETTING.getKey(), paths).build();
    NodeEnvironment nodeEnv = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings));
    // Make sure all our mocking above actually worked:
    NodePath[] nodePaths = nodeEnv.nodePaths();
    assertEquals(2, nodePaths.length);
    assertEquals("mocka", nodePaths[0].fileStore.name());
    assertEquals("mockb", nodePaths[1].fileStore.name());
    // Path a has lots of free space, but b has little, so new shard should go to a:
    aFileStore.usableSpace = 100000;
    bFileStore.usableSpace = 10000;
    Map<Path, Integer> dataPathToShardCount = new HashMap<>();
    ShardId shardId = new ShardId("index", "uid1", 0);
    ShardPath result = ShardPath.selectNewPathForShard(nodeEnv, shardId, INDEX_SETTINGS, 100, dataPathToShardCount);
    createFakeShard(result);
    // First shard should go to a
    assertThat(result.getDataPath().toString(), containsString(aPathPart));
    dataPathToShardCount.compute(NodeEnvironment.shardStatePathToDataPath(result.getDataPath()), (k, v) -> v == null ? 1 : v + 1);
    shardId = new ShardId("index", "uid1", 1);
    result = ShardPath.selectNewPathForShard(nodeEnv, shardId, INDEX_SETTINGS, 100, dataPathToShardCount);
    createFakeShard(result);
    // Second shard should go to b
    assertThat(result.getDataPath().toString(), containsString(bPathPart));
    dataPathToShardCount.compute(NodeEnvironment.shardStatePathToDataPath(result.getDataPath()), (k, v) -> v == null ? 1 : v + 1);
    shardId = new ShardId("index2", "uid3", 0);
    result = ShardPath.selectNewPathForShard(nodeEnv, shardId, INDEX_SETTINGS, 100, dataPathToShardCount);
    createFakeShard(result);
    // Shard for new index should go to a
    assertThat(result.getDataPath().toString(), containsString(aPathPart));
    dataPathToShardCount.compute(NodeEnvironment.shardStatePathToDataPath(result.getDataPath()), (k, v) -> v == null ? 1 : v + 1);
    shardId = new ShardId("index2", "uid2", 0);
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index2", Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3).build());
    ShardPath result1 = ShardPath.selectNewPathForShard(nodeEnv, shardId, idxSettings, 100, dataPathToShardCount);
    createFakeShard(result1);
    dataPathToShardCount.compute(NodeEnvironment.shardStatePathToDataPath(result1.getDataPath()), (k, v) -> v == null ? 1 : v + 1);
    shardId = new ShardId("index2", "uid2", 1);
    ShardPath result2 = ShardPath.selectNewPathForShard(nodeEnv, shardId, idxSettings, 100, dataPathToShardCount);
    createFakeShard(result2);
    dataPathToShardCount.compute(NodeEnvironment.shardStatePathToDataPath(result2.getDataPath()), (k, v) -> v == null ? 1 : v + 1);
    shardId = new ShardId("index2", "uid2", 2);
    ShardPath result3 = ShardPath.selectNewPathForShard(nodeEnv, shardId, idxSettings, 100, dataPathToShardCount);
    createFakeShard(result3);
    // 2 shards go to 'b' and 1 to 'a'
    assertThat(result1.getDataPath().toString(), containsString(bPathPart));
    assertThat(result2.getDataPath().toString(), containsString(aPathPart));
    assertThat(result3.getDataPath().toString(), containsString(bPathPart));
    nodeEnv.close();
}
Also used : NodePath(org.opensearch.env.NodeEnvironment.NodePath) Path(java.nio.file.Path) NodeEnvironment(org.opensearch.env.NodeEnvironment) HashMap(java.util.HashMap) IndexSettings(org.opensearch.index.IndexSettings) Matchers.containsString(org.hamcrest.Matchers.containsString) NodePath(org.opensearch.env.NodeEnvironment.NodePath) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 10 with NodeEnvironment

use of org.opensearch.env.NodeEnvironment in project OpenSearch by opensearch-project.

the class ShardPathTests method testFailLoadShardPathIndexUUIDMissmatch.

public void testFailLoadShardPathIndexUUIDMissmatch() throws IOException {
    try (NodeEnvironment env = newNodeEnvironment(Settings.builder().build())) {
        ShardId shardId = new ShardId("foo", "foobar", 0);
        Path[] paths = env.availableShardPaths(shardId);
        Path path = randomFrom(paths);
        ShardStateMetadata.FORMAT.writeAndCleanup(new ShardStateMetadata(true, "0xDEADBEEF", AllocationId.newInitializing()), path);
        Exception e = expectThrows(IllegalStateException.class, () -> ShardPath.loadShardPath(logger, env, shardId, ""));
        assertThat(e.getMessage(), containsString("expected: foobar on shard path"));
    }
}
Also used : Path(java.nio.file.Path) NodeEnvironment(org.opensearch.env.NodeEnvironment) IOException(java.io.IOException)

Aggregations

NodeEnvironment (org.opensearch.env.NodeEnvironment)62 Settings (org.opensearch.common.settings.Settings)36 Path (java.nio.file.Path)32 Matchers.containsString (org.hamcrest.Matchers.containsString)22 ClusterState (org.opensearch.cluster.ClusterState)21 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)19 ClusterSettings (org.opensearch.common.settings.ClusterSettings)17 IndexWriter (org.apache.lucene.index.IndexWriter)16 Metadata (org.opensearch.cluster.metadata.Metadata)16 Writer (org.opensearch.gateway.PersistedClusterStateService.Writer)16 Index (org.opensearch.index.Index)13 IOException (java.io.IOException)12 FilterDirectory (org.apache.lucene.store.FilterDirectory)9 Environment (org.opensearch.env.Environment)9 ArrayList (java.util.ArrayList)7 Directory (org.apache.lucene.store.Directory)7 TestThreadPool (org.opensearch.threadpool.TestThreadPool)7 FileSystem (java.nio.file.FileSystem)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)6