From be29e48a60345599373128c8cd6632f4f407165c Mon Sep 17 00:00:00 2001 From: Weny Xu Date: Tue, 2 Jul 2024 21:02:04 +0800 Subject: [PATCH] chore: reduce insertion size of fuzz test (#4243) * chore: reduce size of fuzz test * chore: get env cfg variables --- .env.example | 5 +++++ tests-fuzz/src/utils.rs | 17 +++++++++++++++++ .../failover/fuzz_failover_mito_regions.rs | 18 ++++++++++++------ tests-fuzz/targets/fuzz_alter_logical_table.rs | 8 ++++++-- tests-fuzz/targets/fuzz_alter_table.rs | 8 ++++++-- tests-fuzz/targets/fuzz_create_table.rs | 8 ++++++-- tests-fuzz/targets/fuzz_insert.rs | 11 ++++++++--- .../targets/fuzz_insert_logical_table.rs | 17 ++++++++++++----- .../unstable/fuzz_create_table_standalone.rs | 13 ++++++++----- 9 files changed, 80 insertions(+), 25 deletions(-) diff --git a/.env.example b/.env.example index f8518696580a..d463d85a7d16 100644 --- a/.env.example +++ b/.env.example @@ -28,3 +28,8 @@ GT_MYSQL_ADDR = localhost:4002 # Setting for unstable fuzz tests GT_FUZZ_BINARY_PATH=/path/to/ GT_FUZZ_INSTANCE_ROOT_DIR=/tmp/unstable_greptime +GT_FUZZ_INPUT_MAX_ROWS=2048 +GT_FUZZ_INPUT_MAX_TABLES=32 +GT_FUZZ_INPUT_MAX_COLUMNS=32 +GT_FUZZ_INPUT_MAX_ALTER_ACTIONS=256 +GT_FUZZ_INPUT_MAX_INSERT_ACTIONS=8 diff --git a/tests-fuzz/src/utils.rs b/tests-fuzz/src/utils.rs index 8849b45c4d55..0251e988d39b 100644 --- a/tests-fuzz/src/utils.rs +++ b/tests-fuzz/src/utils.rs @@ -117,3 +117,20 @@ pub async fn compact_table(e: &Pool, table_name: &Ident) -> Result<()> { Ok(()) } + +pub const GT_FUZZ_INPUT_MAX_ROWS: &str = "GT_FUZZ_INPUT_MAX_ROWS"; +pub const GT_FUZZ_INPUT_MAX_TABLES: &str = "GT_FUZZ_INPUT_MAX_TABLES"; +pub const GT_FUZZ_INPUT_MAX_COLUMNS: &str = "GT_FUZZ_INPUT_MAX_COLUMNS"; +pub const GT_FUZZ_INPUT_MAX_ALTER_ACTIONS: &str = "GT_FUZZ_INPUT_MAX_ALTER_ACTIONS"; +pub const GT_FUZZ_INPUT_MAX_INSERT_ACTIONS: &str = "GT_FUZZ_INPUT_MAX_INSERT_ACTIONS"; + +/// Retrieves a value from the environment variables +/// or returns a default value if the environment variable is not set. +pub fn get_from_env_or_default_value(key: &str, default_value: usize) -> usize { + let _ = dotenv::dotenv(); + if let Ok(value) = env::var(key) { + value.parse().unwrap() + } else { + default_value + } +} diff --git a/tests-fuzz/targets/failover/fuzz_failover_mito_regions.rs b/tests-fuzz/targets/failover/fuzz_failover_mito_regions.rs index 57332d5a0621..2b6ebd06bc41 100644 --- a/tests-fuzz/targets/failover/fuzz_failover_mito_regions.rs +++ b/tests-fuzz/targets/failover/fuzz_failover_mito_regions.rs @@ -51,8 +51,10 @@ use tests_fuzz::utils::partition::{ }; use tests_fuzz::utils::pod_failure::{inject_datanode_pod_failure, recover_pod_failure}; use tests_fuzz::utils::{ - compact_table, flush_memtable, init_greptime_connections_via_env, Connections, - GT_FUZZ_CLUSTER_NAME, GT_FUZZ_CLUSTER_NAMESPACE, + compact_table, flush_memtable, get_from_env_or_default_value, + init_greptime_connections_via_env, Connections, GT_FUZZ_CLUSTER_NAME, + GT_FUZZ_CLUSTER_NAMESPACE, GT_FUZZ_INPUT_MAX_COLUMNS, GT_FUZZ_INPUT_MAX_INSERT_ACTIONS, + GT_FUZZ_INPUT_MAX_ROWS, GT_FUZZ_INPUT_MAX_TABLES, }; use tests_fuzz::validator::row::count_values; use tokio::sync::Semaphore; @@ -83,10 +85,14 @@ impl Arbitrary<'_> for FuzzInput { fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result { let seed = u.int_in_range(u64::MIN..=u64::MAX)?; let mut rng = ChaChaRng::seed_from_u64(seed); - let columns = rng.gen_range(2..64); - let rows = rng.gen_range(2..2048); - let tables = rng.gen_range(1..64); - let inserts = rng.gen_range(2..8); + let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 64); + let columns = rng.gen_range(2..max_columns); + let max_rows = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ROWS, 2048); + let rows = rng.gen_range(2..max_rows); + let max_tables = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_TABLES, 64); + let tables = rng.gen_range(1..max_tables); + let max_inserts = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_INSERT_ACTIONS, 8); + let inserts = rng.gen_range(2..max_inserts); Ok(FuzzInput { columns, rows, diff --git a/tests-fuzz/targets/fuzz_alter_logical_table.rs b/tests-fuzz/targets/fuzz_alter_logical_table.rs index 57a773c56050..40f1a699b89b 100644 --- a/tests-fuzz/targets/fuzz_alter_logical_table.rs +++ b/tests-fuzz/targets/fuzz_alter_logical_table.rs @@ -42,7 +42,10 @@ use tests_fuzz::ir::{ use tests_fuzz::translator::mysql::alter_expr::AlterTableExprTranslator; use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator; use tests_fuzz::translator::DslTranslator; -use tests_fuzz::utils::{init_greptime_connections_via_env, Connections}; +use tests_fuzz::utils::{ + get_from_env_or_default_value, init_greptime_connections_via_env, Connections, + GT_FUZZ_INPUT_MAX_ALTER_ACTIONS, +}; use tests_fuzz::validator; struct FuzzContext { @@ -65,7 +68,8 @@ impl Arbitrary<'_> for FuzzInput { fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result { let seed = u.int_in_range(u64::MIN..=u64::MAX)?; let mut rng = ChaChaRng::seed_from_u64(seed); - let actions = rng.gen_range(1..256); + let max_actions = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ALTER_ACTIONS, 256); + let actions = rng.gen_range(1..max_actions); Ok(FuzzInput { seed, actions }) } diff --git a/tests-fuzz/targets/fuzz_alter_table.rs b/tests-fuzz/targets/fuzz_alter_table.rs index ced68b22e85d..edb8ce92ef01 100644 --- a/tests-fuzz/targets/fuzz_alter_table.rs +++ b/tests-fuzz/targets/fuzz_alter_table.rs @@ -40,7 +40,10 @@ use tests_fuzz::ir::{droppable_columns, modifiable_columns, AlterTableExpr, Crea use tests_fuzz::translator::mysql::alter_expr::AlterTableExprTranslator; use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator; use tests_fuzz::translator::DslTranslator; -use tests_fuzz::utils::{init_greptime_connections_via_env, Connections}; +use tests_fuzz::utils::{ + get_from_env_or_default_value, init_greptime_connections_via_env, Connections, + GT_FUZZ_INPUT_MAX_COLUMNS, +}; use tests_fuzz::validator; struct FuzzContext { greptime: Pool, @@ -67,7 +70,8 @@ enum AlterTableOption { } fn generate_create_table_expr(rng: &mut R) -> Result { - let columns = rng.gen_range(2..30); + let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 30); + let columns = rng.gen_range(2..max_columns); let create_table_generator = CreateTableExprGeneratorBuilder::default() .name_generator(Box::new(MappedGenerator::new( WordGenerator, diff --git a/tests-fuzz/targets/fuzz_create_table.rs b/tests-fuzz/targets/fuzz_create_table.rs index 0eb29ec7c6dd..eaaf2356fe1c 100644 --- a/tests-fuzz/targets/fuzz_create_table.rs +++ b/tests-fuzz/targets/fuzz_create_table.rs @@ -31,7 +31,10 @@ use tests_fuzz::generator::Generator; use tests_fuzz::ir::CreateTableExpr; use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator; use tests_fuzz::translator::DslTranslator; -use tests_fuzz::utils::{init_greptime_connections_via_env, Connections}; +use tests_fuzz::utils::{ + get_from_env_or_default_value, init_greptime_connections_via_env, Connections, + GT_FUZZ_INPUT_MAX_COLUMNS, +}; use tests_fuzz::validator; struct FuzzContext { @@ -54,7 +57,8 @@ impl Arbitrary<'_> for FuzzInput { fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result { let seed = u.int_in_range(u64::MIN..=u64::MAX)?; let mut rng = ChaChaRng::seed_from_u64(seed); - let columns = rng.gen_range(2..30); + let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 30); + let columns = rng.gen_range(2..max_columns); Ok(FuzzInput { columns, seed }) } } diff --git a/tests-fuzz/targets/fuzz_insert.rs b/tests-fuzz/targets/fuzz_insert.rs index fdb258775187..e73d2287ba8c 100644 --- a/tests-fuzz/targets/fuzz_insert.rs +++ b/tests-fuzz/targets/fuzz_insert.rs @@ -39,7 +39,10 @@ use tests_fuzz::ir::{ use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator; use tests_fuzz::translator::mysql::insert_expr::InsertIntoExprTranslator; use tests_fuzz::translator::DslTranslator; -use tests_fuzz::utils::{flush_memtable, init_greptime_connections_via_env, Connections}; +use tests_fuzz::utils::{ + flush_memtable, get_from_env_or_default_value, init_greptime_connections_via_env, Connections, + GT_FUZZ_INPUT_MAX_COLUMNS, GT_FUZZ_INPUT_MAX_ROWS, +}; use tests_fuzz::validator; struct FuzzContext { @@ -63,8 +66,10 @@ impl Arbitrary<'_> for FuzzInput { fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result { let seed = u.int_in_range(u64::MIN..=u64::MAX)?; let mut rng = ChaChaRng::seed_from_u64(seed); - let columns = rng.gen_range(2..30); - let rows = rng.gen_range(1..4096); + let max_columns = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_COLUMNS, 30); + let columns = rng.gen_range(2..max_columns); + let max_row = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ROWS, 2048); + let rows = rng.gen_range(1..max_row); Ok(FuzzInput { columns, rows, diff --git a/tests-fuzz/targets/fuzz_insert_logical_table.rs b/tests-fuzz/targets/fuzz_insert_logical_table.rs index de518f284ac0..9d7a0a1c00d6 100644 --- a/tests-fuzz/targets/fuzz_insert_logical_table.rs +++ b/tests-fuzz/targets/fuzz_insert_logical_table.rs @@ -43,7 +43,9 @@ use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator; use tests_fuzz::translator::mysql::insert_expr::InsertIntoExprTranslator; use tests_fuzz::translator::DslTranslator; use tests_fuzz::utils::{ - compact_table, flush_memtable, init_greptime_connections_via_env, Connections, + compact_table, flush_memtable, get_from_env_or_default_value, + init_greptime_connections_via_env, Connections, GT_FUZZ_INPUT_MAX_ROWS, + GT_FUZZ_INPUT_MAX_TABLES, }; use tests_fuzz::validator; struct FuzzContext { @@ -60,14 +62,18 @@ impl FuzzContext { struct FuzzInput { seed: u64, tables: usize, + rows: usize, } impl Arbitrary<'_> for FuzzInput { fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result { let seed = u.int_in_range(u64::MIN..=u64::MAX)?; let mut rng = ChaChaRng::seed_from_u64(seed); - let tables = rng.gen_range(1..256); - Ok(FuzzInput { tables, seed }) + let max_tables = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_TABLES, 32); + let tables = rng.gen_range(1..max_tables); + let max_row = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_ROWS, 2048); + let rows = rng.gen_range(1..max_row); + Ok(FuzzInput { tables, seed, rows }) } } @@ -204,11 +210,11 @@ async fn validate_values( } async fn insert_values( + rows: usize, ctx: &FuzzContext, rng: &mut R, logical_table_ctx: TableContextRef, ) -> Result { - let rows = rng.gen_range(1..2048); let insert_expr = generate_insert_expr(rows, rng, logical_table_ctx.clone())?; let translator = InsertIntoExprTranslator; let sql = translator.translate(&insert_expr)?; @@ -257,7 +263,8 @@ async fn execute_insert(ctx: FuzzContext, input: FuzzInput) -> Result<()> { info!("Create logical table: {sql}, result: {result:?}"); let logical_table_ctx = Arc::new(TableContext::from(&create_logical_table_expr)); - let insert_expr = insert_values(&ctx, &mut rng, logical_table_ctx.clone()).await?; + let insert_expr = + insert_values(input.rows, &ctx, &mut rng, logical_table_ctx.clone()).await?; validate_values(&ctx, logical_table_ctx.clone(), &insert_expr).await?; tables.insert(logical_table_ctx.name.clone(), logical_table_ctx.clone()); if rng.gen_bool(0.1) { diff --git a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs index 5f499be33d8f..5ef880666bfc 100644 --- a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs +++ b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs @@ -43,8 +43,10 @@ use tests_fuzz::translator::mysql::create_expr::CreateTableExprTranslator; use tests_fuzz::translator::DslTranslator; use tests_fuzz::utils::config::{get_conf_path, write_config_file}; use tests_fuzz::utils::health::HttpHealthChecker; -use tests_fuzz::utils::load_unstable_test_env_variables; use tests_fuzz::utils::process::{ProcessManager, ProcessState, UnstableProcessController}; +use tests_fuzz::utils::{ + get_from_env_or_default_value, load_unstable_test_env_variables, GT_FUZZ_INPUT_MAX_TABLES, +}; use tests_fuzz::{error, validator}; use tokio::sync::watch; @@ -61,15 +63,16 @@ impl FuzzContext { #[derive(Clone, Debug)] struct FuzzInput { seed: u64, - num: usize, + tables: usize, } impl Arbitrary<'_> for FuzzInput { fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result { let seed = u.int_in_range(u64::MIN..=u64::MAX)?; let mut rng = ChaChaRng::seed_from_u64(seed); - let num = rng.gen_range(1..500); - Ok(FuzzInput { seed, num }) + let max_tables = get_from_env_or_default_value(GT_FUZZ_INPUT_MAX_TABLES, 256); + let tables = rng.gen_range(1..max_tables); + Ok(FuzzInput { seed, tables }) } } @@ -134,7 +137,7 @@ async fn execute_unstable_create_table( let ctx = FuzzContext { greptime: mysql }; let mut table_states = HashMap::new(); - for _ in 0..input.num { + for _ in 0..input.tables { let expr = generate_create_table_expr(&mut rng); let table_ctx = Arc::new(TableContext::from(&expr)); let table_name = expr.table_name.to_string();