Skip to content

Allow multi-shard read-only transactions in SINGLE transaction mode#18173

Merged
deepthi merged 6 commits into
vitessio:mainfrom
planetscale:fix-tx-singlemode
Jun 5, 2025
Merged

Allow multi-shard read-only transactions in SINGLE transaction mode#18173
deepthi merged 6 commits into
vitessio:mainfrom
planetscale:fix-tx-singlemode

Conversation

@harshit-gangal
Copy link
Copy Markdown
Member

@harshit-gangal harshit-gangal commented Apr 12, 2025

Description

This change updates the behavior of transaction_mode = SINGLE to allow transactions that span multiple shards, as long as they only execute read queries.

Previously, any query that opened sessions on more than one shard would cause the transaction to fail, even if all operations were reads. With this change:

  • A transaction is allowed to touch multiple shards if all of them are read-only.
  • The transaction will only fail if more than one shard has executed a write (i.e., data-modifying queries).

This avoids unnecessary failures in common multi-shard read use cases, while still enforcing the single-shard write guarantee of SINGLE mode.

Related Issue(s)

Checklist

  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

… shard session

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot Bot commented Apr 12, 2025

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot Bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Apr 12, 2025
@github-actions github-actions Bot added this to the v23.0.0 milestone Apr 12, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 12, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 3 lines in your changes missing coverage. Please review.

Project coverage is 67.47%. Comparing base (c3daef3) to head (9f44fd9).
Report is 21 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtgate/executorcontext/vcursor_impl.go 40.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #18173   +/-   ##
=======================================
  Coverage   67.46%   67.47%           
=======================================
  Files        1602     1602           
  Lines      262245   262243    -2     
=======================================
+ Hits       176930   176946   +16     
+ Misses      85315    85297   -18     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…reated on executing a read query

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
@harshit-gangal harshit-gangal changed the title Single Transaction Mode to avoid failing on executing a select query to mark as multi-db transaction Allow multi-shard read-only transactions in SINGLE transaction mode Apr 13, 2025
@harshit-gangal harshit-gangal added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Apr 13, 2025
Comment thread go/vt/vtgate/executorcontext/safe_session.go Outdated
Comment thread go/vt/vtgate/executorcontext/safe_session.go
Comment thread proto/vtgate.proto
@eeSeeGee
Copy link
Copy Markdown
Contributor

It'd be nice to have a way to test for queries that would bounce against the new policy rather than actually bouncing it (producing a metric). That'd give us confidence to roll it out in prod

@aparajon
Copy link
Copy Markdown
Contributor

Would it be possible to make cross shard writes opt-in via a comment directive (example)?

@github-actions
Copy link
Copy Markdown
Contributor

This PR is being marked as stale because it has been open for 30 days with no activity. To rectify, you may do any of the following:

  • Push additional commits to the associated branch.
  • Remove the stale label.
  • Add a comment indicating why it is not stale.

If no action is taken within 7 days, this PR will be closed.

@github-actions github-actions Bot added the Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period. label May 16, 2025
@harshit-gangal harshit-gangal removed Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period. NeedsIssue A linked issue is missing for this Pull Request labels May 21, 2025
@harshit-gangal
Copy link
Copy Markdown
Member Author

It'd be nice to have a way to test for queries that would bounce against the new policy rather than actually bouncing it (producing a metric). That'd give us confidence to roll it out in prod

@eeSeeGee There is no easy way to add this metric. What mode currently you are running in production?

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
@harshit-gangal harshit-gangal marked this pull request as ready for review May 22, 2025 04:04
@GrahamCampbell
Copy link
Copy Markdown
Contributor

GrahamCampbell commented May 22, 2025

Isn't this flawed if the select executes a sproc or UDF that does writes?

@harshit-gangal
Copy link
Copy Markdown
Member Author

harshit-gangal commented Jun 2, 2025

Would it be possible to make cross shard writes opt-in via a comment directive (example)?

It is totally doable to add transaction_mode to comment directive.

I can add that in a follow up PR

@harshit-gangal
Copy link
Copy Markdown
Member Author

Isn't this flawed if the select executes a sproc or UDF that does writes?

@GrahamCampbell Only Select/Show statement are considered read-only and others are considered as write.
sproc is invoked with a call which will be considered a write op.
AFAIK UDFs does not allow modification of data.

@aparajon
Copy link
Copy Markdown
Contributor

aparajon commented Jun 3, 2025

Would it be possible to make cross shard writes opt-in via a comment directive (example)?

It is totally doable to add transaction_mode to comment directive.

I can add that in a follow up PR

That's amazing @harshit-gangal, thank you! I can foresee us heavily using that feature at Block to guardrail and test queries

@deepthi deepthi merged commit 26700d5 into vitessio:main Jun 5, 2025
103 of 107 checks passed
@deepthi deepthi deleted the fix-tx-singlemode branch June 5, 2025 16:03
aparajon added a commit to cashapp/misk that referenced this pull request Apr 15, 2026
## Vitess v23 bump

Upgrade VitessTestDb from v22 to v23.0.3. v23 includes
vitessio/vitess#18173 which allows read-only multi-shard transactions
in SINGLE transaction mode. This prevents false positive cross-shard
detection failures when read-only queries touch multiple shards (e.g.
from stale reserved connection shard sessions).

## allowCrossShardTransactions afterCompletion fix

