diff --git a/tools/curate-etl/src/ilp_tests.rs b/tools/curate-etl/src/ilp_tests.rs index 5ff705ed..775d1686 100644 --- a/tools/curate-etl/src/ilp_tests.rs +++ b/tools/curate-etl/src/ilp_tests.rs @@ -125,6 +125,9 @@ struct RecordingSink { writes: Arc>>>, flush_calls: Arc>, fail_on_write_call: Option, + /// `(call_index, prefix_bytes)`: on `call_index`, accept only `prefix_bytes` and then fail + /// on the subsequent `write()` call inside `write_all()`. + partial_then_fail_on_write_call: Option<(usize, usize)>, fail_on_flush: bool, } @@ -140,6 +143,13 @@ impl RecordingSink { } } + fn failing_after_partial_write(call_index: usize, prefix_bytes: usize) -> Self { + Self { + partial_then_fail_on_write_call: Some((call_index, prefix_bytes)), + ..Self::default() + } + } + fn failing_on_flush() -> Self { Self { fail_on_flush: true, @@ -151,6 +161,15 @@ impl RecordingSink { self.writes.lock().unwrap().clone() } + fn captured_bytes(&self) -> Vec { + self.writes + .lock() + .unwrap() + .iter() + .flat_map(|w| w.iter().copied()) + .collect() + } + fn captured_flushes(&self) -> usize { *self.flush_calls.lock().unwrap() } @@ -166,6 +185,19 @@ impl Write for RecordingSink { format!("injected write failure on call {call_idx}"), )); } + if let Some((partial_call, prefix_bytes)) = self.partial_then_fail_on_write_call { + if call_idx == partial_call { + let n = prefix_bytes.min(buf.len()); + writes.push(buf[..n].to_vec()); + return Ok(n); + } + if call_idx > partial_call { + return Err(io::Error::new( + io::ErrorKind::BrokenPipe, + format!("injected write failure after partial write on call {call_idx}"), + )); + } + } writes.push(buf.to_vec()); Ok(buf.len()) } @@ -763,3 +795,109 @@ fn writer_validates_options_handles_zero_rows_and_poisons_on_io_error() { // Sanity check FNV-1a 64-bit known test vector ("foobar" -> 0x85944171f73967e8). assert_eq!(fnv1a_64(b"foobar"), 0x8594_4171_f739_67e8); } + +#[test] +fn partial_write_failure_poisons_writer_and_freezes_sink_bytes() { + let target_listing = (1..1000) + .find(|&id| shard_for_listing_id(id, 4) == 2) + .expect("listing for shard 2"); + let other_listing = (1..1000) + .find(|&id| shard_for_listing_id(id, 4) == 0) + .expect("listing for shard 0"); + + let row1 = sample_bar(target_listing, 1_700_000_000_000_000); + let row2 = sample_bar(target_listing, 1_700_000_060_000_000); + let other_row = sample_bar(other_listing, 1_700_000_000_000_000); + let known_at = 1_700_000_120_000_000; + + let mut expected_row1 = Vec::new(); + encode_bar_1m(&mut expected_row1, &row1, known_at).unwrap(); + let mut expected_row2 = Vec::new(); + encode_bar_1m(&mut expected_row2, &row2, known_at).unwrap(); + let partial_prefix_len = 32; + assert!(partial_prefix_len < expected_row2.len()); + + let healthy_sink = RecordingSink::new(); + let failing_sink = RecordingSink::failing_after_partial_write(1, partial_prefix_len); + let mut writer = IlpShardedWriter::new( + vec![ + healthy_sink.clone(), + RecordingSink::new(), + failing_sink.clone(), + RecordingSink::new(), + ], + IlpWriterOptions { + max_batch_bytes: 100_000, + max_batch_rows: 1, + }, + ) + .unwrap(); + + // First batch on shard 2 succeeds completely. + writer.write_bar_1m(&row1, known_at).unwrap(); + + // Second batch on shard 2 writes a partial prefix, then hits BrokenPipe. + let err = writer.write_bar_1m(&row2, known_at).unwrap_err(); + assert!(matches!(err, IlpWriterError::Io { shard: 2, .. })); + + let mut expected_captured = expected_row1.clone(); + expected_captured.extend_from_slice(&expected_row2[..partial_prefix_len]); + assert_eq!(failing_sink.captured_bytes(), expected_captured); + + // Subsequent writes (both on the failed shard and on another shard) and finish() are rejected, + // leaving all sink bytes frozen without replaying or appending anything. + assert!(matches!( + writer.write_bar_1m(&row2, known_at), + Err(IlpWriterError::Poisoned { failed_shard: 2 }) + )); + assert!(matches!( + writer.write_bar_1m(&other_row, known_at), + Err(IlpWriterError::Poisoned { failed_shard: 2 }) + )); + assert!(matches!( + writer.write_fx_1m(&sample_fx("USD.CAD", 100)), + Err(IlpWriterError::Poisoned { failed_shard: 2 }) + )); + assert!(matches!( + writer.finish(), + Err(IlpWriterError::Poisoned { failed_shard: 2 }) + )); + + assert_eq!(failing_sink.captured_bytes(), expected_captured); + assert!(healthy_sink.captured_bytes().is_empty()); +} + +#[test] +fn golden_bytes_literal_ieee754_little_endian_without_to_le_bytes() { + let row = Bar1mRow { + key: 7, + ts: 1_700_000_000_000_000, + session_date: 20_000, + open: 1.0, + high: 2.0, + low: 0.5, + close: 1.5, + volume: 0.0, + session: IntradaySession::Rth, + source: BARS_1M_SOURCE_EOD, + }; + let mut actual = Vec::new(); + encode_bar_1m(&mut actual, &row, 1_700_000_060_000_000).unwrap(); + + // Hand-written little-endian IEEE-754 bytes (no f64::to_le_bytes): + // 1.0 -> 00 00 00 00 00 00 f0 3f + // 2.0 -> 00 00 00 00 00 00 00 40 + // 0.5 -> 00 00 00 00 00 00 e0 3f + // 1.5 -> 00 00 00 00 00 00 f8 3f + // 0.0 -> 00 00 00 00 00 00 00 00 + let mut expected: Vec = Vec::new(); + expected.extend_from_slice(b"bars_1m,listing_id=7,session=RTH,source=eod_unadj "); + expected.extend_from_slice(b"open==\x10\x00\x00\x00\x00\x00\x00\xf0\x3f"); + expected.extend_from_slice(b",high==\x10\x00\x00\x00\x00\x00\x00\x00\x40"); + expected.extend_from_slice(b",low==\x10\x00\x00\x00\x00\x00\x00\xe0\x3f"); + expected.extend_from_slice(b",close==\x10\x00\x00\x00\x00\x00\x00\xf8\x3f"); + expected.extend_from_slice(b",volume==\x10\x00\x00\x00\x00\x00\x00\x00\x00"); + expected.extend_from_slice(b",known_at=1700000060000000t 1700000000000000t\n"); + + assert_eq!(actual, expected); +}