ic_backup/ops/persistence/execution_workflow/
mod.rs1mod ancestry;
4mod progress;
5mod stage;
6use ancestry::{
7 acquire_stage, read_admitted_binding, read_binding, read_current_binding, stage_path,
8 validate_predecessors,
9};
10pub use progress::{ExecutionWorkflowProgressError, read_execution_workflow_progress};
11pub use stage::ExecutionStageGuard;
12#[cfg(test)]
13use stage::StagePreparationBarrier;
14
15use super::{
16 AttemptJournalError, AttemptJournalGuard, BackupLayoutGuard, ExecutionProgressPersistenceError,
17 ExecutionSettlementCheckpointError, ExecutionSettlementPersistenceError, JournalLock,
18 JournalLockError, OperationPlanPersistenceError, PersistenceError, read_execution_progress,
19 read_execution_settlement, read_json, read_operation_plan, reconcile_execution_settlement,
20 replay_execution_settlement,
21};
22use crate::model::{
23 artifacts::ArtifactChecksumRecord,
24 execution_workflow::{
25 ExecutionStageBindingRecord, ExecutionStagePredecessorRecord, ExecutionWorkflowError,
26 ExecutionWorkflowRecord, MAX_EXECUTION_STAGE_BYTES, MAX_EXECUTION_WORKFLOW_BYTES,
27 },
28 operation_plan::{OperationPlanError, OperationPlanRecord},
29};
30use crate::policy::execution_progress::ExecutionProgressView;
31use std::{
32 collections::{BTreeMap, BTreeSet, btree_map::Entry},
33 fs,
34 path::PathBuf,
35};
36use thiserror::Error;
37
38const WORKFLOW_FILE: &str = "execution-workflow.json";
39const BINDING_FILE: &str = "stage-binding.json";
40
41#[derive(Clone, Debug, Eq, PartialEq)]
47pub struct ExecutionWorkflowSettlementView {
48 workflow: ArtifactChecksumRecord,
49 stages: Vec<ExecutionStageSettlementView>,
50}
51impl ExecutionWorkflowSettlementView {
52 #[must_use]
54 pub const fn workflow(&self) -> &ArtifactChecksumRecord {
55 &self.workflow
56 }
57 #[must_use]
59 pub fn stages(&self) -> &[ExecutionStageSettlementView] {
60 &self.stages
61 }
62}
63
64#[derive(Clone, Debug, Eq, PartialEq)]
66pub struct ExecutionStageSettlementView {
67 stage_sequence: u64,
68 binding: ArtifactChecksumRecord,
69 settlement: ArtifactChecksumRecord,
70}
71impl ExecutionStageSettlementView {
72 #[must_use]
74 pub const fn stage_sequence(&self) -> u64 {
75 self.stage_sequence
76 }
77 #[must_use]
79 pub const fn binding(&self) -> &ArtifactChecksumRecord {
80 &self.binding
81 }
82 #[must_use]
84 pub const fn settlement(&self) -> &ArtifactChecksumRecord {
85 &self.settlement
86 }
87}
88
89pub fn create_execution_workflow(
93 layout: &BackupLayoutGuard,
94 record: &ExecutionWorkflowRecord,
95) -> Result<(), ExecutionWorkflowPersistenceError> {
96 layout.check_root()?;
97 let bytes = super::json::encode_json(record, MAX_EXECUTION_WORKFLOW_BYTES)?;
98 super::immutable_record::create_encoded_record(layout, WORKFLOW_FILE, &bytes)
99}
100pub fn read_execution_workflow(
104 layout: &BackupLayoutGuard,
105 expected: &ArtifactChecksumRecord,
106) -> Result<ExecutionWorkflowRecord, ExecutionWorkflowPersistenceError> {
107 super::immutable_record::read(
108 layout,
109 WORKFLOW_FILE,
110 MAX_EXECUTION_WORKFLOW_BYTES,
111 |record: &ExecutionWorkflowRecord| &record.digest() == expected,
112 || ExecutionWorkflowPersistenceError::DigestMismatch,
113 )
114}
115
116pub(crate) fn read_execution_stage_occupancy(
124 layout: &BackupLayoutGuard,
125 expected: &ArtifactChecksumRecord,
126) -> Result<BTreeSet<u64>, ExecutionWorkflowPersistenceError> {
127 let workflow = read_execution_workflow(layout, expected)?;
128 let mut occupied = BTreeSet::new();
129 for stage in workflow.stages() {
130 let sequence = stage.operation_sequence();
131 match fs::symlink_metadata(stage_path(layout, sequence)) {
132 Ok(metadata) if metadata.is_dir() => {
133 occupied.insert(sequence);
134 }
135 Ok(_) => return Err(ExecutionWorkflowPersistenceError::UnsafeStage),
136 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
137 Err(error) => return Err(PersistenceError::from(error).into()),
138 }
139 }
140 read_execution_workflow(layout, expected)?;
141 Ok(occupied)
142}
143
144pub fn read_complete_execution_workflow(
157 layout: &BackupLayoutGuard,
158 expected: &ArtifactChecksumRecord,
159) -> Result<ExecutionWorkflowSettlementView, ExecutionStageCheckpointError> {
160 read_complete_with(layout, expected, || {
161 read_execution_workflow(layout, expected)
162 })
163}
164
165pub fn read_complete_execution_workflow_locked(
175 layout: &BackupLayoutGuard,
176 journal: &JournalLock,
177 expected: &ArtifactChecksumRecord,
178) -> Result<ExecutionWorkflowSettlementView, ExecutionStageCheckpointError> {
179 if journal.journal() != layout.root().join(WORKFLOW_FILE) {
180 return Err(ExecutionWorkflowPersistenceError::DigestMismatch.into());
181 }
182 read_complete_with(layout, expected, || {
183 layout.check_root()?;
184 let workflow: ExecutionWorkflowRecord =
185 read_json(journal.journal(), MAX_EXECUTION_WORKFLOW_BYTES)?;
186 super::json::check_json_size(&workflow, MAX_EXECUTION_WORKFLOW_BYTES)?;
187 if &workflow.digest() != expected {
188 return Err(ExecutionWorkflowPersistenceError::DigestMismatch);
189 }
190 Ok(workflow)
191 })
192}
193
194fn read_complete_with(
195 layout: &BackupLayoutGuard,
196 expected: &ArtifactChecksumRecord,
197 read_original: impl Fn() -> Result<ExecutionWorkflowRecord, ExecutionWorkflowPersistenceError>,
198) -> Result<ExecutionWorkflowSettlementView, ExecutionStageCheckpointError> {
199 let workflow = read_original()?;
200 let mut stages = Vec::with_capacity(workflow.stages().len());
201 for allocated in workflow.stages() {
202 let sequence = allocated.operation_sequence();
203 let stage = acquire_stage(layout, sequence)?;
204 let (binding, plan) = read_current_binding(&stage, &workflow, sequence)?;
205 let settlement = replay_execution_settlement(&stage, &plan.digest())?;
206 read_binding(&stage, &workflow, sequence, &binding.digest())?;
207 let view = ExecutionStageSettlementView {
208 stage_sequence: sequence,
209 binding: binding.digest(),
210 settlement: settlement.digest(),
211 };
212 stages.push((binding, view));
213 }
214 for (binding, _) in &stages {
215 for predecessor in binding.predecessors() {
216 let index = stages
218 .binary_search_by_key(&predecessor.stage_sequence(), |(_, view)| {
219 view.stage_sequence()
220 })
221 .map_err(|_| ExecutionWorkflowPersistenceError::DigestMismatch)?;
222 let actual = &stages[index].1;
223 if predecessor.binding() != actual.binding()
224 || predecessor.settlement() != actual.settlement()
225 {
226 return Err(ExecutionWorkflowPersistenceError::DigestMismatch.into());
227 }
228 }
229 }
230 read_original()?;
231 Ok(ExecutionWorkflowSettlementView {
232 workflow: expected.clone(),
233 stages: stages.into_iter().map(|(_, view)| view).collect(),
234 })
235}
236
237#[derive(Debug, Error)]
239pub enum ExecutionStageCheckpointError {
240 #[error(transparent)]
242 Stage(#[from] ExecutionWorkflowPersistenceError),
243 #[error(transparent)]
245 Settlement(#[from] ExecutionSettlementCheckpointError),
246}
247
248#[derive(Debug, Error)]
250pub enum ExecutionStagePreparationError {
251 #[error(transparent)]
253 Stage(#[from] ExecutionWorkflowPersistenceError),
254 #[error(transparent)]
256 Authority(#[from] OperationPlanError),
257 #[error(transparent)]
259 Journal(#[from] AttemptJournalError),
260}
261
262#[derive(Debug, Error)]
264pub enum ExecutionStageResumeError {
265 #[error(transparent)]
267 Stage(#[from] ExecutionWorkflowPersistenceError),
268 #[error(transparent)]
270 Progress(#[from] ExecutionProgressPersistenceError),
271}
272#[derive(Debug, Error)]
274pub enum ExecutionWorkflowPersistenceError {
275 #[error("execution workflow/stage digest mismatch")]
277 DigestMismatch,
278 #[error("unsafe execution stage directory")]
280 UnsafeStage,
281 #[error(transparent)]
283 Model(#[from] ExecutionWorkflowError),
284 #[error(transparent)]
286 Plan(#[from] OperationPlanPersistenceError),
287 #[error(transparent)]
289 Settlement(#[from] ExecutionSettlementPersistenceError),
290 #[error(transparent)]
292 Lock(#[from] JournalLockError),
293 #[error(transparent)]
295 Persistence(#[from] PersistenceError),
296}
297
298#[cfg(all(test, unix))]
299mod tests;