odradek-protocol 0.1.0

Sans-I/O implementation of the Kafka wire protocol: primitive codecs, message framing, and versioned message types
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
//! Record batch (v2) round-trips, crc verification, and hostile input.

use bytes::{BufMut, Bytes, BytesMut};
use odradek_protocol::DecodeError;
use odradek_protocol::records::{
    Compression, MAGIC, Record, RecordBatch, RecordHeader, Records, crc32c, decode_set, encode_set,
};
use odradek_protocol::wire;

fn sample_batch() -> RecordBatch {
    RecordBatch {
        base_offset: 42,
        partition_leader_epoch: 3,
        attributes: 0,
        last_offset_delta: 1,
        base_timestamp: 1_726_000_000_000,
        max_timestamp: 1_726_000_000_250,
        producer_id: -1,
        producer_epoch: -1,
        base_sequence: -1,
        records: Records::Plain(vec![
            Record {
                attributes: 0,
                timestamp_delta: 0,
                offset_delta: 0,
                key: Some(Bytes::from_static(b"k1")),
                value: Some(Bytes::from_static(b"first value")),
                headers: vec![RecordHeader {
                    key: "trace-id".into(),
                    value: Some(Bytes::from_static(b"abc123")),
                }],
            },
            Record {
                attributes: 0,
                timestamp_delta: 250,
                offset_delta: 1,
                key: None,
                value: None,
                headers: vec![RecordHeader {
                    key: "tombstone-reason".into(),
                    value: None,
                }],
            },
        ]),
    }
}

fn encode(batch: &RecordBatch) -> Bytes {
    let mut buf = BytesMut::new();
    batch.encode(&mut buf).unwrap();
    buf.freeze()
}

#[test]
fn crc32c_known_vectors() {
    // The standard CRC-32C check value, plus the empty string.
    assert_eq!(crc32c(b"123456789"), 0xe306_9283);
    assert_eq!(crc32c(b""), 0);
}

/// The textbook byte-at-a-time CRC-32C, which whichever implementation
/// is compiled — portable slicing-by-8, or the CPU instruction under the
/// `hardware-crc` feature — must agree with on every input.
fn reference_crc32c(data: &[u8]) -> u32 {
    let mut crc = !0u32;
    for &byte in data {
        crc ^= u32::from(byte);
        for _ in 0..8 {
            crc = if crc & 1 != 0 {
                (crc >> 1) ^ 0x82f6_3b78
            } else {
                crc >> 1
            };
        }
    }
    !crc
}

#[test]
fn crc32c_agrees_with_the_bitwise_reference() {
    // Lengths on and around every eight-byte boundary the wide loop
    // cares about, so a mishandled remainder cannot hide.
    let mut state = 0x2545_f491_4f6c_dd1du64;
    let mut next = || {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        u8::try_from((state >> 33) & 0xff).expect("masked to a byte")
    };
    for len in [
        0, 1, 2, 7, 8, 9, 15, 16, 17, 31, 63, 64, 65, 127, 1000, 1023, 1024, 1025, 4097,
    ] {
        let data: Vec<u8> = (0..len).map(|_| next()).collect();
        assert_eq!(
            crc32c(&data),
            reference_crc32c(&data),
            "crc mismatch at {len} bytes"
        );
        // All-zero and all-one runs of the same length too: the tables
        // are indexed by byte value, not just by position.
        let zeros = vec![0u8; len];
        assert_eq!(crc32c(&zeros), reference_crc32c(&zeros), "zeros at {len}");
        let ones = vec![0xffu8; len];
        assert_eq!(crc32c(&ones), reference_crc32c(&ones), "ones at {len}");
    }
}

#[test]
fn roundtrip_plain_batch() {
    let batch = sample_batch();
    let mut bytes = encode(&batch);
    let decoded = RecordBatch::decode(&mut bytes).unwrap();
    assert!(bytes.is_empty());
    assert_eq!(decoded, batch);
    assert_eq!(decoded.compression(), Compression::None);
    assert!(!decoded.is_transactional());
    assert!(!decoded.is_control());
}

