diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d705813d02..57e32acc1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,6 +24,33 @@ jobs: - run: cargo test --verbose --workspace # No workspace member enables these features, so `--workspace` doesn't build the decoders. - run: cargo test --verbose -p nomt-core --features borsh,serde + torture: + name: NOMT - torture + runs-on: ubuntu-26.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@v5 + - uses: ./.github/actions/install-fuse + - uses: dtolnay/rust-toolchain@stable + - run: cargo build --release --locked -p torture + # Each workload commits, rolls back and grows the hash table, crashing the database in + # between, and checks every value and the root against a model. + # + # A seed fixes the configuration of a workload, but not the moment a crash lands. These + # seeds were picked for the features they turn on; none of them uses trickfs. + # 6: updates, commit crashes, hash table growth with crashes + # 7: deletions, commit crashes, rollbacks with crashes + # 8: reads, updates, deletions, overflow values, commit crashes, rollbacks, growth + - name: Run torture workloads + run: | + for seed in 6 7 8; do + ./target/release/torture run "$seed" \ + --assigned-disk $((4 << 30)) --assigned-memory $((2 << 30)) \ + --max-iterations 20 --ensure_snapshot --workdir "$RUNNER_TEMP" + done + - name: Print the log of the failed workload + if: failure() + run: tail -n 300 "$RUNNER_TEMP"/torture-*/log.txt benchtop_check: name: NOMT - check benchtop runs-on: ubuntu-26.04 diff --git a/Cargo.lock b/Cargo.lock index d5e4763e0d..3cf4f613d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1801,6 +1801,7 @@ dependencies = [ "libc", "nix 0.29.0", "nomt", + "nomt-core", "rand", "rand_distr", "rand_pcg", diff --git a/torture/Cargo.toml b/torture/Cargo.toml index e1c47bdfd5..574c3d6e39 100644 --- a/torture/Cargo.toml +++ b/torture/Cargo.toml @@ -10,6 +10,7 @@ anyhow.workspace = true cfg-if.workspace = true serde.workspace = true nomt = { path = "../nomt" } +nomt-core = { path = "../core" } tokio.workspace = true tokio-util.workspace = true tokio-stream.workspace = true diff --git a/torture/src/agent.rs b/torture/src/agent.rs index 2220d5bffd..5027574406 100644 --- a/torture/src/agent.rs +++ b/torture/src/agent.rs @@ -35,6 +35,7 @@ use crate::{ pub async fn run(input: UnixStream) -> Result<()> { let pid = std::process::id(); trace!(pid, "Child process started"); + disable_core_dumps(); let mut stream = Stream::new(input); let workdir = initialize(&mut stream).await?; @@ -186,6 +187,15 @@ pub async fn run(input: UnixStream) -> Result<()> { }) .await?; } + ToAgent::QueryRoot => { + let root = agent.query_root(); + stream + .send(Envelope { + reqno, + message: ToSupervisor::Root(root), + }) + .await?; + } ToAgent::QueryHashTableUtilization => { let utilization = agent.hash_table_utilization()?; stream @@ -223,6 +233,25 @@ pub async fn run(input: UnixStream) -> Result<()> { Ok(()) } +/// Make this process die without a core dump. +/// +/// The agent crashes on purpose, with `abort`. Where core dumps are collected (for example by +/// systemd-coredump), dumping an agent takes longer than the supervisor waits for it to die, +/// and the workload fails with "agent did not die". +fn disable_core_dumps() { + let no_core = libc::rlimit { + rlim_cur: 0, + rlim_max: 0, + }; + // A failure only brings back the slow crash, so it is not worth stopping the agent for. + if unsafe { libc::setrlimit(libc::RLIMIT_CORE, &no_core) } != 0 { + tracing::warn!( + "failed to disable core dumps: {}", + std::io::Error::last_os_error() + ); + } +} + /// Execute the provided `task` and make it crash after the specified `crash_delay`. async fn crash_task( task: impl Future + Send + 'static, @@ -339,6 +368,8 @@ impl Agent { o.path(workdir.join("nomt_db")); o.bitbox_seed(open_params.bitbox_seed); o.hashtable_buckets(open_params.hashtable_buckets); + o.commit_concurrency(open_params.commit_concurrency); + o.io_workers(open_params.io_workers); o.warm_up(open_params.warm_up); o.preallocate_ht(open_params.preallocate_ht); o.page_cache_size(open_params.page_cache_size); @@ -501,6 +532,12 @@ impl Agent { nomt.sync_seqn() } + fn query_root(&mut self) -> [u8; 32] { + // UNWRAP: `nomt` is always `Some` except recreation. + let nomt = self.nomt.as_ref().unwrap(); + nomt.root().into_inner() + } + fn hash_table_utilization(&mut self) -> Result { // UNWRAP: `nomt` is always `Some` except recreation. let nomt = self.nomt.as_ref().unwrap(); diff --git a/torture/src/message.rs b/torture/src/message.rs index 01fd06a132..8128a08c9f 100644 --- a/torture/src/message.rs +++ b/torture/src/message.rs @@ -167,6 +167,9 @@ pub enum ToAgent { /// The supervisor sends this message to the child process to query the current sequence number /// of the database. QuerySyncSeqn, + /// The supervisor sends this message to the child process to query the current root of the + /// database. + QueryRoot, /// The supervisor sends this message to the child process to query hash table utilization. QueryHashTableUtilization, /// The supervisor sends this message to the child process to indicate that the child should @@ -237,6 +240,8 @@ pub enum ToSupervisor { QueryValue(Option), /// The response to a query for the current sequence number of the database. SyncSeqn(u32), + /// The response to a query for the current root of the database. + Root([u8; 32]), /// The response to a query for hash table utilization. HashTableUtilizationResponse(HashTableUtilization), } diff --git a/torture/src/supervisor/cli.rs b/torture/src/supervisor/cli.rs index 3fb2de3cbd..0fd7957a8d 100644 --- a/torture/src/supervisor/cli.rs +++ b/torture/src/supervisor/cli.rs @@ -62,4 +62,10 @@ pub struct RunParams { /// This applies after every rollback. #[arg(long = "ensure_snapshot", default_value = "false")] pub ensure_snapshot: bool, + + /// Stop the workload after this many iterations. + /// + /// The number of iterations is otherwise derived from the assigned disk space alone. + #[arg(long)] + pub max_iterations: Option, } diff --git a/torture/src/supervisor/comms.rs b/torture/src/supervisor/comms.rs index a4ec44c783..2c75718dad 100644 --- a/torture/src/supervisor/comms.rs +++ b/torture/src/supervisor/comms.rs @@ -107,6 +107,17 @@ impl RequestResponse { } } + /// Requests the current root from the agent. + pub async fn send_query_root(&self) -> anyhow::Result<[u8; 32]> { + match self + .send_request(crate::message::ToAgent::QueryRoot) + .await? + { + crate::message::ToSupervisor::Root(root) => Ok(root), + resp => bail!("unexpected response: {:?}", resp), + } + } + /// Requests the current hash table utilization from the agent. pub async fn send_query_hash_table_utilization( &self, diff --git a/torture/src/supervisor/config.rs b/torture/src/supervisor/config.rs index b23982bd67..c293fe5107 100644 --- a/torture/src/supervisor/config.rs +++ b/torture/src/supervisor/config.rs @@ -295,6 +295,11 @@ impl WorkloadConfiguration { Self::new_inner(rng, avail_bytes).unwrap() } + /// Limit the number of iterations of the workload. + pub fn limit_iterations(&mut self, max_iterations: usize) { + self.iterations = self.iterations.min(max_iterations); + } + pub fn enable_ensure_snapshot(&mut self) { self.ensure_snapshot = true; } diff --git a/torture/src/supervisor/mod.rs b/torture/src/supervisor/mod.rs index 8f9b74a4ad..156fb7fccf 100644 --- a/torture/src/supervisor/mod.rs +++ b/torture/src/supervisor/mod.rs @@ -350,13 +350,22 @@ async fn run_single_workload( workload_dir, 0, /* workload_id */ run_params.ensure_snapshot, + run_params.max_iterations, run_params.assigned_disk, run_params.assigned_memory, ); + info!( + "Running workload, seed={}: {:#?}", + run_params.seed, + workload.config() + ); let maybe_flag = run_workload(cancel_token.clone(), run_params.seed, 0, workload).await?; if let Some(flag) = maybe_flag { print_flag(&flag); + // A scripted run, such as the one in CI, must see the failure in the exit code. + anyhow::bail!("workload failed, seed={}", run_params.seed); } + info!("Workload finished, seed={}", run_params.seed); Ok(()) } diff --git a/torture/src/supervisor/workload.rs b/torture/src/supervisor/workload.rs index d3e75e5f0d..7370e3d4ac 100644 --- a/torture/src/supervisor/workload.rs +++ b/torture/src/supervisor/workload.rs @@ -1,5 +1,7 @@ use anyhow::Result; use imbl::OrdMap; +use nomt::hasher::Blake3Hasher; +use nomt_core::hasher::ValueHasher as _; use rand::{distr::weighted::WeightedIndex, prelude::*}; use std::{ collections::HashSet, @@ -35,6 +37,8 @@ struct Snapshot { sync_seqn: u32, /// The state of the database at this snapshot. state: OrdMap<[u8; 32], Option>>, + /// The hash of every value present in `state`, kept to compute the expected root. + value_hashes: OrdMap<[u8; 32], [u8; 32]>, } impl Snapshot { @@ -42,8 +46,36 @@ impl Snapshot { Self { sync_seqn: 0, state: OrdMap::new(), + value_hashes: OrdMap::new(), } } + + /// Record a change of a key, `None` being a deletion. + fn set(&mut self, key: [u8; 32], value: Option>) { + match value { + Some(ref value) => { + self.value_hashes + .insert(key, Blake3Hasher::hash_value(value)); + } + None => { + self.value_hashes.remove(&key); + } + } + self.state.insert(key, value); + } + + /// The root of a trie that holds exactly the values of this snapshot. + /// + /// It is built from scratch, independently of how the database reached this state. + fn root(&self) -> [u8; 32] { + nomt_core::update::build_trie::( + 0, + self.value_hashes + .iter() + .map(|(key, value_hash)| (*key, *value_hash)), + |_| {}, + ) + } } enum Resources { @@ -182,6 +214,7 @@ impl Workload { workload_dir: TempDir, workload_id: u64, ensure_snapshot: bool, + max_iterations: Option, assigned_disk: u64, assigned_memory: u64, ) -> Self { @@ -192,6 +225,9 @@ impl Workload { if ensure_snapshot { config.enable_ensure_snapshot(); } + if let Some(max_iterations) = max_iterations { + config.limit_iterations(max_iterations); + } Self::new_inner( rng, @@ -444,6 +480,7 @@ impl Workload { } else { self.ensure_changeset_reverted(&changeset).await?; } + self.ensure_root().await?; Ok(()) } @@ -625,6 +662,7 @@ impl Workload { } } + self.ensure_root().await?; self.ensure_snapshot_validity().await?; Ok(()) } @@ -686,6 +724,7 @@ impl Workload { self.reconcile_hashtable_growth(old_hashtable_buckets, new_hashtable_buckets) .await?; + self.ensure_root().await?; self.ensure_snapshot_validity().await?; Ok(()) } @@ -847,6 +886,28 @@ impl Workload { Ok(()) } + /// Ensure that the root of the database is the root of a trie built from scratch out of the + /// committed state. + /// + /// The values are read from the beatree and the root is computed from the merkle pages, + /// which are written and recovered separately. No other check looks at the merkle pages. + /// Right after opening, the root is read from the root page alone; the pages below it show + /// in the root of a later commit that goes through them. + async fn ensure_root(&self) -> anyhow::Result<()> { + let root = self.rr().send_query_root().await?; + let expected_root = self.committed.root(); + if root != expected_root { + return Err(anyhow::anyhow!( + "Unexpected root at sync_seqn {} with {} keys,\n expected: {},\n found: {}", + self.committed.sync_seqn, + self.committed.value_hashes.len(), + hex::encode(expected_root), + hex::encode(root), + )); + } + Ok(()) + } + async fn ensure_snapshot_validity(&mut self) -> anyhow::Result<()> { if !self.config.should_ensure_snapshot() && !self.config.sample_snapshot { return Ok(()); @@ -1008,6 +1069,11 @@ impl Workload { self.workload_dir } + /// Return the configuration of the workload. + pub fn config(&self) -> &WorkloadConfiguration { + &self.config + } + /// Return the workload directory path. pub fn workload_dir_path(&self) -> PathBuf { self.workload_dir.path().to_path_buf() @@ -1052,7 +1118,7 @@ impl Workload { break; } - snapshot.state.insert(*change.key(), change.value()); + snapshot.set(*change.key(), change.value()); changes.push(change); if used_keys.len() + new_keys.len() >= changeset_size { @@ -1213,6 +1279,7 @@ impl Workload { // The application of a rollback counts as increased sync_seq. self.committed.sync_seqn += 1; self.committed.state = snapshot.state; + self.committed.value_hashes = snapshot.value_hashes; } fn commit(&mut self, snapshot: Snapshot) {