Repository navigation
fix(spanner): add BatchWrite retry prerequisites, resumption state, and retry policy #15194
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
Draft
alkatrivedi
wants to merge
1
commit into
main
Choose a base branch
from
fix/spanner-batch-write-retry-1-prereqs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,326
−22
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...loud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/BatchWriteResumptionState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.spanner.spi.v1; | ||
|
|
||
| import com.google.api.gax.grpc.GrpcStatusCode; | ||
| import com.google.api.gax.rpc.ApiException; | ||
| import com.google.api.gax.rpc.ApiExceptionFactory; | ||
| import com.google.spanner.v1.BatchWriteRequest; | ||
| import com.google.spanner.v1.BatchWriteResponse; | ||
| import io.grpc.Status; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Pure state tracker for {@code BatchWrite} mutation groups across stream resumption attempts. | ||
| * | ||
| * <p>Tracks which mutation groups remain unacknowledged, builds compacted retry requests containing | ||
| * only remaining groups, and translates attempt-local response indices back to the caller's | ||
| * original index space while releasing acknowledged payloads for immediate garbage collection. | ||
| * | ||
| * <p>Not thread-safe; callers must synchronize access externally. | ||
| */ | ||
| final class BatchWriteResumptionState { | ||
|
|
||
| // Metadata-only template (session, request_options, exclude_txn_from_change_streams) with | ||
| // mutation_groups cleared so acknowledged payloads can be garbage-collected mid-stream. | ||
| private final BatchWriteRequest requestTemplate; | ||
| // Entries are nulled once acknowledged so large mutation payloads can be GC'd immediately. | ||
| private final BatchWriteRequest.MutationGroup[] pending; | ||
| private int remaining; | ||
| private int[] streamIndexes = new int[0]; | ||
|
|
||
| BatchWriteResumptionState(BatchWriteRequest initialRequest) { | ||
| this.requestTemplate = initialRequest.toBuilder().clearMutationGroups().build(); | ||
| int count = initialRequest.getMutationGroupsCount(); | ||
| this.pending = new BatchWriteRequest.MutationGroup[count]; | ||
| for (int i = 0; i < count; i++) { | ||
| this.pending[i] = initialRequest.getMutationGroups(i); | ||
| } | ||
| this.remaining = count; | ||
| } | ||
|
|
||
| boolean isAllAcknowledged() { | ||
| return remaining == 0; | ||
| } | ||
|
|
||
| int getRemainingCount() { | ||
| return remaining; | ||
| } | ||
|
|
||
| int getTotalCount() { | ||
| return pending.length; | ||
| } | ||
|
|
||
| BatchWriteRequest getRequestTemplate() { | ||
| return requestTemplate; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the request for the next stream attempt containing only unacknowledged mutation groups | ||
| * and updates the relative-to-original index mapping for that attempt. | ||
| */ | ||
| BatchWriteRequest buildNextAttemptRequest() { | ||
| BatchWriteRequest.Builder builder = requestTemplate.toBuilder(); | ||
| int[] nextIndexes = new int[remaining]; | ||
| int pos = 0; | ||
| for (int i = 0; i < pending.length; i++) { | ||
| BatchWriteRequest.MutationGroup group = pending[i]; | ||
| if (group != null) { | ||
| builder.addMutationGroups(group); | ||
| nextIndexes[pos++] = i; | ||
| } | ||
| } | ||
| this.streamIndexes = nextIndexes; | ||
| return builder.build(); | ||
| } | ||
|
|
||
| /** | ||
| * Validates all indices in {@code response}, translates them back to the caller's original index | ||
| * space, and marks those mutation groups as acknowledged. | ||
| * | ||
| * @throws ApiException with {@link Status.Code#INTERNAL} (non-retryable) if any index is out of | ||
| * bounds; tracking state is left unmodified when this exception is thrown. | ||
| */ | ||
| BatchWriteResponse translateAndAcknowledge(BatchWriteResponse response) { | ||
| List<Integer> relIndexes = response.getIndexesList(); | ||
| // Validate all indices before mutating tracking state. | ||
| for (int i = 0; i < relIndexes.size(); i++) { | ||
| int relIdx = relIndexes.get(i); | ||
| if (relIdx < 0 || relIdx >= streamIndexes.length) { | ||
| throw ApiExceptionFactory.createException( | ||
| String.format( | ||
| "Spanner returned mutation group index %d out of bounds for BatchWrite" | ||
| + " stream with %d mutation groups", | ||
| relIdx, streamIndexes.length), | ||
| null, | ||
| GrpcStatusCode.of(Status.Code.INTERNAL), | ||
| /* retryable= */ false); | ||
| } | ||
| } | ||
|
|
||
| BatchWriteResponse.Builder translated = response.toBuilder().clearIndexes(); | ||
| for (int i = 0; i < relIndexes.size(); i++) { | ||
| int origIdx = streamIndexes[relIndexes.get(i)]; | ||
| translated.addIndexes(origIdx); | ||
| if (pending[origIdx] != null) { | ||
| pending[origIdx] = null; | ||
| remaining--; | ||
| } | ||
| } | ||
| return translated.build(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.