seaweed-volume: keep EC shards when the .dat beside them is not their encode source

Mirrors the Go volume server: validate_ec_volume keeps shards that are
exactly the full encode of the source size their .vif records when the
.dat beside them has a different size, and leaves that .dat unloaded and
untouched. Without a recorded size, or with shards that are not that
encode's full size, the existing interrupted-encode reclaim applies.
This commit is contained in:
Chris Lu committed 2026-12-05 23:45:47 -08:00
1 parent faa7be7790
commit ce338a724d
1 file changed
+219 -4
+219 -4
View File
@@ -506,13 +506,12 @@ impl DiskLocation {
// On-disk .dat: an empty <= superblock one is a stub; a transient stat
// error keeps the shards rather than deleting.
let mut dat_size: i64 = -1;
let mut expected_shard_size: Option<i64> = None;
let dat_exists = match fs::metadata(&dat_path) {
Ok(meta) if meta.len() > SUPER_BLOCK_SIZE as u64 => {
expected_shard_size = Some(calculate_expected_shard_size(
meta.len() as i64,
data_shards,
));
dat_size = meta.len() as i64;
expected_shard_size = Some(calculate_expected_shard_size(dat_size, data_shards));
true
}
Ok(_) => false,
@@ -569,6 +568,25 @@ impl DiskLocation {
if shard_count == 0 {
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.
let recorded =
ec_recorded_dat_file_size(&self.directory, &self.idx_directory, collection, vid);
if recorded > 0
&& dat_size != recorded
&& actual_shard_size == Some(calculate_expected_shard_size(recorded, data_shards))
{
warn!(
volume_id = vid.0,
dat_size,
recorded,
".dat size differs from the encode source size the .vif records; keeping the EC shards and leaving the .dat in place untouched"
);
return true;
}
if let (Some(actual), Some(expected)) = (actual_shard_size, expected_shard_size)
&& actual < expected
{
@@ -1519,6 +1537,32 @@ fn ec_data_shards_from_vif(
DATA_SHARDS_COUNT
}
/// The encode-source `.dat` size recorded in the volume's `.vif` in the data
/// dir or the idx dir, or 0 when none records it. Mirrors Go's
/// ecRecordedDatFileSize.
fn ec_recorded_dat_file_size(
directory: &str,
idx_directory: &str,
collection: &str,
vid: VolumeId,
) -> i64 {
for dir in [directory, idx_directory] {
let vif = format!("{}.vif", volume_file_name(dir, collection, vid));
if let Some(size) = fs::read_to_string(&vif)
.ok()
.and_then(|s| serde_json::from_str::<VifVolumeInfo>(&s).ok())
.map(|vi| vi.dat_file_size)
&& size > 0
{
return size;
}
if directory == idx_directory {
break;
}
}
0
}
/// Parse a volume filename like "collection_42.dat" or "42.dat" into (collection, VolumeId).
/// Parse a `<collection>_<vid>` or `<vid>` base name into its parts.
/// Mirrors `parseCollectionVolumeId` in
@@ -1878,6 +1922,177 @@ mod tests {
);
}
fn write_ec_vif(base: &str, dat_file_size: i64) {
let vif = VifVolumeInfo {
version: 3,
dat_file_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();
}
fn write_sized(path: &str, size: i64) {
fs::File::create(path)
.unwrap()
.set_len(size as u64)
.unwrap();
}
fn test_location(dir: &str) -> DiskLocation {
DiskLocation::new(
dir,
dir,
10,
DiskType::HardDrive,
MinFreeSpace::Percent(1.0),
Vec::new(),
)
.unwrap()
}
// A .dat larger than the source the .vif records sits beside a full-size
// shard of that encode: it is not the shards' source, so the shards stay,
// the .dat is not loaded, and nothing on disk is touched.
#[test]
fn test_load_existing_foreign_dat_beside_ec_shards_keeps_both() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path().to_str().unwrap();
let vid = VolumeId(74);
{
let mut loc = test_location(dir);
loc.create_volume(vid, NeedleMapKind::InMemory, &VolumeSpec::default())
.unwrap();
loc.close();
}
let base = volume_file_name(dir, "", vid);
let ds = crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT;
let source_size = 30 * 1024 * 1024i64;
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);
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.
let foreign_size = source_size + 10 * 1024 * 1024;
assert!(calculate_expected_shard_size(foreign_size, ds) > shard_size);
fs::OpenOptions::new()
.write(true)
.open(format!("{}.dat", base))
.unwrap()
.set_len(foreign_size as u64)
.unwrap();
let exts = [".dat", ".idx", ".vif", ".ecx", ".ecj", ".ec09"];
let sizes: Vec<u64> = exts
.iter()
.map(|e| fs::metadata(format!("{}{}", base, e)).unwrap().len())
.collect();
let vif_before = fs::read(format!("{}.vif", base)).unwrap();
let mut loc = test_location(dir);
assert!(
loc.validate_ec_volume("", vid),
"shards whose .vif names a different source size than the .dat must be kept",
);
loc.load_existing_volumes(NeedleMapKind::InMemory).unwrap();
assert!(
loc.find_volume(vid).is_none(),
"a .dat that is not the encode source was loaded beside its EC shards",
);
for (e, size) in exts.iter().zip(sizes) {
let meta = fs::metadata(format!("{}{}", base, e))
.unwrap_or_else(|err| panic!("{} is gone: {}", e, err));
assert_eq!(meta.len(), size, "{} size changed", e);
}
assert_eq!(fs::read(format!("{}.vif", base)).unwrap(), vif_before);
}
// The .dat matches the recorded source size and the shards are short of its
// full encode: an interrupted local encode, still reclaimed.
#[test]
fn test_validate_ec_volume_interrupted_encode_with_recorded_size_reclaims() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path().to_str().unwrap();
let loc = test_location(dir);
let base = volume_file_name(dir, "", VolumeId(75));
let ds = crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT;
let total = crate::storage::erasure_coding::ec_shard::TOTAL_SHARDS_COUNT;
let dat_size = 30 * 1024 * 1024i64;
write_sized(&format!("{}.dat", base), dat_size);
write_ec_vif(&base, dat_size);
let partial = calculate_expected_shard_size(dat_size, ds) / 3;
for i in 0..total {
write_sized(&format!("{}.ec{:02}", base, i), partial);
}
assert!(
!loc.validate_ec_volume("", VolumeId(75)),
"partial shards beside the recorded source .dat should be reclaimable",
);
}
// The .vif is written after the shards, so an interrupted re-encode of a
// volume that was once EC-decoded still has the old encode's .vif. Shards
// that are not that encode's full size are not described by it: reclaim.
#[test]
fn test_validate_ec_volume_interrupted_re_encode_with_stale_vif_reclaims() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path().to_str().unwrap();
let loc = test_location(dir);
let base = volume_file_name(dir, "", VolumeId(76));
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(&format!("{}.dat", base), dat_size);
write_ec_vif(&base, old_source_size);
let partial = calculate_expected_shard_size(dat_size, ds) / 2;
assert_ne!(partial, calculate_expected_shard_size(old_source_size, ds));
for i in 0..total {
write_sized(&format!("{}.ec{:02}", base, i), partial);
}
assert!(
!loc.validate_ec_volume("", VolumeId(76)),
"partial shards of an interrupted re-encode must not be kept for a stale .vif",
);
}
// A .vif without a recorded source size keeps the old size check against
// the .dat, in both directions.
#[test]
fn test_validate_ec_volume_no_recorded_size_keeps_old_rule() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path().to_str().unwrap();
let loc = test_location(dir);
let base = volume_file_name(dir, "", VolumeId(77));
let ds = crate::storage::erasure_coding::ec_shard::DATA_SHARDS_COUNT;
write_ec_vif(&base, 0);
write_sized(
&format!("{}.ec00", base),
calculate_expected_shard_size(30 * 1024 * 1024, ds),
);
write_sized(&format!("{}.dat", base), 40 * 1024 * 1024);
assert!(
!loc.validate_ec_volume("", VolumeId(77)),
"without a recorded size, shards smaller than the .dat's full encode should be reclaimable",
);
write_sized(&format!("{}.dat", base), 30 * 1024 * 1024);
assert!(
loc.validate_ec_volume("", VolumeId(77)),
"without a recorded size, shards matching the .dat's full encode must be kept",
);
}
// The per-disk generation reader behind the fenced EC teardown: a present .vif
// yields its generation (or 0 with no EC config), a missing .vif is unreadable
// (preserved, fail-safe), and the idx dir is a fallback for the split-disk layout.