Search in sources :

Example 6 with Manifest

use of org.elasticsearch.cluster.metadata.Manifest in project crate by crate.

the class MetaStateService method loadFullState.

/**
 * Loads the full state, which includes both the global state and all the indices meta data. <br>
 * When loading, manifest file is consulted (represented by {@link Manifest} class), to load proper generations. <br>
 * If there is no manifest file on disk, this method fallbacks to BWC mode, where latest generation of global and indices
 * metadata is loaded. Please note that currently there is no way to distinguish between manifest file being removed and manifest
 * file was not yet created. It means that this method always fallbacks to BWC mode, if there is no manifest file.
 *
 * @return tuple of {@link Manifest} and {@link Metadata} with global metadata and indices metadata. If there is no state on disk,
 * meta state with globalGeneration -1 and empty meta data is returned.
 * @throws IOException if some IOException when loading files occurs or there is no metadata referenced by manifest file.
 */
public Tuple<Manifest, Metadata> loadFullState() throws IOException {
    final Manifest manifest = MANIFEST_FORMAT.loadLatestState(LOGGER, namedXContentRegistry, nodeEnv.nodeDataPaths());
    if (manifest == null) {
        return loadFullStateBWC();
    }
    final Metadata.Builder metadataBuilder;
    if (manifest.isGlobalGenerationMissing()) {
        metadataBuilder = Metadata.builder();
    } else {
        final Metadata globalMetadata = META_DATA_FORMAT.loadGeneration(LOGGER, namedXContentRegistry, manifest.getGlobalGeneration(), nodeEnv.nodeDataPaths());
        if (globalMetadata != null) {
            metadataBuilder = Metadata.builder(globalMetadata);
        } else {
            throw new IOException("failed to find global metadata [generation: " + manifest.getGlobalGeneration() + "]");
        }
    }
    for (Map.Entry<Index, Long> entry : manifest.getIndexGenerations().entrySet()) {
        final Index index = entry.getKey();
        final long generation = entry.getValue();
        final String indexFolderName = index.getUUID();
        final IndexMetadata indexMetadata = INDEX_META_DATA_FORMAT.loadGeneration(LOGGER, namedXContentRegistry, generation, nodeEnv.resolveIndexFolder(indexFolderName));
        if (indexMetadata != null) {
            metadataBuilder.put(indexMetadata, false);
        } else {
            throw new IOException("failed to find metadata for existing index " + index.getName() + " [location: " + indexFolderName + ", generation: " + generation + "]");
        }
    }
    return new Tuple<>(manifest, metadataBuilder.build());
}
Also used : IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) Manifest(org.elasticsearch.cluster.metadata.Manifest) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) HashMap(java.util.HashMap) Map(java.util.Map) Tuple(io.crate.common.collections.Tuple)

Example 7 with Manifest

use of org.elasticsearch.cluster.metadata.Manifest in project crate by crate.

the class IncrementalClusterStateWriterTests method testAtomicityWithFailures.

public void testAtomicityWithFailures() throws IOException {
    try (NodeEnvironment env = newNodeEnvironment()) {
        MetaStateServiceWithFailures metaStateService = new MetaStateServiceWithFailures(randomIntBetween(100, 1000), env, xContentRegistry());
        // We only guarantee atomicity of writes, if there is initial Manifest file
        Manifest manifest = Manifest.empty();
        Metadata metadata = Metadata.EMPTY_METADATA;
        metaStateService.writeManifestAndCleanup("startup", Manifest.empty());
        long currentTerm = randomNonNegativeLong();
        long clusterStateVersion = randomNonNegativeLong();
        metaStateService.failRandomly();
        Set<Metadata> possibleMetadata = new HashSet<>();
        possibleMetadata.add(metadata);
        for (int i = 0; i < randomIntBetween(1, 5); i++) {
            IncrementalClusterStateWriter.AtomicClusterStateWriter writer = new IncrementalClusterStateWriter.AtomicClusterStateWriter(metaStateService, manifest);
            metadata = randomMetadataForTx();
            Map<Index, Long> indexGenerations = new HashMap<>();
            try {
                long globalGeneration = writer.writeGlobalState("global", metadata);
                for (IndexMetadata indexMetadata : metadata) {
                    long generation = writer.writeIndex("index", indexMetadata);
                    indexGenerations.put(indexMetadata.getIndex(), generation);
                }
                Manifest newManifest = new Manifest(currentTerm, clusterStateVersion, globalGeneration, indexGenerations);
                writer.writeManifestAndCleanup("manifest", newManifest);
                possibleMetadata.clear();
                possibleMetadata.add(metadata);
                manifest = newManifest;
            } catch (WriteStateException e) {
                if (e.isDirty()) {
                    possibleMetadata.add(metadata);
                    /*
                         * If dirty WriteStateException occurred, it's only safe to proceed if there is subsequent
                         * successful write of metadata and Manifest. We prefer to break here, not to over complicate test logic.
                         * See also MetadataStateFormat#testFailRandomlyAndReadAnyState, that does not break.
                         */
                    break;
                }
            }
        }
        metaStateService.noFailures();
        Tuple<Manifest, Metadata> manifestAndMetadata = metaStateService.loadFullState();
        Metadata loadedMetadata = manifestAndMetadata.v2();
        assertTrue(possibleMetadata.stream().anyMatch(md -> metadataEquals(md, loadedMetadata)));
    }
}
Also used : ESAllocationTestCase(org.elasticsearch.cluster.ESAllocationTestCase) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Level(org.apache.logging.log4j.Level) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) ClusterName(org.elasticsearch.cluster.ClusterName) MockLogAppender(org.elasticsearch.test.MockLogAppender) Path(java.nio.file.Path) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging) UUIDs(org.elasticsearch.common.UUIDs) Set(java.util.Set) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Manifest(org.elasticsearch.cluster.metadata.Manifest) Matchers.equalTo(org.hamcrest.Matchers.equalTo) DiscoveryNodeRole(org.elasticsearch.cluster.node.DiscoveryNodeRole) MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) Tuple(io.crate.common.collections.Tuple) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Metadata(org.elasticsearch.cluster.metadata.Metadata) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) Loggers(org.elasticsearch.common.logging.Loggers) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) XContentParser(org.elasticsearch.common.xcontent.XContentParser) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) HashMap(java.util.HashMap) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) Index(org.elasticsearch.index.Index) Manifest(org.elasticsearch.cluster.metadata.Manifest) AtomicLong(java.util.concurrent.atomic.AtomicLong) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) HashSet(java.util.HashSet)

Aggregations

Manifest (org.elasticsearch.cluster.metadata.Manifest)7 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 Metadata (org.elasticsearch.cluster.metadata.Metadata)5 Index (org.elasticsearch.index.Index)4 Tuple (io.crate.common.collections.Tuple)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ClusterState (org.elasticsearch.cluster.ClusterState)3 Map (java.util.Map)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)2 Settings (org.elasticsearch.common.settings.Settings)2 TimeValue (io.crate.common.unit.TimeValue)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1