1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! WAL reader: reads and reassembles records from a WAL file.
use std::fs::File;
use std::io::{BufReader, ErrorKind, Read, Seek};
use std::path::Path;
use crate::error::{Error, Result, ResultExt};
use crate::wal::record::*;
/// WAL reader. Reads records from a WAL file, handling fragmentation.
pub struct WalReader {
reader: BufReader<File>,
/// Current block offset for tracking position within blocks.
block_offset: usize,
/// Whether we've reached EOF.
eof: bool,
/// Byte position of the end of the last successfully read complete record.
/// After iterating all records, this is the safe truncation point for
/// append-after-crash (any bytes beyond this are corrupt/partial).
last_valid_offset: u64,
}
impl WalReader {
/// Open a WAL file for reading.
pub fn new(path: &Path) -> Result<Self> {
let file = File::open(path).ctx()?;
Ok(Self {
reader: BufReader::new(file),
block_offset: 0,
eof: false,
last_valid_offset: 0,
})
}
/// Byte position of the end of the last successfully read complete record.
/// Use this as the truncation point when reopening for append after a crash.
pub fn last_valid_offset(&self) -> u64 {
self.last_valid_offset
}
/// True if every byte from the current read position to EOF is zero.
///
/// After `read_record()` fails, this distinguishes a torn tail from
/// mid-log corruption: a record torn by a crash is the last thing in the
/// file, followed at most by block padding or a filesystem zero-extended
/// tail. Non-zero bytes after the failed record mean data was written
/// after it, so the failure is real corruption and the log suffix would
/// be silently lost by prefix recovery.
///
/// Consumes the remaining bytes; the reader is not usable for further
/// record reads afterwards (`last_valid_offset` is unaffected).
pub fn rest_is_zero_padding(&mut self) -> Result<bool> {
let mut buf = [0u8; 4096];
loop {
match self.reader.read(&mut buf) {
Ok(0) => return Ok(true),
Ok(n) => {
if buf[..n].iter().any(|&b| b != 0) {
return Ok(false);
}
}
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e).ctx(),
}
}
}
/// Return an iterator over all records in the WAL.
#[cfg(test)]
pub fn iter(&mut self) -> WalIterator<'_> {
WalIterator { reader: self }
}
/// Read the next complete record.
///
/// Returns `Ok(Some(data))` for a record, `Ok(None)` at EOF.
pub fn read_record(&mut self) -> Result<Option<Vec<u8>>> {
if self.eof {
return Ok(None);
}
let mut result = Vec::new();
let mut in_fragmented_record = false;
loop {
match self.read_physical_record()? {
None => {
self.eof = true;
if in_fragmented_record {
tracing::warn!("WAL: partial record without end (truncated)");
return Err(Error::corruption(
"partial WAL record without end".to_string(),
));
}
return Ok(None);
}
Some((record_type, data)) => match record_type {
RecordType::Full => {
if in_fragmented_record {
return Err(Error::corruption(
"full record inside fragment".to_string(),
));
}
self.last_valid_offset = self.reader.stream_position().ctx()?;
return Ok(Some(data));
}
RecordType::First => {
if in_fragmented_record {
return Err(Error::corruption(
"first record inside fragment".to_string(),
));
}
in_fragmented_record = true;
result = data;
}
RecordType::Middle => {
if !in_fragmented_record {
return Err(Error::corruption(
"middle record without first".to_string(),
));
}
result.extend_from_slice(&data);
}
RecordType::Last => {
if !in_fragmented_record {
return Err(Error::corruption("last record without first".to_string()));
}
result.extend_from_slice(&data);
self.last_valid_offset = self.reader.stream_position().ctx()?;
return Ok(Some(result));
}
RecordType::Zero => unreachable!("zero records are handled as padding"),
},
}
}
}
/// Read a single physical record (fragment).
/// Returns None at EOF.
fn read_physical_record(&mut self) -> Result<Option<(RecordType, Vec<u8>)>> {
loop {
// Check if we need to skip to the next block
let leftover = BLOCK_SIZE - self.block_offset;
if leftover < HEADER_SIZE {
// Skip the trailer padding. A clean EOF at the trailer start is
// a normal end of file; a partial trailer is a torn tail and is
// surfaced like a partial header, so only the recoverable
// active WAL tolerates it.
if leftover > 0 {
let mut skip = [0u8; HEADER_SIZE];
let mut skipped = 0;
while skipped < leftover {
match self.reader.read(&mut skip[skipped..leftover]) {
Ok(0) if skipped == 0 => return Ok(None),
Ok(0) => {
return Err(Error::corruption(
"truncated WAL block trailer".to_string(),
));
}
Ok(n) => skipped += n,
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e).ctx(),
}
}
}
self.block_offset = 0;
continue;
}
// Read the header. A clean EOF before the first header byte is normal;
// a partial header is a torn tail and must be surfaced to recovery so
// only the active WAL can tolerate it.
let mut header_buf = [0u8; HEADER_SIZE];
let mut header_read = 0;
while header_read < HEADER_SIZE {
match self.reader.read(&mut header_buf[header_read..]) {
Ok(0) if header_read == 0 => return Ok(None),
Ok(0) => {
return Err(Error::corruption("truncated WAL record header".to_string()));
}
Ok(n) => header_read += n,
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e).ctx(),
}
}
let (checksum, length, record_type) = decode_header(&header_buf);
let record_type = match record_type {
Some(rt) => rt,
None => {
return Err(Error::corruption(format!(
"unknown WAL record type: {}",
header_buf[6]
)));
}
};
let length = length as usize;
// Validate that the record payload fits within the current block.
let remaining = BLOCK_SIZE - self.block_offset - HEADER_SIZE;
if length > remaining {
return Err(Error::corruption(format!(
"WAL record length {} exceeds remaining block space {}",
length, remaining
)));
}
// Read the data
let mut data = vec![0u8; length];
match self.reader.read_exact(&mut data) {
Ok(()) => {}
Err(e) if e.kind() == ErrorKind::UnexpectedEof => {
return Err(Error::corruption(
"truncated WAL record payload".to_string(),
));
}
Err(e) => return Err(e).ctx(),
}
self.block_offset += HEADER_SIZE + length;
// All-zero physical headers are block padding. A decoded Zero record
// with any non-zero header field is corruption and must not bypass CRC.
if header_buf == [0u8; HEADER_SIZE] {
continue;
}
if matches!(record_type, RecordType::Zero) {
return Err(Error::corruption("non-padding WAL zero record".to_string()));
}
// Verify checksum
let mut hasher = crc32fast::Hasher::new();
hasher.update(&[record_type as u8]);
hasher.update(&data);
let expected_checksum = hasher.finalize();
if checksum != expected_checksum {
return Err(Error::corruption(format!(
"WAL checksum mismatch: expected {:#x}, got {:#x}",
expected_checksum, checksum
)));
}
return Ok(Some((record_type, data)));
}
}
}
/// Iterator adapter over WAL records (test helper).
#[cfg(test)]
pub struct WalIterator<'a> {
reader: &'a mut WalReader,
}
#[cfg(test)]
impl<'a> Iterator for WalIterator<'a> {
type Item = Result<Vec<u8>>;
fn next(&mut self) -> Option<Self::Item> {
match self.reader.read_record() {
Ok(Some(data)) => Some(Ok(data)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::wal::writer::WalWriter;
use std::io::SeekFrom;
#[test]
fn test_empty_wal() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("empty.wal");
// Create empty file
{
let _writer = WalWriter::new(&path).unwrap();
}
let mut reader = WalReader::new(&path).unwrap();
assert!(reader.read_record().unwrap().is_none());
}
#[test]
fn test_checksum_verification() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("corrupt.wal");
{
let mut writer = WalWriter::new(&path).unwrap();
writer.add_record(b"test data").unwrap();
writer.sync().unwrap();
}
// Corrupt the data portion (after the 7-byte header)
{
use std::io::Write;
let mut file = std::fs::OpenOptions::new().write(true).open(&path).unwrap();
file.seek(SeekFrom::Start(HEADER_SIZE as u64)).unwrap();
file.write_all(b"CORRUPTED").unwrap();
}
let mut reader = WalReader::new(&path).unwrap();
let result = reader.read_record();
assert!(result.is_err());
}
#[test]
fn test_block_trailer_truncation() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("trailer.wal");
// First record ends 6 bytes before the block boundary (< HEADER_SIZE),
// so the writer emits 6 bytes of trailer padding before record two.
let trailer_len = 6u64;
let payload = vec![0x42_u8; BLOCK_SIZE - HEADER_SIZE - trailer_len as usize];
{
let mut writer = WalWriter::new(&path).unwrap();
writer.add_record(&payload).unwrap();
writer.add_record(b"next_block").unwrap();
writer.sync().unwrap();
}
let trailer_start = (BLOCK_SIZE as u64) - trailer_len;
// Truncated mid-trailer: committed records in the next block were
// lost, so this must surface as corruption, not clean EOF.
let file = std::fs::OpenOptions::new().write(true).open(&path).unwrap();
file.set_len(trailer_start + 3).unwrap();
drop(file);
let mut reader = WalReader::new(&path).unwrap();
assert_eq!(reader.read_record().unwrap().unwrap(), payload);
assert!(reader.read_record().is_err());
// Truncated exactly at the trailer start: a legitimate end of file
// (the writer only pads when it is about to append another record).
let file = std::fs::OpenOptions::new().write(true).open(&path).unwrap();
file.set_len(trailer_start).unwrap();
drop(file);
let mut reader = WalReader::new(&path).unwrap();
assert_eq!(reader.read_record().unwrap().unwrap(), payload);
assert!(reader.read_record().unwrap().is_none());
}
}