kacrab-protocol 0.1.1

Kafka wire protocol types — generated from upstream message schemas by `kacrab-codegen`.
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
//! Record-batch v2 container.
//!
//! Layout (after the 12-byte log overhead):
//!
//! ```text
//! baseOffset (8) | batchLength (4)
//!   ─────── log overhead ────────
//! partitionLeaderEpoch (4) | magic (1) | crc (4)
//!   ─────── CRC-covered region ────────
//!     attributes (2) | lastOffsetDelta (4) | firstTimestamp (8)
//!     maxTimestamp (8) | producerId (8) | producerEpoch (2)
//!     baseSequence (4) | recordCount (4) | records[…]
//! ```

use bytes::{Buf, BufMut, Bytes, BytesMut};

use super::{MAX_RECORDS_PER_BATCH, Record, RecordError, RecordErrorKind, Result};
use crate::{
    compression::Compression,
    crc,
    primitives::{PrimitiveError, PrimitiveErrorKind},
};

const MAGIC_V2: i8 = 2;
const LOG_OVERHEAD: usize = 12;
const BATCH_HEADER_SIZE: i32 = 49;
const BATCH_HEADER_SIZE_USIZE: usize = 49;

/// Kafka timestamp type — derived from bit 3 of the batch `attributes` field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TimestampType {
    /// Producer-assigned timestamp (default).
    CreateTime,
    /// Broker-assigned timestamp at log append.
    LogAppendTime,
}

/// A Kafka record batch (message format v2).
///
/// `batchLength` and `crc` are not stored — they are derived during encode
/// and validated during decode.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordBatch {
    /// Offset of the first record in the batch.
    pub base_offset: i64,
    /// Partition leader epoch (KIP-101).
    pub partition_leader_epoch: i32,
    /// Format version. Always 2 for v2 batches.
    pub magic: i8,
    /// Bit-packed flags: compression (bits 0–2), timestamp type (bit 3),
    /// transactional (bit 4), control (bit 5).
    pub attributes: i16,
    /// Difference between the first and last record offsets.
    pub last_offset_delta: i32,
    /// Wall-clock timestamp of the first record.
    pub first_timestamp: i64,
    /// Wall-clock timestamp of the record with the highest timestamp.
    pub max_timestamp: i64,
    /// Producer ID (idempotent / transactional producer).
    pub producer_id: i64,
    /// Producer epoch (idempotent / transactional producer).
    pub producer_epoch: i16,
    /// First sequence number in the batch (idempotent producer).
    pub base_sequence: i32,
    /// Records in the batch.
    pub records: Vec<Record>,
}

impl RecordBatch {
    /// Encode this batch into `buf`, including the 12-byte log overhead,
    /// CRC32C, and any compression dictated by `self.attributes`.
    pub fn encode(&self, buf: &mut BytesMut) -> Result<()> {
        self.encode_with_compression_level(buf, None)
    }

    /// Encode this batch, passing an optional codec-specific compression level.
    pub fn encode_with_compression_level(
        &self,
        buf: &mut BytesMut,
        compression_level: Option<i32>,
    ) -> Result<()> {
        let codec =
            Compression::from_attributes(self.attributes).map_err(|e| self.lift_compression(e))?;
        if codec == Compression::None {
            let records_payload_len = self.records_encoded_len()?;
            return self.write_batch(buf, records_payload_len, |buf| self.write_records(buf));
        }

        let mut records_buf = BytesMut::with_capacity(self.records_encoded_len()?);
        self.write_records(&mut records_buf)?;
        let records_payload = Bytes::from(
            codec
                .compress_with_level(&records_buf, compression_level)
                .map_err(|e| self.lift_compression(e))?,
        );
        self.write_batch(buf, records_payload.len(), |buf| {
            buf.extend_from_slice(&records_payload);
            Ok(())
        })
    }

    /// Return the exact encoded length when records are written without
    /// compression.
    pub fn uncompressed_encoded_len(&self) -> Result<usize> {
        let len = super::add_encoded_len("record batch", LOG_OVERHEAD, BATCH_HEADER_SIZE_USIZE)?;
        super::add_encoded_len("record batch", len, self.records_encoded_len()?)
    }

