Search in sources :

Example 21 with NodeEnvironment

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

the class GatewayMetaStatePersistedStateTests method testStatePersistedOnLoad.

public void testStatePersistedOnLoad() throws IOException {
    // open LucenePersistedState to make sure that cluster state is written out to each data path
    final PersistedClusterStateService persistedClusterStateService = new PersistedClusterStateService(nodeEnvironment, xContentRegistry(), getBigArrays(), new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), () -> 0L);
    final ClusterState state = createClusterState(randomNonNegativeLong(), Metadata.builder().clusterUUID(randomAlphaOfLength(10)).build());
    try (GatewayMetaState.LucenePersistedState ignored = new GatewayMetaState.LucenePersistedState(persistedClusterStateService, 42L, state)) {
    }
    nodeEnvironment.close();
    // verify that the freshest state was rewritten to each data path
    for (Path path : nodeEnvironment.nodeDataPaths()) {
        Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath()).put(Environment.PATH_DATA_SETTING.getKey(), path.getParent().getParent().toString()).build();
        try (NodeEnvironment nodeEnvironment = new NodeEnvironment(settings, TestEnvironment.newEnvironment(settings))) {
            final PersistedClusterStateService newPersistedClusterStateService = new PersistedClusterStateService(nodeEnvironment, xContentRegistry(), getBigArrays(), new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), () -> 0L);
            final PersistedClusterStateService.OnDiskState onDiskState = newPersistedClusterStateService.loadBestOnDiskState();
            assertFalse(onDiskState.empty());
            assertThat(onDiskState.currentTerm, equalTo(42L));
            assertClusterStateEqual(state, ClusterState.builder(ClusterName.DEFAULT).version(onDiskState.lastAcceptedVersion).metadata(onDiskState.metadata).build());
        }
    }
}
Also used : Path(java.nio.file.Path) ClusterState(org.opensearch.cluster.ClusterState) ClusterSettings(org.opensearch.common.settings.ClusterSettings) NodeEnvironment(org.opensearch.env.NodeEnvironment) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 22 with NodeEnvironment

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

the class DanglingIndicesStateTests method testClusterStateListenerNotRegisterWhenSettingDisabled.

/**
 * Check that when auto-imports are disabled, then no change listener is registered with the cluster state.
 */
public void testClusterStateListenerNotRegisterWhenSettingDisabled() throws Exception {
    try (NodeEnvironment env = newNodeEnvironment()) {
        MetaStateService metaStateService = new MetaStateService(env, xContentRegistry());
        LocalAllocateDangledIndices localAllocateDangledIndices = mock(LocalAllocateDangledIndices.class);
        final Settings allocateSettings = Settings.builder().put(AUTO_IMPORT_DANGLING_INDICES_SETTING.getKey(), false).build();
        final ClusterService clusterServiceMock = mock(ClusterService.class);
        when(clusterServiceMock.getSettings()).thenReturn(allocateSettings);
        new DanglingIndicesState(env, metaStateService, localAllocateDangledIndices, clusterServiceMock);
        verify(clusterServiceMock, never()).addListener(any());
    }
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) NodeEnvironment(org.opensearch.env.NodeEnvironment) Settings(org.opensearch.common.settings.Settings)

Example 23 with NodeEnvironment

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

the class DanglingIndicesStateTests method testInvalidIndexFolder.

public void testInvalidIndexFolder() throws Exception {
    try (NodeEnvironment env = newNodeEnvironment()) {
        MetaStateService metaStateService = new MetaStateService(env, xContentRegistry());
        DanglingIndicesState danglingState = createDanglingIndicesState(env, metaStateService);
        Metadata metadata = Metadata.builder().build();
        final String uuid = "test1UUID";
        final Settings.Builder settings = Settings.builder().put(indexSettings).put(IndexMetadata.SETTING_INDEX_UUID, uuid);
        IndexMetadata dangledIndex = IndexMetadata.builder("test1").settings(settings).build();
        metaStateService.writeIndex("test_write", dangledIndex);
        for (Path path : env.resolveIndexFolder(uuid)) {
            if (Files.exists(path)) {
                Files.move(path, path.resolveSibling("invalidUUID"), StandardCopyOption.ATOMIC_MOVE);
            }
        }
        try {
            danglingState.findNewDanglingIndices(emptyMap(), metadata);
            fail("no exception thrown for invalid folder name");
        } catch (IllegalStateException e) {
            assertThat(e.getMessage(), equalTo("[invalidUUID] invalid index folder name, rename to [test1UUID]"));
        }
    }
}
Also used : Path(java.nio.file.Path) NodeEnvironment(org.opensearch.env.NodeEnvironment) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings)

