Skip to main content

quorum_set/quorum/
quorum_set.rs

1use std::sync::Arc;
2
3/// Common interface for every quorum rule supported by this crate.
4///
5/// A quorum is a collection of nodes that a read or write operation in a distributed system has to
6/// contact. See: <http://web.mit.edu/6.033/2005/wwwdocs/quorum_note.html>
7///
8/// Implementations must be upward-closed: adding IDs to an accepted quorum must keep it accepted.
9/// The crate provides implementations for flat majority sets, joint quorum sets, and hierarchical
10/// [`QuorumTree`](crate::QuorumTree) rules.
11pub trait QuorumSet {
12    /// Node ID type in this quorum set.
13    type Id: 'static;
14
15    /// Iterator over every voter ID tracked by this quorum set.
16    ///
17    /// Implementations that combine multiple sub-rules return each ID once.
18    type Iter: Iterator<Item = Self::Id>;
19
20    /// Return `true` if the candidate IDs satisfy this quorum rule.
21    fn is_quorum<'a, I: Iterator<Item = &'a Self::Id> + Clone>(&self, ids: I) -> bool;
22
23    /// Return all voter IDs in this quorum set.
24    fn ids(&self) -> Self::Iter;
25}
26
27impl<T: QuorumSet> QuorumSet for Arc<T> {
28    type Id = T::Id;
29
30    type Iter = T::Iter;
31
32    fn is_quorum<'a, I: Iterator<Item = &'a Self::Id> + Clone>(&self, ids: I) -> bool {
33        self.as_ref().is_quorum(ids)
34    }
35
36    fn ids(&self) -> Self::Iter {
37        self.as_ref().ids()
38    }
39}