Search in sources :

Example 11 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer 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)

Example 12 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testFailsOnMismatchedNodeIds.

public void testFailsOnMismatchedNodeIds() throws IOException {
    final Path[] dataPaths1 = createDataPaths();
    final Path[] dataPaths2 = createDataPaths();
    final String[] nodeIds = new String[2];
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(dataPaths1)) {
        nodeIds[0] = nodeEnvironment.nodeId();
        try (Writer writer = newPersistedClusterStateService(nodeEnvironment).createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(newPersistedClusterStateService(nodeEnvironment));
            writer.writeFullStateAndCommit(0L, ClusterState.builder(clusterState).version(randomLongBetween(1L, Long.MAX_VALUE)).build());
        }
    }
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(dataPaths2)) {
        nodeIds[1] = nodeEnvironment.nodeId();
        try (Writer writer = newPersistedClusterStateService(nodeEnvironment).createWriter()) {
            final ClusterState clusterState = loadPersistedClusterState(newPersistedClusterStateService(nodeEnvironment));
            writer.writeFullStateAndCommit(0L, ClusterState.builder(clusterState).version(randomLongBetween(1L, Long.MAX_VALUE)).build());
        }
    }
    for (Path dataPath : dataPaths2) {
        IOUtils.rm(dataPath.resolve(MetadataStateFormat.STATE_DIR_NAME));
    }
    final Path[] combinedPaths = Stream.concat(Arrays.stream(dataPaths1), Arrays.stream(dataPaths2)).toArray(Path[]::new);
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(combinedPaths)) {
        final String message = expectThrows(IllegalStateException.class, () -> newPersistedClusterStateService(nodeEnvironment).loadBestOnDiskState()).getMessage();
        assertThat(message, allOf(containsString("unexpected node ID in metadata"), containsString(nodeIds[0])));
        assertTrue("[" + message + "] should match " + Arrays.toString(dataPaths2), Arrays.stream(dataPaths2).anyMatch(p -> message.contains(p.toString())));
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) BigArrays(org.elasticsearch.common.util.BigArrays) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Environment(org.elasticsearch.env.Environment) Term(org.apache.lucene.index.Term) Level(org.apache.logging.log4j.Level) CoordinationMetadata(org.elasticsearch.cluster.coordination.CoordinationMetadata) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) Directory(org.apache.lucene.store.Directory) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ClusterName(org.elasticsearch.cluster.ClusterName) MockLogAppender(org.elasticsearch.test.MockLogAppender) IOContext(org.apache.lucene.store.IOContext) Path(java.nio.file.Path) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging) Matchers.allOf(org.hamcrest.Matchers.allOf) Collection(java.util.Collection) UUIDs(org.elasticsearch.common.UUIDs) MockBigArrays(org.elasticsearch.common.util.MockBigArrays) Collectors(java.util.stream.Collectors) MockPageCacheRecycler(org.elasticsearch.common.util.MockPageCacheRecycler) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Matchers.containsString(org.hamcrest.Matchers.containsString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Index(org.elasticsearch.index.Index) ArrayList(java.util.ArrayList) Metadata(org.elasticsearch.cluster.metadata.Metadata) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) Matchers.lessThan(org.hamcrest.Matchers.lessThan) ESTestCase(org.elasticsearch.test.ESTestCase) IndexOutput(org.apache.lucene.store.IndexOutput) Loggers(org.elasticsearch.common.logging.Loggers) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) IOUtils(io.crate.common.io.IOUtils) IOException(java.io.IOException) IOError(java.io.IOError) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) FilterDirectory(org.apache.lucene.store.FilterDirectory) LogManager(org.apache.logging.log4j.LogManager) ClusterState(org.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter)

Example 13 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

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(), BigArrays.NON_RECYCLING_INSTANCE, 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.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) FilterDirectory(org.apache.lucene.store.FilterDirectory) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter)

Example 14 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testReloadsMetadataAcrossMultipleSegments.

public void testReloadsMetadataAcrossMultipleSegments() throws IOException {
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(createDataPaths())) {
        final PersistedClusterStateService persistedClusterStateService = newPersistedClusterStateService(nodeEnvironment);
        final int writes = between(5, 20);
        final List<Index> indices = new ArrayList<>(writes);
        try (Writer writer = persistedClusterStateService.createWriter()) {
            for (int i = 0; i < writes; i++) {
                final Index index = new Index("test-" + i, UUIDs.randomBase64UUID(random()));
                indices.add(index);
                final ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
                writeState(writer, 0L, ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).version(i + 2).put(IndexMetadata.builder(index.getName()).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, index.getUUID())))).incrementVersion().build(), clusterState);
            }
        }
        final ClusterState clusterState = loadPersistedClusterState(persistedClusterStateService);
        for (Index index : indices) {
            final IndexMetadata indexMetadata = clusterState.metadata().index(index.getName());
            assertThat(indexMetadata.getIndexUUID(), equalTo(index.getUUID()));
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter)

Example 15 with Writer

use of org.elasticsearch.gateway.PersistedClusterStateService.Writer in project crate by crate.

the class PersistedClusterStateServiceTests method testPersistsAndReloadsTerm.

public void testPersistsAndReloadsTerm() throws IOException {
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment(createDataPaths())) {
        final PersistedClusterStateService persistedClusterStateService = newPersistedClusterStateService(nodeEnvironment);
        final long newTerm = randomNonNegativeLong();
        assertThat(persistedClusterStateService.loadBestOnDiskState().currentTerm, equalTo(0L));
        try (Writer writer = persistedClusterStateService.createWriter()) {
            writer.writeFullStateAndCommit(newTerm, ClusterState.EMPTY_STATE);
            assertThat(persistedClusterStateService.loadBestOnDiskState().currentTerm, equalTo(newTerm));
        }
        assertThat(persistedClusterStateService.loadBestOnDiskState().currentTerm, equalTo(newTerm));
    }
}
Also used : NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Writer(org.elasticsearch.gateway.PersistedClusterStateService.Writer) IndexWriter(org.apache.lucene.index.IndexWriter)

Aggregations

IndexWriter (org.apache.lucene.index.IndexWriter)16 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)16 Writer (org.elasticsearch.gateway.PersistedClusterStateService.Writer)16 ClusterState (org.elasticsearch.cluster.ClusterState)15 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Path (java.nio.file.Path)10 FilterDirectory (org.apache.lucene.store.FilterDirectory)9 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)6 Directory (org.apache.lucene.store.Directory)6 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)6 IOException (java.io.IOException)5 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Term (org.apache.lucene.index.Term)4 IOContext (org.apache.lucene.store.IOContext)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4