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, storage, or election timeout. Instants that already
10    /// passed were handled by the last poll.
11    pub fn next_deadline(&self) -> Option<Duration> {
12        let candidates = match &self.role {
13            Role::Fenced(_) => [None; 6],
14            Role::Installing(_) => [
15                Some(self.election_deadline),
16                self.exit_retry_at,
17                None,
18                None,
19                None,
20                None,
21            ],
22            Role::ViewChanging(_) => [
23                Some(self.election_deadline),
24                Some(self.retry_at),
25                self.exit_retry_at,
26                None,
27                None,
28                None,
29            ],
30            Role::Normal(normal) => {
31                let snapshot = normal.snapshot();
32                let primary = VoterIndex::primary(snapshot.scope.view) == self.local;
33                let outstanding =
34                    snapshot.accepted != snapshot.committed || !snapshot.ready_for_appends;
35                let unpersisted = snapshot.accepted.op > snapshot.journal.durable;
36                [
37                    primary.then_some(self.retry_at),
38                    (!primary).then_some(self.contact_deadline),
39                    outstanding.then_some(self.progress_deadline),
40                    unpersisted.then_some(self.persistence_deadline),
41                    self.storage_busy_since
42                        .map(|since| since.saturating_add(self.timing.primary_timeout)),
43                    self.exit_retry_at,
44                ]
45            }
46        };
47        candidates
48            .into_iter()
49            .flatten()
50            .filter(|deadline| *deadline > self.now)
51            .min()
52    }
53}