Skip to content

Conversation

@Samriddha9619
Copy link

Description

This PR optimizes the Two-Phase Commit (2PC) path in VTGate by ensuring that only shards that have been modified (tracked via the RowsAffected flag) participate in the atomic commit flow.

Changes

  1. Separate shards by modification status: The commit2PC function now separates ShardSessions into modifiedShards and readOnlyShards based on the RowsAffected flag.

  2. Direct commit for read-only shards: Read-only shards are committed directly using commitShard(), bypassing 2PC. This releases their resources immediately without the overhead of prepare/commit phases.

  3. Single-shard optimization: If only one shard was modified, we use normal commit instead of 2PC. This is a significant performance improvement as it avoids 2PC entirely.

  4. 2PC only for multiple modified shards: The full 2PC flow (CreateTransaction → Prepare → StartCommit → CommitPrepared → Conclude) is only executed when multiple shards have been modified.

Impact

  • Performance: Reduces 2PC overhead when transactions only modify a single shard
  • Resource efficiency: Read-only shards are released faster
  • Correctness: Maintains ACID guarantees for modified shards while optimizing read-only paths

Related Issue(s)

Fixes #18054

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • 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

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

vitess-bot bot commented Jan 31, 2026

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.

@Samriddha9619 Samriddha9619 force-pushed the restrict-2pc-modified-shards branch from e4706d9 to e450493 Compare January 31, 2026 07:37
@promptless
Copy link
Contributor

promptless bot commented Jan 31, 2026

📝 Documentation updates detected!

New suggestion: Document TwoPC performance optimizations

Comment on lines 265 to 292
var modifiedShards []*vtgatepb.Session_ShardSession
var readOnlyShards []*vtgatepb.Session_ShardSession

for _, s := range session.ShardSessions {
if s.RowsAffected {
modifiedShards = append(modifiedShards, s)
} else {
readOnlyShards = append(readOnlyShards, s)
}
}

for _, s := range readOnlyShards {
_ = txc.commitShard(ctx, s, session.GetLogger())
}

// If the number of participants is one or less, then it's a normal commit.
if len(session.ShardSessions) <= 1 {
if len(modifiedShards) <= 1 {
originalShards := session.ShardSessions
session.ShardSessions = modifiedShards
defer func() { session.ShardSessions = originalShards }()

return txc.commitNormal(ctx, session)
}

originalShards := session.ShardSessions
session.ShardSessions = modifiedShards
defer func() { session.ShardSessions = originalShards }()

Copy link
Member

Choose a reason for hiding this comment

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

The idea behind this change is solid, but I'm not sure the implementation is going in the right direction.

Modifying ShardSessions and having the changes be reverted through a defer is confusing, as it's no longer clear what the contents of ShardSessions will be at different points of the code..

Copy link
Author

Choose a reason for hiding this comment

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

@arthurschreiber Thanks for the review.
In the updated PR I have refactored commit2PC to filter the shards into local slices and pass them explicitly to the transaction logic.
Does this approach look better to you? Happy to adjust if I overlooked anything else.

@Samriddha9619 Samriddha9619 force-pushed the restrict-2pc-modified-shards branch from e450493 to 963147e Compare February 2, 2026 17:34
Signed-off-by: Samriddha9619 <sumitkumartripathi0@gmail.com>
@Samriddha9619 Samriddha9619 force-pushed the restrict-2pc-modified-shards branch from 963147e to 672fb3b Compare February 2, 2026 18:19
Copy link
Member

@harshit-gangal harshit-gangal left a comment

Choose a reason for hiding this comment

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

changes looks good to me.

Copy link
Member

@arthurschreiber arthurschreiber left a comment

Choose a reason for hiding this comment

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

Thanks! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restrict Cross-Shard Atomic Commit Path to Only Modified Shards

3 participants