krabka-protocol 0.7.0

Apache Kafka wire-protocol codec (4.3.0), with typed RecordBatch and zero-copy borrowed decode
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
//! `WriteBarrierMarkers`, api key 1014.
//!
//! The coordinator sends this request to a broker that leads a partition of the
//! group. That broker appends one barrier marker to each named partition and
//! returns the offset that each marker took.
//!
//! This is inter-broker traffic. A client never sends it. The fan-out follows
//! the transaction-marker path, and it collects the returned offsets, which the
//! transaction path does not need.

use bytes::{Buf, BufMut};

use crate::{
    Decode, Encode, ProtocolError, ProtocolRequest, UnknownTaggedFields,
    krabka::common::{
        I16_LEN, I32_LEN, I64_LEN, array_len, check_version, decode_array, encode_array,
        read_unknown_tagged_fields, unknown_tagged_fields_len, write_unknown_tagged_fields,
    },
    primitives::{
        fixed::{get_i16, get_i32, get_i64, put_i16, put_i32, put_i64},
        string_bytes::{compact_string_len, get_compact_string_owned, put_compact_string},
    },
};

/// Api key of `WriteBarrierMarkers`.
pub const API_KEY: i16 = 1014;
/// Lowest version of `WriteBarrierMarkers` that this build speaks.
pub const MIN_VERSION: i16 = 0;
/// Highest version of `WriteBarrierMarkers` that this build speaks.
pub const MAX_VERSION: i16 = 0;
/// First version with flexible framing from KIP-482.
pub const FLEXIBLE_MIN: i16 = 0;

/// Reports whether `version` uses flexible framing from KIP-482.
///
/// The request header takes the same framing as the request body, so a caller
/// that writes the header needs this answer.
#[inline]
#[must_use]
pub fn is_flexible(version: i16) -> bool {
    version >= FLEXIBLE_MIN
}

/// One partition for the target broker to mark, and its expected leader epoch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WritableBarrierPartition {
    /// Partition index within the topic.
    pub partition: i32,
    /// Leader epoch that the coordinator read for this partition when it froze
    /// the target set. The target broker compares this epoch against its own
    /// leader epoch, and it rejects a mismatch with `FENCED_LEADER_EPOCH`. `-1`
    /// means the coordinator had no epoch for this partition, and the target
    /// broker does not fence on it.
    pub expected_leader_epoch: i32,
    /// Tagged fields that this build does not know.
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Default for WritableBarrierPartition {
    fn default() -> Self {
        Self {
            partition: 0,
            expected_leader_epoch: -1,
            unknown_tagged_fields: UnknownTaggedFields::default(),
        }
    }
}

impl Encode for WritableBarrierPartition {
    fn encode<B: BufMut>(&self, buf: &mut B, _version: i16) -> Result<(), ProtocolError> {
        put_i32(buf, self.partition);
        put_i32(buf, self.expected_leader_epoch);
        write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
        Ok(())
    }

    fn encoded_len(&self, _version: i16) -> usize {
        I32_LEN + I32_LEN + unknown_tagged_fields_len(&self.unknown_tagged_fields)
    }
}

impl Decode<'_> for WritableBarrierPartition {
    fn decode<B: Buf>(buf: &mut B, _version: i16) -> Result<Self, ProtocolError> {
        Ok(Self {
            partition: get_i32(buf)?,
            expected_leader_epoch: get_i32(buf)?,
            unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
        })
    }
}

/// The partitions of one topic for the target broker to mark.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WritableBarrierTopic {
    /// Topic name.
    pub topic: String,
    /// One entry for each partition that the target broker leads.
    pub partitions: Vec<WritableBarrierPartition>,
    /// Tagged fields that this build does not know.
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Encode for WritableBarrierTopic {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        put_compact_string(buf, &self.topic);
        encode_array(buf, &self.partitions, version)?;
        write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
        Ok(())
    }

    fn encoded_len(&self, version: i16) -> usize {
        compact_string_len(&self.topic)
            + array_len(&self.partitions, version)
            + unknown_tagged_fields_len(&self.unknown_tagged_fields)
    }
}

impl Decode<'_> for WritableBarrierTopic {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        Ok(Self {
            topic: get_compact_string_owned(buf)?,
            partitions: decode_array(buf, version)?,
            unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
        })
    }
}

