|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.rpc; |
| 31 | + |
| 32 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | + |
| 34 | +import com.google.api.core.ApiFuture; |
| 35 | +import com.google.api.core.ApiFutureCallback; |
| 36 | +import com.google.api.core.ApiFutures; |
| 37 | +import com.google.api.gax.resumable.ChunkUploadRequest; |
| 38 | +import com.google.api.gax.resumable.ChunkUploadResponse; |
| 39 | +import com.google.api.gax.retrying.NonCancellableFuture; |
| 40 | +import com.google.api.gax.retrying.RetryingFuture; |
| 41 | +import com.google.common.util.concurrent.MoreExecutors; |
| 42 | +import java.time.Duration; |
| 43 | +import java.util.concurrent.Callable; |
| 44 | +import org.jspecify.annotations.NullMarked; |
| 45 | +import org.jspecify.annotations.Nullable; |
| 46 | + |
| 47 | +/** |
| 48 | + * A {@link Callable} representing an attempt to transmit a single chunk in a resumable upload |
| 49 | + * session. Used with {@link com.google.api.gax.retrying.ScheduledRetryingExecutor}. |
| 50 | + * |
| 51 | + * <p>Execution follows the standard six-step attempt template with an initial attempt preparation |
| 52 | + * seam. The callable never blocks on {@code .get()}; results and cancellations propagate |
| 53 | + * asynchronously. |
| 54 | + * |
| 55 | + * @param <ResponseT> the type of the final response message once the upload completes |
| 56 | + */ |
| 57 | +@NullMarked |
| 58 | +class ChunkAttemptCallable<ResponseT> implements Callable<ChunkUploadResponse<ResponseT>> { |
| 59 | + |
| 60 | + private final UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> |
| 61 | + uploadChunkCallable; |
| 62 | + private final ChunkUploadRequest request; |
| 63 | + private final ApiCallContext originalCallContext; |
| 64 | + private final UploadCommand command; |
| 65 | + |
| 66 | + private volatile @Nullable RetryingFuture<ChunkUploadResponse<ResponseT>> retryingFuture; |
| 67 | + private volatile @Nullable Throwable lastFailure; |
| 68 | + |
| 69 | + ChunkAttemptCallable( |
| 70 | + UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable, |
| 71 | + ChunkUploadRequest request, |
| 72 | + ApiCallContext callContext, |
| 73 | + UploadCommand command) { |
| 74 | + this.uploadChunkCallable = |
| 75 | + checkNotNull(uploadChunkCallable, "uploadChunkCallable must not be null"); |
| 76 | + this.request = checkNotNull(request, "request must not be null"); |
| 77 | + this.originalCallContext = checkNotNull(callContext, "callContext must not be null"); |
| 78 | + this.command = checkNotNull(command, "command must not be null"); |
| 79 | + } |
| 80 | + |
| 81 | + void setRetryingFuture(RetryingFuture<ChunkUploadResponse<ResponseT>> retryingFuture) { |
| 82 | + this.retryingFuture = checkNotNull(retryingFuture, "retryingFuture must not be null"); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Pre-attempt hook called before each transmission attempt. |
| 87 | + * |
| 88 | + * <p>In this phase, Category 2 (recoverable) errors throw to fail fast. Subsequent phases expand |
| 89 | + * this seam into the query status -> realign buffer -> top up recovery sequence. |
| 90 | + */ |
| 91 | + void prepareAttempt() { |
| 92 | + if (lastFailure != null) { |
| 93 | + UploadErrorCategory category = UploadErrorClassifier.classify(lastFailure, command); |
| 94 | + if (category == UploadErrorCategory.RECOVERABLE) { |
| 95 | + throw new UnsupportedOperationException( |
| 96 | + "Category 2 (recoverable) error recovery is not yet implemented", lastFailure); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public @Nullable ChunkUploadResponse<ResponseT> call() { |
| 103 | + RetryingFuture<ChunkUploadResponse<ResponseT>> currentRetryingFuture = |
| 104 | + checkNotNull(retryingFuture, "retryingFuture must be set before call()"); |
| 105 | + ApiCallContext attemptContext = originalCallContext; |
| 106 | + |
| 107 | + try { |
| 108 | + // Seam for recoverable error handling. |
| 109 | + prepareAttempt(); |
| 110 | + |
| 111 | + // Set the RPC timeout if caller did not provide their own. |
| 112 | + Duration rpcTimeout = currentRetryingFuture.getAttemptSettings().getRpcTimeoutDuration(); |
| 113 | + if (!rpcTimeout.isZero() && attemptContext.getTimeoutDuration() == null) { |
| 114 | + attemptContext = attemptContext.withTimeoutDuration(rpcTimeout); |
| 115 | + } |
| 116 | + |
| 117 | + // Placeholder non-cancellable future. |
| 118 | + currentRetryingFuture.setAttemptFuture( |
| 119 | + new NonCancellableFuture<ChunkUploadResponse<ResponseT>>()); |
| 120 | + |
| 121 | + // Early exit if retryingFuture was already cancelled or completed. |
| 122 | + if (currentRetryingFuture.isDone()) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + // Dispatch chunk upload and wire cancellation propagation and error |
| 127 | + // tracking. |
| 128 | + attemptContext |
| 129 | + .getTracer() |
| 130 | + .attemptStarted( |
| 131 | + request, currentRetryingFuture.getAttemptSettings().getOverallAttemptCount()); |
| 132 | + |
| 133 | + ApiFuture<ChunkUploadResponse<ResponseT>> internalFuture = |
| 134 | + uploadChunkCallable.futureCall(request, attemptContext); |
| 135 | + |
| 136 | + // Propagate cancellation to the feeder future immediately. |
| 137 | + currentRetryingFuture.addListener( |
| 138 | + () -> { |
| 139 | + if (currentRetryingFuture.isCancelled()) { |
| 140 | + internalFuture.cancel(true); |
| 141 | + } |
| 142 | + }, |
| 143 | + MoreExecutors.directExecutor()); |
| 144 | + |
| 145 | + ApiFutures.addCallback( |
| 146 | + internalFuture, |
| 147 | + new ApiFutureCallback<ChunkUploadResponse<ResponseT>>() { |
| 148 | + @Override |
| 149 | + public void onSuccess(ChunkUploadResponse<ResponseT> response) { |
| 150 | + lastFailure = null; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onFailure(Throwable t) { |
| 155 | + lastFailure = t; |
| 156 | + } |
| 157 | + }, |
| 158 | + MoreExecutors.directExecutor()); |
| 159 | + |
| 160 | + currentRetryingFuture.setAttemptFuture(internalFuture); |
| 161 | + } catch (Throwable e) { |
| 162 | + lastFailure = e; |
| 163 | + currentRetryingFuture.setAttemptFuture( |
| 164 | + ApiFutures.<ChunkUploadResponse<ResponseT>>immediateFailedFuture(e)); |
| 165 | + } |
| 166 | + |
| 167 | + return null; |
| 168 | + } |
| 169 | +} |
0 commit comments