Description
As part of the work done to remove support for versionize, we realized that we could improve the API of the snapshot module.
The pattern that would like to follow is as follows:
#[derive(Serialize, Deserialize)]
pub struct SnapshotHeader {
magic: u64,
version: Version
}
impl SnapshotHeader {
fn load<R: Read>(reader: &mut R) -> Result<Self> { ... }
fn store<W: Write>(writer: &mut W) -> Result<Self> { ... }
}
#[derive(Serialize, Deserialize)]
pub struct Snapshot<Data> {
header: SnapshotHeader,
data: Data
}
impl<Data: Deserialize> Snapshot<Data> {
fn load_unchecked<R: Read>(reader: &mut R) -> Result<Self> { ... }
fn load<R: Read>(reader: &mut R) -> Result<Self> { ... }
}
impl<Data: Serialize> Snapshot<Data> {
fn save<W: Write>(&self, writer: &mut W) -> Result<usize> { ... }
fn save_with_crc<W: Write>(&self, writer: &mut W) -> Result<usize> { ... }
}
Checklist:
- Modify the API of the module to fit the above pattern.Modify the code of Firecracker making use of the new API.All the existing snapshot testing pass.
Activity
beamandala commentedon Mar 27, 2024
Hi, can I work on this issue?
zulinx86 commentedon Mar 27, 2024
Hello @beamandala 👋 Yes, feel free to work on this issue! Thank you so much for your interest in this issue!
beamandala commentedon Mar 27, 2024
@zulinx86 I've refactored the snapshot module code based on the pattern provided and wanted to check in with you to make sure I'm heading in the right direction. Let me know if there's anything I need to change or if everything looks good.
zulinx86 commentedon Mar 28, 2024
@roypat @bchalios Could you please check if the above code looks legit to you?
ShadowCurse commentedon Apr 1, 2024
I have been looking through our usage of snapshots and I think we can remove
Write
/Read
traits and use plain old&[u8]
forload
and just return aVec<u8>
forsave
. This will simplify loading/saving logic, because we will be working with known types and will remove need forCRC64Reader
/CRC64Writer
Also will not need asnapshot_len
parameter forload
as we will be able to pass a slice with known size.Additionally if we play smart, we can avoid doing memcopy of a snapshot data during the loading stage (the
crc_reader.read_exact(&mut snapshot)
part) if wemmap
the file so it can be presented as&[u8]
.roypat commentedon Apr 2, 2024
Hi @beamandala,
sorry for the late response, it was holiday season where our team is based. The general structure of what you posted above looks pretty much exactly what I had in mind. The only two comments I have right now is that the check for the magic value of the header should probably be moved into
SnapshoHdr::deserialize
, together with the version check. Please also feel free to open a draft PR is you want some more early feedback! :)I also think @ShadowCurse's comment about specializing the generics to
&[u8]
andVec<u8>
is valid, although we can also keep that as a separate issue/PR (I'm also not 100% convinced on the "presentingmmap
as&[u8]
", that sounds like a slippery slope to undefined behavior)ShadowCurse commentedon Apr 2, 2024
@roypat The
mmap
thing is basically:file.metadata().size()
)mmap
the file with correct lengthThe way we can keep it within Rust safety is to create a wrapper that can be created from
&File
so it will live as long as file itself. Snippet of potential solution:beamandala commentedon Jul 20, 2024
Hey @zulinx86 @roypat @ShadowCurse, I opened up #4691 as a draft PR. Any feedback on it would be great.
I apologize for the inactivity but I'll actively work on it now. Thanks!
roypat commentedon Jul 24, 2024
Hi @beamandala,
I see that #4691 is still in draft. Is there anything specific you'd like feedback on or have questions about?
roypat commentedon Aug 15, 2024
Hi @beamandala,
friendly ping on the above :)
beamandala commentedon Aug 15, 2024
Sorry, I missed your previous comment. My two questions are whether the snapshot module I rewrote matches what you were expecting. I also refactored some of the existing snapshot code and wanted to make sure those changes looked good so I can continue doing it for the other places where snapshot is used.
7 remaining items