-
Notifications
You must be signed in to change notification settings - Fork 131
feat(spanner): add BatchWrite transaction #5502
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
Open
olavloite
wants to merge
5
commits into
googleapis:main
Choose a base branch
from
olavloite:spanner-batch-write-step-2
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
726eb82
chore(spanner): foundation for batch write transactions
olavloite accd8a5
Merge branch 'main' into spanner-batch-write-step-1
olavloite e79d1e7
feat(spanner): add BatchWrite transaction
olavloite fd49bcf
Merge branch 'main' into spanner-batch-write-step-2
olavloite ab9dcf3
chore(spanner): fix doc error for stream
olavloite 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,285 @@ | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| use crate::client::DatabaseClient; | ||
| use crate::model::BatchWriteRequest; | ||
| use crate::model::BatchWriteResponse; | ||
| use crate::mutation::MutationGroup; | ||
| use crate::server_streaming::stream::BatchWriteStream; | ||
| use gaxi::prost::FromProto; | ||
|
|
||
| #[cfg(feature = "unstable-stream")] | ||
| use futures::Stream; | ||
|
|
||
| /// A builder for [BatchWriteTransaction]. | ||
| pub struct BatchWriteTransactionBuilder { | ||
| client: DatabaseClient, | ||
| } | ||
|
|
||
| impl BatchWriteTransactionBuilder { | ||
| pub(crate) fn new(client: DatabaseClient) -> Self { | ||
| Self { client } | ||
| } | ||
|
|
||
| /// Builds the [BatchWriteTransaction]. | ||
| pub fn build(self) -> BatchWriteTransaction { | ||
| let session_name = self.client.session_name(); | ||
| BatchWriteTransaction { | ||
| session_name, | ||
| client: self.client, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A transaction for executing batch writes. | ||
| /// | ||
| /// Batch writes are not guaranteed to be atomic across mutation groups. | ||
| /// All mutations within a group are applied atomically. | ||
| pub struct BatchWriteTransaction { | ||
| session_name: String, | ||
| client: DatabaseClient, | ||
| } | ||
|
|
||
| impl BatchWriteTransaction { | ||
| /// Executes the batch write and returns a stream of responses. | ||
| /// | ||
| /// # Example | ||
| /// ``` | ||
| /// # use google_cloud_spanner::client::{Mutation, Spanner, MutationGroup}; | ||
| /// # use google_cloud_gax::error::rpc::Code; | ||
| /// # async fn sample() -> Result<(), Box<dyn std::error::Error>> { | ||
| /// let client = Spanner::builder().build().await?; | ||
| /// let db = client.database_client("projects/p/instances/i/databases/d").build().await?; | ||
| /// | ||
| /// let mutation = Mutation::new_insert_builder("Users") | ||
| /// .set("UserId").to(&1) | ||
| /// .build(); | ||
| /// let group = MutationGroup::new(vec![mutation]); | ||
| /// | ||
| /// let tx = db.batch_write_transaction().build(); | ||
| /// let mut stream = tx.execute_streaming(vec![group]).await?; | ||
| /// | ||
| /// while let Some(response) = stream.next().await { | ||
| /// let response = response?; | ||
| /// if let Some(status) = response.status.as_ref().filter(|s| s.code != Code::Ok as i32) { | ||
| /// eprintln!("Error applying groups {:?}: {}", response.indexes, status.message); | ||
| /// } else { | ||
| /// println!("Applied groups: {:?}", response.indexes); | ||
| /// } | ||
| /// } | ||
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| /// | ||
| /// 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. | ||
| /// The caller is responsible for handling any errors and for retrying the transaction in | ||
| /// case it is aborted by Spanner. | ||
| pub async fn execute_streaming<I>(self, groups: I) -> crate::Result<BatchWriteResponseStream> | ||
| where | ||
| I: IntoIterator<Item = MutationGroup>, | ||
| { | ||
| let req = BatchWriteRequest::new() | ||
| .set_session(self.session_name.clone()) | ||
| .set_mutation_groups(groups.into_iter().map(|g| g.build_proto())); | ||
|
|
||
| let stream = self | ||
| .client | ||
| .spanner | ||
| .batch_write(req, crate::RequestOptions::default()) | ||
| .send() | ||
| .await?; | ||
| Ok(BatchWriteResponseStream { inner: stream }) | ||
| } | ||
| } | ||
|
|
||
| /// A stream of [BatchWriteResponse] messages. | ||
| pub struct BatchWriteResponseStream { | ||
| pub(crate) inner: BatchWriteStream, | ||
| } | ||
|
|
||
| impl BatchWriteResponseStream { | ||
| /// Fetches the next [BatchWriteResponse] from the stream. | ||
| /// | ||
| /// Returns `Some(Ok(BatchWriteResponse))` when a message is successfully received, | ||
| /// `None` when the stream concludes naturally, or `Some(Err(_))` on RPC errors. | ||
| pub async fn next(&mut self) -> Option<crate::Result<BatchWriteResponse>> { | ||
| let proto_opt = self.inner.next_message().await?; | ||
| match proto_opt { | ||
| Ok(proto) => match proto.cnv() { | ||
| Ok(model) => Some(Ok(model)), | ||
| Err(e) => Some(Err(crate::Error::deser(e))), | ||
| }, | ||
| Err(e) => Some(Err(e)), | ||
| } | ||
| } | ||
|
|
||
| /// Converts the [`BatchWriteResponseStream`] into a [`Stream`]. | ||
| /// | ||
| /// This consumes the [`BatchWriteResponseStream`] and returns a stream of responses. | ||
| #[cfg(feature = "unstable-stream")] | ||
| pub fn into_stream(self) -> impl Stream<Item = crate::Result<BatchWriteResponse>> + Unpin { | ||
| use futures::stream::unfold; | ||
| Box::pin(unfold(self, |mut stream| async move { | ||
| stream.next().await.map(|res| (res, stream)) | ||
| })) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::client::{Mutation, Spanner}; | ||
| use crate::result_set::tests::adapt; | ||
| use anyhow::Result; | ||
| use gaxi::grpc::tonic::Response; | ||
| use spanner_grpc_mock::MockSpanner; | ||
| use spanner_grpc_mock::google::spanner::v1 as mock_v1; | ||
|
|
||
| pub(crate) async fn setup_db_client( | ||
| mock: MockSpanner, | ||
| ) -> (DatabaseClient, tokio::task::JoinHandle<()>) { | ||
| use google_cloud_auth::credentials::anonymous::Builder as Anonymous; | ||
| let (address, server) = spanner_grpc_mock::start("0.0.0.0:0", mock) | ||
| .await | ||
| .expect("Failed to start mock server"); | ||
| let spanner = Spanner::builder() | ||
| .with_endpoint(address) | ||
| .with_credentials(Anonymous::new().build()) | ||
| .build() | ||
| .await | ||
| .expect("Failed to build client"); | ||
|
|
||
| let db_client = spanner | ||
| .database_client("projects/p/instances/i/databases/d") | ||
| .build() | ||
| .await | ||
| .expect("Failed to create DatabaseClient"); | ||
|
|
||
| (db_client, server) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn execute_streaming() -> Result<()> { | ||
| let mut mock = MockSpanner::new(); | ||
| mock.expect_create_session().returning(|_| { | ||
| Ok(Response::new(mock_v1::Session { | ||
| name: "projects/p/instances/i/databases/d/sessions/123".to_string(), | ||
| ..Default::default() | ||
| })) | ||
| }); | ||
|
|
||
| mock.expect_batch_write().once().returning(|req| { | ||
| let req = req.into_inner(); | ||
| assert_eq!( | ||
| req.session, | ||
| "projects/p/instances/i/databases/d/sessions/123" | ||
| ); | ||
| assert_eq!(req.mutation_groups.len(), 1); | ||
|
|
||
| let response = mock_v1::BatchWriteResponse { | ||
| indexes: vec![0], | ||
| status: None, | ||
| commit_timestamp: None, | ||
| }; | ||
|
|
||
| Ok(Response::from(adapt([Ok(response)]))) | ||
| }); | ||
|
|
||
| let (db_client, _server) = setup_db_client(mock).await; | ||
|
|
||
| let mutation = Mutation::new_insert_builder("Users") | ||
| .set("UserId") | ||
| .to(&1) | ||
| .build(); | ||
| let group = MutationGroup::new(vec![mutation]); | ||
|
|
||
| let tx = db_client.batch_write_transaction().build(); | ||
| let mut stream = tx.execute_streaming(vec![group]).await?; | ||
|
|
||
| let result = stream | ||
| .next() | ||
| .await | ||
| .expect("stream should have yielded a message")?; | ||
| assert_eq!( | ||
| result.indexes, | ||
| vec![0], | ||
| "indexes should match the mocked response" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(feature = "unstable-stream")] | ||
| #[tokio::test] | ||
| async fn execute_streaming_into_stream() -> Result<()> { | ||
| use futures::StreamExt; | ||
|
|
||
| let mut mock = MockSpanner::new(); | ||
| mock.expect_create_session().returning(|_| { | ||
| Ok(Response::new(mock_v1::Session { | ||
| name: "projects/p/instances/i/databases/d/sessions/123".to_string(), | ||
| ..Default::default() | ||
| })) | ||
| }); | ||
|
|
||
| mock.expect_batch_write().once().returning(|req| { | ||
| let req = req.into_inner(); | ||
| assert_eq!( | ||
| req.session, "projects/p/instances/i/databases/d/sessions/123", | ||
| "session name should match" | ||
| ); | ||
| assert_eq!( | ||
| req.mutation_groups.len(), | ||
| 1, | ||
| "should contain precisely 1 mutation group" | ||
| ); | ||
|
|
||
| let response = mock_v1::BatchWriteResponse { | ||
| indexes: vec![0], | ||
| status: None, | ||
| commit_timestamp: None, | ||
| }; | ||
|
|
||
| Ok(Response::from(adapt([Ok(response)]))) | ||
| }); | ||
|
|
||
| let (db_client, _server) = setup_db_client(mock).await; | ||
|
|
||
| let mutation = Mutation::new_insert_builder("Users") | ||
| .set("UserId") | ||
| .to(&1) | ||
| .build(); | ||
| let group = MutationGroup::new(vec![mutation]); | ||
|
|
||
| let transaction = db_client.batch_write_transaction().build(); | ||
| let stream = transaction.execute_streaming(vec![group]).await?; | ||
| let mut stream = stream.into_stream(); | ||
|
|
||
| let result = stream | ||
| .next() | ||
| .await | ||
| .expect("stream should have yielded a message")?; | ||
| assert_eq!( | ||
| result.indexes, | ||
| vec![0], | ||
| "indexes should match the mocked response" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
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
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.
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.
super nit: add a newline before this paragraph?
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.
Done