Skip to main content

ic_backup/workflow/ic_snapshot_transfer_read/
mod.rs

1//! One freshly reserved metadata/data update; no automatic receipt or retry.
2
3use crate::{
4    model::{
5        ic_snapshot_transfer_read::{
6            IcSnapshotTransferReadError, IcSnapshotTransferReadPayload,
7            IcSnapshotTransferReadRequest, IcSnapshotTransferReadResponse,
8        },
9        operation_plan::OperationPlanError,
10    },
11    ops::persistence::{
12        AttemptJournalError, AttemptJournalGuard, ExecutionProgressPersistenceError,
13        ExecutionStageGuard, ExecutionWorkflowPersistenceError,
14    },
15    policy::ic_snapshot_transfer_read::{
16        IcSnapshotTransferReadAssociationError, validate_response,
17    },
18    ports::{
19        ic_observation::IcObservationProviderError,
20        ic_snapshot_transfer_read::IcSnapshotTransferReadProvider,
21    },
22};
23use thiserror::Error;
24
25/// Durably reserve one original planned read, freshly admit it and invoke its provider once.
26///
27/// Hold no attempt guards on entry. The selected original journal remains locked
28/// through admission, dispatch and passive response association. Missing originals,
29/// pending attempts and unfulfilled prerequisites reject before provider invocation.
30/// This only accepts a newly reserved attempt; reconstructed pending requests are
31/// never dispatched. Every failure after reservation retains its consumed allowance.
32/// Admission and provider submission are awaited under that same selected guard.
33/// Dropping the future releases the guard without refunding or granting reentry.
34///
35/// `admit` must qualify actual fresh method-specific access, original metadata/raw-ID
36/// custody, current application requirements and exclusive never-dispatched command
37/// custody for this exact request. There is no default admission. Any remote preflight
38/// calls require their own prior accounting. The provider retains its existing
39/// authenticated single-update contract, without retries or hidden observations.
40///
41/// The returned bounded reply has only structural association. Integrations must
42/// independently authenticate it and explicitly use the existing journal receipt
43/// owner to record a qualified outcome. Even success leaves the attempt pending.
44/// No artifact, complete-transfer, fence or terminal/reference-release proof follows.
45/// # Errors
46/// Rejects changed originals, payloads, spending, fresh admission, provider failure
47/// or malformed/mismatched replies. Replies returned before a later rejection are
48/// retained in the error. No failure grants a repeat call, refund or cleanup.
49pub async fn read_snapshot<E: std::error::Error + 'static>(
50    stage: &ExecutionStageGuard<'_>,
51    operation_sequence: u64,
52    payload: IcSnapshotTransferReadPayload<'_, '_>,
53    provider: &mut impl IcSnapshotTransferReadProvider,
54    admit: impl AsyncFnOnce(&IcSnapshotTransferReadRequest<'_, '_>) -> Result<(), E>,
55) -> Result<IcSnapshotTransferReadResponse, IcSnapshotTransferReadExecutionError<E>> {
56    let plan = stage.plan();
57    let authority = plan.attempt_authority(operation_sequence)?;
58    payload.validate_binding(&authority)?;
59    let mut journal = AttemptJournalGuard::open(stage.layout()?, &authority)?;
60    journal.reserve_planned_mutation(&plan.digest())?;
61    let request =
62        IcSnapshotTransferReadRequest::new(plan, operation_sequence, journal.record()?, payload)?;
63    let response = super::reserved_effect::submit_once(
64        &journal,
65        &request,
66        async |request| {
67            admit(request)
68                .await
69                .map_err(IcSnapshotTransferReadExecutionError::Admission)
70        },
71        || {
72            stage
73                .layout()
74                .map(|_| ())
75                .map_err(IcSnapshotTransferReadExecutionError::Stage)
76        },
77        async |request, record| {
78            provider
79                .read_snapshot(request, record)
80                .await
81                .map_err(IcSnapshotTransferReadExecutionError::Provider)
82        },
83        IcSnapshotTransferReadExecutionError::Journal,
84    )
85    .await?;
86    if let Err(error) = super::reserved_effect::check_returned(
87        stage,
88        &journal,
89        &request,
90        &response,
91        |request, record, reply| validate_response(request, record, reply).map(|_| ()),
92    ) {
93        return Err(match error {
94            super::reserved_effect::ReturnedError::Journal(source) => {
95                IcSnapshotTransferReadExecutionError::AfterReplyJournal {
96                    source,
97                    response: Box::new(response),
98                }
99            }
100            super::reserved_effect::ReturnedError::Association(source) => {
101                IcSnapshotTransferReadExecutionError::Association {
102                    source,
103                    response: Box::new(response),
104                }
105            }
106            super::reserved_effect::ReturnedError::Stage(source) => {
107                IcSnapshotTransferReadExecutionError::AfterReplyStage {
108                    source,
109                    response: Box::new(response),
110                }
111            }
112        });
113    }
114    Ok(response)
115}
116
117/// Typed step failures retain original spending; returned raw replies remain available.
118#[derive(Debug, Error)]
119pub enum IcSnapshotTransferReadExecutionError<E: std::error::Error + 'static> {
120    /// The original operation cannot be derived.
121    #[error(transparent)]
122    Plan(#[from] OperationPlanError),
123    /// Retained workflow, stage or ancestors changed before dispatch.
124    #[error(transparent)]
125    Stage(#[from] ExecutionWorkflowPersistenceError),
126    /// Original journal admission or durable reservation failed.
127    #[error(transparent)]
128    Journal(#[from] AttemptJournalError),
129    /// Complete original progress, prerequisites or reservation failed.
130    #[error(transparent)]
131    Progress(#[from] ExecutionProgressPersistenceError),
132    /// Exact original payload/reservation admission failed.
133    #[error(transparent)]
134    Request(#[from] IcSnapshotTransferReadError),
135    /// Integration-owned fresh admission failed after durable reservation.
136    #[error("fresh snapshot read admission failed: {0}")]
137    Admission(#[source] E),
138    /// The single provider call failed; its reservation stays pending.
139    #[error(transparent)]
140    Provider(#[from] IcObservationProviderError),
141    /// Passive association rejected a bounded returned reply.
142    #[error("snapshot read response association failed: {source}")]
143    Association {
144        /// Canonical structural rejection.
145        source: IcSnapshotTransferReadAssociationError,
146        /// Exact bounded returned response, without authentication or outcome.
147        response: Box<IcSnapshotTransferReadResponse>,
148    },
149    /// Journal re-admission failed after a reply; retain the reply.
150    #[error("snapshot read journal changed after reply: {source}")]
151    AfterReplyJournal {
152        /// Original journal rejection.
153        source: AttemptJournalError,
154        /// Exact bounded returned response.
155        response: Box<IcSnapshotTransferReadResponse>,
156    },
157    /// Stage/ancestor re-admission failed after a reply; retain the reply.
158    #[error("snapshot read stage changed after reply: {source}")]
159    AfterReplyStage {
160        /// Original stage or ancestor rejection.
161        source: ExecutionWorkflowPersistenceError,
162        /// Exact bounded returned response.
163        response: Box<IcSnapshotTransferReadResponse>,
164    },
165}
166
167#[cfg(all(test, unix))]
168mod tests;