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
//! `ListOffsets` request and response.
//!
//! # References
//! - [KIP-79](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=65868090)
//! - [KIP-98](https://cwiki.apache.org/confluence/display/KAFKA/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging)
use std::io::{Read, Write};
use crate::protocol::{
api_key::ApiKey,
api_version::{ApiVersion, ApiVersionRange},
error::Error as ApiError,
messages::{IsolationLevel, read_versioned_array, write_versioned_array},
primitives::{Array, Int8, Int16, Int32, Int64, String_},
traits::{ReadType, WriteType},
};
use super::{
ReadVersionedError, ReadVersionedType, RequestBody, WriteVersionedError, WriteVersionedType,
};
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct ListOffsetsRequestPartition {
/// The partition index.
pub partition_index: Int32,
/// The current timestamp.
///
/// Depending on the version this will return:
///
/// - **version 0:** `max_num_offsets` offsets that are smaller/equal than this timestamp.
/// - **version 1 and later:** return timestamp and offset of the first/message greater/equal than this timestamp
///
/// Per [KIP-79] this can have the following special values:
///
/// - `-1`: latest offset
/// - `-2`: earlist offset
///
/// [KIP-79]: https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=65868090
pub timestamp: Int64,
/// The maximum number of offsets to report.
///
/// Defaults to 1.
///
/// Removed in version 1.
pub max_num_offsets: Option<Int32>,
}
impl<W> WriteVersionedType<W> for ListOffsetsRequestPartition
where
W: Write,
{
fn write_versioned(
&self,
writer: &mut W,
version: ApiVersion,
) -> Result<(), WriteVersionedError> {
let v = version.0.0;
assert!(v <= 3);
self.partition_index.write(writer)?;
self.timestamp.write(writer)?;
if v < 1 {
// Only fetch 1 offset by default.
self.max_num_offsets.unwrap_or(Int32(1)).write(writer)?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct ListOffsetsRequestTopic {
/// The topic name.
pub name: String_,
/// Each partition in the request.
///
/// Note: A partition may only appear once within the request.
pub partitions: Vec<ListOffsetsRequestPartition>,
}
impl<W> WriteVersionedType<W> for ListOffsetsRequestTopic
where
W: Write,
{
fn write_versioned(
&self,
writer: &mut W,
version: ApiVersion,
) -> Result<(), WriteVersionedError> {
let v = version.0.0;
assert!(v <= 3);
self.name.write(writer)?;
write_versioned_array(writer, version, Some(&self.partitions))?;
Ok(())
}
}
#[derive(Debug)]
pub struct ListOffsetsRequest {
/// The broker ID of the requestor, or -1 if this request is being made by a normal consumer.
pub replica_id: Int32,
/// This setting controls the visibility of transactional records.
///
/// Using `READ_UNCOMMITTED` (`isolation_level = 0`) makes all records visible. With `READ_COMMITTED`
/// (`isolation_level = 1`), non-transactional and `COMMITTED` transactional records are visible. To be more
/// concrete, `READ_COMMITTED` returns all data from offsets smaller than the current LSO (last stable offset), and
/// enables the inclusion of the list of aborted transactions in the result, which allows consumers to discard
/// `ABORTED` transactional records.
///
/// As per [KIP-98] the default is `READ_UNCOMMITTED`.
///
/// Added in version 2.
///
/// [KIP-98]: https://cwiki.apache.org/confluence/display/KAFKA/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging
pub isolation_level: Option<IsolationLevel>,
/// Each topic in the request.
///
/// Note: A topic may only appear once within the request.
pub topics: Vec<ListOffsetsRequestTopic>,
}
impl<W> WriteVersionedType<W> for ListOffsetsRequest
where
W: Write,
{
fn write_versioned(
&self,
writer: &mut W,
version: ApiVersion,
) -> Result<(), WriteVersionedError> {
let v = version.0.0;
assert!(v <= 3);
self.replica_id.write(writer)?;
if v >= 2 {
// The default is `READ_UNCOMMITTED`.
let level: Int8 = self.isolation_level.unwrap_or_default().into();
level.write(writer)?;
}
write_versioned_array(writer, version, Some(&self.topics))?;
Ok(())
}
}
impl RequestBody for ListOffsetsRequest {
type ResponseBody = ListOffsetsResponse;
const API_KEY: ApiKey = ApiKey::ListOffsets;
/// At the time of writing this is the same subset supported by rdkafka
const API_VERSION_RANGE: ApiVersionRange =
ApiVersionRange::new(ApiVersion(Int16(0)), ApiVersion(Int16(3)));
const FIRST_TAGGED_FIELD_IN_REQUEST_VERSION: ApiVersion = ApiVersion(Int16(6));
}
#[derive(Debug)]
pub struct ListOffsetsResponsePartition {
/// The partition index.
pub partition_index: Int32,
/// The partition error code, or 0 if there was no error.
pub error_code: Option<ApiError>,
/// The result offsets.
///
/// Removed in version 1.
pub old_style_offsets: Option<Array<Int64>>,
/// The timestamp associated with the returned offset.
///
/// Added in version 1.
pub timestamp: Option<Int64>,
/// The returned offset.
///
/// Added in version 1.
pub offset: Option<Int64>,
}
impl<R> ReadVersionedType<R> for ListOffsetsResponsePartition
where
R: Read,
{
fn read_versioned(reader: &mut R, version: ApiVersion) -> Result<Self, ReadVersionedError> {
let v = version.0.0;
assert!(v <= 3);
Ok(Self {
partition_index: Int32::read(reader)?,
error_code: ApiError::new(Int16::read(reader)?.0),
old_style_offsets: (v < 1).then(|| Array::read(reader)).transpose()?,
timestamp: (v >= 1).then(|| Int64::read(reader)).transpose()?,
offset: (v >= 1).then(|| Int64::read(reader)).transpose()?,
})
}
}
#[derive(Debug)]
pub struct ListOffsetsResponseTopic {
/// The topic name.
pub name: String_,
/// Each partition in the response.
pub partitions: Vec<ListOffsetsResponsePartition>,
}
impl<R> ReadVersionedType<R> for ListOffsetsResponseTopic
where
R: Read,
{
fn read_versioned(reader: &mut R, version: ApiVersion) -> Result<Self, ReadVersionedError> {
let v = version.0.0;
assert!(v <= 3);
Ok(Self {
name: String_::read(reader)?,
partitions: read_versioned_array(reader, version)?.unwrap_or_default(),
})
}
}
#[derive(Debug)]
pub struct ListOffsetsResponse {
/// The duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
///
/// Added in version 2.
pub throttle_time_ms: Option<Int32>,
/// Each topic in the response.
pub topics: Vec<ListOffsetsResponseTopic>,
}
impl<R> ReadVersionedType<R> for ListOffsetsResponse
where
R: Read,
{
fn read_versioned(reader: &mut R, version: ApiVersion) -> Result<Self, ReadVersionedError> {
let v = version.0.0;
assert!(v <= 3);
Ok(Self {
throttle_time_ms: (v >= 2).then(|| Int32::read(reader)).transpose()?,
topics: read_versioned_array(reader, version)?.unwrap_or_default(),
})
}
}