-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor(gax): remove circular ref between resumable upload future and chunk coordinator #14421
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 |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |
| import com.google.api.core.ApiFutureCallback; | ||
| import com.google.api.core.ApiFutures; | ||
| import com.google.api.core.InternalApi; | ||
| import com.google.api.core.SettableApiFuture; | ||
| import com.google.api.gax.resumable.ChunkUploadRequest; | ||
| import com.google.api.gax.resumable.ChunkUploadResponse; | ||
| import com.google.api.gax.resumable.ResumableUploadStatus; | ||
|
|
@@ -45,6 +46,7 @@ | |
| import java.util.Arrays; | ||
| import java.util.concurrent.CancellationException; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Coordinates chunk transmission steps of a resumable upload session. | ||
|
|
@@ -64,32 +66,40 @@ | |
| private final byte[] buffer; | ||
| private final int chunkSize; | ||
| private final ApiCallContext callContext; | ||
| private final ResumableUploadFutureImpl<ResponseT> sessionFuture; | ||
| private final SettableApiFuture<ResponseT> result = SettableApiFuture.create(); | ||
| private volatile @Nullable ApiFuture<?> currentChunkFuture; | ||
|
Check warning on line 70 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ResumableUploadChunkCoordinator.java
|
||
|
|
||
| ResumableUploadChunkCoordinator( | ||
| UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable, | ||
| String uploadUrl, | ||
| InputStream payload, | ||
| int chunkSize, | ||
| ApiCallContext callContext, | ||
| ResumableUploadFutureImpl<ResponseT> sessionFuture) { | ||
| ApiCallContext callContext) { | ||
| this.uploadChunkCallable = | ||
| checkNotNull(uploadChunkCallable, "uploadChunkCallable must not be null"); | ||
| this.uploadUrl = checkNotNull(uploadUrl, "uploadUrl must not be null"); | ||
| this.payload = checkNotNull(payload, "payload must not be null"); | ||
| this.chunkSize = chunkSize; | ||
| this.callContext = checkNotNull(callContext, "callContext must not be null"); | ||
| this.sessionFuture = checkNotNull(sessionFuture, "sessionFuture must not be null"); | ||
| this.buffer = new byte[chunkSize]; | ||
| } | ||
|
|
||
| void start() { | ||
| ApiFuture<ResponseT> start() { | ||
| result.addListener( | ||
| () -> { | ||
| ApiFuture<?> chunk = currentChunkFuture; | ||
| if (result.isCancelled() && chunk != null) { | ||
| chunk.cancel(true); | ||
| } | ||
| }, | ||
| MoreExecutors.directExecutor()); | ||
| transmitChunk(0L); | ||
| return result; | ||
| } | ||
|
|
||
| private void transmitChunk(long currentOffset) { | ||
|
Check failure on line 100 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ResumableUploadChunkCoordinator.java
|
||
| // Abort if the session was already completed or canceled. | ||
| if (sessionFuture.isDone()) { | ||
| if (result.isDone()) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -98,7 +108,7 @@ | |
| try { | ||
| bytesRead = ByteStreams.read(payload, buffer, 0, chunkSize); | ||
| } catch (IOException e) { | ||
| sessionFuture.fail(e); | ||
| result.setException(e); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -126,22 +136,25 @@ | |
| try { | ||
| ApiFuture<ChunkUploadResponse<ResponseT>> chunkFuture = | ||
| uploadChunkCallable.futureCall(chunkRequest, callContext); | ||
| sessionFuture.setInFlightFuture(chunkFuture); | ||
| this.currentChunkFuture = chunkFuture; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a race condition where To prevent this, check if this.currentChunkFuture = chunkFuture;
if (result.isCancelled()) {
chunkFuture.cancel(true);
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| if (result.isCancelled()) { | ||
| chunkFuture.cancel(true); | ||
| return; | ||
| } | ||
|
|
||
| // Asynchronously handle the response: complete, fail, or chain the next chunk. | ||
| ApiFutures.addCallback( | ||
| chunkFuture, | ||
| new ApiFutureCallback<ChunkUploadResponse<ResponseT>>() { | ||
| @Override | ||
| public void onSuccess(ChunkUploadResponse<ResponseT> response) { | ||
| if (sessionFuture.isDone()) { | ||
| if (result.isDone()) { | ||
| return; | ||
| } | ||
| long nextOffset = currentOffset + chunkLength; | ||
| if (response.getUploadStatus() == ResumableUploadStatus.FINAL) { | ||
| sessionFuture.succeed(response.getResponse()); | ||
| result.set(response.getResponse()); | ||
| } else if (isFinal) { | ||
| sessionFuture.fail( | ||
| result.setException( | ||
| new IllegalStateException( | ||
| "Upload stream ended and final chunk was transmitted, but server returned" | ||
| + " incomplete status")); | ||
|
|
@@ -152,15 +165,15 @@ | |
|
|
||
| @Override | ||
| public void onFailure(Throwable t) { | ||
| if (t instanceof CancellationException || sessionFuture.isDone()) { | ||
| if (t instanceof CancellationException || result.isDone()) { | ||
| return; | ||
| } | ||
| sessionFuture.fail(t); | ||
| result.setException(t); | ||
| } | ||
| }, | ||
| MoreExecutors.directExecutor()); | ||
| } catch (Throwable t) { | ||
| sessionFuture.fail(t); | ||
| result.setException(t); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,16 +128,46 @@ public void onSuccess(ResumableUploadSession session) { | |
| ResumableUploadChunkCoordinator<ResponseT> coordinator = | ||
| new ResumableUploadChunkCoordinator<>( | ||
| uploadChunkCallable, | ||
| session.getUploadUrl(), | ||
| uploadSessionUrl, | ||
| payload, | ||
| settings.getChunkSize(), | ||
| callContext, | ||
| ResumableUploadFutureImpl.this); | ||
| callContext); | ||
| ApiFuture<ResponseT> uploadFuture; | ||
| try { | ||
| coordinator.start(); | ||
| uploadFuture = coordinator.start(); | ||
| } catch (Throwable t) { | ||
| fail(t); | ||
| return; | ||
| } | ||
| boolean alreadyDone = false; | ||
| synchronized (lock) { | ||
| if (resultFuture.isDone()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the only possibility that resultFuture is already done at this moment is that it is cancelled? |
||
| alreadyDone = true; | ||
| } else { | ||
| inFlightFuture = uploadFuture; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the purpose of In general, I feel we have a lot of complex logics just to support correct cancellation of this future. Maybe we can restructure the code to make it simpler. |
||
| } | ||
| } | ||
| if (alreadyDone) { | ||
| uploadFuture.cancel(true); | ||
| return; | ||
| } | ||
|
Comment on lines
+143
to
153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the removal of To fix this, check if boolean shouldCancel = false;
synchronized (lock) {
if (resultFuture.isDone()) {
shouldCancel = resultFuture.isCancelled();
} else {
inFlightFuture = uploadFuture;
}
}
if (shouldCancel) {
uploadFuture.cancel(true);
return;
}References
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| ApiFutures.addCallback( | ||
| uploadFuture, | ||
| new ApiFutureCallback<ResponseT>() { | ||
| @Override | ||
| public void onSuccess(ResponseT response) { | ||
| succeed(response); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Throwable t) { | ||
| if (t instanceof CancellationException) { | ||
| return; | ||
| } | ||
| fail(t); | ||
| } | ||
| }, | ||
| MoreExecutors.directExecutor()); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -151,33 +181,15 @@ public void onFailure(Throwable t) { | |
| MoreExecutors.directExecutor()); | ||
| } | ||
|
|
||
| /** | ||
| * Registers the active in-flight future for cancellation. If this session future has already been | ||
| * canceled, the supplied future is canceled immediately. | ||
| */ | ||
| void setInFlightFuture(ApiFuture<?> inFlightFuture) { | ||
| boolean shouldCancel = false; | ||
| synchronized (lock) { | ||
| if (resultFuture.isDone()) { | ||
| shouldCancel = resultFuture.isCancelled(); | ||
| } else { | ||
| this.inFlightFuture = inFlightFuture; | ||
| } | ||
| } | ||
| if (shouldCancel) { | ||
| inFlightFuture.cancel(true); | ||
| } | ||
| } | ||
|
|
||
| void succeed(@Nullable ResponseT result) { | ||
| private void succeed(@Nullable ResponseT result) { | ||
| synchronized (lock) { | ||
| inFlightFuture = null; | ||
| } | ||
| closePayload(); | ||
| resultFuture.set(result); | ||
| } | ||
|
|
||
| void fail(Throwable t) { | ||
| private void fail(Throwable t) { | ||
| synchronized (lock) { | ||
| inFlightFuture = null; | ||
| } | ||
|
|
||
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.
nit: rename
resultto something more readable, otherwise it's not easy to comprehend. There might be a more concise name for it, but IIUC, it's basically auploadAllChunksResultFuture?