mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
seaweed-volume: require the .ecx to fit the recorded source before trusting it
Mirrors the Go volume server: validate_ec_volume trusts the source size the .vif records only when no .ecx beside the shards indexes a needle past it, so partial shards of a re-encode interrupted under an earlier encode's .vif are reclaimed even when their length matches that earlier encode. find_dat_file_size_with_dirs now shares the .ecx extent scan.
This commit is contained in:
1 parent
9efd3951fe
commit
54efc0bf7e
2 files changed
+124
-22
No files matched your search
@@ -574,14 +574,18 @@ impl DiskLocation {
|
||||
return false;
|
||||
}
|
||||
// Shards that are exactly the full encode of the source size the .vif
|
||||
// records were made from that source. A .dat of any other size is not
|
||||
// it (e.g. a replica that kept taking writes), so its size says nothing
|
||||
// about these shards: keep them, and leave the .dat unloaded so it
|
||||
// cannot collide with the EC volume.
|
||||
// records, under an .ecx indexing nothing past that size, were made
|
||||
// from that source. A .dat of any other size is not it (e.g. a replica
|
||||
// that kept taking writes), so its size says nothing about these
|
||||
// shards: keep them, and leave the .dat unloaded so it cannot collide
|
||||
// with the EC volume. The .ecx check catches a re-encode interrupted
|
||||
// under an earlier encode's .vif: it writes the .ecx of the grown .dat
|
||||
// before the shards.
|
||||
let recorded = encode_info.as_ref().map_or(0, |vi| vi.dat_file_size);
|
||||
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)
|
||||
{
|
||||
warn!(
|
||||
volume_id = vid.0,
|
||||
@@ -605,6 +609,39 @@ 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.
|
||||
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]);
|
||||
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;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Remove all EC-related files for a volume.
|
||||
/// `pub(crate)` so the Store-level cross-disk passes in
|
||||
/// `store_ec_reconcile.rs` can call it to scrub partial EC artefacts
|
||||
@@ -1925,6 +1962,29 @@ mod tests {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// An .ecx with one live needle ending at `end` in a current-version .dat.
|
||||
fn write_ecx_ending_at(base: &str, end: i64) {
|
||||
let size = Size(1000);
|
||||
let offset =
|
||||
end - crate::storage::needle::needle::get_actual_size(size, Version::current());
|
||||
let mut entry = [0u8; NEEDLE_MAP_ENTRY_SIZE];
|
||||
idx_entry_to_bytes(
|
||||
&mut entry,
|
||||
NeedleId(1),
|
||||
Offset::from_actual_offset(offset),
|
||||
size,
|
||||
);
|
||||
fs::write(format!("{}.ecx", base), entry).unwrap();
|
||||
}
|
||||
|
||||
// A .dat of `size` whose superblock names the current needle version.
|
||||
fn write_sized_dat(path: &str, size: i64) {
|
||||
use std::io::Write;
|
||||
let mut f = fs::File::create(path).unwrap();
|
||||
f.write_all(&[Version::current().0]).unwrap();
|
||||
f.set_len(size as u64).unwrap();
|
||||
}
|
||||
|
||||
fn test_location(dir: &str) -> DiskLocation {
|
||||
DiskLocation::new(
|
||||
dir,
|
||||
@@ -1957,7 +2017,8 @@ mod tests {
|
||||
write_ec_vif(&base, source_size);
|
||||
let shard_size = calculate_expected_shard_size(source_size, ds);
|
||||
write_sized(&format!("{}.ec09", base), shard_size);
|
||||
write_sized(&format!("{}.ecx", base), 20);
|
||||
// The .ecx indexes a needle ending exactly at the source size.
|
||||
write_ecx_ending_at(&base, source_size);
|
||||
write_sized(&format!("{}.ecj", base), 0);
|
||||
// Grown past the source by later writes: its full encode would be
|
||||
// larger than the shard, which used to read as an interrupted encode.
|
||||
@@ -2088,6 +2149,45 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// A re-encode of a grown .dat interrupted under an earlier encode's .vif,
|
||||
// with every partial shard exactly as long as that earlier encode's full
|
||||
// shards. The re-encode wrote the .ecx of the grown .dat first, so it
|
||||
// indexes needles past the recorded size: the shards are not that
|
||||
// encode's, and the complete .dat is reclaimed.
|
||||
#[test]
|
||||
fn test_validate_ec_volume_interrupted_re_encode_matching_stale_length_reclaims() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let dir = tmp.path().to_str().unwrap();
|
||||
let loc = test_location(dir);
|
||||
let vid = VolumeId(79);
|
||||
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);
|
||||
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 stale .vif was trusted for partial shards under an .ecx of the grown .dat",
|
||||
);
|
||||
|
||||
// The same shards under an .ecx that stays within the recorded size
|
||||
// are that encode's: keep them.
|
||||
write_ecx_ending_at(&base, old_source_size);
|
||||
assert!(
|
||||
loc.validate_ec_volume("", vid),
|
||||
"shards whose .ecx and length match the recorded source must be kept",
|
||||
);
|
||||
}
|
||||
|
||||
// A .vif without a recorded source size keeps the old size check against
|
||||
// the .dat, in both directions.
|
||||
#[test]
|
||||
|
||||
@@ -48,28 +48,30 @@ pub fn find_dat_file_size_with_dirs(
|
||||
ec00.read_exact(&mut sb_buf)?;
|
||||
let version = Version(sb_buf[0]);
|
||||
|
||||
// Start with at least the superblock size
|
||||
let mut dat_size: i64 = SUPER_BLOCK_SIZE as i64;
|
||||
// At least the superblock size
|
||||
let extent = ecx_needle_extent(&format!("{}.ecx", ecx_base), version, deleted)?;
|
||||
Ok(extent.max(SUPER_BLOCK_SIZE as i64))
|
||||
}
|
||||
|
||||
// Scan .ecx entries
|
||||
let ecx_path = format!("{}.ecx", ecx_base);
|
||||
let ecx_data = std::fs::read(&ecx_path)?;
|
||||
let entry_count = ecx_data.len() / NEEDLE_MAP_ENTRY_SIZE;
|
||||
|
||||
for i in 0..entry_count {
|
||||
let start = i * NEEDLE_MAP_ENTRY_SIZE;
|
||||
let (key, offset, size) =
|
||||
idx_entry_from_bytes(&ecx_data[start..start + NEEDLE_MAP_ENTRY_SIZE]);
|
||||
/// Where the furthest live needle indexed by the `.ecx` at `ecx_path` ends in
|
||||
/// the source `.dat`, or 0 when it indexes none. Needles in `deleted` count as
|
||||
/// deleted. Mirrors Go's EcxNeedleExtent.
|
||||
pub fn ecx_needle_extent(
|
||||
ecx_path: &str,
|
||||
version: Version,
|
||||
deleted: &HashSet<NeedleId>,
|
||||
) -> io::Result<i64> {
|
||||
let ecx_data = std::fs::read(ecx_path)?;
|
||||
let (entries, _) = ecx_data.as_chunks::<NEEDLE_MAP_ENTRY_SIZE>();
|
||||
let mut extent = 0i64;
|
||||
for entry in entries {
|
||||
let (key, offset, size) = idx_entry_from_bytes(entry);
|
||||
if size.is_deleted() || deleted.contains(&key) {
|
||||
continue;
|
||||
}
|
||||
let entry_stop = offset.to_actual_offset() + get_actual_size(size, version);
|
||||
if entry_stop > dat_size {
|
||||
dat_size = entry_stop;
|
||||
}
|
||||
extent = extent.max(offset.to_actual_offset() + get_actual_size(size, version));
|
||||
}
|
||||
|
||||
Ok(dat_size)
|
||||
Ok(extent)
|
||||
}
|
||||
|
||||
/// Whether the `.ecx` in `ecx_dir` indexes a needle deleted neither there nor
|
||||
|
||||
Reference in new issue
Block a user