#[test]
fn roundtrip_negative_deltas_and_flags() {
    // Delete-horizon batches carry negative timestamp deltas; flag bits
    // and unusual values must survive the trip.
    let batch = RecordBatch {
        attributes: (1 << 4) | (1 << 6), // transactional + delete horizon
        base_timestamp: i64::MAX,
        records: Records::Plain(vec![Record {
            timestamp_delta: -12_345,
            offset_delta: 0,
            value: Some(Bytes::from_static(b"v")),
            ..Default::default()
        }]),
        ..Default::default()
    };
    let mut bytes = encode(&batch);
    let decoded = RecordBatch::decode(&mut bytes).unwrap();
    assert_eq!(decoded, batch);
    assert!(decoded.is_transactional());
    assert!(decoded.has_delete_horizon());
    assert!(!decoded.is_log_append_time());
}

#[test]
fn compressed_payload_roundtrips_byte_identical() {
    // Codec bits set: the payload is opaque — no decompression, and
    // re-encoding reproduces the input exactly (the proxy guarantee).
    // The payload here is not real gzip; it must not matter.
    let batch = RecordBatch {
        attributes: 1, // gzip
        records: Records::Compressed {
            count: 7,
            payload: Bytes::from_static(b"\x1f\x8b-opaque-compressed-noise"),
        },
        ..Default::default()
    };
    let wire = encode(&batch);
    let decoded = RecordBatch::decode(&mut wire.clone()).unwrap();
    assert_eq!(decoded.compression(), Compression::Gzip);
    assert_eq!(decoded, batch);
    assert_eq!(encode(&decoded), wire);
}

#[test]
fn unknown_codec_is_carried_not_rejected() {
    let batch = RecordBatch {
        attributes: 7,
        records: Records::Compressed {
            count: 1,
            payload: Bytes::from_static(b"future-codec-bytes"),
        },
        ..Default::default()
    };
    let wire = encode(&batch);
    let decoded = RecordBatch::decode(&mut wire.clone()).unwrap();
    assert_eq!(decoded.compression(), Compression::Unknown(7));
    assert_eq!(encode(&decoded), wire);
}

#[test]
fn corrupt_byte_fails_crc() {
    let mut wire = BytesMut::from(&encode(&sample_batch())[..]);
    let last = wire.len() - 1;
    wire[last] ^= 0x01;
    match RecordBatch::decode(&mut wire.freeze()) {
        Err(DecodeError::CrcMismatch { .. }) => {}
        other => panic!("expected crc mismatch, got {other:?}"),
    }
}

#[test]
fn wrong_magic_is_rejected() {
    let mut wire = BytesMut::from(&encode(&sample_batch())[..]);
    wire[16] = 1; // magic byte: base_offset(8) + batch_length(4) + epoch(4)
    match RecordBatch::decode(&mut wire.freeze()) {
        Err(DecodeError::UnknownDiscriminant { kind, value }) => {
            assert_eq!(kind, "record batch magic");
            assert_eq!(value, 1);
        }
        other => panic!("expected magic rejection, got {other:?}"),
    }
    assert_eq!(MAGIC, 2);
}

#[test]
fn record_set_roundtrip_and_truncated_tail() {
    let a = sample_batch();
    let b = RecordBatch {
        base_offset: 44,
        ..RecordBatch::default()
    };
    let mut wire = BytesMut::new();
    encode_set(&mut wire, &[a.clone(), b.clone()]).unwrap();

    // Whole set decodes to both batches.
    let full = decode_set(&mut wire.clone().freeze()).unwrap();
    assert_eq!(full, vec![a.clone(), b.clone()]);

    // A fetch-style cut inside the second batch discards it silently.
    let cut = wire.len() - 5;
    let mut truncated = Bytes::copy_from_slice(&wire[..cut]);
    let partial = decode_set(&mut truncated).unwrap();
    assert_eq!(partial, vec![a]);
    assert!(truncated.is_empty());
}

/// A log segment produced by Apache Kafka 4.1.0 (console producer, two
/// keyed records, one batch): on-disk segments hold record batches in
/// exactly the on-wire format, so this is ground truth, crc included.
const KAFKA_SEGMENT_HEX: &str = "00000000000000000000005d00000000028b7c13e00000000000010000\
                                 01a0abfa7edd000001a0abfa7ef600000000000000000000000000000000\
                                 00022a000000046b311a68656c6c6f206f64726164656b002a003202046b\
                                 321a7365636f6e64207265636f726400";

