Search in sources :

Example 61 with NodeEnvironment

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

the class PersistedClusterStateServiceTests method testFailsIfGlobalMetadataIsDuplicated.

public void testFailsIfGlobalMetadataIsDuplicated() throws IOException {
    // if someone attempted surgery on the metadata index by hand, e.g. deleting broken segments, then maybe the global metadata
    // is duplicated
    final Path[] dataPaths1 = createDataPaths();
    final Path[] dataPaths2 = createDataPaths();
    final Path[] combinedPaths = Stream.concat(Arrays.stream(dataPaths1), Arrays.stream(dataPaths2)).toArray(Path[]::new);
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(combinedPaths)) {
        try (Writer writer = newPersistedClusterStateService(nodeEnvironment).createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(newPersistedClusterStateService(nodeEnvironment));
            writeState(writer, 0L, ClusterState.builder(clusterState).version(randomLongBetween(1L, Long.MAX_VALUE)).build(), clusterState);
        }
        final Path brokenPath = randomFrom(nodeEnvironment.nodeDataPaths());
        final Path dupPath = randomValueOtherThan(brokenPath, () -> randomFrom(nodeEnvironment.nodeDataPaths()));
        try (Directory directory = new NIOFSDirectory(brokenPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME));
            Directory dupDirectory = new NIOFSDirectory(dupPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME))) {
            try (IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig())) {
                indexWriter.addIndexes(dupDirectory);
                indexWriter.commit();
            }
        }
        final String message = expectThrows(IllegalStateException.class, () -> newPersistedClusterStateService(nodeEnvironment).loadBestOnDiskState()).getMessage();
        assertThat(message, allOf(containsString("duplicate global metadata found"), containsString(brokenPath.toString())));
    }
}
Also used : Path(java.nio.file.Path) ClusterState(org.opensearch.cluster.ClusterState) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) NodeEnvironment(org.opensearch.env.NodeEnvironment) IndexWriter(org.apache.lucene.index.IndexWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexWriter(org.apache.lucene.index.IndexWriter) Writer(org.opensearch.gateway.PersistedClusterStateService.Writer) Directory(org.apache.lucene.store.Directory) FilterDirectory(org.apache.lucene.store.FilterDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 62 with NodeEnvironment

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

the class PersistedClusterStateServiceTests method testCrashesWithIOErrorOnCommitFailure.

public void testCrashesWithIOErrorOnCommitFailure() throws IOException {
    final AtomicBoolean throwException = new AtomicBoolean();
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(createDataPaths())) {
        final PersistedClusterStateService persistedClusterStateService = new PersistedClusterStateService(nodeEnvironment, xContentRegistry(), getBigArrays(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), () -> 0L) {

            @Override
            Directory createDirectory(Path path) throws IOException {
                return new FilterDirectory(super.createDirectory(path)) {

                    @Override
                    public void rename(String source, String dest) throws IOException {
                        if (throwException.get() && dest.startsWith("segments")) {
                            throw new IOException("simulated");
                        }
                    }
                };
            }
        };
        try (Writer writer = persistedClusterStateService.createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
            final long newTerm = randomNonNegativeLong();
            final ClusterState newState = ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).clusterUUID(UUIDs.randomBase64UUID(random())).clusterUUIDCommitted(true).version(randomLongBetween(1L, Long.MAX_VALUE))).incrementVersion().build();
            throwException.set(true);
            assertThat(expectThrows(IOError.class, () -> {
                if (randomBoolean()) {
                    writeState(writer, newTerm, newState, clusterState);
                } else {
                    writer.commit(newTerm, newState.version());
                }
            }).getMessage(), containsString("simulated"));
            assertFalse(writer.isOpen());
        }
        // check if we can open writer again
        try (Writer ignored = persistedClusterStateService.createWriter()) {
        }
    }
}
Also used : Path(java.nio.file.Path) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.opensearch.cluster.ClusterState) ClusterSettings(org.opensearch.common.settings.ClusterSettings) NodeEnvironment(org.opensearch.env.NodeEnvironment) FilterDirectory(org.apache.lucene.store.FilterDirectory) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) IndexWriter(org.apache.lucene.index.IndexWriter) Writer(org.opensearch.gateway.PersistedClusterStateService.Writer)

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