xref: /aosp_15_r20/external/aws-crt-java/src/main/java/software/amazon/awssdk/crt/s3/S3MetaRequest.java (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 package software.amazon.awssdk.crt.s3;
6 
7 import java.util.concurrent.CompletableFuture;
8 import software.amazon.awssdk.crt.CrtResource;
9 
10 public class S3MetaRequest extends CrtResource {
11 
12     private final CompletableFuture<Void> shutdownComplete = new CompletableFuture<>();
13 
S3MetaRequest()14     public S3MetaRequest() {
15 
16     }
17 
onShutdownComplete()18     private void onShutdownComplete() {
19         releaseReferences();
20 
21         this.shutdownComplete.complete(null);
22     }
23 
24     /**
25      * Determines whether a resource releases its dependencies at the same time the
26      * native handle is released or if it waits. Resources that wait are responsible
27      * for calling releaseReferences() manually.
28      */
29     @Override
canReleaseReferencesImmediately()30     protected boolean canReleaseReferencesImmediately() {
31         return false;
32     }
33 
34     /**
35      * Cleans up the native resources associated with this client. The client is
36      * unusable after this call
37      */
38     @Override
releaseNativeHandle()39     protected void releaseNativeHandle() {
40         if (!isNull()) {
41             s3MetaRequestDestroy(getNativeHandle());
42         }
43     }
44 
setMetaRequestNativeHandle(long nativeHandle)45     void setMetaRequestNativeHandle(long nativeHandle) {
46         acquireNativeHandle(nativeHandle);
47     }
48 
getShutdownCompleteFuture()49     public CompletableFuture<Void> getShutdownCompleteFuture() { return shutdownComplete; }
50 
cancel()51     public void cancel() {
52         if (isNull()) {
53             throw new IllegalStateException("S3MetaRequest has been closed.");
54         }
55         s3MetaRequestCancel(getNativeHandle());
56     }
57 
58     /**
59      * Pauses meta request and returns a token that can be used to resume a meta request.
60      * For PutObject resume, input stream should always start at the beginning,
61      * already uploaded parts will be skipped, but checksums on those will be verified if request specified checksum algo.
62      * @return token to resume request. might be null if request has not started executing yet
63      */
pause()64     public ResumeToken pause() {
65         if (isNull()) {
66             throw new IllegalStateException("S3MetaRequest has been closed.");
67         }
68         return s3MetaRequestPause(getNativeHandle());
69     }
70 
71     /**
72      * Increment the flow-control window, so that response data continues downloading.
73      * <p>
74      * If the client was created with {@link S3ClientOptions#withReadBackpressureEnabled} set true,
75      * each S3MetaRequest has a flow-control window that shrinks as response
76      * body data is downloaded (headers do not affect the size of the window).
77      * {@link S3ClientOptions#withInitialReadWindowSize} sets the starting size for each S3MetaRequest's window.
78      * Whenever the window reaches zero, data stops downloading.
79      * Increment the window to keep data flowing.
80      * Maintain a larger window to keep up a high download throughput,
81      * parts cannot download in parallel unless the window is large enough to hold multiple parts.
82      * Maintain a smaller window to limit the amount of data buffered in memory.
83      * <p>
84      * If backpressure is disabled this call has no effect, data is downloaded as fast as possible.
85      * <p>
86      * WARNING: This feature is experimental.
87      * Currently, backpressure is only applied to GetObject requests which are split into multiple parts,
88      * and you may still receive some data after the window reaches zero.
89      *
90      * @param bytes size to increment window by
91 
92      * @see S3ClientOptions#withReadBackpressureEnabled
93      */
incrementReadWindow(long bytes)94     public void incrementReadWindow(long bytes) {
95         if (isNull()) {
96             throw new IllegalStateException("S3MetaRequest has been closed.");
97         }
98         s3MetaRequestIncrementReadWindow(getNativeHandle(), bytes);
99     }
100 
101     /*******************************************************************************
102      * native methods
103      ******************************************************************************/
s3MetaRequestDestroy(long s3MetaRequest)104     private static native void s3MetaRequestDestroy(long s3MetaRequest);
105 
s3MetaRequestCancel(long s3MetaRequest)106     private static native void s3MetaRequestCancel(long s3MetaRequest);
107 
s3MetaRequestPause(long s3MetaRequest)108     private static native ResumeToken s3MetaRequestPause(long s3MetaRequest);
109 
s3MetaRequestIncrementReadWindow(long s3MetaRequest, long bytes)110     private static native void s3MetaRequestIncrementReadWindow(long s3MetaRequest, long bytes);
111 }
112