Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASI journal and stateful persistence #4263

Merged
merged 137 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
137 commits
Select commit Hold shift + click to select a range
6d407eb
Reduced verbosity on a warning
john-sharratt Oct 18, 2023
523add3
Added the draft API and CLI changes needed for the snapshot functiona…
john-sharratt Oct 20, 2023
6940ba8
Added more snapshot trigger types
john-sharratt Oct 20, 2023
6d04848
Puts the snapshot functionality behind a feature flag
john-sharratt Oct 20, 2023
c1872fd
Added an example on how certain events will be loggin to the snap sho…
john-sharratt Oct 20, 2023
ec74b83
Added the basics of the snapshot mechanism
john-sharratt Oct 22, 2023
8f11ac7
Addressed comments and added a basic doc
john-sharratt Oct 22, 2023
99ecf2b
Added compactor and filter snapshot capturer
john-sharratt Oct 23, 2023
2a9e147
Added missing snapshot-on type
john-sharratt Oct 23, 2023
85b12b1
Added an init log entry which has the WASM hash
john-sharratt Oct 23, 2023
1fe4303
Added the remaining data structures for the snapshot journal
john-sharratt Oct 23, 2023
ebfe824
Cleaned up type complexity using WasiResult
john-sharratt Oct 24, 2023
34f233b
Wired up the snapshot triggers
john-sharratt Oct 24, 2023
b829407
Runtime will now snapshot when the STDIN trigger is used
john-sharratt Oct 24, 2023
19c3946
Moved the snapshot trigger list from the process to WasiEnv which is …
john-sharratt Oct 24, 2023
31c4a61
Refactored the snapshot feature toggle
john-sharratt Oct 24, 2023
24368b5
More wiring of the snapshot options and the intercepts
john-sharratt Oct 24, 2023
e299170
Added a snapshot trigger on reading the environment variables
john-sharratt Oct 24, 2023
63980d8
Minor document updates
john-sharratt Oct 24, 2023
ffeeb33
Process will now properly terminate when snapshots fail
john-sharratt Oct 24, 2023
34fa6ea
Process will now properly terminate when snapshots fail
john-sharratt Oct 24, 2023
485176b
Simplified the checkpoint loop
john-sharratt Oct 24, 2023
6e557ba
The runtime now properly saves the journal events to a snapshot file
john-sharratt Oct 25, 2023
c3f0e52
Now currently saving the snapshot state
john-sharratt Oct 25, 2023
27dcb1d
Now currently saving the snapshot state
john-sharratt Oct 25, 2023
a99b840
Now currently saving the snapshot state
john-sharratt Oct 25, 2023
e320aee
The rewinding is now fixed again for snapshots
john-sharratt Oct 25, 2023
46aa873
Finished a basic version of the snapshot restoration
john-sharratt Oct 25, 2023
c519022
Added more snapshot events for the file system
john-sharratt Nov 10, 2023
3efa30e
Added intercept and replay calls for all the major file system operat…
john-sharratt Nov 11, 2023
7cef370
Merge remote-tracking branch 'origin/master' into dcgi
john-sharratt Nov 11, 2023
397daca
Merge remote-tracking branch 'origin/master' into dcgi
john-sharratt Nov 18, 2023
f167eb9
cargo fmt
john-sharratt Nov 19, 2023
7d023ea
Fixed compile errors without the snapshot feature
john-sharratt Nov 19, 2023
1223d37
Fixed compile errors without the snapshot feature
john-sharratt Nov 19, 2023
4aa1ec5
More feature toggle fixes
john-sharratt Nov 19, 2023
ccc4c29
Refactored the snapshot to journal to make it clearer how it works
john-sharratt Nov 19, 2023
4906460
Updated the journal documentation file
john-sharratt Nov 19, 2023
00fc6f6
More wiring of the journal snapshot triggers
john-sharratt Nov 20, 2023
8337e72
Clippy fixes
john-sharratt Nov 20, 2023
687667d
Fixed the JSC compile errors
john-sharratt Nov 20, 2023
897b8d7
Added a panic journal event
john-sharratt Nov 20, 2023
2f4baeb
More clippy fixes
john-sharratt Nov 20, 2023
9cd4c44
More cleanup of the CLI and naming
john-sharratt Nov 20, 2023
305fc4c
Renamed the journaling submodule
john-sharratt Nov 20, 2023
ffad63c
Update of gramma
john-sharratt Nov 20, 2023
cbeff30
Fix for a linting issue
john-sharratt Nov 20, 2023
e36b898
Added an event for process exit and a module hash to validate the sam…
john-sharratt Nov 20, 2023
85d4b6d
Fixed some linting issues
john-sharratt Nov 20, 2023
0dda34c
Fixed some unit tests
john-sharratt Nov 20, 2023
b539572
Fixed a clippy error
john-sharratt Nov 20, 2023
e80ddb4
Fix for some unit tests
john-sharratt Nov 20, 2023
e6e3478
Made the run commands backwards compatible
john-sharratt Nov 20, 2023
a4bc071
Undid the example refactor
john-sharratt Nov 20, 2023
8a9d5a8
Added events for ports and sockets
john-sharratt Nov 20, 2023
61c6825
Split the port and socket syscalls so they can be invokes seperately
john-sharratt Nov 20, 2023
e53e11c
Added the socket syscall hooks
john-sharratt Nov 21, 2023
8af77a2
Finished off the sockets for journaling
john-sharratt Nov 21, 2023
37d6c57
Fixed some linting issues
john-sharratt Nov 21, 2023
74c1d31
Fixed a compile issue
john-sharratt Nov 21, 2023
68604f1
More linting fixes
john-sharratt Nov 21, 2023
f852722
Fixed a regression issue on the socket binding
john-sharratt Nov 21, 2023
00a8873
The journal entries can now support zero copy operations
john-sharratt Nov 21, 2023
a57f30d
Switched to rkyv for the serialization of the journal so that reads a…
john-sharratt Nov 21, 2023
cbfd167
Added unit tests for the journal and fixed the rkyv serializer
john-sharratt Nov 22, 2023
41f4a92
cargo update
john-sharratt Nov 22, 2023
09efd35
Added the compacting journal with some basic test cases
john-sharratt Nov 22, 2023
c73b5a8
Fixed some linting issues
john-sharratt Nov 22, 2023
d4fdbc5
Another fix for the linting
john-sharratt Nov 22, 2023
787f9e9
More linting fixes and additional tests
john-sharratt Nov 22, 2023
e4fc594
Finished the compactor and added CLI
john-sharratt Nov 23, 2023
abe6691
Merge remote-tracking branch 'origin/master' into dcgi
john-sharratt Nov 23, 2023
0f1a9fa
Added a couple more unit tests
john-sharratt Nov 23, 2023
38d74be
Some linting and compile fixes
john-sharratt Nov 23, 2023
0d728c3
Fix for lints
john-sharratt Nov 23, 2023
7875068
Another linting fix
john-sharratt Nov 23, 2023
bce86dd
Added better error handling that reduces a panic
john-sharratt Nov 23, 2023
b6eb4f6
Fixed a runtime in runtime bug on journal replays
john-sharratt Nov 23, 2023
4f6a193
Missing rewinds no long cause the process to fail
john-sharratt Nov 23, 2023
74edba8
Added additional method for rewinding
john-sharratt Nov 23, 2023
d1d5b8e
Removed the journals parameter from the API
john-sharratt Nov 23, 2023
cc95b87
Fixed the process exit record replay
john-sharratt Nov 23, 2023
10e9c02
Fixed a bug where the journals were not being returned by the runtime
john-sharratt Nov 23, 2023
cc97e1b
Fixed the bootstrapping process for one of the execution paths
john-sharratt Nov 23, 2023
0320f04
Added another bootstrap and cleaned up the an interface
john-sharratt Nov 23, 2023
36cb053
The journals are now properly being restored
john-sharratt Nov 23, 2023
953a0af
Fixed the linting and compile error
john-sharratt Nov 23, 2023
4baa50a
Added safety to the journal restore code
john-sharratt Nov 23, 2023
fc19f5f
Linting fixes
john-sharratt Nov 23, 2023
4abc215
More compile fixes
john-sharratt Nov 24, 2023
52ee3ea
Module mismatch is now a warning
john-sharratt Nov 24, 2023
ec617db
We now restore the TTY state after the program executes
john-sharratt Nov 26, 2023
1fea8d7
Fixed an issue where the restoration of the journal was not properly …
john-sharratt Nov 26, 2023
489f82c
Fixed a linting issue
john-sharratt Nov 26, 2023
3eaabd9
The TTY is now delayed during the journal bootstrap process
john-sharratt Nov 26, 2023
e1c2570
Fixed another linting issue
john-sharratt Nov 26, 2023
88c5d8c
Fixed the padding on the change directory event
john-sharratt Nov 26, 2023
46f1503
Removed the pop journals call which is not needed
john-sharratt Nov 26, 2023
f62f568
Fixed a bug where the offsets were not updating properly on journal w…
john-sharratt Nov 27, 2023
6ab87f9
Fixed a bug where the module hash plumbing was not correct
john-sharratt Nov 27, 2023
fd4f55e
Implemented fixes so the journal restoration works properly
john-sharratt Nov 27, 2023
69bcc61
The stdout and stderr are no longer restored if the process exits
john-sharratt Nov 27, 2023
d494348
Changes made after review with Christoph plus added compression of jo…
john-sharratt Nov 27, 2023
a41c996
Added some linting fixes and fix for compile erors
john-sharratt Nov 27, 2023
7d79610
Merge remote-tracking branch 'origin/master' into dcgi
john-sharratt Dec 20, 2023
dd74d08
Added the basics of a DCGI runner
john-sharratt Dec 21, 2023
aba3c65
Finished off the basics of DCGI
john-sharratt Dec 21, 2023
355fcfa
Now opening the preopens when the WASM environment is reinitialized
john-sharratt Dec 21, 2023
a06789d
Fixed an issue where the callbacks for DCGI were not be wired properly
john-sharratt Dec 21, 2023
8b9b8f6
Now properly propogating the errors to the caller
john-sharratt Dec 21, 2023
9e3f3c0
Now reusing DCGI instances but not the memory associated with them
john-sharratt Dec 22, 2023
ffb45ea
DCGI can not reuse memory for now due to several restrictions
john-sharratt Dec 22, 2023
25a4824
Added a fix so that stderr is properly returned on DCGI calls
john-sharratt Dec 22, 2023
0174592
Merge remote-tracking branch 'origin/master' into dcgi
john-sharratt Dec 23, 2023
e732ff8
Fixes after the latest merge
john-sharratt Dec 23, 2023
9caf4bf
Lint fixes and compile fixes
john-sharratt Dec 23, 2023
472d918
More linting and compile fixes
john-sharratt Dec 23, 2023
79f4d26
Added support for journals to DCGI and removed sharding
john-sharratt Dec 23, 2023
b4131b0
Fixed the journal wiring to DCGI
john-sharratt Dec 23, 2023
ca401d8
Compacting a journal will now remove more redundant file system event…
john-sharratt Dec 23, 2023
515aa16
Fixed an issue where the compacting logs were not rotating and compac…
john-sharratt Dec 23, 2023
803529a
Linting and compile fixes
john-sharratt Dec 23, 2023
b2b528e
Added more unit test fixes, linting and compile errors
john-sharratt Dec 23, 2023
0a32ca7
Merge remote-tracking branch 'origin/master' into dcgi
john-sharratt Dec 23, 2023
4bd2b9d
Linting fix
john-sharratt Dec 23, 2023
9bb6f18
Fixed a race condition in the virtual-net unit tests
john-sharratt Dec 23, 2023
4420e61
Excluding tokio from the docs build
john-sharratt Dec 23, 2023
1d5e5ec
Merge branch 'master' into dcgi
john-sharratt Jan 3, 2024
eb96a9b
Added comments on the JournalEffector
john-sharratt Jan 3, 2024
fb6d4d6
Split out the config responsibility from the FilteredJournal
john-sharratt Jan 3, 2024
58a8941
Added a comment on the excluding of tokio from the docs build
john-sharratt Jan 3, 2024
7c1b68d
Moved most of the journal functionality out into its own crate
john-sharratt Jan 3, 2024
21a23ef
Fixed some linting and compile issues from last refactor
john-sharratt Jan 4, 2024
77bb3cc
More linting fixes
john-sharratt Jan 4, 2024
d80d469
No longer re-exporting standard types in virtual-net
john-sharratt Jan 4, 2024
b6ef7dd
Removed the wasix and virtual-net dependencies from the archived events
john-sharratt Jan 4, 2024
4a22ad5
Merge remote-tracking branch 'origin/master' into dcgi
john-sharratt Jan 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 138 additions & 142 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ default = [
"wat",
"wast",
"compiler",
"snapshooter",
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved
"wasmer-artifact-create",
"static-artifact-create",
]
snapshooter = ["wasmer-wasix/snapshooter"]
backend = []
coredump = ["wasm-coredump-builder"]
sys = ["compiler", "wasmer-vm"]
Expand Down
76 changes: 76 additions & 0 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
collections::{BTreeSet, HashMap},
path::{Path, PathBuf},
str::FromStr,
sync::{mpsc::Sender, Arc},
time::Duration,
};
Expand Down Expand Up @@ -33,6 +34,7 @@ use wasmer_wasix::{
VirtualTaskManagerExt,
},
},
snapshot,
types::__WASI_STDIN_FILENO,
wasmer_wasix_types::wasi::Errno,
PluggableRuntime, RewindState, Runtime, WasiEnv, WasiEnvBuilder, WasiError, WasiFunctionEnv,
Expand Down Expand Up @@ -105,6 +107,34 @@ pub struct Wasi {
#[clap(long = "enable-async-threads")]
pub enable_async_threads: bool,

/// Specifies the snapshot file that Wasmer will use to store
/// the state of the WASM process so that it can be later restored
#[cfg(feature = "snapshooter")]
#[clap(long = "snapshot-to")]
pub snapshot_to: Option<PathBuf>,

/// Indicates what events will cause a snapshot to be taken
/// and written to the snapshot file.
///
/// If not specified, the default is to snapshot on idle plus if a
/// snapshot period is provided it will also default to periodic snapshots
/// as well.
#[cfg(feature = "snapshooter")]
#[clap(long = "snapshot-on")]
pub snapshot_on: Vec<SnapshotTrigger>,

/// Time in seconds between taking snapshots of the process and dumping
/// them to the snapshot file.
#[cfg(feature = "snapshooter")]
#[clap(long = "snapshot-period")]
pub snapshot_period: Option<u64>,
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved

/// When specified, the runtime will restore a previous snapshot
/// using the supplied file.
#[cfg(feature = "snapshooter")]
#[clap(long = "resume-from")]
pub resume_from: Option<PathBuf>,
theduke marked this conversation as resolved.
Show resolved Hide resolved

/// Allow instances to send http requests.
///
/// Access to domains is granted by default.
Expand All @@ -116,6 +146,47 @@ pub struct Wasi {
pub deny_multiple_wasi_versions: bool,
}

/// Various triggers that will cause the runtime to take snapshot
/// of the WASM state and store it in the snapshot file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SnapshotTrigger {
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved
/// Triggered when all the threads in the process goes idle
Idle,
/// Triggered when a listen syscall is invoked on a socket
Listen,
/// Triggered when the process reads stdin for the first time
Stdin,
/// Triggered periodically (default 10 seconds) which can be specified using the `snapshot-period` option
Periodic,
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved
/// Issued if the user sends an interrupt signal (Ctrl + C).
Sigint,
/// Alarm clock signal (used for timers)
Sigalrm,
/// The SIGTSTP signal is sent to a process by its controlling terminal to request it to stop temporarily. It is commonly initiated by the user pressing Ctrl-Z.
Sigtstp,
/// The SIGSTOP signal instructs the operating system to stop a process for later resumption.
Sigstop,
}

