From 35caf8d947bd7271ef655d6e35cb9754c84e98ec Mon Sep 17 00:00:00 2001 From: WenyXu Date: Mon, 27 May 2024 04:02:22 +0000 Subject: [PATCH 1/4] fix(fuzz-tests): avoid to drop in-use database --- .../unstable/fuzz_create_table_standalone.rs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs index ad0a64f7d28c..eb834c375ac0 100644 --- a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs +++ b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs @@ -94,11 +94,11 @@ fn generate_create_table_expr(rng: &mut R) -> CreateTableExpr create_table_generator.generate(rng).unwrap() } -async fn connect_mysql(addr: &str) -> Pool { +async fn connect_mysql(addr: &str, database: &str) -> Pool { loop { match MySqlPoolOptions::new() .acquire_timeout(Duration::from_secs(30)) - .connect(&format!("mysql://{addr}/public")) + .connect(&format!("mysql://{addr}/{database}")) .await { Ok(mysql) => return mysql, @@ -109,6 +109,8 @@ async fn connect_mysql(addr: &str) -> Pool { } } +const FUZZ_TESTS_DATABASE: &str = "fuzz_tests"; + async fn execute_unstable_create_table( unstable_process_controller: Arc, rx: watch::Receiver, @@ -117,10 +119,20 @@ async fn execute_unstable_create_table( // Starts the unstable process. let moved_unstable_process_controller = unstable_process_controller.clone(); let handler = tokio::spawn(async move { moved_unstable_process_controller.start().await }); + let mysql_public = connect_mysql(DEFAULT_MYSQL_URL, "public").await; + loop { + let sql = format!("CREATE DATABASE IF NOT EXISTS {FUZZ_TESTS_DATABASE}"); + match sqlx::query(&sql).execute(&mysql_public).await { + Ok(result) => { + info!("Create database: {}, result: {result:?}", sql); + break; + } + Err(err) => warn!("Failed to create database: {}, error: {err}", sql), + } + } + let mysql = connect_mysql(DEFAULT_MYSQL_URL, FUZZ_TESTS_DATABASE).await; let mut rng = ChaChaRng::seed_from_u64(input.seed); - let mysql = connect_mysql(DEFAULT_MYSQL_URL).await; let ctx = FuzzContext { greptime: mysql }; - let mut table_states = HashMap::new(); for _ in 0..input.num { @@ -163,13 +175,13 @@ async fn execute_unstable_create_table( } loop { - let sql = "DROP DATABASE IF EXISTS public"; - match sqlx::query(sql).execute(&ctx.greptime).await { + let sql = format!("DROP DATABASE IF EXISTS {FUZZ_TESTS_DATABASE}"); + match sqlx::query(&sql).execute(&mysql_public).await { Ok(result) => { - info!("Drop table: {}, result: {result:?}", sql); + info!("Drop database: {}, result: {result:?}", sql); break; } - Err(err) => warn!("Failed to drop table: {}, error: {err}", sql), + Err(err) => warn!("Failed to drop database: {}, error: {err}", sql), } } // Cleans up From 12afdcccceb6438c4f15c666736acf40f6734de0 Mon Sep 17 00:00:00 2001 From: WenyXu Date: Mon, 27 May 2024 06:40:28 +0000 Subject: [PATCH 2/4] fix: correct datahome path --- .../unstable/fuzz_create_table_standalone.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs index eb834c375ac0..60f6392b74e4 100644 --- a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs +++ b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs @@ -76,7 +76,6 @@ impl Arbitrary<'_> for FuzzInput { const DEFAULT_TEMPLATE: &str = "standalone.template.toml"; const DEFAULT_CONFIG_NAME: &str = "standalone.template.toml"; const DEFAULT_ROOT_DIR: &str = "/tmp/unstable_greptime/"; -const DEFAULT_DATA_HOME: &str = "/tmp/unstable_greptime/datahome/"; const DEFAULT_MYSQL_URL: &str = "127.0.0.1:4002"; const DEFAULT_HTTP_HEALTH_URL: &str = "http://127.0.0.1:4000/health"; @@ -219,6 +218,8 @@ fuzz_target!(|input: FuzzInput| { let root_dir = variables.root_dir.unwrap_or(DEFAULT_ROOT_DIR.to_string()); create_dir_all(&root_dir).unwrap(); let output_config_path = format!("{root_dir}{DEFAULT_CONFIG_NAME}"); + let data_home = format!("{root_dir}datahome"); + let mut conf_path = get_conf_path(); conf_path.push(DEFAULT_TEMPLATE); let template_path = conf_path.to_str().unwrap().to_string(); @@ -228,15 +229,9 @@ fuzz_target!(|input: FuzzInput| { struct Context { data_home: String, } - write_config_file( - &template_path, - &Context { - data_home: DEFAULT_DATA_HOME.to_string(), - }, - &output_config_path, - ) - .await - .unwrap(); + write_config_file(&template_path, &Context { data_home }, &output_config_path) + .await + .unwrap(); let args = vec![ "standalone".to_string(), From be996fcf04fbc3b691e357920e4500f57b01ded7 Mon Sep 17 00:00:00 2001 From: WenyXu Date: Mon, 27 May 2024 07:09:38 +0000 Subject: [PATCH 3/4] fix: correct `schema_name` --- .../targets/unstable/fuzz_create_table_standalone.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs index 60f6392b74e4..7efc237cb10c 100644 --- a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs +++ b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs @@ -151,7 +151,7 @@ async fn execute_unstable_create_table( Ok(result) => { let state = *rx.borrow(); table_states.insert(table_name, state); - validate_columns(&ctx.greptime, &table_ctx).await; + validate_columns(&ctx.greptime, FUZZ_TESTS_DATABASE.to_string(), &table_ctx).await; info!("Create table: {sql}, result: {result:?}"); } Err(err) => { @@ -191,10 +191,14 @@ async fn execute_unstable_create_table( Ok(()) } -async fn validate_columns(client: &Pool, table_ctx: &TableContext) { +async fn validate_columns(client: &Pool, schema_name: String, table_ctx: &TableContext) { loop { - match validator::column::fetch_columns(client, "public".into(), table_ctx.name.clone()) - .await + match validator::column::fetch_columns( + client, + schema_name.clone().into(), + table_ctx.name.clone(), + ) + .await { Ok(mut column_entries) => { column_entries.sort_by(|a, b| a.column_name.cmp(&b.column_name)); From b9671de04b0e3917c1df8f6879ba9af00e3c7f7c Mon Sep 17 00:00:00 2001 From: WenyXu Date: Mon, 27 May 2024 07:44:52 +0000 Subject: [PATCH 4/4] chore: apply suggestions from CR --- .../targets/unstable/fuzz_create_table_standalone.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs index 7efc237cb10c..5f499be33d8f 100644 --- a/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs +++ b/tests-fuzz/targets/unstable/fuzz_create_table_standalone.rs @@ -151,7 +151,7 @@ async fn execute_unstable_create_table( Ok(result) => { let state = *rx.borrow(); table_states.insert(table_name, state); - validate_columns(&ctx.greptime, FUZZ_TESTS_DATABASE.to_string(), &table_ctx).await; + validate_columns(&ctx.greptime, FUZZ_TESTS_DATABASE, &table_ctx).await; info!("Create table: {sql}, result: {result:?}"); } Err(err) => { @@ -191,14 +191,10 @@ async fn execute_unstable_create_table( Ok(()) } -async fn validate_columns(client: &Pool, schema_name: String, table_ctx: &TableContext) { +async fn validate_columns(client: &Pool, schema_name: &str, table_ctx: &TableContext) { loop { - match validator::column::fetch_columns( - client, - schema_name.clone().into(), - table_ctx.name.clone(), - ) - .await + match validator::column::fetch_columns(client, schema_name.into(), table_ctx.name.clone()) + .await { Ok(mut column_entries) => { column_entries.sort_by(|a, b| a.column_name.cmp(&b.column_name));