-
Notifications
You must be signed in to change notification settings - Fork 212
feat: 2pc logic for resharding #1088
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3090989
feat: 2pc logic for resharding
yogesh1801 e6add87
fix: comment for rollback
yogesh1801 d8e60e0
feat: transaction naming moved to another file
yogesh1801 0b20dcb
feat: add cleanup wait before retry
yogesh1801 23e79bd
fix clippy errors
yogesh1801 54d9cb0
fix add wait for monitor cleanup and duration for cleaup loop
yogesh1801 90543d3
fix fmt errors
yogesh1801 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
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
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
47 changes: 47 additions & 0 deletions
47
pgdog/src/frontend/client/query_engine/two_pc/statement.rs
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,47 @@ | ||
| //! Per-shard two-phase commit transaction names and control statements. | ||
|
|
||
| use super::TwoPcPhase; | ||
|
|
||
| /// Prepared transaction name for a coordinator transaction on one shard. | ||
| pub fn shard_name(transaction: &str, shard: usize) -> String { | ||
| format!("{transaction}_{shard}") | ||
| } | ||
|
|
||
| /// Build `PREPARE TRANSACTION`, `COMMIT PREPARED`, or `ROLLBACK PREPARED` | ||
| /// for a shard participant. | ||
| pub fn phase_control(transaction: &str, shard: usize, phase: TwoPcPhase) -> String { | ||
| let name = shard_name(transaction, shard); | ||
|
|
||
| match phase { | ||
| TwoPcPhase::Phase1 => format!("PREPARE TRANSACTION '{name}'"), | ||
| TwoPcPhase::Phase2 => format!("COMMIT PREPARED '{name}'"), | ||
| TwoPcPhase::Rollback => format!("ROLLBACK PREPARED '{name}'"), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn shard_name_appends_index() { | ||
| assert_eq!(shard_name("__pgdog_2pc_42", 0), "__pgdog_2pc_42_0"); | ||
| assert_eq!(shard_name("test_txn", 3), "test_txn_3"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn phase_control_statements() { | ||
| assert_eq!( | ||
| phase_control("test", 1, TwoPcPhase::Phase1), | ||
| "PREPARE TRANSACTION 'test_1'" | ||
| ); | ||
| assert_eq!( | ||
| phase_control("test", 1, TwoPcPhase::Phase2), | ||
| "COMMIT PREPARED 'test_1'" | ||
| ); | ||
| assert_eq!( | ||
| phase_control("test", 1, TwoPcPhase::Rollback), | ||
| "ROLLBACK PREPARED 'test_1'" | ||
| ); | ||
| } | ||
| } |
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.
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.
I'm not sure if it's really a problem, but the commit/rollback for prepared transactions happens inside
Manager::monitorhandler and it's async to this code meaning we may actually start new attempt before the manager logic is fired and the cleanup could happen in the middle of new copy attempt.@levkk wdyt? should we care about it?
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.
Maybe we can add a waiter to make sure the transaction is rolled back / committed?
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.
That makes sense. Should we add some code to wait for the transaction to be rolled back?
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.
yeah, something like this, but this should be probably done not just after the error, but when starting the new attempt, so we won't wait twice until the transaction is rolled back and then the sleep timeout between retries. We could wait for manager's queue exhausting, before starting to push data.
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.
@meskill @levkk have added a v1 for this problem, could you review this
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.
Taking a look!
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.
Sure thanks @levkk