Skip to main content

tests_fuzz/
utils.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod cluster_info;
16pub mod config;
17pub mod crd;
18/// CSV dump writer utilities for fuzz tests.
19pub mod csv_dump_writer;
20pub mod health;
21pub mod kafka_wal_http;
22pub mod migration;
23pub mod network_chaos;
24pub mod partition;
25pub mod pod_failure;
26pub mod procedure;
27#[cfg(feature = "unstable")]
28pub mod process;
29pub mod retry;
30/// SQL dump writer utilities for fuzz tests.
31pub mod sql_dump_writer;
32pub mod wait;
33
34use std::env;
35use std::str::FromStr;
36
37use common_base::readable_size::ReadableSize;
38use common_telemetry::info;
39use common_telemetry::tracing::log::LevelFilter;
40use paste::paste;
41use snafu::ResultExt;
42use sqlx::mysql::{MySqlConnectOptions, MySqlPoolOptions};
43use sqlx::{ConnectOptions, MySql, Pool};
44
45use crate::error::{self, Result};
46use crate::ir::Ident;
47
48/// Database connections
49pub struct Connections {
50    pub mysql: Option<Pool<MySql>>,
51}
52
53const GT_MYSQL_ADDR: &str = "GT_MYSQL_ADDR";
54
55/// Connects to GreptimeDB via env variables.
56pub async fn init_greptime_connections_via_env() -> Connections {
57    crate::install_rustls_crypto_provider();
58
59    let _ = dotenv::dotenv();
60    let mysql = if let Ok(addr) = env::var(GT_MYSQL_ADDR) {
61        Some(addr)
62    } else {
63        info!("GT_MYSQL_ADDR is empty, ignores test");
64        None
65    };
66
67    init_greptime_connections(mysql).await
68}
69
70/// Connects to GreptimeDB.
71pub async fn init_greptime_connections(mysql: Option<String>) -> Connections {
72    let mysql = if let Some(addr) = mysql {
73        let opts = format!("mysql://{addr}/public")
74            .parse::<MySqlConnectOptions>()
75            .unwrap()
76            .log_statements(LevelFilter::Off);
77
78        Some(MySqlPoolOptions::new().connect_with(opts).await.unwrap())
79    } else {
80        None
81    };
82
83    Connections { mysql }
84}
85
86const GT_FUZZ_BINARY_PATH: &str = "GT_FUZZ_BINARY_PATH";
87const GT_FUZZ_INSTANCE_ROOT_DIR: &str = "GT_FUZZ_INSTANCE_ROOT_DIR";
88
89/// The variables for unstable test
90pub struct UnstableTestVariables {
91    pub binary_path: String,
92    pub root_dir: Option<String>,
93}
94
95/// Loads env variables for unstable test
96pub fn load_unstable_test_env_variables() -> UnstableTestVariables {
97    let _ = dotenv::dotenv();
98    let binary_path = env::var(GT_FUZZ_BINARY_PATH).expect("GT_FUZZ_BINARY_PATH not found");
99    let root_dir = env::var(GT_FUZZ_INSTANCE_ROOT_DIR).ok();
100
101    UnstableTestVariables {
102        binary_path,
103        root_dir,
104    }
105}
106
107pub const GT_FUZZ_CLUSTER_NAMESPACE: &str = "GT_FUZZ_CLUSTER_NAMESPACE";
108pub const GT_FUZZ_CLUSTER_NAME: &str = "GT_FUZZ_CLUSTER_NAME";
109
110/// Flushes memtable to SST file.
111pub async fn flush_memtable(e: &Pool<MySql>, table_name: &Ident) -> Result<()> {
112    let sql = format!("admin flush_table(\"{}\")", table_name);
113    let result = sqlx::query(sqlx::AssertSqlSafe(sql.as_str()))
114        .execute(e)
115        .await
116        .context(error::ExecuteQuerySnafu { sql })?;
117    info!("Flush table: {}\n\nResult: {result:?}\n\n", table_name);
118
119    Ok(())
120}
121
122/// Triggers a compaction for table
123pub async fn compact_table(e: &Pool<MySql>, table_name: &Ident) -> Result<()> {
124    let sql = format!("admin compact_table(\"{}\")", table_name);
125    let result = sqlx::query(sqlx::AssertSqlSafe(sql.as_str()))
126        .execute(e)
127        .await
128        .context(error::ExecuteQuerySnafu { sql })?;
129    info!("Compact table: {}\n\nResult: {result:?}\n\n", table_name);
130
131    Ok(())
132}
133
134/// Compacts `table_name` with the strict-window mode, which rewrites even a lone file.
135///
136/// A merge is refused while a participant still holds an SST another region created, and a split
137/// hands the parent's files to its children unchanged; this is what materialises them again.
138pub async fn compact_table_strict_window(e: &Pool<MySql>, table_name: &Ident) -> Result<()> {
139    let sql = format!(
140        "admin compact_table(\"{}\", \"strict_window\", \"window=3600\")",
141        table_name
142    );
143    let result = sqlx::query(sqlx::AssertSqlSafe(sql.as_str()))
144        .execute(e)
145        .await
146        .context(error::ExecuteQuerySnafu { sql })?;
147    info!(
148        "Strict-window compact table: {}\n\nResult: {result:?}\n\n",
149        table_name
150    );
151
152    Ok(())
153}
154
155pub const GT_FUZZ_INPUT_MAX_ROWS: &str = "GT_FUZZ_INPUT_MAX_ROWS";
156pub const GT_FUZZ_INPUT_MAX_TABLES: &str = "GT_FUZZ_INPUT_MAX_TABLES";
157pub const GT_FUZZ_INPUT_MAX_COLUMNS: &str = "GT_FUZZ_INPUT_MAX_COLUMNS";
158pub const GT_FUZZ_INPUT_MAX_ALTER_ACTIONS: &str = "GT_FUZZ_INPUT_MAX_ALTER_ACTIONS";
159pub const GT_FUZZ_INPUT_MAX_INSERT_ACTIONS: &str = "GT_FUZZ_INPUT_MAX_INSERT_ACTIONS";
160pub const FUZZ_OVERRIDE_PREFIX: &str = "GT_FUZZ_OVERRIDE_";
161/// Enables CSV dump generation for fuzz runs.
162pub const GT_FUZZ_DUMP_TABLE_CSV: &str = "GT_FUZZ_DUMP_TABLE_CSV";
163/// Base directory for CSV dump sessions.
164pub const GT_FUZZ_DUMP_DIR: &str = "GT_FUZZ_DUMP_DIR";
165/// Directory suffix used by one CSV dump session.
166pub const GT_FUZZ_DUMP_SUFFIX: &str = "GT_FUZZ_DUMP_SUFFIX";
167/// Max in-memory CSV buffer size before auto flush.
168pub const GT_FUZZ_DUMP_BUFFER_MAX_BYTES: &str = "GT_FUZZ_DUMP_BUFFER_MAX_BYTES";
169
170/// Reads an override value for a fuzz parameter from env `GT_FUZZ_OVERRIDE_<NAME>`.
171pub fn get_fuzz_override<T>(name: &str) -> Option<T>
172where
173    T: std::str::FromStr,
174{
175    let _ = dotenv::dotenv();
176    let key = format!("{}{}", FUZZ_OVERRIDE_PREFIX, name.to_ascii_uppercase());
177    env::var(&key).ok().and_then(|v| v.parse().ok())
178}
179
180/// Returns CSV dump base directory.
181pub fn get_gt_fuzz_dump_dir() -> String {
182    let _ = dotenv::dotenv();
183    env::var(GT_FUZZ_DUMP_DIR).unwrap_or_else(|_| "/tmp/greptime-fuzz-dumps".to_string())
184}
185
186/// Returns CSV dump directory suffix.
187pub fn get_gt_fuzz_dump_suffix() -> String {
188    let _ = dotenv::dotenv();
189    env::var(GT_FUZZ_DUMP_SUFFIX).unwrap_or_else(|_| ".repartition-metric-csv".to_string())
190}
191
192/// Returns max CSV in-memory buffer size.
193pub fn get_gt_fuzz_dump_buffer_max_bytes() -> usize {
194    let _ = dotenv::dotenv();
195    env::var(GT_FUZZ_DUMP_BUFFER_MAX_BYTES)
196        .ok()
197        .and_then(|value| {
198            value.parse::<usize>().ok().or_else(|| {
199                ReadableSize::from_str(&value)
200                    .ok()
201                    .map(|size| size.as_bytes() as usize)
202            })
203        })
204        .unwrap_or(8 * 1024 * 1024)
205}
206
207macro_rules! make_get_from_env_helper {
208    ($key:expr, $default: expr) => {
209        paste! {
210            #[doc = "Retrieves `" $key "` environment variable \
211                     or returns a default value (`" $default "`) if the environment variable is not set.
212            "]
213            pub fn [<get_ $key:lower>]() -> usize {
214                get_from_env_or_default_value($key, $default)
215            }
216        }
217    };
218}
219
220make_get_from_env_helper!(GT_FUZZ_INPUT_MAX_ALTER_ACTIONS, 256);
221make_get_from_env_helper!(GT_FUZZ_INPUT_MAX_INSERT_ACTIONS, 4);
222make_get_from_env_helper!(GT_FUZZ_INPUT_MAX_ROWS, 512);
223make_get_from_env_helper!(GT_FUZZ_INPUT_MAX_TABLES, 32);
224make_get_from_env_helper!(GT_FUZZ_INPUT_MAX_COLUMNS, 16);
225
226/// Retrieves a value from the environment variables
227/// or returns a default value if the environment variable is not set.
228fn get_from_env_or_default_value(key: &str, default_value: usize) -> usize {
229    let _ = dotenv::dotenv();
230    if let Ok(value) = env::var(key) {
231        value.parse().unwrap()
232    } else {
233        default_value
234    }
235}