aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/remake/catchup.rs46
1 files changed, 17 insertions, 29 deletions
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<H: Host>(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<Epoch> {
- let mut counts: HashMap<Epoch, usize> = 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<Epoch> {
+ 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));
}