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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//! Incremental fetch sessions (KIP-227), one per broker.
//!
//! This is Kafka's `FetchSessionHandler` and `FetchMetadata`. The first request
//! to a broker is a full request with session id 0 and epoch 0. When the
//! broker answers with a session id, the next requests are incremental: they
//! name only the partitions that are new or changed, and they forget the
//! partitions that are no longer fetched.
use std::collections::BTreeMap;
use krabka_protocol::primitives::uuid::Uuid as WireUuid;
/// Kafka's `FetchMetadata.INVALID_SESSION_ID`.
pub(crate) const INVALID_SESSION_ID: i32 = 0;
/// Kafka's `FetchMetadata.INITIAL_EPOCH`.
const INITIAL_EPOCH: i32 = 0;
/// Kafka's `FetchMetadata.FINAL_EPOCH`.
pub(crate) const FINAL_EPOCH: i32 = -1;
/// `FETCH_SESSION_ID_NOT_FOUND`.
const FETCH_SESSION_ID_NOT_FOUND: i16 = 70;
/// Kafka's `FetchMetadata`: the session id and epoch of the next request.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct FetchMetadata {
pub session_id: i32,
pub epoch: i32,
}
impl FetchMetadata {
/// Kafka's `FetchMetadata.INITIAL`: create a new session.
const INITIAL: Self = Self {
session_id: INVALID_SESSION_ID,
epoch: INITIAL_EPOCH,
};
/// A full request sends every partition.
fn is_full(self) -> bool {
self.epoch == INITIAL_EPOCH || self.epoch == FINAL_EPOCH
}
/// Close the session and create a new one with a full request.
fn close_existing_attempt_new(self) -> Self {
Self {
session_id: self.session_id,
epoch: INITIAL_EPOCH,
}
}
fn next_epoch(epoch: i32) -> i32 {
if epoch < 0 {
FINAL_EPOCH
} else if epoch == i32::MAX {
1
} else {
epoch + 1
}
}
}
/// The fields of one partition in a fetch session. Kafka's
/// `FetchRequest.PartitionData`; a change of any field makes the partition part
/// of the next incremental request.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct SessionPartition {
pub topic_id: WireUuid,
pub fetch_offset: i64,
pub current_leader_epoch: i32,
pub last_fetched_epoch: i32,
pub partition_max_bytes: i32,
}
/// The session fields and partitions of one Fetch request.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct SessionRequest {
pub session_id: i32,
pub session_epoch: i32,
/// The partitions to send: all of them in a full request, the new and the
/// changed ones in an incremental request.
pub partitions: BTreeMap<(String, i32), SessionPartition>,
/// The partitions to forget, with their topic ids.
pub forgotten: Vec<((String, i32), WireUuid)>,
}
/// The fetch session of one broker. Kafka's `FetchSessionHandler`.
#[derive(Debug)]
pub(crate) struct FetchSession {
next: FetchMetadata,
partitions: BTreeMap<(String, i32), SessionPartition>,
}
impl Default for FetchSession {
fn default() -> Self {
Self {
next: FetchMetadata::INITIAL,
partitions: BTreeMap::new(),
}
}
}
impl FetchSession {
/// The session id of the next request, or 0 when there is no session.
pub(crate) fn session_id(&self) -> i32 {
self.next.session_id
}
/// Build the next request for the partitions in `wanted`, and record them
/// as the partitions of the session. Kafka's `FetchSessionHandler.Builder.build`.
pub(crate) fn build(
&mut self,
wanted: BTreeMap<(String, i32), SessionPartition>,
) -> SessionRequest {
// Kafka forgets a partition whose topic id changed and sends it again.
// The forget goes by topic id only from Fetch v13, and the client does
// not know the version here. A full request that closes the session and
// creates a new one is correct at every version.
let replaced = wanted.iter().any(|(key, next)| {
self.partitions.get(key).is_some_and(|previous| {
previous.topic_id != next.topic_id
&& previous.topic_id != WireUuid::default()
&& next.topic_id != WireUuid::default()
})
});
if replaced && !self.next.is_full() {
self.next = self.next.close_existing_attempt_new();
}
let metadata = self.next;
if metadata.is_full() {
self.partitions.clone_from(&wanted);
return SessionRequest {
session_id: metadata.session_id,
session_epoch: metadata.epoch,
partitions: wanted,
forgotten: Vec::new(),
};
}
let forgotten = self
.partitions
.iter()
.filter(|(key, _)| !wanted.contains_key(*key))
.map(|(key, partition)| (key.clone(), partition.topic_id))
.collect();
let changed = wanted
.iter()
.filter(|(key, partition)| self.partitions.get(*key) != Some(*partition))
.map(|(key, partition)| (key.clone(), *partition))
.collect();
self.partitions = wanted;
SessionRequest {
session_id: metadata.session_id,
session_epoch: metadata.epoch,
partitions: changed,
forgotten,
}
}
/// Update the session after a response. Return `false` when the consumer
/// must not use the partition data of the response. Kafka's
/// `FetchSessionHandler.handleResponse`.
pub(crate) fn handle_response(
&mut self,
error_code: i16,
response_session_id: i32,
response_partitions: usize,
throttle_time_ms: i32,
) -> bool {
if error_code != 0 {
self.next = if error_code == FETCH_SESSION_ID_NOT_FOUND {
FetchMetadata::INITIAL
} else {
self.next.close_existing_attempt_new()
};
return false;
}
if self.next.is_full() {
if response_partitions == 0 && throttle_time_ms > 0 {
// KIP-219: an empty full response throttles the client.
self.next = FetchMetadata::INITIAL;
return false;
}
self.next = if response_session_id == INVALID_SESSION_ID {
FetchMetadata::INITIAL
} else {
FetchMetadata {
session_id: response_session_id,
epoch: FetchMetadata::next_epoch(INITIAL_EPOCH),
}
};
} else {
self.next = if response_session_id == INVALID_SESSION_ID {
FetchMetadata::INITIAL
} else {
FetchMetadata {
session_id: self.next.session_id,
epoch: FetchMetadata::next_epoch(self.next.epoch),
}
};
}
true
}
/// Update the session after the request failed. Kafka's
/// `FetchSessionHandler.handleError`.
pub(crate) fn handle_error(&mut self) {
self.next = self.next.close_existing_attempt_new();
}
}
#[cfg(test)]
mod tests {
use super::*;
const TOPIC_ID: WireUuid = WireUuid([1; 16]);
fn partition(fetch_offset: i64) -> SessionPartition {
SessionPartition {
topic_id: TOPIC_ID,
fetch_offset,
current_leader_epoch: 3,
last_fetched_epoch: 2,
partition_max_bytes: 1024,
}
}
fn key(index: i32) -> (String, i32) {
("orders".to_owned(), index)
}
/// One step of a session case: the partitions to fetch, and the response
/// `(error_code, session_id, partitions, throttle_time_ms)` or a transport
/// error.
enum Step {
Fetch(Vec<((String, i32), SessionPartition)>),
Respond(i16, i32, usize, i32),
Fail,
}
/// The request and response handling of Kafka's `FetchSessionHandler`.
#[test]
fn fetch_session_builds_requests_as_kafka_does() {
use Step::{Fail, Fetch, Respond};
let request = |session_id,
session_epoch,
partitions: Vec<((String, i32), SessionPartition)>,
forgotten: Vec<(String, i32)>| {
SessionRequest {
session_id,
session_epoch,
partitions: partitions.into_iter().collect(),
forgotten: forgotten.into_iter().map(|key| (key, TOPIC_ID)).collect(),
}
};
let both = vec![(key(0), partition(10)), (key(1), partition(20))];
let mut actual = Vec::new();
let mut wanted = Vec::new();
for (name, steps, expected) in [
(
"full, then incremental with no change",
vec![
Fetch(both.clone()),
Respond(0, 77, 2, 0),
Fetch(both.clone()),
Respond(0, 77, 0, 0),
Fetch(both.clone()),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(77, 1, Vec::new(), Vec::new()),
request(77, 2, Vec::new(), Vec::new()),
],
),
(
"incremental sends a changed offset and forgets a removed partition",
vec![
Fetch(both.clone()),
Respond(0, 77, 2, 0),
Fetch(vec![(key(0), partition(15))]),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(77, 1, vec![(key(0), partition(15))], vec![key(1)]),
],
),
(
"session id not found starts a new session",
vec![
Fetch(both.clone()),
Respond(0, 77, 2, 0),
Fetch(both.clone()),
Respond(70, 0, 0, 0),
Fetch(both.clone()),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(77, 1, Vec::new(), Vec::new()),
request(0, 0, both.clone(), Vec::new()),
],
),
(
"invalid session epoch closes the session and creates a new one",
vec![
Fetch(both.clone()),
Respond(0, 77, 2, 0),
Fetch(both.clone()),
Respond(71, 0, 0, 0),
Fetch(both.clone()),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(77, 1, Vec::new(), Vec::new()),
request(77, 0, both.clone(), Vec::new()),
],
),
(
"a transport error closes the session and creates a new one",
vec![
Fetch(both.clone()),
Respond(0, 77, 2, 0),
Fetch(both.clone()),
Fail,
Fetch(both.clone()),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(77, 1, Vec::new(), Vec::new()),
request(77, 0, both.clone(), Vec::new()),
],
),
(
"a broker without sessions answers session id 0",
vec![
Fetch(both.clone()),
Respond(0, 0, 2, 0),
Fetch(both.clone()),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(0, 0, both.clone(), Vec::new()),
],
),
(
"a changed topic id sends a full request",
vec![
Fetch(both.clone()),
Respond(0, 77, 2, 0),
Fetch(vec![(
key(0),
SessionPartition {
topic_id: WireUuid([2; 16]),
..partition(10)
},
)]),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(
77,
0,
vec![(
key(0),
SessionPartition {
topic_id: WireUuid([2; 16]),
..partition(10)
},
)],
Vec::new(),
),
],
),
(
"an empty throttled full response keeps the next request full",
vec![
Fetch(both.clone()),
Respond(0, 77, 0, 100),
Fetch(both.clone()),
],
vec![
request(0, 0, both.clone(), Vec::new()),
request(0, 0, both.clone(), Vec::new()),
],
),
] {
let mut session = FetchSession::default();
let mut requests = Vec::new();
for step in steps {
match step {
Fetch(partitions) => {
requests.push(session.build(partitions.into_iter().collect()));
}
Respond(error_code, session_id, partitions, throttle) => {
session.handle_response(error_code, session_id, partitions, throttle);
}
Fail => session.handle_error(),
}
}
actual.push((name, requests));
wanted.push((name, expected));
}
assert2::assert!(actual == wanted);
}
}