|
| 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.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.opensearch.common.io.Streams; |
| 14 | +import org.opensearch.common.remote.BlobPathParameters; |
| 15 | +import org.opensearch.common.remote.RemoteWriteableBlobEntity; |
| 16 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 17 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 18 | +import org.opensearch.core.common.io.stream.Writeable; |
| 19 | +import org.opensearch.core.compress.Compressor; |
| 20 | +import org.opensearch.index.remote.RemoteStoreUtils; |
| 21 | +import org.opensearch.repositories.blobstore.ChecksumWritableBlobStoreFormat; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | + |
| 31 | +import static org.opensearch.gateway.remote.RemoteClusterStateUtils.DELIMITER; |
| 32 | + |
| 33 | +/** |
| 34 | + * Wrapper class for uploading/downloading {@link RemotePinnedTimestamps} to/from remote blob store |
| 35 | + * |
| 36 | + * @opensearch.internal |
| 37 | + */ |
| 38 | +public class RemotePinnedTimestamps extends RemoteWriteableBlobEntity<RemotePinnedTimestamps.PinnedTimestamps> { |
| 39 | + private static final Logger logger = LogManager.getLogger(RemotePinnedTimestamps.class); |
| 40 | + |
| 41 | + /** |
| 42 | + * Represents a collection of pinned timestamps and their associated pinning entities. |
| 43 | + * This class is thread-safe and implements the Writeable interface for serialization. |
| 44 | + */ |
| 45 | + public static class PinnedTimestamps implements Writeable { |
| 46 | + private final Map<Long, List<String>> pinnedTimestampPinningEntityMap; |
| 47 | + |
| 48 | + public PinnedTimestamps(Map<Long, List<String>> pinnedTimestampPinningEntityMap) { |
| 49 | + this.pinnedTimestampPinningEntityMap = new ConcurrentHashMap<>(pinnedTimestampPinningEntityMap); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void writeTo(StreamOutput out) throws IOException { |
| 54 | + out.writeMap(pinnedTimestampPinningEntityMap, StreamOutput::writeLong, StreamOutput::writeStringCollection); |
| 55 | + } |
| 56 | + |
| 57 | + public static PinnedTimestamps readFrom(StreamInput in) throws IOException { |
| 58 | + return new PinnedTimestamps(in.readMap(StreamInput::readLong, StreamInput::readStringList)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Pins a timestamp against a pinning entity. |
| 63 | + * |
| 64 | + * @param timestamp The timestamp to pin. |
| 65 | + * @param pinningEntity The entity pinning the timestamp. |
| 66 | + */ |
| 67 | + public void pin(Long timestamp, String pinningEntity) { |
| 68 | + logger.debug("Pinning timestamp = {} against entity = {}", timestamp, pinningEntity); |
| 69 | + pinnedTimestampPinningEntityMap.computeIfAbsent(timestamp, k -> new ArrayList<>()).add(pinningEntity); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Unpins a timestamp for a specific pinning entity. |
| 74 | + * |
| 75 | + * @param timestamp The timestamp to unpin. |
| 76 | + * @param pinningEntity The entity unpinning the timestamp. |
| 77 | + */ |
| 78 | + public void unpin(Long timestamp, String pinningEntity) { |
| 79 | + logger.debug("Unpinning timestamp = {} against entity = {}", timestamp, pinningEntity); |
| 80 | + if (pinnedTimestampPinningEntityMap.containsKey(timestamp) == false |
| 81 | + || pinnedTimestampPinningEntityMap.get(timestamp).contains(pinningEntity) == false) { |
| 82 | + logger.warn("Timestamp: {} is not pinned by entity: {}", timestamp, pinningEntity); |
| 83 | + } |
| 84 | + pinnedTimestampPinningEntityMap.compute(timestamp, (k, v) -> { |
| 85 | + v.remove(pinningEntity); |
| 86 | + return v.isEmpty() ? null : v; |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + public Map<Long, List<String>> getPinnedTimestampPinningEntityMap() { |
| 91 | + return new HashMap<>(pinnedTimestampPinningEntityMap); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public static final String PINNED_TIMESTAMPS = "pinned_timestamps"; |
| 96 | + public static final ChecksumWritableBlobStoreFormat<PinnedTimestamps> PINNED_TIMESTAMPS_FORMAT = new ChecksumWritableBlobStoreFormat<>( |
| 97 | + PINNED_TIMESTAMPS, |
| 98 | + PinnedTimestamps::readFrom |
| 99 | + ); |
| 100 | + |
| 101 | + private PinnedTimestamps pinnedTimestamps; |
| 102 | + |
| 103 | + public RemotePinnedTimestamps(String clusterUUID, Compressor compressor) { |
| 104 | + super(clusterUUID, compressor); |
| 105 | + pinnedTimestamps = new PinnedTimestamps(new HashMap<>()); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public BlobPathParameters getBlobPathParameters() { |
| 110 | + return new BlobPathParameters(List.of(PINNED_TIMESTAMPS), PINNED_TIMESTAMPS); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String getType() { |
| 115 | + return PINNED_TIMESTAMPS; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String generateBlobFileName() { |
| 120 | + return this.blobFileName = String.join(DELIMITER, PINNED_TIMESTAMPS, RemoteStoreUtils.invertLong(System.currentTimeMillis())); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public InputStream serialize() throws IOException { |
| 125 | + return PINNED_TIMESTAMPS_FORMAT.serialize(pinnedTimestamps, generateBlobFileName(), getCompressor()).streamInput(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public PinnedTimestamps deserialize(InputStream inputStream) throws IOException { |
| 130 | + return PINNED_TIMESTAMPS_FORMAT.deserialize(blobName, Streams.readFully(inputStream)); |
| 131 | + } |
| 132 | + |
| 133 | + public void setBlobFileName(String blobFileName) { |
| 134 | + this.blobFileName = blobFileName; |
| 135 | + } |
| 136 | + |
| 137 | + public void setPinnedTimestamps(PinnedTimestamps pinnedTimestamps) { |
| 138 | + this.pinnedTimestamps = pinnedTimestamps; |
| 139 | + } |
| 140 | + |
| 141 | + public PinnedTimestamps getPinnedTimestamps() { |
| 142 | + return pinnedTimestamps; |
| 143 | + } |
| 144 | +} |
0 commit comments