-
Notifications
You must be signed in to change notification settings - Fork 927
Fix bug in MultipartS3AsyncClient GetObject Retryable Errors #6309
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
Changes from 7 commits
3ceee7f
0381604
4cdded1
54457df
7f8803f
22b0a0b
05ea027
14cccda
9749657
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "bugfix", | ||
"category": "Amazon S3", | ||
"contributor": "", | ||
"description": "Fix bug in MultipartS3AsyncClient GET where retryable errors may not be retried, and if retried, successful responses are incorrectly processed with the initial error." | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -18,6 +18,7 @@ | |||
import java.nio.ByteBuffer; | ||||
import java.util.concurrent.CompletableFuture; | ||||
import java.util.concurrent.atomic.AtomicBoolean; | ||||
import java.util.concurrent.atomic.AtomicInteger; | ||||
import java.util.concurrent.atomic.AtomicLong; | ||||
import org.reactivestreams.Subscriber; | ||||
import org.reactivestreams.Subscription; | ||||
|
@@ -54,20 +55,10 @@ public class SplittingTransformer<ResponseT, ResultT> implements SdkPublisher<As | |||
*/ | ||||
private final AsyncResponseTransformer<ResponseT, ResultT> upstreamResponseTransformer; | ||||
|
||||
/** | ||||
* Set to true once {@code .prepare()} is called on the upstreamResponseTransformer | ||||
*/ | ||||
private final AtomicBoolean preparedCalled = new AtomicBoolean(false); | ||||
|
||||
/** | ||||
* Set to true once {@code .onResponse()} is called on the upstreamResponseTransformer | ||||
*/ | ||||
private final AtomicBoolean onResponseCalled = new AtomicBoolean(false); | ||||
|
||||
/** | ||||
* Set to true once {@code .onStream()} is called on the upstreamResponseTransformer | ||||
*/ | ||||
private final AtomicBoolean onStreamCalled = new AtomicBoolean(false); | ||||
private boolean onStreamCalled; | ||||
|
||||
/** | ||||
* Set to true once {@code .cancel()} is called in the subscription of the downstream subscriber, or if the | ||||
|
@@ -111,6 +102,17 @@ public class SplittingTransformer<ResponseT, ResultT> implements SdkPublisher<As | |||
|
||||
private final Object cancelLock = new Object(); | ||||
|
||||
/** | ||||
* Keeps track of the upstream future returned by {@code upstreamResponseTransformer.prepare()}. If an error occurs, we | ||||
* forward this to the {@code resultFuture}. | ||||
*/ | ||||
private volatile CompletableFuture<ResultT> upstreamFuture; | ||||
|
||||
/** | ||||
* Tracks the part number. Errors will only be retried for the first part. | ||||
*/ | ||||
private final AtomicInteger partNumber = new AtomicInteger(0); | ||||
|
||||
private SplittingTransformer(AsyncResponseTransformer<ResponseT, ResultT> upstreamResponseTransformer, | ||||
Long maximumBufferSizeInBytes, | ||||
CompletableFuture<ResultT> resultFuture) { | ||||
|
@@ -198,7 +200,7 @@ private boolean doEmit() { | |||
} | ||||
if (outstandingDemand.get() > 0) { | ||||
demand = outstandingDemand.decrementAndGet(); | ||||
downstreamSubscriber.onNext(new IndividualTransformer()); | ||||
downstreamSubscriber.onNext(new IndividualTransformer(partNumber.incrementAndGet())); | ||||
} | ||||
} | ||||
return false; | ||||
|
@@ -216,7 +218,7 @@ private void handleSubscriptionCancel() { | |||
log.trace(() -> "downstreamSubscriber already null, skipping downstreamSubscriber.onComplete()"); | ||||
return; | ||||
} | ||||
if (!onStreamCalled.get()) { | ||||
if (!onStreamCalled) { | ||||
// we never subscribe publisherToUpstream to the upstream, it would not complete | ||||
downstreamSubscriber = null; | ||||
return; | ||||
|
@@ -230,6 +232,7 @@ private void handleSubscriptionCancel() { | |||
} else { | ||||
log.trace(() -> "calling downstreamSubscriber.onComplete()"); | ||||
downstreamSubscriber.onComplete(); | ||||
CompletableFutureUtils.forwardResultTo(upstreamFuture, resultFuture); | ||||
} | ||||
downstreamSubscriber = null; | ||||
}); | ||||
|
@@ -259,28 +262,27 @@ private void handleFutureCancel(Throwable e) { | |||
* body publisher. | ||||
*/ | ||||
private class IndividualTransformer implements AsyncResponseTransformer<ResponseT, ResponseT> { | ||||
private final int partNumber; | ||||
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. NIT: same here, avoid referring to s3 related terms |
||||
private ResponseT response; | ||||
private CompletableFuture<ResponseT> individualFuture; | ||||
|
||||
IndividualTransformer(int partNumber) { | ||||
this.partNumber = partNumber; | ||||
} | ||||
|
||||
@Override | ||||
public CompletableFuture<ResponseT> prepare() { | ||||
this.individualFuture = new CompletableFuture<>(); | ||||
if (preparedCalled.compareAndSet(false, true)) { | ||||
|
||||
if (partNumber == 1) { | ||||
if (isCancelled.get()) { | ||||
return individualFuture; | ||||
} | ||||
log.trace(() -> "calling prepare on the upstream transformer"); | ||||
CompletableFuture<ResultT> upstreamFuture = upstreamResponseTransformer.prepare(); | ||||
if (!resultFuture.isDone()) { | ||||
CompletableFutureUtils.forwardResultTo(upstreamFuture, resultFuture); | ||||
} | ||||
upstreamFuture = upstreamResponseTransformer.prepare(); | ||||
|
||||
} | ||||
resultFuture.whenComplete((r, e) -> { | ||||
if (e == null) { | ||||
return; | ||||
} | ||||
individualFuture.completeExceptionally(e); | ||||
}); | ||||
|
||||
individualFuture.whenComplete((r, e) -> { | ||||
if (isCancelled.get()) { | ||||
handleSubscriptionCancel(); | ||||
|
@@ -291,7 +293,7 @@ public CompletableFuture<ResponseT> prepare() { | |||
|
||||
@Override | ||||
public void onResponse(ResponseT response) { | ||||
if (onResponseCalled.compareAndSet(false, true)) { | ||||
if (partNumber == 1) { | ||||
log.trace(() -> "calling onResponse on the upstream transformer"); | ||||
upstreamResponseTransformer.onResponse(response); | ||||
} | ||||
|
@@ -304,7 +306,8 @@ public void onStream(SdkPublisher<ByteBuffer> publisher) { | |||
return; | ||||
} | ||||
synchronized (cancelLock) { | ||||
if (onStreamCalled.compareAndSet(false, true)) { | ||||
if (partNumber == 1) { | ||||
onStreamCalled = true; | ||||
log.trace(() -> "calling onStream on the upstream transformer"); | ||||
upstreamResponseTransformer.onStream(upstreamSubscriber -> publisherToUpstream.subscribe( | ||||
DelegatingBufferingSubscriber.builder() | ||||
|
@@ -314,14 +317,26 @@ public void onStream(SdkPublisher<ByteBuffer> publisher) { | |||
); | ||||
} | ||||
} | ||||
publisher.subscribe(new IndividualPartSubscriber<>(this.individualFuture, response)); | ||||
|
||||
CompletableFutureUtils.forwardResultTo(upstreamFuture, resultFuture); | ||||
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. Why do we forward result here again and do it for every part? 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. I guess we just need to do it for the first part. When this is removed, an error on a subsequent part won't get propagated. 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. Specifically, in the new test I added, if we don't call this at all, instead of S3Exception being thrown, we get:
Line 317 in 14cccda
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. Hmm, we should probably figure out why onError was invoked multiple times. 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. This is the flow of events:
Not sure how we can prevent 4) from happening, is it necessary? 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. We could always prevent sending bytes to the |
||||
publisher.subscribe(new IndividualPartSubscriber<>(this.individualFuture, response, partNumber)); | ||||
} | ||||
|
||||
@Override | ||||
public void exceptionOccurred(Throwable error) { | ||||
publisherToUpstream.error(error); | ||||
log.trace(() -> "calling exceptionOccurred on the upstream transformer"); | ||||
upstreamResponseTransformer.exceptionOccurred(error); | ||||
if (partNumber == 1) { | ||||
log.trace(() -> "calling exceptionOccurred on the upstream transformer"); | ||||
upstreamResponseTransformer.exceptionOccurred(error); | ||||
} | ||||
|
||||
// Invoking publisherToUpstream.error() essentially fails the request immediately. We should only call this if | ||||
// 1) The part number is greater than 1, since we want to retry errors on the first part OR 2) onStream() has | ||||
// already been invoked and data has started to be written | ||||
synchronized (cancelLock) { | ||||
davidh44 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
if (partNumber > 1 || onStreamCalled) { | ||||
publisherToUpstream.error(error); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
|
||||
|
@@ -332,11 +347,13 @@ class IndividualPartSubscriber<T> implements Subscriber<ByteBuffer> { | |||
|
||||
private final CompletableFuture<T> future; | ||||
private final T response; | ||||
private final int partNumber; | ||||
private Subscription subscription; | ||||
|
||||
IndividualPartSubscriber(CompletableFuture<T> future, T response) { | ||||
IndividualPartSubscriber(CompletableFuture<T> future, T response, int partNumber) { | ||||
this.future = future; | ||||
this.response = response; | ||||
this.partNumber = partNumber; | ||||
} | ||||
|
||||
@Override | ||||
|
@@ -376,7 +393,9 @@ public void onComplete() { | |||
} | ||||
|
||||
private void handleError(Throwable t) { | ||||
publisherToUpstream.error(t); | ||||
if (partNumber > 1) { | ||||
davidh44 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
publisherToUpstream.error(t); | ||||
} | ||||
future.completeExceptionally(t); | ||||
} | ||||
} | ||||
|
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.
NIT (non-blocking): I think we should avoid referring to 'parts' in the
SplittingTranformer
, that's a s3 concept, when this class is more abstract, in a core package. Maybe rename to something like 'onNextSignalsSent' oronNextNumber
which would track the total amount of 'onNext' signals sent to thedownstreamSubscriber