|
41 | 41 | import com.google.api.gax.resumable.QueryStatusRequest; |
42 | 42 | import com.google.api.gax.resumable.QueryStatusResponse; |
43 | 43 | import com.google.api.gax.resumable.ResumableUploadStatus; |
| 44 | +import com.google.api.gax.retrying.RetrySettings; |
44 | 45 | import com.google.api.gax.rpc.ResumableUploadErrorClassifier.Category; |
45 | 46 | import com.google.common.util.concurrent.MoreExecutors; |
46 | 47 | import java.io.IOException; |
47 | 48 | import java.io.InputStream; |
| 49 | +import java.time.Duration; |
48 | 50 | import java.util.concurrent.CancellationException; |
49 | 51 | import java.util.concurrent.Executor; |
50 | 52 | import org.jspecify.annotations.NullMarked; |
@@ -72,25 +74,31 @@ final class ResumableUploadChunkCoordinator<ResponseT> { |
72 | 74 | queryStatusCallable; |
73 | 75 | private final String uploadUrl; |
74 | 76 | private final RewindableStreamBuffer buffer; |
| 77 | + private final ResumableUploadCallSettings settings; |
| 78 | + private final long deadlineNanos; |
75 | 79 | private final ApiCallContext callContext; |
| 80 | + private final ClientContext clientContext; |
76 | 81 | private final SettableApiFuture<ResponseT> result = SettableApiFuture.create(); |
77 | 82 | private volatile @Nullable ApiFuture<?> currentChunkFuture; |
78 | 83 |
|
79 | 84 | ResumableUploadChunkCoordinator( |
80 | 85 | UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable, |
81 | 86 | UnaryCallable<QueryStatusRequest, QueryStatusResponse<ResponseT>> queryStatusCallable, |
82 | 87 | String uploadUrl, |
83 | | - InputStream payload, |
84 | | - int chunkSize, |
85 | | - ApiCallContext callContext) { |
| 88 | + ResumableUploadCallSettings settings, |
| 89 | + long deadlineNanos, |
| 90 | + ClientContext clientContext) { |
86 | 91 | this.uploadChunkCallable = |
87 | 92 | checkNotNull(uploadChunkCallable, "uploadChunkCallable must not be null"); |
88 | 93 | this.queryStatusCallable = |
89 | 94 | checkNotNull(queryStatusCallable, "queryStatusCallable must not be null"); |
90 | 95 | this.uploadUrl = checkNotNull(uploadUrl, "uploadUrl must not be null"); |
91 | 96 | 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); |
94 | 102 | } |
95 | 103 |
|
96 | 104 | ApiFuture<ResponseT> start() { |
@@ -125,7 +133,7 @@ private void dispatchCurrentChunk() { |
125 | 133 | try { |
126 | 134 | ChunkUploadRequest chunkRequest = buildCurrentChunkRequest(); |
127 | 135 | ApiFuture<ChunkUploadResponse<ResponseT>> chunkFuture = |
128 | | - uploadChunkCallable.futureCall(chunkRequest, callContext); |
| 136 | + uploadChunkCallable.futureCall(chunkRequest, chunkCallContext()); |
129 | 137 | this.currentChunkFuture = chunkFuture; |
130 | 138 | if (result.isCancelled()) { |
131 | 139 | chunkFuture.cancel(true); |
@@ -171,7 +179,7 @@ private void recover() { |
171 | 179 | } |
172 | 180 | try { |
173 | 181 | ApiFuture<QueryStatusResponse<ResponseT>> queryFuture = |
174 | | - queryStatusCallable.futureCall(QueryStatusRequest.create(uploadUrl), callContext); |
| 182 | + queryStatusCallable.futureCall(QueryStatusRequest.create(uploadUrl), queryCallContext()); |
175 | 183 | this.currentChunkFuture = queryFuture; |
176 | 184 | if (result.isCancelled()) { |
177 | 185 | queryFuture.cancel(true); |
@@ -268,6 +276,60 @@ public StatusCode.Code getCode() { |
268 | 276 | } |
269 | 277 | }; |
270 | 278 |
|
| 279 | + /** |
| 280 | + * Returns the call context for a chunk upload, bounding its retry cycle and each of its attempts |
| 281 | + * to the chunk's local deadline: at most half the global timeout, never past the global deadline. |
| 282 | + */ |
| 283 | + private ApiCallContext chunkCallContext() { |
| 284 | + Duration remaining = remainingGlobalTimeout(); |
| 285 | + if (remaining == null) { |
| 286 | + return callContext; |
| 287 | + } |
| 288 | + Duration deadline = min(settings.getGlobalTimeout().dividedBy(2), remaining); |
| 289 | + return boundedCallContext( |
| 290 | + deadline, |
| 291 | + callContext.getRetrySettings().toBuilder() |
| 292 | + .setInitialRpcTimeoutDuration(deadline) |
| 293 | + .setRpcTimeoutMultiplier(1.0) |
| 294 | + .setMaxRpcTimeoutDuration(deadline)); |
| 295 | + } |
| 296 | + |
| 297 | + /** Returns the call context for a status query, bounded by the remaining global timeout. */ |
| 298 | + private ApiCallContext queryCallContext() { |
| 299 | + Duration remaining = remainingGlobalTimeout(); |
| 300 | + if (remaining == null) { |
| 301 | + return callContext; |
| 302 | + } |
| 303 | + RetrySettings baseSettings = callContext.getRetrySettings(); |
| 304 | + return boundedCallContext( |
| 305 | + remaining, |
| 306 | + baseSettings.toBuilder() |
| 307 | + .setInitialRpcTimeoutDuration( |
| 308 | + min(baseSettings.getInitialRpcTimeoutDuration(), remaining)) |
| 309 | + .setMaxRpcTimeoutDuration(min(baseSettings.getMaxRpcTimeoutDuration(), remaining))); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Returns the time left before the global upload deadline, or null if deadlines cannot be derived |
| 314 | + * because no global timeout or no base retry settings were configured. |
| 315 | + */ |
| 316 | + private @Nullable Duration remainingGlobalTimeout() { |
| 317 | + if (settings.getGlobalTimeout() == null || callContext.getRetrySettings() == null) { |
| 318 | + return null; |
| 319 | + } |
| 320 | + return Duration.ofNanos(Math.max(1L, deadlineNanos - clientContext.getClock().nanoTime())); |
| 321 | + } |
| 322 | + |
| 323 | + private ApiCallContext boundedCallContext(Duration deadline, RetrySettings.Builder settings) { |
| 324 | + return callContext |
| 325 | + .withRetrySettings(settings.setTotalTimeoutDuration(deadline).build()) |
| 326 | + .withTimeoutDuration(deadline); |
| 327 | + } |
| 328 | + |
| 329 | + private static Duration min(Duration left, Duration right) { |
| 330 | + return left.compareTo(right) <= 0 ? left : right; |
| 331 | + } |
| 332 | + |
271 | 333 | private static FailedPreconditionException protocolViolation(String message) { |
272 | 334 | return new FailedPreconditionException(message, null, FAILED_PRECONDITION_STATUS_CODE, false); |
273 | 335 | } |
|
0 commit comments