From 1b4d5b7df2fba58837d5c80fce90cb694ee66d29 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 6 Dec 2026 00:53:20 -0800 Subject: [PATCH] seaweed-volume: check only the .ecx the mount opens against the recorded source Mirrors the Go volume server: ecx_indexes_past reads the .ecx the mount selects (the data dir's when non-empty, else the idx dir's), so a stale shared .ecx the mount never opens cannot disqualify a healthy encode. --- seaweed-volume/src/storage/disk_location.rs | 89 ++++++++++++++++----- 1 file changed, 69 insertions(+), 20 deletions(-) diff --git a/seaweed-volume/src/storage/disk_location.rs b/seaweed-volume/src/storage/disk_location.rs index 9aa166a0e6..10a9cd5f40 100644 --- a/seaweed-volume/src/storage/disk_location.rs +++ b/seaweed-volume/src/storage/disk_location.rs @@ -609,10 +609,11 @@ impl DiskLocation { true } - /// Whether an .ecx of (collection, vid) in the data or idx dir indexes a - /// live needle ending past `size`, sized with the needle version of the - /// .dat beside it. A missing or unreadable file reports false: a doubt - /// keeps the shards. Mirrors Go's ecxIndexesPast. + /// Whether the .ecx the mount would open for (collection, vid) -- the data + /// dir's when non-empty, else the idx dir's -- indexes a live needle + /// ending past `size`, sized with the needle version of the .dat beside + /// it. A missing or unreadable file reports false: a doubt keeps the + /// shards. Mirrors Go's ecxIndexesPast. fn ecx_indexes_past(&self, collection: &str, vid: VolumeId, dat_path: &str, size: i64) -> bool { use std::io::Read; let mut version_byte = [0u8; 1]; @@ -623,23 +624,22 @@ impl DiskLocation { return false; } let version = Version(version_byte[0]); - for dir in [self.directory.as_str(), self.idx_directory.as_str()] { - let ecx = format!("{}.ecx", volume_file_name(dir, collection, vid)); - // A torn tail still reports the whole entries before it. - if crate::storage::erasure_coding::ec_decoder::ecx_needle_extent( - &ecx, - version, - &HashSet::new(), - ) - .is_ok_and(|extent| extent > size) - { - return true; - } - if self.directory == self.idx_directory { - break; - } + let mut ecx = format!("{}.ecx", volume_file_name(&self.directory, collection, vid)); + if self.idx_directory != self.directory + && !crate::storage::erasure_coding::ec_volume::is_usable_ecx_file(&ecx) + { + ecx = format!( + "{}.ecx", + volume_file_name(&self.idx_directory, collection, vid) + ); } - false + // A torn tail still reports the whole entries before it. + crate::storage::erasure_coding::ec_decoder::ecx_needle_extent( + &ecx, + version, + &HashSet::new(), + ) + .is_ok_and(|extent| extent > size) } /// Remove all EC-related files for a volume. @@ -2188,6 +2188,55 @@ mod tests { ); } + // Only the .ecx the mount opens speaks for the shards: a non-empty + // data-dir .ecx within the recorded size wins over an older shared-idx + // .ecx pointing past it, and the shards stay. + #[test] + fn test_validate_ec_volume_checks_the_ecx_the_mount_opens() { + let tmp = TempDir::new().unwrap(); + let root = tmp.path().to_str().unwrap(); + let (data, idx) = (format!("{root}/data"), format!("{root}/idx")); + for d in [&data, &idx] { + fs::create_dir_all(d).unwrap(); + } + let loc = DiskLocation::new( + &data, + &idx, + 10, + DiskType::HardDrive, + MinFreeSpace::Percent(1.0), + Vec::new(), + ) + .unwrap(); + let vid = VolumeId(80); + let base = volume_file_name(&data, "", vid); + let idx_base = volume_file_name(&idx, "", vid); + let ds = crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT; + + let source_size = 30 * 1024 * 1024i64; + write_ec_vif(&base, source_size); + write_ecx_ending_at(&base, source_size); + write_ecx_ending_at(&idx_base, source_size + 20 * 1024 * 1024); + write_sized( + &format!("{}.ec09", base), + calculate_expected_shard_size(source_size, ds), + ); + write_sized_dat(&format!("{}.dat", base), source_size + 10 * 1024 * 1024); + + assert!( + loc.validate_ec_volume("", vid), + "an .ecx the mount ignores decided against the shards", + ); + + // With no data-dir .ecx the mount opens the shared one, which indexes + // past the recorded size: the old rule applies and reclaims. + fs::remove_file(format!("{}.ecx", base)).unwrap(); + assert!( + !loc.validate_ec_volume("", vid), + "the shared .ecx the mount opens was not checked", + ); + } + // A .vif without a recorded source size keeps the old size check against // the .dat, in both directions. #[test]