hyperactor/checkpoint.rs
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! Checkpoint functionality for various objects to save and load states.
10
11use std::fmt::Debug;
12
13use async_trait::async_trait;
14
15use crate::RemoteMessage;
16use crate::mailbox::log::SeqId;
17
18/// Errors that occur during checkpoint operations.
19/// This enum is marked non-exhaustive to allow for extensibility.
20#[derive(thiserror::Error, Debug)]
21#[non_exhaustive]
22pub enum CheckpointError {
23 /// An error occured during saving checkpoints.
24 #[error("save")]
25 Save(#[source] anyhow::Error),
26
27 /// An error occured during loading checkpoints.
28 #[error("load: {0}")]
29 Load(SeqId, #[source] anyhow::Error),
30}
31
32/// [`Checkpoint`] is used to save the state of an instance so that it can be restored later.
33#[async_trait]
34pub trait Checkpointable: Send + Sync + Sized {
35 /// The type of the state that is saved. The state can be serialized and deserialized
36 /// from persistent storage.
37 type State: RemoteMessage;
38
39 /// Saves the current state.
40 async fn save(&self) -> Result<Self::State, CheckpointError>;
41
42 /// Loads the a state to restore the instance.
43 async fn load(state: Self::State) -> Result<Self, CheckpointError>;
44}