kafka_protocol/messages/
init_producer_id_request.rs

1//! InitProducerIdRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/InitProducerIdRequest.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-5
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct InitProducerIdRequest {
24    /// The transactional id, or null if the producer is not transactional.
25    ///
26    /// Supported API versions: 0-5
27    pub transactional_id: Option<super::TransactionalId>,
28
29    /// The time in ms to wait before aborting idle transactions sent by this producer. This is only relevant if a TransactionalId has been defined.
30    ///
31    /// Supported API versions: 0-5
32    pub transaction_timeout_ms: i32,
33
34    /// The producer id. This is used to disambiguate requests if a transactional id is reused following its expiration.
35    ///
36    /// Supported API versions: 3-5
37    pub producer_id: super::ProducerId,
38
39    /// The producer's current epoch. This will be checked against the producer epoch on the broker, and the request will return an error if they do not match.
40    ///
41    /// Supported API versions: 3-5
42    pub producer_epoch: i16,
43
44    /// Other tagged fields
45    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
46}
47
48impl InitProducerIdRequest {
49    /// Sets `transactional_id` to the passed value.
50    ///
51    /// The transactional id, or null if the producer is not transactional.
52    ///
53    /// Supported API versions: 0-5
54    pub fn with_transactional_id(mut self, value: Option<super::TransactionalId>) -> Self {
55        self.transactional_id = value;
56        self
57    }
58    /// Sets `transaction_timeout_ms` to the passed value.
59    ///
60    /// The time in ms to wait before aborting idle transactions sent by this producer. This is only relevant if a TransactionalId has been defined.
61    ///
62    /// Supported API versions: 0-5
63    pub fn with_transaction_timeout_ms(mut self, value: i32) -> Self {
64        self.transaction_timeout_ms = value;
65        self
66    }
67    /// Sets `producer_id` to the passed value.
68    ///
69    /// The producer id. This is used to disambiguate requests if a transactional id is reused following its expiration.
70    ///
71    /// Supported API versions: 3-5
72    pub fn with_producer_id(mut self, value: super::ProducerId) -> Self {
73        self.producer_id = value;
74        self
75    }
76    /// Sets `producer_epoch` to the passed value.
77    ///
78    /// The producer's current epoch. This will be checked against the producer epoch on the broker, and the request will return an error if they do not match.
79    ///
80    /// Supported API versions: 3-5
81    pub fn with_producer_epoch(mut self, value: i16) -> Self {
82        self.producer_epoch = value;
83        self
84    }
85    /// Sets unknown_tagged_fields to the passed value.
86    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
87        self.unknown_tagged_fields = value;
88        self
89    }
90    /// Inserts an entry into unknown_tagged_fields.
91    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
92        self.unknown_tagged_fields.insert(key, value);
93        self
94    }
95}
96
97#[cfg(feature = "client")]
98impl Encodable for InitProducerIdRequest {
99    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
100        if version >= 2 {
101            types::CompactString.encode(buf, &self.transactional_id)?;
102        } else {
103            types::String.encode(buf, &self.transactional_id)?;
104        }
105        types::Int32.encode(buf, &self.transaction_timeout_ms)?;
106        if version >= 3 {
107            types::Int64.encode(buf, &self.producer_id)?;
108        } else {
109            if self.producer_id != -1 {
110                bail!("A field is set that is not available on the selected protocol version");
111            }
112        }
113        if version >= 3 {
114            types::Int16.encode(buf, &self.producer_epoch)?;
115        } else {
116            if self.producer_epoch != -1 {
117                bail!("A field is set that is not available on the selected protocol version");
118            }
119        }
120        if version >= 2 {
121            let num_tagged_fields = self.unknown_tagged_fields.len();
122            if num_tagged_fields > std::u32::MAX as usize {
123                bail!(
124                    "Too many tagged fields to encode ({} fields)",
125                    num_tagged_fields
126                );
127            }
128            types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
129
130            write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
131        }
132        Ok(())
133    }
134    fn compute_size(&self, version: i16) -> Result<usize> {
135        let mut total_size = 0;
136        if version >= 2 {
137            total_size += types::CompactString.compute_size(&self.transactional_id)?;
138        } else {
139            total_size += types::String.compute_size(&self.transactional_id)?;
140        }
141        total_size += types::Int32.compute_size(&self.transaction_timeout_ms)?;
142        if version >= 3 {
143            total_size += types::Int64.compute_size(&self.producer_id)?;
144        } else {
145            if self.producer_id != -1 {
146                bail!("A field is set that is not available on the selected protocol version");
147            }
148        }
149        if version >= 3 {
150            total_size += types::Int16.compute_size(&self.producer_epoch)?;
151        } else {
152            if self.producer_epoch != -1 {
153                bail!("A field is set that is not available on the selected protocol version");
154            }
155        }
156        if version >= 2 {
157            let num_tagged_fields = self.unknown_tagged_fields.len();
158            if num_tagged_fields > std::u32::MAX as usize {
159                bail!(
160                    "Too many tagged fields to encode ({} fields)",
161                    num_tagged_fields
162                );
163            }
164            total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
165
166            total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
167        }
168        Ok(total_size)
169    }
170}
171
172#[cfg(feature = "broker")]
173impl Decodable for InitProducerIdRequest {
174    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
175        let transactional_id = if version >= 2 {
176            types::CompactString.decode(buf)?
177        } else {
178            types::String.decode(buf)?
179        };
180        let transaction_timeout_ms = types::Int32.decode(buf)?;
181        let producer_id = if version >= 3 {
182            types::Int64.decode(buf)?
183        } else {
184            (-1).into()
185        };
186        let producer_epoch = if version >= 3 {
187            types::Int16.decode(buf)?
188        } else {
189            -1
190        };
191        let mut unknown_tagged_fields = BTreeMap::new();
192        if version >= 2 {
193            let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
194            for _ in 0..num_tagged_fields {
195                let tag: u32 = types::UnsignedVarInt.decode(buf)?;
196                let size: u32 = types::UnsignedVarInt.decode(buf)?;
197                let unknown_value = buf.try_get_bytes(size as usize)?;
198                unknown_tagged_fields.insert(tag as i32, unknown_value);
199            }
200        }
201        Ok(Self {
202            transactional_id,
203            transaction_timeout_ms,
204            producer_id,
205            producer_epoch,
206            unknown_tagged_fields,
207        })
208    }
209}
210
211impl Default for InitProducerIdRequest {
212    fn default() -> Self {
213        Self {
214            transactional_id: Some(Default::default()),
215            transaction_timeout_ms: 0,
216            producer_id: (-1).into(),
217            producer_epoch: -1,
218            unknown_tagged_fields: BTreeMap::new(),
219        }
220    }
221}
222
223impl Message for InitProducerIdRequest {
224    const VERSIONS: VersionRange = VersionRange { min: 0, max: 5 };
225    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
226}
227
228impl HeaderVersion for InitProducerIdRequest {
229    fn header_version(version: i16) -> i16 {
230        if version >= 2 {
231            2
232        } else {
233            1
234        }
235    }
236}