Skip to content

Commit d885bb2

Browse files
committed
refactor(gax): invert resumable upload future and coordinator ownership
1 parent 628d442 commit d885bb2

3 files changed

Lines changed: 98 additions & 126 deletions

File tree

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import com.google.api.core.ApiFuture;
3535
import com.google.api.core.ApiFutureCallback;
3636
import com.google.api.core.ApiFutures;
37-
import com.google.api.core.InternalApi;
37+
import com.google.api.core.SettableApiFuture;
3838
import com.google.api.gax.resumable.ChunkUploadRequest;
3939
import com.google.api.gax.resumable.ChunkUploadResponse;
4040
import com.google.api.gax.resumable.ResumableUploadStatus;
@@ -45,13 +45,13 @@
4545
import java.util.Arrays;
4646
import java.util.concurrent.CancellationException;
4747
import org.jspecify.annotations.NullMarked;
48+
import org.jspecify.annotations.Nullable;
4849

4950
/**
5051
* Coordinates chunk transmission steps of a resumable upload session.
5152
*
5253
* @param <ResponseT> the type of the final response message returned once the upload completes
5354
*/
54-
@InternalApi
5555
@NullMarked
5656
final class ResumableUploadChunkCoordinator<ResponseT> {
5757

@@ -64,45 +64,52 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
6464
private final byte[] buffer;
6565
private final int chunkSize;
6666
private final ApiCallContext callContext;
67-
private final ResumableUploadFutureImpl<ResponseT> sessionFuture;
67+
private final SettableApiFuture<ResponseT> result = SettableApiFuture.create();
68+
private volatile @Nullable ApiFuture<?> currentChunkFuture;
6869

6970
ResumableUploadChunkCoordinator(
7071
UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable,
7172
String uploadUrl,
7273
InputStream payload,
7374
int chunkSize,
74-
ApiCallContext callContext,
75-
ResumableUploadFutureImpl<ResponseT> sessionFuture) {
75+
ApiCallContext callContext) {
7676
this.uploadChunkCallable =
7777
checkNotNull(uploadChunkCallable, "uploadChunkCallable must not be null");
7878
this.uploadUrl = checkNotNull(uploadUrl, "uploadUrl must not be null");
7979
this.payload = checkNotNull(payload, "payload must not be null");
8080
this.chunkSize = chunkSize;
8181
this.callContext = checkNotNull(callContext, "callContext must not be null");
82-
this.sessionFuture = checkNotNull(sessionFuture, "sessionFuture must not be null");
8382
this.buffer = new byte[chunkSize];
8483
}
8584

86-
void start() {
85+
ApiFuture<ResponseT> start() {
86+
result.addListener(
87+
() -> {
88+
if (result.isCancelled()) {
89+
ApiFuture<?> inFlight = currentChunkFuture;
90+
if (inFlight != null) {
91+
inFlight.cancel(true);
92+
}
93+
}
94+
},
95+
MoreExecutors.directExecutor());
8796
transmitChunk(0L);
97+
return result;
8898
}
8999

90100
private void transmitChunk(long currentOffset) {
91-
// Abort if the session was already completed or canceled.
92-
if (sessionFuture.isDone()) {
101+
if (result.isDone()) {
93102
return;
94103
}
95104

96-
// Read the next chunk slice from the payload stream.
97105
int bytesRead;
98106
try {
99107
bytesRead = ByteStreams.read(payload, buffer, 0, chunkSize);
100108
} catch (IOException e) {
101-
sessionFuture.fail(e);
109+
result.setException(e);
102110
return;
103111
}
104112

105-
// Determine if this is the final chunk and build the chunk request.
106113
boolean isFinal = bytesRead < chunkSize;
107114
byte[] chunkPayload;
108115
if (bytesRead == chunkSize) {
@@ -121,27 +128,25 @@ private void transmitChunk(long currentOffset) {
121128
.setFinal(isFinal)
122129
.build();
123130

124-
// Dispatch the chunk upload call and register the in-flight future for cancellation.
125131
long chunkLength = chunkPayload.length;
126132
try {
127133
ApiFuture<ChunkUploadResponse<ResponseT>> chunkFuture =
128134
uploadChunkCallable.futureCall(chunkRequest, callContext);
129-
sessionFuture.setInFlightFuture(chunkFuture);
135+
this.currentChunkFuture = chunkFuture;
130136

131-
// Asynchronously handle the response: complete, fail, or chain the next chunk.
132137
ApiFutures.addCallback(
133138
chunkFuture,
134139
new ApiFutureCallback<ChunkUploadResponse<ResponseT>>() {
135140
@Override
136141
public void onSuccess(ChunkUploadResponse<ResponseT> response) {
137-
if (sessionFuture.isDone()) {
142+
if (result.isDone()) {
138143
return;
139144
}
140145
long nextOffset = currentOffset + chunkLength;
141146
if (response.getUploadStatus() == ResumableUploadStatus.FINAL) {
142-
sessionFuture.succeed(response.getResponse());
147+
result.set(response.getResponse());
143148
} else if (isFinal) {
144-
sessionFuture.fail(
149+
result.setException(
145150
new IllegalStateException(
146151
"Upload stream ended and final chunk was transmitted, but server returned"
147152
+ " incomplete status"));
@@ -152,15 +157,15 @@ public void onSuccess(ChunkUploadResponse<ResponseT> response) {
152157

153158
@Override
154159
public void onFailure(Throwable t) {
155-
if (t instanceof CancellationException || sessionFuture.isDone()) {
160+
if (t instanceof CancellationException || result.isDone()) {
156161
return;
157162
}
158-
sessionFuture.fail(t);
163+
result.setException(t);
159164
}
160165
},
161166
MoreExecutors.directExecutor());
162167
} catch (Throwable t) {
163-
sessionFuture.fail(t);
168+
result.setException(t);
164169
}
165170
}
166171
}

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

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ final class ResumableUploadFutureImpl<ResponseT> implements ResumableUploadFutur
7272

7373
private volatile @Nullable String uploadSessionUrl;
7474

75+
@GuardedBy("lock")
76+
private boolean done;
77+
78+
@GuardedBy("lock")
79+
private boolean payloadClosed;
80+
7581
@GuardedBy("lock")
7682
private @Nullable ApiFuture<?> inFlightFuture;
7783

@@ -91,15 +97,11 @@ static <ResponseT> ResumableUploadFutureImpl<ResponseT> create(
9197
ResumableUploadFutureImpl<ResponseT> future =
9298
new ResumableUploadFutureImpl<>(
9399
startFuture, uploadChunkCallable, payload, settings, callContext);
94-
try {
95-
future.start();
96-
} catch (Throwable t) {
97-
future.fail(t);
98-
}
100+
future.start();
99101
return future;
100102
}
101103

102-
private ResumableUploadFutureImpl(
104+
ResumableUploadFutureImpl(
103105
ApiFuture<ResumableUploadSession> startFuture,
104106
UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable,
105107
InputStream payload,
@@ -112,84 +114,94 @@ private ResumableUploadFutureImpl(
112114
this.settings = checkNotNull(settings, "settings must not be null");
113115
checkArgument(settings.getChunkSize() > 0, "chunkSize must be > 0");
114116
this.callContext = checkNotNull(callContext, "callContext must not be null");
115-
this.inFlightFuture = startFuture;
117+
synchronized (lock) {
118+
this.inFlightFuture = startFuture;
119+
}
116120
}
117121

118-
private void start() {
122+
void start() {
119123
ApiFutures.addCallback(
120124
startFuture,
121125
new ApiFutureCallback<ResumableUploadSession>() {
122126
@Override
123127
public void onSuccess(ResumableUploadSession session) {
124-
if (resultFuture.isDone()) {
125-
return;
126-
}
127-
uploadSessionUrl = session.getUploadUrl();
128-
ResumableUploadChunkCoordinator<ResponseT> coordinator =
129-
new ResumableUploadChunkCoordinator<>(
130-
uploadChunkCallable,
131-
session.getUploadUrl(),
132-
payload,
133-
settings.getChunkSize(),
134-
callContext,
135-
ResumableUploadFutureImpl.this);
136-
try {
137-
coordinator.start();
138-
} catch (Throwable t) {
139-
fail(t);
128+
ApiFuture<ResponseT> uploadFuture;
129+
synchronized (lock) {
130+
if (done) {
131+
return;
132+
}
133+
uploadSessionUrl = session.getUploadUrl();
134+
ResumableUploadChunkCoordinator<ResponseT> coordinator =
135+
new ResumableUploadChunkCoordinator<>(
136+
uploadChunkCallable,
137+
uploadSessionUrl,
138+
payload,
139+
settings.getChunkSize(),
140+
callContext);
141+
uploadFuture = coordinator.start();
142+
inFlightFuture = uploadFuture;
140143
}
144+
ApiFutures.addCallback(
145+
uploadFuture,
146+
new ApiFutureCallback<ResponseT>() {
147+
@Override
148+
public void onSuccess(ResponseT response) {
149+
finish(response, null);
150+
}
151+
152+
@Override
153+
public void onFailure(Throwable t) {
154+
if (t instanceof CancellationException) {
155+
return;
156+
}
157+
finish(null, t);
158+
}
159+
},
160+
MoreExecutors.directExecutor());
141161
}
142162

143163
@Override
144164
public void onFailure(Throwable t) {
145-
if (t instanceof CancellationException || resultFuture.isDone()) {
165+
if (t instanceof CancellationException) {
146166
return;
147167
}
148-
fail(t);
168+
finish(null, t);
149169
}
150170
},
151171
MoreExecutors.directExecutor());
152172
}
153173

154-
/**
155-
* Registers the active in-flight future for cancellation. If this session future has already been
156-
* canceled, the supplied future is canceled immediately.
157-
*/
158-
void setInFlightFuture(ApiFuture<?> inFlightFuture) {
159-
boolean shouldCancel = false;
174+
private void finish(@Nullable ResponseT response, @Nullable Throwable error) {
160175
synchronized (lock) {
161-
if (resultFuture.isDone()) {
162-
shouldCancel = resultFuture.isCancelled();
163-
} else {
164-
this.inFlightFuture = inFlightFuture;
176+
if (done) {
177+
return;
165178
}
166-
}
167-
if (shouldCancel) {
168-
inFlightFuture.cancel(true);
169-
}
170-
}
171-
172-
void succeed(@Nullable ResponseT result) {
173-
synchronized (lock) {
179+
done = true;
174180
inFlightFuture = null;
175181
}
176-
closePayload();
177-
resultFuture.set(result);
182+
IOException closeError = closePayload();
183+
if (error == null) {
184+
resultFuture.set(response);
185+
} else {
186+
if (closeError != null) {
187+
error.addSuppressed(closeError);
188+
}
189+
resultFuture.setException(error);
190+
}
178191
}
179192

180-
void fail(Throwable t) {
193+
private @Nullable IOException closePayload() {
181194
synchronized (lock) {
182-
inFlightFuture = null;
195+
if (payloadClosed) {
196+
return null;
197+
}
198+
payloadClosed = true;
183199
}
184-
closePayload();
185-
resultFuture.setException(t);
186-
}
187-
188-
private void closePayload() {
189200
try {
190201
payload.close();
191-
} catch (IOException ignored) {
192-
// Suppressed during stream cleanup
202+
return null;
203+
} catch (IOException e) {
204+
return e;
193205
}
194206
}
195207

@@ -205,18 +217,20 @@ public void addListener(Runnable listener, Executor executor) {
205217

206218
@Override
207219
public boolean cancel(boolean mayInterruptIfRunning) {
208-
boolean cancelled;
209220
ApiFuture<?> inFlight;
210221
synchronized (lock) {
211-
cancelled = resultFuture.cancel(mayInterruptIfRunning);
222+
if (done) {
223+
return resultFuture.isCancelled();
224+
}
225+
done = true;
212226
inFlight = this.inFlightFuture;
213227
this.inFlightFuture = null;
214228
}
215229
if (inFlight != null) {
216230
inFlight.cancel(mayInterruptIfRunning);
217231
}
218232
closePayload();
219-
return cancelled;
233+
return resultFuture.cancel(mayInterruptIfRunning);
220234
}
221235

222236
@Override

‎sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/rpc/ResumableUploadCallableImplTest.java‎

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,6 @@ void testUploadCallable_cancelInFlight_haltsUpload() throws Exception {
229229
assertThrows(CancellationException.class, future::get);
230230
}
231231

232-
@Test
233-
void testUploadCallable_setInFlightFutureAfterCancel_immediatelyCancelsFuture() {
234-
SettableApiFuture<ResumableUploadSession> startFuture = SettableApiFuture.create();
235-
when(mockStartCallable.futureCall(any(), any())).thenReturn(startFuture);
236-
ResumableUploadFuture<String> future =
237-
callable.futureCall("resource-path", streamOf("data"), null);
238-
assertThat(future.cancel(true)).isTrue();
239-
assertThat(future.isCancelled()).isTrue();
240-
241-
SettableApiFuture<String> lateFuture = SettableApiFuture.create();
242-
((ResumableUploadFutureImpl<String>) future).setInFlightFuture(lateFuture);
243-
assertThat(lateFuture.isCancelled()).isTrue();
244-
}
245-
246232
@Test
247233
void testUploadCallable_startFailure_failsFuture() {
248234
when(mockStartCallable.futureCall(any(), any()))
@@ -297,39 +283,6 @@ void testUploadCallable_closesPayloadOnFailure() {
297283
assertThat(stream.closed).isTrue();
298284
}
299285

300-
@Test
301-
void testUploadCallable_closesPayloadOnCancel() throws Exception {
302-
stubStartSession("https://upload.url/close-cancel");
303-
CountDownLatch chunkStarted = new CountDownLatch(1);
304-
when(mockChunkCallable.futureCall(any(ChunkUploadRequest.class), any()))
305-
.thenAnswer(
306-
inv -> {
307-
chunkStarted.countDown();
308-
return SettableApiFuture.create();
309-
});
310-
311-
TrackableStream stream = new TrackableStream("data");
312-
ResumableUploadFuture<String> future = callable.futureCall("resource-path", stream, null);
313-
assertThat(chunkStarted.await(5, TimeUnit.SECONDS)).isTrue();
314-
future.cancel(true);
315-
316-
assertThat(stream.closed).isTrue();
317-
}
318-
319-
@Test
320-
void testUploadCallable_closesPayloadOnStartSyncFailure() {
321-
when(mockStartCallable.futureCall(any(), any()))
322-
.thenThrow(new RuntimeException("sync start failure"));
323-
324-
TrackableStream stream = new TrackableStream("data");
325-
ResumableUploadFuture<String> future = callable.futureCall("resource-path", stream, null);
326-
327-
ExecutionException exception = assertThrows(ExecutionException.class, future::get);
328-
assertThat(exception.getCause()).isInstanceOf(RuntimeException.class);
329-
assertThat(exception.getCause()).hasMessageThat().contains("sync start failure");
330-
assertThat(stream.closed).isTrue();
331-
}
332-
333286
@Test
334287
void testUploadCallable_withApiCallContext_mergesAndPassesContext() throws Exception {
335288
stubStartSession("https://upload.url/context");

0 commit comments

Comments
 (0)