From 1e0244dfc8f649f905ecbd8606a82c6ad9eaeb74 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 22 Nov 2026 10:51:29 -0800 Subject: [PATCH] seaweed-volume: drop a shard the master places nowhere from the location cache Leaving shards with no location out of the reply let a complete reply keep a former holder's cached address for a shard the master now places nowhere. An EC delete tried that address first: an offline peer failed the delete without the fallback to other shard holders, and a peer that had unmounted the volume acknowledged it without writing a tombstone. The reply now keeps every slot and only located shards count toward its completeness. A complete reply drops a shard it places nowhere, as Go's delete-then-append does, and a partial reply's empty slots add nothing. Co-Authored-By: Claude Opus 5.5 --- seaweed-volume/src/server/store_ec.rs | 37 ++++++++----------- .../src/storage/erasure_coding/ec_volume.rs | 25 ++++++++++--- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/seaweed-volume/src/server/store_ec.rs b/seaweed-volume/src/server/store_ec.rs index f8691c3b67..4dfb015691 100644 --- a/seaweed-volume/src/server/store_ec.rs +++ b/seaweed-volume/src/server/store_ec.rs @@ -712,7 +712,7 @@ pub async fn scrub_ec_volume_distributed( ) { match cached_lookup_ec_shard_locations(state, vid).await { Ok(fresh) => { - let partial = fresh.len() < data_shards; + let partial = located_shard_count(&fresh) < data_shards; if write_back_shard_locations(state, vid, fresh, data_shards, expected_encode_ts_ns) .is_none() || partial @@ -1235,19 +1235,15 @@ async fn cached_lookup_ec_shard_locations( Ok(shard_locations_from_reply(resp.into_inner())) } -/// The shards a `LookupEcVolume` reply places on at least one server. The -/// master answers every shard slot it tracks, so a shard whose holders are all -/// down comes back with no locations: kept, it would count toward the reply's -/// completeness and overwrite the cached address of a holder that is about to -/// return. Go's `cachedLookupEcShardLocations` drops such a shard the same way. +/// The shard slots of a `LookupEcVolume` reply. The master answers every slot +/// it tracks, so a shard whose holders are all down comes back with no +/// addresses: it does not count toward the reply's completeness, and a complete +/// reply drops it from the cache, as Go's `cachedLookupEcShardLocations` does. fn shard_locations_from_reply( resp: master_pb::LookupEcVolumeResponse, ) -> HashMap> { let mut out = HashMap::new(); for entry in resp.shard_id_locations { - if entry.locations.is_empty() { - continue; - } let Ok(sid) = shard_id_try_from(entry.shard_id) else { continue; }; @@ -1261,6 +1257,10 @@ fn shard_locations_from_reply( out } +fn located_shard_count(locations: &HashMap>) -> usize { + locations.values().filter(|addrs| !addrs.is_empty()).count() +} + /// Merge a fresh `LookupEcVolume` reply into the per-EcVolume shard-location /// cache. Returns the resulting map, or `None` when the volume is gone. /// @@ -1280,7 +1280,7 @@ fn write_back_shard_locations( ) -> Option>> { let store = state.store.read().unwrap(); let ecv = find_ec_volume_for_scrub(&store, vid, expected_encode_ts_ns)?; - if locations.len() < data_shards { + if located_shard_count(&locations) < data_shards { return Some(ecv.fill_shard_location_gaps(locations)); } Some(ecv.merge_shard_locations(locations)) @@ -2792,11 +2792,10 @@ mod tests { } // The master answers all 32 shard slots, located or not. While 7 of a 10+4 - // volume's shards have a holder up, the reply must read as 7 shards, short of - // the data shards, and must not carry empty entries that would replace a - // returning holder's cached address. + // volume's shards have a holder up, the reply must count as 7 shards, short + // of the data shards. #[test] - fn a_lookup_reply_drops_shards_with_no_location() { + fn a_lookup_reply_counts_only_located_shards() { use master_pb::lookup_ec_volume_response::EcShardIdLocation; let located = [0u32, 3, 4, 6, 7, 9, 12]; let reply = master_pb::LookupEcVolumeResponse { @@ -2818,13 +2817,9 @@ mod tests { .collect(), }; let got = shard_locations_from_reply(reply); - let mut ids: Vec = got.keys().copied().collect(); - ids.sort(); - assert_eq!(ids, vec![0, 3, 4, 6, 7, 9, 12]); - assert!( - got.len() < 10, - "a 7-shard reply must fail the data-shard completeness guard" - ); + assert_eq!(got.len(), 32); + assert_eq!(located_shard_count(&got), 7); + assert!(got[&1].is_empty()); assert_eq!(got[&3], vec!["10.0.0.5:8180.18180".to_string()]); } diff --git a/seaweed-volume/src/storage/erasure_coding/ec_volume.rs b/seaweed-volume/src/storage/erasure_coding/ec_volume.rs index 4388a5a3cf..9721fadef6 100644 --- a/seaweed-volume/src/storage/erasure_coding/ec_volume.rs +++ b/seaweed-volume/src/storage/erasure_coding/ec_volume.rs @@ -1726,10 +1726,11 @@ impl EcVolume { /// stamp the refresh time, returning a clone of the resulting map. /// /// Per-shard upsert (mirrors Go's `cachedLookupEcShardLocations`): each shard - /// id present in the reply overwrites its cached entry, while shard ids absent - /// from the reply keep their previously-cached locations — so a reply that - /// passes the data-shard completeness guard but omits a shard already in cache - /// does not drop that shard's known location (unlike a full replace). + /// id present in the reply overwrites its cached entry, and one the reply + /// places nowhere is dropped, while shard ids absent from the reply keep + /// their previously-cached locations — so a reply that passes the data-shard + /// completeness guard but omits a shard already in cache does not drop that + /// shard's known location (unlike a full replace). /// /// Map and refresh time advance in one write section, so no reader can pair /// the merged map with the previous lookup's timestamp. @@ -1739,7 +1740,11 @@ impl EcVolume { ) -> HashMap> { let mut cache = self.shard_locations.write().unwrap(); for (shard_id, addrs) in locations { - cache.locations.insert(shard_id, addrs); + if addrs.is_empty() { + cache.locations.remove(&shard_id); + } else { + cache.locations.insert(shard_id, addrs); + } } cache.refreshed_at = Some(Instant::now()); cache.locations.clone() @@ -1755,6 +1760,9 @@ impl EcVolume { ) -> HashMap> { let mut cache = self.shard_locations.write().unwrap(); for (shard_id, addrs) in locations { + if addrs.is_empty() { + continue; + } let known = cache.locations.entry(shard_id).or_default(); if known.is_empty() { *known = addrs; @@ -5467,6 +5475,11 @@ mod tests { later.unwrap() >= before, "the second merge re-stamped the refresh time" ); + + // A shard the master places nowhere is dropped, not kept at an address + // a delete would trust. + let merged = vol.merge_shard_locations(HashMap::from([(0, Vec::new())])); + assert_eq!(merged, HashMap::from([(1, vec!["c:3".to_string()])])); } #[test] @@ -5487,9 +5500,11 @@ mod tests { let filled = vol.fill_shard_location_gaps(HashMap::from([ (1, vec!["c:3".to_string()]), (2, vec!["d:4".to_string()]), + (3, Vec::new()), ])); assert_eq!(filled.get(&1), Some(&vec!["b:2".to_string()])); assert_eq!(filled.get(&2), Some(&vec!["d:4".to_string()])); + assert!(!filled.contains_key(&3), "a partial reply's empty slot adds nothing"); assert_eq!(vol.shard_locations_snapshot().1, stamped); assert!( vol.claim_shard_locations_refresh(|stale| stale),