test(fuzz): make a duplicated repartition row observable (#10753)

* test(fuzz): make a duplicated repartition row observable

The target validated with COUNT(1) on a table that was not append-mode, so the scan's
last-row dedup collapsed two copies of the same (primary key, time index) before the
count ran: a duplicate was invisible even when the data held it.

Create the table in append mode and add a group-by over the key and time-index columns
with HAVING COUNT(1) > 1, which fails naming the duplicated key. The pre-merge
strict-window compaction stays: it is what makes a second copy reachable.

Signed-off-by: discord9 <discord9@163.com>

* test(fuzz): name the duplicate before the counts panic

The duplicate-row assertion ran after COUNT(1) and COUNT(DISTINCT ts); a duplicate
trips those first, so the query that names the offending key never ran. Move it ahead of
both.

Signed-off-by: discord9 <discord9@163.com>

---------

Signed-off-by: discord9 <discord9@163.com>
This commit is contained in:
discord9
2026-12-01 10:08:12 +00:00
committed by GitHub
parent ba53f68f0c
commit d5f4d711ad
@@ -14,6 +14,7 @@
#![no_main]
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
@@ -24,7 +25,7 @@ use common_time::Timestamp;
use libfuzzer_sys::fuzz_target;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaChaRng;
use snafu::ResultExt;
use snafu::{ResultExt, ensure};
use sqlx::{Executor, MySql, Pool};
use tests_fuzz::context::{TableContext, TableContextRef};
use tests_fuzz::error::{self, Result};
@@ -107,6 +108,10 @@ fn generate_create_expr<R: Rng + 'static>(
.partition(input.partitions)
.partition_column(input.partitions <= 1)
.engine("mito")
.with_clause(HashMap::from([(
"append_mode".to_string(),
"true".to_string(),
)]))
.ts_column_type_generator(Box::new(MySQLTsColumnTypeGenerator))
.build()
.unwrap();
@@ -243,6 +248,36 @@ async fn write_loop<R: Rng + 'static>(
Ok(())
}
/// Fails on any (primary key, time index) row that occurs more than once.
///
/// The table is created in append mode so that the scan does not apply last-row dedup: under
/// dedup two copies of one row collapse before a `COUNT(1)` can see them.
async fn assert_no_duplicate_rows(ctx: &FuzzContext, table_ctx: &TableContextRef) -> Result<()> {
let key_columns = table_ctx
.columns
.iter()
.filter(|column| column.is_primary_key() || column.is_time_index())
.map(|column| column.name.to_string())
.collect::<Vec<_>>()
.join(", ");
let sql = format!(
"SELECT {key_columns}, COUNT(1) AS count FROM {} GROUP BY {key_columns} HAVING COUNT(1) > 1",
table_ctx.name
);
let duplicates = validator::row::fetch_values(&ctx.greptime, &sql).await?;
info!("Validate no duplicated rows: sql={sql}, duplicates={duplicates:?}");
ensure!(
duplicates.is_empty(),
error::AssertSnafu {
reason: format!(
"found duplicated (primary key, time index) rows: sql={sql}, duplicates={duplicates:?}"
)
}
);
Ok(())
}
async fn execute_repartition_table(ctx: FuzzContext, input: FuzzInput) -> Result<()> {
info!("input: {input:?}");
let mut rng = ChaChaRng::seed_from_u64(input.seed);
@@ -362,6 +397,9 @@ async fn execute_repartition_table(ctx: FuzzContext, input: FuzzInput) -> Result
shared_state.lock().unwrap().running = false;
writer_task.await.unwrap().unwrap();
let count_sql = format!("SELECT COUNT(1) AS count FROM {}", table_ctx.name);
// Runs before the count assertions: a duplicate is the interesting failure, and its
// diagnostic names the offending key, while a count mismatch would panic first and hide it.
assert_no_duplicate_rows(&ctx, &table_ctx).await?;
let count = count_values(&ctx.greptime, &count_sql).await?;
assert_eq!(
count.count as usize,