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 Ok((
186 ExecutionProgressView {
187 intent,
188 graph: plan.graph().digest(),
189 applied_operations: journals
190 .keys()
191 .filter(|sequence| is_applied(sequence))
192 .count(),
193 attempts: totals,
194 operations,
195 },
196 journals,
197 ))
198}
199impl ExecutionAttemptTotalsView {
200 fn add(&mut self, attempts: &AttemptJournalView) -> Result<(), ExecutionProgressError> {
201 self.mutations_used = sum(self.mutations_used, attempts.mutations_used)?;
202 self.observations_used = sum(self.observations_used, attempts.observations_used)?;
203 self.mutations_remaining = sum(self.mutations_remaining, attempts.mutations_remaining)?;
204 self.observations_remaining =
205 sum(self.observations_remaining, attempts.observations_remaining)?;
206 Ok(())
207 }
208}
209fn sum(left: u32, right: u32) -> Result<u32, ExecutionProgressError> {
210 left.checked_add(right)
211 .ok_or(ExecutionProgressError::AccountingOverflow)
212}
213fn condition(attempts: &AttemptJournalView, unmet_dependencies: bool) -> OperationProgressState {
214 if attempts.applied {
215 OperationProgressState::Applied
216 } else if attempts.pending_observation.is_some() {
217 OperationProgressState::ObservationUnresolved
218 } else if attempts.pending_mutation.is_some() {
219 if attempts.observations_remaining == 0 {
220 OperationProgressState::ReconciliationExhausted
221 } else {
222 OperationProgressState::MutationUnresolved
223 }
224 } else if unmet_dependencies {
225 OperationProgressState::AwaitingDependencies
226 } else if attempts.mutations_remaining == 0 {
227 OperationProgressState::MutationExhausted
228 } else {
229 OperationProgressState::MutationAvailable
230 }
231}
232
233#[derive(Debug, Error, Eq, PartialEq)]
235pub enum ExecutionProgressError {
236 #[error("execution progress exceeds {MAX_EFFECT_OPERATIONS} journals")]
238 TooManyJournals,
239 #[error("duplicate journal for operation {0}")]
241 DuplicateJournal(u64),
242 #[error("journal operation {0} is absent from the original plan")]
244 UnknownOperation(u64),
245 #[error("missing original journal for operation {0}")]
247 MissingJournal(u64),
248 #[error("original journal authority mismatch for operation {0}")]
250 AuthorityMismatch(u64),
251 #[error(
253 "operation {operation_sequence} was attempted without applied prerequisite {prerequisite}"
254 )]
255 PrematureAttempt {
256 operation_sequence: u64,
258 prerequisite: u64,
260 },
261 #[error("execution attempt accounting overflow")]
263 AccountingOverflow,
264}
265
266#[cfg(test)]
267mod tests;