From 1ec6ca0a3863ae8fb3f0cab84451e73adb3b452f Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 04:50:49 +0000 Subject: remake: holding a store, a node rejoins a lower epoch a peer serves Of two epochs decided for one, a node holding a store follows the lower ID, and a node on the other rejoins it, however many peers are on each (`docs/store.md`, Remakes): one peer on it is enough, so that two sides of unequal size come together. A made-up epoch keeps a node apart at worst, and makes it take nothing it wouldn't (`Holding::follows`). --- src/remake/catchup.rs | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) (limited to 'src/remake') diff --git a/src/remake/catchup.rs b/src/remake/catchup.rs index bb8b03b..b8dc1b9 100644 --- a/src/remake/catchup.rs +++ b/src/remake/catchup.rs @@ -175,7 +175,7 @@ pub(super) async fn check(host: &H) { let sideways = ready .then(|| { if lowest { - lower(local, &serving, required) + lower(local, &serving) } else { sideways(local, &serving, required) } @@ -240,22 +240,17 @@ fn sideways(local: Epoch, serving: &[(String, Epoch)], required: usize) -> Optio most_followed(&others) } -/// For a ready node holding a store, another epoch of its own generation with a lower ID -/// than its own that `required` peers are on, the lowest: of two epochs decided for one, -/// a node follows the lower ID, and a node on the other rejoins it (`docs/store.md`, -/// Remakes), however many are on each. -fn lower(local: Epoch, serving: &[(String, Epoch)], required: usize) -> Option { - let mut counts: HashMap = HashMap::new(); - for (_, epoch) in serving { - if epoch.generation == local.generation && epoch.id < local.id { - *counts.entry(*epoch).or_default() += 1; - } - } - let lower: Vec<(Epoch, usize)> = counts - .into_iter() - .filter(|(_, count)| *count >= required) - .collect(); - lowest_id(&lower) +/// For a ready node holding a store, the lowest epoch of its own generation, with a lower +/// ID than its own, that a peer is on: of two epochs decided for one, a node follows the +/// lower ID, and a node on the other rejoins it (`docs/store.md`, Remakes), however many +/// are on each. One made up keeps it apart at worst, and makes it take nothing it +/// wouldn't (`Holding::follows`). +fn lower(local: Epoch, serving: &[(String, Epoch)]) -> Option { + serving + .iter() + .map(|(_, epoch)| *epoch) + .filter(|epoch| epoch.generation == local.generation && epoch.id < local.id) + .min_by_key(|epoch| epoch.id) } /// Of epochs, the one with the lowest ID. @@ -815,8 +810,8 @@ mod tests { assert_eq!(sideways(ours, &serving, 2), Some(theirs)); } - /// Holding a store: of two epochs of the local generation, the lower ID, however many - /// peers are on each; a higher one isn't followed. + /// Holding a store: of two epochs of the local generation, the lower ID, however few + /// peers are on it; a higher one isn't followed. #[test] fn holding_a_store_a_node_follows_the_lower_epoch() { let epoch = |id: u8| Epoch { @@ -830,16 +825,9 @@ mod tests { .map(|(peer, epoch)| ((*peer).to_owned(), *epoch)) .collect() }; - let serving = on(&[ - ("a", ours), - ("b", ours), - ("c", ours), - ("d", low), - ("e", low), - ]); - assert_eq!(lower(ours, &serving, 2), Some(low)); - assert_eq!(lower(ours, &on(&[("d", low)]), 2), None); - assert_eq!(lower(ours, &on(&[("d", high), ("e", high)]), 2), None); + let serving = on(&[("a", ours), ("b", ours), ("c", ours), ("d", low)]); + assert_eq!(lower(ours, &serving), Some(low)); + assert_eq!(lower(ours, &on(&[("d", high), ("e", high)])), None); assert_eq!(lowest_id(&[(high, 3), (low, 1)]), Some(low)); } -- cgit v1.3.1