-
Notifications
You must be signed in to change notification settings - Fork 2.3k
vtgate: restrict 2PC commit path to only modified shards #19256
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
base: main
Are you sure you want to change the base?
vtgate: restrict 2PC commit path to only modified shards #19256
Conversation
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
e4706d9 to
e450493
Compare
|
📝 Documentation updates detected! New suggestion: Document TwoPC performance optimizations |
| 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 }() | ||
|
|
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.
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..
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.
@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.
e450493 to
963147e
Compare
Signed-off-by: Samriddha9619 <sumitkumartripathi0@gmail.com>
963147e to
672fb3b
Compare
harshit-gangal
left a comment
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.
changes looks good to me.
arthurschreiber
left a comment
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.
Thanks! ❤️
Description
This PR optimizes the Two-Phase Commit (2PC) path in VTGate by ensuring that only shards that have been modified (tracked via the
RowsAffectedflag) participate in the atomic commit flow.Changes
Separate shards by modification status: The
commit2PCfunction now separatesShardSessionsintomodifiedShardsandreadOnlyShardsbased on theRowsAffectedflag.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.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.
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
Related Issue(s)
Fixes #18054
Checklist