From bccaacff2763ed808fdfd6f3c08f6ae3d1c83abc Mon Sep 17 00:00:00 2001 From: Hauser Zhi Date: Wed, 2 Dec 2026 22:36:56 +0800 Subject: [PATCH] fix(ecstore): require drive RPC evidence for remote recovery Keep TCP probe cadence and peer reachability separate from disk I/O progress. Use the existing single-flight recovery monitor for degraded disks and discard a connectivity failure overtaken by successful drive I/O. Co-Authored-By: heihutu Co-Authored-By: zhi22915 Co-Authored-By: qunjiazhu Signed-off-by: Hauser Zhi --- crates/ecstore/src/cluster/rpc/remote_disk.rs | 200 +++++++++--------- crates/ecstore/src/disk/disk_store.rs | 35 +-- 2 files changed, 106 insertions(+), 129 deletions(-) diff --git a/crates/ecstore/src/cluster/rpc/remote_disk.rs b/crates/ecstore/src/cluster/rpc/remote_disk.rs index e3114c5d69..9ae6900b4b 100644 --- a/crates/ecstore/src/cluster/rpc/remote_disk.rs +++ b/crates/ecstore/src/cluster/rpc/remote_disk.rs @@ -2297,7 +2297,10 @@ impl RemoteDisk { let handle_id = self.handle_id; let recovery_monitor_active = Arc::clone(&self.recovery_monitor_active); #[cfg(test)] - let recovery_monitor_teardown_hook = Arc::clone(&self.recovery_monitor_teardown_hook); + let recovery_test_state = RecoveryMonitorTestState { + start_count: Arc::clone(&self.recovery_monitor_start_count), + teardown_hook: Arc::clone(&self.recovery_monitor_teardown_hook), + }; tokio::spawn(async move { Self::monitor_remote_disk_health( @@ -2308,13 +2311,13 @@ impl RemoteDisk { cancel_token, recovery_monitor_active, #[cfg(test)] - recovery_monitor_teardown_hook, + recovery_test_state, ) .await; }); } - /// Monitor remote disk health periodically + /// Sample peer reachability and hand disk recovery to the disk-specific probe. async fn monitor_remote_disk_health( addr: String, endpoint: Endpoint, @@ -2322,50 +2325,16 @@ impl RemoteDisk { health: Arc, cancel_token: CancellationToken, recovery_monitor_active: Arc, - #[cfg(test)] recovery_monitor_teardown_hook: Arc>>>, + #[cfg(test)] recovery_test_state: RecoveryMonitorTestState, ) { let mut interval = time::interval(get_drive_active_check_interval()); - - // A TCP handshake detects reachability. Only an RPC response can prove progress - // when a process has stopped servicing requests but the kernel still accepts TCP. - let initial_probe_ok = Self::perform_connectivity_check(&addr).await.is_ok(); - if initial_probe_ok { - health.record_connectivity_success(&endpoint, "connectivity_probe_success"); - rustfs_io_metrics::internode_metrics::record_peer_reachable_if_offline(&addr); - } else if health.mark_failure(&endpoint, "connectivity_probe_failed") { - warn!( - event = EVENT_REMOTE_DISK_HEALTH, - component = LOG_COMPONENT_ECSTORE, - subsystem = LOG_SUBSYSTEM_REMOTE_DISK, - endpoint = %endpoint, - addr, - state = "initial_probe_failed", - result = "mark_faulty", - "Remote disk initial health probe failed" - ); - - // Start recovery monitoring - let health_clone = Arc::clone(&health); - let addr_clone = addr.clone(); - let endpoint_clone = endpoint.clone(); - let cancel_clone = cancel_token.clone(); - Self::schedule_recovery_monitor( - addr_clone, - endpoint_clone, - handle_id, - health_clone, - cancel_clone, - Arc::clone(&recovery_monitor_active), - #[cfg(test)] - RecoveryMonitorTestState { - start_count: Arc::new(AtomicU32::new(0)), - teardown_hook: Arc::clone(&recovery_monitor_teardown_hook), - }, - ); - } + interval.set_missed_tick_behavior(time::MissedTickBehavior::Skip); + let mut initial_probe = true; + let mut next_tcp_probe = time::Instant::now(); loop { tokio::select! { + biased; _ = cancel_token.cancelled() => { debug!( event = EVENT_REMOTE_DISK_HEALTH, @@ -2378,65 +2347,53 @@ impl RemoteDisk { ); return; } - _ = interval.tick() => { - if cancel_token.is_cancelled() { - return; - } - - // Skip health check if disk is already marked as faulty - if health.is_faulty() { - continue; - } - - let last_success_nanos = health.last_success.load(Ordering::Relaxed); - let elapsed = Duration::from_nanos( - (std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .expect("operation should succeed") - .as_nanos() as i64 - last_success_nanos) as u64 - ); - - if elapsed < SKIP_IF_SUCCESS_BEFORE { - continue; - } - - // Perform basic connectivity check - if Self::perform_connectivity_check(&addr).await.is_ok() { - health.record_connectivity_success(&endpoint, "connectivity_probe_success"); - rustfs_io_metrics::internode_metrics::record_peer_reachable_if_offline(&addr); - } else if health.mark_failure(&endpoint, "connectivity_probe_failed") { - warn!( - event = EVENT_REMOTE_DISK_HEALTH, - component = LOG_COMPONENT_ECSTORE, - subsystem = LOG_SUBSYSTEM_REMOTE_DISK, - endpoint = %endpoint, - addr, - state = "probe_failed", - result = "mark_faulty", - "Remote disk health probe failed" - ); - - // Start recovery monitoring - let health_clone = Arc::clone(&health); - let addr_clone = addr.clone(); - let endpoint_clone = endpoint.clone(); - let cancel_clone = cancel_token.clone(); - Self::schedule_recovery_monitor( - addr_clone, - endpoint_clone, - handle_id, - health_clone, - cancel_clone, - Arc::clone(&recovery_monitor_active), - #[cfg(test)] - RecoveryMonitorTestState { - start_count: Arc::new(AtomicU32::new(0)), - teardown_hook: Arc::clone(&recovery_monitor_teardown_hook), - }, - ); - } - } + _ = interval.tick() => {} } + let initial = std::mem::replace(&mut initial_probe, false); + if recovery_monitor_active.load(Ordering::Acquire) + || (!initial + && (health.is_faulty() + || time::Instant::now() < next_tcp_probe + || health.time_since_success() < SKIP_IF_SUCCESS_BEFORE)) + { + continue; + } + + let completed_before = health.completed_ops(); + if Self::perform_connectivity_check(&addr).await.is_ok() { + // TCP reachability can throttle connectivity probes. A DiskInfo + // reply owns the recovery decision and the drive's I/O timestamp. + next_tcp_probe = time::Instant::now() + SKIP_IF_SUCCESS_BEFORE; + rustfs_io_metrics::internode_metrics::record_peer_reachable_if_offline(&addr); + if health.runtime_state() == RuntimeDriveHealthState::Online { + continue; + } + } else { + if health.mark_failure_unless_progressed(&endpoint, "connectivity_probe_failed", completed_before) != Some(true) { + continue; + } + warn!( + event = EVENT_REMOTE_DISK_HEALTH, + component = LOG_COMPONENT_ECSTORE, + subsystem = LOG_SUBSYSTEM_REMOTE_DISK, + endpoint = %endpoint, + addr, + state = if initial { "initial_probe_failed" } else { "probe_failed" }, + result = "mark_faulty", + "Remote disk health probe failed" + ); + } + + Self::schedule_recovery_monitor( + addr.clone(), + endpoint.clone(), + handle_id, + Arc::clone(&health), + cancel_token.clone(), + Arc::clone(&recovery_monitor_active), + #[cfg(test)] + recovery_test_state.clone(), + ); } } @@ -11060,6 +11017,49 @@ mod tests { .await; } + #[tokio::test] + async fn tcp_reachability_leaves_suspect_drive_to_disk_info_recovery() { + runtime_sources::ensure_test_rpc_secret(); + let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind TCP-only peer"); + let addr = listener.local_addr().expect("listen address"); + let endpoint = Endpoint::try_from(format!("http://{addr}/data/rustfs0").as_str()).expect("remote endpoint"); + let disk = RemoteDisk::new( + &endpoint, + &DiskOption { + cleanup: false, + health_check: true, + }, + Arc::new(TcpHttpInternodeDataTransport), + ) + .await + .expect("remote disk"); + disk.health.mark_failure(&endpoint, "test_drive_timeout"); + assert_eq!(disk.runtime_state(), RuntimeDriveHealthState::Suspect); + let last_drive_success = disk.health.last_success.load(Ordering::Relaxed); + let completed_before = disk.health.completed_ops(); + + disk.enable_health_check(); + let (stream, _) = time::timeout(Duration::from_secs(2), listener.accept()) + .await + .expect("active monitor makes a TCP connection") + .expect("accept TCP connection"); + let observed = time::timeout(Duration::from_secs(2), async { + while disk.runtime_state() == RuntimeDriveHealthState::Suspect + && !disk.recovery_monitor_active.load(Ordering::Acquire) + { + tokio::task::yield_now().await; + } + }) + .await; + + assert!(observed.is_ok(), "the TCP-only peer must leave recovery to a drive RPC"); + assert_ne!(disk.runtime_state(), RuntimeDriveHealthState::Online); + assert!(disk.recovery_monitor_active.load(Ordering::Acquire)); + assert_eq!(disk.health.last_success.load(Ordering::Relaxed), last_drive_success); + assert_eq!(disk.health.completed_ops(), completed_before); + drop(stream); + } + #[tokio::test] async fn test_remote_disk_recovery_requires_disk_rpc_readiness() { init_tracing(Level::ERROR); diff --git a/crates/ecstore/src/disk/disk_store.rs b/crates/ecstore/src/disk/disk_store.rs index fbd219b4e9..b09f5895dc 100644 --- a/crates/ecstore/src/disk/disk_store.rs +++ b/crates/ecstore/src/disk/disk_store.rs @@ -1263,14 +1263,12 @@ impl DiskHealthTracker { /// Log a successful operation pub fn log_success(&self) { - self.record_success_time(true); + self.last_success.store(current_unix_nanos(), Ordering::Relaxed); + self.completed_ops.fetch_add(1, Ordering::Relaxed); } - fn record_success_time(&self, drive_progress: bool) { - self.last_success.store(current_unix_nanos(), Ordering::Relaxed); - if drive_progress { - self.completed_ops.fetch_add(1, Ordering::Relaxed); - } + pub(crate) fn time_since_success(&self) -> Duration { + elapsed_since(self.last_success.load(Ordering::Relaxed), current_unix_nanos()) } pub fn completed_ops(&self) -> u64 { @@ -1471,10 +1469,6 @@ impl DiskHealthTracker { } pub fn mark_recovery_success(&self, endpoint: &Endpoint, reason: &'static str) -> bool { - self.recover_after_success(endpoint, reason, true) - } - - fn recover_after_success(&self, endpoint: &Endpoint, reason: &'static str, drive_progress: bool) -> bool { #[cfg(test)] if self.test_forced_offline.load(Ordering::Acquire) { return false; @@ -1505,7 +1499,7 @@ impl DiskHealthTracker { } self.transition_state(endpoint, current, next, reason); if became_online { - self.record_success_time(drive_progress); + self.log_success(); } became_online } @@ -1535,14 +1529,6 @@ impl DiskHealthTracker { } } - pub(crate) fn record_connectivity_success(&self, endpoint: &Endpoint, reason: &'static str) { - if self.runtime_state() == RuntimeDriveHealthState::Online { - self.record_success_time(false); - } else { - self.recover_after_success(endpoint, reason, false); - } - } - fn transition_state( &self, endpoint: &Endpoint, @@ -2108,10 +2094,7 @@ impl LocalDiskWrapper { continue; } - let last_success_nanos = health.last_success.load(Ordering::Relaxed); - let elapsed = elapsed_since(last_success_nanos, current_unix_nanos()); - - if elapsed < SKIP_IF_SUCCESS_BEFORE { + if health.time_since_success() < SKIP_IF_SUCCESS_BEFORE { continue; } @@ -6947,12 +6930,6 @@ mod tests { let health = DiskHealthTracker::new(); let before = health.completed_ops(); - assert!(!health.mark_failure(&endpoint, "operation_timeout")); - assert_eq!(health.runtime_state(), RuntimeDriveHealthState::Suspect); - health.record_connectivity_success(&endpoint, "connectivity_probe_success"); - assert_eq!(health.runtime_state(), RuntimeDriveHealthState::Online); - assert_eq!(health.completed_ops(), before, "a TCP handshake does not complete drive I/O"); - // Sibling operation completes while a queued operation is waiting. health.record_operation_success(&endpoint, "operation_success"); assert!(health.completed_ops() > before);