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 ;
@@ -69,7 +71,10 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
6971 queryStatusCallable ;
7072 private final String uploadUrl ;
7173 private final RewindableStreamBuffer buffer ;
74+ private final ResumableUploadCallSettings settings ;
75+ private final long deadlineNanos ;
7276 private final ApiCallContext callContext ;
77+ private final ClientContext clientContext ;
7378 private final SettableApiFuture <ResponseT > result = SettableApiFuture .create ();
7479 private volatile @ Nullable ApiFuture <?> currentChunkFuture ;
7580
@@ -78,16 +83,20 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
7883 UnaryCallable <QueryStatusRequest , QueryStatusResponse <ResponseT >> queryStatusCallable ,
7984 String uploadUrl ,
8085 InputStream payload ,
81- int chunkSize ,
82- ApiCallContext callContext ) {
86+ ResumableUploadCallSettings settings ,
87+ long deadlineNanos ,
88+ ClientContext clientContext ) {
8389 this .uploadChunkCallable =
8490 checkNotNull (uploadChunkCallable , "uploadChunkCallable must not be null" );
8591 this .queryStatusCallable =
8692 checkNotNull (queryStatusCallable , "queryStatusCallable must not be null" );
8793 this .uploadUrl = checkNotNull (uploadUrl , "uploadUrl must not be null" );
8894 checkNotNull (payload , "payload must not be null" );
89- this .callContext = checkNotNull (callContext , "callContext must not be null" );
90- this .buffer = new RewindableStreamBuffer (payload , chunkSize , uploadUrl );
95+ this .settings = checkNotNull (settings , "settings must not be null" );
96+ this .deadlineNanos = deadlineNanos ;
97+ this .clientContext = checkNotNull (clientContext , "clientContext must not be null" );
98+ this .callContext = clientContext .getDefaultCallContext ();
99+ this .buffer = new RewindableStreamBuffer (payload , settings .getChunkSize (), uploadUrl );
91100 }
92101
93102 ApiFuture <ResponseT > start () {
@@ -123,7 +132,7 @@ private void dispatchCurrentChunk() {
123132
124133 // Dispatch the chunk upload call and register the in-flight future for cancellation.
125134 ApiFuture <ChunkUploadResponse <ResponseT >> chunkFuture =
126- uploadChunkCallable .futureCall (chunkRequest , callContext );
135+ uploadChunkCallable .futureCall (chunkRequest , chunkCallContext () );
127136 if (!tryRegisterInFlight (chunkFuture )) {
128137 return ;
129138 }
@@ -168,7 +177,7 @@ private void recover() {
168177 try {
169178 // Dispatch the query status call and register the in-flight future for cancellation.
170179 ApiFuture <QueryStatusResponse <ResponseT >> queryFuture =
171- queryStatusCallable .futureCall (QueryStatusRequest .create (uploadUrl ), callContext );
180+ queryStatusCallable .futureCall (QueryStatusRequest .create (uploadUrl ), queryCallContext () );
172181 if (!tryRegisterInFlight (queryFuture )) {
173182 return ;
174183 }
@@ -271,6 +280,60 @@ public StatusCode.Code getCode() {
271280 }
272281 };
273282
283+ /**
284+ * Returns the call context for a chunk upload, bounding its retry cycle and each of its attempts
285+ * to the chunk's local deadline: at most half the global timeout, never past the global deadline.
286+ */
287+ private ApiCallContext chunkCallContext () {
288+ Duration remaining = remainingGlobalTimeout ();
289+ if (remaining == null ) {
290+ return callContext ;
291+ }
292+ Duration deadline = min (settings .getGlobalTimeout ().dividedBy (2 ), remaining );
293+ return boundedCallContext (
294+ deadline ,
295+ callContext .getRetrySettings ().toBuilder ()
296+ .setInitialRpcTimeoutDuration (deadline )
297+ .setRpcTimeoutMultiplier (1.0 )
298+ .setMaxRpcTimeoutDuration (deadline ));
299+ }
300+
301+ /** Returns the call context for a status query, bounded by the remaining global timeout. */
302+ private ApiCallContext queryCallContext () {
303+ Duration remaining = remainingGlobalTimeout ();
304+ if (remaining == null ) {
305+ return callContext ;
306+ }
307+ RetrySettings baseSettings = callContext .getRetrySettings ();
308+ return boundedCallContext (
309+ remaining ,
310+ baseSettings .toBuilder ()
311+ .setInitialRpcTimeoutDuration (
312+ min (baseSettings .getInitialRpcTimeoutDuration (), remaining ))
313+ .setMaxRpcTimeoutDuration (min (baseSettings .getMaxRpcTimeoutDuration (), remaining )));
314+ }
315+
316+ /**
317+ * Returns the time left before the global upload deadline, or null if deadlines cannot be derived
318+ * because no global timeout or no base retry settings were configured.
319+ */
320+ private @ Nullable Duration remainingGlobalTimeout () {
321+ if (settings .getGlobalTimeout () == null || callContext .getRetrySettings () == null ) {
322+ return null ;
323+ }
324+ return Duration .ofNanos (Math .max (1L , deadlineNanos - clientContext .getClock ().nanoTime ()));
325+ }
326+
327+ private ApiCallContext boundedCallContext (Duration deadline , RetrySettings .Builder settings ) {
328+ return callContext
329+ .withRetrySettings (settings .setTotalTimeoutDuration (deadline ).build ())
330+ .withTimeoutDuration (deadline );
331+ }
332+
333+ private static Duration min (Duration left , Duration right ) {
334+ return left .compareTo (right ) <= 0 ? left : right ;
335+ }
336+
274337 private static FailedPreconditionException protocolViolation (String message ) {
275338 return new FailedPreconditionException (message , null , FAILED_PRECONDITION_STATUS_CODE , false );
276339 }
0 commit comments