4141import com .google .api .gax .resumable .QueryStatusRequest ;
4242import com .google .api .gax .resumable .QueryStatusResponse ;
4343import com .google .api .gax .resumable .ResumableUploadStatus ;
44+ import com .google .api .gax .retrying .RetrySettings ;
4445import com .google .api .gax .rpc .ResumableUploadErrorClassifier .Category ;
4546import com .google .common .util .concurrent .MoreExecutors ;
4647import java .io .IOException ;
4748import java .io .InputStream ;
49+ import java .time .Duration ;
4850import java .util .concurrent .CancellationException ;
4951import java .util .concurrent .Executor ;
5052import org .jspecify .annotations .NullMarked ;
@@ -72,25 +74,31 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
7274 queryStatusCallable ;
7375 private final String uploadUrl ;
7476 private final RewindableStreamBuffer buffer ;
77+ private final ResumableUploadCallSettings settings ;
78+ private final long deadlineNanos ;
7579 private final ApiCallContext callContext ;
80+ private final ClientContext clientContext ;
7681 private final SettableApiFuture <ResponseT > result = SettableApiFuture .create ();
7782 private volatile @ Nullable ApiFuture <?> currentChunkFuture ;
7883
7984 ResumableUploadChunkCoordinator (
8085 UnaryCallable <ChunkUploadRequest , ChunkUploadResponse <ResponseT >> uploadChunkCallable ,
8186 UnaryCallable <QueryStatusRequest , QueryStatusResponse <ResponseT >> queryStatusCallable ,
8287 String uploadUrl ,
83- InputStream payload ,
84- int chunkSize ,
85- ApiCallContext callContext ) {
88+ ResumableUploadCallSettings settings ,
89+ long deadlineNanos ,
90+ ClientContext clientContext ) {
8691 this .uploadChunkCallable =
8792 checkNotNull (uploadChunkCallable , "uploadChunkCallable must not be null" );
8893 this .queryStatusCallable =
8994 checkNotNull (queryStatusCallable , "queryStatusCallable must not be null" );
9095 this .uploadUrl = checkNotNull (uploadUrl , "uploadUrl must not be null" );
9196 checkNotNull (payload , "payload must not be null" );
92- this .callContext = checkNotNull (callContext , "callContext must not be null" );
93- this .buffer = new RewindableStreamBuffer (payload , chunkSize , uploadUrl );
97+ this .settings = checkNotNull (settings , "settings must not be null" );
98+ this .deadlineNanos = deadlineNanos ;
99+ this .clientContext = checkNotNull (clientContext , "clientContext must not be null" );
100+ this .callContext = clientContext .getDefaultCallContext ();
101+ this .buffer = new RewindableStreamBuffer (payload , settings .getChunkSize (), uploadUrl );
94102 }
95103
96104 ApiFuture <ResponseT > start () {
@@ -154,7 +162,7 @@ public void onFailure(Throwable t) {
154162 private ApiFuture <ChunkUploadResponse <ResponseT >> executeChunkWithRecovery () {
155163 ApiFuture <ChunkUploadResponse <ResponseT >> transmitted =
156164 ApiFutures .catchingAsync (
157- uploadChunkCallable .futureCall (buildCurrentChunkRequest (), callContext ),
165+ uploadChunkCallable .futureCall (buildCurrentChunkRequest (), chunkCallContext () ),
158166 Throwable .class ,
159167 t -> {
160168 Category category =
@@ -180,7 +188,7 @@ private ApiFuture<ChunkUploadResponse<ResponseT>> executeChunkWithRecovery() {
180188
181189 private ApiFuture <ChunkUploadResponse <ResponseT >> recover () {
182190 return ApiFutures .transformAsync (
183- queryStatusCallable .futureCall (QueryStatusRequest .create (uploadUrl ), callContext ),
191+ queryStatusCallable .futureCall (QueryStatusRequest .create (uploadUrl ), queryCallContext () ),
184192 this ::resumeFrom ,
185193 chunkExecutor );
186194 }
@@ -247,6 +255,60 @@ public StatusCode.Code getCode() {
247255 }
248256 };
249257
258+ /**
259+ * Returns the call context for a chunk upload, bounding its retry cycle and each of its attempts
260+ * to the chunk's local deadline: at most half the global timeout, never past the global deadline.
261+ */
262+ private ApiCallContext chunkCallContext () {
263+ Duration remaining = remainingGlobalTimeout ();
264+ if (remaining == null ) {
265+ return callContext ;
266+ }
267+ Duration deadline = min (settings .getGlobalTimeout ().dividedBy (2 ), remaining );
268+ return boundedCallContext (
269+ deadline ,
270+ callContext .getRetrySettings ().toBuilder ()
271+ .setInitialRpcTimeoutDuration (deadline )
272+ .setRpcTimeoutMultiplier (1.0 )
273+ .setMaxRpcTimeoutDuration (deadline ));
274+ }
275+
276+ /** Returns the call context for a status query, bounded by the remaining global timeout. */
277+ private ApiCallContext queryCallContext () {
278+ Duration remaining = remainingGlobalTimeout ();
279+ if (remaining == null ) {
280+ return callContext ;
281+ }
282+ RetrySettings baseSettings = callContext .getRetrySettings ();
283+ return boundedCallContext (
284+ remaining ,
285+ baseSettings .toBuilder ()
286+ .setInitialRpcTimeoutDuration (
287+ min (baseSettings .getInitialRpcTimeoutDuration (), remaining ))
288+ .setMaxRpcTimeoutDuration (min (baseSettings .getMaxRpcTimeoutDuration (), remaining )));
289+ }
290+
291+ /**
292+ * Returns the time left before the global upload deadline, or null if deadlines cannot be derived
293+ * because no global timeout or no base retry settings were configured.
294+ */
295+ private @ Nullable Duration remainingGlobalTimeout () {
296+ if (settings .getGlobalTimeout () == null || callContext .getRetrySettings () == null ) {
297+ return null ;
298+ }
299+ return Duration .ofNanos (Math .max (1L , deadlineNanos - clientContext .getClock ().nanoTime ()));
300+ }
301+
302+ private ApiCallContext boundedCallContext (Duration deadline , RetrySettings .Builder settings ) {
303+ return callContext
304+ .withRetrySettings (settings .setTotalTimeoutDuration (deadline ).build ())
305+ .withTimeoutDuration (deadline );
306+ }
307+
308+ private static Duration min (Duration left , Duration right ) {
309+ return left .compareTo (right ) <= 0 ? left : right ;
310+ }
311+
250312 private static FailedPreconditionException protocolViolation (String message ) {
251313 return new FailedPreconditionException (message , null , FAILED_PRECONDITION_STATUS_CODE , false );
252314 }
0 commit comments