Example 24 with NodeEnvironment

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

the class DanglingIndicesStateTests method testDanglingIndicesDiscovery.

public void testDanglingIndicesDiscovery() throws Exception {
    try (NodeEnvironment env = newNodeEnvironment()) {
        MetaStateService metaStateService = new MetaStateService(env, xContentRegistry());
        DanglingIndicesState danglingState = createDanglingIndicesState(env, metaStateService);
        assertTrue(danglingState.getDanglingIndices().isEmpty());
        Metadata metadata = Metadata.builder().build();
        final Settings.Builder settings = Settings.builder().put(indexSettings).put(IndexMetadata.SETTING_INDEX_UUID, "test1UUID");
        IndexMetadata dangledIndex = IndexMetadata.builder("test1").settings(settings).build();
        metaStateService.writeIndex("test_write", dangledIndex);
        Map<Index, IndexMetadata> newDanglingIndices = danglingState.findNewDanglingIndices(emptyMap(), metadata);
        assertTrue(newDanglingIndices.containsKey(dangledIndex.getIndex()));
        metadata = Metadata.builder().put(dangledIndex, false).build();
        newDanglingIndices = danglingState.findNewDanglingIndices(emptyMap(), metadata);
        assertFalse(newDanglingIndices.containsKey(dangledIndex.getIndex()));
    }
}
Also used : NodeEnvironment(org.opensearch.env.NodeEnvironment) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) Index(org.opensearch.index.Index) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings)

Example 25 with NodeEnvironment

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

the class DanglingIndicesStateTests method testDanglingProcessing.

public void testDanglingProcessing() throws Exception {
    try (NodeEnvironment env = newNodeEnvironment()) {
        MetaStateService metaStateService = new MetaStateService(env, xContentRegistry());
        DanglingIndicesState danglingState = createDanglingIndicesState(env, metaStateService);
        Metadata metadata = Metadata.builder().build();
        final Settings.Builder settings = Settings.builder().put(indexSettings).put(IndexMetadata.SETTING_INDEX_UUID, "test1UUID");
        IndexMetadata dangledIndex = IndexMetadata.builder("test1").settings(settings).build();
        metaStateService.writeIndex("test_write", dangledIndex);
        // check that several runs when not in the metadata still keep the dangled index around
        int numberOfChecks = randomIntBetween(1, 10);
        for (int i = 0; i < numberOfChecks; i++) {
            Map<Index, IndexMetadata> newDanglingIndices = danglingState.findNewDanglingIndices(emptyMap(), metadata);
            assertThat(newDanglingIndices.size(), equalTo(1));
            assertThat(newDanglingIndices.keySet(), Matchers.hasItems(dangledIndex.getIndex()));
            assertTrue(danglingState.getDanglingIndices().isEmpty());
        }
        for (int i = 0; i < numberOfChecks; i++) {
            danglingState.findNewAndAddDanglingIndices(metadata);
            assertThat(danglingState.getDanglingIndices().size(), equalTo(1));
            assertThat(danglingState.getDanglingIndices().keySet(), Matchers.hasItems(dangledIndex.getIndex()));
        }
        // simulate allocation to the metadata
        metadata = Metadata.builder(metadata).put(dangledIndex, true).build();
        // check that several runs when in the metadata, but not cleaned yet, still keeps dangled
        for (int i = 0; i < numberOfChecks; i++) {
            Map<Index, IndexMetadata> newDanglingIndices = danglingState.findNewDanglingIndices(emptyMap(), metadata);
            assertTrue(newDanglingIndices.isEmpty());
            assertThat(danglingState.getDanglingIndices().size(), equalTo(1));
            assertThat(danglingState.getDanglingIndices().keySet(), Matchers.hasItems(dangledIndex.getIndex()));
        }
        danglingState.cleanupAllocatedDangledIndices(metadata);
        assertTrue(danglingState.getDanglingIndices().isEmpty());
    }
}
Also used : NodeEnvironment(org.opensearch.env.NodeEnvironment) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) Index(org.opensearch.index.Index) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings)

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