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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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::{Int8, Int16, Int32, Int64, Records, String_},
traits::{ReadType, WriteType},
};
use super::{
ReadVersionedError, ReadVersionedType, RequestBody, WriteVersionedError, WriteVersionedType,
};
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct FetchRequestPartition {
/// The partition index.
pub partition: Int32,
/// The message offset.
pub fetch_offset: Int64,
/// The maximum bytes to fetch from this partition.
///
/// See KIP-74 for cases where this limit may not be honored.
pub partition_max_bytes: Int32,
}
impl<W> WriteVersionedType<W> for FetchRequestPartition
where
W: Write,
{
fn write_versioned(
&self,
writer: &mut W,
version: ApiVersion,
) -> Result<(), WriteVersionedError> {
let v = version.0.0;
assert!(v <= 4);
self.partition.write(writer)?;
self.fetch_offset.write(writer)?;
self.partition_max_bytes.write(writer)?;
Ok(())
}
}
#[derive(Debug)]
pub struct FetchRequestTopic {
/// The name of the topic to fetch.
pub topic: String_,
/// The partitions to fetch.
pub partitions: Vec<FetchRequestPartition>,
}
impl<W> WriteVersionedType<W> for FetchRequestTopic
where
W: Write,
{
fn write_versioned(
&self,
writer: &mut W,
version: ApiVersion,
) -> Result<(), WriteVersionedError> {
let v = version.0.0;
assert!(v <= 4);
self.topic.write(writer)?;
write_versioned_array(writer, version, Some(&self.partitions))?;
Ok(())
}
}
#[derive(Debug)]
pub struct FetchRequest {
/// The broker ID of the follower, of -1 if this request is from a consumer.
pub replica_id: Int32,
/// The maximum time in milliseconds to wait for the response.
pub max_wait_ms: Int32,
/// The minimum bytes to accumulate in the response.
pub min_bytes: Int32,
/// The maximum bytes to fetch. See KIP-74 for cases where this limit may not be honored.
///
/// Defaults to "no limit / max".
///
/// Added in version 3.
pub max_bytes: Option<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 4.
///
/// [KIP-98]: https://cwiki.apache.org/confluence/display/KAFKA/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging
pub isolation_level: Option<IsolationLevel>,
/// The topics to fetch.
pub topics: Vec<FetchRequestTopic>,
}
impl<W> WriteVersionedType<W> for FetchRequest
where
W: Write,
{
fn write_versioned(
&self,
writer: &mut W,
version: ApiVersion,
) -> Result<(), WriteVersionedError> {
let v = version.0.0;
assert!(v <= 4);
self.replica_id.write(writer)?;
self.max_wait_ms.write(writer)?;
self.min_bytes.write(writer)?;
if v >= 3 {
// defaults to "no limit / max".
self.max_bytes.unwrap_or(Int32(i32::MAX)).write(writer)?;
}
if v >= 4 {
// 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 FetchRequest {
type ResponseBody = FetchResponse;
const API_KEY: ApiKey = ApiKey::Fetch;
/// That's enough for now.
///
/// Note that we do not support fetch request prior to version 4, since this is the version when message version 2
/// was introduced ([KIP-98]).
///
/// [KIP-98]: https://cwiki.apache.org/confluence/display/KAFKA/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging
const API_VERSION_RANGE: ApiVersionRange =
ApiVersionRange::new(ApiVersion(Int16(4)), ApiVersion(Int16(4)));
const FIRST_TAGGED_FIELD_IN_REQUEST_VERSION: ApiVersion = ApiVersion(Int16(12));
}
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct FetchResponseAbortedTransaction {
/// The producer id associated with the aborted transaction.
pub producer_id: Int64,
/// The first offset in the aborted transaction.
pub first_offset: Int64,
}
impl<R> ReadVersionedType<R> for FetchResponseAbortedTransaction
where
R: Read,
{
fn read_versioned(reader: &mut R, version: ApiVersion) -> Result<Self, ReadVersionedError> {
let v = version.0.0;
assert!(4 <= v && v <= 4);
Ok(Self {
producer_id: Int64::read(reader)?,
first_offset: Int64::read(reader)?,
})
}
}
#[derive(Debug)]
pub struct FetchResponsePartition {
/// The partition index.
pub partition_index: Int32,
/// The error code, or 0 if there was no fetch error.
pub error_code: Option<ApiError>,
/// The current high water mark.
pub high_watermark: Int64,
/// The last stable offset (or LSO) of the partition.
///
/// This is the last offset such that the state of all transactional records prior to this offset have been decided
/// (`ABORTED` or `COMMITTED`).
///
/// Added in version 4.
pub last_stable_offset: Option<Int64>,
/// The aborted transactions.
///
/// Added in version 4.
pub aborted_transactions: Vec<FetchResponseAbortedTransaction>,
/// The record data.
pub records: Records,
}
impl<R> ReadVersionedType<R> for FetchResponsePartition
where
R: Read,
{
fn read_versioned(reader: &mut R, version: ApiVersion) -> Result<Self, ReadVersionedError> {
let v = version.0.0;
assert!(v <= 4);
Ok(Self {
partition_index: Int32::read(reader)?,
error_code: ApiError::new(Int16::read(reader)?.0),
high_watermark: Int64::read(reader)?,
last_stable_offset: (v >= 4).then(|| Int64::read(reader)).transpose()?,
aborted_transactions: (v >= 4)
.then(|| read_versioned_array(reader, version))
.transpose()?
.flatten()
.unwrap_or_default(),
records: Records::read(reader)?,
})
}
}
#[derive(Debug)]
pub struct FetchResponseTopic {
/// The topic name.
pub topic: String_,
/// The topic partitions.
pub partitions: Vec<FetchResponsePartition>,
}
impl<R> ReadVersionedType<R> for FetchResponseTopic
where
R: Read,
{
fn read_versioned(reader: &mut R, version: ApiVersion) -> Result<Self, ReadVersionedError> {
let v = version.0.0;
assert!(v <= 4);
Ok(Self {
topic: String_::read(reader)?,
partitions: read_versioned_array(reader, version)?.unwrap_or_default(),
})
}
}
#[derive(Debug)]
pub struct FetchResponse {
/// 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 1.
pub throttle_time_ms: Option<Int32>,
/// The response topics.
pub responses: Vec<FetchResponseTopic>,
}
impl<R> ReadVersionedType<R> for FetchResponse
where
R: Read,
{
fn read_versioned(reader: &mut R, version: ApiVersion) -> Result<Self, ReadVersionedError> {
let v = version.0.0;
assert!(v <= 4);
Ok(Self {
throttle_time_ms: (v >= 1).then(|| Int32::read(reader)).transpose()?,
responses: read_versioned_array(reader, version)?.unwrap_or_default(),
})
}
}