-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor S3 Transfer Manager uploadDirectory to limit the number of c…
…oncurrent upload file requests.
- Loading branch information
Showing
7 changed files
with
214 additions
and
17 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "bugfix", | ||
"category": "S3 Transfer Manager", | ||
"contributor": "", | ||
"description": "Set a limit on the number of concurrent upload file requests for upload directory. This fixes the OOM issue that could surface when users try to upload a directory that has millions of small files. See [#5023](https://github.com/aws/aws-sdk-java-v2/issues/5023)." | ||
} |
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
132 changes: 132 additions & 0 deletions
132
utils/src/main/java/software/amazon/awssdk/utils/async/IterablePublisher.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,132 @@ | ||
/* | ||
* 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.utils.async; | ||
|
||
|
||
import java.util.Iterator; | ||
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.SdkProtectedApi; | ||
import software.amazon.awssdk.utils.Validate; | ||
import software.amazon.awssdk.utils.internal.async.EmptySubscription; | ||
|
||
/** | ||
* Converts an {@link Iterable} to a {@link Publisher}. | ||
*/ | ||
@SdkProtectedApi | ||
public class IterablePublisher<T> implements Publisher<T> { | ||
|
||
private final Iterator<T> iterator; | ||
private Subscriber<? super T> subscriber; | ||
private AtomicBoolean isSendingData = new AtomicBoolean(false); | ||
private final AtomicLong outstandingDemand = new AtomicLong(); | ||
|
||
// public IterablePublisher(Iterator<T> iterable) { | ||
// Validate.notNull(iterable, "iterable"); | ||
// this.iterator = iterable.iterator(); | ||
// } | ||
|
||
public IterablePublisher(Iterable<T> iterable) { | ||
Validate.notNull(iterable, "iterable"); | ||
this.iterator = iterable.iterator(); | ||
} | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super T> s) { | ||
if (subscriber != null) { | ||
s.onSubscribe(new NoOpSubscription(s)); | ||
s.onError(new IllegalArgumentException("Only one subscription may be active at a time.")); | ||
} | ||
|
||
this.subscriber = s; | ||
|
||
if (!iterator.hasNext()) { | ||
subscriber.onSubscribe(new EmptySubscription(s)); | ||
return; | ||
} | ||
|
||
subscriber.onSubscribe(new IteratorSubscription()); | ||
} | ||
|
||
private class IteratorSubscription implements Subscription { | ||
private volatile boolean done; | ||
|
||
@Override | ||
public void request(long newDemand) { | ||
if (newDemand <= 0) { | ||
subscriber.onError(new IllegalArgumentException("demand is not positive")); | ||
} | ||
|
||
outstandingDemand.updateAndGet(current -> { | ||
if (Long.MAX_VALUE - current < newDemand) { | ||
return Long.MAX_VALUE; | ||
} | ||
|
||
return current + newDemand; | ||
}); | ||
sendData(); | ||
} | ||
|
||
private void sendData() { | ||
do { | ||
if (!isSendingData.compareAndSet(false, true)) { | ||
return; | ||
} | ||
try { | ||
doSendData(); | ||
} finally { | ||
isSendingData.set(false); | ||
} | ||
} while (shouldSendMoreData()); | ||
} | ||
|
||
private boolean shouldSendMoreData() { | ||
if (done) { | ||
return false; | ||
} | ||
|
||
if (!iterator.hasNext()) { | ||
done = true; | ||
subscriber.onComplete(); | ||
return false; | ||
} | ||
|
||
return outstandingDemand.get() > 0; | ||
} | ||
|
||
private void doSendData() { | ||
while (shouldSendMoreData()) { | ||
outstandingDemand.decrementAndGet(); | ||
T next = iterator.next(); | ||
if (next == null) { | ||
done = true; | ||
subscriber.onError(new IllegalArgumentException("Iterable returned null")); | ||
} else { | ||
subscriber.onNext(next); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
done = true; | ||
subscriber = null; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
utils/src/main/java/software/amazon/awssdk/utils/async/NoOpSubscription.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,48 @@ | ||
/* | ||
* 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.utils.async; | ||
|
||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import software.amazon.awssdk.annotations.SdkProtectedApi; | ||
|
||
/** An implementation of {@link Subscription} that does nothing. | ||
* <p> | ||
* Useful in situations where a {@link Publisher} needs to | ||
* signal {@code exceptionOccurred} or {@code onComplete} immediately after | ||
* {@code subscribe()} but but it needs to signal{@code onSubscription} first. | ||
*/ | ||
@SdkProtectedApi | ||
public final class NoOpSubscription implements Subscription { | ||
private final Subscriber<?> subscriber; | ||
|
||
public NoOpSubscription(Subscriber<?> subscriber) { | ||
this.subscriber = subscriber; | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
if (n < 1) { | ||
subscriber.onError(new IllegalArgumentException("Demand must be positive!")); | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
|
||
} | ||
} |