Skip to content

feat(spanner): add BatchWrite transaction#5502

Open
olavloite wants to merge 5 commits intogoogleapis:mainfrom
olavloite:spanner-batch-write-step-2
Open

feat(spanner): add BatchWrite transaction#5502
olavloite wants to merge 5 commits intogoogleapis:mainfrom
olavloite:spanner-batch-write-step-2

Conversation

@olavloite
Copy link
Copy Markdown
Contributor

Adds support for BatchWrite transactions. BatchWrite can be used to write
large numbers of mutations to Spanner. A BatchWrite transaction is not
guaranteed to be atomic. However, each MutationGroup in a BatchWrite
transaction is guaranteed to be atomic.

This change adds a method that returns a stream of responses and delegates
all error handling to the application. A follow-up pull request will add a
managed execution method that handles errors automatically.

Depends on #5499

Adds the foundations for batch write transactions. The public API will be
added in a follow-up pull request.
@olavloite olavloite requested review from a team as code owners April 22, 2026 16:04
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Apr 22, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 98.91304% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.77%. Comparing base (a1269aa) to head (ab9dcf3).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/spanner/src/batch_write_transaction.rs 98.44% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #5502    +/-   ##
========================================
  Coverage   97.77%   97.77%            
========================================
  Files         221      222     +1     
  Lines       51107    51291   +184     
========================================
+ Hits        49971    50152   +181     
- Misses       1136     1139     +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Adds support for BatchWrite transactions. BatchWrite can be used to write
large numbers of mutations to Spanner. A BatchWrite transaction is not
guaranteed to be atomic. However, each MutationGroup in a BatchWrite
transaction is guaranteed to be atomic.

This change adds a method that returns a stream of responses and delegates
all error handling to the application. A follow-up pull request will add a
managed execution method that handles errors automatically.
@olavloite olavloite force-pushed the spanner-batch-write-step-2 branch from f757e38 to e79d1e7 Compare April 22, 2026 16:25
@dbolduc dbolduc self-assigned this Apr 30, 2026
Copy link
Copy Markdown
Member

@dbolduc dbolduc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly curious about why we aren't using the type from the model.

/// This method sends the mutation groups to Spanner and returns the responses as a stream.
/// Each response includes a status code that indicates whether the mutation groups that
/// it references were applied successfully.
/// The method does not handle any errors, including retryable errors like Aborted.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: add a newline before this paragraph?

Suggested change
/// The method does not handle any errors, including retryable errors like Aborted.
///
/// The method does not handle any errors, including retryable errors like Aborted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// limitations under the License.

use crate::key::KeySet;
use crate::model::batch_write_request::MutationGroup as ProtoMutationGroup;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we write our own type instead of exposing the generated type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the generated MutationGroup struct would also mean that customers would need to use the generated Mutation struct (or manually convert hand-written Mutation instances to the generated version before setting them on MutationGroup). And we certainly want to use a hand-written Mutation struct, as it allows us to encapsulate the way that Spanner expects the various data types to be encoded on the wire. A hand-written Mutation can be constructed like this:

let mutation = Mutation::new_insert_builder("Users")
    .set("UserId").to(&1)
    .set("UserName").to(&"Alice")
    .build();

Our ToValue trait then takes care or encoding these values the way that Spanner expects them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

Comment thread src/spanner/src/batch_write_transaction.rs Outdated
Comment on lines +272 to +276
/// A group of mutations that are applied atomically in a [crate::BatchWriteTransaction].
#[derive(Clone, Debug, PartialEq)]
pub struct MutationGroup {
pub mutations: Vec<Mutation>,
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: #5566

It is a breaking change to add any fields to this in the future. Are you sure it will not grow?

(if we are keeping the type)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added #[non_exhaustive]

@olavloite olavloite requested a review from dbolduc May 8, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants