-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds a new feature "persist". Using this feature, the upper layer can save and restore the state of the struct (for example, `Vfs` and `PseudoFs`) which implement the `crate::api::persist::Snapshotter` trait. This feature introduces a new trait `create::api::persist::Snapshotter`, which has two methods: - `fn save_to_bytes(&self) -> Result<Vec<u8>>` which saves the state of the struct to a byte array - ` fn load_from_bytes(constructor_args: Self::ConstructorArgs, buf: &mut Vec<u8>) -> Result<Self>` which restores the state of the struct from a byte array The `Snapshotter` trait uses [the `Snapshot` crate](https://github.com/firecracker-microvm/firecracker/tree/main/src/snapshot) to serialize and deserialize the struct data. Therefore, the struct which implement the `Snapshotter` trait must implement the `snapshot::Persist` trait and implement the `create::api::persist::VersionManager` trait to define it's versions. Signed-off-by: Nan Li <[email protected]>
- Loading branch information
Showing
5 changed files
with
632 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
use std::{ | ||
any::TypeId, | ||
collections::HashMap, | ||
fmt::Debug, | ||
io::{Error as IoError, ErrorKind, Result}, | ||
}; | ||
|
||
use snapshot::{Persist, Snapshot}; | ||
use versionize::{VersionMap, Versionize}; | ||
|
||
/// A list of versions. | ||
type Versions = Vec<HashMap<TypeId, u16>>; | ||
|
||
/// Version Manager trait | ||
pub trait VersionManger { | ||
/// Returns a list of versions. | ||
fn get_versions() -> Versions; | ||
|
||
/// Returns a `VersionMap` with the versions defined by `get_versions`. | ||
fn new_version_map() -> VersionMap { | ||
let mut version_map = VersionMap::new(); | ||
for (idx, map) in Self::get_versions().into_iter().enumerate() { | ||
if idx > 0 { | ||
version_map.new_version(); | ||
} | ||
for (type_id, version) in map { | ||
version_map.set_type_version(type_id, version); | ||
} | ||
} | ||
version_map | ||
} | ||
|
||
/// Returns a new `Snapshot` with the versions defined by `get_versions`. | ||
fn new_snapshot() -> Snapshot { | ||
let vm = Self::new_version_map(); | ||
let target_version = vm.latest_version(); | ||
Snapshot::new(vm, target_version) | ||
} | ||
} | ||
|
||
/// Snapshotter trait | ||
pub trait Snapshotter<'a>: Persist<'a> | ||
where | ||
Self::State: Versionize + VersionManger, | ||
Self::Error: Debug, | ||
{ | ||
/// Serializes `self` to a byte array. | ||
fn save_to_bytes(&self) -> Result<Vec<u8>> { | ||
let state = self.save(); | ||
let mut buf = Vec::new(); | ||
let mut snapshot = Self::State::new_snapshot(); | ||
snapshot.save(&mut buf, &state).map_err(|e| { | ||
IoError::new( | ||
ErrorKind::Other, | ||
format!("Failed to save snapshot: {:?}", e), | ||
) | ||
})?; | ||
|
||
Ok(buf) | ||
} | ||
|
||
/// Restores `self` from a byte array. | ||
fn load_from_bytes(constructor_args: Self::ConstructorArgs, buf: &mut Vec<u8>) -> Result<Self> { | ||
let state: Self::State = Snapshot::load( | ||
&mut buf.as_slice(), | ||
buf.len(), | ||
Self::State::new_version_map(), | ||
) | ||
.map_err(|e| { | ||
IoError::new( | ||
ErrorKind::Other, | ||
format!("Failed to load snapshot: {:?}", e), | ||
) | ||
})?; | ||
let restored_self = Self::restore(constructor_args, &state).map_err(|e| { | ||
IoError::new( | ||
ErrorKind::Other, | ||
format!("Failed to restore snapshot: {:?}", e), | ||
) | ||
})?; | ||
|
||
Ok(restored_self) | ||
} | ||
} | ||
|
||
impl<'a, T: Persist<'a>> Snapshotter<'a> for T | ||
where | ||
T::State: Versionize + VersionManger, | ||
T::Error: Debug, | ||
{ | ||
} | ||
|
||
mod test { | ||
use std::collections::HashMap; | ||
|
||
use snapshot::Persist; | ||
use versionize::{VersionMap, Versionize, VersionizeResult}; | ||
use versionize_derive::Versionize; | ||
|
||
use super::VersionManger; | ||
|
||
#[derive(Debug, PartialEq)] | ||
struct Test { | ||
a: u32, | ||
b: u32, | ||
} | ||
|
||
#[derive(Debug, Versionize)] | ||
struct TestState { | ||
a: u32, | ||
b: u32, | ||
} | ||
|
||
impl VersionManger for TestState { | ||
fn get_versions() -> super::Versions { | ||
vec![HashMap::from([(std::any::TypeId::of::<u32>(), 1)])] | ||
} | ||
} | ||
|
||
impl Persist<'_> for Test { | ||
type State = TestState; | ||
|
||
type ConstructorArgs = (); | ||
|
||
type Error = std::io::Error; | ||
|
||
fn save(&self) -> Self::State { | ||
TestState { | ||
a: self.a, | ||
b: self.b, | ||
} | ||
} | ||
|
||
fn restore( | ||
_constructor_args: Self::ConstructorArgs, | ||
state: &Self::State, | ||
) -> std::result::Result<Self, Self::Error> { | ||
Ok(Test { | ||
a: state.a, | ||
b: state.b, | ||
}) | ||
} | ||
} | ||
|
||
#[test] | ||
fn save_load_test() { | ||
use crate::api::persist::Snapshotter; | ||
|
||
let t = Test { a: 1u32, b: 4u32 }; | ||
|
||
// save | ||
let mut buf = t.save_to_bytes().unwrap(); | ||
|
||
// restore | ||
let restored_t = Test::load_from_bytes((), &mut buf).unwrap(); | ||
|
||
// assert | ||
assert_eq!(t, restored_t); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.