When the Hibernate session is still open after transaction commit (e.g.
onSessionClose hooks pending), re-set transaction_mode to MULTI so those
hooks can use cross-shard transactions. Without this, vtgate's reserved
connection mode (triggered by SET) retains stale shard sessions across
COMMIT (vitessio/vitess#11616), causing subsequent WRITE operations on
the same pooled connection to fail in SINGLE mode.

Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
aparajon added a commit to cashapp/misk that referenced this pull request Apr 15, 2026
Upgrade VitessTestDb from v22 to v23.0.0. v23 includes
vitessio/vitess#18173 which allows read-only multi-shard transactions
in SINGLE transaction mode. This changes SINGLE mode semantics:

- v22: ALL multi-shard transactions rejected (reads and writes)
- v23: Only multi-shard WRITES rejected; reads pass through

Updated tests:
- `cross-shard read` now succeeds (was expected to fail)
- `cross-shard read + single-shard write` now succeeds (only multi-shard
  writes are rejected)
- Added `onSessionClose hooks` test verifying read-only hooks work after
  allowCrossShardTransactions

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
aparajon added a commit to cashapp/misk that referenced this pull request Apr 16, 2026
Upgrade VitessTestDb from v22 to v23.0.0. v23 includes
vitessio/vitess#18173 which changes SINGLE transaction mode semantics:

- v22: ALL multi-shard transactions rejected (reads and writes)
- v23: Only multi-shard WRITES rejected; multi-shard reads allowed

Updated tests to match v23 behavior:
- cross-shard reads now succeed (previously expected to fail)
- cross-shard read + single-shard write now succeeds
- Added onSessionClose test for read-only hooks after allowCrossShardTransactions

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
aparajon added a commit to cashapp/misk that referenced this pull request Apr 16, 2026
Upgrade VitessTestDb from v22 to v23.0.0. v23 includes
vitessio/vitess#18173 which changes SINGLE transaction mode semantics:

- v22: ALL multi-shard transactions rejected (reads and writes)
- v23: Only multi-shard WRITES rejected; multi-shard reads allowed

Updated tests to match v23 behavior:
- cross-shard reads now succeed (previously expected to fail)
- cross-shard read + single-shard write now succeeds
- Added onSessionClose test for read-only hooks after allowCrossShardTransactions

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
aparajon added a commit to cashapp/misk that referenced this pull request Apr 16, 2026
Upgrade VitessTestDb from v22.0.4 to v23.0.3. v23 includes
vitessio/vitess#18173 which changes SINGLE transaction mode semantics:

- v22: ALL multi-shard transactions rejected (reads and writes)
- v23: Multi-shard reads allowed; only multi-shard writes rejected

Changes:
- Bump VITESS_IMAGE to v23.0.3-mysql84 and VTCTLD_CLIENT_IMAGE to v23.0.3
- Add platform fallback to linux/amd64 for Docker pull (Vitess does not
  publish ARM images; this enables transparent Rosetta emulation on
  Apple Silicon without breaking when ARM images are eventually published)
- Update VitessTransactionModeIntegrationTest: cross-shard reads now
  succeed in SINGLE mode; multi-shard writes still fail

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
aparajon added a commit to cashapp/misk that referenced this pull request Apr 16, 2026
Upgrade VitessTestDb from v22.0.4 to v23.0.3. v23 includes
vitessio/vitess#18173 which changes SINGLE transaction mode semantics:

- v22: ALL multi-shard transactions rejected (reads and writes)
- v23: Multi-shard reads allowed; only multi-shard writes rejected

Changes:
- Bump VITESS_IMAGE to v23.0.3-mysql84 and VTCTLD_CLIENT_IMAGE to v23.0.3
- Add platform fallback to linux/amd64 for Docker pull (Vitess does not
  publish ARM images; this enables transparent Rosetta emulation on
  Apple Silicon without breaking when ARM images are eventually published)
- Update VitessTransactionModeIntegrationTest: cross-shard reads now
  succeed in SINGLE mode; multi-shard writes still fail

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
github-merge-queue Bot pushed a commit to cashapp/misk that referenced this pull request Apr 17, 2026
* Bump Vitess test image to v23 and update SINGLE mode tests

Upgrade VitessTestDb from v22.0.4 to v23.0.3. v23 includes
vitessio/vitess#18173 which changes SINGLE transaction mode semantics:

- v22: ALL multi-shard transactions rejected (reads and writes)
- v23: Multi-shard reads allowed; only multi-shard writes rejected

Changes:
- Bump VITESS_IMAGE to v23.0.3-mysql84 and VTCTLD_CLIENT_IMAGE to v23.0.3
- Add platform fallback to linux/amd64 for Docker pull (Vitess does not
  publish ARM images; this enables transparent Rosetta emulation on
  Apple Silicon without breaking when ARM images are eventually published)
- Update VitessTransactionModeIntegrationTest: cross-shard reads now
  succeed in SINGLE mode; multi-shard writes still fail

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Use explicit BEGIN in executeTransaction for reliable timeout enforcement

Since vitessio/vitess#19277 (backported to v22.0.4 via #19341), vtgate
defers implicit transaction start for autocommit=0 sessions. This means
vttablet no longer applies --queryserver-config-transaction-timeout to
queries running under SET autocommit=0. Switching to explicit BEGIN
ensures vttablet tracks the transaction and enforces the timeout.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: Query Serving Type: Enhancement Logical improvement (somewhere between a bug and feature)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Allow Transaction Mode Single to Execute Read only queries to multiple shards

7 participants