link_cli/engine/
replication.rs1use super::error;
4use crate::{LinkError, TransitionLogStore};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Synchronization {
8 Asynchronous,
10 Synchronous { acknowledgements: usize },
12}
13
14pub struct ReplicatedLog<P, R> {
15 primary: P,
16 replicas: Vec<R>,
17 policy: Synchronization,
18 trace: bool,
19}
20impl<P: TransitionLogStore, R: TransitionLogStore> ReplicatedLog<P, R> {
21 pub fn new(primary: P, replicas: Vec<R>, policy: Synchronization) -> Result<Self, LinkError> {
22 if let Synchronization::Synchronous { acknowledgements } = policy {
23 if acknowledgements == 0 || acknowledgements > replicas.len() + 1 {
24 return Err(error("acknowledgements must fit the fixed membership"));
25 }
26 }
27 Ok(Self {
28 primary,
29 replicas,
30 policy,
31 trace: false,
32 })
33 }
34 pub fn set_trace(&mut self, enabled: bool) {
35 self.trace = enabled;
36 }
37 pub fn into_parts(self) -> (P, Vec<R>) {
38 (self.primary, self.replicas)
39 }
40 pub fn synchronize(&mut self) -> Result<usize, LinkError> {
43 self.primary.flush_log()?;
44 self.synchronize_flushed()
45 }
46 fn synchronize_flushed(&mut self) -> Result<usize, LinkError> {
49 let entries = self.primary.read_log_entries()?;
50 let mut acknowledgements = 1;
51 for (index, replica) in self.replicas.iter_mut().enumerate() {
52 let result = (|| {
53 let previous = replica.read_log_entries()?;
54 if previous.len() > entries.len() || !entries.starts_with(&previous) {
55 return Err(error("replica log diverges from primary"));
56 }
57 for entry in &entries[previous.len()..] {
58 replica.append_log_entry(entry)?;
59 }
60 replica.flush_log()
61 })();
62 match result {
63 Ok(()) => acknowledgements += 1,
64 Err(cause) => {
65 if self.trace {
66 crate::diagnostics::stderr!(
67 "[Replication] replica={index} unavailable: {cause}"
68 );
69 }
70 }
71 }
72 }
73 if self.trace {
74 crate::diagnostics::stderr!(
75 "[Replication] acknowledgements={acknowledgements}/{}",
76 self.replicas.len() + 1
77 );
78 }
79 Ok(acknowledgements)
80 }
81}
82impl<P: TransitionLogStore, R: TransitionLogStore> TransitionLogStore for ReplicatedLog<P, R> {
83 fn supports_durable_flush(&self) -> bool {
84 self.primary.supports_durable_flush()
85 && match self.policy {
86 Synchronization::Asynchronous => true,
87 Synchronization::Synchronous { acknowledgements } => {
88 acknowledgements == 1
89 || self
90 .replicas
91 .iter()
92 .all(TransitionLogStore::supports_durable_flush)
93 }
94 }
95 }
96 fn append_log_entry(&mut self, entry: &str) -> Result<(), LinkError> {
97 self.primary.append_log_entry(entry)
98 }
99 fn read_log_entries(&mut self) -> Result<Vec<String>, LinkError> {
105 let entries = self.primary.read_log_entries()?;
106 for (index, replica) in self.replicas.iter_mut().enumerate() {
107 if let Ok(copy) = replica.read_log_entries() {
108 if copy.len() > entries.len() && copy.starts_with(&entries) {
109 return Err(error(&format!(
110 "replica {index} holds {} records the primary lacks; repair the primary from it before writing",
111 copy.len() - entries.len()
112 )));
113 }
114 }
115 }
116 Ok(entries)
117 }
118 fn flush_log(&mut self) -> Result<(), LinkError> {
119 self.primary.flush_log()?;
120 if let Synchronization::Synchronous { acknowledgements } = self.policy {
121 let actual = self.synchronize_flushed()?;
122 if actual < acknowledgements {
123 return Err(error(&format!("indeterminate replication outcome: {actual}/{acknowledgements} acknowledgements")));
124 }
125 }
126 Ok(())
127 }
128}