-
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 5 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 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
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
142 changes: 142 additions & 0 deletions
142
core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/async/ChunkBuffer.java
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,142 @@ | ||
/* | ||
* 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.core.internal.async; | ||
|
||
import static software.amazon.awssdk.core.HttpChecksumConstant.DEFAULT_ASYNC_CHUNK_SIZE; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.utils.Validate; | ||
import software.amazon.awssdk.utils.builder.SdkBuilder; | ||
|
||
/** | ||
* Class that will buffer incoming BufferBytes of totalBytes length to chunks of bufferSize* | ||
*/ | ||
@SdkInternalApi | ||
public final class ChunkBuffer { | ||
private final AtomicLong remainingBytes; | ||
private final ByteBuffer currentBuffer; | ||
private final int bufferSize; | ||
private List<ByteBuffer> bufferedList; | ||
|
||
|
||
private ChunkBuffer(Long totalBytes, Integer bufferSize) { | ||
Validate.notNull(totalBytes, "The totalBytes must not be null"); | ||
|
||
int chunkSize = bufferSize != null ? bufferSize : DEFAULT_ASYNC_CHUNK_SIZE; | ||
this.bufferSize = chunkSize; | ||
this.currentBuffer = ByteBuffer.allocate(chunkSize); | ||
this.remainingBytes = new AtomicLong(totalBytes); | ||
bufferedList = new ArrayList<>(); | ||
} | ||
|
||
|
||
public static Builder builder() { | ||
return new DefaultBuilder(); | ||
} | ||
|
||
public List<ByteBuffer> getBufferedList() { | ||
if (currentBuffer == null) { | ||
throw new IllegalStateException(""); | ||
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. currentBuffer should never be null, right? |
||
} | ||
List<ByteBuffer> ret = bufferedList; | ||
bufferedList = new ArrayList<>(); | ||
return Collections.unmodifiableList(ret); | ||
} | ||
|
||
public Iterable<ByteBuffer> bufferAndCreateChunks(ByteBuffer buffer) { | ||
int startPosition = 0; | ||
int currentBytesRead = buffer.remaining(); | ||
|
||
do { | ||
|
||
int bufferedBytes = currentBuffer.position(); | ||
int availableToRead = bufferSize - 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() == bufferSize) { | ||
currentBuffer.position(0); | ||
ByteBuffer bufferToSend = ByteBuffer.allocate(bufferSize); | ||
bufferToSend.put(currentBuffer.array(), 0, bufferSize); | ||
bufferToSend.clear(); | ||
currentBuffer.clear(); | ||
bufferedList.add(bufferToSend); | ||
remainingBytes.addAndGet(-bufferSize); | ||
} | ||
} while (startPosition < currentBytesRead); | ||
|
||
int remainingBytesInBuffer = currentBuffer.position(); | ||
|
||
// Send the remaining buffer when | ||
// 1. remainingBytes in buffer are same as the last few bytes to be read. | ||
// 2. If it is a zero byte and the last byte to be read. | ||
if (remainingBytes.get() == remainingBytesInBuffer && | ||
(buffer.remaining() == 0 || remainingBytesInBuffer > 0)) { | ||
currentBuffer.clear(); | ||
ByteBuffer trimmedBuffer = ByteBuffer.allocate(remainingBytesInBuffer); | ||
trimmedBuffer.put(currentBuffer.array(), 0, remainingBytesInBuffer); | ||
trimmedBuffer.clear(); | ||
bufferedList.add(trimmedBuffer); | ||
remainingBytes.addAndGet(-remainingBytesInBuffer); | ||
} | ||
return bufferedList; | ||
} | ||
|
||
public interface Builder extends SdkBuilder<Builder, ChunkBuffer> { | ||
|
||
Builder bufferSize(int bufferSize); | ||
|
||
Builder totalBytes(long totalBytes); | ||
|
||
|
||
} | ||
|
||
private static final class DefaultBuilder implements Builder { | ||
|
||
private Integer bufferSize; | ||
private Long totalBytes; | ||
|
||
@Override | ||
public ChunkBuffer build() { | ||
return new ChunkBuffer(totalBytes, bufferSize); | ||
} | ||
|
||
@Override | ||
public Builder bufferSize(int bufferSize) { | ||
this.bufferSize = bufferSize; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder totalBytes(long totalBytes) { | ||
this.totalBytes = totalBytes; | ||
return this; | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline, we should probably make this thread safe