-
Notifications
You must be signed in to change notification settings - Fork 1k
Cross region support for CRT Client #4129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.services.s3.crt; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static software.amazon.awssdk.services.s3.crt.S3CrtClientCopyIntegrationTest.randomBytes; | ||
| import static software.amazon.awssdk.services.s3.utils.ChecksumUtils.computeCheckSum; | ||
| import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.file.Files; | ||
| import java.util.Random; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.awssdk.core.ResponseBytes; | ||
| import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
| import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
| import software.amazon.awssdk.core.sync.ResponseTransformer; | ||
| import software.amazon.awssdk.crt.CrtResource; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.s3.S3AsyncClient; | ||
| import software.amazon.awssdk.services.s3.S3IntegrationTestBase; | ||
| import software.amazon.awssdk.services.s3.model.CopyObjectResponse; | ||
| import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
| import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
| import software.amazon.awssdk.services.s3.model.S3Exception; | ||
| import software.amazon.awssdk.testutils.RandomTempFile; | ||
| import software.amazon.awssdk.testutils.service.AwsTestBase; | ||
|
|
||
| public class S3CrossRegionCrtIntegrationTest extends S3IntegrationTestBase { | ||
| public static final Region CROSS_REGION = Region.EU_CENTRAL_1; | ||
| private static final String BUCKET = temporaryBucketName(S3CrossRegionCrtIntegrationTest.class); | ||
| private static final String KEY = "key"; | ||
| private static final String ORIGINAL_OBJ = "test_file.dat"; | ||
| private static final String COPIED_OBJ = "test_file_copy.dat"; | ||
| private static final long OBJ_SIZE = ThreadLocalRandom.current().nextLong(8 * 1024, 16 * 1024 + 1); | ||
| private static S3AsyncClient crtClient; | ||
| private static File file; | ||
| private static ExecutorService executorService; | ||
|
|
||
| @BeforeAll | ||
| public static void setup() throws Exception { | ||
| S3IntegrationTestBase.setUp(); | ||
| S3IntegrationTestBase.createBucket(BUCKET); | ||
| crtClient = S3AsyncClient.crtBuilder() | ||
| .region(CROSS_REGION) | ||
| .crossRegionAccessEnabled(true) | ||
| .credentialsProvider(AwsTestBase.CREDENTIALS_PROVIDER_CHAIN) | ||
| .build(); | ||
| file = new RandomTempFile(10_000); | ||
| S3IntegrationTestBase.s3.putObject(PutObjectRequest.builder() | ||
| .bucket(BUCKET) | ||
| .key(KEY) | ||
| .build(), file.toPath()); | ||
| executorService = Executors.newFixedThreadPool(2); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void cleanup() { | ||
| crtClient.close(); | ||
| S3IntegrationTestBase.deleteBucketAndAllContents(BUCKET); | ||
| executorService.shutdown(); | ||
| CrtResource.waitForNoResources(); | ||
| } | ||
|
|
||
| @Test | ||
| void crossRegionClient_getObject() throws IOException { | ||
| byte[] bytes = | ||
| crtClient.getObject(b -> b.bucket(BUCKET).key(KEY), AsyncResponseTransformer.toBytes()).join().asByteArray(); | ||
| assertThat(bytes).isEqualTo(Files.readAllBytes(file.toPath())); | ||
| } | ||
|
|
||
| @Test | ||
| void putObjectNoSuchBucket() { | ||
| assertThatThrownBy(() -> crtClient.getObject(GetObjectRequest.builder().bucket("nonExistingTestBucket" + UUID.randomUUID()).key(KEY).build(), | ||
| AsyncResponseTransformer.toBytes()).get()) | ||
| .hasCauseInstanceOf(S3Exception.class) | ||
| .satisfies(throwable -> assertThat(throwable.getCause()).satisfies(cause -> assertThat(((S3Exception) cause).statusCode()).isEqualTo(404))); | ||
| } | ||
|
|
||
| @Test | ||
| void copy_copiedObject_hasSameContent() { | ||
| byte[] originalContent = randomBytes(OBJ_SIZE); | ||
| createOriginalObject(originalContent, ORIGINAL_OBJ); | ||
| copyObject(ORIGINAL_OBJ, COPIED_OBJ); | ||
| validateCopiedObject(originalContent, ORIGINAL_OBJ); | ||
| } | ||
|
|
||
| private void copyObject(String original, String destination) { | ||
| CompletableFuture<CopyObjectResponse> future = crtClient.copyObject(c -> c | ||
| .sourceBucket(BUCKET) | ||
| .sourceKey(original) | ||
| .destinationBucket(BUCKET) | ||
| .destinationKey(destination)); | ||
|
|
||
| CopyObjectResponse copyObjectResponse = future.join(); | ||
| assertThat(copyObjectResponse.responseMetadata().requestId()).isNotNull(); | ||
| assertThat(copyObjectResponse.sdkHttpResponse()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void putObject_byteBufferBody_objectSentCorrectly() { | ||
| byte[] data = new byte[16384]; | ||
| new Random().nextBytes(data); | ||
| ByteBuffer byteBuffer = ByteBuffer.wrap(data); | ||
|
|
||
| AsyncRequestBody body = AsyncRequestBody.fromByteBuffer(byteBuffer); | ||
|
|
||
| crtClient.putObject(r -> r.bucket(BUCKET).key(KEY), body).join(); | ||
|
|
||
| ResponseBytes<GetObjectResponse> responseBytes = S3IntegrationTestBase.s3.getObject(r -> r.bucket(BUCKET).key(KEY), | ||
| ResponseTransformer.toBytes()); | ||
|
|
||
| byte[] expectedSum = computeCheckSum(byteBuffer); | ||
|
|
||
| assertThat(computeCheckSum(responseBytes.asByteBuffer())).isEqualTo(expectedSum); | ||
| } | ||
|
|
||
| private void validateCopiedObject(byte[] originalContent, String originalKey) { | ||
| ResponseBytes<GetObjectResponse> copiedObject = s3.getObject(r -> r.bucket(BUCKET) | ||
| .key(originalKey), | ||
| ResponseTransformer.toBytes()); | ||
| assertThat(computeCheckSum(copiedObject.asByteBuffer())).isEqualTo(computeCheckSum(ByteBuffer.wrap(originalContent))); | ||
| } | ||
|
|
||
| private void createOriginalObject(byte[] originalContent, String originalKey) { | ||
| crtClient.putObject(r -> r.bucket(BUCKET) | ||
| .key(originalKey), | ||
| AsyncRequestBody.fromBytes(originalContent)).join(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.services.s3.crt; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
| import software.amazon.awssdk.core.async.SdkPublisher; | ||
| import software.amazon.awssdk.http.async.SimpleSubscriber; | ||
| import software.amazon.awssdk.services.s3.model.GetObjectResponse; | ||
|
|
||
| public final class TestResponseTransformer implements AsyncResponseTransformer<GetObjectResponse, Void> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the use case for this class?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove this from S3CrtGetObjectIntegrationTest |
||
| private CompletableFuture<Void> future; | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> prepare() { | ||
| future = new CompletableFuture<>(); | ||
| return future; | ||
| } | ||
|
|
||
| @Override | ||
| public void onResponse(GetObjectResponse response) { | ||
| assertThat(response).isNotNull(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onStream(SdkPublisher<ByteBuffer> publisher) { | ||
| publisher.subscribe(new SimpleSubscriber(b -> { | ||
| }) { | ||
| @Override | ||
| public void onComplete() { | ||
| super.onComplete(); | ||
| future.complete(null); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void exceptionOccurred(Throwable error) { | ||
| future.completeExceptionally(error); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import static software.amazon.awssdk.services.s3.internal.crt.S3InternalSdkHttpExecutionAttribute.HTTP_CHECKSUM; | ||
| import static software.amazon.awssdk.services.s3.internal.crt.S3InternalSdkHttpExecutionAttribute.METAREQUEST_PAUSE_OBSERVABLE; | ||
| import static software.amazon.awssdk.services.s3.internal.crt.S3InternalSdkHttpExecutionAttribute.OPERATION_NAME; | ||
| import static software.amazon.awssdk.services.s3.internal.crt.S3InternalSdkHttpExecutionAttribute.SIGNING_REGION; | ||
| import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; | ||
|
|
||
| import java.net.URI; | ||
|
|
@@ -31,6 +32,7 @@ | |
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.annotations.SdkTestInternalApi; | ||
| import software.amazon.awssdk.core.interceptor.trait.HttpChecksum; | ||
| import software.amazon.awssdk.crt.auth.signing.AwsSigningConfig; | ||
| import software.amazon.awssdk.crt.http.HttpHeader; | ||
| import software.amazon.awssdk.crt.http.HttpRequest; | ||
| import software.amazon.awssdk.crt.s3.ChecksumConfig; | ||
|
|
@@ -43,6 +45,7 @@ | |
| import software.amazon.awssdk.http.SdkHttpRequest; | ||
| import software.amazon.awssdk.http.async.AsyncExecuteRequest; | ||
| import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.utils.AttributeMap; | ||
| import software.amazon.awssdk.utils.Logger; | ||
| import software.amazon.awssdk.utils.NumericUtils; | ||
|
|
@@ -117,6 +120,7 @@ public CompletableFuture<Void> execute(AsyncExecuteRequest asyncRequest) { | |
|
|
||
| HttpChecksum httpChecksum = asyncRequest.httpExecutionAttributes().getAttribute(HTTP_CHECKSUM); | ||
| ResumeToken resumeToken = asyncRequest.httpExecutionAttributes().getAttribute(CRT_PAUSE_RESUME_TOKEN); | ||
| Region signingRegion = asyncRequest.httpExecutionAttributes().getAttribute(SIGNING_REGION); | ||
|
|
||
| ChecksumConfig checksumConfig = | ||
| checksumConfig(httpChecksum, requestType, s3NativeClientConfiguration.checksumValidationEnabled()); | ||
|
|
@@ -130,6 +134,13 @@ public CompletableFuture<Void> execute(AsyncExecuteRequest asyncRequest) { | |
| .withResponseHandler(responseHandler) | ||
| .withResumeToken(resumeToken); | ||
|
|
||
| // Create a new SigningConfig object only if the signing region has changed from the previously configured region. | ||
| if (signingRegion != null && !s3ClientOptions.getRegion().equals(signingRegion)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch ! |
||
| requestOptions.withSigningConfig( | ||
| AwsSigningConfig.getDefaultS3SigningConfig(signingRegion.id(), | ||
| s3ClientOptions.getCredentialsProvider())); | ||
| } | ||
|
|
||
| S3MetaRequest s3MetaRequest = crtS3Client.makeMetaRequest(requestOptions); | ||
| S3MetaRequestPauseObservable observable = | ||
| asyncRequest.httpExecutionAttributes().getAttribute(METAREQUEST_PAUSE_OBSERVABLE); | ||
|
|
@@ -144,6 +155,7 @@ public CompletableFuture<Void> execute(AsyncExecuteRequest asyncRequest) { | |
| return executeFuture; | ||
| } | ||
|
|
||
|
|
||
| private static URI getEndpoint(URI uri) { | ||
| return invokeSafely(() -> new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), null, null, null)); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.