kafka_api/schemata/init_producer_id_request.rs
1// Copyright 2024 tison <wander4096@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use byteorder::ReadBytesExt;
16
17use crate::codec::*;
18use crate::IoResult;
19
20// Version 1 is the same as version 0.
21//
22// Version 2 is the first flexible version.
23//
24// Version 3 adds ProducerId and ProducerEpoch, allowing producers to try to resume after an
25// INVALID_PRODUCER_EPOCH error
26//
27// Version 4 adds the support for new error code PRODUCER_FENCED.
28
29#[derive(Debug, Default, Clone)]
30pub struct InitProducerIdRequest {
31 /// The transactional id, or null if the producer is not transactional.
32 pub transactional_id: Option<String>,
33 /// The time in ms to wait before aborting idle transactions sent by this producer. This is
34 /// only relevant if a TransactionalId has been defined.
35 pub transaction_timeout_ms: i32,
36 /// The producer id. This is used to disambiguate requests if a transactional id is reused
37 /// following its expiration.
38 pub producer_id: i64,
39 /// The producer's current epoch. This will be checked against the producer epoch on the
40 /// broker, and the request will return an error if they do not match.
41 pub producer_epoch: i16,
42 /// Unknown tagged fields.
43 pub unknown_tagged_fields: Vec<RawTaggedField>,
44}
45
46impl Decodable for InitProducerIdRequest {
47 fn read<B: ReadBytesExt>(buf: &mut B, version: i16) -> IoResult<Self> {
48 let mut res = InitProducerIdRequest {
49 transactional_id: NullableString(version >= 3).decode(buf)?,
50 transaction_timeout_ms: Int32.decode(buf)?,
51 producer_id: if version >= 3 { Int64.decode(buf)? } else { -1 },
52 producer_epoch: if version >= 3 { Int16.decode(buf)? } else { -1 },
53 ..Default::default()
54 };
55 if version >= 2 {
56 res.unknown_tagged_fields = RawTaggedFieldList.decode(buf)?;
57 }
58 Ok(res)
59 }
60}