|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.services.s3.internal.crossregion;
|
17 | 17 |
|
| 18 | +import static software.amazon.awssdk.services.s3.internal.crossregion.commons.CrossRegionUtils.getBucketRegionFromException; |
| 19 | +import static software.amazon.awssdk.services.s3.internal.crossregion.commons.CrossRegionUtils.isS3RedirectException; |
| 20 | +import static software.amazon.awssdk.services.s3.internal.crossregion.commons.CrossRegionUtils.requestWithDecoratedEndpointProvider; |
| 21 | + |
| 22 | +import java.util.Map; |
18 | 23 | import java.util.Optional;
|
19 | 24 | import java.util.concurrent.CompletableFuture;
|
| 25 | +import java.util.concurrent.ConcurrentHashMap; |
20 | 26 | import java.util.function.Function;
|
| 27 | +import java.util.function.Supplier; |
21 | 28 | import software.amazon.awssdk.annotations.SdkInternalApi;
|
22 |
| -import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration; |
| 29 | +import software.amazon.awssdk.regions.Region; |
23 | 30 | import software.amazon.awssdk.services.s3.DelegatingS3AsyncClient;
|
24 | 31 | import software.amazon.awssdk.services.s3.S3AsyncClient;
|
25 |
| -import software.amazon.awssdk.services.s3.endpoints.S3EndpointProvider; |
26 |
| -import software.amazon.awssdk.services.s3.internal.crossregion.endpointprovider.BucketEndpointProvider; |
| 32 | +import software.amazon.awssdk.services.s3.model.HeadBucketRequest; |
| 33 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
27 | 34 | import software.amazon.awssdk.services.s3.model.S3Request;
|
| 35 | +import software.amazon.awssdk.utils.CompletableFutureUtils; |
| 36 | +import software.amazon.awssdk.utils.StringUtils; |
28 | 37 |
|
29 | 38 | @SdkInternalApi
|
30 | 39 | public final class S3CrossRegionAsyncClient extends DelegatingS3AsyncClient {
|
| 40 | + |
| 41 | + private final Map<String, CompletableFuture<Region>> bucketToRegionCache = new ConcurrentHashMap<>(); |
| 42 | + |
31 | 43 | public S3CrossRegionAsyncClient(S3AsyncClient s3Client) {
|
32 | 44 | super(s3Client);
|
33 | 45 | }
|
34 | 46 |
|
35 | 47 | @Override
|
36 |
| - protected <T extends S3Request, ReturnT> CompletableFuture<ReturnT> |
37 |
| - invokeOperation(T request, Function<T, CompletableFuture<ReturnT>> operation) { |
| 48 | + protected <T extends S3Request, ReturnT> CompletableFuture<ReturnT> invokeOperation( |
| 49 | + T request, Function<T, CompletableFuture<ReturnT>> operation) { |
38 | 50 |
|
39 | 51 | Optional<String> bucket = request.getValueForField("Bucket", String.class);
|
40 | 52 |
|
41 | 53 | if (!bucket.isPresent()) {
|
42 | 54 | return operation.apply(request);
|
43 | 55 | }
|
| 56 | + String bucketName = bucket.get(); |
44 | 57 |
|
45 |
| - return operation.apply(requestWithDecoratedEndpointProvider(request, bucket.get())) |
46 |
| - .whenComplete((r, t) -> handleOperationFailure(t, bucket.get())); |
| 58 | + if (bucketToRegionCache.containsKey(bucketName)) { |
| 59 | + return operation.apply(requestWithDecoratedEndpointProvider(request, |
| 60 | + regionSupplier(bucketName), |
| 61 | + serviceClientConfiguration().endpointProvider().get())); |
| 62 | + } |
| 63 | + return operation.apply(request).thenApply(CompletableFuture::completedFuture) |
| 64 | + .exceptionally(exception -> { |
| 65 | + if (isS3RedirectException(exception.getCause())) { |
| 66 | + bucketToRegionCache.remove(bucketName); |
| 67 | + getBucketRegionFromException((S3Exception) exception.getCause()) |
| 68 | + .ifPresent( |
| 69 | + region -> bucketToRegionCache.put(bucketName, |
| 70 | + CompletableFuture.completedFuture(Region.of(region)))); |
| 71 | + return operation.apply( |
| 72 | + requestWithDecoratedEndpointProvider(request, |
| 73 | + regionSupplier(bucketName), |
| 74 | + serviceClientConfiguration().endpointProvider().get())); |
| 75 | + } |
| 76 | + return CompletableFutureUtils.failedFuture(exception); |
| 77 | + }).thenCompose(Function.identity()); |
47 | 78 | }
|
48 | 79 |
|
49 |
| - private void handleOperationFailure(Throwable t, String bucket) { |
50 |
| - //TODO: handle failure case |
51 |
| - } |
52 | 80 |
|
53 |
| - //Cannot avoid unchecked cast without upstream changes to supply builder function |
54 |
| - @SuppressWarnings("unchecked") |
55 |
| - private <T extends S3Request> T requestWithDecoratedEndpointProvider(T request, String bucket) { |
56 |
| - return (T) request.toBuilder() |
57 |
| - .overrideConfiguration(getOrCreateConfigWithEndpointProvider(request, bucket)) |
58 |
| - .build(); |
| 81 | + private Supplier<Region> regionSupplier(String bucket) { |
| 82 | + return () -> bucketToRegionCache.computeIfAbsent(bucket, this::regionCompletableFuture).join(); |
59 | 83 | }
|
60 | 84 |
|
61 |
| - //TODO: optimize shared sync/async code |
62 |
| - private AwsRequestOverrideConfiguration getOrCreateConfigWithEndpointProvider(S3Request request, String bucket) { |
63 |
| - AwsRequestOverrideConfiguration requestOverrideConfig = |
64 |
| - request.overrideConfiguration().orElseGet(() -> AwsRequestOverrideConfiguration.builder().build()); |
65 |
| - |
66 |
| - S3EndpointProvider delegateEndpointProvider = (S3EndpointProvider) |
67 |
| - requestOverrideConfig.endpointProvider().orElseGet(() -> serviceClientConfiguration().endpointProvider().get()); |
68 |
| - |
69 |
| - // TODO : separate PR to provide supplier for Async client |
70 |
| - return requestOverrideConfig.toBuilder() |
71 |
| - .endpointProvider(BucketEndpointProvider.create(delegateEndpointProvider, null)) |
72 |
| - .build(); |
| 85 | + private CompletableFuture<Region> regionCompletableFuture(String bucketName) { |
| 86 | + StringBuilder stringBuilder = new StringBuilder(); |
| 87 | + return CompletableFuture.supplyAsync( |
| 88 | + () -> ((S3AsyncClient) delegate()).headBucket(HeadBucketRequest.builder() |
| 89 | + .bucket(bucketName) |
| 90 | + .build()) |
| 91 | + .exceptionally(exception -> { |
| 92 | + if (isS3RedirectException(exception)) { |
| 93 | + getBucketRegionFromException( |
| 94 | + (S3Exception) exception).ifPresent( |
| 95 | + stringBuilder::append); |
| 96 | + } else { |
| 97 | + CompletableFutureUtils.failedFuture(exception); |
| 98 | + } |
| 99 | + return null; |
| 100 | + })) |
| 101 | + .thenApplyAsync(h -> { |
| 102 | + h.join(); |
| 103 | + if (h != null && StringUtils.isNotBlank(stringBuilder.toString())) { |
| 104 | + return Region.of(stringBuilder.toString()); |
| 105 | + } |
| 106 | + return null; |
| 107 | + }); |
73 | 108 | }
|
74 |
| - |
75 |
| - //TODO: add cross region logic |
76 |
| - |
77 | 109 | }
|
0 commit comments