|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.gateway.remote.model; |
| 10 | + |
| 11 | +import org.opensearch.common.io.Streams; |
| 12 | +import org.opensearch.common.remote.AbstractRemoteWritableBlobEntity; |
| 13 | +import org.opensearch.common.remote.BlobPathParameters; |
| 14 | +import org.opensearch.core.compress.Compressor; |
| 15 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 16 | +import org.opensearch.gateway.remote.ClusterMetadataManifest; |
| 17 | +import org.opensearch.gateway.remote.ClusterMetadataManifest.UploadedMetadata; |
| 18 | +import org.opensearch.gateway.remote.ClusterMetadataManifest.UploadedMetadataAttribute; |
| 19 | +import org.opensearch.gateway.remote.RemoteClusterStateUtils; |
| 20 | +import org.opensearch.index.remote.RemoteStoreUtils; |
| 21 | +import org.opensearch.repositories.blobstore.ChecksumBlobStoreFormat; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static org.opensearch.gateway.remote.RemoteClusterStateUtils.DELIMITER; |
| 28 | + |
| 29 | +/** |
| 30 | + * Wrapper class for uploading/downloading {@link ClusterMetadataManifest} to/from remote blob store |
| 31 | + */ |
| 32 | +public class RemoteClusterMetadataManifest extends AbstractRemoteWritableBlobEntity<ClusterMetadataManifest> { |
| 33 | + |
| 34 | + public static final String MANIFEST = "manifest"; |
| 35 | + public static final int SPLITTED_MANIFEST_FILE_LENGTH = 6; |
| 36 | + |
| 37 | + public static final String METADATA_MANIFEST_NAME_FORMAT = "%s"; |
| 38 | + public static final int MANIFEST_CURRENT_CODEC_VERSION = ClusterMetadataManifest.CODEC_V3; |
| 39 | + public static final String COMMITTED = "C"; |
| 40 | + public static final String PUBLISHED = "P"; |
| 41 | + |
| 42 | + /** |
| 43 | + * Manifest format compatible with older codec v0, where codec version was missing. |
| 44 | + */ |
| 45 | + public static final ChecksumBlobStoreFormat<ClusterMetadataManifest> CLUSTER_METADATA_MANIFEST_FORMAT_V0 = |
| 46 | + new ChecksumBlobStoreFormat<>("cluster-metadata-manifest", METADATA_MANIFEST_NAME_FORMAT, ClusterMetadataManifest::fromXContentV0); |
| 47 | + /** |
| 48 | + * Manifest format compatible with older codec v1, where global metadata was missing. |
| 49 | + */ |
| 50 | + public static final ChecksumBlobStoreFormat<ClusterMetadataManifest> CLUSTER_METADATA_MANIFEST_FORMAT_V1 = |
| 51 | + new ChecksumBlobStoreFormat<>("cluster-metadata-manifest", METADATA_MANIFEST_NAME_FORMAT, ClusterMetadataManifest::fromXContentV1); |
| 52 | + |
| 53 | + /** |
| 54 | + * Manifest format compatible with codec v2, where we introduced codec versions/global metadata. |
| 55 | + */ |
| 56 | + public static final ChecksumBlobStoreFormat<ClusterMetadataManifest> CLUSTER_METADATA_MANIFEST_FORMAT = new ChecksumBlobStoreFormat<>( |
| 57 | + "cluster-metadata-manifest", |
| 58 | + METADATA_MANIFEST_NAME_FORMAT, |
| 59 | + ClusterMetadataManifest::fromXContent |
| 60 | + ); |
| 61 | + |
| 62 | + private ClusterMetadataManifest clusterMetadataManifest; |
| 63 | + |
| 64 | + public RemoteClusterMetadataManifest( |
| 65 | + final ClusterMetadataManifest clusterMetadataManifest, |
| 66 | + final String clusterUUID, |
| 67 | + final Compressor compressor, |
| 68 | + final NamedXContentRegistry namedXContentRegistry |
| 69 | + ) { |
| 70 | + super(clusterUUID, compressor, namedXContentRegistry); |
| 71 | + this.clusterMetadataManifest = clusterMetadataManifest; |
| 72 | + } |
| 73 | + |
| 74 | + public RemoteClusterMetadataManifest( |
| 75 | + final String blobName, |
| 76 | + final String clusterUUID, |
| 77 | + final Compressor compressor, |
| 78 | + final NamedXContentRegistry namedXContentRegistry |
| 79 | + ) { |
| 80 | + super(clusterUUID, compressor, namedXContentRegistry); |
| 81 | + this.blobName = blobName; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public BlobPathParameters getBlobPathParameters() { |
| 86 | + return new BlobPathParameters(List.of(MANIFEST), MANIFEST); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String generateBlobFileName() { |
| 91 | + // 123456789012_test-cluster/cluster-state/dsgYj10Nkso7/manifest/manifest__<inverted_term>__<inverted_version>__C/P__<inverted__timestamp>__ |
| 92 | + // <codec_version> |
| 93 | + String blobFileName = String.join( |
| 94 | + DELIMITER, |
| 95 | + MANIFEST, |
| 96 | + RemoteStoreUtils.invertLong(clusterMetadataManifest.getClusterTerm()), |
| 97 | + RemoteStoreUtils.invertLong(clusterMetadataManifest.getStateVersion()), |
| 98 | + (clusterMetadataManifest.isCommitted() ? COMMITTED : PUBLISHED), |
| 99 | + RemoteStoreUtils.invertLong(System.currentTimeMillis()), |
| 100 | + String.valueOf(clusterMetadataManifest.getCodecVersion()) |
| 101 | + // Keep the codec version at last place only, during we read last place to determine codec version. |
| 102 | + ); |
| 103 | + this.blobFileName = blobFileName; |
| 104 | + return blobFileName; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public UploadedMetadata getUploadedMetadata() { |
| 109 | + assert blobName != null; |
| 110 | + return new UploadedMetadataAttribute(MANIFEST, blobName); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public InputStream serialize() throws IOException { |
| 115 | + return CLUSTER_METADATA_MANIFEST_FORMAT.serialize( |
| 116 | + clusterMetadataManifest, |
| 117 | + generateBlobFileName(), |
| 118 | + getCompressor(), |
| 119 | + RemoteClusterStateUtils.FORMAT_PARAMS |
| 120 | + ).streamInput(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public ClusterMetadataManifest deserialize(final InputStream inputStream) throws IOException { |
| 125 | + ChecksumBlobStoreFormat<ClusterMetadataManifest> blobStoreFormat = getClusterMetadataManifestBlobStoreFormat(); |
| 126 | + return blobStoreFormat.deserialize(blobName, getNamedXContentRegistry(), Streams.readFully(inputStream)); |
| 127 | + } |
| 128 | + |
| 129 | + private int getManifestCodecVersion() { |
| 130 | + assert blobName != null; |
| 131 | + String[] splitName = blobName.split(DELIMITER); |
| 132 | + if (splitName.length == SPLITTED_MANIFEST_FILE_LENGTH) { |
| 133 | + return Integer.parseInt(splitName[splitName.length - 1]); // Last value would be codec version. |
| 134 | + } else if (splitName.length < SPLITTED_MANIFEST_FILE_LENGTH) { // Where codec is not part of file name, i.e. default codec version 0 |
| 135 | + // is used. |
| 136 | + return ClusterMetadataManifest.CODEC_V0; |
| 137 | + } else { |
| 138 | + throw new IllegalArgumentException("Manifest file name is corrupted"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private ChecksumBlobStoreFormat<ClusterMetadataManifest> getClusterMetadataManifestBlobStoreFormat() { |
| 143 | + long codecVersion = getManifestCodecVersion(); |
| 144 | + if (codecVersion == MANIFEST_CURRENT_CODEC_VERSION) { |
| 145 | + return CLUSTER_METADATA_MANIFEST_FORMAT; |
| 146 | + } else if (codecVersion == ClusterMetadataManifest.CODEC_V1) { |
| 147 | + return CLUSTER_METADATA_MANIFEST_FORMAT_V1; |
| 148 | + } else if (codecVersion == ClusterMetadataManifest.CODEC_V0) { |
| 149 | + return CLUSTER_METADATA_MANIFEST_FORMAT_V0; |
| 150 | + } |
| 151 | + throw new IllegalArgumentException("Cluster metadata manifest file is corrupted, don't have valid codec version"); |
| 152 | + } |
| 153 | +} |
0 commit comments