kaya-wal 0.1.28

Write-ahead log: CRC32C-protected record codec, append-only writer, crash-safe recovery, and inspector for KayaDB
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use std::fmt;

use kaya_core::{crc32c, Bytes, KayaError, Lsn, Result, SequenceNumber};

pub const WAL_MAGIC: u32 = 0x4b41_5941;
pub const WAL_VERSION: u16 = 1;
pub const WAL_HEADER_LEN: usize = 40;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WalRecordType {
    Put = 1,
    Delete = 2,
    Noop = 3,
}

impl WalRecordType {
    pub fn from_wire(value: u16) -> Option<Self> {
        match value {
            1 => Some(Self::Put),
            2 => Some(Self::Delete),
            3 => Some(Self::Noop),
            _ => None,
        }
    }

    pub const fn as_wire(self) -> u16 {
        self as u16
    }
}

impl fmt::Display for WalRecordType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Put => write!(f, "PUT"),
            Self::Delete => write!(f, "DELETE"),
            Self::Noop => write!(f, "NOOP"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WalPayload {
    Put { key: Bytes, value: Bytes },
    Delete { key: Bytes },
    Noop,
}

impl WalPayload {
    pub const fn record_type(&self) -> WalRecordType {
        match self {
            Self::Put { .. } => WalRecordType::Put,
            Self::Delete { .. } => WalRecordType::Delete,
            Self::Noop => WalRecordType::Noop,
        }
    }

    pub fn key_len(&self) -> Option<usize> {
        match self {
            Self::Put { key, .. } | Self::Delete { key } => Some(key.len()),
            Self::Noop => None,
        }
    }

    pub fn value_len(&self) -> Option<usize> {
        match self {
            Self::Put { value, .. } => Some(value.len()),
            Self::Delete { .. } | Self::Noop => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WalRecord {
    pub flags: u16,
    pub lsn: Lsn,
    pub sequence: SequenceNumber,
    pub payload: WalPayload,
}

impl WalRecord {
    pub fn new(lsn: Lsn, sequence: SequenceNumber, payload: WalPayload) -> Self {
        Self {
            flags: 0,
            lsn,
            sequence,
            payload,
        }
    }

    pub const fn record_type(&self) -> WalRecordType {
        self.payload.record_type()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WalWarning {
    PartialHeader {
        offset: u64,
    },
    PartialPayload {
        offset: u64,
        expected: usize,
        actual: usize,
    },
    BadMagic {
        offset: u64,
        found: u32,
    },
    UnsupportedVersion {
        offset: u64,
        found: u16,
    },
    BadHeaderLength {
        offset: u64,
        found: u16,
    },
    UnknownFlags {
        offset: u64,
        found: u16,
    },
    UnknownRecordType {
        offset: u64,
        found: u16,
    },
    OversizedPayload {
        offset: u64,
        found: u32,
        max: u32,
    },
    BadHeaderChecksum {
        offset: u64,
        expected: u32,
        actual: u32,
    },
    BadPayloadChecksum {
        offset: u64,
        expected: u32,
        actual: u32,
    },
    MalformedPayload {
        offset: u64,
        message: String,
    },
    NonMonotonicLsn {
        offset: u64,
        expected: u64,
        found: u64,
    },
    TailTruncated {
        path: String,
        valid_len: u64,
        truncated_bytes: u64,
    },
    TrailingSegmentsIgnored {
        count: usize,
    },
}

impl fmt::Display for WalWarning {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::PartialHeader { offset } => write!(f, "PartialHeader offset={offset}"),
            Self::PartialPayload {
                offset,
                expected,
                actual,
            } => write!(
                f,
                "PartialPayload offset={offset} expected={expected} actual={actual}"
            ),
            Self::BadMagic { offset, found } => {
                write!(f, "BadMagic offset={offset} found=0x{found:08x}")
            }
            Self::UnsupportedVersion { offset, found } => {
                write!(f, "UnsupportedVersion offset={offset} found={found}")
            }
            Self::BadHeaderLength { offset, found } => {
                write!(f, "BadHeaderLength offset={offset} found={found}")
            }
            Self::UnknownFlags { offset, found } => {
                write!(f, "UnknownFlags offset={offset} found=0x{found:04x}")
            }
            Self::UnknownRecordType { offset, found } => {
                write!(f, "UnknownRecordType offset={offset} found={found}")
            }
            Self::OversizedPayload { offset, found, max } => write!(
                f,
                "OversizedPayload offset={offset} found={found} max={max}"
            ),
            Self::BadHeaderChecksum {
                offset,
                expected,
                actual,
            } => write!(
                f,
                "BadHeaderChecksum offset={offset} expected=0x{expected:08x} actual=0x{actual:08x}"
            ),
            Self::BadPayloadChecksum {
                offset,
                expected,
                actual,
            } => write!(
                f,
                "BadPayloadChecksum offset={offset} expected=0x{expected:08x} actual=0x{actual:08x}"
            ),
            Self::MalformedPayload { offset, message } => {
                write!(f, "MalformedPayload offset={offset} {message}")
            }
            Self::NonMonotonicLsn {
                offset,
                expected,
                found,
            } => write!(
                f,
                "NonMonotonicLsn offset={offset} expected={expected} found={found}"
            ),
            Self::TailTruncated {
                path,
                valid_len,
                truncated_bytes,
            } => write!(
                f,
                "TailTruncated path={path} valid_len={valid_len} truncated_bytes={truncated_bytes}"
            ),
            Self::TrailingSegmentsIgnored { count } => {
                write!(f, "TrailingSegmentsIgnored count={count}")
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodeRecordResult {
    Complete {
        record: WalRecord,
        bytes_read: usize,
    },
    Incomplete {
        warning: WalWarning,
    },
    Invalid {
        warning: WalWarning,
    },
}

pub fn encode_record(record: &WalRecord) -> Result<Vec<u8>> {
    let payload = encode_payload(&record.payload)?;
    let payload_len = u32::try_from(payload.len())
        .map_err(|_| KayaError::invalid_argument("WAL payload length does not fit into u32"))?;
    let payload_crc = crc32c(&payload);

    let mut encoded = Vec::with_capacity(WAL_HEADER_LEN + payload.len());
    encoded.extend_from_slice(&WAL_MAGIC.to_le_bytes());
    encoded.extend_from_slice(&WAL_VERSION.to_le_bytes());
    encoded.extend_from_slice(&(WAL_HEADER_LEN as u16).to_le_bytes());
    encoded.extend_from_slice(&record.flags.to_le_bytes());
    encoded.extend_from_slice(&record.record_type().as_wire().to_le_bytes());
    encoded.extend_from_slice(&record.lsn.get().to_le_bytes());
    encoded.extend_from_slice(&record.sequence.get().to_le_bytes());
    encoded.extend_from_slice(&payload_len.to_le_bytes());
    encoded.extend_from_slice(&0_u32.to_le_bytes());
    encoded.extend_from_slice(&payload_crc.to_le_bytes());

    let header_crc = crc32c(&encoded[..WAL_HEADER_LEN]);
    encoded[32..36].copy_from_slice(&header_crc.to_le_bytes());
    encoded.extend_from_slice(&payload);
    Ok(encoded)
}

pub fn decode_record(input: &[u8], offset: u64, max_payload_len: u32) -> DecodeRecordResult {
    if input.is_empty() {
        return DecodeRecordResult::Incomplete {
            warning: WalWarning::PartialHeader { offset },
        };
    }
    if input.len() < WAL_HEADER_LEN {
        return DecodeRecordResult::Incomplete {
            warning: WalWarning::PartialHeader { offset },
        };
    }

    let magic = read_u32(&input[0..4]);
    if magic != WAL_MAGIC {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::BadMagic {
                offset,
                found: magic,
            },
        };
    }

    let version = read_u16(&input[4..6]);
    if version != WAL_VERSION {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::UnsupportedVersion {
                offset,
                found: version,
            },
        };
    }

    let header_len = read_u16(&input[6..8]);
    if usize::from(header_len) != WAL_HEADER_LEN {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::BadHeaderLength {
                offset,
                found: header_len,
            },
        };
    }

    let flags = read_u16(&input[8..10]);
    if flags != 0 {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::UnknownFlags {
                offset,
                found: flags,
            },
        };
    }

    let record_type_raw = read_u16(&input[10..12]);
    let Some(record_type) = WalRecordType::from_wire(record_type_raw) else {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::UnknownRecordType {
                offset,
                found: record_type_raw,
            },
        };
    };

    let lsn = read_u64(&input[12..20]);
    let sequence = read_u64(&input[20..28]);
    let payload_len = read_u32(&input[28..32]);
    if payload_len > max_payload_len {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::OversizedPayload {
                offset,
                found: payload_len,
                max: max_payload_len,
            },
        };
    }

    let actual_header_crc = read_u32(&input[32..36]);
    let mut header = [0_u8; WAL_HEADER_LEN];
    header.copy_from_slice(&input[..WAL_HEADER_LEN]);
    header[32..36].copy_from_slice(&0_u32.to_le_bytes());
    let expected_header_crc = crc32c(&header);
    if actual_header_crc != expected_header_crc {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::BadHeaderChecksum {
                offset,
                expected: expected_header_crc,
                actual: actual_header_crc,
            },
        };
    }

    let actual_payload_crc = read_u32(&input[36..40]);
    let total_len = WAL_HEADER_LEN + payload_len as usize;
    if input.len() < total_len {
        return DecodeRecordResult::Incomplete {
            warning: WalWarning::PartialPayload {
                offset,
                expected: total_len,
                actual: input.len(),
            },
        };
    }

    let payload_bytes = &input[WAL_HEADER_LEN..total_len];
    let expected_payload_crc = crc32c(payload_bytes);
    if actual_payload_crc != expected_payload_crc {
        return DecodeRecordResult::Invalid {
            warning: WalWarning::BadPayloadChecksum {
                offset,
                expected: expected_payload_crc,
                actual: actual_payload_crc,
            },
        };
    }

    let payload = match decode_payload(record_type, payload_bytes) {
        Ok(payload) => payload,
        Err(error) => {
            return DecodeRecordResult::Invalid {
                warning: WalWarning::MalformedPayload {
                    offset,
                    message: error.to_string(),
                },
            };
        }
    };

    DecodeRecordResult::Complete {
        record: WalRecord {
            flags,
            lsn: Lsn::new(lsn),
            sequence: SequenceNumber::new(sequence),
            payload,
        },
        bytes_read: total_len,
    }
}

fn encode_payload(payload: &WalPayload) -> Result<Vec<u8>> {
    let mut encoded = Vec::new();
    match payload {
        WalPayload::Put { key, value } => {
            let key_len = u32::try_from(key.len()).map_err(|_| {
                KayaError::invalid_argument("WAL PUT key length does not fit into u32")
            })?;
            let value_len = u32::try_from(value.len()).map_err(|_| {
                KayaError::invalid_argument("WAL PUT value length does not fit into u32")
            })?;
            encoded.extend_from_slice(&key_len.to_le_bytes());
            encoded.extend_from_slice(&value_len.to_le_bytes());
            encoded.extend_from_slice(key);
            encoded.extend_from_slice(value);
        }
        WalPayload::Delete { key } => {
            let key_len = u32::try_from(key.len()).map_err(|_| {
                KayaError::invalid_argument("WAL DELETE key length does not fit into u32")
            })?;
            encoded.extend_from_slice(&key_len.to_le_bytes());
            encoded.extend_from_slice(key);
        }
        WalPayload::Noop => {}
    }
    Ok(encoded)
}

fn decode_payload(record_type: WalRecordType, payload: &[u8]) -> Result<WalPayload> {
    match record_type {
        WalRecordType::Put => {
            if payload.len() < 8 {
                return Err(KayaError::corruption("PUT payload header is too short"));
            }
            let key_len = read_u32(&payload[0..4]) as usize;
            let value_len = read_u32(&payload[4..8]) as usize;
            let expected = 8_usize
                .checked_add(key_len)
                .and_then(|len| len.checked_add(value_len))
                .ok_or_else(|| KayaError::corruption("PUT payload length overflows usize"))?;
            if payload.len() != expected {
                return Err(KayaError::corruption(format!(
                    "PUT payload length mismatch: expected {expected}, got {}",
                    payload.len()
                )));
            }
            let key = payload[8..8 + key_len].to_vec();
            let value = payload[8 + key_len..].to_vec();
            Ok(WalPayload::Put { key, value })
        }
        WalRecordType::Delete => {
            if payload.len() < 4 {
                return Err(KayaError::corruption("DELETE payload header is too short"));
            }
            let key_len = read_u32(&payload[0..4]) as usize;
            let expected = 4_usize
                .checked_add(key_len)
                .ok_or_else(|| KayaError::corruption("DELETE payload length overflows usize"))?;
            if payload.len() != expected {
                return Err(KayaError::corruption(format!(
                    "DELETE payload length mismatch: expected {expected}, got {}",
                    payload.len()
                )));
            }
            Ok(WalPayload::Delete {
                key: payload[4..].to_vec(),
            })
        }
        WalRecordType::Noop => {
            if !payload.is_empty() {
                return Err(KayaError::corruption("NOOP payload must be empty"));
            }
            Ok(WalPayload::Noop)
        }
    }
}

fn read_u16(bytes: &[u8]) -> u16 {
    u16::from_le_bytes(bytes.try_into().expect("slice length checked by caller"))
}

fn read_u32(bytes: &[u8]) -> u32 {
    u32::from_le_bytes(bytes.try_into().expect("slice length checked by caller"))
}

fn read_u64(bytes: &[u8]) -> u64 {
    u64::from_le_bytes(bytes.try_into().expect("slice length checked by caller"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn put_roundtrip() {
        let record = WalRecord::new(
            Lsn::new(1),
            SequenceNumber::new(1),
            WalPayload::Put {
                key: b"user:1".to_vec(),
                value: b"Ada".to_vec(),
            },
        );
        let encoded = encode_record(&record).expect("record encodes");
        match decode_record(&encoded, 0, 1024) {
            DecodeRecordResult::Complete {
                record: decoded, ..
            } => assert_eq!(decoded, record),
            other => panic!("unexpected decode result: {other:?}"),
        }
    }

    #[test]
    fn rejects_bad_magic_without_panic() {
        let record = WalRecord::new(Lsn::new(1), SequenceNumber::new(1), WalPayload::Noop);
        let mut encoded = encode_record(&record).expect("record encodes");
        encoded[0] = 0;
        match decode_record(&encoded, 0, 1024) {
            DecodeRecordResult::Invalid {
                warning: WalWarning::BadMagic { .. },
            } => {}
            other => panic!("unexpected decode result: {other:?}"),
        }
    }
}