use crate::{
model::{
ic_mutation::{IcMutationAcknowledgement, IcMutationRequest, IcMutationRequestError},
ic_request::{IcManagementMethodRecord, IcManagementRequestRecord},
operation_plan::OperationPlanError,
},
ops::persistence::{
AttemptJournalError, ExecutionProgressPersistenceError, ExecutionStageGuard,
ExecutionWorkflowPersistenceError,
},
policy::ic_mutation::{IcMutationAssociationError, validate_acknowledgement},
ports::ic_mutation::{IcMutationProvider, IcMutationProviderError},
};
use thiserror::Error;
pub async fn capture_snapshot<E: std::error::Error + 'static>(
stage: &ExecutionStageGuard<'_>,
operation_sequence: u64,
payload: &IcManagementRequestRecord,
provider: &mut impl IcMutationProvider,
admit: impl AsyncFnOnce(&IcMutationRequest<'_>) -> Result<(), E>,
) -> Result<IcMutationAcknowledgement, IcSnapshotCaptureExecutionError<E>> {
if payload.method() != IcManagementMethodRecord::TakeCanisterSnapshot {
return Err(IcSnapshotCaptureExecutionError::CaptureRequired);
}
let plan = stage.plan();
let authority = plan.attempt_authority(operation_sequence)?;
let (acknowledgement, admission) = super::ic_mutation::submit_with(
stage,
&authority,
payload,
provider,
async |request| {
admit(request)
.await
.map_err(IcSnapshotCaptureExecutionError::Admission)?;
stage.layout()?;
Ok::<_, IcSnapshotCaptureExecutionError<E>>(())
},
async |journal, request, acknowledgement| {
validate_acknowledgement(request, journal.record()?, acknowledgement)?;
stage.layout()?;
Ok::<_, IcSnapshotCaptureReplyError>(())
},
)
.await?;
let admitted = admission;
super::qualified_receipt::retain_reply(acknowledgement, admitted, |source, acknowledgement| {
IcSnapshotCaptureExecutionError::AfterReply {
source,
acknowledgement,
}
})
.map(|(reply, ())| reply)
}
#[derive(Debug, Error)]
pub enum IcSnapshotCaptureExecutionError<E: std::error::Error + 'static> {
#[error("snapshot capture requires take_canister_snapshot")]
CaptureRequired,
#[error(transparent)]
Plan(#[from] OperationPlanError),
#[error(transparent)]
Stage(#[from] ExecutionWorkflowPersistenceError),
#[error(transparent)]
Journal(#[from] AttemptJournalError),
#[error(transparent)]
Progress(#[from] ExecutionProgressPersistenceError),
#[error(transparent)]
Request(#[from] IcMutationRequestError),
#[error("fresh snapshot capture admission failed: {0}")]
Admission(#[source] E),
#[error(transparent)]
Provider(#[from] IcMutationProviderError),
#[error("snapshot capture reply rejected: {source}")]
AfterReply {
source: IcSnapshotCaptureReplyError,
acknowledgement: Box<IcMutationAcknowledgement>,
},
}
#[derive(Debug, Error)]
pub enum IcSnapshotCaptureReplyError {
#[error(transparent)]
Journal(#[from] AttemptJournalError),
#[error(transparent)]
Association(#[from] IcMutationAssociationError),
#[error(transparent)]
Stage(#[from] ExecutionWorkflowPersistenceError),
}
#[cfg(all(test, unix))]
mod tests;