Skip to main content

ic_backup/model/ic_snapshot_upload/plan/
mod.rs

1//! Exact bounded data-upload declarations, retaining no aggregate payload bytes.
2
3use super::{
4    IcSnapshotUploadError, IcSnapshotUploadReply, IcSnapshotUploadReplyKind,
5    IcSnapshotUploadRequest,
6};
7use crate::model::{
8    artifacts::ArtifactChecksumRecord,
9    attempt_journal::AttemptJournalRecordError,
10    data_stage_plan::{self, DataStagePlanError, LearnedDataStage},
11    effect_graph::EffectGraphError,
12    execution_workflow::{
13        ExecutionStageBindingRecord, ExecutionStagePredecessorRecord, ExecutionWorkflowError,
14        ExecutionWorkflowRecord,
15    },
16    ic_snapshot_data::{ExtentPlanningError, planned_extents},
17    operation_plan::{OperationPlanError, OperationPlanRecord, PlannedOperationRecord},
18};
19use ic_platform::management_canister_types::SnapshotDataKind;
20use thiserror::Error;
21
22/// Complete ordered extents and source-bound request digests for one original data stage.
23///
24/// Guarded local source preparation creates this declaration. Module, heap and stable
25/// regions precede known chunks in original metadata order. Every operation has one
26/// update and zero observations with an explicit predecessor dependency. Original
27/// stage headroom stays unassigned. No upload bytes, spending, authenticated allocation
28/// or complete-transfer proof are retained here. Empty data needs no placeholder plan.
29pub struct IcSnapshotDataUploadPlan<'workflow, 'request, 'source> {
30    binding: LearnedDataStage<'workflow>,
31    metadata: &'request IcSnapshotUploadRequest<'source>,
32    destination: Vec<u8>,
33    kinds: Vec<SnapshotDataKind>,
34    plan: Option<OperationPlanRecord>,
35}
36// Completion records the admitted operation, never a new effect or persisted mode.
37#[derive(Clone)]
38pub(crate) enum UploadCompletion {
39    Data,
40    Allocation {
41        sequence: u64,
42        evidence: ArtifactChecksumRecord,
43    },
44}
45
46// Carry the admitted allocation and bounded extents across guarded source IO.
47// Only finish can turn their exact ordered byte bindings into a public plan.
48pub(crate) struct DataUploadPreparation<'workflow, 'request, 'source> {
49    workflow: &'workflow ExecutionWorkflowRecord,
50    stage: &'workflow PlannedOperationRecord,
51    metadata: &'request IcSnapshotUploadRequest<'source>,
52    destination: Vec<u8>,
53    allocation_evidence: ArtifactChecksumRecord,
54    kinds: Vec<SnapshotDataKind>,
55}
56
57impl<'workflow, 'request, 'source> DataUploadPreparation<'workflow, 'request, 'source> {
58    pub(crate) fn new(
59        workflow: &'workflow ExecutionWorkflowRecord,
60        sequence: u64,
61        allocation: &IcSnapshotUploadReply<'request, 'source>,
62        chunk_bytes: u64,
63    ) -> Result<Self, IcSnapshotDataUploadPlanningError> {
64        let metadata = allocation.request();
65        let stage = workflow.stage(sequence)?;
66        let IcSnapshotUploadReplyKind::Metadata { snapshot_id } = allocation.kind() else {
67            return Err(IcSnapshotDataUploadPlanningError::OriginalMismatch);
68        };
69        metadata.validate_data_destination(snapshot_id)?;
70        if stage.target() != metadata.target()
71            || !super::attempt::source_context_matches(workflow.allocation(), metadata)
72        {
73            return Err(IcSnapshotDataUploadPlanningError::OriginalMismatch);
74        }
75        let kinds = planned_extents(metadata.source(), chunk_bytes, stage.budget().mutations())?;
76        Ok(Self {
77            workflow,
78            stage,
79            metadata,
80            destination: snapshot_id.clone(),
81            allocation_evidence: allocation.digest(),
82            kinds,
83        })
84    }
85
86    pub(crate) const fn metadata(&self) -> &'request IcSnapshotUploadRequest<'source> {
87        self.metadata
88    }
89    pub(crate) fn destination(&self) -> &[u8] {
90        &self.destination
91    }
92    pub(crate) fn kinds(&self) -> &[SnapshotDataKind] {
93        &self.kinds
94    }
95
96    pub(crate) fn finish(
97        self,
98        bindings: &[ArtifactChecksumRecord],
99    ) -> Result<
100        IcSnapshotDataUploadPlan<'workflow, 'request, 'source>,
101        IcSnapshotDataUploadPlanningError,
102    > {
103        if bindings.len() != self.kinds.len() {
104            return Err(IcSnapshotDataUploadPlanningError::OriginalMismatch);
105        }
106        let plan = data_stage_plan::build(
107            self.workflow.allocation(),
108            self.stage,
109            bindings.iter().cloned(),
110        )?;
111        Ok(IcSnapshotDataUploadPlan {
112            binding: LearnedDataStage {
113                workflow: self.workflow,
114                sequence: self.stage.operation_sequence(),
115                evidence: self.allocation_evidence,
116            },
117            metadata: self.metadata,
118            destination: self.destination,
119            kinds: self.kinds,
120            plan,
121        })
122    }
123}
124
125impl<'request, 'source> IcSnapshotDataUploadPlan<'_, 'request, 'source> {
126    pub(crate) fn workflow_digest(&self) -> ArtifactChecksumRecord {
127        self.binding.workflow.digest()
128    }
129    pub(crate) fn validate_completion(
130        &self,
131        binding: &ExecutionStageBindingRecord,
132        plan: &OperationPlanRecord,
133        learned: &ArtifactChecksumRecord,
134    ) -> Result<UploadCompletion, IcSnapshotDataUploadPlanningError> {
135        if let Some(expected) = self.plan() {
136            self.validate_binding(binding)?;
137            if plan != expected {
138                return Err(IcSnapshotDataUploadPlanningError::OriginalMismatch);
139            }
140            Ok(UploadCompletion::Data)
141        } else {
142            binding.validate(self.binding.workflow, plan)?;
143            if plan.operations().len() != 1
144                || plan.operations()[0].target() != self.metadata.target()
145                || plan.operations()[0].request() != self.metadata.binding_digest().hash()
146                || learned != &self.binding.evidence
147            {
148                return Err(IcSnapshotDataUploadPlanningError::OriginalMismatch);
149            }
150            Ok(UploadCompletion::Allocation {
151                sequence: plan.operations()[0].operation_sequence(),
152                evidence: self.binding.evidence.clone(),
153            })
154        }
155    }
156    /// Read exact ordered source extents. Operation sequences are zero-based indices.
157    #[must_use]
158    pub fn kinds(&self) -> &[SnapshotDataKind] {
159        &self.kinds
160    }
161    /// Read the original metadata allocation declaration and retained source association.
162    #[must_use]
163    pub const fn metadata(&self) -> &'request IcSnapshotUploadRequest<'source> {
164        self.metadata
165    }
166    /// Read exact declared destination bytes; they grant no allocation authority.
167    #[must_use]
168    pub fn destination(&self) -> &[u8] {
169        &self.destination
170    }
171    /// Read the immutable child plan, or None when there are no data writes.
172    #[must_use]
173    pub const fn plan(&self) -> Option<&OperationPlanRecord> {
174        self.plan.as_ref()
175    }
176
177    /// Bind the exact original singleton allocation and request/raw-reply evidence.
178    ///
179    /// Existing stage persistence requires complete Applied predecessor journals and
180    /// their chronological checkpoint. This check authenticates neither allocation
181    /// attribution nor byte custody and grants no dispatch/load/start authority.
182    /// # Errors
183    /// Rejects an empty plan, changed allocation/request/evidence or original dependency.
184    pub fn bind(
185        &self,
186        allocation_binding: &ExecutionStageBindingRecord,
187        allocation_plan: &OperationPlanRecord,
188        predecessors: Vec<ExecutionStagePredecessorRecord>,
189    ) -> Result<ExecutionStageBindingRecord, IcSnapshotDataUploadPlanningError> {
190        let plan = self
191            .plan
192            .as_ref()
193            .ok_or(IcSnapshotDataUploadPlanningError::NoDataWrites)?;
194        self.binding
195            .bind(
196                plan,
197                allocation_binding,
198                allocation_plan,
199                self.metadata.binding_digest().hash(),
200                predecessors,
201            )?
202            .ok_or(IcSnapshotDataUploadPlanningError::OriginalMismatch)
203    }
204    pub(crate) fn validate_binding(
205        &self,
206        binding: &ExecutionStageBindingRecord,
207    ) -> Result<(), IcSnapshotDataUploadPlanningError> {
208        let plan = self
209            .plan
210            .as_ref()
211            .ok_or(IcSnapshotDataUploadPlanningError::NoDataWrites)?;
212        if !self.binding.validate(binding, plan)? {
213            return Err(IcSnapshotDataUploadPlanningError::OriginalMismatch);
214        }
215        Ok(())
216    }
217}
218
219/// Planning rejection changes no source bytes, journal, allowance or remote effects.
220#[derive(Debug, Error)]
221pub enum IcSnapshotDataUploadPlanningError {
222    /// Chunk size is outside the existing 1..=1 MiB data boundary.
223    #[error("invalid snapshot data upload chunk size")]
224    InvalidChunkSize,
225    /// Complete declared extent counts overflow nat64 or host indexing.
226    #[error("snapshot data upload count overflow")]
227    CountOverflow,
228    /// Complete data writes cannot fit their original stage allocation.
229    #[error("snapshot data upload needs {required} updates; original allocation is {original}")]
230    InsufficientAllowance {
231        /// Complete required update count, excluding metadata allocation.
232        required: u64,
233        /// Original data-stage ceiling, excluding unassigned workflow headroom.
234        original: u32,
235    },
236    /// Original context, source, allocation, request or predecessor evidence differs.
237    #[error("snapshot data upload originals differ")]
238    OriginalMismatch,
239    /// Empty data has no placeholder child plan or checkpoint.
240    #[error("snapshot data upload has no data writes to bind")]
241    NoDataWrites,
242    /// Existing immutable operation plan admission.
243    #[error(transparent)]
244    Plan(#[from] OperationPlanError),
245    /// Existing explicit graph admission.
246    #[error(transparent)]
247    Graph(#[from] EffectGraphError),
248    /// Existing original stage binding admission.
249    #[error(transparent)]
250    Workflow(#[from] ExecutionWorkflowError),
251    /// Existing exact source/destination codec admission.
252    #[error(transparent)]
253    Upload(#[from] IcSnapshotUploadError),
254    /// Existing original per-operation attempt admission.
255    #[error(transparent)]
256    Budget(#[from] AttemptJournalRecordError),
257}
258impl From<ExtentPlanningError> for IcSnapshotDataUploadPlanningError {
259    fn from(error: ExtentPlanningError) -> Self {
260        match error {
261            ExtentPlanningError::InvalidChunkSize => Self::InvalidChunkSize,
262            ExtentPlanningError::CountOverflow => Self::CountOverflow,
263            ExtentPlanningError::InsufficientAllowance { required, original } => {
264                Self::InsufficientAllowance { required, original }
265            }
266        }
267    }
268}
269
270impl From<DataStagePlanError> for IcSnapshotDataUploadPlanningError {
271    fn from(error: DataStagePlanError) -> Self {
272        match error {
273            DataStagePlanError::CountOverflow => Self::CountOverflow,
274            DataStagePlanError::Graph(error) => Self::Graph(error),
275            DataStagePlanError::Plan(error) => Self::Plan(error),
276            DataStagePlanError::Budget(error) => Self::Budget(error),
277        }
278    }
279}
280
281#[cfg(test)]
282mod tests;