Files
greptimedb/tests-fuzz/src/utils/sql_dump_writer.rs
T
Weny Xu 20f38d8a6a test(fuzz): add metric table repartition fuzz target (#7754)
* test: add fuzz_repartition_metric_table target scaffold

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: add metric logical lifecycle in repartition fuzz target

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: support partitioned metric tables in repartition fuzz

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: add repartition loop and partition assertions for metric target

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: use shared timestamp clock in metric repartition writes

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor: unify string value and bound generation for fuzzing

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: use fixed physical table name in metric repartition fuzz

Signed-off-by: WenyXu <wenymedia@gmail.com>

* chore: fmt

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: update ci config

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor: use btreemap

Signed-off-by: WenyXu <wenymedia@gmail.com>

* print count result

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: add csv translator for insert expr

Introduce a dedicated top-level csv translator so fuzz insert expressions can be converted into writer-ready records through a structured path instead of ad-hoc formatting in targets.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: add csv dump session utilities

Introduce CSV dump env helpers and a session writer that creates run directories, emits seed metadata, and flushes staged CSV records for fuzz workflows.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: bound csv dump buffer with auto flush

Parse readable buffer sizes from env and flush staged CSV records automatically when the in-memory threshold is reached to prevent unbounded growth during long fuzz runs.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: flush csv dump before repartition validation

Wire csv dump session into the metric repartition fuzz flow so successful inserts are translated from insert expressions into CSV records during write loops and flushed to disk right before row validation.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: keep csv dumps on failure and cleanup on pass

Capture run outcomes in metric repartition fuzz, remove dump directories only after successful validation, and retain dump paths on failures so CI and local investigations can use the same artifacts.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: align partial csv records with table headers

Keep append payload compact by storing partial insert-expression columns, then expand to full table-context headers at flush time and fill missing values with empty strings.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* chore: add logs

Signed-off-by: WenyXu <wenymedia@gmail.com>

* dump csv

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: dump csv

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: add table-scoped sql dump writer primitives

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: capture table-scoped sql traces after execution

Record insert and repartition SQL only after successful execution, include started_at_ms and elapsed_ms in trace comments, and broadcast repartition events into every logical-table trace file for consistent debugging context.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: harden sql trace comments and include create sql

Normalize multiline trace comments into valid SQL comment lines and append logical-table CREATE SQL to per-table traces for better timeline reconstruction during repartition debugging.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: dump physical create and repartition SQL traces

Signed-off-by: WenyXu <wenymedia@gmail.com>

* dump repartition sql

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: scaffold writer control channel for barrier flow

Add Barrier/Resume/Stop control skeleton and channel wiring in write_loop to prepare per-repartition validation barriers. Also align SQL dump tests with broadcast SQL payload behavior.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: implement writer barrier pause and resume control

Make writer control messages effective by pausing writes on barrier, resuming on resume, and stopping via channel signaling so the next commit can enforce deterministic per-repartition validation boundaries.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: validate rows after each repartition barrier

Add per-action barrier/ack synchronization with timeout, run immediate logical-table row validation after each repartition, and resume writer only after validation completes to improve minimal failure localization.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: flush dump sessions before per-epoch validation

Extract a shared flush-and-snapshot helper and call it before each immediate row validation so CSV/SQL artifacts are persisted at the same epoch boundary being validated.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: fix unit tests

Signed-off-by: WenyXu <wenymedia@gmail.com>

* chore: add retry

Signed-off-by: WenyXu <wenymedia@gmail.com>

* chore: apply suggestions from CR

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
2026-03-13 08:00:09 +00:00

268 lines
8.3 KiB
Rust

// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::fs::{OpenOptions, create_dir_all};
use std::io::Write;
use std::path::PathBuf;
use snafu::ResultExt;
use crate::error::{self, Result};
use crate::utils::get_gt_fuzz_dump_buffer_max_bytes;
/// Session writer for table-scoped SQL trace files.
#[derive(Debug)]
pub struct SqlDumpSession {
/// Session directory path.
pub run_dir: PathBuf,
/// Max in-memory buffer size before auto flush.
pub max_buffer_bytes: usize,
buffered_bytes: usize,
entries_by_table: HashMap<String, Vec<String>>,
}
impl SqlDumpSession {
/// Creates SQL dump session with default buffer limit.
pub fn new(run_dir: PathBuf) -> Result<Self> {
Self::new_with_buffer_limit(run_dir, get_gt_fuzz_dump_buffer_max_bytes())
}
/// Creates SQL dump session with custom buffer limit.
pub fn new_with_buffer_limit(run_dir: PathBuf, max_buffer_bytes: usize) -> Result<Self> {
create_dir_all(&run_dir).context(error::CreateFileSnafu {
path: run_dir.to_string_lossy().to_string(),
})?;
Ok(Self {
run_dir,
max_buffer_bytes,
buffered_bytes: 0,
entries_by_table: HashMap::new(),
})
}
/// Appends one SQL statement for a logical table.
pub fn append_sql(&mut self, table: &str, sql: &str, comment: Option<&str>) -> Result<()> {
let entry = format_sql_entry(sql, comment);
self.push_entry(table, entry)?;
Ok(())
}
/// Broadcasts one comment event to all table trace files.
pub fn broadcast_event<I, T>(&mut self, tables: I, event: &str, sql: &str) -> Result<()>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
{
let entry = format_sql_entry(sql, Some(event));
for table in tables {
self.push_entry(table.as_ref(), entry.clone())?;
}
Ok(())
}
/// Flushes all staged SQL traces to table-scoped files.
pub fn flush_all(&mut self) -> Result<()> {
self.flush_buffered_entries()
}
fn push_entry(&mut self, table: &str, entry: String) -> Result<()> {
self.buffered_bytes += entry.len();
self.entries_by_table
.entry(table.to_string())
.or_default()
.push(entry);
if self.buffered_bytes >= self.max_buffer_bytes {
self.flush_buffered_entries()?;
}
Ok(())
}
fn flush_buffered_entries(&mut self) -> Result<()> {
if self.entries_by_table.is_empty() {
return Ok(());
}
for (table, entries) in &self.entries_by_table {
let path = self
.run_dir
.join(format!("{}.trace.sql", sanitize_file_name(table)));
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(&path)
.context(error::CreateFileSnafu {
path: path.to_string_lossy().to_string(),
})?;
for entry in entries {
file.write_all(entry.as_bytes())
.context(error::WriteFileSnafu {
path: path.to_string_lossy().to_string(),
})?;
file.write_all(b"\n").context(error::WriteFileSnafu {
path: path.to_string_lossy().to_string(),
})?;
}
}
self.entries_by_table.clear();
self.buffered_bytes = 0;
Ok(())
}
}
fn format_sql_entry(sql: &str, comment: Option<&str>) -> String {
let normalized_sql = normalize_sql(sql);
if let Some(comment) = comment {
format!("{}\n{normalized_sql}", format_comment(comment))
} else {
normalized_sql
}
}
fn format_comment(comment: &str) -> String {
comment
.lines()
.map(|line| format!("-- {line}"))
.collect::<Vec<_>>()
.join("\n")
}
fn normalize_sql(sql: &str) -> String {
let trimmed = sql.trim_end();
if trimmed.ends_with(';') {
trimmed.to_string()
} else {
format!("{trimmed};")
}
}
fn sanitize_file_name(raw: &str) -> String {
raw.chars()
.map(|ch| {
if ch.is_ascii_alphanumeric() || ch == '_' || ch == '-' {
ch
} else {
'_'
}
})
.collect()
}
#[cfg(test)]
mod tests {
use std::time::{SystemTime, UNIX_EPOCH};
use super::SqlDumpSession;
#[test]
fn test_append_sql_writes_table_trace_file() {
let run_dir = std::env::temp_dir().join(format!(
"tests-fuzz-sql-dump-{}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
));
let mut session = SqlDumpSession::new_with_buffer_limit(run_dir.clone(), 1024).unwrap();
session
.append_sql(
"metric-a",
"INSERT INTO t VALUES(1)",
Some("kind=insert elapsed_ms=10"),
)
.unwrap();
session.flush_all().unwrap();
let content = std::fs::read_to_string(run_dir.join("metric-a.trace.sql")).unwrap();
assert!(content.contains("-- kind=insert elapsed_ms=10"));
assert!(content.contains("INSERT INTO t VALUES(1);"));
}
#[test]
fn test_broadcast_event_writes_to_all_tables() {
let run_dir = std::env::temp_dir().join(format!(
"tests-fuzz-sql-broadcast-{}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
));
let mut session = SqlDumpSession::new_with_buffer_limit(run_dir.clone(), 1024).unwrap();
session
.broadcast_event(
["metric-a", "metric-b"],
"repartition action_idx=3",
"ALTER TABLE t REPARTITION",
)
.unwrap();
session.flush_all().unwrap();
let content_a = std::fs::read_to_string(run_dir.join("metric-a.trace.sql")).unwrap();
let content_b = std::fs::read_to_string(run_dir.join("metric-b.trace.sql")).unwrap();
assert!(content_a.contains("-- repartition action_idx=3"));
assert!(content_a.contains("ALTER TABLE t REPARTITION;"));
assert!(content_b.contains("-- repartition action_idx=3"));
assert!(content_b.contains("ALTER TABLE t REPARTITION;"));
}
#[test]
fn test_multiline_comment_is_prefixed_per_line() {
let run_dir = std::env::temp_dir().join(format!(
"tests-fuzz-sql-dump-comment-{}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
));
let mut session = SqlDumpSession::new_with_buffer_limit(run_dir.clone(), 1024).unwrap();
session
.append_sql(
"metric-a",
"INSERT INTO t VALUES(1)",
Some("kind=insert\nstarted_at_ms=1 elapsed_ms=2"),
)
.unwrap();
session.flush_all().unwrap();
let content = std::fs::read_to_string(run_dir.join("metric-a.trace.sql")).unwrap();
assert!(content.contains("-- kind=insert\n-- started_at_ms=1 elapsed_ms=2"));
}
#[test]
fn test_auto_flush_on_buffer_limit() {
let run_dir = std::env::temp_dir().join(format!(
"tests-fuzz-sql-dump-limit-{}",
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
));
let mut session = SqlDumpSession::new_with_buffer_limit(run_dir.clone(), 1).unwrap();
session
.append_sql("metric-a", "INSERT INTO t VALUES(1)", None)
.unwrap();
assert!(run_dir.join("metric-a.trace.sql").exists());
assert_eq!(session.buffered_bytes, 0);
}
}