-
Notifications
You must be signed in to change notification settings - Fork 212
fix(replication): replace custom AbortToken with tokio::CancellationToken #1131
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
meskill
wants to merge
1
commit into
main
Choose a base branch
from
meskill-2026-06-30-fix-replication---rzm
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
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 was deleted.
Oops, something went wrong.
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,19 +5,10 @@ | |||||
| //! | ||||||
| use std::sync::Arc; | ||||||
|
|
||||||
| use tokio::{ | ||||||
| spawn, | ||||||
| sync::{ | ||||||
| Semaphore, | ||||||
| mpsc::{UnboundedSender, unbounded_channel}, | ||||||
| }, | ||||||
| task::JoinHandle, | ||||||
| time::sleep, | ||||||
| }; | ||||||
| use tokio::{spawn, sync::Semaphore, task::JoinHandle, time::sleep}; | ||||||
| use tracing::{info, warn}; | ||||||
|
|
||||||
| use super::super::Error; | ||||||
| use super::AbortSignal; | ||||||
| use crate::backend::{ | ||||||
| Cluster, Pool, | ||||||
| pool::{Address, Request}, | ||||||
|
|
@@ -26,18 +17,20 @@ use crate::backend::{ | |||||
| use crate::frontend::client::query_engine::two_pc::Manager; | ||||||
| use crate::net::messages::Protocol; | ||||||
| use crate::util::escape_identifier; | ||||||
| use futures::{StreamExt, stream::FuturesUnordered}; | ||||||
| use tokio_util::sync::CancellationToken; | ||||||
|
|
||||||
| struct ParallelSync { | ||||||
| table: Table, | ||||||
| addr: Address, | ||||||
| dest: Cluster, | ||||||
| tx: UnboundedSender<Result<Table, Error>>, | ||||||
| permit: Arc<Semaphore>, | ||||||
| cancel: CancellationToken, | ||||||
| } | ||||||
|
|
||||||
| impl ParallelSync { | ||||||
| // Run parallel sync. | ||||||
| pub fn run(mut self) -> JoinHandle<Result<(), Error>> { | ||||||
| pub fn run(self) -> JoinHandle<Result<Table, Error>> { | ||||||
| spawn(async move { | ||||||
| // Record copy in queue before waiting for permit. | ||||||
| let tracker = TableCopy::new(&self.table.table.schema, &self.table.table.name); | ||||||
|
|
@@ -51,7 +44,7 @@ impl ParallelSync { | |||||
| .await | ||||||
| .map_err(|_| Error::ParallelConnection)?; | ||||||
|
|
||||||
| if self.tx.is_closed() { | ||||||
| if self.cancel.is_cancelled() { | ||||||
| return Err(Error::DataSyncAborted); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -61,25 +54,18 @@ impl ParallelSync { | |||||
|
|
||||||
| /// Retry loop: attempt the table copy up to `max_retries` times. | ||||||
| /// Abort signals and schema errors are not retried. | ||||||
| async fn run_with_retry(&mut self, tracker: &TableCopy) -> Result<(), Error> { | ||||||
| async fn run_with_retry(mut self, tracker: &TableCopy) -> Result<Table, Error> { | ||||||
| let max_retries = self.dest.resharding_copy_retry_max_attempts(); | ||||||
| let base_delay = *self.dest.resharding_copy_retry_min_delay(); | ||||||
| let mut attempt = 0usize; | ||||||
|
|
||||||
| loop { | ||||||
| let abort = AbortSignal::new(self.tx.clone()); | ||||||
|
|
||||||
| match self | ||||||
| .table | ||||||
| .data_sync(&self.addr, &self.dest, abort, tracker) | ||||||
| .data_sync(&self.addr, &self.dest, &self.cancel, tracker) | ||||||
| .await | ||||||
| { | ||||||
| Ok(_) => { | ||||||
| self.tx | ||||||
| .send(Ok(self.table.clone())) | ||||||
| .map_err(|_| Error::ParallelConnection)?; | ||||||
| return Ok(()); | ||||||
| } | ||||||
| Ok(_) => return Ok(self.table), | ||||||
| Err(err) if !err.is_retryable() || attempt >= max_retries => { | ||||||
| tracker.error(&err); | ||||||
| // Terminal failure: warn if rows remain so the operator can truncate. | ||||||
|
|
@@ -202,20 +188,24 @@ impl ParallelSyncManager { | |||||
| } | ||||||
|
|
||||||
| /// Run parallel table sync and return table LSNs when everything is done. | ||||||
| pub async fn run(self) -> Result<Vec<Table>, Error> { | ||||||
| pub async fn run(self, cancel: CancellationToken) -> Result<Vec<Table>, Error> { | ||||||
| info!( | ||||||
| "starting parallel table copy using {} replicas and {} parallel copies", | ||||||
| self.replicas.len(), | ||||||
| self.permit.available_permits() / self.replicas.len(), | ||||||
| ); | ||||||
|
|
||||||
| // Create a child cancel token with the guard to cancel the handles below | ||||||
| // in case any of it fails without affecting the parent task. | ||||||
| // If every handle succeed the guard token will just cancel already finished work | ||||||
| let cancel = cancel.child_token(); | ||||||
|
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. I think we should require the caller to call
Suggested change
|
||||||
| let _guard = cancel.clone().drop_guard(); | ||||||
|
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.
Suggested change
|
||||||
|
|
||||||
| // cycle() is the idiomatic "rewind": it restarts the iterator from the | ||||||
| // beginning once exhausted, giving round-robin distribution across replicas. | ||||||
| let mut replicas_iter = self.replicas.iter().cycle(); | ||||||
|
|
||||||
| let (tx, mut rx) = unbounded_channel(); | ||||||
| let mut tables = vec![]; | ||||||
| let mut handles = vec![]; | ||||||
| let mut handles = FuturesUnordered::new(); | ||||||
|
|
||||||
| for table in self.tables { | ||||||
| // SAFETY: cycle() on a non-empty slice never returns None. | ||||||
|
|
@@ -227,21 +217,19 @@ impl ParallelSyncManager { | |||||
| table, | ||||||
| addr: replica.addr().clone(), | ||||||
| dest: self.dest.clone(), | ||||||
| tx: tx.clone(), | ||||||
| permit: self.permit.clone(), | ||||||
| cancel: cancel.clone(), | ||||||
| } | ||||||
| .run(), | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| drop(tx); | ||||||
|
|
||||||
| while let Some(table) = rx.recv().await { | ||||||
| tables.push(table?); | ||||||
| } | ||||||
| let mut tables = Vec::with_capacity(handles.len()); | ||||||
|
|
||||||
| for handle in handles { | ||||||
| handle.await??; | ||||||
| // Short-circuit on first error and cancel other futures (JoinHandles that are not cancellable on drop) | ||||||
| // thanks to cancel guard. | ||||||
| while let Some(joined) = handles.next().await { | ||||||
| tables.push(joined??); | ||||||
| } | ||||||
|
|
||||||
| Ok(tables) | ||||||
|
|
||||||
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.