From 61fa7546c1c0fb1eaabafd021ee37f33d80c05c3 Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Fri, 29 Aug 2025 11:47:46 -0300 Subject: [PATCH] fold some `SimulatorOpts` fields to `Profile` --- simulator/generation/property.rs | 13 +++--- simulator/main.rs | 1 - simulator/runner/cli.rs | 32 +++++--------- simulator/runner/env.rs | 71 +++++++++++++++++++------------- 4 files changed, 59 insertions(+), 58 deletions(-) diff --git a/simulator/generation/property.rs b/simulator/generation/property.rs index 8fe207685..ab11a2d0e 100644 --- a/simulator/generation/property.rs +++ b/simulator/generation/property.rs @@ -1444,9 +1444,10 @@ fn property_faulty_query( impl ArbitraryFrom<(&SimulatorEnv, &InteractionStats)> for Property { fn arbitrary_from( rng: &mut R, - _context: &C, + context: &C, (env, stats): (&SimulatorEnv, &InteractionStats), ) -> Self { + let opts = context.opts(); let remaining_ = remaining(env.opts.max_interactions, &env.profile.query, stats); frequency( @@ -1509,7 +1510,7 @@ impl ArbitraryFrom<(&SimulatorEnv, &InteractionStats)> for Property { Box::new(|rng: &mut R| property_select_select_optimizer(rng, env)), ), ( - if env.opts.experimental_indexes && !env.opts.disable_where_true_false_null { + if opts.indexes && !env.opts.disable_where_true_false_null { remaining_.select / 2 } else { 0 @@ -1517,9 +1518,7 @@ impl ArbitraryFrom<(&SimulatorEnv, &InteractionStats)> for Property { Box::new(|rng: &mut R| property_where_true_false_null(rng, env)), ), ( - if env.opts.experimental_indexes - && !env.opts.disable_union_all_preserves_cardinality - { + if opts.indexes && !env.opts.disable_union_all_preserves_cardinality { remaining_.select / 3 } else { 0 @@ -1527,7 +1526,7 @@ impl ArbitraryFrom<(&SimulatorEnv, &InteractionStats)> for Property { Box::new(|rng: &mut R| property_union_all_preserves_cardinality(rng, env)), ), ( - if !env.opts.disable_fsync_no_wait { + if env.profile.io.enable && !env.opts.disable_fsync_no_wait { 50 // Freestyle number } else { 0 @@ -1535,7 +1534,7 @@ impl ArbitraryFrom<(&SimulatorEnv, &InteractionStats)> for Property { Box::new(|rng: &mut R| property_fsync_no_wait(rng, env, &remaining_)), ), ( - if !env.opts.disable_faulty_query { + if env.profile.io.enable && !env.opts.disable_faulty_query { 20 } else { 0 diff --git a/simulator/main.rs b/simulator/main.rs index 2d4a45b30..be6df9c37 100644 --- a/simulator/main.rs +++ b/simulator/main.rs @@ -38,7 +38,6 @@ fn main() -> anyhow::Result<()> { let profile = Profile::parse_from_type(cli_opts.profile.clone())?; tracing::debug!(sim_profile = ?profile); - dbg!(&profile); match cli_opts.subcommand { Some(SimulatorCommand::List) => { diff --git a/simulator/runner/cli.rs b/simulator/runner/cli.rs index d0ceaea24..9891ee7b2 100644 --- a/simulator/runner/cli.rs +++ b/simulator/runner/cli.rs @@ -114,28 +114,16 @@ pub struct SimulatorCLI { pub disable_faulty_query: bool, #[clap(long, help = "disable Reopen-Database fault", default_value_t = false)] pub disable_reopen_database: bool, - #[clap( - long = "latency-prob", - help = "added IO latency probability", - default_value_t = 1 - )] - pub latency_probability: usize, - #[clap( - long, - help = "Minimum tick time in microseconds for simulated time", - default_value_t = 1 - )] - pub min_tick: u64, - #[clap( - long, - help = "Maximum tick time in microseconds for simulated time", - default_value_t = 30 - )] - pub max_tick: u64, + #[clap(long = "latency-prob", help = "added IO latency probability")] + pub latency_probability: Option, + #[clap(long, help = "Minimum tick time in microseconds for simulated time")] + pub min_tick: Option, + #[clap(long, help = "Maximum tick time in microseconds for simulated time")] + pub max_tick: Option, #[clap(long, help = "Enable experimental MVCC feature")] - pub experimental_mvcc: bool, + pub experimental_mvcc: Option, #[clap(long, help = "Disable experimental indexing feature")] - pub disable_experimental_indexes: bool, + pub disable_experimental_indexes: Option, #[clap( long, help = "Keep all database and plan files", @@ -202,10 +190,10 @@ impl SimulatorCLI { anyhow::bail!("Cannot set seed and load plan at the same time"); } - if self.latency_probability > 100 { + if self.latency_probability.is_some_and(|prob| prob > 100) { anyhow::bail!( "latency probability must be a number between 0 and 100. Got `{}`", - self.latency_probability + self.latency_probability.unwrap() ); } diff --git a/simulator/runner/env.rs b/simulator/runner/env.rs index c0e97d45a..567f2bad9 100644 --- a/simulator/runner/env.rs +++ b/simulator/runner/env.rs @@ -5,6 +5,7 @@ use std::panic::UnwindSafe; use std::path::{Path, PathBuf}; use std::sync::Arc; +use garde::Validate; use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha8Rng; use sql_generation::model::table::Table; @@ -96,13 +97,15 @@ impl SimulatorEnv { self.connections.iter_mut().for_each(|c| c.disconnect()); self.rng = ChaCha8Rng::seed_from_u64(self.opts.seed); + let latency_prof = &self.profile.io.latency; + let io = Arc::new( SimulatorIO::new( self.opts.seed, self.opts.page_size, - self.opts.latency_probability, - self.opts.min_tick, - self.opts.max_tick, + latency_prof.latency_probability, + latency_prof.min_tick, + latency_prof.max_tick, ) .unwrap(), ); @@ -122,8 +125,8 @@ impl SimulatorEnv { let db = match Database::open_file( io.clone(), db_path.to_str().unwrap(), - self.opts.experimental_mvcc, - self.opts.experimental_indexes, + self.profile.experimental_mvcc, + self.profile.query.gen_opts.indexes, ) { Ok(db) => db, Err(e) => { @@ -243,24 +246,8 @@ impl SimulatorEnv { as u32, max_time_simulation: cli_opts.maximum_time, disable_reopen_database: cli_opts.disable_reopen_database, - latency_probability: cli_opts.latency_probability, - experimental_mvcc: cli_opts.experimental_mvcc, - experimental_indexes: !cli_opts.disable_experimental_indexes, - min_tick: cli_opts.min_tick, - max_tick: cli_opts.max_tick, }; - let io = Arc::new( - SimulatorIO::new( - seed, - opts.page_size, - cli_opts.latency_probability, - cli_opts.min_tick, - cli_opts.max_tick, - ) - .unwrap(), - ); - // Remove existing database file if it exists let db_path = paths.db(&simulation_type, &SimulationPhase::Test); @@ -273,11 +260,44 @@ impl SimulatorEnv { std::fs::remove_file(&wal_path).unwrap(); } + let mut profile = profile.clone(); + // Conditionals here so that we can override some profile options from the CLI + if let Some(mvcc) = cli_opts.experimental_mvcc { + profile.experimental_mvcc = mvcc; + } + if let Some(indexes) = cli_opts.disable_experimental_indexes { + profile.query.gen_opts.indexes = indexes; + } + if let Some(latency_prob) = cli_opts.latency_probability { + profile.io.latency.latency_probability = latency_prob; + } + if let Some(max_tick) = cli_opts.max_tick { + profile.io.latency.max_tick = max_tick; + } + if let Some(min_tick) = cli_opts.min_tick { + profile.io.latency.min_tick = min_tick; + } + + profile.validate().unwrap(); + + let latency_prof = &profile.io.latency; + + let io = Arc::new( + SimulatorIO::new( + seed, + opts.page_size, + latency_prof.latency_probability, + latency_prof.min_tick, + latency_prof.max_tick, + ) + .unwrap(), + ); + let db = match Database::open_file( io.clone(), db_path.to_str().unwrap(), - opts.experimental_mvcc, - opts.experimental_indexes, + profile.experimental_mvcc, + profile.query.gen_opts.indexes, ) { Ok(db) => db, Err(e) => { @@ -409,11 +429,6 @@ pub(crate) struct SimulatorOpts { pub(crate) max_interactions: u32, pub(crate) page_size: usize, pub(crate) max_time_simulation: usize, - pub(crate) latency_probability: usize, - pub(crate) experimental_mvcc: bool, - pub(crate) experimental_indexes: bool, - pub min_tick: u64, - pub max_tick: u64, } #[derive(Debug, Clone)]