Skip to content

perf(spanner): inline begin transaction error handling (step 2)#5321

Open
olavloite wants to merge 6 commits intogoogleapis:mainfrom
olavloite:spanner-inline-begin-tx-step-2
Open

perf(spanner): inline begin transaction error handling (step 2)#5321
olavloite wants to merge 6 commits intogoogleapis:mainfrom
olavloite:spanner-inline-begin-tx-step-2

Conversation

@olavloite
Copy link
Copy Markdown
Contributor

@olavloite olavloite commented Apr 8, 2026

Adds error handling for inline-begin-transaction. If the first statement in a
transaction fails, and that statement included a BeginTransaction option, then
the transaction has not been started. In order to keep the semantics of the
transaction consistent for an 'outside observer', we need to do the following:

  1. Catch the error that was thrown by the initial statement.
  2. Start the transaction using an explicit BeginTransaction RPC.
  3. Retry the initial statement, but now using the transaction ID from step 2.
  4. Return the error or result for the retried initial statement.

The above makes sure that:

  1. The transaction is actually started when the first statement is executed, also
    when the statement failed.
  2. The statement becomes part of the transaction, and the result of the statement
    is consistent with the read-timestamp of the transaction.

The second part is important in order to comply with Spanner's strong consistency
guarantees; If for example a statement returns a 'Table not found' error, then
that error is only valid for the read timestamp that was used for executing the
statement. This is the reason that we retry the statement after the BeginTransaction
RPC to be able to return a result that is guaranteed to be consistent with any
other queries/reads that will be executed in the same transaction.

This is step 2 of the implementation of inline-begin-transaction for Spanner. A preliminary view of the full implementation after all steps can be seen in #5307

Adds support for inlining the BeginTransaction with the first query in a read-only
transaction. This saves one round-trip to Spanner for multi-use read-only transactions.

This implementation is intentionally simple:
1. It does not support parallel queries at the start of the transaction.
2. It does not include error handling for the first query.
3. It only supports read-only transactions.

This is step 1. Follow-up pull requests addresses the above points.
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Apr 8, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 98.38188% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.81%. Comparing base (fab3aa8) to head (c19e250).

Files with missing lines Patch % Lines
src/spanner/src/result_set.rs 98.28% 6 Missing ⚠️
src/spanner/src/read_only_transaction.rs 98.50% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5321      +/-   ##
==========================================
+ Coverage   97.79%   97.81%   +0.01%     
==========================================
  Files         222      222              
  Lines       46349    46915     +566     
==========================================
+ Hits        45326    45888     +562     
- Misses       1023     1027       +4     

☔ 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.

@olavloite olavloite force-pushed the spanner-inline-begin-tx-step-2 branch from ed6da67 to 4261add Compare April 8, 2026 07:41
Adds error handling for inline-begin-transaction. If the first statement in a
transaction fails, and that statement included a BeginTransaction option, then
the transaction has not been started. In order to keep the semantics of the
transaction consistent for an 'outside observer', we need to do the following:
1. Catch the error that was thrown by the initial statement.
2. Start the transaction using an explicit BeginTransaction RPC.
3. Retry the initial statement, but now using the transaction ID from step 2.
4. Return the error or result for the retried initial statement.

The above makes sure that:
1. The transaction is actually started when the first statement is executed, also
   when the statement failed.
2. The statement becomes part of the transaction, and the result of the statement
   is consistent with the read-timestamp of the transaction.

The second part is important in order to comply with Spanner's strong consistency
guarantees; If for example a statement returns a 'Table not found' error, then
that error is only valid for the read timestamp that was used for executing the
statement. This is the reason that we retry the statement after the BeginTransaction
RPC to be able to return a result that is guaranteed to be consistent with any
other queries/reads that will be executed in the same transaction.
@olavloite olavloite force-pushed the spanner-inline-begin-tx-step-2 branch from 4261add to c3e8b33 Compare April 8, 2026 07:52
@olavloite olavloite changed the title perf(spanner): [WIP] inline begin transaction error handling perf(spanner): [WIP] inline begin transaction error handling (step 2) Apr 8, 2026
@olavloite olavloite changed the title perf(spanner): [WIP] inline begin transaction error handling (step 2) perf(spanner): inline begin transaction error handling (step 2) Apr 8, 2026
@olavloite olavloite force-pushed the spanner-inline-begin-tx-step-2 branch from 8d68fa3 to c19e250 Compare April 9, 2026 11:36
@olavloite olavloite marked this pull request as ready for review April 9, 2026 11:53
@olavloite olavloite requested a review from a team as a code owner April 9, 2026 11:53
@dbolduc dbolduc requested a review from a team April 9, 2026 13:12
@dbolduc dbolduc marked this pull request as draft April 9, 2026 13:13
@dbolduc dbolduc marked this pull request as ready for review April 9, 2026 13:13
@dbolduc
Copy link
Copy Markdown
Member

dbolduc commented Apr 9, 2026

@olavloite can you have someone from @googleapis/spanner-team review this PR? I want to test if y'all can approve and merge PRs that only affect spanner. (#5341)

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