Search in sources :

Example 36 with SimpleFSDirectory

use of org.apache.lucene.store.SimpleFSDirectory in project crate by crate.

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 SimpleFSDirectory(brokenPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME));
            Directory dupDirectory = new SimpleFSDirectory(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.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) IndexWriter(org.apache.lucene.index.IndexWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter) Directory(org.apache.lucene.store.Directory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 37 with SimpleFSDirectory

use of org.apache.lucene.store.SimpleFSDirectory in project crate by crate.

the class PersistedClusterStateServiceTests method testFailsIfIndexMetadataIsDuplicated.

public void testFailsIfIndexMetadataIsDuplicated() throws IOException {
    // if someone attempted surgery on the metadata index by hand, e.g. deleting broken segments, then maybe some index 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)) {
        final String indexUUID = UUIDs.randomBase64UUID(random());
        final String indexName = randomAlphaOfLength(10);
        try (Writer writer = newPersistedClusterStateService(nodeEnvironment).createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(newPersistedClusterStateService(nodeEnvironment));
            writeState(writer, 0L, ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).version(1L).coordinationMetadata(CoordinationMetadata.builder(clusterState.coordinationMetadata()).term(1L).build()).put(IndexMetadata.builder(indexName).version(1L).settings(Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1).put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0).put(IndexMetadata.SETTING_INDEX_VERSION_CREATED.getKey(), Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, indexUUID)))).incrementVersion().build(), clusterState);
        }
        final Path brokenPath = randomFrom(nodeEnvironment.nodeDataPaths());
        final Path dupPath = randomValueOtherThan(brokenPath, () -> randomFrom(nodeEnvironment.nodeDataPaths()));
        try (Directory directory = new SimpleFSDirectory(brokenPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME));
            Directory dupDirectory = new SimpleFSDirectory(dupPath.resolve(PersistedClusterStateService.METADATA_DIRECTORY_NAME))) {
            try (IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig())) {
                // do not duplicate global metadata
                indexWriter.deleteDocuments(new Term("type", "global"));
                indexWriter.addIndexes(dupDirectory);
                indexWriter.commit();
            }
        }
        final String message = expectThrows(IllegalStateException.class, () -> newPersistedClusterStateService(nodeEnvironment).loadBestOnDiskState()).getMessage();
        assertThat(message, allOf(containsString("duplicate metadata found"), containsString(brokenPath.toString()), containsString(indexName), containsString(indexUUID)));
    }
}
Also used : Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) IndexWriter(org.apache.lucene.index.IndexWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter) Directory(org.apache.lucene.store.Directory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) FilterDirectory(org.apache.lucene.store.FilterDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)37 Directory (org.apache.lucene.store.Directory)23 Path (java.nio.file.Path)15 IOException (java.io.IOException)13 File (java.io.File)9 IndexWriter (org.apache.lucene.index.IndexWriter)9 FSDirectory (org.apache.lucene.store.FSDirectory)7 Settings (org.elasticsearch.common.settings.Settings)7 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)6 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)5 IndexSearcher (org.apache.lucene.search.IndexSearcher)5 FilterDirectory (org.apache.lucene.store.FilterDirectory)5 IndexInput (org.apache.lucene.store.IndexInput)5 InputStream (java.io.InputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4 Dictionary (org.apache.lucene.analysis.hunspell.Dictionary)4 IndexReader (org.apache.lucene.index.IndexReader)4 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)4 MMapDirectory (org.apache.lucene.store.MMapDirectory)4 NIOFSDirectory (org.apache.lucene.store.NIOFSDirectory)4