Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/log_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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,
};
Expand All @@ -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(())
}
Expand Down
107 changes: 107 additions & 0 deletions src/log_chunk_crc32c_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>, 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());
}
}
Loading