|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.core.internal.async; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.core.async.AsyncRequestBodySplitConfiguration.DEFAULT_BUFFER_SIZE; |
| 19 | +import static software.amazon.awssdk.core.async.AsyncRequestBodySplitConfiguration.DEFAULT_CHUNK_SIZE; |
| 20 | + |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import java.util.concurrent.atomic.AtomicLong; |
| 27 | +import org.reactivestreams.Subscriber; |
| 28 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 29 | +import software.amazon.awssdk.annotations.SdkTestInternalApi; |
| 30 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 31 | +import software.amazon.awssdk.core.async.AsyncRequestBodySplitConfiguration; |
| 32 | +import software.amazon.awssdk.core.async.SdkPublisher; |
| 33 | +import software.amazon.awssdk.utils.Logger; |
| 34 | +import software.amazon.awssdk.utils.Validate; |
| 35 | +import software.amazon.awssdk.utils.async.SimplePublisher; |
| 36 | + |
| 37 | +/** |
| 38 | + * A helper class to split a {@link FileAsyncRequestBody} to multiple smaller async request bodies. It ensures the buffer used to |
| 39 | + * be under the configured size via {@link AsyncRequestBodySplitConfiguration#bufferSizeInBytes()} by tracking the number of |
| 40 | + * concurrent ongoing {@link AsyncRequestBody}s. |
| 41 | + */ |
| 42 | +@SdkInternalApi |
| 43 | +public final class FileAsyncRequestBodySplitHelper { |
| 44 | + private static final Logger log = Logger.loggerFor(FileAsyncRequestBodySplitHelper.class); |
| 45 | + |
| 46 | + private final AtomicBoolean isSendingRequestBody = new AtomicBoolean(false); |
| 47 | + private final AtomicLong remainingBytes; |
| 48 | + |
| 49 | + private final long totalContentLength; |
| 50 | + private final Path path; |
| 51 | + private final int bufferPerAsyncRequestBody; |
| 52 | + private final long totalBufferSize; |
| 53 | + private final long chunkSize; |
| 54 | + |
| 55 | + private volatile boolean isDone = false; |
| 56 | + |
| 57 | + private AtomicInteger numAsyncRequestBodiesInFlight = new AtomicInteger(0); |
| 58 | + private AtomicInteger chunkIndex = new AtomicInteger(0); |
| 59 | + |
| 60 | + public FileAsyncRequestBodySplitHelper(FileAsyncRequestBody asyncRequestBody, |
| 61 | + AsyncRequestBodySplitConfiguration splitConfiguration) { |
| 62 | + Validate.notNull(asyncRequestBody, "asyncRequestBody"); |
| 63 | + Validate.notNull(splitConfiguration, "splitConfiguration"); |
| 64 | + Validate.isTrue(asyncRequestBody.contentLength().isPresent(), "Content length must be present", asyncRequestBody); |
| 65 | + this.totalContentLength = asyncRequestBody.contentLength().get(); |
| 66 | + this.remainingBytes = new AtomicLong(totalContentLength); |
| 67 | + this.path = asyncRequestBody.path(); |
| 68 | + this.chunkSize = splitConfiguration.chunkSizeInBytes() == null ? |
| 69 | + DEFAULT_CHUNK_SIZE : splitConfiguration.chunkSizeInBytes(); |
| 70 | + this.totalBufferSize = splitConfiguration.bufferSizeInBytes() == null ? DEFAULT_BUFFER_SIZE : |
| 71 | + splitConfiguration.bufferSizeInBytes(); |
| 72 | + this.bufferPerAsyncRequestBody = asyncRequestBody.chunkSizeInBytes(); |
| 73 | + } |
| 74 | + |
| 75 | + public SdkPublisher<AsyncRequestBody> split() { |
| 76 | + |
| 77 | + SimplePublisher<AsyncRequestBody> simplePublisher = new SimplePublisher<>(); |
| 78 | + |
| 79 | + try { |
| 80 | + sendAsyncRequestBody(simplePublisher); |
| 81 | + } catch (Throwable throwable) { |
| 82 | + simplePublisher.error(throwable); |
| 83 | + } |
| 84 | + |
| 85 | + return SdkPublisher.adapt(simplePublisher); |
| 86 | + } |
| 87 | + |
| 88 | + private void sendAsyncRequestBody(SimplePublisher<AsyncRequestBody> simplePublisher) { |
| 89 | + if (!isSendingRequestBody.compareAndSet(false, true)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + doSendAsyncRequestBody(simplePublisher); |
| 95 | + } finally { |
| 96 | + isSendingRequestBody.set(false); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void doSendAsyncRequestBody(SimplePublisher<AsyncRequestBody> simplePublisher) { |
| 101 | + while (true) { |
| 102 | + if (!shouldSendMore()) { |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + AsyncRequestBody currentAsyncRequestBody = newFileAsyncRequestBody(simplePublisher); |
| 107 | + simplePublisher.send(currentAsyncRequestBody); |
| 108 | + numAsyncRequestBodiesInFlight.incrementAndGet(); |
| 109 | + checkCompletion(simplePublisher, currentAsyncRequestBody); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void checkCompletion(SimplePublisher<AsyncRequestBody> simplePublisher, AsyncRequestBody currentAsyncRequestBody) { |
| 114 | + long remaining = remainingBytes.addAndGet(-currentAsyncRequestBody.contentLength().get()); |
| 115 | + |
| 116 | + if (remaining == 0) { |
| 117 | + isDone = true; |
| 118 | + simplePublisher.complete(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private AsyncRequestBody newFileAsyncRequestBody(SimplePublisher<AsyncRequestBody> simplePublisher) { |
| 123 | + long position = chunkSize * chunkIndex.getAndIncrement(); |
| 124 | + long numBytesToReadForThisChunk = Math.min(totalContentLength - position, chunkSize); |
| 125 | + FileAsyncRequestBody fileAsyncRequestBody = FileAsyncRequestBody.builder() |
| 126 | + .path(path) |
| 127 | + .position(position) |
| 128 | + .numBytesToRead(numBytesToReadForThisChunk) |
| 129 | + .build(); |
| 130 | + return new AsyncRequestBody() { |
| 131 | + |
| 132 | + @Override |
| 133 | + public void subscribe(Subscriber<? super ByteBuffer> s) { |
| 134 | + fileAsyncRequestBody.doAfterOnComplete(() -> { |
| 135 | + numAsyncRequestBodiesInFlight.decrementAndGet(); |
| 136 | + sendAsyncRequestBody(simplePublisher); |
| 137 | + }).subscribe(s); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Optional<Long> contentLength() { |
| 142 | + return fileAsyncRequestBody.contentLength(); |
| 143 | + } |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Should not send more if it's done OR sending next request body would exceed the total buffer size |
| 149 | + */ |
| 150 | + private boolean shouldSendMore() { |
| 151 | + if (isDone) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + long currentUsedBuffer = numAsyncRequestBodiesInFlight.get() * bufferPerAsyncRequestBody; |
| 156 | + return currentUsedBuffer + bufferPerAsyncRequestBody < totalBufferSize; |
| 157 | + } |
| 158 | + |
| 159 | + @SdkTestInternalApi |
| 160 | + AtomicInteger numAsyncRequestBodiesInFlight() { |
| 161 | + return numAsyncRequestBodiesInFlight; |
| 162 | + } |
| 163 | +} |
0 commit comments