|
| 1 | +package io.temporal.payload.storage.s3driver.awssdkv2; |
| 2 | + |
| 3 | +import io.temporal.common.Experimental; |
| 4 | +import io.temporal.payload.storage.s3driver.S3StorageDriverClient; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.concurrent.CompletionException; |
| 10 | +import javax.annotation.Nonnull; |
| 11 | +import software.amazon.awssdk.core.ResponseBytes; |
| 12 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 13 | +import software.amazon.awssdk.core.async.AsyncResponseTransformer; |
| 14 | +import software.amazon.awssdk.regions.Region; |
| 15 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 16 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 17 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 18 | +import software.amazon.awssdk.services.s3.model.HeadObjectRequest; |
| 19 | +import software.amazon.awssdk.services.s3.model.HeadObjectResponse; |
| 20 | +import software.amazon.awssdk.services.s3.model.NoSuchKeyException; |
| 21 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 22 | +import software.amazon.awssdk.services.s3.model.PutObjectResponse; |
| 23 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
| 24 | + |
| 25 | +/** |
| 26 | + * {@link S3StorageDriverClient} backed by the AWS SDK for Java v2 {@link S3AsyncClient}. The |
| 27 | + * wrapped client must be configured with credentials and a region by the caller. |
| 28 | + */ |
| 29 | +@Experimental |
| 30 | +public final class S3AsyncClientAdapter implements S3StorageDriverClient { |
| 31 | + private final S3AsyncClient client; |
| 32 | + |
| 33 | + public S3AsyncClientAdapter(@Nonnull S3AsyncClient client) { |
| 34 | + this.client = Objects.requireNonNull(client, "client"); |
| 35 | + } |
| 36 | + |
| 37 | + @Nonnull |
| 38 | + @Override |
| 39 | + public CompletableFuture<Void> putObject( |
| 40 | + @Nonnull String bucket, @Nonnull String key, @Nonnull byte[] data) { |
| 41 | + CompletableFuture<PutObjectResponse> request = |
| 42 | + client.putObject( |
| 43 | + PutObjectRequest.builder().bucket(bucket).key(key).build(), |
| 44 | + AsyncRequestBody.fromBytesUnsafe(data)); // avoids a defensive copy |
| 45 | + return abortRequestOnCancel(request, request.thenApply(response -> (Void) null)); |
| 46 | + } |
| 47 | + |
| 48 | + @Nonnull |
| 49 | + @Override |
| 50 | + public CompletableFuture<Boolean> objectExists(@Nonnull String bucket, @Nonnull String key) { |
| 51 | + CompletableFuture<HeadObjectResponse> request = |
| 52 | + client.headObject(HeadObjectRequest.builder().bucket(bucket).key(key).build()); |
| 53 | + return abortRequestOnCancel( |
| 54 | + request, |
| 55 | + request.handle( |
| 56 | + (response, ex) -> { |
| 57 | + if (ex == null) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + Throwable cause = |
| 61 | + (ex instanceof CompletionException && ex.getCause() != null) ? ex.getCause() : ex; |
| 62 | + if (cause instanceof NoSuchKeyException) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + if (cause instanceof S3Exception && ((S3Exception) cause).statusCode() == 404) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + if (cause instanceof RuntimeException) { |
| 69 | + throw (RuntimeException) cause; |
| 70 | + } |
| 71 | + throw new RuntimeException(cause); |
| 72 | + })); |
| 73 | + } |
| 74 | + |
| 75 | + @Nonnull |
| 76 | + @Override |
| 77 | + public CompletableFuture<byte[]> getObject(@Nonnull String bucket, @Nonnull String key) { |
| 78 | + CompletableFuture<ResponseBytes<GetObjectResponse>> request = |
| 79 | + client.getObject( |
| 80 | + GetObjectRequest.builder().bucket(bucket).key(key).build(), |
| 81 | + AsyncResponseTransformer.toBytes()); |
| 82 | + return abortRequestOnCancel(request, request.thenApply(ResponseBytes::asByteArrayUnsafe)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns {@code result}, wired so that cancelling it cancels the underlying {@code request}. The |
| 87 | + * AWS SDK aborts an async request when the future it returns is cancelled. Cancellation does not |
| 88 | + * otherwise propagate across the {@code thenApply}/{@code handle} boundary. |
| 89 | + */ |
| 90 | + private static <T> CompletableFuture<T> abortRequestOnCancel( |
| 91 | + CompletableFuture<?> request, CompletableFuture<T> result) { |
| 92 | + result.whenComplete( |
| 93 | + (value, ex) -> { |
| 94 | + if (result.isCancelled()) { |
| 95 | + request.cancel(true); |
| 96 | + } |
| 97 | + }); |
| 98 | + return result; |
| 99 | + } |
| 100 | + |
| 101 | + @Nonnull |
| 102 | + @Override |
| 103 | + public Map<String, String> describe() { |
| 104 | + Region region = client.serviceClientConfiguration().region(); |
| 105 | + if (region == null) { |
| 106 | + return Collections.emptyMap(); |
| 107 | + } |
| 108 | + return Collections.singletonMap("client_region", region.id()); |
| 109 | + } |
| 110 | +} |
0 commit comments