/// Asks a broker to append one barrier marker to each named partition.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WriteBarrierMarkersRequest {
    /// Name of the barrier group that the epoch belongs to.
    pub group: String,
    /// Epoch that the coordinator assigned to this cut. The marker carries it.
    pub epoch: i64,
    /// Milliseconds since the Unix epoch, at the moment the coordinator started
    /// the injection. The marker carries it.
    pub triggered_at: i64,
    /// Target partitions, grouped by topic.
    pub topics: Vec<WritableBarrierTopic>,
    /// Tagged fields that this build does not know.
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Encode for WriteBarrierMarkersRequest {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
        put_compact_string(buf, &self.group);
        put_i64(buf, self.epoch);
        put_i64(buf, self.triggered_at);
        encode_array(buf, &self.topics, version)?;
        write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
        Ok(())
    }

    fn encoded_len(&self, version: i16) -> usize {
        compact_string_len(&self.group)
            + I64_LEN
            + I64_LEN
            + array_len(&self.topics, version)
            + unknown_tagged_fields_len(&self.unknown_tagged_fields)
    }
}

impl Decode<'_> for WriteBarrierMarkersRequest {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
        Ok(Self {
            group: get_compact_string_owned(buf)?,
            epoch: get_i64(buf)?,
            triggered_at: get_i64(buf)?,
            topics: decode_array(buf, version)?,
            unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
        })
    }
}

impl ProtocolRequest for WriteBarrierMarkersRequest {
    const API_KEY: i16 = API_KEY;
    const MIN_VERSION: i16 = MIN_VERSION;
    const MAX_VERSION: i16 = MAX_VERSION;
    const LATEST_STABLE_VERSION: i16 = MAX_VERSION;
    const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;

    type Response = WriteBarrierMarkersResponse;
}

/// The outcome for one partition that the target broker tried to mark.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WrittenBarrierPartition {
    /// Partition index within the topic.
    pub partition: i32,
    /// Kafka error code. `0` means the append succeeded and `offset` holds the
    /// position of the marker.
    pub error_code: i16,
    /// Offset that the barrier marker took. The coordinator writes this offset
    /// into the cut for this partition.
    pub offset: i64,
    /// Tagged fields that this build does not know.
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Encode for WrittenBarrierPartition {
    fn encode<B: BufMut>(&self, buf: &mut B, _version: i16) -> Result<(), ProtocolError> {
        put_i32(buf, self.partition);
        put_i16(buf, self.error_code);
        put_i64(buf, self.offset);
        write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
        Ok(())
    }

    fn encoded_len(&self, _version: i16) -> usize {
        I32_LEN + I16_LEN + I64_LEN + unknown_tagged_fields_len(&self.unknown_tagged_fields)
    }
}

impl Decode<'_> for WrittenBarrierPartition {
    fn decode<B: Buf>(buf: &mut B, _version: i16) -> Result<Self, ProtocolError> {
        Ok(Self {
            partition: get_i32(buf)?,
            error_code: get_i16(buf)?,
            offset: get_i64(buf)?,
            unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
        })
    }
}

/// The outcomes for the partitions of one topic.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WrittenBarrierTopic {
    /// Topic name.
    pub topic: String,
    /// One outcome for each partition that the request named for this topic.
    pub partitions: Vec<WrittenBarrierPartition>,
    /// Tagged fields that this build does not know.
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Encode for WrittenBarrierTopic {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        put_compact_string(buf, &self.topic);
        encode_array(buf, &self.partitions, version)?;
        write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
        Ok(())
    }

    fn encoded_len(&self, version: i16) -> usize {
        compact_string_len(&self.topic)
            + array_len(&self.partitions, version)
            + unknown_tagged_fields_len(&self.unknown_tagged_fields)
    }
}

impl Decode<'_> for WrittenBarrierTopic {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        Ok(Self {
            topic: get_compact_string_owned(buf)?,
            partitions: decode_array(buf, version)?,
            unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
        })
    }
}

/// The offset that each barrier marker took.
///
/// This response carries no throttle time. It is inter-broker traffic, and the
/// broker applies no client quota to it.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WriteBarrierMarkersResponse {
    /// One entry for each topic that the request named, in request order.
    pub topics: Vec<WrittenBarrierTopic>,
    /// Tagged fields that this build does not know.
    pub unknown_tagged_fields: UnknownTaggedFields,
}

