|
| 1 | +package org.nuxeo.ecm.blob.s3v2; |
| 2 | + |
| 3 | +import org.apache.logging.log4j.LogManager; |
| 4 | +import org.apache.logging.log4j.Logger; |
| 5 | +import org.nuxeo.ecm.blob.s3.S3BlobProvider; |
| 6 | +import org.nuxeo.ecm.blob.s3.S3BlobStoreConfiguration; |
| 7 | +import org.nuxeo.ecm.core.api.Blob; |
| 8 | +import org.nuxeo.ecm.core.api.impl.blob.FileBlob; |
| 9 | +import org.nuxeo.ecm.core.blob.BlobInfo; |
| 10 | +import org.nuxeo.runtime.api.Framework; |
| 11 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 12 | +import software.amazon.awssdk.auth.credentials.AwsCredentials; |
| 13 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 14 | +import software.amazon.awssdk.regions.Region; |
| 15 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 16 | +import software.amazon.awssdk.transfer.s3.CompletedFileDownload; |
| 17 | +import software.amazon.awssdk.transfer.s3.DownloadFileRequest; |
| 18 | +import software.amazon.awssdk.transfer.s3.FileDownload; |
| 19 | +import software.amazon.awssdk.transfer.s3.S3TransferManager; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.net.URI; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.CompletableFuture; |
| 30 | +import java.util.concurrent.ExecutionException; |
| 31 | + |
| 32 | +public class S3V2BlobReader { |
| 33 | + |
| 34 | + private static final Logger log = LogManager.getLogger(S3V2BlobReader.class); |
| 35 | + |
| 36 | + private static final String NUXEO_S3STORAGE_MIN_PARTSIZE_PROPERTY = "nuxeo.s3storage.minPartSize"; |
| 37 | + private static final String NUXEO_S3STORAGE_TARGET_THROUGHPUT_PROPERTY = "nuxeo.s3storage.targetThroughput"; |
| 38 | + private static final String NUXEO_S3STORAGE_MAX_CONCURRENCY_PROPERTY = "nuxeo.s3storage.maxConcurrency"; |
| 39 | + |
| 40 | + final String bucketName; |
| 41 | + final String endpoint; |
| 42 | + final String region; |
| 43 | + final String accessKeyId; |
| 44 | + final String secretAccessKey; |
| 45 | + final Integer pathDepth; |
| 46 | + final Long minimumPartSizeInBytes; |
| 47 | + final Double targetThroughputInGbps; |
| 48 | + final Integer maxConcurrency; |
| 49 | + final Path frameworkBlobCacheDir; |
| 50 | + final S3BlobProvider frameworkBlobProvider; |
| 51 | + final S3TransferManager transferManager; |
| 52 | + |
| 53 | + /** |
| 54 | + * In the POC, this reader shares the filesystem cache with the {@link S3BlobProvider} that is contributed |
| 55 | + * by the marketplace package. This is a temporary approach until the concurrent read functionality demonstrated |
| 56 | + * by this POC is incorporated into the addon. |
| 57 | + * |
| 58 | + * @param frameworkBlobProvider so we can share the cache as described as well as other properties |
| 59 | + */ |
| 60 | + S3V2BlobReader(S3BlobProvider frameworkBlobProvider) { |
| 61 | + this.frameworkBlobProvider = frameworkBlobProvider; |
| 62 | + frameworkBlobCacheDir = frameworkBlobProvider.config.cachingConfiguration.dir; |
| 63 | + |
| 64 | + bucketName = frameworkBlobProvider.config.bucketName; |
| 65 | + endpoint = frameworkBlobProvider.config.getProperty(S3BlobStoreConfiguration.ENDPOINT_PROPERTY); |
| 66 | + region = frameworkBlobProvider.config.amazonS3.getRegionName(); |
| 67 | + accessKeyId = frameworkBlobProvider.config.getProperty(S3BlobStoreConfiguration.AWS_ID_PROPERTY); |
| 68 | + secretAccessKey = frameworkBlobProvider.config.getProperty(S3BlobStoreConfiguration.AWS_SECRET_PROPERTY); |
| 69 | + pathDepth = frameworkBlobProvider.config.getIntProperty(S3BlobStoreConfiguration.BUCKET_SUB_DIRS_DEPTH_PROPERTY); |
| 70 | + minimumPartSizeInBytes = Framework.getProperty(NUXEO_S3STORAGE_MIN_PARTSIZE_PROPERTY) == null ? null : |
| 71 | + Long.parseLong(Framework.getProperty(NUXEO_S3STORAGE_MIN_PARTSIZE_PROPERTY)); |
| 72 | + targetThroughputInGbps = Framework.getProperty(NUXEO_S3STORAGE_TARGET_THROUGHPUT_PROPERTY) == null ? null : |
| 73 | + Double.parseDouble(Framework.getProperty(NUXEO_S3STORAGE_TARGET_THROUGHPUT_PROPERTY)); |
| 74 | + maxConcurrency = Framework.getProperty(NUXEO_S3STORAGE_MAX_CONCURRENCY_PROPERTY) == null ? null : |
| 75 | + Integer.parseInt(Framework.getProperty(NUXEO_S3STORAGE_MAX_CONCURRENCY_PROPERTY)); |
| 76 | + |
| 77 | + AwsCredentials awsCreds = AwsBasicCredentials.create(accessKeyId, secretAccessKey); |
| 78 | + |
| 79 | + transferManager = S3TransferManager.builder().s3ClientConfiguration(cfg -> cfg |
| 80 | + .credentialsProvider(StaticCredentialsProvider.create(awsCreds)) |
| 81 | + .region(Region.of(region)) |
| 82 | + .endpointOverride(endpoint == null ? null : URI.create(endpoint)) |
| 83 | + .minimumPartSizeInBytes(minimumPartSizeInBytes) |
| 84 | + .targetThroughputInGbps(targetThroughputInGbps) |
| 85 | + .maxConcurrency(maxConcurrency)) |
| 86 | + .build(); |
| 87 | + |
| 88 | + log.info("Blob reader initialized using framework blob provider: " + frameworkBlobProvider |
| 89 | + .getClass() |
| 90 | + .getSimpleName()); |
| 91 | + } |
| 92 | + |
| 93 | + Map<BlobInfo, Blob> readBlobs(List<BlobInfo> blobInfos) { |
| 94 | + Map<BlobInfo, CompletableFuture<CompletedFileDownload>> futureMap = readBlobsInternal(blobInfos); |
| 95 | + Map<BlobInfo, Blob> blobMap = new HashMap<>(); |
| 96 | + for (Map.Entry<BlobInfo, CompletableFuture<CompletedFileDownload>> entry : futureMap.entrySet()) { |
| 97 | + try { |
| 98 | + entry.getValue().get(); |
| 99 | + } catch (ExecutionException | InterruptedException e) { |
| 100 | + if (!(e.getMessage().contains("FileAlreadyExistsException"))) { |
| 101 | + throw new RuntimeException(e); |
| 102 | + } |
| 103 | + } |
| 104 | + blobMap.put(entry.getKey(), new FileBlob(new File(frameworkBlobCacheDir.toAbsolutePath().toString(), |
| 105 | + entry.getKey().digest))); |
| 106 | + } |
| 107 | + // add blobs that were already cached so the blobs returned by the method matches the input |
| 108 | + blobInfos.forEach(blobInfo -> { |
| 109 | + if (blobMap.get(blobInfo.digest) == null) { |
| 110 | + blobMap.put(blobInfo, |
| 111 | + new FileBlob(new File(frameworkBlobCacheDir.toAbsolutePath().toString(), blobInfo.digest))); |
| 112 | + } |
| 113 | + }); |
| 114 | + return blobMap; |
| 115 | + } |
| 116 | + |
| 117 | + void close() { |
| 118 | + try { |
| 119 | + transferManager.close(); |
| 120 | + } catch (Exception e) { |
| 121 | + log.info("Exception attempting to close the transfer manager", e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Uses the AWS SDK V2 (Preview) Transfer Manager to download blobs concurrently. |
| 127 | + * |
| 128 | + * @param blobInfos a list of BlobInfos representing the blobs to download. |
| 129 | + * @return a Map in which the key is a blob info, and the value is a future to wait on. When the |
| 130 | + * future completes, the blob is downloaded. Only the blobs downloaded by the method are |
| 131 | + * returned: i.e. if the blob was already present in cache, it is excluded from the return value. |
| 132 | + */ |
| 133 | + private Map<BlobInfo, CompletableFuture<CompletedFileDownload>> readBlobsInternal(List<BlobInfo> blobInfos) { |
| 134 | + Map<BlobInfo, CompletableFuture<CompletedFileDownload>> futureMap = new HashMap<>(); |
| 135 | + blobInfos.forEach(blobInfo -> { |
| 136 | + Path blobFile = Paths.get(frameworkBlobCacheDir.toAbsolutePath().toString(), blobInfo.digest); |
| 137 | + // only enqueue a download if blob is not already cached, and haven't already enqueued a download |
| 138 | + if (!Files.exists(blobFile) && futureMap.get(blobInfo.digest) == null) { |
| 139 | + GetObjectRequest getObjectRequest = GetObjectRequest.builder() |
| 140 | + .key(toPathedKey(blobInfo.digest)) |
| 141 | + .bucket(bucketName) |
| 142 | + .build(); |
| 143 | + DownloadFileRequest downloadFileRequest = DownloadFileRequest.builder() |
| 144 | + .destination(blobFile) |
| 145 | + .getObjectRequest(getObjectRequest) |
| 146 | + .build(); |
| 147 | + FileDownload download = transferManager.downloadFile(downloadFileRequest); |
| 148 | + futureMap.put(blobInfo, download.completionFuture()); |
| 149 | + } |
| 150 | + }); |
| 151 | + return futureMap; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Given <code>digest</code> "123456" and {@link #pathDepth} == 2, then returns |
| 156 | + * "12/34/123456". |
| 157 | + * |
| 158 | + * @param digest an MD5 digest |
| 159 | + * @return the digest with path segments optionally prepended |
| 160 | + */ |
| 161 | + private String toPathedKey(String digest) { |
| 162 | + if (pathDepth == 0) { |
| 163 | + return digest; |
| 164 | + } |
| 165 | + StringBuilder builder = new StringBuilder(); |
| 166 | + for (int i = 0; i < pathDepth; i++) { |
| 167 | + int idx = i * 2; |
| 168 | + builder.append(digest, idx, idx + 2).append("/"); |
| 169 | + } |
| 170 | + return builder.append(digest).toString(); |
| 171 | + } |
| 172 | +} |
0 commit comments