GeezerVison is showing github.com — feat(gax): wire-level changes for resuming uploads by whowes · Pull Request #14870 · googleapis/google-cloud-java
feat(gax): wire-level changes for resuming uploads
#14870
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.java
(7)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -29,6 +29,9 @@
*/
package com.google.api.gax.httpjson;
import static com.google.api.gax.httpjson.ResumableUploadHeaders.UPLOAD_COMMAND_HEADER;
import static com.google.api.gax.httpjson.ResumableUploadHeaders.UPLOAD_OFFSET_HEADER;
import com.google.api.client.http.HttpMethods;
import com.google.api.core.ApiFuture;
import com.google.api.gax.resumable.ChunkUploadRequest;
Expand All
@@ -55,10 +58,6 @@
class ResumableUploadChunkCallable<ResponseT>
extends UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> {
private static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command";
private static final String UPLOAD_OFFSET_HEADER = "X-Goog-Upload-Offset";
private static final String UPLOAD_STATUS_HEADER = "X-Goog-Upload-Status";
private static final String COMMAND_UPLOAD = "upload";
private static final String COMMAND_FINALIZE = "finalize";
private static final String COMMAND_UPLOAD_FINALIZE = "upload, finalize";
Expand Down
Expand Up
@@ -175,10 +174,7 @@ private ChunkUploadResponseListener(
@Override
public void onHeaders(HttpJsonMetadata responseHeaders) {
Map<String, Object> headers = responseHeaders.getHeaders();
this.uploadStatus =
ResumableUploadStatus.fromHeader(
HttpHeadersUtils.getSingleHeader(headers, UPLOAD_STATUS_HEADER));
this.uploadStatus = ResumableUploadHeaders.parseUploadStatus(responseHeaders);
}
@Override
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,78 @@
/*
* 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.httpjson;
import com.google.api.gax.resumable.ResumableUploadSession;
import com.google.api.gax.resumable.ResumableUploadStatus;
import com.google.common.base.Strings;
import org.jspecify.annotations.NullMarked;
/** Shared header constants and parsing utilities for HTTP/JSON resumable upload wire calls. */
@NullMarked
final class ResumableUploadHeaders {
static final String UPLOAD_PROTOCOL_HEADER = "X-Goog-Upload-Protocol";
static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command";
static final String UPLOAD_URL_HEADER = "X-Goog-Upload-URL";
static final String UPLOAD_GRANULARITY_HEADER = "X-Goog-Upload-Chunk-Granularity";
static final String UPLOAD_OFFSET_HEADER = "X-Goog-Upload-Offset";
static final String UPLOAD_STATUS_HEADER = "X-Goog-Upload-Status";
static final String UPLOAD_SIZE_RECEIVED_HEADER = "X-Goog-Upload-Size-Received";
private ResumableUploadHeaders() {}
static ResumableUploadStatus parseUploadStatus(HttpJsonMetadata responseHeaders) {
return ResumableUploadStatus.fromHeader(
HttpHeadersUtils.getSingleHeader(responseHeaders.getHeaders(), UPLOAD_STATUS_HEADER));
}
static int parseChunkGranularity(HttpJsonMetadata responseHeaders) {
String granularityStr =
HttpHeadersUtils.getSingleHeader(responseHeaders.getHeaders(), UPLOAD_GRANULARITY_HEADER);
if (Strings.isNullOrEmpty(granularityStr)) {
return ResumableUploadSession.DEFAULT_CHUNK_GRANULARITY;
}
try {
int parsed = Integer.parseInt(granularityStr);
if (parsed <= 0) {
throw new IllegalStateException(
"Response contained non-positive "
+ UPLOAD_GRANULARITY_HEADER
+ " header: "
+ granularityStr);
}
return parsed;
} catch (NumberFormatException e) {
throw new IllegalStateException(
"Response contained invalid " + UPLOAD_GRANULARITY_HEADER + " header: " + granularityStr,
e);
}
}
Comment thread
whowes marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -29,15 +29,17 @@
*/
package com.google.api.gax.httpjson;
import static com.google.api.gax.httpjson.ResumableUploadHeaders.UPLOAD_COMMAND_HEADER;
import static com.google.api.gax.httpjson.ResumableUploadHeaders.UPLOAD_SIZE_RECEIVED_HEADER;
import com.google.api.client.http.HttpMethods;
import com.google.api.core.ApiFuture;
import com.google.api.gax.resumable.QueryStatusRequest;
import com.google.api.gax.resumable.QueryStatusResponse;
import com.google.api.gax.resumable.ResumableUploadSession;
import com.google.api.gax.resumable.ResumableUploadStatus;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.ApiExceptionFactory;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.base.Preconditions;
Expand All
@@ -61,9 +63,6 @@
class ResumableUploadQueryStatusCallable<ResponseT>
extends UnaryCallable<QueryStatusRequest, QueryStatusResponse<ResponseT>> {
private static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command";
private static final String UPLOAD_STATUS_HEADER = "X-Goog-Upload-Status";
private static final String UPLOAD_SIZE_RECEIVED_HEADER = "X-Goog-Upload-Size-Received";
private static final String COMMAND_QUERY = "query";
private static final Map<String, List<String>> QUERY_STATUS_HEADERS =
Expand Down
Expand Up
@@ -152,25 +151,20 @@ private static Long parseSizeReceived(HttpJsonMetadata responseHeaders) {
try {
long parsed = Long.parseLong(sizeReceivedStr);
if (parsed < 0) {
throw ApiExceptionFactory.createException (
throw new IllegalStateException (
"Response contained negative "
+ UPLOAD_SIZE_RECEIVED_HEADER
+ " header: "
+ sizeReceivedStr,
/* cause= */ null,
HttpJsonStatusCode.of(StatusCode.Code.INTERNAL),
/* retryable= */ false);
+ sizeReceivedStr);
}
return parsed;
} catch (NumberFormatException e) {
throw ApiExceptionFactory.createException (
throw new IllegalStateException (
"Response contained invalid "
+ UPLOAD_SIZE_RECEIVED_HEADER
+ " header: "
+ sizeReceivedStr,
e,
HttpJsonStatusCode.of(StatusCode.Code.INTERNAL),
/* retryable= */ false);
e);
}
}
return null;
Expand All
@@ -183,6 +177,7 @@ private static class QueryStatusResponseListener<ResponseT>
private final ResumableUploadHttpJsonFuture<QueryStatusResponse<ResponseT>> future;
private final HttpResponseParser<ResponseT> responseParser;
private ResumableUploadStatus uploadStatus = ResumableUploadStatus.UNKNOWN;
private int chunkGranularity = ResumableUploadSession.DEFAULT_CHUNK_GRANULARITY;
@Nullable private Long committedOffset = null;
@Nullable private Throwable headerParsingException;
private String responseBody = "";
Expand All
@@ -196,12 +191,10 @@ private QueryStatusResponseListener(
@Override
public void onHeaders(HttpJsonMetadata responseHeaders) {
Map<String, Object> headers = responseHeaders.getHeaders();
this.uploadStatus =
ResumableUploadStatus.fromHeader(
HttpHeadersUtils.getSingleHeader(headers, UPLOAD_STATUS_HEADER));
this.uploadStatus = ResumableUploadHeaders.parseUploadStatus(responseHeaders);
try {
this.committedOffset = parseSizeReceived(responseHeaders);
this.chunkGranularity = ResumableUploadHeaders.parseChunkGranularity(responseHeaders);
} catch (Throwable t) {
this.headerParsingException = t;
}
Expand All
@@ -228,23 +221,23 @@ public void onClose(int statusCode, HttpJsonMetadata trailers) {
future.set(
QueryStatusResponse.<ResponseT>newBuilder()
.setUploadStatus(uploadStatus)
.setCommittedOffset(committedOffset)
.setResponse(responseParser.parse(stream))
.setChunkGranularity(chunkGranularity)
.build());
} else if (committedOffset != null) {
future.set(
QueryStatusResponse.<ResponseT>newBuilder()
.setCommittedOffset(committedOffset)
.setUploadStatus(uploadStatus)
.setChunkGranularity(chunkGranularity)
.build());
} else {
future.setException(
ApiExceptionFactory.createException (
new IllegalStateException (
"Query status response did not contain valid "
+ UPLOAD_SIZE_RECEIVED_HEADER
+ " header",
/* cause= */ null,
HttpJsonStatusCode.of(StatusCode.Code.INTERNAL),
/* retryable= */ false));
+ " header"));
}
} else {
Throwable cause = trailers.getException();
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -29,6 +29,10 @@
*/
package com.google.api.gax.httpjson;
import static com.google.api.gax.httpjson.ResumableUploadHeaders.UPLOAD_COMMAND_HEADER;
import static com.google.api.gax.httpjson.ResumableUploadHeaders.UPLOAD_PROTOCOL_HEADER;
import static com.google.api.gax.httpjson.ResumableUploadHeaders.UPLOAD_URL_HEADER;
import com.google.api.core.ApiFuture;
import com.google.api.gax.resumable.ResumableUploadSession;
import com.google.api.gax.rpc.ApiCallContext;
Expand All
@@ -52,11 +56,6 @@
class ResumableUploadStartCallable<RequestT>
extends UnaryCallable<RequestT, ResumableUploadSession> {
private static final String UPLOAD_PROTOCOL_HEADER = "X-Goog-Upload-Protocol";
private static final String UPLOAD_COMMAND_HEADER = "X-Goog-Upload-Command";
private static final String UPLOAD_URL_HEADER = "X-Goog-Upload-URL";
private static final String UPLOAD_GRANULARITY_HEADER = "X-Goog-Upload-Chunk-Granularity";
private static final Map<String, List<String>> START_UPLOAD_HEADERS =
ImmutableMap.of(
UPLOAD_PROTOCOL_HEADER, ImmutableList.of("resumable"),
Expand Down
Expand Up
@@ -108,7 +107,7 @@
/** A listener that parses HTTP response headers to produce a {@link ResumableUploadSession}. */
private static class StartUploadResponseListener extends HttpJsonClientCall.Listener<String> {
private final ResumableUploadHttpJsonFuture<ResumableUploadSession> future;
private int chunkGranularity = 1 ;
private int chunkGranularity = ResumableUploadSession.DEFAULT_CHUNK_GRANULARITY ;
@Nullable private String uploadUrl;
@Nullable private Throwable headerParsingException;
Expand All
@@ -119,39 +118,16 @@
@Override
public void onHeaders(HttpJsonMetadata responseHeaders) {
Map<String, Object> headers = responseHeaders.getHeaders();
String url = HttpHeadersUtils.getSingleHeader(headers, UPLOAD_URL_HEADER);
String url =
HttpHeadersUtils.getSingleHeader(responseHeaders.getHeaders(), UPLOAD_URL_HEADER);
if (!Strings.isNullOrEmpty(url)) {
this.uploadUrl = url;
}
String granularityStr = HttpHeadersUtils.getSingleHeader(headers, UPLOAD_GRANULARITY_HEADER);
if (Strings.isNullOrEmpty(granularityStr)) {
return;
}
try {
int parsed = Integer.parseInt(granularityStr);
if (parsed <= 0) {
this.headerParsingException =
ApiExceptionFactory.createException(
"Start upload response contained non-positive chunk granularity header: "
+ granularityStr,
/* cause= */ null,
HttpJsonStatusCode.of(StatusCode.Code.INTERNAL),
/* retryable= */ false);
} else {
this.chunkGranularity = parsed;
}
} catch (NumberFormatException e) {
this.headerParsingException =
ApiExceptionFactory.createException(
"Start upload response contained invalid chunk granularity header: "
+ granularityStr,
e,
HttpJsonStatusCode.of(StatusCode.Code.INTERNAL),
/* retryable= */ false);
this.chunkGranularity = ResumableUploadHeaders.parseChunkGranularity(responseHeaders);
} catch (Throwable t) {
Check warning
on line 129
in sdk-platform-java/gax-java/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ResumableUploadStartCallable.java
View check run for this annotation
SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis
Catch Exception instead of Throwable.
See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaGPLjHGVFGSMShBxcyN&open=AaGPLjHGVFGSMShBxcyN&pullRequest=14870
this.headerParsingException = t;
}
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -58,6 +58,7 @@
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
Expand Down
Expand Up
@@ -140,8 +141,7 @@ void startUpload_malformedChunkGranularityHeader_throwsException() {
assertThrows(
ExecutionException.class, () -> client.startUploadCallable().futureCall(request).get());
assertThat(exception.getCause()).isInstanceOf(InternalException.class);
assertThat(exception.getCause().getCause()).isInstanceOf(NumberFormatException.class);
assertThat(Throwables.getRootCause(exception)).isInstanceOf(NumberFormatException.class);
}
@Test
Expand All
@@ -158,10 +158,9 @@ void startUpload_nonPositiveChunkGranularityHeader_throwsException() {
assertThrows(
ExecutionException.class, () -> client.startUploadCallable().futureCall(request).get());
assertThat(exception.getCause()).isInstanceOf(InternalException.class);
assertThat(exception.getCause())
assertThat(Throwables.getRootCause(exception))
.hasMessageThat()
.contains("Start upload response contained non-positive chunk granularity header: -256");
.contains("Response contained non-positive X-Goog-Upload-Chunk-Granularity header: -256");
}
@Test
Expand Down
Expand Up
@@ -514,6 +513,24 @@ void queryStatus_activeUpload_returnsCommittedOffset() {
assertThat(transport.capturedHeaders.get("x-goog-upload-command")).containsExactly("query");
}
@Test
void queryStatus_withChunkGranularity_returnsGranularity() {
MockLowLevelHttpResponse httpResponse = new MockLowLevelHttpResponse();
httpResponse.setStatusCode(200);
httpResponse.addHeader("X-Goog-Upload-Status", "active");
httpResponse.addHeader("X-Goog-Upload-Size-Received", "524288");
httpResponse.addHeader("X-Goog-Upload-Chunk-Granularity", "262144");
HttpJsonResumableUploadClient<TestRequest, String> client = createClient(httpResponse);
QueryStatusRequest request = QueryStatusRequest.create(TEST_UPLOAD_URL);
QueryStatusResponse<String> response = client.queryStatusCallable().call(request);
assertThat(response.getCommittedOffset()).isEqualTo(524288L);
assertThat(response.getChunkGranularity()).isEqualTo(262144);
assertThat(response.getUploadStatus()).isEqualTo(ResumableUploadStatus.ACTIVE);
}
@Test
void queryStatus_finalUpload_returnsCompleteAndResponseBody() {
MockLowLevelHttpResponse httpResponse = new MockLowLevelHttpResponse();
Expand Down
Expand Up
@@ -590,8 +607,7 @@ void queryStatus_missingOrMalformedSizeReceivedHeader_throwsException() {
assertThrows(
ExecutionException.class,
() -> missingClient.queryStatusCallable().futureCall(request).get());
assertThat(missingException.getCause()).isInstanceOf(InternalException.class);
assertThat(missingException.getCause())
assertThat(Throwables.getRootCause(missingException))
.hasMessageThat()
.contains("Query status response did not contain valid X-Goog-Upload-Size-Received header");
Expand All
@@ -607,10 +623,8 @@ void queryStatus_missingOrMalformedSizeReceivedHeader_throwsException() {
assertThrows(
ExecutionException.class,
() -> malformedClient.queryStatusCallable().futureCall(request).get());
assertThat(malformedException.getCause()).isInstanceOf(InternalException.class);
assertThat(malformedException.getCause())
.hasMessageThat()
.contains("Response contained invalid X-Goog-Upload-Size-Received header: not-a-number");
assertThat(Throwables.getRootCause(malformedException))
.isInstanceOf(NumberFormatException.class);
}
private static HttpJsonResumableUploadClient<TestRequest, String> createClient(
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.