-
Notifications
You must be signed in to change notification settings - Fork 910
Fix Trailer based Http Checksum for Async Request body with variable chunk size #3380
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6a7a43
Fix Trailer based Http Checksum for Async Request body created from File
joviegas f02605b
Updated Fix to support AsyncRequestFileBody of variable chunk size
joviegas b662515
Reusing .flatMapIterable API of SDKPublisher to create Chunks of fixe…
joviegas 7eba119
Adding a separate class ChunkBuffer to handle Multiple thread accessi…
joviegas 71aab4f
Creating a SynchronousBuffer for mapper , also handling ZeroByte s3 p…
joviegas e671beb
Made ChunkBuffer synchronized
joviegas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "bugfix", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"description": "Fixed issue where request used to fail while calculating Trailer based checksum for Async File Request body." | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
import static software.amazon.awssdk.core.internal.util.ChunkContentUtils.createChunk; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.reactivestreams.Subscriber; | ||
|
@@ -41,12 +43,16 @@ | |
@SdkInternalApi | ||
public class ChecksumCalculatingAsyncRequestBody implements AsyncRequestBody { | ||
|
||
public static final int DEFAULT_CHUNK_SIZE = 16 * 1024; | ||
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. Instead of making it a public member in this class, can we create a constant class to put all checksum related chunk size constants? |
||
|
||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static final byte[] FINAL_BYTE = new byte[0]; | ||
joviegas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final AsyncRequestBody wrapped; | ||
private final SdkChecksum sdkChecksum; | ||
private final Algorithm algorithm; | ||
private final String trailerHeader; | ||
private final AtomicLong remainingBytes; | ||
private final long totalBytes; | ||
private final ByteBuffer currentBuffer; | ||
|
||
private ChecksumCalculatingAsyncRequestBody(DefaultBuilder builder) { | ||
|
||
|
@@ -57,8 +63,10 @@ private ChecksumCalculatingAsyncRequestBody(DefaultBuilder builder) { | |
this.algorithm = builder.algorithm; | ||
this.sdkChecksum = builder.algorithm != null ? SdkChecksum.forAlgorithm(algorithm) : null; | ||
this.trailerHeader = builder.trailerHeader; | ||
this.remainingBytes = new AtomicLong(wrapped.contentLength() | ||
.orElseThrow(() -> new UnsupportedOperationException("Content length must be supplied."))); | ||
this.totalBytes = wrapped.contentLength() | ||
.orElseThrow(() -> new UnsupportedOperationException("Content length must be supplied.")); | ||
this.remainingBytes = new AtomicLong(); | ||
this.currentBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); | ||
} | ||
|
||
/** | ||
|
@@ -148,7 +156,11 @@ public void subscribe(Subscriber<? super ByteBuffer> s) { | |
if (sdkChecksum != null) { | ||
sdkChecksum.reset(); | ||
} | ||
wrapped.subscribe(new ChecksumCalculatingSubscriber(s, sdkChecksum, trailerHeader, remainingBytes)); | ||
|
||
this.remainingBytes.set(totalBytes); | ||
|
||
wrapped.flatMapIterable(this::bufferAndCreateChunks) | ||
.subscribe(new ChecksumCalculatingSubscriber(s, sdkChecksum, trailerHeader, totalBytes)); | ||
} | ||
|
||
private static final class ChecksumCalculatingSubscriber implements Subscriber<ByteBuffer> { | ||
|
@@ -162,11 +174,11 @@ private static final class ChecksumCalculatingSubscriber implements Subscriber<B | |
|
||
ChecksumCalculatingSubscriber(Subscriber<? super ByteBuffer> wrapped, | ||
SdkChecksum checksum, | ||
String trailerHeader, AtomicLong remainingBytes) { | ||
String trailerHeader, long totalBytes) { | ||
this.wrapped = wrapped; | ||
this.checksum = checksum; | ||
this.trailerHeader = trailerHeader; | ||
this.remainingBytes = remainingBytes; | ||
this.remainingBytes = new AtomicLong(totalBytes); | ||
} | ||
|
||
@Override | ||
|
@@ -189,7 +201,8 @@ public void onNext(ByteBuffer byteBuffer) { | |
ByteBuffer allocatedBuffer = getFinalChecksumAppendedChunk(byteBuffer); | ||
wrapped.onNext(allocatedBuffer); | ||
} else { | ||
wrapped.onNext(byteBuffer); | ||
ByteBuffer allocatedBuffer = createChunk(byteBuffer, false); | ||
wrapped.onNext(allocatedBuffer); | ||
} | ||
} catch (SdkException sdkException) { | ||
this.subscription.cancel(); | ||
|
@@ -225,4 +238,48 @@ public void onComplete() { | |
wrapped.onComplete(); | ||
} | ||
} | ||
|
||
private Iterable<ByteBuffer> bufferAndCreateChunks(ByteBuffer buffer) { | ||
int startPosition = 0; | ||
int currentBytesRead = buffer.remaining(); | ||
|
||
List<ByteBuffer> resultBufferedList = new ArrayList<>(); | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
do { | ||
int bufferedBytes = currentBuffer.position(); | ||
int availableToRead = DEFAULT_CHUNK_SIZE - bufferedBytes; | ||
int bytesToMove = Math.min(availableToRead, currentBytesRead - startPosition); | ||
|
||
if (bufferedBytes == 0) { | ||
currentBuffer.put(buffer.array(), startPosition, bytesToMove); | ||
} else { | ||
currentBuffer.put(buffer.array(), 0, bytesToMove); | ||
} | ||
|
||
startPosition = startPosition + bytesToMove; | ||
|
||
// Send the data once the buffer is full | ||
if (currentBuffer.position() == DEFAULT_CHUNK_SIZE) { | ||
currentBuffer.position(0); | ||
ByteBuffer bufferToSend = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); | ||
bufferToSend.put(currentBuffer.array(), 0, DEFAULT_CHUNK_SIZE); | ||
bufferToSend.clear(); | ||
currentBuffer.clear(); | ||
resultBufferedList.add(bufferToSend); | ||
remainingBytes.addAndGet(-DEFAULT_CHUNK_SIZE); | ||
} | ||
|
||
} while (startPosition < currentBytesRead); | ||
|
||
int bufferedBytes = currentBuffer.position(); | ||
// Send the remainder buffered bytes at the end when there no more bytes | ||
if (bufferedBytes > 0 && remainingBytes.get() == bufferedBytes) { | ||
currentBuffer.clear(); | ||
ByteBuffer trimmedBuffer = ByteBuffer.allocate(bufferedBytes); | ||
trimmedBuffer.put(currentBuffer.array(), 0, bufferedBytes); | ||
trimmedBuffer.clear(); | ||
resultBufferedList.add(trimmedBuffer); | ||
remainingBytes.addAndGet(-bufferedBytes); | ||
} | ||
return resultBufferedList; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.