impl FromStr for SnapshotTrigger {
type Err = anyhow::Error;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let s = s.trim().to_lowercase();
Ok(match s.as_str() {
"idle" => Self::Idle,
"listen" => Self::Listen,
"stdin" => Self::Stdin,
"periodic" => Self::Periodic,
"intr" | "sigint" | "ctrlc" | "ctrl-c" => Self::Sigint,
"alarm" | "timer" | "sigalrm" => Self::Sigalrm,
"sigtstp" | "ctrlz" | "ctrl-z" => Self::Sigtstp,
"stop" | "sigstop" => Self::Sigstop,
a => return Err(anyhow::format_err!("invalid or unknown trigger ({a})")),
})
}
}

pub struct RunProperties {
pub ctx: WasiFunctionEnv,
pub path: PathBuf,
Expand Down Expand Up @@ -267,6 +338,11 @@ impl Wasi {
rt.set_networking_implementation(virtual_net::UnsupportedVirtualNetworking::default());
}

#[cfg(feature = "snapshooter")]
if let Some(path) = &self.resume_from {
rt.set_snapshooter(Arc::new(snapshot::LogFileSnapShooter::new_std(path)?));
}

if !self.no_tty {
let tty = Arc::new(SysTty::default());
tty.reset();
Expand Down
1 change: 1 addition & 0 deletions lib/wasix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ sys = ["webc/mmap", "time", "virtual-mio/sys"]
sys-default = ["sys", "logging", "host-fs", "sys-poll", "sys-thread", "host-vnet", "host-threads", "host-reqwest"]
sys-poll = []
sys-thread = ["tokio/rt", "tokio/time", "tokio/rt-multi-thread", "rayon"]
snapshooter = []

# Deprecated. Kept it for compatibility
compiler = []
Expand Down
2 changes: 2 additions & 0 deletions lib/wasix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub mod http;
mod rewind;
pub mod runners;
pub mod runtime;
#[cfg(feature = "snapshooter")]
pub mod snapshot;
mod state;
mod syscalls;
mod utils;
Expand Down
3 changes: 2 additions & 1 deletion lib/wasix/src/os/task/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
Expand All @@ -23,7 +24,7 @@ use super::{
};

/// Represents the ID of a WASI thread
#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct WasiThreadId(u32);

impl WasiThreadId {
Expand Down
26 changes: 25 additions & 1 deletion lib/wasix/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use self::{
module_cache::{CacheError, ModuleHash},
task_manager::InlineWaker,
};
use crate::snapshot::UNSUPPORTED_SNAP_SHOOTER;

use std::{
fmt,
Expand All @@ -19,6 +20,8 @@ use futures::future::BoxFuture;
use virtual_net::{DynVirtualNetworking, VirtualNetworking};
use wasmer::Module;

#[cfg(feature = "snapshooter")]
use crate::snapshot::{DynSnapShooter, UnsupportedSnapShooter};
use crate::{
http::{DynHttpClient, HttpClient},
os::TtyBridge,
Expand Down Expand Up @@ -90,7 +93,7 @@ where
}

/// Load a a Webassembly module, trying to use a pre-compiled version if possible.
fn load_module<'a>(&'a self, wasm: &'a [u8]) -> BoxFuture<'a, Result<Module, anyhow::Error>> {
fn load_module<'a>(&'a self, wasm: &'a [u8]) -> BoxFuture<'a, anyhow::Result<Module>> {
let engine = self.engine();
let module_cache = self.module_cache();

Expand All @@ -105,6 +108,13 @@ where
fn load_module_sync(&self, wasm: &[u8]) -> Result<Module, anyhow::Error> {
InlineWaker::block_on(self.load_module(wasm))
}

/// The snap shooter takes and restores snapshots of the WASM process at specific
/// points in time by reading and writing log entries
#[cfg(feature = "snapshooter")]
fn snap_shooter<'a>(&'a self) -> &'_ DynSnapShooter {
&UNSUPPORTED_SNAP_SHOOTER
}
}

/// Load a a Webassembly module, trying to use a pre-compiled version if possible.
Expand Down Expand Up @@ -180,6 +190,9 @@ pub struct PluggableRuntime {
pub module_cache: Arc<dyn ModuleCache + Send + Sync>,
#[derivative(Debug = "ignore")]
pub tty: Option<Arc<dyn TtyBridge + Send + Sync>>,
#[cfg(feature = "snapshooter")]
#[derivative(Debug = "ignore")]
pub snapshooter: Arc<DynSnapShooter>,
}

impl PluggableRuntime {
Expand Down Expand Up @@ -214,6 +227,7 @@ impl PluggableRuntime {
source: Arc::new(source),
package_loader: Arc::new(loader),
module_cache: Arc::new(module_cache::in_memory()),
snapshooter: Arc::new(UnsupportedSnapShooter::default()) as Arc<DynSnapShooter>,
}
}

Expand Down Expand Up @@ -263,6 +277,11 @@ impl PluggableRuntime {
self.http_client = Some(Arc::new(client));
self
}

pub fn set_snapshooter(&mut self, snapshooter: Arc<DynSnapShooter>) -> &mut Self {
self.snapshooter = snapshooter;
self
}
}

impl Runtime for PluggableRuntime {
Expand Down Expand Up @@ -308,4 +327,9 @@ impl Runtime for PluggableRuntime {
fn module_cache(&self) -> Arc<dyn ModuleCache + Send + Sync> {
self.module_cache.clone()
}

#[cfg(feature = "snapshooter")]
fn snap_shooter<'a>(&'a self) -> &DynSnapShooter {
self.snapshooter.as_ref()
}
}
Loading
Loading