diff --git a/seaweed-volume/src/storage/disk_location.rs b/seaweed-volume/src/storage/disk_location.rs index 10a9cd5f40..6143c65af9 100644 --- a/seaweed-volume/src/storage/disk_location.rs +++ b/seaweed-volume/src/storage/disk_location.rs @@ -585,7 +585,12 @@ impl DiskLocation { if recorded > 0 && dat_size != recorded && actual_shard_size == Some(calculate_expected_shard_size(recorded, data_shards)) - && !self.ecx_indexes_past(collection, vid, &dat_path, recorded) + && !self.ecx_indexes_past( + collection, + vid, + encode_needle_version(&encode_info), + recorded, + ) { warn!( volume_id = vid.0, @@ -611,19 +616,17 @@ impl DiskLocation { /// 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]; - if fs::File::open(dat_path) - .and_then(|mut f| f.read_exact(&mut version_byte)) - .is_err() - { - return false; - } - let version = Version(version_byte[0]); + /// ending past `size`. A missing or unreadable file reports false: a + /// doubt keeps the shards. A torn tail is what an interrupted write + /// leaves, so the whole entries before it still count. Mirrors Go's + /// ecxIndexesPast. + fn ecx_indexes_past( + &self, + collection: &str, + vid: VolumeId, + version: Version, + size: i64, + ) -> bool { 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) @@ -633,7 +636,6 @@ impl DiskLocation { volume_file_name(&self.idx_directory, collection, vid) ); } - // A torn tail still reports the whole entries before it. crate::storage::erasure_coding::ec_decoder::ecx_needle_extent( &ecx, version, @@ -1533,6 +1535,16 @@ fn rm_if_present(path: String) -> io::Result<()> { } } +/// The needle version the encode's `.vif` records, as the mount reads it, +/// defaulting to the current version when unset. Mirrors Go's +/// encodeNeedleVersion. +fn encode_needle_version(info: &Option) -> Version { + match info.as_ref().map_or(0, |vi| vi.version) { + 0 => Version::current(), + v => Version(v as u8), + } +} + /// The `.vif` describing the encode of an EC volume: the first with an EC /// shard config in the data dir, the idx dir, then the sibling data dir the /// mount takes it from under a shared idx dir. Without one it falls back to @@ -1964,9 +1976,12 @@ mod tests { // An .ecx with one live needle ending at `end` in a current-version .dat. fn write_ecx_ending_at(base: &str, end: i64) { + write_ecx_ending_at_version(base, end, Version::current()); + } + + fn write_ecx_ending_at_version(base: &str, end: i64, version: Version) { let size = Size(1000); - let offset = - end - crate::storage::needle::needle::get_actual_size(size, Version::current()); + let offset = end - crate::storage::needle::needle::get_actual_size(size, version); let mut entry = [0u8; NEEDLE_MAP_ENTRY_SIZE]; idx_entry_to_bytes( &mut entry, @@ -2237,6 +2252,82 @@ mod tests { ); } + // The .ecx extent is sized with the needle version the encode's .vif + // records, as the mount reads it, not the version of the foreign .dat. + #[test] + fn test_validate_ec_volume_sizes_the_ecx_with_the_encode_version() { + let tmp = TempDir::new().unwrap(); + let dir = tmp.path().to_str().unwrap(); + let loc = test_location(dir); + let vid = VolumeId(81); + let base = volume_file_name(dir, "", vid); + let ds = crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT; + let source_size = 30 * 1024 * 1024i64; + let vif = VifVolumeInfo { + version: 2, + dat_file_size: source_size, + ec_shard_config: Some(crate::storage::volume::VifEcShardConfig { + data_shards: 10, + parity_shards: 4, + ..Default::default() + }), + ..Default::default() + }; + fs::write( + format!("{}.vif", base), + serde_json::to_string(&vif).unwrap(), + ) + .unwrap(); + // Ends exactly at the source under version 2; version 3 would put it + // 8 bytes past. + write_ecx_ending_at_version(&base, source_size, Version(2)); + 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), + "the .ecx was sized with the foreign .dat's needle version, not the encode's", + ); + } + + // An interrupted write leaves a torn .ecx tail; the whole entries before + // it still show the re-encode's .ecx indexing past the stale recorded size. + #[test] + fn test_validate_ec_volume_torn_ecx_tail_still_reclaims_re_encode() { + use std::io::Write; + let tmp = TempDir::new().unwrap(); + let dir = tmp.path().to_str().unwrap(); + let loc = test_location(dir); + let vid = VolumeId(82); + let base = volume_file_name(dir, "", vid); + let ds = crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT; + let total = crate::storage::erasure_coding::ec_shard::TOTAL_SHARDS_COUNT; + let old_source_size = 20 * 1024 * 1024i64; + let dat_size = 60 * 1024 * 1024i64; + write_sized_dat(&format!("{}.dat", base), dat_size); + write_ec_vif(&base, old_source_size); + write_ecx_ending_at(&base, dat_size); + fs::OpenOptions::new() + .append(true) + .open(format!("{}.ecx", base)) + .unwrap() + .write_all(&[1, 2, 3]) + .unwrap(); + for i in 0..total { + write_sized( + &format!("{}.ec{:02}", base, i), + calculate_expected_shard_size(old_source_size, ds), + ); + } + assert!( + !loc.validate_ec_volume("", vid), + "a torn .ecx tail hid the entries past the stale recorded size", + ); + } + // A .vif without a recorded source size keeps the old size check against // the .dat, in both directions. #[test]