#[test]
fn decodes_real_kafka_segment_and_reencodes_byte_identical() {
    let hex: String = KAFKA_SEGMENT_HEX.split_whitespace().collect();
    let wire: Vec<u8> = (0..hex.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap())
        .collect();
    let wire = Bytes::from(wire);

    let batches = decode_set(&mut wire.clone()).unwrap();
    assert_eq!(batches.len(), 1);
    let batch = &batches[0];
    assert_eq!(batch.base_offset, 0);
    assert_eq!(batch.last_offset_delta, 1);
    assert_eq!(batch.compression(), Compression::None);
    // The console producer is idempotent by default in 4.x: a real
    // producer id, not -1.
    assert_eq!(batch.producer_id, 0);
    assert_eq!(batch.base_sequence, 0);

    let Records::Plain(records) = &batch.records else {
        panic!("expected plain records");
    };
    assert_eq!(records.len(), 2);
    assert_eq!(records[0].key.as_deref(), Some(b"k1".as_slice()));
    assert_eq!(
        records[0].value.as_deref(),
        Some(b"hello odradek".as_slice())
    );
    assert_eq!(records[0].offset_delta, 0);
    assert_eq!(records[1].key.as_deref(), Some(b"k2".as_slice()));
    assert_eq!(
        records[1].value.as_deref(),
        Some(b"second record".as_slice())
    );
    assert_eq!(records[1].offset_delta, 1);
    assert_eq!(
        records[1].timestamp_delta,
        batch.max_timestamp - batch.base_timestamp
    );

    // The proxy guarantee against ground truth: re-encoding a broker's
    // bytes reproduces them exactly, crc and all.
    let mut reencoded = BytesMut::new();
    encode_set(&mut reencoded, &batches).unwrap();
    assert_eq!(reencoded.freeze(), wire);
}

#[test]
fn hostile_prefixes_error_cleanly() {
    // Every prefix of a valid batch must decode to an error, not a panic
    // or a bogus success (except the full batch itself).
    let wire = encode(&sample_batch());
    for cut in 0..wire.len() {
        let mut prefix = wire.slice(..cut);
        assert!(
            RecordBatch::decode(&mut prefix).is_err(),
            "prefix of {cut} bytes decoded successfully"
        );
    }
    // So must a batch_length that lies about the record contents.
    let mut lying = BytesMut::from(&wire[..]);
    lying[60] ^= 0x40; // somewhere inside the first record's bytes
    assert!(RecordBatch::decode(&mut lying.freeze()).is_err());
}

/// A batch whose records exercise every field shape the encoder has to
/// size analytically: absent, empty and present key/value, multi-byte
/// zigzag varints of both signs, a value long enough to need a two-byte
/// length varint, and headers with and without values.
fn wide_batch() -> RecordBatch {
    RecordBatch {
        base_offset: 9_000_000_000,
        partition_leader_epoch: 17,
        attributes: 1 << 3, // log-append time
        last_offset_delta: 4,
        base_timestamp: 1_726_000_000_000,
        max_timestamp: 1_726_000_009_999,
        producer_id: 77,
        producer_epoch: 2,
        base_sequence: 13,
        records: Records::Plain(vec![
            Record::default(),
            Record {
                attributes: -1,
                timestamp_delta: -1_000_000,
                offset_delta: 1,
                key: Some(Bytes::from_static(b"")),
                value: Some(Bytes::from_static(b"")),
                headers: Vec::new(),
            },
            Record {
                attributes: 0,
                timestamp_delta: 9_999,
                offset_delta: 2,
                key: Some(Bytes::from_static(b"k")),
                value: Some(Bytes::from(vec![0x5au8; 300])),
                headers: vec![
                    RecordHeader {
                        key: String::new(),
                        value: None,
                    },
                    RecordHeader {
                        key: "h".into(),
                        value: Some(Bytes::from_static(b"v")),
                    },
                ],
            },
            Record {
                attributes: 0,
                timestamp_delta: i64::MAX,
                offset_delta: 3,
                key: None,
                value: Some(Bytes::from_static(b"tail")),
                // 64 headers: one past the point where the zigzag header
                // count itself needs a second varint byte.
                headers: (0..64)
                    .map(|i| RecordHeader {
                        key: format!("h{i}"),
                        value: (i % 2 == 0).then(|| Bytes::from(vec![u8::try_from(i).unwrap(); 3])),
                    })
                    .collect(),
            },
            Record {
                attributes: 0,
                timestamp_delta: i64::MIN,
                offset_delta: 4,
                key: Some(Bytes::from_static(b"last")),
                value: None,
                headers: Vec::new(),
            },
        ]),
    }
}

