|
| 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.core.InternalApi; |
| 38 | +import com.google.api.gax.resumable.ChunkUploadRequest; |
| 39 | +import com.google.api.gax.resumable.ChunkUploadResponse; |
| 40 | +import com.google.common.io.ByteStreams; |
| 41 | +import java.io.IOException; |
| 42 | +import java.io.InputStream; |
| 43 | +import java.util.Arrays; |
| 44 | +import java.util.concurrent.CancellationException; |
| 45 | +import java.util.concurrent.ScheduledExecutorService; |
| 46 | +import org.jspecify.annotations.NullMarked; |
| 47 | + |
| 48 | +/** |
| 49 | + * Coordinates chunk transmission steps of a resumable upload session. |
| 50 | + * |
| 51 | + * @param <ResponseT> the type of the final response message returned once the upload completes |
| 52 | + */ |
| 53 | +@InternalApi |
| 54 | +@NullMarked |
| 55 | +final class ResumableUploadChunkCoordinator<ResponseT> { |
| 56 | + |
| 57 | + private static final byte[] EMPTY_PAYLOAD = new byte[0]; |
| 58 | + |
| 59 | + private final UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> |
| 60 | + uploadChunkCallable; |
| 61 | + private final String uploadUrl; |
| 62 | + private final InputStream payload; |
| 63 | + private final int chunkSize; |
| 64 | + private final ApiCallContext callContext; |
| 65 | + private final ScheduledExecutorService executor; |
| 66 | + private final ResumableUploadFutureImpl<ResponseT> sessionFuture; |
| 67 | + |
| 68 | + ResumableUploadChunkCoordinator( |
| 69 | + UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable, |
| 70 | + String uploadUrl, |
| 71 | + InputStream payload, |
| 72 | + int chunkSize, |
| 73 | + ApiCallContext callContext, |
| 74 | + ScheduledExecutorService executor, |
| 75 | + ResumableUploadFutureImpl<ResponseT> sessionFuture) { |
| 76 | + this.uploadChunkCallable = |
| 77 | + checkNotNull(uploadChunkCallable, "uploadChunkCallable must not be null"); |
| 78 | + this.uploadUrl = checkNotNull(uploadUrl, "uploadUrl must not be null"); |
| 79 | + this.payload = checkNotNull(payload, "payload must not be null"); |
| 80 | + this.chunkSize = chunkSize; |
| 81 | + this.callContext = checkNotNull(callContext, "callContext must not be null"); |
| 82 | + this.executor = checkNotNull(executor, "executor must not be null"); |
| 83 | + this.sessionFuture = checkNotNull(sessionFuture, "sessionFuture must not be null"); |
| 84 | + } |
| 85 | + |
| 86 | + void start() { |
| 87 | + transmitChunk(0L); |
| 88 | + } |
| 89 | + |
| 90 | + private void transmitChunk(long currentOffset) { |
| 91 | + // Abort if the session was already completed or canceled. |
| 92 | + if (sessionFuture.isDone()) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // Read the next chunk slice from the payload stream. |
| 97 | + byte[] buffer = new byte[chunkSize]; |
| 98 | + int bytesRead; |
| 99 | + try { |
| 100 | + bytesRead = ByteStreams.read(payload, buffer, 0, chunkSize); |
| 101 | + } catch (IOException e) { |
| 102 | + sessionFuture.fail(e); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Determine if this is the final chunk and build the chunk request. |
| 107 | + boolean isFinal = bytesRead < chunkSize; |
| 108 | + byte[] chunkPayload; |
| 109 | + if (bytesRead == chunkSize) { |
| 110 | + chunkPayload = buffer; |
| 111 | + } else if (bytesRead == 0) { |
| 112 | + chunkPayload = EMPTY_PAYLOAD; |
| 113 | + } else { |
| 114 | + chunkPayload = Arrays.copyOf(buffer, bytesRead); |
| 115 | + } |
| 116 | + |
| 117 | + ChunkUploadRequest chunkRequest = |
| 118 | + ChunkUploadRequest.newBuilder() |
| 119 | + .setUploadUrl(uploadUrl) |
| 120 | + .setPayload(chunkPayload) |
| 121 | + .setOffset(currentOffset) |
| 122 | + .setFinal(isFinal) |
| 123 | + .build(); |
| 124 | + |
| 125 | + // Dispatch the chunk upload call and register the in-flight future for cancellation. |
| 126 | + long chunkLength = chunkPayload.length; |
| 127 | + try { |
| 128 | + ApiFuture<ChunkUploadResponse<ResponseT>> chunkFuture = |
| 129 | + uploadChunkCallable.futureCall(chunkRequest, callContext); |
| 130 | + sessionFuture.setInFlightFuture(chunkFuture); |
| 131 | + |
| 132 | + // Asynchronously handle the response: complete, fail, or chain the next chunk. |
| 133 | + ApiFutures.addCallback( |
| 134 | + chunkFuture, |
| 135 | + new ApiFutureCallback<ChunkUploadResponse<ResponseT>>() { |
| 136 | + @Override |
| 137 | + public void onSuccess(ChunkUploadResponse<ResponseT> response) { |
| 138 | + if (sessionFuture.isDone()) { |
| 139 | + return; |
| 140 | + } |
| 141 | + long nextOffset = currentOffset + chunkLength; |
| 142 | + if (response.isComplete()) { |
| 143 | + sessionFuture.succeed(response.getResponse()); |
| 144 | + } else if (isFinal) { |
| 145 | + sessionFuture.fail( |
| 146 | + new IllegalStateException( |
| 147 | + "Upload stream ended and final chunk was transmitted, but server returned" |
| 148 | + + " incomplete status")); |
| 149 | + } else { |
| 150 | + transmitChunk(nextOffset); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void onFailure(Throwable t) { |
| 156 | + if (t instanceof CancellationException || sessionFuture.isDone()) { |
| 157 | + return; |
| 158 | + } |
| 159 | + sessionFuture.fail(t); |
| 160 | + } |
| 161 | + }, |
| 162 | + executor); |
| 163 | + } catch (Throwable t) { |
| 164 | + sessionFuture.fail(t); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments