Skip to main content

quorum_set/quorum/
quorum_intersection_impl.rs

1use std::collections::BTreeSet;
2
3use crate::quorum::QuorumBridge;
4use crate::quorum::QuorumIntersection;
5
6/// Two joint configs are treated as having quorum intersection when they share at least one
7/// config: every quorum of one then intersects every quorum of the other. Sharing a config is
8/// sufficient but not necessary for that property, so this check answers `None` for a pair of
9/// configs whose quorums do in fact all intersect.
10impl<ID> QuorumIntersection<Vec<BTreeSet<ID>>> for Vec<BTreeSet<ID>>
11where ID: Ord + Clone
12{
13    /// Return `Some(true)` when two joint quorum sets share a config.
14    ///
15    /// Return `Some(false)` when either joint is empty: an empty joint accepts the empty set as a
16    /// quorum, and the empty set intersects nothing.
17    ///
18    /// Return `None` for every other pair, because the absence of a shared config proves nothing.
19    ///
20    /// Read more about extended membership change in OpenRaft:
21    /// <https://docs.rs/openraft/latest/openraft/docs/data/extended_membership/index.html>
22    fn intersects_with(&self, other: &Vec<BTreeSet<ID>>) -> Option<bool> {
23        let either_is_empty = self.is_empty() || other.is_empty();
24        if either_is_empty {
25            return Some(false);
26        }
27
28        for a in self {
29            for b in other {
30                if a == b {
31                    return Some(true);
32                }
33            }
34        }
35        None
36    }
37}
38
39/// Builds an intermediate joint quorum set for a target flat config.
40impl<ID> QuorumBridge<BTreeSet<ID>> for Vec<BTreeSet<ID>>
41where ID: Ord + Clone
42{
43    fn bridge_to(&self, other: BTreeSet<ID>) -> Self {
44        let intersects = self.intersects_with(&vec![other.clone()]);
45        if intersects == Some(true) {
46            vec![other]
47        } else if let Some(last) = self.last() {
48            vec![last.clone(), other]
49        } else {
50            vec![other]
51        }
52    }
53}