/// The record encoder as it was before it sized bodies analytically:
/// build the body in a side buffer, then prefix its measured length.
/// Kept here as the reference the fast path must match byte for byte.
fn reference_encode_record(record: &Record, buf: &mut BytesMut) {
    fn put_bytes(buf: &mut BytesMut, value: Option<&[u8]>) {
        match value {
            Some(v) => {
                wire::put_varint(buf, v.len() as i64);
                buf.put_slice(v);
            }
            None => wire::put_varint(buf, -1),
        }
    }

    let mut body = BytesMut::new();
    body.put_i8(record.attributes);
    wire::put_varint(&mut body, record.timestamp_delta);
    wire::put_varint(&mut body, i64::from(record.offset_delta));
    put_bytes(&mut body, record.key.as_deref());
    put_bytes(&mut body, record.value.as_deref());
    wire::put_varint(&mut body, record.headers.len() as i64);
    for header in &record.headers {
        put_bytes(&mut body, Some(header.key.as_bytes()));
        put_bytes(&mut body, header.value.as_deref());
    }
    wire::put_varint(buf, body.len() as i64);
    buf.put_slice(&body);
}

/// The same for a whole batch: body first, then the prefix whose crc
/// covers it.
fn reference_encode_batch(batch: &RecordBatch, buf: &mut BytesMut) {
    let mut tail = BytesMut::new();
    tail.put_i16(batch.attributes);
    tail.put_i32(batch.last_offset_delta);
    tail.put_i64(batch.base_timestamp);
    tail.put_i64(batch.max_timestamp);
    tail.put_i64(batch.producer_id);
    tail.put_i16(batch.producer_epoch);
    tail.put_i32(batch.base_sequence);
    tail.put_i32(batch.record_count().unwrap());
    match &batch.records {
        Records::Plain(records) => {
            for record in records {
                reference_encode_record(record, &mut tail);
            }
        }
        Records::Compressed { payload, .. } => tail.extend_from_slice(payload),
    }
    buf.put_i64(batch.base_offset);
    buf.put_i32(i32::try_from(4 + 1 + 4 + tail.len()).unwrap());
    buf.put_i32(batch.partition_leader_epoch);
    buf.put_i8(MAGIC);
    buf.put_u32(crc32c(&tail));
    buf.put_slice(&tail);
}

#[test]
fn encoding_is_byte_identical_to_the_side_buffer_reference() {
    // Analytic body lengths are only correct if they agree with the
    // measured ones for every field shape, in every record of a batch.
    for batch in [
        wide_batch(),
        sample_batch(),
        RecordBatch::default(),
        RecordBatch {
            attributes: 2, // snappy: the payload stays opaque
            records: Records::Compressed {
                count: 3,
                payload: Bytes::from_static(b"opaque"),
            },
            ..RecordBatch::default()
        },
    ] {
        let mut expected = BytesMut::new();
        reference_encode_batch(&batch, &mut expected);
        assert_eq!(encode(&batch), expected.clone().freeze(), "generic encode");

        // The in-place encoder must agree too, including when it is
        // appending after bytes that are already in the buffer.
        let mut in_place = BytesMut::from(&b"prefix"[..]);
        batch.encode_to(&mut in_place).unwrap();
        assert_eq!(&in_place[6..], &expected[..], "encode_to");

        // And the round trip still closes.
        let decoded = RecordBatch::decode(&mut expected.freeze()).unwrap();
        assert_eq!(decoded, batch);
    }
}