Skip to main content

ozzy_replication/driver/
deadline.rs

1//! The next instant at which polling the driver acts without new input.
2
3use super::{ReplicaDriver, Role, VoterIndex};
4use std::time::Duration;
5
6impl ReplicaDriver {
7    /// The earliest future instant at which `poll` acts on time alone: a
8    /// heartbeat, retransmission, or exit retry, or a contact, progress,
9    /// persistence, or election timeout. Instants that already passed were
10    /// handled by the last poll.
11    pub fn next_deadline(&self) -> Option<Duration> {
12        let candidates = match &self.role {
13            Role::Fenced(_) => [None; 5],
14            Role::Installing(_) => [
15                Some(self.election_deadline),
16                self.exit_retry_at,
17                None,
18                None,
19                None,
20            ],
21            Role::ViewChanging(_) => [
22                Some(self.election_deadline),
23                Some(self.retry_at),
24                self.exit_retry_at,
25                None,
26                None,
27            ],
28            Role::Normal(normal) => {
29                let snapshot = normal.snapshot();
30                let primary = VoterIndex::primary(snapshot.scope.view) == self.local;
31                let outstanding =
32                    snapshot.accepted != snapshot.committed || !snapshot.ready_for_appends;
33                let unpersisted = snapshot.accepted.op > snapshot.journal.durable;
34                [
35                    primary.then_some(self.retry_at),
36                    (!primary).then_some(self.contact_deadline),
37                    outstanding.then_some(self.progress_deadline),
38                    unpersisted.then_some(self.persistence_deadline),
39                    self.exit_retry_at,
40                ]
41            }
42        };
43        candidates
44            .into_iter()
45            .flatten()
46            .filter(|deadline| *deadline > self.now)
47            .min()
48    }
49}