iggy_binary_protocol 0.11.1-edge.3

Wire protocol types and codec for the Iggy binary protocol. Shared between server and SDK.
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! Wire codes, record and item bodies, the change cursor and the current
//! state page token of the metadata change log.
//!
//! A record body is encoded once on the server and copied verbatim into every
//! answer, so decoding is strict: unknown reason codes, a known kind under the
//! wrong group, presence bytes other than 0 and 1 and trailing bytes are all
//! refused. A kind or group this build does not know is not an error, the
//! caller keeps it with its raw body.

mod body;
mod position;

pub use body::{
    WireChangeOptions, WireConsumerGroupCreatedBody, WireConsumerGroupDeletedBody,
    WireConsumerGroupItemBody, WireMemberItemBody, WireMemberJoinedBody, WireMemberLeftBody,
    WireMetadataChange, WireMetadataCurrentItem, WirePartitionHandedOverBody,
    WirePartitionsChangedBody, WirePartitionsItemBody, WireSessionBody, WireSessionClosedBody,
    WireStreamBody, WireStreamDeletedBody, WireStreamKey, WireTokenBody, WireTokenDeletedBody,
    WireTopicBody, WireTopicDeletedBody, WireTopicItemBody, WireTopicKey, WireUserBody,
    WireUserIdentityBody, WireUserKey, WireUserPermissionsBody,
};
pub use position::{
    MAX_PAGE_TOKEN_KEY_SIZE, MAX_PAGE_TOKEN_SIZE, METADATA_CHANGE_CURSOR_SIZE,
    WireMetadataChangeCursor, WireMetadataCurrentPageToken,
};

use crate::WireDecode;
use crate::WireError;
use crate::codec::{bounded_capacity, read_u8, read_u32_le, read_u64_le};
use crate::primitives::identifier::WireName;
use bytes::{BufMut, BytesMut};
use std::borrow::Cow;

macro_rules! wire_code_enum {
    (
        $(#[$meta:meta])*
        $name:ident {
            $($(#[$variant_meta:meta])* $variant:ident = $code:literal),+ $(,)?
        }
    ) => {
        $(#[$meta])*
        #[repr(u8)]
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
        pub enum $name {
            $($(#[$variant_meta])* $variant = $code),+
        }

        impl $name {
            /// Decode from the wire code.
            ///
            /// # Errors
            /// Returns `WireError::UnknownDiscriminant` for a code this build
            /// does not know.
            pub const fn from_code(code: u8) -> Result<Self, WireError> {
                match code {
                    $($code => Ok(Self::$variant),)+
                    value => Err(WireError::UnknownDiscriminant {
                        type_name: stringify!($name),
                        value,
                        offset: 0,
                    }),
                }
            }

            /// The wire code.
            #[must_use]
            pub const fn as_u8(self) -> u8 {
                self as u8
            }
        }
    };
}

wire_code_enum! {
    /// Family of change records and the unit of filtering. Codes 9 to 127 are
    /// reserved for future groups, 128 to 255 for downstream extensions.
    ///
    /// A page token holds the groups in an 8 bit mask, so a ninth group needs
    /// a new token format.
    WireMetadataChangeGroup {
        Streams = 1,
        Topics = 2,
        Partitions = 3,
        ConsumerGroups = 4,
        ConsumerGroupMembers = 5,
        Users = 6,
        PersonalAccessTokens = 7,
        Sessions = 8,
    }
}

impl WireMetadataChangeGroup {
    /// Bit of this group in a page token's group mask.
    #[must_use]
    pub const fn mask_bit(self) -> u8 {
        1 << (self as u8 - 1)
    }
}

wire_code_enum! {
    /// Kind of a change record. Existing kind bodies never change in place, a
    /// new layout is a new kind.
    WireMetadataChangeKind {
        StreamCreated = 1,
        StreamUpdated = 2,
        StreamDeleted = 3,
        TopicCreated = 10,
        TopicUpdated = 11,
        TopicDeleted = 12,
        PartitionsChanged = 20,
        ConsumerGroupCreated = 30,
        ConsumerGroupDeleted = 31,
        ConsumerGroupMemberJoined = 40,
        ConsumerGroupMemberLeft = 41,
        ConsumerGroupPartitionHandedOver = 42,
        UserCreated = 50,
        UserUpdated = 51,
        UserDeleted = 52,
        UserPasswordChanged = 53,
        UserPermissionsUpdated = 54,
        PersonalAccessTokenCreated = 60,
        PersonalAccessTokenDeleted = 61,
        SessionRegistered = 70,
        SessionClosed = 71,
    }
}

impl WireMetadataChangeKind {
    /// The group every record of this kind belongs to.
    #[must_use]
    pub const fn group(self) -> WireMetadataChangeGroup {
        match self {
            Self::StreamCreated | Self::StreamUpdated | Self::StreamDeleted => {
                WireMetadataChangeGroup::Streams
            }
            Self::TopicCreated | Self::TopicUpdated | Self::TopicDeleted => {
                WireMetadataChangeGroup::Topics
            }
            Self::PartitionsChanged => WireMetadataChangeGroup::Partitions,
            Self::ConsumerGroupCreated | Self::ConsumerGroupDeleted => {
                WireMetadataChangeGroup::ConsumerGroups
            }
            Self::ConsumerGroupMemberJoined
            | Self::ConsumerGroupMemberLeft
            | Self::ConsumerGroupPartitionHandedOver => {
                WireMetadataChangeGroup::ConsumerGroupMembers
            }
            Self::UserCreated
            | Self::UserUpdated
            | Self::UserDeleted
            | Self::UserPasswordChanged
            | Self::UserPermissionsUpdated => WireMetadataChangeGroup::Users,
            Self::PersonalAccessTokenCreated | Self::PersonalAccessTokenDeleted => {
                WireMetadataChangeGroup::PersonalAccessTokens
            }
            Self::SessionRegistered | Self::SessionClosed => WireMetadataChangeGroup::Sessions,
        }
    }
}

wire_code_enum! {
    /// Why a consumer group member was removed.
    WireMemberLeftReason {
        Left = 0,
        SessionEnded = 1,
    }
}

wire_code_enum! {
    /// Why a personal access token was removed.
    WireTokenDeletedReason {
        Deleted = 0,
        Expired = 1,
    }
}

wire_code_enum! {
    /// Why a session was closed.
    WireSessionClosedReason {
        Logout = 0,
        Expired = 1,
        /// The user was deactivated or deleted.
        Revoked = 2,
    }
}

wire_code_enum! {
    /// Why a user's public permission set changed.
    WirePermissionsUpdatedReason {
        Updated = 0,
        ResourceDeleted = 1,
    }
}

wire_code_enum! {
    /// Outcome of a change log query.
    WireMetadataChangeStatus {
        Ok = 0,
        Behind = 1,
        Expired = 2,
        Reset = 3,
    }
}

wire_code_enum! {
    /// Whether an answer holds change records or current state items.
    WireMetadataChangePhase {
        Changes = 0,
        Current = 1,
    }
}

wire_code_enum! {
    /// Where a change log query starts.
    WireMetadataChangeStartMode {
        Earliest = 0,
        Latest = 1,
        After = 2,
        Current = 3,
    }
}

/// Read a presence or boolean byte, refusing anything but 0 and 1.
pub(crate) fn read_flag(buf: &[u8], offset: usize) -> Result<bool, WireError> {
    match read_u8(buf, offset)? {
        0 => Ok(false),
        1 => Ok(true),
        value => Err(WireError::UnknownDiscriminant {
            type_name: "flag",
            value,
            offset,
        }),
    }
}

pub(crate) fn put_flag(buf: &mut BytesMut, value: bool) {
    buf.put_u8(u8::from(value));
}

pub(crate) fn read_wire_name(buf: &[u8], offset: usize) -> Result<(WireName, usize), WireError> {
    let rest = buf.get(offset..).ok_or(WireError::UnexpectedEof {
        offset,
        need: 1,
        have: 0,
    })?;
    WireName::decode(rest)
}

pub(crate) const fn optional_u64_size(value: Option<u64>) -> usize {
    if value.is_some() { 9 } else { 1 }
}

pub(crate) fn put_optional_u64(buf: &mut BytesMut, value: Option<u64>) {
    put_flag(buf, value.is_some());
    if let Some(value) = value {
        buf.put_u64_le(value);
    }
}

pub(crate) fn read_optional_u64(
    buf: &[u8],
    offset: usize,
) -> Result<(Option<u64>, usize), WireError> {
    if read_flag(buf, offset)? {
        Ok((Some(read_u64_le(buf, offset + 1)?), 9))
    } else {
        Ok((None, 1))
    }
}

pub(crate) const fn optional_u32_size(value: Option<u32>) -> usize {
    if value.is_some() { 5 } else { 1 }
}

pub(crate) fn put_optional_u32(buf: &mut BytesMut, value: Option<u32>) {
    put_flag(buf, value.is_some());
    if let Some(value) = value {
        buf.put_u32_le(value);
    }
}

pub(crate) fn read_optional_u32(
    buf: &[u8],
    offset: usize,
) -> Result<(Option<u32>, usize), WireError> {
    if read_flag(buf, offset)? {
        Ok((Some(read_u32_le(buf, offset + 1)?), 5))
    } else {
        Ok((None, 1))
    }
}

pub(crate) const fn partition_ids_size(ids: &[u32]) -> usize {
    4 + ids.len() * 4
}

/// Encode `[count:u32][id:u32]*`. Callers hold ascending distinct ids.
pub(crate) fn put_partition_ids(buf: &mut BytesMut, ids: &[u32]) {
    // A topic holds at most `u32::MAX` partitions, so the count fits.
    #[allow(clippy::cast_possible_truncation)]
    buf.put_u32_le(ids.len() as u32);
    for &id in ids {
        buf.put_u32_le(id);
    }
}

/// Decode `[count:u32][id:u32]*`, refusing ids that are not strictly
/// ascending.
pub(crate) fn read_partition_ids(
    buf: &[u8],
    offset: usize,
) -> Result<(Vec<u32>, usize), WireError> {
    let count = read_u32_le(buf, offset)? as usize;
    let mut position = offset + 4;
    let remaining = buf.len().saturating_sub(position);
    let mut ids = Vec::with_capacity(bounded_capacity(count, remaining, 4));
    for _ in 0..count {
        let id = read_u32_le(buf, position)?;
        if ids.last().is_some_and(|&previous| previous >= id) {
            return Err(WireError::Validation(Cow::Borrowed(
                "partition ids must be strictly ascending",
            )));
        }
        ids.push(id);
        position += 4;
    }
    Ok((ids, position - offset))
}

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

    #[test]
    fn given_every_group_when_decoding_its_code_should_round_trip_and_set_one_mask_bit() {
        let mut mask = 0u8;
        for code in 1..=8u8 {
            let group = WireMetadataChangeGroup::from_code(code).unwrap();
            assert_eq!(group.as_u8(), code);
            assert_eq!(mask & group.mask_bit(), 0);
            mask |= group.mask_bit();
        }
        assert_eq!(mask, u8::MAX);
    }

    #[test]
    fn given_reserved_group_codes_when_decoding_should_refuse() {
        for code in [0u8, 9, 127, 128, 255] {
            assert!(WireMetadataChangeGroup::from_code(code).is_err());
        }
    }

    #[test]
    fn given_every_kind_when_decoding_its_code_should_round_trip() {
        let codes = [
            1u8, 2, 3, 10, 11, 12, 20, 30, 31, 40, 41, 42, 50, 51, 52, 53, 54, 60, 61, 70, 71,
        ];
        for code in codes {
            let kind = WireMetadataChangeKind::from_code(code).unwrap();
            assert_eq!(kind.as_u8(), code);
        }
        for code in 0..=u8::MAX {
            assert_eq!(
                WireMetadataChangeKind::from_code(code).is_ok(),
                codes.contains(&code)
            );
        }
    }

    #[test]
    fn given_kind_codes_when_mapping_groups_should_follow_the_catalog_decades() {
        for code in 0..=u8::MAX {
            if let Ok(kind) = WireMetadataChangeKind::from_code(code) {
                let expected = match code {
                    1..=3 => 1,
                    10..=12 => 2,
                    20 => 3,
                    30 | 31 => 4,
                    40..=42 => 5,
                    50..=54 => 6,
                    60 | 61 => 7,
                    _ => 8,
                };
                assert_eq!(kind.group().as_u8(), expected, "kind {code}");
            }
        }
    }

    #[test]
    fn given_unknown_reason_codes_when_decoding_should_refuse() {
        assert!(WireMemberLeftReason::from_code(2).is_err());
        assert!(WireTokenDeletedReason::from_code(2).is_err());
        assert!(WireSessionClosedReason::from_code(3).is_err());
        assert!(WirePermissionsUpdatedReason::from_code(2).is_err());
        assert!(WireMetadataChangeStatus::from_code(4).is_err());
        assert!(WireMetadataChangePhase::from_code(2).is_err());
        assert!(WireMetadataChangeStartMode::from_code(4).is_err());
    }

    #[test]
    fn given_flag_byte_above_one_when_reading_should_refuse() {
        assert!(!read_flag(&[0], 0).unwrap());
        assert!(read_flag(&[1], 0).unwrap());
        assert!(read_flag(&[2], 0).is_err());
        assert!(read_flag(&[], 0).is_err());
    }

    #[test]
    fn given_partition_ids_when_round_tripping_should_preserve_order() {
        let ids = [0u32, 3, 7, u32::MAX];
        let mut buf = BytesMut::new();
        put_partition_ids(&mut buf, &ids);
        assert_eq!(buf.len(), partition_ids_size(&ids));
        let (decoded, consumed) = read_partition_ids(&buf, 0).unwrap();
        assert_eq!(decoded, ids);
        assert_eq!(consumed, buf.len());
    }

    #[test]
    fn given_unsorted_or_duplicate_partition_ids_when_decoding_should_refuse() {
        for ids in [[3u32, 1], [2, 2]] {
            let mut buf = BytesMut::new();
            buf.put_u32_le(2);
            for id in ids {
                buf.put_u32_le(id);
            }
            assert!(read_partition_ids(&buf, 0).is_err());
        }
    }

    #[test]
    fn given_huge_partition_count_when_decoding_should_fail_without_allocating_it() {
        let mut buf = BytesMut::new();
        buf.put_u32_le(u32::MAX);
        assert!(read_partition_ids(&buf, 0).is_err());
    }
}