From 72b95e149e1735b50f6987f0a48573545f02d3ea Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 3 Dec 2026 14:37:40 -0800 Subject: [PATCH] volume(rs): recheck expiry from the commit-start status The heartbeat's expiry recheck runs under the store write lock and read the .dat size through the io lock, so a TTL volume that came due while a compaction commit reloaded it held the heartbeat until the reload ended. It now reads the size from collect_status, and delete_volume keeps refusing the committing volume until a later pulse. Co-Authored-By: Claude Opus 5.5 --- seaweed-volume/src/server/heartbeat.rs | 94 +++++++++++++++++++++++++- seaweed-volume/src/storage/volume.rs | 6 ++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/seaweed-volume/src/server/heartbeat.rs b/seaweed-volume/src/server/heartbeat.rs index ab82ed209c..fdbc229b71 100644 --- a/seaweed-volume/src/server/heartbeat.rs +++ b/seaweed-volume/src/server/heartbeat.rs @@ -1170,7 +1170,7 @@ fn apply_volume_actions_to(store: &mut Store, actions: VolumeActions) { }; let still_expired = loc.find_volume(vid).is_some_and(|vol| { !vol.should_quarantine() - && vol.is_expired(vol.dat_file_size().unwrap_or(0), volume_size_limit) + && vol.is_expired(vol.collect_status().dat_file_size, volume_size_limit) && vol.is_expired_long_enough(MAX_TTL_VOLUME_REMOVAL_DELAY) }); if still_expired { @@ -3087,6 +3087,98 @@ mod tests { ); } + // The recheck runs under the store write lock inside the heartbeat, so a + // TTL volume that comes due mid-commit must not wait out the reload. + #[test] + fn test_expiry_recheck_does_not_wait_for_a_commit() { + use crate::storage::needle::needle::Needle; + use crate::storage::types::{Cookie, NeedleId}; + use std::sync::Mutex; + use std::sync::mpsc::channel; + use std::time::Duration; + + let temp_dir = tempfile::tempdir().unwrap(); + let dir = temp_dir.path().to_str().unwrap(); + let vid = VolumeId(1366); + let mut store = Store::new(NeedleMapKind::InMemory); + store + .add_location( + dir, + dir, + 8, + DiskType::HardDrive, + MinFreeSpace::Percent(1.0), + Vec::new(), + ) + .unwrap(); + store.volume_size_limit.store(1, Ordering::Relaxed); + store + .add_volume( + vid, + DiskType::HardDrive, + &VolumeSpec { + ttl: Some(crate::storage::needle::ttl::TTL::read("20m").unwrap()), + ..Default::default() + }, + ) + .unwrap(); + let mut n = Needle { + id: NeedleId(1), + cookie: Cookie(1), + data_size: 4, + data: b"live".to_vec(), + ..Needle::default() + }; + store.write_volume_needle(vid, &mut n, true).unwrap(); + store.compact_volume(vid, 0, 0, |_| true).unwrap(); + let (_, vol) = store.find_volume(vid).unwrap(); + vol.set_last_modified_ts_for_test( + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap_or_default() + .as_secs() + .saturating_sub(30 * 60), + ); + + let (entered, in_window) = channel(); + let (release, released) = channel::<()>(); + let (entered, released) = (Mutex::new(entered), Mutex::new(released)); + let _hook = crate::storage::volume::commit_swap_hook::register(vid, move || { + entered.lock().unwrap().send(()).unwrap(); + released.lock().unwrap().recv().unwrap(); + }); + let committing = vol.clone(); + let commit = std::thread::spawn(move || { + let _sync_guard = committing.data_file_sync_lock().lock().unwrap(); + let _lease = committing.instance().write_lock(); + committing.commit_compact_held() + }); + in_window + .recv_timeout(Duration::from_secs(10)) + .expect("the commit never reached its swap window"); + let reload = vol.io_write_for_test(); + + let (applied, done) = channel(); + let recheck = std::thread::spawn(move || { + apply_volume_actions_to( + &mut store, + VolumeActions { + delete_expired: vec![(0, vid)], + ..Default::default() + }, + ); + let _ = applied.send(()); + store + }); + let waited = done.recv_timeout(Duration::from_secs(5)).is_err(); + drop(reload); + release.send(()).unwrap(); + commit.join().unwrap().unwrap(); + let store = recheck.join().unwrap(); + assert!(!waited, "the expiry recheck waited for the commit"); + assert!(store.has_volume(vid), "the volume was deleted mid-commit"); + } + // A shard mounted after the EC phase is held when the volume list is // taken; reporting "no EC shards" alongside it would clear it on the master. #[tokio::test] diff --git a/seaweed-volume/src/storage/volume.rs b/seaweed-volume/src/storage/volume.rs index 147c1bb0ee..31ed080d4d 100644 --- a/seaweed-volume/src/storage/volume.rs +++ b/seaweed-volume/src/storage/volume.rs @@ -6933,6 +6933,12 @@ impl Volume { self.io.read().unwrap() } + /// Holds `io` the way a compaction commit's reload does. + #[cfg(test)] + pub(crate) fn io_write_for_test(&self) -> std::sync::RwLockWriteGuard<'_, VolumeIo> { + self.io.write().unwrap() + } + /// Holds the lock a write waits on behind another write's fsync. #[cfg(test)] pub(crate) fn sync_lock_for_test(&self) -> std::sync::MutexGuard<'_, ()> {