-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(gax): implement baseline Callable and Future for resumable uploads #14241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.rpc; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import com.google.api.core.InternalApi; | ||
| import com.google.api.gax.resumable.ResumableUploadClient; | ||
| import java.io.InputStream; | ||
| import java.util.concurrent.Executor; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Concrete implementation of {@link ResumableUploadCallable} that coordinates the upload session | ||
| * via {@link ResumableUploadFutureImpl}. | ||
| * | ||
| * @param <RequestT> the type of the initial request message that initiates the upload session | ||
| * @param <ResponseT> the type of the final response message returned once the upload completes | ||
| */ | ||
| @BetaApi | ||
| @InternalApi | ||
| @NullMarked | ||
| public class ResumableUploadCallableImpl<RequestT, ResponseT> | ||
|
Check warning on line 52 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ResumableUploadCallableImpl.java
|
||
| extends ResumableUploadCallable<RequestT, ResponseT> { | ||
|
|
||
| private final ResumableUploadClient<RequestT, ResponseT> client; | ||
| private final ResumableUploadCallSettings defaultCallSettings; | ||
| private final ApiCallContext defaultCallContext; | ||
| private final Executor executor; | ||
|
|
||
| public ResumableUploadCallableImpl( | ||
| ResumableUploadClient<RequestT, ResponseT> client, | ||
| ResumableUploadCallSettings defaultCallSettings, | ||
| ApiCallContext defaultCallContext, | ||
| Executor executor) { | ||
| this.client = checkNotNull(client, "client must not be null"); | ||
| this.defaultCallSettings = | ||
| checkNotNull(defaultCallSettings, "defaultCallSettings must not be null"); | ||
| this.defaultCallContext = | ||
| checkNotNull(defaultCallContext, "defaultCallContext must not be null"); | ||
| this.executor = checkNotNull(executor, "executor must not be null"); | ||
| } | ||
|
|
||
| @Override | ||
| public ResumableUploadFuture<ResponseT> futureCall( | ||
| RequestT request, InputStream payload, @Nullable ResumableUploadCallSettings settings) { | ||
| checkNotNull(request, "request must not be null"); | ||
| checkNotNull(payload, "payload must not be null"); | ||
| ResumableUploadCallSettings effectiveSettings = defaultCallSettings.merge(settings); | ||
|
|
||
| return ResumableUploadFutureImpl.create( | ||
| client, request, payload, effectiveSettings.getChunkSize(), defaultCallContext, executor); | ||
| } | ||
|
|
||
| @Override | ||
| public ResumableUploadFuture<ResponseT> resumeCall( | ||
| String sessionUrl, InputStream payload, @Nullable ResumableUploadCallSettings settings) { | ||
| throw new UnsupportedOperationException("Session resumption is not yet implemented."); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.rpc; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.core.ApiFutures; | ||
| import com.google.api.gax.resumable.ChunkUploadRequest; | ||
| import com.google.api.gax.resumable.ResumableUploadClient; | ||
| import com.google.common.io.ByteStreams; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Arrays; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Implementation of {@link ResumableUploadFuture} that coordinates session initiation and chunk | ||
| * streaming. | ||
| * | ||
| * @param <ResponseT> the type of the final response message returned once the upload completes | ||
| */ | ||
| @NullMarked | ||
| final class ResumableUploadFutureImpl<ResponseT> implements ResumableUploadFuture<ResponseT> { | ||
|
Check warning on line 58 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ResumableUploadFutureImpl.java
|
||
|
|
||
| private static final byte[] EMPTY_PAYLOAD = new byte[0]; | ||
|
|
||
| private final InputStream payload; | ||
| private final AtomicReference<@Nullable String> uploadSessionUrl; | ||
| private final ApiFuture<ResponseT> delegate; | ||
|
|
||
| /** | ||
| * Initiates a new resumable upload session. | ||
| * | ||
| * <p>The provided {@code payload} stream is managed by the returned future and will be closed | ||
| * automatically upon completion, failure, or cancellation. | ||
| */ | ||
| static <RequestT, ResponseT> ResumableUploadFutureImpl<ResponseT> create( | ||
|
Check warning on line 72 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ResumableUploadFutureImpl.java
|
||
| ResumableUploadClient<RequestT, ResponseT> client, | ||
| RequestT initialRequest, | ||
| InputStream payload, | ||
| int chunkSize, | ||
| ApiCallContext callContext, | ||
| Executor executor) { | ||
| checkNotNull(client, "client must not be null"); | ||
| checkNotNull(initialRequest, "initialRequest must not be null"); | ||
| checkNotNull(payload, "payload must not be null"); | ||
| checkArgument(chunkSize > 0, "chunkSize must be > 0"); | ||
| checkNotNull(callContext, "callContext must not be null"); | ||
| checkNotNull(executor, "executor must not be null"); | ||
|
|
||
| AtomicReference<@Nullable String> uploadSessionUrl = new AtomicReference<>(); | ||
|
|
||
| ApiFuture<ResponseT> sessionFuture; | ||
| try { | ||
| // Initiate upload session asynchronously, then chain into the chunk transmission loop. | ||
| sessionFuture = | ||
| ApiFutures.transformAsync( | ||
| client.startUploadCallable().futureCall(initialRequest, callContext), | ||
| session -> { | ||
| String url = session.getUploadUrl(); | ||
| uploadSessionUrl.set(url); | ||
| // Begin transmitting chunks starting at byte offset 0. | ||
| return transmitChunks(client, payload, chunkSize, callContext, url, 0L, executor); | ||
| }, | ||
| executor); | ||
| sessionFuture.addListener(() -> closePayload(payload), executor); | ||
| } catch (Exception e) { | ||
| closePayload(payload); | ||
| sessionFuture = ApiFutures.immediateFailedFuture(e); | ||
| } | ||
|
|
||
| return new ResumableUploadFutureImpl<>(payload, uploadSessionUrl, sessionFuture); | ||
| } | ||
|
|
||
| private ResumableUploadFutureImpl( | ||
| InputStream payload, | ||
| AtomicReference<@Nullable String> uploadSessionUrl, | ||
| ApiFuture<ResponseT> delegate) { | ||
| this.payload = payload; | ||
| this.uploadSessionUrl = uploadSessionUrl; | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| private static <ResponseT> ApiFuture<ResponseT> transmitChunks( | ||
|
Check warning on line 119 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ResumableUploadFutureImpl.java
|
||
| ResumableUploadClient<?, ResponseT> client, | ||
| InputStream payload, | ||
| int chunkSize, | ||
| ApiCallContext callContext, | ||
| String uploadSessionUrl, | ||
| long bytesUploaded, | ||
| Executor executor) { | ||
| byte[] buffer = new byte[chunkSize]; | ||
| int bytesRead; | ||
| try { | ||
| bytesRead = ByteStreams.read(payload, buffer, 0, chunkSize); | ||
| } catch (IOException e) { | ||
| return ApiFutures.immediateFailedFuture(e); | ||
| } | ||
|
|
||
| boolean isFinal = bytesRead < chunkSize; | ||
| byte[] chunkPayload; | ||
| if (bytesRead == chunkSize) { | ||
| chunkPayload = buffer; | ||
| } else if (bytesRead == 0) { | ||
| chunkPayload = EMPTY_PAYLOAD; | ||
| } else { | ||
| chunkPayload = Arrays.copyOf(buffer, bytesRead); | ||
| } | ||
|
|
||
| ChunkUploadRequest chunkRequest = | ||
| ChunkUploadRequest.newBuilder() | ||
| .setUploadUrl(uploadSessionUrl) | ||
| .setPayload(chunkPayload) | ||
| .setOffset(bytesUploaded) | ||
| .setFinal(isFinal) | ||
| .build(); | ||
|
|
||
| long nextOffset = bytesUploaded + chunkPayload.length; | ||
| return ApiFutures.transformAsync( | ||
| client.uploadChunkCallable().futureCall(chunkRequest, callContext), | ||
| response -> { | ||
| // Terminal success: server finalized upload and returned the completion response. | ||
| if (response.isComplete()) { | ||
| return ApiFutures.immediateFuture(response.getResponse()); | ||
| } | ||
|
whowes marked this conversation as resolved.
|
||
| // Protocol error: payload stream reached EOF but server did not finalize upload. | ||
| if (isFinal) { | ||
| return ApiFutures.immediateFailedFuture( | ||
| new IllegalStateException( | ||
| "Upload stream ended and final chunk was transmitted, but server returned" | ||
| + " incomplete status")); | ||
| } | ||
| // Continuation: asynchronously transmit subsequent chunk with updated offset. | ||
| return transmitChunks( | ||
| client, payload, chunkSize, callContext, uploadSessionUrl, nextOffset, executor); | ||
| }, | ||
| executor); | ||
| } | ||
|
|
||
| private static void closePayload(InputStream payload) { | ||
| try { | ||
| payload.close(); | ||
| } catch (IOException ignored) { | ||
| // Suppressed during stream cleanup | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable String getUploadSessionUrl() { | ||
| return uploadSessionUrl.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public void addListener(Runnable listener, Executor executor) { | ||
| delegate.addListener(listener, executor); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean cancel(boolean mayInterruptIfRunning) { | ||
| boolean cancelled = delegate.cancel(mayInterruptIfRunning); | ||
| closePayload(payload); | ||
| return cancelled; | ||
| } | ||
|
|
||
|
whowes marked this conversation as resolved.
|
||
| @Override | ||
| public boolean isCancelled() { | ||
| return delegate.isCancelled(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isDone() { | ||
| return delegate.isDone(); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseT get() throws InterruptedException, ExecutionException { | ||
| return delegate.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public ResponseT get(long timeout, TimeUnit unit) | ||
| throws InterruptedException, ExecutionException, TimeoutException { | ||
| return delegate.get(timeout, unit); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performing blocking I/O (
ByteStreams.read) inside asynchronous future callbacks (which run on the providedexecutor) can lead to thread starvation or deadlocks if the executor is a direct executor or a limited thread pool (such as gRPC network threads). Consider documenting that theexecutorpassed to the callable must be a dedicated thread pool suitable for blocking I/O operations, or offloading the blocking read to a dedicated I/O executor.