Skip to content

Commit 3e2fdd4

Browse files
committed
feat(gax): enforce global timeout for resumable uploads
Enforces ResumableUploadCallSettings.getGlobalTimeout() across the resumable upload lifecycle. A scheduled task is registered at future initiation using the client context executor, covering both session initiation and chunk upload streaming. On deadline expiry, any in-flight attempt future is cancelled, the payload stream is closed, and the session future fails with a non-retryable DeadlineExceededException. The scheduled timeout task is cancelled cleanly across all terminal outcomes (success, failure, cancellation). Single payload closure is guaranteed via AtomicBoolean compare-and-set, with close errors attached via addSuppressed on failure paths and logged on success paths. Buffer safety is maintained during in-flight timeout cancellation without stream advancement or buffer corruption.
1 parent ea2e309 commit 3e2fdd4

2 files changed

Lines changed: 292 additions & 10 deletions

File tree

‎sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ResumableUploadChunkCoordinator.java‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
import java.io.InputStream;
5353
import java.time.Duration;
5454
import java.util.concurrent.CancellationException;
55+
import java.util.concurrent.ScheduledExecutorService;
56+
import java.util.concurrent.ScheduledFuture;
57+
import java.util.concurrent.TimeUnit;
5558
import org.jspecify.annotations.NullMarked;
5659
import org.jspecify.annotations.Nullable;
5760

@@ -84,6 +87,7 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
8487
private final UnaryCallable<QueryStatusRequest, QueryStatusResponse<ResponseT>>
8588
queryStatusCallable;
8689
private final InputStream payload;
90+
private final ResumableUploadCallSettings settings;
8791
private final int chunkSize;
8892
private final ApiCallContext callContext;
8993
private final ClientContext clientContext;
@@ -101,6 +105,9 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
101105
@GuardedBy("lock")
102106
private @Nullable ApiFuture<?> inFlightFuture;
103107

108+
@GuardedBy("lock")
109+
private @Nullable ScheduledFuture<?> timeoutFuture;
110+
104111
ResumableUploadChunkCoordinator(
105112
SettableApiFuture<ResponseT> result,
106113
ApiFuture<ResumableUploadSession> startFuture,
@@ -139,7 +146,7 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
139146
this.queryStatusCallable =
140147
checkNotNull(queryStatusCallable, "queryStatusCallable must not be null");
141148
this.payload = checkNotNull(payload, "payload must not be null");
142-
checkNotNull(settings, "settings must not be null");
149+
this.settings = checkNotNull(settings, "settings must not be null");
143150
checkArgument(settings.getChunkSize() > 0, "chunkSize must be > 0");
144151
this.chunkSize = settings.getChunkSize();
145152
this.callContext = checkNotNull(callContext, "callContext must not be null");
@@ -152,6 +159,19 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
152159
}
153160

154161
void start() {
162+
Duration timeout = settings.getGlobalTimeout();
163+
if (timeout != null && !timeout.isZero() && !timeout.isNegative()) {
164+
ScheduledExecutorService executor = clientContext.getExecutor();
165+
if (executor != null) {
166+
synchronized (lock) {
167+
if (!done) {
168+
this.timeoutFuture =
169+
executor.schedule(this::onTimeout, timeout.toMillis(), TimeUnit.MILLISECONDS);
170+
}
171+
}
172+
}
173+
}
174+
155175
ApiFutures.addCallback(
156176
startFuture,
157177
new ApiFutureCallback<ResumableUploadSession>() {
@@ -178,6 +198,32 @@ public void onFailure(Throwable t) {
178198
MoreExecutors.directExecutor());
179199
}
180200

201+
private static final StatusCode TIMEOUT_STATUS_CODE =
202+
new StatusCode() {
203+
@Override
204+
public StatusCode.Code getCode() {
205+
return StatusCode.Code.DEADLINE_EXCEEDED;
206+
}
207+
208+
@Override
209+
public @Nullable Object getTransportCode() {
210+
return null;
211+
}
212+
};
213+
214+
private void onTimeout() {
215+
synchronized (lock) {
216+
if (done) {
217+
return;
218+
}
219+
}
220+
String message =
221+
uploadSessionUrl != null
222+
? "Resumable upload timed out for session: " + uploadSessionUrl
223+
: "Resumable upload timed out before session initiation completed";
224+
finish(null, new DeadlineExceededException(message, null, TIMEOUT_STATUS_CODE, false));
225+
}
226+
181227
@Nullable String getUploadSessionUrl() {
182228
return uploadSessionUrl;
183229
}
@@ -186,7 +232,7 @@ void setInFlightFuture(ApiFuture<?> future) {
186232
boolean shouldCancel = false;
187233
synchronized (lock) {
188234
if (done) {
189-
shouldCancel = result.isCancelled();
235+
shouldCancel = true;
190236
} else {
191237
this.inFlightFuture = future;
192238
}
@@ -197,6 +243,7 @@ void setInFlightFuture(ApiFuture<?> future) {
197243
}
198244

199245
void cancel(boolean mayInterruptIfRunning) {
246+
ScheduledFuture<?> timeout;
200247
ApiFuture<?> inFlight;
201248
synchronized (lock) {
202249
if (done) {
@@ -205,6 +252,11 @@ void cancel(boolean mayInterruptIfRunning) {
205252
done = true;
206253
inFlight = this.inFlightFuture;
207254
this.inFlightFuture = null;
255+
timeout = this.timeoutFuture;
256+
this.timeoutFuture = null;
257+
}
258+
if (timeout != null) {
259+
timeout.cancel(false);
208260
}
209261
if (inFlight != null) {
210262
inFlight.cancel(mayInterruptIfRunning);
@@ -213,12 +265,23 @@ void cancel(boolean mayInterruptIfRunning) {
213265
}
214266

215267
private void finish(@Nullable ResponseT response, @Nullable Throwable error) {
268+
ScheduledFuture<?> timeout;
269+
ApiFuture<?> inFlight;
216270
synchronized (lock) {
217271
if (done) {
218272
return;
219273
}
220274
done = true;
275+
inFlight = inFlightFuture;
221276
inFlightFuture = null;
277+
timeout = timeoutFuture;
278+
timeoutFuture = null;
279+
}
280+
if (timeout != null) {
281+
timeout.cancel(false);
282+
}
283+
if (inFlight != null) {
284+
inFlight.cancel(true);
222285
}
223286
IOException closeError = closePayload();
224287
if (error == null) {

0 commit comments

Comments
 (0)