-
Notifications
You must be signed in to change notification settings - Fork 833
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
Fixed bugs in async signing. #4567
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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
135 changes: 135 additions & 0 deletions
135
.../main/java/software/amazon/awssdk/http/auth/aws/internal/signer/io/InMemoryPublisher.java
This file contains 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,135 @@ | ||
/* | ||
* 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.http.auth.aws.internal.signer.io; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
/** | ||
* Temporarily used for buffering all data into memory. TODO(sra-identity-auth): Remove this by supporting chunked encoding. We | ||
* should not buffer everything into memory. | ||
*/ | ||
@SdkInternalApi | ||
public class InMemoryPublisher implements Publisher<ByteBuffer> { | ||
private final AtomicBoolean subscribed = new AtomicBoolean(false); | ||
private final List<ByteBuffer> data; | ||
|
||
public InMemoryPublisher(List<ByteBuffer> data) { | ||
this.data = new ArrayList<>(Validate.noNullElements(data, "Data must not contain null elements.")); | ||
} | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super ByteBuffer> s) { | ||
if (!subscribed.compareAndSet(false, true)) { | ||
s.onSubscribe(new NoOpSubscription()); | ||
s.onError(new IllegalStateException("InMemoryPublisher cannot be subscribed to twice.")); | ||
return; | ||
} | ||
|
||
s.onSubscribe(new Subscription() { | ||
private final AtomicBoolean sending = new AtomicBoolean(false); | ||
|
||
private final Object doneLock = new Object(); | ||
private final AtomicBoolean done = new AtomicBoolean(false); | ||
private final AtomicLong demand = new AtomicLong(0); | ||
private int position = 0; | ||
|
||
@Override | ||
public void request(long n) { | ||
if (done.get()) { | ||
return; | ||
} | ||
|
||
try { | ||
demand.addAndGet(n); | ||
fulfillDemand(); | ||
} catch (Throwable t) { | ||
finish(() -> s.onError(t)); | ||
} | ||
} | ||
|
||
private void fulfillDemand() { | ||
do { | ||
if (sending.compareAndSet(false, true)) { | ||
try { | ||
send(); | ||
} finally { | ||
sending.set(false); | ||
} | ||
} | ||
} while (!done.get() && demand.get() > 0); | ||
} | ||
|
||
private void send() { | ||
while (true) { | ||
assert position >= 0; | ||
assert position <= data.size(); | ||
|
||
if (done.get()) { | ||
break; | ||
} | ||
|
||
if (position == data.size()) { | ||
finish(s::onComplete); | ||
break; | ||
} | ||
|
||
if (demand.get() == 0) { | ||
break; | ||
} | ||
|
||
demand.decrementAndGet(); | ||
int dataIndex = position; | ||
s.onNext(data.get(dataIndex)); | ||
data.set(dataIndex, null); // We're done with this data here, so allow it to be garbage collected | ||
position++; | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
finish(() -> { | ||
}); | ||
} | ||
|
||
private void finish(Runnable thingToDo) { | ||
synchronized (doneLock) { | ||
if (done.compareAndSet(false, true)) { | ||
thingToDo.run(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private static class NoOpSubscription implements Subscription { | ||
@Override | ||
public void request(long n) { | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
} | ||
} | ||
} |
This file contains 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 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 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.
For my understanding:
streamFuture = new CompletableFuture<>();
here instead ofprepare()
, and further why not initializestreamFuture = new CompletableFuture<>()
in the field declaration itself instead of inprepare()
.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.
FWIU,
prepare()
would be called in the normal flow to get the transformed response (seeMakeAsyncHttpRequestStage
), so if there is an error afterprepare()
is called there, thenstreamFuture
wouldn't be null. On the other hand, if there is an error before the normal flow is executed (e.g.prepare()
wasn't called before an error occured), thenprepare
is called here so that the response-handler is called before exceptionally-completing the stream-future.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.
Yes. We aren't clear in the docs, but it can happen as it happened in this case. I think in general our use of prepare() should be pushed upstream more, since we have other cases where prepare() isn't getting called soon enough. I wanted to have a light touch here, since I know this is a fragile part of the codebase.
I was going for a light touch, but it's possible we could have done something else.
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.
If we did
streamFuture = new CompletableFuture<>()
instead ofprepare()
, the behavior would be the same, assumingprepare()
is defined as it is today. Which we should use depends on what we think is safer in the future.Initializing streamFuture in the field declaration is riskier. We don't really document the lifecycle of a handler, so if the same handler is used for retries it wouldn't work. I don't know exactly which we do.
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.
Thanks Matt for helping me understand better. At least my questions seem ballpark relevant. Agree with the light touch approach - this change seems safest option to me right now.