ic_backup/policy/execution_progress/
mod.rs1use crate::model::{
4 artifacts::ArtifactChecksumRecord,
5 attempt_journal::{AttemptJournalRecord, AttemptJournalView},
6 effect_graph::MAX_EFFECT_OPERATIONS,
7 operation_plan::OperationPlanRecord,
8};
9use serde::Serialize;
10use std::collections::BTreeMap;
11use thiserror::Error;
12
13#[derive(Clone, Debug)]
15pub struct ExecutionProgressRequest<'a> {
16 pub plan: &'a OperationPlanRecord,
18 pub journals: &'a [&'a AttemptJournalRecord],
20}
21
22#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
24#[serde(rename_all = "snake_case")]
25pub enum OperationProgressState {
26 AwaitingDependencies,
28 MutationAvailable,
30 MutationExhausted,
32 MutationUnresolved,
34 ObservationUnresolved,
36 ReconciliationExhausted,
38 Applied,
40}
41
42#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
44pub struct OperationProgressView {
45 pub operation_sequence: u64,
47 pub state: OperationProgressState,
49 pub attempts: AttemptJournalView,
51}
52
53#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
55pub struct ExecutionAttemptTotalsView {
56 pub mutations_used: u32,
58 pub observations_used: u32,
60 pub mutations_remaining: u32,
62 pub observations_remaining: u32,
64}
65
66#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
71pub struct ExecutionProgressView {
72 pub intent: ArtifactChecksumRecord,
74 pub graph: ArtifactChecksumRecord,
76 pub applied_operations: usize,
78 pub attempts: ExecutionAttemptTotalsView,
80 pub operations: Vec<OperationProgressView>,
82}
83
84impl ExecutionProgressView {
85 pub(crate) const fn is_unattempted(&self) -> bool {
86 self.attempts.mutations_used == 0 && self.attempts.observations_used == 0
87 }
88
89 pub(crate) fn all_applied(&self) -> bool {
90 self.applied_operations == self.operations.len()
91 }
92}
93
94pub fn progress(
105 request: &ExecutionProgressRequest<'_>,
106) -> Result<ExecutionProgressView, ExecutionProgressError> {
107 Ok(progress_with_journals(request.plan, request.journals.iter().copied())?.0)
108}
109
110pub(crate) fn progress_with_journals<'a>(
113 plan: &OperationPlanRecord,
114 originals: impl ExactSizeIterator<Item = &'a AttemptJournalRecord>,
115) -> Result<
116 (
117 ExecutionProgressView,
118 BTreeMap<u64, &'a AttemptJournalRecord>,
119 ),
120 ExecutionProgressError,
121> {
122 if originals.len() > MAX_EFFECT_OPERATIONS {
123 return Err(ExecutionProgressError::TooManyJournals);
124 }
125 let intent = plan.digest();
126 let mut journals = BTreeMap::new();
127 for journal in originals {
128 let sequence = journal.authority().binding().operation_sequence();
129 let operation = plan
130 .operation(sequence)
131 .map_err(|_| ExecutionProgressError::UnknownOperation(sequence))?;
132 if journals.contains_key(&sequence) {
133 return Err(ExecutionProgressError::DuplicateJournal(sequence));
134 }
135 if !plan.matches_authority(operation, &intent, journal.authority()) {
136 return Err(ExecutionProgressError::AuthorityMismatch(sequence));
137 }
138 journals.insert(sequence, journal);
139 }
140 for operation in plan.operations() {
141 if !journals.contains_key(&operation.operation_sequence()) {
142 return Err(ExecutionProgressError::MissingJournal(
143 operation.operation_sequence(),
144 ));
145 }
146 }
147 let is_applied = |sequence: &u64| {
148 journals
149 .get(sequence)
150 .is_some_and(|journal| journal.applied_receipt().is_some())
151 };
152 let mut totals = ExecutionAttemptTotalsView {
153 mutations_used: 0,
154 observations_used: 0,
155 mutations_remaining: 0,
156 observations_remaining: 0,
157 };
158 let mut operations = Vec::with_capacity(journals.len());
159 for node in plan.graph().ordered_nodes() {
160 let sequence = node.operation_sequence();
161 let attempts = journals
163 .get(&sequence)
164 .ok_or(ExecutionProgressError::MissingJournal(sequence))?
165 .view();
166 let unmet = node
167 .depends_on()
168 .iter()
169 .find(|dependency| !is_applied(dependency));
170 if attempts.mutations_used != 0
171 && let Some(prerequisite) = unmet
172 {
173 return Err(ExecutionProgressError::PrematureAttempt {
174 operation_sequence: sequence,
175 prerequisite: *prerequisite,
176 });
177 }
178 totals.add(&attempts)?;
179 operations.push(OperationProgressView {
180 operation_sequence: sequence,
181 state: condition(&attempts, unmet.is_some()),
182 attempts,
183 });
184 }
185 let applied_operations = operations
186 .iter()
187 .filter(|operation| operation.attempts.applied)
188 .count();
189 Ok((
190 ExecutionProgressView {
191 intent,
192 graph: plan.graph().digest(),
193 applied_operations,
194 attempts: totals,
195 operations,
196 },
197 journals,
198 ))
199}
200impl ExecutionAttemptTotalsView {
201 fn add(&mut self, attempts: &AttemptJournalView) -> Result<(), ExecutionProgressError> {
202 self.mutations_used = sum(self.mutations_used, attempts.mutations_used)?;
203 self.observations_used = sum(self.observations_used, attempts.observations_used)?;
204 self.mutations_remaining = sum(self.mutations_remaining, attempts.mutations_remaining)?;
205 self.observations_remaining =
206 sum(self.observations_remaining, attempts.observations_remaining)?;
207 Ok(())
208 }
209}
210fn sum(left: u32, right: u32) -> Result<u32, ExecutionProgressError> {
211 left.checked_add(right)
212 .ok_or(ExecutionProgressError::AccountingOverflow)
213}
214fn condition(attempts: &AttemptJournalView, unmet_dependencies: bool) -> OperationProgressState {
215 if attempts.applied {
216 OperationProgressState::Applied
217 } else if attempts.pending_observation.is_some() {
218 OperationProgressState::ObservationUnresolved
219 } else if attempts.pending_mutation.is_some() {
220 if attempts.observations_remaining == 0 {
221 OperationProgressState::ReconciliationExhausted
222 } else {
223 OperationProgressState::MutationUnresolved
224 }
225 } else if unmet_dependencies {
226 OperationProgressState::AwaitingDependencies
227 } else if attempts.mutations_remaining == 0 {
228 OperationProgressState::MutationExhausted
229 } else {
230 OperationProgressState::MutationAvailable
231 }
232}
233
234#[derive(Debug, Error, Eq, PartialEq)]
236pub enum ExecutionProgressError {
237 #[error("execution progress exceeds {MAX_EFFECT_OPERATIONS} journals")]
239 TooManyJournals,
240 #[error("duplicate journal for operation {0}")]
242 DuplicateJournal(u64),
243 #[error("journal operation {0} is absent from the original plan")]
245 UnknownOperation(u64),
246 #[error("missing original journal for operation {0}")]
248 MissingJournal(u64),
249 #[error("original journal authority mismatch for operation {0}")]
251 AuthorityMismatch(u64),
252 #[error(
254 "operation {operation_sequence} was attempted without applied prerequisite {prerequisite}"
255 )]
256 PrematureAttempt {
257 operation_sequence: u64,
259 prerequisite: u64,
261 },
262 #[error("execution attempt accounting overflow")]
264 AccountingOverflow,
265}
266
267#[cfg(test)]
268mod tests;