    fn records_encoded_len(&self) -> Result<usize> {
        let mut len = 0;
        for record in &self.records {
            len = super::add_encoded_len(
                "record batch records",
                len,
                record
                    .encoded_len()
                    .map_err(|e| RecordError::at_offset(self.base_offset, e.kind))?,
            )?;
        }
        Ok(len)
    }

    fn write_records(&self, buf: &mut BytesMut) -> Result<()> {
        for record in &self.records {
            record
                .encode(buf)
                .map_err(|e| RecordError::at_offset(self.base_offset, e.kind))?;
        }
        Ok(())
    }

    fn write_batch<F>(
        &self,
        buf: &mut BytesMut,
        records_payload_len: usize,
        write_records_payload: F,
    ) -> Result<()>
    where
        F: FnOnce(&mut BytesMut) -> Result<()>,
    {
        buf.put_i64(self.base_offset);

        buf.put_i32(self.batch_length(records_payload_len)?);

        buf.put_i32(self.partition_leader_epoch);
        buf.put_i8(self.magic);

        let crc_field_pos = buf.len();
        buf.put_u32(0);

        let crc_start = buf.len();
        buf.put_i16(self.attributes);
        buf.put_i32(self.last_offset_delta);
        buf.put_i64(self.first_timestamp);
        buf.put_i64(self.max_timestamp);
        buf.put_i64(self.producer_id);
        buf.put_i16(self.producer_epoch);
        buf.put_i32(self.base_sequence);
        buf.put_i32(self.record_count()?);
        write_records_payload(buf)?;

        let crc = crc::crc32c(buf.get(crc_start..).unwrap_or(&[]));
        let crc_bytes = crc.to_be_bytes();
        let crc_field_end = crc_field_pos.checked_add(4).ok_or_else(|| {
            RecordError::at_offset(
                self.base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "crc field",
                    got: crc_field_pos,
                    remaining: buf.len(),
                },
            )
        })?;
        let Some(slot) = buf.get_mut(crc_field_pos..crc_field_end) else {
            return Err(RecordError::at_offset(
                self.base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "crc field",
                    got: crc_field_end,
                    remaining: buf.len(),
                },
            ));
        };
        slot.copy_from_slice(&crc_bytes);

        Ok(())
    }

    fn batch_length(&self, records_payload_len: usize) -> Result<i32> {
        let payload_len = i32::try_from(records_payload_len).map_err(|_| {
            RecordError::at_offset(
                self.base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "compressed records",
                    got: records_payload_len,
                    remaining: usize::try_from(i32::MAX).unwrap_or(usize::MAX),
                },
            )
        })?;
        BATCH_HEADER_SIZE.checked_add(payload_len).ok_or_else(|| {
            RecordError::at_offset(
                self.base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "batch length",
                    got: records_payload_len,
                    remaining: usize::try_from(i32::MAX).unwrap_or(usize::MAX),
                },
            )
        })
    }

    fn record_count(&self) -> Result<i32> {
        i32::try_from(self.records.len()).map_err(|_| {
            RecordError::at_offset(
                self.base_offset,
                RecordErrorKind::RecordCountTooLarge {
                    got: i32::MAX,
                    max: MAX_RECORDS_PER_BATCH,
                },
            )
        })
    }

    /// Decode one batch from `buf`. Validates CRC32C, decompresses if needed,
    /// and rejects a `record_count` above [`super::MAX_RECORDS_PER_BATCH`].
    #[expect(
        clippy::too_many_lines,
        reason = "Record-batch decoding mirrors the Kafka wire layout step-by-step; splitting it \
                  before generated protocol decode is stable would obscure validation order."
    )]
    pub fn decode(buf: &mut Bytes) -> Result<Self> {
        let available = buf.remaining();
        if available < LOG_OVERHEAD {
            return Err(RecordError::unknown_offset(RecordErrorKind::Primitive(
                PrimitiveError::from(PrimitiveErrorKind::InsufficientData {
                    needed: LOG_OVERHEAD,
                    available,
                }),
            )));
        }
        let base_offset = buf.get_i64();
        let batch_length = buf.get_i32();

        if batch_length < BATCH_HEADER_SIZE {
            return Err(RecordError::at_offset(
                base_offset,
                RecordErrorKind::BatchTooSmall {
                    got: batch_length,
                    min: BATCH_HEADER_SIZE,
                },
            ));
        }
        let batch_len = usize::try_from(batch_length).map_err(|_| {
            RecordError::at_offset(
                base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "batch payload",
                    got: usize::MAX,
                    remaining: buf.remaining(),
                },
            )
        })?;
        let remaining = buf.remaining();
        if batch_len > remaining {
            return Err(RecordError::at_offset(
                base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "batch payload",
                    got: batch_len,
                    remaining,
                },
            ));
        }

        let mut batch_data = buf.split_to(batch_len);

        let crc_slice = batch_data.get(5..9).ok_or_else(|| {
            RecordError::at_offset(
                base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "crc field",
                    got: 9,
                    remaining: batch_data.len(),
                },
            )
        })?;
        let crc_bytes: [u8; 4] = crc_slice.try_into().map_err(|_| {
            RecordError::at_offset(
                base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "crc field",
                    got: 4,
                    remaining: crc_slice.len(),
                },
            )
        })?;
        let stored_crc = u32::from_be_bytes(crc_bytes);
        let crc_payload = batch_data.get(9..).unwrap_or(&[]);
        crc::validate_crc32c(crc_payload, stored_crc)
            .map_err(|e| RecordError::at_offset(base_offset, RecordErrorKind::Crc(e)))?;

        let partition_leader_epoch = batch_data.get_i32();
        let magic = batch_data.get_i8();
        if magic != MAGIC_V2 {
            return Err(RecordError::at_offset(
                base_offset,
                RecordErrorKind::UnsupportedMagic(magic),
            ));
        }
        let _crc = batch_data.get_u32();
        let attributes = batch_data.get_i16();
        let last_offset_delta = batch_data.get_i32();
        let first_timestamp = batch_data.get_i64();
        let max_timestamp = batch_data.get_i64();
        let producer_id = batch_data.get_i64();
        let producer_epoch = batch_data.get_i16();
        let base_sequence = batch_data.get_i32();
        let record_count = batch_data.get_i32();

        if record_count < 0 {
            return Err(RecordError::at_offset(
                base_offset,
                RecordErrorKind::NegativeLength {
                    field: "record count",
                    length: record_count,
                },
            ));
        }
        let record_count_usize = usize::try_from(record_count).map_err(|_| {
            RecordError::at_offset(
                base_offset,
                RecordErrorKind::LengthOverflow {
                    field: "record count",
                    got: usize::MAX,
                    remaining: MAX_RECORDS_PER_BATCH,
                },
            )
        })?;
        if record_count_usize > MAX_RECORDS_PER_BATCH {
            return Err(RecordError::at_offset(
                base_offset,
                RecordErrorKind::RecordCountTooLarge {
                    got: record_count,
                    max: MAX_RECORDS_PER_BATCH,
                },
            ));
        }

        let codec = Compression::from_attributes(attributes)
            .map_err(|e| RecordError::at_offset(base_offset, RecordErrorKind::Compression(e)))?;
        let mut records_data = if codec == Compression::None {
            batch_data
        } else {
            let decompressed = codec.decompress(&batch_data).map_err(|e| {
                RecordError::at_offset(base_offset, RecordErrorKind::Compression(e))
            })?;
            Bytes::from(decompressed)
        };

        let mut records = Vec::with_capacity(record_count_usize);
        for _ in 0..record_count {
            let rec = Record::decode(&mut records_data)
                .map_err(|e| RecordError::at_offset(base_offset, e.kind))?;
            records.push(rec);
        }

        Ok(Self {
            base_offset,
            partition_leader_epoch,
            magic,
            attributes,
            last_offset_delta,
            first_timestamp,
            max_timestamp,
            producer_id,
            producer_epoch,
            base_sequence,
            records,
        })
    }

    /// Compression codec selected by bits 0–2 of `attributes`.
    pub fn compression(&self) -> Result<Compression> {
        Compression::from_attributes(self.attributes).map_err(|e| self.lift_compression(e))
    }

    /// Timestamp type from bit 3 of `attributes`.
    #[must_use]
    pub const fn timestamp_type(&self) -> TimestampType {
        if self.attributes & 0x08 != 0 {
            TimestampType::LogAppendTime
        } else {
            TimestampType::CreateTime
        }
    }

    /// `true` if bit 4 of `attributes` is set.
    #[must_use]
    pub const fn is_transactional(&self) -> bool {
        self.attributes & 0x10 != 0
    }

    /// `true` if bit 5 of `attributes` is set.
    #[must_use]
    pub const fn is_control_batch(&self) -> bool {
        self.attributes & 0x20 != 0
    }

    const fn lift_compression(&self, err: crate::compression::CompressionError) -> RecordError {
        RecordError::at_offset(self.base_offset, RecordErrorKind::Compression(err))
    }
}

