-
Notifications
You must be signed in to change notification settings - Fork 273
feat: allow source and destination clusters to overlap #1473
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
Open
levkk
wants to merge
3
commits into
main
Choose a base branch
from
levkk-omni-only-sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
|
|
||
| use futures::future::join_all; | ||
| use pg_raw_parse::Node; | ||
| use tracing::debug; | ||
| use tracing::{debug, warn}; | ||
|
|
||
| use crate::frontend::client::query_engine::TwoPcPhase; | ||
| use crate::frontend::client::query_engine::two_pc::{ | ||
|
|
@@ -13,15 +13,17 @@ use crate::frontend::client::query_engine::two_pc::{ | |
| use crate::frontend::router::parser::Error as ParseError; | ||
| use crate::{ | ||
| backend::{Cluster, ConnectReason, replication::subscriber::ParallelConnection}, | ||
| config::Role, | ||
| frontend::router::parser::{CopyParser, Shard}, | ||
| net::{ | ||
| CopyData, CopyDone, ErrorResponse, FromBytes, Message, Protocol, ProtocolMessage, Query, | ||
| ToBytes, | ||
| }, | ||
| }; | ||
|
|
||
| use super::super::{CopyStatement, Error}; | ||
| use super::{ | ||
| super::{CopyStatement, Error}, | ||
| OverlappingShardsCheck, | ||
| }; | ||
|
|
||
| // Not really needed, but we're currently | ||
| // sharding 3 CopyData messages at a time. | ||
|
|
@@ -31,8 +33,8 @@ static BUFFER_SIZE: usize = 3; | |
| #[derive(Debug)] | ||
| pub(crate) struct CopySubscriber { | ||
| copy: CopyParser, | ||
| /// Destination cluster. | ||
| cluster: Cluster, | ||
| dest: Cluster, | ||
| source: Cluster, | ||
| buffer: Vec<CopyData>, | ||
| connections: Vec<ParallelConnection>, | ||
| stmt: CopyStatement, | ||
|
|
@@ -48,12 +50,12 @@ impl CopySubscriber { | |
| pub(crate) fn new( | ||
| copy_stmt: &CopyStatement, | ||
| source: &Cluster, | ||
| cluster: &Cluster, | ||
| dest: &Cluster, | ||
| ) -> Result<Self, Error> { | ||
| let ast = pg_raw_parse::parse(©_stmt.copy_in()).map_err(ParseError::from)?; | ||
| let stmt = ast.stmts().next().ok_or(ParseError::EmptyQuery)?; | ||
| let mut copy = if let Node::CopyStmt(stmt) = stmt { | ||
| CopyParser::new(stmt, cluster).map_err(|_| Error::MissingData)? | ||
| CopyParser::new(stmt, dest).map_err(|_| Error::MissingData)? | ||
| } else { | ||
| return Err(Error::MissingData); | ||
| }; | ||
|
|
@@ -63,7 +65,8 @@ impl CopySubscriber { | |
|
|
||
| Ok(Self { | ||
| copy, | ||
| cluster: cluster.clone(), | ||
| dest: dest.clone(), | ||
| source: source.clone(), | ||
| buffer: vec![], | ||
| connections: vec![], | ||
| stmt: copy_stmt.clone(), | ||
|
|
@@ -73,20 +76,32 @@ impl CopySubscriber { | |
|
|
||
| /// Connect to all shards. One connection per primary. | ||
| pub(crate) async fn connect(&mut self) -> Result<(), Error> { | ||
| let mut servers = vec![]; | ||
| for shard in self.cluster.shards() { | ||
| let primary = shard | ||
| .pools_with_roles() | ||
| .iter() | ||
| .find(|(role, _)| role == &Role::Primary) | ||
| .ok_or(Error::NoPrimary)? | ||
| .1 | ||
| .standalone(ConnectReason::Replication) | ||
| .await?; | ||
| servers.push(ParallelConnection::new(primary)?); | ||
| let mut connections = vec![]; | ||
| let overlap_check = OverlappingShardsCheck::new(&self.source); | ||
| let destination_shards = self.dest.shards(); | ||
|
|
||
| if destination_shards.is_empty() { | ||
| return Err(Error::DestinationNoShards); | ||
| } | ||
|
|
||
| self.connections = servers; | ||
| for (shard_number, shard) in destination_shards.iter().enumerate() { | ||
| if overlap_check.overlaps(shard)? { | ||
| warn!( | ||
| "skipping data sync to {} because it is part of the source cluster", | ||
| shard.primary_address()?, | ||
| ); | ||
| continue; | ||
|
Comment on lines
+89
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some edge case to validate maybe: check if the shard number is different in source/destination cluster, just in case |
||
| } | ||
|
|
||
| let primary = shard.primary_standalone(ConnectReason::Replication).await?; | ||
| connections.push(ParallelConnection::new(primary, shard_number)?); | ||
| } | ||
|
|
||
| if connections.is_empty() { | ||
| return Err(Error::SourceDestinationIdentical); | ||
| } | ||
|
|
||
| self.connections = connections; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
@@ -227,13 +242,14 @@ impl CopySubscriber { | |
| // scope). Shards not yet committed roll back on connection close. The | ||
| // destination_has_rows() guard in parallel_sync.rs prevents a doomed retry if this | ||
| // window is ever hit. | ||
| if self.cluster.two_pc_enabled() { | ||
| if self.dest.two_pc_enabled() { | ||
| self.commit_two_pc().await?; | ||
| } else { | ||
| for (shard, server) in self.connections.iter_mut().enumerate() { | ||
| for server in &mut self.connections { | ||
| if let Err(error) = | ||
| Self::send_and_confirm(server, Query::new("COMMIT").into()).await | ||
| { | ||
| let shard = server.shard_number(); | ||
| tracing::error!( | ||
| "COMMIT failed on destination shard {shard} during copy_done: {error}; \ | ||
| shards committed before it stay committed, the rest roll back on \ | ||
|
|
@@ -250,7 +266,7 @@ impl CopySubscriber { | |
| async fn commit_two_pc(&mut self) -> Result<(), Error> { | ||
| let manager = Manager::get(); | ||
| let txn = TwoPcTransaction::new(); | ||
| let identifier = self.cluster.identifier(); | ||
| let identifier = self.dest.identifier(); | ||
|
|
||
| async { | ||
| let _guard_phase_1 = manager | ||
|
|
@@ -280,14 +296,14 @@ impl CopySubscriber { | |
| ) -> Result<(), Error> { | ||
| let mut futures = Vec::new(); | ||
|
|
||
| for (shard, server) in self.connections.iter_mut().enumerate() { | ||
| for server in &mut self.connections { | ||
| // Rollback is not issued here. If this path fails, the TwoPcGuards in | ||
| // commit_two_pc() are dropped without manager.done(), and the 2PC Manager | ||
| // cleanup task issues ROLLBACK PREPARED (Phase1) or COMMIT PREPARED (Phase2) | ||
| // via binding.rs using the same phase_control() helper. | ||
| let query = match phase { | ||
| TwoPcPhase::Rollback => unreachable!(), | ||
| phase => phase_control(txn, shard, phase), | ||
| phase => phase_control(txn, server.shard_number(), phase), | ||
| }; | ||
| futures.push(Self::send_and_confirm(server, Query::new(query).into())); | ||
| } | ||
|
|
@@ -317,16 +333,16 @@ impl CopySubscriber { | |
| let bytes = result.iter().map(|row| row.len()).sum::<usize>(); | ||
|
|
||
| for row in &result { | ||
| for (shard, server) in self.connections.iter_mut().enumerate() { | ||
| for server in &mut self.connections { | ||
| match row.shard() { | ||
| Shard::All => server.send_one(&row.message().into()).await?, | ||
| Shard::Direct(destination) => { | ||
| if *destination == shard { | ||
| if *destination == server.shard_number() { | ||
| server.send_one(&row.message().into()).await?; | ||
| } | ||
| } | ||
| Shard::Multi(multi) => { | ||
| if multi.contains(&shard) { | ||
| if multi.contains(&server.shard_number()) { | ||
| server.send_one(&row.message().into()).await?; | ||
| } | ||
| } | ||
|
|
@@ -402,7 +418,7 @@ mod test { | |
| .await | ||
| .unwrap(); | ||
|
|
||
| let mut subscriber = CopySubscriber::new(©, &cluster, &cluster).unwrap(); | ||
| let mut subscriber = CopySubscriber::new(©, &Cluster::default(), &cluster).unwrap(); | ||
| subscriber.start_copy().await.unwrap(); | ||
|
|
||
| let header = CopyData::new(&Header::default().to_bytes()); | ||
|
|
@@ -445,7 +461,7 @@ mod test { | |
| crate::logger(); | ||
|
|
||
| let server = test_server().await; | ||
| let mut conn = ParallelConnection::new(server).unwrap(); | ||
| let mut conn = ParallelConnection::new(server, 0).unwrap(); | ||
|
|
||
| // RAISE WARNING emits a NoticeResponse ('N') before the statement's | ||
| // CommandComplete. Without async-message skipping this was misread as | ||
|
|
@@ -468,7 +484,7 @@ mod test { | |
| crate::logger(); | ||
|
|
||
| let server = test_server().await; | ||
| let mut conn = ParallelConnection::new(server).unwrap(); | ||
| let mut conn = ParallelConnection::new(server, 0).unwrap(); | ||
|
|
||
| // A NoticeResponse precedes the ErrorResponse. The notice is skipped, the | ||
| // error is surfaced, and drain_to_ready consumes the trailing ReadyForQuery | ||
|
|
||
29 changes: 29 additions & 0 deletions
29
pgdog/src/backend/replication/logical/subscriber/duplicate_check.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,29 @@ | ||
| //! Check that the source and destination clusters | ||
| //! don't have overlapping shards. | ||
|
|
||
| use super::super::Error; | ||
| use crate::backend::{Cluster, Shard}; | ||
|
|
||
| pub(super) struct OverlappingShardsCheck<'a> { | ||
| source: &'a Cluster, | ||
| } | ||
|
|
||
| impl<'a> OverlappingShardsCheck<'a> { | ||
| /// Create check. | ||
| pub(super) fn new(source: &'a Cluster) -> Self { | ||
| Self { source } | ||
| } | ||
|
|
||
| /// Check if the destination shard overlaps with any shards in the source cluster. | ||
| pub(super) fn overlaps(&self, shard: &Shard) -> Result<bool, Error> { | ||
| let address = shard.primary_address()?; | ||
|
|
||
| for source_shard in self.source.shards() { | ||
| if source_shard.primary_address()?.same_database(address) { | ||
| return Ok(true); | ||
| } | ||
| } | ||
|
|
||
| Ok(false) | ||
| } | ||
| } |
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.
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 wonder is we should use the
Shardtype or similar everywhere instead of tuples (shard_number, smth). There is a number already inside theShardand we could simplify it in places like this and maybe reuse instead of adding shard_number to other structs