diff --git a/crates/ecstore/src/bucket/replication/replication_pool.rs b/crates/ecstore/src/bucket/replication/replication_pool.rs index 2cff9204df..e6c6784db2 100644 --- a/crates/ecstore/src/bucket/replication/replication_pool.rs +++ b/crates/ecstore/src/bucket/replication/replication_pool.rs @@ -38,8 +38,8 @@ use super::replication_resync_boundary::{ decode_resync_file, encode_mrf_file, should_auto_resume_resync, }; use super::replication_resyncer::{ - ReplicationResyncer, get_heal_replicate_object_info, replicate_delete, replicate_delete_with_outcome, replicate_object, - replicate_object_with_outcome, update_resync_status_cas, + DeleteReplicationAttempt, ReplicationResyncer, get_heal_replicate_object_info, replicate_delete_attempt, + replicate_delete_with_outcome, replicate_object, replicate_object_with_outcome, update_resync_status_cas, }; use super::replication_state::ReplicationStats; use super::replication_storage_boundary::{ @@ -1329,12 +1329,14 @@ impl ReplicationPool { let storage = self.storage.clone(); let stats = self.stats.clone(); let in_flight = self.in_flight.clone(); + let mrf_save_tx = self.mrf_save_tx.clone(); let handle = tokio::spawn(async move { let mut rx = rx; while let Some(operation) = rx.recv().await { let _active = ActiveWorkerGuard::new(active_counter.clone()); - process_replication_operation(operation, stats.clone(), storage.clone(), in_flight.clone()).await; + process_replication_operation(operation, stats.clone(), storage.clone(), in_flight.clone(), &mrf_save_tx) + .await; } }); @@ -1389,12 +1391,14 @@ impl ReplicationPool { let stats = self.stats.clone(); let storage = self.storage.clone(); let in_flight = self.in_flight.clone(); + let mrf_save_tx = self.mrf_save_tx.clone(); let handle = tokio::spawn(async move { let mut rx = rx; while let Some(operation) = rx.recv().await { let _active = ActiveWorkerGuard::new(active_counter.clone()); - process_replication_operation(operation, stats.clone(), storage.clone(), in_flight.clone()).await; + process_replication_operation(operation, stats.clone(), storage.clone(), in_flight.clone(), &mrf_save_tx) + .await; } }); @@ -1434,6 +1438,7 @@ impl ReplicationPool { let stats = self.stats.clone(); let storage = self.storage.clone(); let in_flight = self.in_flight.clone(); + let mrf_save_tx = self.mrf_save_tx.clone(); let mrf_rx = Arc::clone(&self.mrf_replica_rx); let handle = tokio::spawn(async move { @@ -1453,7 +1458,8 @@ impl ReplicationPool { let Some(operation) = operation else { break }; let _active = ActiveWorkerGuard::new(active_counter.clone()); - process_replication_operation(operation, stats.clone(), storage.clone(), in_flight.clone()).await; + process_replication_operation(operation, stats.clone(), storage.clone(), in_flight.clone(), &mrf_save_tx) + .await; } }); self.task_handles.lock().await.push(handle); @@ -1935,7 +1941,14 @@ impl ReplicationPool { ) { while let Some(operation) = rx.recv().await { let _active = ActiveWorkerGuard::new(active_counter.clone()); - process_replication_operation(operation, stats.clone(), self.storage.clone(), self.in_flight.clone()).await; + process_replication_operation( + operation, + stats.clone(), + self.storage.clone(), + self.in_flight.clone(), + &self.mrf_save_tx, + ) + .await; } } @@ -1953,7 +1966,8 @@ impl ReplicationPool { ) { while let Some(operation) = rx.recv().await { let _active = ActiveWorkerGuard::new(active_counter.clone()); - process_replication_operation(operation, stats.clone(), storage.clone(), self.in_flight.clone()).await; + process_replication_operation(operation, stats.clone(), storage.clone(), self.in_flight.clone(), &self.mrf_save_tx) + .await; } } @@ -1970,7 +1984,14 @@ impl ReplicationPool { ) { while let Some(operation) = rx.recv().await { let _active = ActiveWorkerGuard::new(active_counter.clone()); - process_replication_operation(operation, stats.clone(), self.storage.clone(), self.in_flight.clone()).await; + process_replication_operation( + operation, + stats.clone(), + self.storage.clone(), + self.in_flight.clone(), + &self.mrf_save_tx, + ) + .await; } } @@ -2539,6 +2560,7 @@ async fn process_replication_operation( stats: Arc, storage: Arc, in_flight: Arc, + mrf_save_tx: &Sender, ) { match operation { ReplicationOperation::Object(obj_info) => { @@ -2548,7 +2570,14 @@ async fn process_replication_operation( } ReplicationOperation::Delete(del_info) => { let _backlog = ReplicationBacklogGuard::for_delete(stats, del_info.as_ref()); - replicate_delete(*del_info, storage).await; + // The delete request already succeeded, so a source lock that is + // still held (a bulk delete keeps its key locks until the whole + // batch commits) must not cost the replication: journal it for + // the next MRF replay. + let retry_entry = del_info.to_mrf_entry(); + if replicate_delete_attempt(*del_info, storage).await == DeleteReplicationAttempt::SourceDeferred { + queue_mrf_save_entry(mrf_save_tx, retry_entry, "delete_source_deferred").await; + } } } } @@ -4506,6 +4535,102 @@ mod tests { } } + /// Runs one live delete through the worker and returns what it journaled for MRF replay. + async fn delete_worker_journal( + delete: DeletedObjectReplicationInfo, + writeback_error: EcstoreError, + ) -> Vec { + let shared = empty_resync_shared_state(); + *shared.delete_error.lock().expect("delete error lock should not be poisoned") = writeback_error; + let storage = Arc::new(LoadResyncNodeStore::new("delete-worker-journal", shared)); + let (mrf_save_tx, mut mrf_save_rx) = mpsc::channel(4); + + process_replication_operation( + ReplicationOperation::Delete(Box::new(delete)), + Arc::new(ReplicationStats::new()), + storage, + Arc::new(ReplicationInFlight::default()), + &mrf_save_tx, + ) + .await; + + drop(mrf_save_tx); + let mut journaled = Vec::new(); + while let Some(entry) = mrf_save_rx.recv().await { + journaled.push(entry); + } + journaled + } + + fn purged_on_target_delete( + arn: &str, + version_id: Uuid, + target_status: VersionPurgeStatusType, + ) -> DeletedObjectReplicationInfo { + DeletedObjectReplicationInfo { + bucket: "source".to_string(), + delete_object: ReplicationDeletedObject { + object_name: "purged".to_string(), + version_id: Some(version_id), + replication_state: Some(ReplicationState { + replicate_decision_str: test_replicate_decision(&[arn]).to_string(), + purge_targets: HashMap::from([(arn.to_string(), target_status)]), + ..Default::default() + }), + ..Default::default() + }, + op_type: ReplicationType::Delete, + ..Default::default() + } + } + + #[tokio::test] + async fn delete_worker_journals_a_delete_whose_source_writeback_lost_the_object_lock() { + let arn = "arn:test"; + let version_id = Uuid::new_v4(); + // A bulk delete of the same key still holds the object lock. + let lock_timeout = EcstoreError::Lock(rustfs_lock::LockError::internal( + "Lock acquisition timeout for resource 'source/purged@latest' after 5s".to_string(), + )); + + let journaled = + delete_worker_journal(purged_on_target_delete(arn, version_id, VersionPurgeStatusType::Complete), lock_timeout).await; + + assert_eq!( + journaled.len(), + 1, + "a delete whose result never reached the source must be kept for replay" + ); + let entry = &journaled[0]; + assert_eq!((entry.bucket.as_str(), entry.object.as_str()), ("source", "purged")); + assert_eq!(entry.version_id, Some(version_id)); + assert_eq!(entry.op, MrfOpKind::Delete); + assert_eq!(entry.target_arns, vec![arn.to_string()]); + } + + #[tokio::test] + async fn delete_worker_does_not_journal_deletes_other_paths_own() { + let arn = "arn:test"; + let source_gone = || EcstoreError::ObjectNotFound("source".to_string(), "purged".to_string()); + + let completed = delete_worker_journal( + purged_on_target_delete(arn, Uuid::new_v4(), VersionPurgeStatusType::Complete), + source_gone(), + ) + .await; + assert!(completed.is_empty(), "a completed delete must not be replayed"); + + let target_pending = delete_worker_journal( + purged_on_target_delete(arn, Uuid::new_v4(), VersionPurgeStatusType::Pending), + source_gone(), + ) + .await; + assert!( + target_pending.is_empty(), + "unfinished target work is recorded in the source state for the scanner, not journaled per delete" + ); + } + #[tokio::test] async fn delete_worker_preserves_persisted_admission_without_live_config() { let arn = "arn:test"; diff --git a/crates/ecstore/src/bucket/replication/replication_resyncer.rs b/crates/ecstore/src/bucket/replication/replication_resyncer.rs index 598bd56ba2..d15d475c08 100644 --- a/crates/ecstore/src/bucket/replication/replication_resyncer.rs +++ b/crates/ecstore/src/bucket/replication/replication_resyncer.rs @@ -2422,12 +2422,40 @@ pub async fn replicate_delete(dobj: DeletedObjectReplicat let _ = replicate_delete_with_outcome(dobj, storage).await; } +/// How one delete replication attempt ended. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum DeleteReplicationAttempt { + /// Every admitted target finished and the source state was persisted. + Completed, + /// The attempt did not finish for a reason another path already owns: a + /// target failure is recorded in the persisted state for the scanner, and + /// a force delete or delete-marker purge journals its own intent. + Unfinished, + /// A source-side lock or read kept the attempt from running, or from + /// persisting its result. Nothing durable records that, so the caller must + /// keep the delete for a later attempt. + SourceDeferred, +} + +impl DeleteReplicationAttempt { + fn from_finished(finished: bool) -> Self { + if finished { Self::Completed } else { Self::Unfinished } + } +} + pub(crate) async fn replicate_delete_with_outcome( dobj: DeletedObjectReplicationInfo, storage: Arc, ) -> bool { + replicate_delete_attempt(dobj, storage).await == DeleteReplicationAttempt::Completed +} + +pub(crate) async fn replicate_delete_attempt( + dobj: DeletedObjectReplicationInfo, + storage: Arc, +) -> DeleteReplicationAttempt { if dobj.delete_object.force_delete { - return replicate_force_delete_to_targets(&dobj, storage).await; + return DeleteReplicationAttempt::from_finished(replicate_force_delete_to_targets(&dobj, storage).await); } let bucket = dobj.bucket.clone(); @@ -2472,7 +2500,7 @@ pub(crate) async fn replicate_delete_with_outcome( user_agent: "Internal: [Replication]".to_string(), ..Default::default() }); - return false; + return DeleteReplicationAttempt::Unfinished; } }; let target_arns = dobj.admitted_target_arns(); @@ -2480,7 +2508,7 @@ pub(crate) async fn replicate_delete_with_outcome( // Batch expiry can return an empty state after removing the source. // It owes no remote work or local writeback. Persisted target admission // must still be honored even when the live configuration is gone. - return true; + return DeleteReplicationAttempt::Completed; } if dobj.delete_object.delete_marker @@ -2502,7 +2530,7 @@ pub(crate) async fn replicate_delete_with_outcome( reason = "source_not_delete_marker", "Skipping stale delete-marker replication" ); - return true; + return DeleteReplicationAttempt::Completed; } Err(err) if is_err_object_not_found(&err) || is_err_version_not_found(&err) => { debug!( @@ -2520,7 +2548,7 @@ pub(crate) async fn replicate_delete_with_outcome( // purge-intent replay landing here on purpose). Purge instead // of just skipping; the result decides whether an MRF replay // may acknowledge the entry. - return purge_stale_delete_marker_targets(&bucket, &dobj).await; + return DeleteReplicationAttempt::from_finished(purge_stale_delete_marker_targets(&bucket, &dobj).await); } Err(err) => { // A transient source error (lock timeout, IO error) must not @@ -2529,7 +2557,7 @@ pub(crate) async fn replicate_delete_with_outcome( // target mint one more delete marker (rustfs#6823). Fail the // entry without touching the target; the MRF replay / heal // scanner retries once the source is readable again. - debug!( + warn!( event = EVENT_REPLICATION_DELETE_SKIPPED, component = LOG_COMPONENT_ECSTORE, subsystem = LOG_SUBSYSTEM_REPLICATION_RESYNC, @@ -2554,7 +2582,7 @@ pub(crate) async fn replicate_delete_with_outcome( user_agent: "Internal: [Replication]".to_string(), ..Default::default() }); - return false; + return DeleteReplicationAttempt::SourceDeferred; } } } @@ -2565,7 +2593,7 @@ pub(crate) async fn replicate_delete_with_outcome( { Ok(ns_lock) => ns_lock, Err(e) => { - debug!( + warn!( event = EVENT_REPLICATION_DELETE_SKIPPED, component = LOG_COMPONENT_ECSTORE, subsystem = LOG_SUBSYSTEM_REPLICATION_RESYNC, @@ -2589,14 +2617,14 @@ pub(crate) async fn replicate_delete_with_outcome( user_agent: "Internal: [Replication]".to_string(), ..Default::default() }); - return false; + return DeleteReplicationAttempt::SourceDeferred; } }; let _lock_guard = match ns_lock.get_write_lock(ReplicationLockTiming::acquire_timeout()).await { Ok(lock_guard) => lock_guard, Err(e) => { - debug!( + warn!( event = EVENT_REPLICATION_DELETE_SKIPPED, component = LOG_COMPONENT_ECSTORE, subsystem = LOG_SUBSYSTEM_REPLICATION_RESYNC, @@ -2620,7 +2648,7 @@ pub(crate) async fn replicate_delete_with_outcome( user_agent: "Internal: [Replication]".to_string(), ..Default::default() }); - return false; + return DeleteReplicationAttempt::SourceDeferred; } }; @@ -2858,7 +2886,12 @@ pub(crate) async fn replicate_delete_with_outcome( } }; - replicate_delete_outcome( + if !state_persisted { + // The targets were driven, but the result never reached the source + // metadata, so a scanner pass would still see the pre-attempt state. + return DeleteReplicationAttempt::SourceDeferred; + } + DeleteReplicationAttempt::from_finished(replicate_delete_outcome( expected_targets, rinfos.targets.len(), state_persisted, @@ -2866,7 +2899,7 @@ pub(crate) async fn replicate_delete_with_outcome( // error returns early above instead of replicating unverified. true, &replication_status, - ) + )) } /// Reads the source version addressed by `delete_marker_version_id` so a