/// Decode the next complete batch from `buf`, when one is present.
///
/// Returns `Ok(None)` when the buffer is empty or ends in a truncated trailing
/// batch (fetch responses may cut the last batch short) — an error only if a
/// batch is *malformed*, not just incomplete. Lets a consumer decode a fetched
/// blob one batch at a time instead of materializing every record up front.
pub fn decode_next_batch(buf: &mut Bytes) -> Result<Option<RecordBatch>> {
    if buf.remaining() < LOG_OVERHEAD {
        return Ok(None);
    }
    let Some(len_slice) = buf.get(8..12) else {
        return Ok(None);
    };
    let len_bytes: [u8; 4] = match len_slice.try_into() {
        Ok(arr) => arr,
        Err(_) => return Ok(None),
    };
    let batch_length = i32::from_be_bytes(len_bytes);
    if batch_length < 0 {
        return Ok(None);
    }
    let Ok(batch_len) = usize::try_from(batch_length) else {
        return Ok(None);
    };
    let needed = LOG_OVERHEAD.saturating_add(batch_len);
    if buf.remaining() < needed {
        return Ok(None);
    }
    RecordBatch::decode(buf).map(Some)
}

/// Decode every batch in a contiguous buffer.
///
/// Stops cleanly on a truncated trailing batch (returns the batches decoded so
/// far). Returns an error only if a batch is *malformed* — not just incomplete.
pub fn decode_batches(buf: &mut Bytes) -> Result<Vec<RecordBatch>> {
    let mut batches = Vec::new();
    while let Some(batch) = decode_next_batch(buf)? {
        batches.push(batch);
    }
    Ok(batches)
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::expect_used,
        clippy::missing_assert_message,
        reason = "Record-batch encoding tests fail fastest with contextual expect calls."
    )]

    use bytes::{Bytes, BytesMut};

    use super::{Record, RecordBatch};
    use crate::record::RecordHeader;

    #[test]
    fn batch_uncompressed_encoded_len_matches_encoded_bytes() {
        let batch = RecordBatch {
            base_offset: 0,
            partition_leader_epoch: -1,
            magic: 2,
            attributes: 0,
            last_offset_delta: 1,
            first_timestamp: 10,
            max_timestamp: 11,
            producer_id: -1,
            producer_epoch: -1,
            base_sequence: -1,
            records: vec![
                Record {
                    attributes: 0,
                    timestamp_delta: 0,
                    offset_delta: 0,
                    key: Some(Bytes::from_static(b"key-0")),
                    value: Some(Bytes::from_static(b"value-0")),
                    headers: Vec::new(),
                },
                Record {
                    attributes: 0,
                    timestamp_delta: 1,
                    offset_delta: 1,
                    key: None,
                    value: Some(Bytes::from_static(b"value-1")),
                    headers: vec![RecordHeader {
                        key: Bytes::from_static(b"h"),
                        value: None,
                    }],
                },
            ],
        };
        let encoded_len = batch.uncompressed_encoded_len().expect("batch encoded len");
        let mut bytes = BytesMut::with_capacity(encoded_len);

        batch.encode(&mut bytes).expect("batch encode");

        assert_eq!(encoded_len, bytes.len());
        assert_eq!(encoded_len, bytes.capacity());
        let decoded = RecordBatch::decode(&mut bytes.freeze()).expect("record batch decode");
        assert_eq!(decoded.records.len(), 2);
    }
}