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