impl Encode for WriteBarrierMarkersResponse {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
        encode_array(buf, &self.topics, version)?;
        write_unknown_tagged_fields(buf, &self.unknown_tagged_fields);
        Ok(())
    }

    fn encoded_len(&self, version: i16) -> usize {
        array_len(&self.topics, version) + unknown_tagged_fields_len(&self.unknown_tagged_fields)
    }
}

impl Decode<'_> for WriteBarrierMarkersResponse {
    fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
        check_version(API_KEY, version, MIN_VERSION, MAX_VERSION)?;
        Ok(Self {
            topics: decode_array(buf, version)?,
            unknown_tagged_fields: read_unknown_tagged_fields(buf)?,
        })
    }
}

#[cfg(test)]
impl WriteBarrierMarkersRequest {
    /// Builds a request with every field set, for the round-trip tests.
    fn populated() -> Self {
        Self {
            group: "orders".to_string(),
            epoch: 41,
            triggered_at: 1_724_500_000_000,
            topics: vec![
                WritableBarrierTopic {
                    topic: "orders".to_string(),
                    partitions: vec![
                        WritableBarrierPartition {
                            partition: 0,
                            expected_leader_epoch: 4,
                            unknown_tagged_fields: UnknownTaggedFields::default(),
                        },
                        WritableBarrierPartition {
                            partition: 3,
                            expected_leader_epoch: -1,
                            unknown_tagged_fields:
                                crate::krabka::test_support::sample_tagged_fields(),
                        },
                        WritableBarrierPartition {
                            partition: 7,
                            expected_leader_epoch: 17,
                            unknown_tagged_fields: UnknownTaggedFields::default(),
                        },
                    ],
                    unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
                },
                WritableBarrierTopic {
                    topic: "payments".to_string(),
                    partitions: Vec::new(),
                    unknown_tagged_fields: UnknownTaggedFields::default(),
                },
            ],
            unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
        }
    }
}

#[cfg(test)]
impl WriteBarrierMarkersResponse {
    /// Builds a response with every field set, for the round-trip tests.
    fn populated() -> Self {
        Self {
            topics: vec![WrittenBarrierTopic {
                topic: "orders".to_string(),
                partitions: vec![
                    WrittenBarrierPartition {
                        partition: 0,
                        error_code: 0,
                        offset: 9_412,
                        unknown_tagged_fields: UnknownTaggedFields::default(),
                    },
                    WrittenBarrierPartition {
                        partition: 3,
                        error_code: 6,
                        offset: -1,
                        unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
                    },
                ],
                unknown_tagged_fields: UnknownTaggedFields::default(),
            }],
            unknown_tagged_fields: crate::krabka::test_support::sample_tagged_fields(),
        }
    }
}

#[cfg(test)]
mod tests {
    use assert2::assert;

    use super::*;
    use crate::krabka::test_support::{reject_truncated, roundtrip, unsupported_versions};

    #[test]
    fn default_roundtrips_all_versions() {
        for version in MIN_VERSION..=MAX_VERSION {
            roundtrip(&WriteBarrierMarkersRequest::default(), version);
            roundtrip(&WriteBarrierMarkersResponse::default(), version);
        }
    }

    #[test]
    fn populated_roundtrips_all_versions() {
        for version in MIN_VERSION..=MAX_VERSION {
            roundtrip(&WriteBarrierMarkersRequest::populated(), version);
            roundtrip(&WriteBarrierMarkersResponse::populated(), version);
        }
    }

    #[test]
    fn no_expected_leader_epoch_is_the_partition_default() {
        assert!(
            WritableBarrierPartition::default()
                == WritableBarrierPartition {
                    partition: 0,
                    expected_leader_epoch: -1,
                    unknown_tagged_fields: UnknownTaggedFields::default(),
                }
        );
    }

    #[test]
    fn truncated_input_is_rejected() {
        reject_truncated(&WriteBarrierMarkersRequest::populated(), MIN_VERSION);
        reject_truncated(&WriteBarrierMarkersResponse::populated(), MIN_VERSION);
    }

    #[test]
    fn rejects_unsupported_versions() {
        for version in unsupported_versions(MIN_VERSION, MAX_VERSION) {
            let request = WriteBarrierMarkersRequest::populated();
            assert!(matches!(
                request.encode(&mut Vec::<u8>::new(), version),
                Err(ProtocolError::UnsupportedVersion {
                    api_key: API_KEY,
                    ..
                })
            ));
        }
    }
}