T-94: M1.2l — tools/curate-etl: Zstd Parquet writer for fx_1d/part.parquet (contract schema, atomic single-file publish) with round-trip tests and local benchmark (no new crates)
This commit is contained in:
parent
4e93497625
commit
05564bb08b
3 changed files with 180 additions and 5 deletions
|
|
@ -2662,6 +2662,7 @@ struct ParquetFxBenchIter {
|
|||
weekdays: Vec<i32>,
|
||||
te_tail_start_day: i32,
|
||||
pair_count: usize,
|
||||
pair_width: usize,
|
||||
base_per_pair: usize,
|
||||
rem_pairs: usize,
|
||||
cur_pair_idx: usize,
|
||||
|
|
@ -2680,6 +2681,8 @@ impl ParquetFxBenchIter {
|
|||
.max(PARQUET_FX_BENCH_BASE_PAIRS)
|
||||
};
|
||||
let active_pairs = rows.min(min_pairs);
|
||||
let max_synthetic_idx = active_pairs.saturating_sub(2);
|
||||
let pair_width = (max_synthetic_idx.checked_ilog10().unwrap_or(0) as usize + 1).max(4);
|
||||
let base_per_pair = if active_pairs == 0 {
|
||||
0
|
||||
} else {
|
||||
|
|
@ -2695,6 +2698,7 @@ impl ParquetFxBenchIter {
|
|||
weekdays,
|
||||
te_tail_start_day,
|
||||
pair_count: active_pairs,
|
||||
pair_width,
|
||||
base_per_pair,
|
||||
rem_pairs,
|
||||
cur_pair_idx: 0,
|
||||
|
|
@ -2722,10 +2726,14 @@ impl ParquetFxBenchIter {
|
|||
// is strictly ascending and zero-allocation for up to 80 pairs (~1.16M rows).
|
||||
if pair_idx + 1 == self.pair_count {
|
||||
Cow::Borrowed(FX_PAIR_USDCAD)
|
||||
} else if let Some(&static_pair) = BENCH_FX_STATIC_PAIRS.get(pair_idx) {
|
||||
Cow::Borrowed(static_pair)
|
||||
} else if self.pair_width == 4 {
|
||||
if let Some(&static_pair) = BENCH_FX_STATIC_PAIRS.get(pair_idx) {
|
||||
Cow::Borrowed(static_pair)
|
||||
} else {
|
||||
Cow::Owned(format!("FX{pair_idx:04}"))
|
||||
}
|
||||
} else {
|
||||
Cow::Owned(format!("FX{pair_idx:04}"))
|
||||
Cow::Owned(format!("FX{pair_idx:0width$}", width = self.pair_width))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3552,4 +3560,34 @@ mod tests {
|
|||
"error must report expected (255) and actual (250) row counts, got: {msg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parquet_fx_bench_iter_pair_labels_stay_sorted_across_10000_boundary() {
|
||||
let single_day = vec![ymd_to_epoch_days(2024, 1, 2).expect("valid date")];
|
||||
let iter = ParquetFxBenchIter::new(10_002, single_day);
|
||||
assert_eq!(iter.pair_count, 10_002);
|
||||
assert_eq!(iter.pair_width, 5);
|
||||
assert_eq!(iter.pair_label(0).as_ref(), "FX00000");
|
||||
assert_eq!(iter.pair_label(9_999).as_ref(), "FX09999");
|
||||
assert_eq!(iter.pair_label(10_000).as_ref(), "FX10000");
|
||||
assert_eq!(iter.pair_label(10_001).as_ref(), FX_PAIR_USDCAD);
|
||||
assert!(iter.pair_label(9_999).as_ref() < iter.pair_label(10_000).as_ref());
|
||||
assert!(iter.pair_label(10_000).as_ref() < iter.pair_label(10_001).as_ref());
|
||||
|
||||
let mut prev_pair = String::new();
|
||||
let mut count = 0usize;
|
||||
for row in iter {
|
||||
if count > 0 {
|
||||
assert!(
|
||||
prev_pair.as_str() < row.pair.as_ref(),
|
||||
"pair order regressed: {prev_pair} >= {}",
|
||||
row.pair
|
||||
);
|
||||
}
|
||||
prev_pair.clear();
|
||||
prev_pair.push_str(row.pair.as_ref());
|
||||
count += 1;
|
||||
}
|
||||
assert_eq!(count, 10_002);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -646,21 +646,24 @@ where
|
|||
options,
|
||||
DEFAULT_LOCK_TIMEOUT,
|
||||
|_, _| {},
|
||||
|path| fs::remove_file(path),
|
||||
)
|
||||
}
|
||||
|
||||
fn write_fx_1d_dataset_with_hooks<P, I, R, F>(
|
||||
fn write_fx_1d_dataset_with_hooks<P, I, R, F, Rm>(
|
||||
out_dir: P,
|
||||
rows: I,
|
||||
options: Fx1dWriteOptions,
|
||||
lock_timeout: Duration,
|
||||
mut on_after_backup: F,
|
||||
mut remove_backup: Rm,
|
||||
) -> Result<Fx1dWriteStats, ParquetFxError>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
I: IntoIterator<Item = R>,
|
||||
R: Borrow<Fx1dParquetRow>,
|
||||
F: FnMut(&Path, Option<&Path>),
|
||||
Rm: FnMut(&Path) -> io::Result<()>,
|
||||
{
|
||||
if options.row_group_rows == 0 {
|
||||
return Err(ParquetFxError::InvalidRowGroupRows);
|
||||
|
|
@ -804,7 +807,23 @@ where
|
|||
staging_guard.disarm();
|
||||
|
||||
if let Some(bak) = backup_path {
|
||||
let _ = fs::remove_file(bak);
|
||||
if let Err(remove_err) = remove_backup(&bak) {
|
||||
let cause = ParquetFxError::Io {
|
||||
path: bak.clone(),
|
||||
source: remove_err,
|
||||
};
|
||||
return match fs::rename(&bak, &synced.final_path) {
|
||||
Ok(()) => Err(cause),
|
||||
Err(restore_err) => Err(ParquetFxError::RollbackFailed {
|
||||
cause: Box::new(cause),
|
||||
failures: vec![RollbackFailure {
|
||||
final_path: synced.final_path,
|
||||
backup_path: Some(bak),
|
||||
source: restore_err,
|
||||
}],
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Fx1dWriteStats {
|
||||
|
|
|
|||
|
|
@ -766,6 +766,7 @@ fn stale_lock_times_out_leaving_lock_and_existing_part_intact() {
|
|||
Fx1dWriteOptions::default(),
|
||||
Duration::from_millis(30),
|
||||
|_, _| {},
|
||||
|path| fs::remove_file(path),
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
|
|
@ -811,6 +812,7 @@ fn live_part_parquet_stays_readable_between_backup_and_rename() {
|
|||
assert_eq!(fs::read(backup_path.unwrap()).unwrap(), before);
|
||||
*observed_clone.lock().unwrap() = true;
|
||||
},
|
||||
|path| fs::remove_file(path),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
|
@ -971,3 +973,119 @@ fn panicking_iterator_cleans_up_staging_file_and_releases_lock() {
|
|||
assert_row_bitwise_eq(&got[0], &replacement[0]);
|
||||
assert_no_transient_files(&tmp.path().join("fx_1d"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backup_unlink_failure_after_rename_restores_live_file_and_returns_error() {
|
||||
let tmp = TempDirGuard::new("backup-unlink-fail");
|
||||
let (part, before) = seed_existing(tmp.path());
|
||||
|
||||
let replacement = vec![make_row(
|
||||
FX_PAIR_USDCAD,
|
||||
day(2024, 9, 15),
|
||||
1.360,
|
||||
1.365,
|
||||
1.355,
|
||||
1.362,
|
||||
FxSource::TeUsdCad,
|
||||
)];
|
||||
let observed_bak: Arc<Mutex<Option<PathBuf>>> = Arc::new(Mutex::new(None));
|
||||
let observed_bak_clone = Arc::clone(&observed_bak);
|
||||
|
||||
let err = write_fx_1d_dataset_with_hooks(
|
||||
tmp.path(),
|
||||
&replacement,
|
||||
Fx1dWriteOptions::default(),
|
||||
Duration::from_secs(5),
|
||||
|_, _| {},
|
||||
move |bak_path| {
|
||||
*observed_bak_clone.lock().unwrap() = Some(bak_path.to_path_buf());
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"simulated backup unlink failure",
|
||||
))
|
||||
},
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
let expected_bak = observed_bak
|
||||
.lock()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.expect("backup unlink hook must be called");
|
||||
assert!(
|
||||
matches!(
|
||||
err,
|
||||
ParquetFxError::Io { ref path, ref source }
|
||||
if path == &expected_bak
|
||||
&& source.kind() == std::io::ErrorKind::PermissionDenied
|
||||
),
|
||||
"expected Io error on backup path, got {err:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(&part).unwrap(),
|
||||
before,
|
||||
"live part.parquet must be atomically restored from backup"
|
||||
);
|
||||
assert_no_transient_files(&tmp.path().join("fx_1d"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backup_unlink_and_restore_failure_reports_rollback_failed() {
|
||||
let tmp = TempDirGuard::new("backup-rollback-fail");
|
||||
let (part, _before) = seed_existing(tmp.path());
|
||||
|
||||
let replacement = vec![make_row(
|
||||
FX_PAIR_USDCAD,
|
||||
day(2024, 9, 15),
|
||||
1.360,
|
||||
1.365,
|
||||
1.355,
|
||||
1.362,
|
||||
FxSource::TeUsdCad,
|
||||
)];
|
||||
let observed_bak: Arc<Mutex<Option<PathBuf>>> = Arc::new(Mutex::new(None));
|
||||
let observed_bak_clone = Arc::clone(&observed_bak);
|
||||
|
||||
let err = write_fx_1d_dataset_with_hooks(
|
||||
tmp.path(),
|
||||
&replacement,
|
||||
Fx1dWriteOptions::default(),
|
||||
Duration::from_secs(5),
|
||||
|_, _| {},
|
||||
move |bak_path| {
|
||||
*observed_bak_clone.lock().unwrap() = Some(bak_path.to_path_buf());
|
||||
// Remove the backup before returning an error so the restore rename also fails.
|
||||
let _ = fs::remove_file(bak_path);
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"simulated backup unlink failure",
|
||||
))
|
||||
},
|
||||
)
|
||||
.unwrap_err();
|
||||
|
||||
let expected_bak = observed_bak
|
||||
.lock()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.expect("backup unlink hook must be called");
|
||||
match err {
|
||||
ParquetFxError::RollbackFailed { cause, failures } => {
|
||||
assert!(
|
||||
matches!(
|
||||
*cause,
|
||||
ParquetFxError::Io { ref path, ref source }
|
||||
if path == &expected_bak
|
||||
&& source.kind() == std::io::ErrorKind::PermissionDenied
|
||||
),
|
||||
"unexpected cause: {cause:?}"
|
||||
);
|
||||
assert_eq!(failures.len(), 1);
|
||||
assert_eq!(failures[0].final_path, part);
|
||||
assert_eq!(failures[0].backup_path.as_deref(), Some(expected_bak.as_path()));
|
||||
assert_eq!(failures[0].source.kind(), std::io::ErrorKind::NotFound);
|
||||
}
|
||||
other => panic!("expected RollbackFailed, got {other:?}"),
|
||||
}
|
||||
assert_no_transient_files(&tmp.path().join("fx_1d"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue