Skip to content

Commit a6d366b

Browse files
committed
refactor(gax): invert resumable upload future and coordinator ownership
1 parent 429933c commit a6d366b

3 files changed

Lines changed: 92 additions & 126 deletions

File tree

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

Lines changed: 20 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;
@@ -51,7 +51,6 @@
5151
*
5252
* @param <ResponseT> the type of the final response message returned once the upload completes
5353
*/
54-
@InternalApi
5554
@NullMarked
5655
final class ResumableUploadChunkCoordinator<ResponseT> {
5756

@@ -64,45 +63,41 @@ final class ResumableUploadChunkCoordinator<ResponseT> {
6463
private final byte[] buffer;
6564
private final int chunkSize;
6665
private final ApiCallContext callContext;
67-
private final ResumableUploadFutureImpl<ResponseT> sessionFuture;
66+
private final SettableApiFuture<ResponseT> result = SettableApiFuture.create();
6867

6968
ResumableUploadChunkCoordinator(
7069
UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable,
7170
String uploadUrl,
7271
InputStream payload,
7372
int chunkSize,
74-
ApiCallContext callContext,
75-
ResumableUploadFutureImpl<ResponseT> sessionFuture) {
73+
ApiCallContext callContext) {
7674
this.uploadChunkCallable =
7775
checkNotNull(uploadChunkCallable, "uploadChunkCallable must not be null");
7876
this.uploadUrl = checkNotNull(uploadUrl, "uploadUrl must not be null");
7977
this.payload = checkNotNull(payload, "payload must not be null");
8078
this.chunkSize = chunkSize;
8179
this.callContext = checkNotNull(callContext, "callContext must not be null");
82-
this.sessionFuture = checkNotNull(sessionFuture, "sessionFuture must not be null");
8380
this.buffer = new byte[chunkSize];
8481
}
8582

86-
void start() {
83+
ApiFuture<ResponseT> start() {
8784
transmitChunk(0L);
85+
return result;
8886
}
8987

9088
private void transmitChunk(long currentOffset) {
91-
// Abort if the session was already completed or canceled.
92-
if (sessionFuture.isDone()) {
89+
if (result.isDone()) {
9390
return;
9491
}
9592

96-
// Read the next chunk slice from the payload stream.
9793
int bytesRead;
9894
try {
9995
bytesRead = ByteStreams.read(payload, buffer, 0, chunkSize);
10096
} catch (IOException e) {
101-
sessionFuture.fail(e);
97+
result.setException(e);
10298
return;
10399
}
104100

105-
// Determine if this is the final chunk and build the chunk request.
106101
boolean isFinal = bytesRead < chunkSize;
107102
byte[] chunkPayload;
108103
if (bytesRead == chunkSize) {
@@ -121,27 +116,31 @@ private void transmitChunk(long currentOffset) {
121116
.setFinal(isFinal)
122117
.build();
123118

124-
// Dispatch the chunk upload call and register the in-flight future for cancellation.
125119
long chunkLength = chunkPayload.length;
126120
try {
127121
ApiFuture<ChunkUploadResponse<ResponseT>> chunkFuture =
128122
uploadChunkCallable.futureCall(chunkRequest, callContext);
129-
sessionFuture.setInFlightFuture(chunkFuture);
123+
result.addListener(
124+
() -> {
125+
if (result.isCancelled()) {
126+
chunkFuture.cancel(true);
127+
}
128+
},
129+
MoreExecutors.directExecutor());
130130

131-
// Asynchronously handle the response: complete, fail, or chain the next chunk.
132131
ApiFutures.addCallback(
133132
chunkFuture,
134133
new ApiFutureCallback<ChunkUploadResponse<ResponseT>>() {
135134
@Override
136135
public void onSuccess(ChunkUploadResponse<ResponseT> response) {
137-
if (sessionFuture.isDone()) {
136+
if (result.isDone()) {
138137
return;
139138
}
140139
long nextOffset = currentOffset + chunkLength;
141140
if (response.getUploadStatus() == ResumableUploadStatus.FINAL) {
142-
sessionFuture.succeed(response.getResponse());
141+
result.set(response.getResponse());
143142
} else if (isFinal) {
144-
sessionFuture.fail(
143+
result.setException(
145144
new IllegalStateException(
146145
"Upload stream ended and final chunk was transmitted, but server returned"
147146
+ " incomplete status"));
@@ -152,15 +151,15 @@ public void onSuccess(ChunkUploadResponse<ResponseT> response) {
152151

153152
@Override
154153
public void onFailure(Throwable t) {
155-
if (t instanceof CancellationException || sessionFuture.isDone()) {
154+
if (t instanceof CancellationException || result.isDone()) {
156155
return;
157156
}
158-
sessionFuture.fail(t);
157+
result.setException(t);
159158
}
160159
},
161160
MoreExecutors.directExecutor());
162161
} catch (Throwable t) {
163-
sessionFuture.fail(t);
162+
result.setException(t);
164163
}
165164
}
166165
}

‎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
@@ -235,20 +235,6 @@ void testUploadCallable_cancelInFlight_haltsUpload() throws Exception {
235235
assertThrows(CancellationException.class, future::get);
236236
}
237237

238-
@Test
239-
void testUploadCallable_setInFlightFutureAfterCancel_immediatelyCancelsFuture() {
240-
SettableApiFuture<ResumableUploadSession> startFuture = SettableApiFuture.create();
241-
when(mockStartCallable.futureCall(any(), any())).thenReturn(startFuture);
242-
ResumableUploadFuture<String> future =
243-
callable.futureCall("resource-path", streamOf("data"), null);
244-
assertThat(future.cancel(true)).isTrue();
245-
assertThat(future.isCancelled()).isTrue();
246-
247-
SettableApiFuture<String> lateFuture = SettableApiFuture.create();
248-
((ResumableUploadFutureImpl<String>) future).setInFlightFuture(lateFuture);
249-
assertThat(lateFuture.isCancelled()).isTrue();
250-
}
251-
252238
@Test
253239
void testUploadCallable_startFailure_failsFuture() {
254240
when(mockStartCallable.futureCall(any(), any()))
@@ -306,39 +292,6 @@ void testUploadCallable_closesPayloadOnFailure() {
306292
assertThat(stream.closed).isTrue();
307293
}
308294

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

0 commit comments

Comments
 (0)