kafka_protocol/messages/
broker_heartbeat_request.rs

1//! BrokerHeartbeatRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/BrokerHeartbeatRequest.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15    buf::{ByteBuf, ByteBufMut},
16    compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17    Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20/// Valid versions: 0-1
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct BrokerHeartbeatRequest {
24    /// The broker ID.
25    ///
26    /// Supported API versions: 0-1
27    pub broker_id: super::BrokerId,
28
29    /// The broker epoch.
30    ///
31    /// Supported API versions: 0-1
32    pub broker_epoch: i64,
33
34    /// The highest metadata offset which the broker has reached.
35    ///
36    /// Supported API versions: 0-1
37    pub current_metadata_offset: i64,
38
39    /// True if the broker wants to be fenced, false otherwise.
40    ///
41    /// Supported API versions: 0-1
42    pub want_fence: bool,
43
44    /// True if the broker wants to be shut down, false otherwise.
45    ///
46    /// Supported API versions: 0-1
47    pub want_shut_down: bool,
48
49    /// Log directories that failed and went offline.
50    ///
51    /// Supported API versions: 1
52    pub offline_log_dirs: Vec<Uuid>,
53
54    /// Other tagged fields
55    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
56}
57
58impl BrokerHeartbeatRequest {
59    /// Sets `broker_id` to the passed value.
60    ///
61    /// The broker ID.
62    ///
63    /// Supported API versions: 0-1
64    pub fn with_broker_id(mut self, value: super::BrokerId) -> Self {
65        self.broker_id = value;
66        self
67    }
68    /// Sets `broker_epoch` to the passed value.
69    ///
70    /// The broker epoch.
71    ///
72    /// Supported API versions: 0-1
73    pub fn with_broker_epoch(mut self, value: i64) -> Self {
74        self.broker_epoch = value;
75        self
76    }
77    /// Sets `current_metadata_offset` to the passed value.
78    ///
79    /// The highest metadata offset which the broker has reached.
80    ///
81    /// Supported API versions: 0-1
82    pub fn with_current_metadata_offset(mut self, value: i64) -> Self {
83        self.current_metadata_offset = value;
84        self
85    }
86    /// Sets `want_fence` to the passed value.
87    ///
88    /// True if the broker wants to be fenced, false otherwise.
89    ///
90    /// Supported API versions: 0-1
91    pub fn with_want_fence(mut self, value: bool) -> Self {
92        self.want_fence = value;
93        self
94    }
95    /// Sets `want_shut_down` to the passed value.
96    ///
97    /// True if the broker wants to be shut down, false otherwise.
98    ///
99    /// Supported API versions: 0-1
100    pub fn with_want_shut_down(mut self, value: bool) -> Self {
101        self.want_shut_down = value;
102        self
103    }
104    /// Sets `offline_log_dirs` to the passed value.
105    ///
106    /// Log directories that failed and went offline.
107    ///
108    /// Supported API versions: 1
109    pub fn with_offline_log_dirs(mut self, value: Vec<Uuid>) -> Self {
110        self.offline_log_dirs = value;
111        self
112    }
113    /// Sets unknown_tagged_fields to the passed value.
114    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
115        self.unknown_tagged_fields = value;
116        self
117    }
118    /// Inserts an entry into unknown_tagged_fields.
119    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
120        self.unknown_tagged_fields.insert(key, value);
121        self
122    }
123}
124
125#[cfg(feature = "client")]
126impl Encodable for BrokerHeartbeatRequest {
127    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
128        if version < 0 || version > 1 {
129            bail!("specified version not supported by this message type");
130        }
131        types::Int32.encode(buf, &self.broker_id)?;
132        types::Int64.encode(buf, &self.broker_epoch)?;
133        types::Int64.encode(buf, &self.current_metadata_offset)?;
134        types::Boolean.encode(buf, &self.want_fence)?;
135        types::Boolean.encode(buf, &self.want_shut_down)?;
136        let mut num_tagged_fields = self.unknown_tagged_fields.len();
137        if version >= 1 {
138            if !self.offline_log_dirs.is_empty() {
139                num_tagged_fields += 1;
140            }
141        }
142        if num_tagged_fields > std::u32::MAX as usize {
143            bail!(
144                "Too many tagged fields to encode ({} fields)",
145                num_tagged_fields
146            );
147        }
148        types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
149        if version >= 1 {
150            if !self.offline_log_dirs.is_empty() {
151                let computed_size =
152                    types::CompactArray(types::Uuid).compute_size(&self.offline_log_dirs)?;
153                if computed_size > std::u32::MAX as usize {
154                    bail!(
155                        "Tagged field is too large to encode ({} bytes)",
156                        computed_size
157                    );
158                }
159                types::UnsignedVarInt.encode(buf, 0)?;
160                types::UnsignedVarInt.encode(buf, computed_size as u32)?;
161                types::CompactArray(types::Uuid).encode(buf, &self.offline_log_dirs)?;
162            }
163        }
164        write_unknown_tagged_fields(buf, 1.., &self.unknown_tagged_fields)?;
165        Ok(())
166    }
167    fn compute_size(&self, version: i16) -> Result<usize> {
168        let mut total_size = 0;
169        total_size += types::Int32.compute_size(&self.broker_id)?;
170        total_size += types::Int64.compute_size(&self.broker_epoch)?;
171        total_size += types::Int64.compute_size(&self.current_metadata_offset)?;
172        total_size += types::Boolean.compute_size(&self.want_fence)?;
173        total_size += types::Boolean.compute_size(&self.want_shut_down)?;
174        let mut num_tagged_fields = self.unknown_tagged_fields.len();
175        if version >= 1 {
176            if !self.offline_log_dirs.is_empty() {
177                num_tagged_fields += 1;
178            }
179        }
180        if num_tagged_fields > std::u32::MAX as usize {
181            bail!(
182                "Too many tagged fields to encode ({} fields)",
183                num_tagged_fields
184            );
185        }
186        total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
187        if version >= 1 {
188            if !self.offline_log_dirs.is_empty() {
189                let computed_size =
190                    types::CompactArray(types::Uuid).compute_size(&self.offline_log_dirs)?;
191                if computed_size > std::u32::MAX as usize {
192                    bail!(
193                        "Tagged field is too large to encode ({} bytes)",
194                        computed_size
195                    );
196                }
197                total_size += types::UnsignedVarInt.compute_size(0)?;
198                total_size += types::UnsignedVarInt.compute_size(computed_size as u32)?;
199                total_size += computed_size;
200            }
201        }
202        total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
203        Ok(total_size)
204    }
205}
206
207#[cfg(feature = "broker")]
208impl Decodable for BrokerHeartbeatRequest {
209    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
210        if version < 0 || version > 1 {
211            bail!("specified version not supported by this message type");
212        }
213        let broker_id = types::Int32.decode(buf)?;
214        let broker_epoch = types::Int64.decode(buf)?;
215        let current_metadata_offset = types::Int64.decode(buf)?;
216        let want_fence = types::Boolean.decode(buf)?;
217        let want_shut_down = types::Boolean.decode(buf)?;
218        let mut offline_log_dirs = Default::default();
219        let mut unknown_tagged_fields = BTreeMap::new();
220        let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
221        for _ in 0..num_tagged_fields {
222            let tag: u32 = types::UnsignedVarInt.decode(buf)?;
223            let size: u32 = types::UnsignedVarInt.decode(buf)?;
224            match tag {
225                0 => {
226                    if version >= 1 {
227                        offline_log_dirs = types::CompactArray(types::Uuid).decode(buf)?;
228                    } else {
229                        bail!("Tag {} is not valid for version {}", tag, version);
230                    }
231                }
232                _ => {
233                    let unknown_value = buf.try_get_bytes(size as usize)?;
234                    unknown_tagged_fields.insert(tag as i32, unknown_value);
235                }
236            }
237        }
238        Ok(Self {
239            broker_id,
240            broker_epoch,
241            current_metadata_offset,
242            want_fence,
243            want_shut_down,
244            offline_log_dirs,
245            unknown_tagged_fields,
246        })
247    }
248}
249
250impl Default for BrokerHeartbeatRequest {
251    fn default() -> Self {
252        Self {
253            broker_id: (0).into(),
254            broker_epoch: -1,
255            current_metadata_offset: 0,
256            want_fence: false,
257            want_shut_down: false,
258            offline_log_dirs: Default::default(),
259            unknown_tagged_fields: BTreeMap::new(),
260        }
261    }
262}
263
264impl Message for BrokerHeartbeatRequest {
265    const VERSIONS: VersionRange = VersionRange { min: 0, max: 1 };
266    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
267}
268
269impl HeaderVersion for BrokerHeartbeatRequest {
270    fn header_version(version: i16) -> i16 {
271        2
272    }
273}