Skip to content

Various performance improvements #3175

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

Merged
merged 4 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,27 @@ private SdkStandardLogger() {
* Log the response status code and request ID
*/
public static void logRequestId(SdkHttpResponse response) {
String placeholder = "not available";
String requestId = String.format("Request ID: %s, Extended Request ID: %s",
SdkHttpUtils.firstMatchingHeaderFromCollection(response.headers(),
X_AMZN_REQUEST_ID_HEADERS)
.orElse(placeholder),
response.firstMatchingHeader(X_AMZ_ID_2_HEADER)
.orElse(placeholder));
Supplier<String> logStatement = () -> String.format("Received %s response: %s, %s",
response.isSuccessful() ? "successful" : "failed",
response.statusCode(),
requestId);
Supplier<String> logStatement = () -> constructLog(response);
REQUEST_ID_LOGGER.debug(logStatement);
REQUEST_LOGGER.debug(logStatement);
}

private static String constructLog(SdkHttpResponse response) {
String placeholder = "not available";
String requestId = SdkHttpUtils.firstMatchingHeaderFromCollection(response.headers(),
X_AMZN_REQUEST_ID_HEADERS)
.orElse(placeholder);
StringBuilder details = new StringBuilder().append("Received")
.append(response.isSuccessful() ? " successful" : " failed")
.append(" response:")
.append(" Status Code: ")
.append(response.statusCode())
.append(" Request ID: ")
.append(requestId);

response.firstMatchingHeader(X_AMZ_ID_2_HEADER).ifPresent(extendedRequestId ->
details.append(" Extended Request ID: ")
.append(extendedRequestId));
return details.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

package software.amazon.awssdk.core.checksums;

import java.util.Arrays;
import java.util.Map;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.internal.EnumUtils;

/**
* Enum that indicates all the checksums supported by Flexible checksums in a Service Request/Response Header.
Expand All @@ -31,6 +32,8 @@ public enum Algorithm {
SHA1("sha1", 28),
;

private static final Map<String, Algorithm> VALUE_MAP = EnumUtils.uniqueIndex(Algorithm.class, Algorithm::toString);

private final String value;
private final int length;

Expand All @@ -44,12 +47,18 @@ public static Algorithm fromValue(String value) {
return null;
}
String normalizedValue = StringUtils.lowerCase(value);
return Arrays.stream(values())
.filter(algorithm -> algorithm.value.equals(normalizedValue))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(String.format("Unknown Algorithm '%s'", normalizedValue)));
Algorithm algorithm = VALUE_MAP.get(normalizedValue);
if (algorithm == null) {
throw new IllegalArgumentException("The provided value is not a valid algorithm " + value);
}

return algorithm;
}

@Override
public String toString() {
return String.valueOf(value);
}

/**
* Length corresponds to Base64Encoded length for a given Checksum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

package software.amazon.awssdk.core.internal.async;

import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;
import static software.amazon.awssdk.utils.FunctionalUtils.runAndLogError;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
Expand Down Expand Up @@ -59,6 +59,8 @@ public final class FileAsyncRequestBody implements AsyncRequestBody {
*/
private final Path path;

private final long fileLength;

/**
* Size (in bytes) of ByteBuffer chunks read from the file and delivered to the subscriber.
*/
Expand All @@ -67,15 +69,12 @@ public final class FileAsyncRequestBody implements AsyncRequestBody {
private FileAsyncRequestBody(DefaultBuilder builder) {
this.path = builder.path;
this.chunkSizeInBytes = builder.chunkSizeInBytes == null ? DEFAULT_CHUNK_SIZE : builder.chunkSizeInBytes;
this.fileLength = invokeSafely(() -> Files.size(path));
}

@Override
public Optional<Long> contentLength() {
try {
return Optional.of(Files.size(path));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return Optional.of(fileLength);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import org.reactivestreams.Publisher;
import org.reactivestreams.tck.TestEnvironment;
import software.amazon.awssdk.core.internal.async.FileAsyncRequestBody;
import software.amazon.awssdk.utils.FunctionalUtils;

/**
* TCK verification test for {@link FileAsyncRequestBody}.
Expand Down Expand Up @@ -64,10 +67,16 @@ public Publisher<ByteBuffer> createPublisher(long elements) {
@Override
public Publisher<ByteBuffer> createFailedPublisher() {
// tests properly failing on non existing files:
return FileAsyncRequestBody.builder()
.chunkSizeInBytes(CHUNK_SIZE)
.path(rootDir.resolve("does-not-exist"))
.build();
Path path = rootDir.resolve("createFailedPublisher" + UUID.randomUUID());

FunctionalUtils.invokeSafely(() -> Files.write(path, "test".getBytes(StandardCharsets.UTF_8)));
FileAsyncRequestBody fileAsyncRequestBody = FileAsyncRequestBody.builder()
.chunkSizeInBytes(CHUNK_SIZE)
.path(path)
.build();

FunctionalUtils.invokeSafely(() -> Files.delete(path));
return fileAsyncRequestBody;
}

private Path fileOfNChunks(long nChunks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand All @@ -46,6 +44,7 @@
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.async.DrainingSubscriber;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
Expand Down Expand Up @@ -268,33 +267,33 @@ public void uploadFile_failure_shouldInvokeListener() throws Exception {
UploadFileRequest uploadFileRequest = UploadFileRequest.builder()
.putObjectRequest(r -> r.bucket("bucket")
.key("key"))
.source(Paths.get("/some/nonexistent/path"))
.source(path)
.overrideConfiguration(b -> b.addListener(listener))
.build();
SdkClientException sdkClientException = SdkClientException.create("");
when(s3Crt.putObject(any(PutObjectRequest.class), any(AsyncRequestBody.class)))
.thenThrow(sdkClientException);
FileUpload fileUpload = tm.uploadFile(uploadFileRequest);

CompletableFuture<CompletedFileUpload> future = fileUpload.completionFuture();
assertThatThrownBy(future::join)
.isInstanceOf(CompletionException.class)
.hasCauseInstanceOf(NoSuchFileException.class);
.hasCause(sdkClientException);

ArgumentCaptor<TransferListener.Context.TransferInitiated> captor1 =
ArgumentCaptor.forClass(TransferListener.Context.TransferInitiated.class);
verify(listener, timeout(1000).times(1)).transferInitiated(captor1.capture());
TransferListener.Context.TransferInitiated ctx1 = captor1.getValue();
assertThat(ctx1.request()).isSameAs(uploadFileRequest);
// transferSize is not known since file did not exist
assertThat(ctx1.progressSnapshot().transferSizeInBytes()).isNotPresent();
assertThat(ctx1.progressSnapshot().bytesTransferred()).isZero();

ArgumentCaptor<TransferListener.Context.TransferFailed> captor2 =
ArgumentCaptor.forClass(TransferListener.Context.TransferFailed.class);
verify(listener, timeout(1000).times(1)).transferFailed(captor2.capture());
TransferListener.Context.TransferFailed ctx2 = captor2.getValue();
assertThat(ctx2.request()).isSameAs(uploadFileRequest);
assertThat(ctx2.progressSnapshot().transferSizeInBytes()).isNotPresent();
assertThat(ctx2.progressSnapshot().bytesTransferred()).isZero();
assertThat(ctx2.exception()).isInstanceOf(NoSuchFileException.class);
assertThat(ctx2.exception()).isEqualTo(sdkClientException);

verifyNoMoreInteractions(listener);
}
Expand Down