|
| 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.blobstore.BlobPath; |
| 12 | +import org.opensearch.common.remote.AbstractRemoteWritableBlobEntity; |
| 13 | +import org.opensearch.common.remote.RemoteWritableEntityStore; |
| 14 | +import org.opensearch.common.remote.RemoteWriteableEntity; |
| 15 | +import org.opensearch.core.action.ActionListener; |
| 16 | +import org.opensearch.gateway.remote.RemoteClusterStateUtils; |
| 17 | +import org.opensearch.index.translog.transfer.BlobStoreTransferService; |
| 18 | +import org.opensearch.repositories.blobstore.BlobStoreRepository; |
| 19 | +import org.opensearch.threadpool.ThreadPool; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.util.concurrent.ExecutorService; |
| 24 | + |
| 25 | +/** |
| 26 | + * Abstract class for a blob type storage |
| 27 | + * |
| 28 | + * @param <T> The entity which can be uploaded to / downloaded from blob store |
| 29 | + * @param <U> The concrete class implementing {@link RemoteWriteableEntity} which is used as a wrapper for T entity. |
| 30 | + */ |
| 31 | +public class RemoteClusterStateBlobStore<T, U extends AbstractRemoteWritableBlobEntity<T>> implements RemoteWritableEntityStore<T, U> { |
| 32 | + |
| 33 | + private final BlobStoreTransferService transferService; |
| 34 | + private final BlobStoreRepository blobStoreRepository; |
| 35 | + private final String clusterName; |
| 36 | + private final ExecutorService executorService; |
| 37 | + |
| 38 | + public RemoteClusterStateBlobStore( |
| 39 | + final BlobStoreTransferService blobStoreTransferService, |
| 40 | + final BlobStoreRepository blobStoreRepository, |
| 41 | + final String clusterName, |
| 42 | + final ThreadPool threadPool, |
| 43 | + final String executor |
| 44 | + ) { |
| 45 | + this.transferService = blobStoreTransferService; |
| 46 | + this.blobStoreRepository = blobStoreRepository; |
| 47 | + this.clusterName = clusterName; |
| 48 | + this.executorService = threadPool.executor(executor); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void writeAsync(final U entity, final ActionListener<Void> listener) { |
| 53 | + try { |
| 54 | + try (InputStream inputStream = entity.serialize()) { |
| 55 | + BlobPath blobPath = getBlobPathForUpload(entity); |
| 56 | + entity.setFullBlobName(blobPath); |
| 57 | + // TODO uncomment below logic after merging PR https://github.com/opensearch-project/OpenSearch/pull/13836 |
| 58 | + // transferService.uploadBlob(inputStream, getBlobPathForUpload(entity), entity.getBlobFileName(), WritePriority.URGENT, |
| 59 | + // listener); |
| 60 | + } |
| 61 | + } catch (Exception e) { |
| 62 | + listener.onFailure(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public T read(final U entity) throws IOException { |
| 67 | + // TODO Add timing logs and tracing |
| 68 | + assert entity.getFullBlobName() != null; |
| 69 | + return entity.deserialize(transferService.downloadBlob(getBlobPathForDownload(entity), entity.getBlobFileName())); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void readAsync(final U entity, final ActionListener<T> listener) { |
| 74 | + executorService.execute(() -> { |
| 75 | + try { |
| 76 | + listener.onResponse(read(entity)); |
| 77 | + } catch (Exception e) { |
| 78 | + listener.onFailure(e); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private BlobPath getBlobPathForUpload(final AbstractRemoteWritableBlobEntity<T> obj) { |
| 84 | + BlobPath blobPath = blobStoreRepository.basePath() |
| 85 | + .add(RemoteClusterStateUtils.encodeString(clusterName)) |
| 86 | + .add("cluster-state") |
| 87 | + .add(obj.clusterUUID()); |
| 88 | + for (String token : obj.getBlobPathParameters().getPathTokens()) { |
| 89 | + blobPath = blobPath.add(token); |
| 90 | + } |
| 91 | + return blobPath; |
| 92 | + } |
| 93 | + |
| 94 | + private BlobPath getBlobPathForDownload(final AbstractRemoteWritableBlobEntity<T> obj) { |
| 95 | + String[] pathTokens = obj.getBlobPathTokens(); |
| 96 | + BlobPath blobPath = new BlobPath(); |
| 97 | + if (pathTokens == null || pathTokens.length < 1) { |
| 98 | + return blobPath; |
| 99 | + } |
| 100 | + // Iterate till second last path token to get the blob folder |
| 101 | + for (int i = 0; i < pathTokens.length - 1; i++) { |
| 102 | + blobPath = blobPath.add(pathTokens[i]); |
| 103 | + } |
| 104 | + return blobPath; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments