Skip to main content

iggy_binary_protocol/responses/consumer_groups/
sync_consumer_group.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::WireError;
19use crate::codec::{WireDecode, WireEncode, bounded_capacity, read_u32_le, read_u64_le};
20use bytes::{BufMut, BytesMut};
21
22/// `SyncConsumerGroup` response.
23///
24/// The requesting member's current partition assignment plus the group
25/// generation it belongs to. The client caches this and re-syncs when the
26/// generation advances (rebalance) or a poll is fenced.
27///
28/// Wire format: `[generation:8][partitions_count:4][partition_id:4]*`
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct SyncConsumerGroupResponse {
31    pub generation: u64,
32    pub partitions: Vec<u32>,
33}
34
35impl WireEncode for SyncConsumerGroupResponse {
36    fn encoded_size(&self) -> usize {
37        8 + 4 + self.partitions.len() * 4
38    }
39
40    #[allow(clippy::cast_possible_truncation)]
41    fn encode(&self, buf: &mut BytesMut) {
42        buf.put_u64_le(self.generation);
43        buf.put_u32_le(self.partitions.len() as u32);
44        for &partition_id in &self.partitions {
45            buf.put_u32_le(partition_id);
46        }
47    }
48}
49
50impl WireDecode for SyncConsumerGroupResponse {
51    fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
52        let generation = read_u64_le(buf, 0)?;
53        let partitions_count = read_u32_le(buf, 8)?;
54        let remaining = buf.len().saturating_sub(12);
55        let mut partitions =
56            Vec::with_capacity(bounded_capacity(partitions_count as usize, remaining, 4));
57        let mut offset = 12;
58        for _ in 0..partitions_count {
59            partitions.push(read_u32_le(buf, offset)?);
60            offset += 4;
61        }
62        Ok((
63            Self {
64                generation,
65                partitions,
66            },
67            offset,
68        ))
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn roundtrip() {
78        let response = SyncConsumerGroupResponse {
79            generation: 7,
80            partitions: vec![0, 2, 4],
81        };
82        let bytes = response.to_bytes();
83        let (decoded, consumed) = SyncConsumerGroupResponse::decode(&bytes).unwrap();
84        assert_eq!(consumed, bytes.len());
85        assert_eq!(decoded, response);
86    }
87
88    #[test]
89    fn roundtrip_empty_assignment() {
90        let response = SyncConsumerGroupResponse {
91            generation: 1,
92            partitions: vec![],
93        };
94        let bytes = response.to_bytes();
95        let (decoded, consumed) = SyncConsumerGroupResponse::decode(&bytes).unwrap();
96        assert_eq!(consumed, bytes.len());
97        assert_eq!(decoded, response);
98    }
99
100    #[test]
101    fn truncated_returns_error() {
102        let response = SyncConsumerGroupResponse {
103            generation: 7,
104            partitions: vec![1, 2],
105        };
106        let bytes = response.to_bytes();
107        for i in 0..bytes.len() {
108            assert!(
109                SyncConsumerGroupResponse::decode(&bytes[..i]).is_err(),
110                "expected error for truncation at byte {i}"
111            );
112        }
113    }
114}