diff --git a/seaweed-volume/src/server/store_ec.rs b/seaweed-volume/src/server/store_ec.rs index 1a60c1d642..f8691c3b67 100644 --- a/seaweed-volume/src/server/store_ec.rs +++ b/seaweed-volume/src/server/store_ec.rs @@ -158,7 +158,7 @@ pub async fn read_ec_shard_needle_or_miss( match cached_lookup_ec_shard_locations(state, vid).await { Ok(fresh) => { // A complete reply merges into the cache; an incomplete one - // (< data_shards) is left unwritten — keep the prior cache. + // (< data_shards) only fills shards the cache lacks. match write_back_shard_locations( state, vid, @@ -167,8 +167,6 @@ pub async fn read_ec_shard_needle_or_miss( snapshot.encode_ts_ns, ) { Some(merged) => shard_locations = merged, - // An incomplete reply leaves the cache unwritten and its refresh - // time unadvanced, so the mark this refresh consumed goes back. None => mark_shard_locations_stale(state, vid), } } @@ -714,8 +712,10 @@ pub async fn scrub_ec_volume_distributed( ) { match cached_lookup_ec_shard_locations(state, vid).await { Ok(fresh) => { + let partial = fresh.len() < data_shards; if write_back_shard_locations(state, vid, fresh, data_shards, expected_encode_ts_ns) .is_none() + || partial { mark_shard_locations_stale(state, vid); return ( @@ -1262,16 +1262,15 @@ fn shard_locations_from_reply( } /// Merge a fresh `LookupEcVolume` reply into the per-EcVolume shard-location -/// cache. Returns the merged map on success, or `None` when the reply is -/// incomplete (and was therefore NOT written) or the volume is gone. +/// cache. Returns the resulting map, or `None` when the volume is gone. /// /// Completeness guard (mirrors Go's `cachedLookupEcShardLocations`): a reply /// carrying fewer than `data_shards` shard locations is a master-mid-recovery -/// partial, not ground truth — Go aborts the lookup with a retryable error and -/// leaves the cache + refresh time untouched. Returning `None` without writing -/// does the same, so a previously-complete cache is never clobbered with a -/// partial map. On a complete reply the write is a per-shard MERGE (see -/// `merge_shard_locations`), not a full replace. +/// partial, not ground truth. It only fills shards the cache does not know, and +/// leaves the refresh time unadvanced with the map marked stale, so a +/// previously-complete cache is never clobbered with a partial map. On a +/// complete reply the write is a per-shard MERGE (see `merge_shard_locations`), +/// not a full replace. fn write_back_shard_locations( state: &Arc, vid: VolumeId, @@ -1279,11 +1278,11 @@ fn write_back_shard_locations( data_shards: usize, expected_encode_ts_ns: i64, ) -> Option>> { - if locations.len() < data_shards { - return None; - } let store = state.store.read().unwrap(); let ecv = find_ec_volume_for_scrub(&store, vid, expected_encode_ts_ns)?; + if locations.len() < data_shards { + return Some(ecv.fill_shard_location_gaps(locations)); + } Some(ecv.merge_shard_locations(locations)) } diff --git a/seaweed-volume/src/storage/erasure_coding/ec_volume.rs b/seaweed-volume/src/storage/erasure_coding/ec_volume.rs index ee30d49860..4388a5a3cf 100644 --- a/seaweed-volume/src/storage/erasure_coding/ec_volume.rs +++ b/seaweed-volume/src/storage/erasure_coding/ec_volume.rs @@ -1745,6 +1745,25 @@ impl EcVolume { cache.locations.clone() } + /// Take a partial reply's holders for the shards the cache does not know, + /// leaving every known shard and the refresh time alone. A partial is not + /// the volume's map, but a cold cache can still read those shards directly; + /// the map stays marked so the next read looks it up again. + pub(crate) fn fill_shard_location_gaps( + &self, + locations: HashMap>, + ) -> HashMap> { + let mut cache = self.shard_locations.write().unwrap(); + for (shard_id, addrs) in locations { + let known = cache.locations.entry(shard_id).or_default(); + if known.is_empty() { + *known = addrs; + } + } + cache.stale = true; + cache.locations.clone() + } + /// The cached map and the time it was learned, read in one section. Both /// halves describe the same refresh, which is what the freshness heuristic /// assumes when it ages the map against the timestamp. @@ -5450,6 +5469,34 @@ mod tests { ); } + #[test] + fn fill_shard_location_gaps_keeps_known_shards_and_refresh_time() { + let tmp = TempDir::new().unwrap(); + let vol = mount_bare_ec_volume(tmp.path().to_str().unwrap()); + + let filled = vol.fill_shard_location_gaps(HashMap::from([(0, vec!["a:1".to_string()])])); + assert_eq!(filled.get(&0), Some(&vec!["a:1".to_string()])); + assert!( + vol.shard_locations_snapshot().1.is_none(), + "a partial reply is not a refresh" + ); + + vol.merge_shard_locations(HashMap::from([(1, vec!["b:2".to_string()])])); + let stamped = vol.shard_locations_snapshot().1; + vol.claim_shard_locations_refresh(|_| true); + let filled = vol.fill_shard_location_gaps(HashMap::from([ + (1, vec!["c:3".to_string()]), + (2, vec!["d:4".to_string()]), + ])); + assert_eq!(filled.get(&1), Some(&vec!["b:2".to_string()])); + assert_eq!(filled.get(&2), Some(&vec!["d:4".to_string()])); + assert_eq!(vol.shard_locations_snapshot().1, stamped); + assert!( + vol.claim_shard_locations_refresh(|stale| stale), + "the partial leaves the map marked for the next lookup" + ); + } + /// The stale mark is consumed by the refresh that acts on it, exactly once: /// otherwise every later read keeps re-looking-up a map no one has disproved /// since. A claim that declines the refresh must leave the mark standing.