Search in sources :

Example 1 with InMemoryPersistedState

use of org.opensearch.cluster.coordination.InMemoryPersistedState in project OpenSearch by opensearch-project.

the class GatewayMetaState method start.

public void start(Settings settings, TransportService transportService, ClusterService clusterService, MetaStateService metaStateService, MetadataIndexUpgradeService metadataIndexUpgradeService, MetadataUpgrader metadataUpgrader, PersistedClusterStateService persistedClusterStateService) {
    assert persistedState.get() == null : "should only start once, but already have " + persistedState.get();
    if (DiscoveryNode.isMasterNode(settings) || DiscoveryNode.isDataNode(settings)) {
        try {
            final PersistedClusterStateService.OnDiskState onDiskState = persistedClusterStateService.loadBestOnDiskState();
            Metadata metadata = onDiskState.metadata;
            long lastAcceptedVersion = onDiskState.lastAcceptedVersion;
            long currentTerm = onDiskState.currentTerm;
            if (onDiskState.empty()) {
                assert Version.CURRENT.major <= LegacyESVersion.V_7_0_0.major + 1 : "legacy metadata loader is not needed anymore from v9 onwards";
                final Tuple<Manifest, Metadata> legacyState = metaStateService.loadFullState();
                if (legacyState.v1().isEmpty() == false) {
                    metadata = legacyState.v2();
                    lastAcceptedVersion = legacyState.v1().getClusterStateVersion();
                    currentTerm = legacyState.v1().getCurrentTerm();
                }
            }
            PersistedState persistedState = null;
            boolean success = false;
            try {
                final ClusterState clusterState = prepareInitialClusterState(transportService, clusterService, ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.get(settings)).version(lastAcceptedVersion).metadata(upgradeMetadataForNode(metadata, metadataIndexUpgradeService, metadataUpgrader)).build());
                if (DiscoveryNode.isMasterNode(settings)) {
                    persistedState = new LucenePersistedState(persistedClusterStateService, currentTerm, clusterState);
                } else {
                    persistedState = new AsyncLucenePersistedState(settings, transportService.getThreadPool(), new LucenePersistedState(persistedClusterStateService, currentTerm, clusterState));
                }
                if (DiscoveryNode.isDataNode(settings)) {
                    // unreference legacy files (only keep them for dangling indices functionality)
                    metaStateService.unreferenceAll();
                } else {
                    // delete legacy files
                    metaStateService.deleteAll();
                }
                // write legacy node metadata to prevent accidental downgrades from spawning empty cluster state
                NodeMetadata.FORMAT.writeAndCleanup(new NodeMetadata(persistedClusterStateService.getNodeId(), Version.CURRENT), persistedClusterStateService.getDataPaths());
                success = true;
            } finally {
                if (success == false) {
                    IOUtils.closeWhileHandlingException(persistedState);
                }
            }
            this.persistedState.set(persistedState);
        } catch (IOException e) {
            throw new OpenSearchException("failed to load metadata", e);
        }
    } else {
        final long currentTerm = 0L;
        final ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.get(settings)).build();
        if (persistedClusterStateService.getDataPaths().length > 0) {
            // cluster uuid as coordinating-only nodes do not snap into a cluster as they carry no state
            try (PersistedClusterStateService.Writer persistenceWriter = persistedClusterStateService.createWriter()) {
                persistenceWriter.writeFullStateAndCommit(currentTerm, clusterState);
            } catch (IOException e) {
                throw new OpenSearchException("failed to load metadata", e);
            }
            try {
                // delete legacy cluster state files
                metaStateService.deleteAll();
                // write legacy node metadata to prevent downgrades from spawning empty cluster state
                NodeMetadata.FORMAT.writeAndCleanup(new NodeMetadata(persistedClusterStateService.getNodeId(), Version.CURRENT), persistedClusterStateService.getDataPaths());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
        persistedState.set(new InMemoryPersistedState(currentTerm, clusterState));
    }
}
Also used : PersistedState(org.opensearch.cluster.coordination.CoordinationState.PersistedState) InMemoryPersistedState(org.opensearch.cluster.coordination.InMemoryPersistedState) ClusterState(org.opensearch.cluster.ClusterState) Metadata(org.opensearch.cluster.metadata.Metadata) NodeMetadata(org.opensearch.env.NodeMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Manifest(org.opensearch.cluster.metadata.Manifest) NodeMetadata(org.opensearch.env.NodeMetadata) InMemoryPersistedState(org.opensearch.cluster.coordination.InMemoryPersistedState) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 OpenSearchException (org.opensearch.OpenSearchException)1 ClusterState (org.opensearch.cluster.ClusterState)1 CoordinationMetadata (org.opensearch.cluster.coordination.CoordinationMetadata)1 PersistedState (org.opensearch.cluster.coordination.CoordinationState.PersistedState)1 InMemoryPersistedState (org.opensearch.cluster.coordination.InMemoryPersistedState)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)1 Manifest (org.opensearch.cluster.metadata.Manifest)1 Metadata (org.opensearch.cluster.metadata.Metadata)1 NodeMetadata (org.opensearch.env.NodeMetadata)1