mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-27 15:37:02 +00:00
fix(lock): refresh object write locks with a heartbeat (#4388)
fix(lock): renew distributed locks via heartbeat; wire server refresh (backlog#899) A held distributed write lock had a fixed 30s TTL and was never renewed, so any operation exceeding it got its per-node lease reclaimed and stolen by a contender, causing split-brain writes. This implements Phase 0 + Phase 1 of the #899 design (Phase 2 abort-on-loss is deferred and tracked in code comments). Phase 0 (server refresh wiring, P0): - node_service handle_refresh was a no-op stub that parsed args then always returned success=true. Extract a testable `refresh_lock` free function that actually delegates to the node's lock backend. Not-found maps to success=false with no error_info, so RemoteClient::refresh keeps its Ok(resp.success) semantics and yields a real not_found signal to the coordinator heartbeat. Without this, client heartbeats were silently no-ops. Phase 1 (client heartbeat + safe interval + observability): - DistributedLockGuard spawns a heartbeat that refreshes every per-client lease on a derived interval; interval is derived without Duration::clamp (entries<=1 or interval>=ttl => no spawn), fixing the sub-second-ttl panic. - Add LockLostSignal: declare the lock lost when not_found exceeds entries.len() - refresh_quorum; RPC errors are not counted (absorbed by the ttl > interval margin). Expose is_lock_lost()/lock_lost() for observers. - disarm(), release(), and Drop now abort the heartbeat before releasing so no refresh races the unlock (refresh only extends, never creates, a lease). - Reclaim path stays behaviorally unchanged but now warns with owner/resource/ lease age and records a metric (#698 scavenger preserved). - Add DEFAULT_LOCK_REFRESH_INTERVAL, LockRequest.refresh_interval (serde default for RPC back-compat) + builder, and lock lifecycle metrics. Open questions adopt the design's documented defaults (marked TODO in code): refresh not-found -> Ok(false)/error_info=None; lost-quorum base entries.len(); DEFAULT_LOCK_REFRESH_INTERVAL=10s. Tests: heartbeat keepalive/quorum-loss/jitter/boundary/disarm (lock crate), server refresh delegation (rustfs), and end-to-end survives-past-ttl plus crashed-owner-reclaim regressions (namespace).
This commit is contained in:
@@ -1235,3 +1235,94 @@ async fn test_namespace_lock_distributed_failure_retries_and_cleans_up_late_succ
|
||||
|
||||
drop(write_guard);
|
||||
}
|
||||
|
||||
// C1 -- a long-held write lock survives past its ttl thanks to the heartbeat; a
|
||||
// contender fails to steal it while it is held (#899 direct regression).
|
||||
// Without a heartbeat: owner-a's per-node entries expire after ttl, owner-b triggers
|
||||
// reclaim and steals it -> contended.is_some() -> assertion fails (red). Once fixed the
|
||||
// entries stay refreshed -> is_none() (green).
|
||||
#[tokio::test]
|
||||
async fn distributed_write_lock_survives_past_ttl_with_heartbeat() {
|
||||
let managers = (0..3).map(|_| Arc::new(GlobalLockManager::new())).collect::<Vec<_>>();
|
||||
let clients = managers
|
||||
.iter()
|
||||
.map(|m| Arc::new(LocalClient::with_manager(m.clone())) as Arc<dyn LockClient>)
|
||||
.collect::<Vec<_>>();
|
||||
let lock = Arc::new(NamespaceLock::with_clients("heartbeat-keepalive".to_string(), clients));
|
||||
let resource = create_test_object_key("bucket", "object-heartbeat-keepalive");
|
||||
|
||||
let req_a = LockRequest::new(resource.clone(), LockType::Exclusive, "owner-a")
|
||||
.with_acquire_timeout(Duration::from_millis(200))
|
||||
.with_ttl(Duration::from_millis(150))
|
||||
.with_refresh_interval(Duration::from_millis(40)); // < ttl -> spawn heartbeat
|
||||
|
||||
let guard_a = lock
|
||||
.acquire_guard(&req_a)
|
||||
.await
|
||||
.expect("owner-a acquire should not error")
|
||||
.expect("owner-a should hold the distributed write lock");
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(400)).await; // hold well past a single ttl window
|
||||
|
||||
let req_b = LockRequest::new(resource.clone(), LockType::Exclusive, "owner-b")
|
||||
.with_acquire_timeout(Duration::from_millis(150))
|
||||
.with_ttl(Duration::from_millis(150));
|
||||
let contended = lock.acquire_guard(&req_b).await.expect("owner-b acquire should not error");
|
||||
assert!(
|
||||
contended.is_none(),
|
||||
"heartbeat must keep owner-a's lock alive; owner-b must not steal it past ttl"
|
||||
);
|
||||
|
||||
drop(guard_a);
|
||||
let mut acquired_after_release = false;
|
||||
for _ in 0..20 {
|
||||
let req_c = LockRequest::new(resource.clone(), LockType::Exclusive, "owner-b")
|
||||
.with_acquire_timeout(Duration::from_millis(150))
|
||||
.with_ttl(Duration::from_millis(150));
|
||||
if let Some(g) = lock.acquire_guard(&req_c).await.expect("acquire should not error") {
|
||||
drop(g);
|
||||
acquired_after_release = true;
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
assert!(acquired_after_release, "after owner-a releases, owner-b must eventually acquire");
|
||||
}
|
||||
|
||||
// C2 -- a crashed owner (leaving orphan entries that are never refreshed again) has its
|
||||
// lock eventually reclaimed; no permanent deadlock (#698 scavenger). Must stay green.
|
||||
#[tokio::test]
|
||||
async fn crashed_owner_distributed_lock_is_reclaimed() {
|
||||
let managers = (0..3).map(|_| Arc::new(GlobalLockManager::new())).collect::<Vec<_>>();
|
||||
let node_clients = managers
|
||||
.iter()
|
||||
.map(|m| Arc::new(LocalClient::with_manager(m.clone())))
|
||||
.collect::<Vec<_>>();
|
||||
let resource = create_test_object_key("bucket", "object-crashed-owner");
|
||||
|
||||
// Simulate a dead coordinator: plant short-ttl orphan entries on each node backend,
|
||||
// then never refresh them.
|
||||
for c in &node_clients {
|
||||
let orphan = LockRequest::new(resource.clone(), LockType::Exclusive, "dead-owner").with_ttl(Duration::from_millis(60));
|
||||
let resp = c.acquire_lock(&orphan).await.expect("orphan acquire");
|
||||
assert!(resp.success, "orphan entry should be planted on each node");
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(120)).await; // ttl elapses
|
||||
|
||||
let clients = node_clients
|
||||
.iter()
|
||||
.map(|c| c.clone() as Arc<dyn LockClient>)
|
||||
.collect::<Vec<_>>();
|
||||
let lock = NamespaceLock::with_clients("crashed-owner-recovery".to_string(), clients);
|
||||
|
||||
let req_new = LockRequest::new(resource.clone(), LockType::Exclusive, "owner-live")
|
||||
.with_acquire_timeout(Duration::from_millis(300))
|
||||
.with_ttl(Duration::from_millis(150));
|
||||
let recovered = lock
|
||||
.acquire_guard(&req_new)
|
||||
.await
|
||||
.expect("acquire should not error")
|
||||
.expect("stale orphan entries must be reclaimed; no permanent deadlock (#698)");
|
||||
drop(recovered);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user