From be52158683b29111c58e4cedd292fd9f11b8ea9f Mon Sep 17 00:00:00 2001 From: bjmeetsfo Date: Mon, 23 Nov 2026 09:03:19 -0800 Subject: [PATCH] Accelerate chunk append checksums --- .github/workflows/ci.yml | 5 ++ src/log_chunk.rs | 6 +- src/log_chunk_crc32c_tests.rs | 107 ++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2c0c771f..a79bc968 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2333,6 +2333,11 @@ jobs: cargo test --locked --lib chunk_writer_outcome_tests -- --test-threads=1 | tee /tmp/unified-writer-outcomes.log grep -q '^running 18 tests$' /tmp/unified-writer-outcomes.log grep -q '^test result: ok. 18 passed' /tmp/unified-writer-outcomes.log + - name: append checksum byte compatibility + run: | + set -euo pipefail + cargo test --locked --lib log_chunk::crc32c_tests -- --test-threads=1 2>&1 | tee /tmp/append-crc.log + grep -q '^test result: ok. 3 passed' /tmp/append-crc.log - name: borrowed LOG record writes handle short and interrupted IO run: | set -euo pipefail diff --git a/src/log_chunk.rs b/src/log_chunk.rs index df018990..69afa789 100644 --- a/src/log_chunk.rs +++ b/src/log_chunk.rs @@ -143,7 +143,7 @@ pub fn crc32c_update(crc: u32, data: &[u8]) -> u32 { } !state } -// Dispatch only while validating persistent bytes; append-time checksums stay portable. +// Runtime acceleration preserves the CRC-32C byte contract, with a portable fallback. fn crc32c_update_stored(crc: u32, data: &[u8]) -> u32 { #[cfg(target_arch = "x86_64")] if data.len() >= 8 && std::arch::is_x86_feature_detected!("sse4.2") { @@ -1402,7 +1402,7 @@ impl LogChunk { epoch, offset, length: data.len() as u32, - data_crc: crc32c(data), + data_crc: crc32c_stored_record(data), attempt_tag, published_length, }; @@ -1421,7 +1421,7 @@ impl LogChunk { self.length += data.len() as u64; self.published_length = published; self.publication_mode |= published_length.is_some(); - self.crc = crc32c_update(self.crc, data); + self.crc = crc32c_update_stored(self.crc, data); self.epoch = epoch; Ok(()) } diff --git a/src/log_chunk_crc32c_tests.rs b/src/log_chunk_crc32c_tests.rs index d90303b9..f30aac3b 100644 --- a/src/log_chunk_crc32c_tests.rs +++ b/src/log_chunk_crc32c_tests.rs @@ -88,3 +88,110 @@ fn measure_crc32c_update_backends() { } } } + +#[test] +fn append_checksums_preserve_existing_journal_bytes_and_sealed_recovery() { + use super::{Header, LogChunk, KIND_APPEND, KIND_INITIALIZE, KIND_SEAL}; + let root = std::env::temp_dir().join(format!("append-crc-bytes-{}", std::process::id())); + std::fs::create_dir(&root).unwrap(); + struct Cleanup(std::path::PathBuf); + impl Drop for Cleanup { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let _cleanup = Cleanup(root.clone()); + let data = input(65536 + 16); + for mode in 0..3 { + let path = root.join(format!("mode-{mode}.log")); + let mut chunk = LogChunk::open(&path, 7, 1 << 20).unwrap(); + let mut expected = Vec::new(); + let mut all = Vec::new(); + let add_header = |out: &mut Vec, header: Header| { + out.extend_from_slice(&header.encode()[..header.encoded_len()]); + }; + if mode == 2 { + chunk.initialize_publication(11, [9; 16]).unwrap(); + add_header( + &mut expected, + Header { + kind: KIND_INITIALIZE, + flags: 0, + generation: 7, + epoch: 11, + offset: 0, + length: 0, + data_crc: 0, + attempt_tag: Some([9; 16]), + published_length: Some(0), + }, + ); + } + for (i, length) in [1usize, 7, 8, 9, 4095, 4096, 4097, 65535, 65536] + .into_iter() + .enumerate() + { + let bytes = &data[i..i + length]; + let offset = all.len() as u64; + let end = offset + length as u64; + let tag = [i as u8 + 1; 16]; + match mode { + 0 => { + chunk.append(11, offset, bytes).unwrap(); + } + 1 => { + chunk.append_identified(11, offset, bytes, tag).unwrap(); + } + _ => { + chunk.append_published(11, offset, bytes, tag, end).unwrap(); + } + } + add_header( + &mut expected, + Header { + kind: KIND_APPEND, + flags: 0, + generation: 7, + epoch: 11, + offset, + length: length as u32, + data_crc: crc32c_update_portable(0, bytes), + attempt_tag: (mode != 0).then_some(tag), + published_length: (mode == 2).then_some(end), + }, + ); + expected.extend_from_slice(bytes); + all.extend_from_slice(bytes); + assert_eq!(chunk.footer_crc(), crc32c_update_portable(0, &all)); + } + if mode == 2 { + chunk.seal_publication(11, false).unwrap(); + } else { + chunk.seal(11, false).unwrap(); + } + add_header( + &mut expected, + Header { + kind: KIND_SEAL, + flags: 0, + generation: 7, + epoch: 11, + offset: all.len() as u64, + length: 0, + data_crc: crc32c_update_portable(0, &all), + attempt_tag: None, + published_length: None, + }, + ); + drop(chunk); + assert_eq!( + std::fs::read(&path).unwrap(), + expected, + "mode{mode} changed persisted bytes" + ); + let recovered = LogChunk::open(&path, 7, 1 << 20).unwrap(); + assert_eq!(recovered.durable_length(), all.len() as u64); + assert_eq!(recovered.footer_crc(), crc32c_update_portable(0, &all)); + assert!(recovered.sealed().is_some()); + } +}