-
Notifications
You must be signed in to change notification settings - Fork 896
Ensure onNext will be called even if publishing empty content and onC… #4290
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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": "Sends final checksum chunk and trailer when only onComplete() is called by upstream (empty content)" | ||
} |
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 |
---|---|---|
|
@@ -16,7 +16,8 @@ | |
package software.amazon.awssdk.core.internal.async; | ||
|
||
import static software.amazon.awssdk.core.HttpChecksumConstant.DEFAULT_ASYNC_CHUNK_SIZE; | ||
import static software.amazon.awssdk.core.internal.util.ChunkContentUtils.calculateChecksumContentLength; | ||
import static software.amazon.awssdk.core.internal.util.ChunkContentUtils.LAST_CHUNK_LEN; | ||
import static software.amazon.awssdk.core.internal.util.ChunkContentUtils.calculateChecksumTrailerLength; | ||
import static software.amazon.awssdk.core.internal.util.ChunkContentUtils.calculateChunkLength; | ||
import static software.amazon.awssdk.core.internal.util.ChunkContentUtils.createChecksumTrailer; | ||
import static software.amazon.awssdk.core.internal.util.ChunkContentUtils.createChunk; | ||
|
@@ -28,11 +29,13 @@ | |
import org.reactivestreams.Subscription; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
import software.amazon.awssdk.core.async.SdkPublisher; | ||
import software.amazon.awssdk.core.checksums.Algorithm; | ||
import software.amazon.awssdk.core.checksums.SdkChecksum; | ||
import software.amazon.awssdk.core.exception.SdkException; | ||
import software.amazon.awssdk.utils.BinaryUtils; | ||
import software.amazon.awssdk.utils.Validate; | ||
import software.amazon.awssdk.utils.async.DelegatingSubscriber; | ||
import software.amazon.awssdk.utils.builder.SdkBuilder; | ||
|
||
/** | ||
|
@@ -129,13 +132,12 @@ public ChecksumCalculatingAsyncRequestBody.Builder trailerHeader(String trailerH | |
|
||
@Override | ||
public Optional<Long> contentLength() { | ||
|
||
if (wrapped.contentLength().isPresent() && algorithm != null) { | ||
return Optional.of(calculateChunkLength(wrapped.contentLength().get()) | ||
+ calculateChecksumContentLength(algorithm, trailerHeader)); | ||
} else { | ||
return wrapped.contentLength(); | ||
+ LAST_CHUNK_LEN | ||
+ calculateChecksumTrailerLength(algorithm, trailerHeader)); | ||
} | ||
return wrapped.contentLength(); | ||
} | ||
|
||
@Override | ||
|
@@ -149,12 +151,15 @@ public void subscribe(Subscriber<? super ByteBuffer> s) { | |
if (sdkChecksum != null) { | ||
sdkChecksum.reset(); | ||
} | ||
|
||
SynchronousChunkBuffer synchronousChunkBuffer = new SynchronousChunkBuffer(totalBytes); | ||
wrapped.flatMapIterable(synchronousChunkBuffer::buffer) | ||
alwaysInvokeOnNext(wrapped).flatMapIterable(synchronousChunkBuffer::buffer) | ||
.subscribe(new ChecksumCalculatingSubscriber(s, sdkChecksum, trailerHeader, totalBytes)); | ||
} | ||
|
||
private SdkPublisher<ByteBuffer> alwaysInvokeOnNext(SdkPublisher<ByteBuffer> source) { | ||
return subscriber -> source.subscribe(new OnNextGuaranteedSubscriber(subscriber)); | ||
} | ||
|
||
private static final class ChecksumCalculatingSubscriber implements Subscriber<ByteBuffer> { | ||
|
||
private final Subscriber<? super ByteBuffer> wrapped; | ||
|
@@ -243,4 +248,30 @@ private Iterable<ByteBuffer> buffer(ByteBuffer bytes) { | |
} | ||
} | ||
|
||
public static class OnNextGuaranteedSubscriber extends DelegatingSubscriber<ByteBuffer, ByteBuffer> { | ||
|
||
private volatile boolean onNextInvoked; | ||
|
||
public OnNextGuaranteedSubscriber(Subscriber<? super ByteBuffer> subscriber) { | ||
super(subscriber); | ||
} | ||
|
||
@Override | ||
public void onNext(ByteBuffer t) { | ||
if (!onNextInvoked) { | ||
onNextInvoked = true; | ||
} | ||
|
||
subscriber.onNext(t); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
if (!onNextInvoked) { | ||
subscriber.onNext(ByteBuffer.wrap(new byte[0])); | ||
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. Just a note: request demand should be handled in FlatteningSubscriber as part of |
||
} | ||
super.onComplete(); | ||
} | ||
} | ||
|
||
} |
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.
Should this be an
AtomicBoolean
instead of volatile and usecompareAndSet
like in theDelegatingSubscriber
?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.
I'm not sure what is most correct in this case. If more than one thread can modify it, it seems like then we can miss a case where we're thinking onnext was never called but it was (reading before updating). Then Atomic would be better. @zoewangg does this count as an operation where atomicity is important?
Also have not been able to see a lot of benefits of volatile vs atomic...
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.
onNext, onComplete are guaranteed to be signaled in serial per Reactive Streams spec. https://github.com/reactive-streams/reactive-streams-jvm, so it's not possible to be invoked concurrently.