krabka-client-consumer 0.7.1

Subscribe-style consumer client for Apache Kafka in Rust
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! KIP-516 offset wire-shape helpers.
//!
//! The consumer sends `OffsetCommit` and `OffsetFetch` at v9 or lower, where
//! each topic has its name. Apache Kafka's classic `ConsumerCoordinator`
//! builds both requests with the `forTopicNames` builder, which caps the
//! version at 9 (`sendOffsetCommitRequest`, `sendOffsetFetchRequest`). At v8+
//! `OffsetFetch` carries a per-group `groups[]` array, and the legacy
//! `group_id` + `topics` fields are v0-7 only.
//!
//! The builders populate BOTH the legacy and the new fields. The codegen
//! encodes only the set that is valid for the negotiated version, so one
//! request works regardless of what the broker negotiated. The parser flattens
//! an `OffsetFetch` response across either shape.

use std::collections::{BTreeSet, HashMap};

pub(crate) use krabka_client_core::TopicNameOffsetFetch;
use krabka_protocol::{
    owned::{
        offset_commit_request::{OffsetCommitRequestPartition, OffsetCommitRequestTopic},
        offset_fetch_request::{
            OffsetFetchRequest, OffsetFetchRequestGroup, OffsetFetchRequestTopic,
            OffsetFetchRequestTopics,
        },
        offset_fetch_response::{
            OffsetFetchResponse, OffsetFetchResponsePartition, OffsetFetchResponsePartitions,
        },
    },
    primitives::uuid::Uuid as WireUuid,
};

use crate::{
    commit::OffsetAndMetadata,
    coordinator::{COORDINATOR_NOT_AVAILABLE, NOT_COORDINATOR},
};

/// An `OffsetFetch` request that names its topics, capped at v9.
///
/// `OffsetFetch` v10 names each topic by id only. Apache Kafka's classic
/// `ConsumerCoordinator.sendOffsetFetchRequest` uses
/// `OffsetFetchRequest.Builder.forTopicNames`, which allows
/// `ApiKeys.OFFSET_FETCH.oldestVersion()` to `TOPIC_ID_MIN_VERSION - 1` (9).
/// This type gives the same range to version negotiation. When the coordinator
/// supports only v10 or higher, the send fails with
/// `ClientError::IncompatibleVersion` before the request goes out. Kafka's
/// builder fails with `UnsupportedVersionException` in that case.
/// Build an `OffsetFetch` request that covers `by_topic` and is valid at each
/// version from v1 to v9.
///
/// This function populates both the legacy `group_id`/`topics` fields for v0-7
/// and the v8+ `groups[]` array. Each topic has its name and no topic id, as
/// in Kafka's `ConsumerCoordinator.sendOffsetFetchRequest`.
///
/// The request sets `require_stable`, as Apache Kafka's consumers do
/// (`CommitRequestManager.OffsetFetchRequestState.toUnsentRequest` and
/// `ConsumerCoordinator.sendOffsetFetchRequest`). From v7 the coordinator then
/// answers `UNSTABLE_OFFSET_COMMIT` (88) for a partition with a pending
/// transactional offset commit, and `send_offset_fetch` asks again. Below v7
/// the field is not on the wire. Kafka's `OffsetFetchRequest.Builder` also
/// drops the flag there (`throwIfStableOffsetsUnsupported`), because
/// `internal.throw.on.fetch.stable.offset.unsupported` defaults to `false`.
pub(crate) fn build_offset_fetch(
    group_id: &str,
    by_topic: &HashMap<String, Vec<i32>>,
) -> TopicNameOffsetFetch {
    let legacy_topics: Vec<OffsetFetchRequestTopic> = by_topic
        .iter()
        .map(|(name, parts)| OffsetFetchRequestTopic {
            name: name.clone(),
            partition_indexes: parts.clone(),
            ..Default::default()
        })
        .collect();
    let group_topics: Vec<OffsetFetchRequestTopics> = by_topic
        .iter()
        .map(|(name, parts)| OffsetFetchRequestTopics {
            name: name.clone(),
            partition_indexes: parts.clone(),
            ..Default::default()
        })
        .collect();
    TopicNameOffsetFetch::new(OffsetFetchRequest {
        group_id: group_id.to_string(),
        topics: Some(legacy_topics),
        groups: vec![OffsetFetchRequestGroup {
            group_id: group_id.to_string(),
            topics: Some(group_topics),
            ..Default::default()
        }],
        require_stable: true,
        ..Default::default()
    })
}

/// Flatten an `OffsetFetch` response into `(topic_name, partition,
/// committed_offset, committed_leader_epoch)` tuples.
///
/// v8 and v9 data lives in `groups`, and v0-7 data lives in `topics`.
pub(crate) fn parse_offset_fetch(resp: &OffsetFetchResponse) -> Vec<(String, i32, i64, i32)> {
    let mut out = Vec::new();
    if resp.groups.is_empty() {
        for t in &resp.topics {
            append_topic_offsets(&mut out, &t.name, &t.partitions);
        }
    } else {
        for g in &resp.groups {
            for t in &g.topics {
                append_topic_offsets(&mut out, &t.name, &t.partitions);
            }
        }
    }
    out
}

trait OffsetFetchPartitionData {
    fn offset_fields(&self) -> (i32, i64, i32);
    fn error_code(&self) -> i16;
}

impl OffsetFetchPartitionData for OffsetFetchResponsePartition {
    fn offset_fields(&self) -> (i32, i64, i32) {
        (
            self.partition_index,
            self.committed_offset,
            self.committed_leader_epoch,
        )
    }

    fn error_code(&self) -> i16 {
        self.error_code
    }
}

impl OffsetFetchPartitionData for OffsetFetchResponsePartitions {
    fn offset_fields(&self) -> (i32, i64, i32) {
        (
            self.partition_index,
            self.committed_offset,
            self.committed_leader_epoch,
        )
    }

    fn error_code(&self) -> i16 {
        self.error_code
    }
}

fn append_topic_offsets<P: OffsetFetchPartitionData>(
    out: &mut Vec<(String, i32, i64, i32)>,
    topic: &str,
    partitions: &[P],
) {
    out.extend(partitions.iter().map(|partition| {
        let (index, offset, leader_epoch) = partition.offset_fields();
        (topic.to_owned(), index, offset, leader_epoch)
    }));
}

fn append_topic_errors<'a, P: OffsetFetchPartitionData>(
    out: &mut Vec<(&'a str, i16)>,
    topic: &'a str,
    partitions: &[P],
) {
    out.extend(
        partitions
            .iter()
            .map(|partition| (topic, partition.error_code())),
    );
}

/// The committed offsets of an `OffsetFetch` response. Kafka's
/// `ConsumerCoordinator.OffsetFetchResponseHandler`: a partition with offset
/// `-1` or an error has no committed offset.
///
/// v8 and v9 data lives in `groups`, and v0-7 data lives in `topics`.
pub(crate) fn parse_committed_offsets(
    resp: &OffsetFetchResponse,
) -> HashMap<(String, i32), Option<OffsetAndMetadata>> {
    let committed = |error_code: i16, offset: i64, leader_epoch: i32, metadata: &Option<String>| {
        (error_code == 0 && offset >= 0).then(|| OffsetAndMetadata {
            offset,
            leader_epoch: (leader_epoch >= 0).then_some(leader_epoch),
            metadata: metadata.clone().unwrap_or_default(),
        })
    };
    let mut out = HashMap::new();
    for t in &resp.topics {
        for p in &t.partitions {
            out.insert(
                (t.name.clone(), p.partition_index),
                committed(
                    p.error_code,
                    p.committed_offset,
                    p.committed_leader_epoch,
                    &p.metadata,
                ),
            );
        }
    }
    for g in &resp.groups {
        for t in &g.topics {
            for p in &t.partitions {
                out.insert(
                    (t.name.clone(), p.partition_index),
                    committed(
                        p.error_code,
                        p.committed_offset,
                        p.committed_leader_epoch,
                        &p.metadata,
                    ),
                );
            }
        }
    }
    out
}

/// `UNKNOWN_TOPIC_OR_PARTITION`: the coordinator does not know the topic.
const UNKNOWN_TOPIC_OR_PARTITION: i16 = 3;
/// `TOPIC_AUTHORIZATION_FAILED`: the principal cannot describe the topic.
const TOPIC_AUTHORIZATION_FAILED: i16 = 29;
/// `GROUP_AUTHORIZATION_FAILED`: the principal cannot describe the group.
const GROUP_AUTHORIZATION_FAILED: i16 = 30;
/// `UNSTABLE_OFFSET_COMMIT`: a transaction or a replication holds the offset.
const UNSTABLE_OFFSET_COMMIT: i16 = 88;
/// `UNKNOWN_TOPIC_ID`: the coordinator does not hold the topic id.
const UNKNOWN_TOPIC_ID: i16 = 100;

/// What the consumer does with one `OffsetFetch` response.
///
/// The mapping follows Apache Kafka's `CommitRequestManager.OffsetFetchRequestState`
/// (`onResponse`, `onFailure` and `onSuccess`) and
/// `CommitRequestManager.fetchOffsetsWithRetries`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum OffsetFetchAction {
    /// Use the committed offsets in the response.
    Complete,
    /// A partition answered 3 or 100. Send the request again until the
    /// deadline. After the deadline, use the response. Its errored partitions
    /// start from the reset policy.
    RetryUnknownTopic(i16),
    /// The group answered a retriable code, or a partition answered 88. Send
    /// the request again until the deadline, then fail with the code.
    Retry(i16),
    /// The group answered 15 or 16. Find the coordinator again and send the
    /// request again until the deadline, then fail with the code.
    FindCoordinator(i16),
    /// The group answered 30. Fail.
    GroupAuthorizationFailed,
    /// Partitions answered 29. Fail with the names of their topics.
    TopicAuthorizationFailed(BTreeSet<String>),
    /// The group or a partition answered a code that Kafka does not retry.
    /// Fail with the code.
    Fatal(i16),
}

/// Classify an `OffsetFetch` response as Apache Kafka's consumer does.
///
/// A group error code comes first. Kafka's `OffsetFetchRequestState.onFailure`
/// finds the coordinator again for 15 and 16, fails with a group authorization
/// error for 30, retries every other `RetriableException` code, and fails for
/// all other codes.
///
/// Without a group error, `OffsetFetchRequestState.onSuccess` reads each
/// partition in order. The first code other than 0, 3, 29, 88 and 100 fails
/// the fetch at once. After the loop, 29 fails the fetch with the topic names,
/// then 88 makes it retriable, then 3 and 100 make it retriable with partial
/// results.
///
/// v8 and v9 data lives in `groups`, and v0-7 data lives in the top-level
/// fields.
pub(crate) fn classify_offset_fetch(resp: &OffsetFetchResponse) -> OffsetFetchAction {
    let group_error = std::iter::once(resp.error_code)
        .chain(resp.groups.iter().map(|g| g.error_code))
        .find(|code| *code != 0);
    if let Some(code) = group_error {
        return classify_group_error(code);
    }

    let mut partitions = Vec::new();
    for topic in &resp.topics {
        append_topic_errors(&mut partitions, &topic.name, &topic.partitions);
    }
    for group in &resp.groups {
        for topic in &group.topics {
            append_topic_errors(&mut partitions, &topic.name, &topic.partitions);
        }
    }
    let mut unauthorized_topics = BTreeSet::new();
    let mut unstable = false;
    let mut unknown_topic = None;
    for (topic, code) in partitions {
        match code {
            0 => {}
            UNKNOWN_TOPIC_OR_PARTITION | UNKNOWN_TOPIC_ID => {
                unknown_topic.get_or_insert(code);
            }
            TOPIC_AUTHORIZATION_FAILED => {
                unauthorized_topics.insert(topic.to_string());
            }
            UNSTABLE_OFFSET_COMMIT => unstable = true,
            code => return OffsetFetchAction::Fatal(code),
        }
    }
    if !unauthorized_topics.is_empty() {
        OffsetFetchAction::TopicAuthorizationFailed(unauthorized_topics)
    } else if unstable {
        OffsetFetchAction::Retry(UNSTABLE_OFFSET_COMMIT)
    } else if let Some(code) = unknown_topic {
        OffsetFetchAction::RetryUnknownTopic(code)
    } else {
        OffsetFetchAction::Complete
    }
}

fn classify_group_error(code: i16) -> OffsetFetchAction {
    match code {
        COORDINATOR_NOT_AVAILABLE | NOT_COORDINATOR => OffsetFetchAction::FindCoordinator(code),
        GROUP_AUTHORIZATION_FAILED => OffsetFetchAction::GroupAuthorizationFailed,
        code if is_retriable_error(code) => OffsetFetchAction::Retry(code),
        code => OffsetFetchAction::Fatal(code),
    }
}

/// Whether Apache Kafka's `common/protocol/Errors` maps `code` to an exception
/// that extends `RetriableException`.
pub(crate) fn is_retriable_error(code: i16) -> bool {
    matches!(
        code,
        2 | 3
            | 5
            | 6
            | 7
            | 9
            | 13
            | 14
            | 15
            | 16
            | 19
            | 20
            | 41
            | 51
            | 56
            | 70
            | 71
            | 72
            | 74
            | 75
            | 78
            | 80
            | 83
            | 84
            | 88
            | 89
            | 100
            | 103
            | 106
            | 122
            | 123
            | 133
    )
}

/// An `OffsetCommit` request that names its topics, capped at v9.
pub(crate) use krabka_client_core::TopicNameOffsetCommit;

/// Build the `topics` for an `OffsetCommit`.
///
/// Each topic has its name and no topic id, as in Kafka's
/// `ConsumerCoordinator.sendOffsetCommitRequest`. `offsets` maps `(topic,
/// partition)` to `(committed_offset, committed_leader_epoch)`.
pub(crate) fn build_commit_topics(
    offsets: HashMap<(String, i32), OffsetAndMetadata>,
) -> Vec<OffsetCommitRequestTopic> {
    let mut by_topic: HashMap<String, Vec<(i32, OffsetAndMetadata)>> = HashMap::new();
    for ((t, p), offset) in offsets {
        by_topic.entry(t).or_default().push((p, offset));
    }
    by_topic
        .into_iter()
        .map(|(name, parts)| OffsetCommitRequestTopic {
            name,
            partitions: parts
                .into_iter()
                .map(|(p, offset)| OffsetCommitRequestPartition {
                    partition_index: p,
                    committed_offset: offset.offset,
                    committed_leader_epoch: offset.leader_epoch.unwrap_or(-1),
                    committed_metadata: Some(offset.metadata),
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        })
        .collect()
}

/// Build the `topic_id → name` reverse map from the consumer's `name →
/// topic_id` table. The fetch path uses it to resolve `Fetch` v13+ responses,
/// which name each topic by id only.
pub(crate) fn id_to_name(topic_ids: &HashMap<String, WireUuid>) -> HashMap<WireUuid, String> {
    topic_ids.iter().map(|(n, id)| (*id, n.clone())).collect()
}

#[cfg(test)]
mod tests {

    use krabka_protocol::{
        UnknownTaggedFields,
        owned::offset_fetch_response::{
            OffsetFetchResponse, OffsetFetchResponseGroup, OffsetFetchResponsePartition,
            OffsetFetchResponsePartitions, OffsetFetchResponseTopic, OffsetFetchResponseTopics,
        },
    };

    use super::*;
    use crate::test_support::wire_uuid;

    #[test]
    fn build_offset_fetch_populates_legacy_and_groups() {
        let mut by_topic = HashMap::new();
        by_topic.insert("t".to_string(), vec![0, 1]);

        let req = build_offset_fetch("g", &by_topic);
        // Legacy single-group fields (v0-7) AND v8+ groups[] by topic name.
        assert2::assert!(
            req == TopicNameOffsetFetch::new(OffsetFetchRequest {
                group_id: "g".to_string(),
                topics: Some(vec![OffsetFetchRequestTopic {
                    name: "t".to_string(),
                    partition_indexes: vec![0, 1],
                    unknown_tagged_fields: UnknownTaggedFields(vec![]),
                }]),
                groups: vec![OffsetFetchRequestGroup {
                    group_id: "g".to_string(),
                    member_id: None,
                    member_epoch: -1,
                    topics: Some(vec![OffsetFetchRequestTopics {
                        name: "t".to_string(),
                        topic_id: WireUuid::ZERO,
                        partition_indexes: vec![0, 1],
                        unknown_tagged_fields: UnknownTaggedFields(vec![]),
                    }]),
                    unknown_tagged_fields: UnknownTaggedFields(vec![]),
                }],
                require_stable: true,
                unknown_tagged_fields: UnknownTaggedFields(vec![]),
            })
        );
    }

    /// The topic name of `n` in the `grouped` rows.
    fn topic_name(n: u8) -> String {
        match n {
            1 => "orders".into(),
            2 => "payments".into(),
            _ => "shipments".into(),
        }
    }

    trait MissingOffsetFetchPartition {
        fn with_error_code(error_code: i16) -> Self;
    }

    impl MissingOffsetFetchPartition for OffsetFetchResponsePartition {
        fn with_error_code(error_code: i16) -> Self {
            Self {
                committed_offset: -1,
                error_code,
                ..Default::default()
            }
        }
    }

    impl MissingOffsetFetchPartition for OffsetFetchResponsePartitions {
        fn with_error_code(error_code: i16) -> Self {
            Self {
                committed_offset: -1,
                error_code,
                ..Default::default()
            }
        }
    }

    /// A v8 or v9 response for group `g`. Each row is `(topic number, partition
    /// error code)`, and `topic_name` gives the name of the topic number.
    fn grouped(group_error: i16, rows: &[(u8, i16)]) -> OffsetFetchResponse {
        OffsetFetchResponse {
            groups: vec![OffsetFetchResponseGroup {
                group_id: "g".into(),
                error_code: group_error,
                topics: rows
                    .iter()
                    .map(|(topic, error_code)| OffsetFetchResponseTopics {
                        name: topic_name(*topic),
                        partitions: vec![OffsetFetchResponsePartitions::with_error_code(
                            *error_code,
                        )],
                        ..Default::default()
                    })
                    .collect(),
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    /// A v2-7 response. Each row is `(topic name, partition error code)`.
    fn legacy(group_error: i16, rows: &[(&str, i16)]) -> OffsetFetchResponse {
        OffsetFetchResponse {
            error_code: group_error,
            topics: rows
                .iter()
                .map(|(name, error_code)| OffsetFetchResponseTopic {
                    name: (*name).into(),
                    partitions: vec![OffsetFetchResponsePartition::with_error_code(*error_code)],
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        }
    }

    /// Kafka's `CommitRequestManager.OffsetFetchRequestState` maps each group
    /// and partition error code of an `OffsetFetch` response.
    #[test]
    fn classify_offset_fetch_maps_codes_as_kafka_does() {
        use OffsetFetchAction::{
            Complete, Fatal, FindCoordinator, GroupAuthorizationFailed, Retry, RetryUnknownTopic,
            TopicAuthorizationFailed,
        };
        let topics = |names: &[&str]| {
            TopicAuthorizationFailed(names.iter().map(|name| (*name).to_string()).collect())
        };
        for (name, response, expected) in [
            ("no error", grouped(0, &[(1, 0), (2, 0)]), Complete),
            ("no rows", grouped(0, &[]), Complete),
            // Group codes: `onFailure`.
            ("coordinator load in progress", grouped(14, &[]), Retry(14)),
            (
                "coordinator not available",
                grouped(15, &[]),
                FindCoordinator(15),
            ),
            ("not coordinator", grouped(16, &[]), FindCoordinator(16)),
            ("request timed out", grouped(7, &[]), Retry(7)),
            ("network exception", grouped(13, &[]), Retry(13)),
            (
                "group authorization failed",
                grouped(30, &[]),
                GroupAuthorizationFailed,
            ),
            ("unknown member id", grouped(25, &[]), Fatal(25)),
            ("stale member epoch", grouped(113, &[]), Fatal(113)),
            ("group id not found", grouped(69, &[]), Fatal(69)),
            ("unknown server error", grouped(-1, &[]), Fatal(-1)),
            (
                "a group code hides the partition codes",
                grouped(14, &[(1, 29)]),
                Retry(14),
            ),
            // Partition codes: `onSuccess`.
            (
                "unknown topic or partition",
                grouped(0, &[(1, 0), (2, 3)]),
                RetryUnknownTopic(3),
            ),
            (
                "unknown topic id",
                grouped(0, &[(1, 100)]),
                RetryUnknownTopic(100),
            ),
            (
                "topic authorization failed names each topic",
                grouped(0, &[(1, 29), (2, 29), (3, 29)]),
                topics(&["orders", "payments", "shipments"]),
            ),
            (
                "unstable offset commit",
                grouped(0, &[(1, 88)]),
                Retry(UNSTABLE_OFFSET_COMMIT),
            ),
            (
                "not leader or follower is unexpected",
                grouped(0, &[(1, 6)]),
                Fatal(6),
            ),
            (
                "unknown member id on a partition is unexpected",
                grouped(0, &[(1, 25)]),
                Fatal(25),
            ),
            (
                "topic authorization comes before unstable offsets",
                grouped(0, &[(1, 88), (2, 29)]),
                topics(&["payments"]),
            ),
            (
                "unstable offsets come before unknown topics",
                grouped(0, &[(1, 100), (2, 88)]),
                Retry(UNSTABLE_OFFSET_COMMIT),
            ),
            (
                "an unexpected code comes before collected codes",
                grouped(0, &[(1, 29), (2, 6)]),
                Fatal(6),
            ),
            (
                "the first unexpected code wins",
                grouped(0, &[(1, 6), (2, 25)]),
                Fatal(6),
            ),
            // v2-7 responses.
            (
                "legacy group code",
                legacy(16, &[("orders", 0)]),
                FindCoordinator(16),
            ),
            (
                "legacy topic authorization failed",
                legacy(0, &[("orders", 29)]),
                topics(&["orders"]),
            ),
            (
                "legacy unknown topic or partition",
                legacy(0, &[("orders", 3)]),
                RetryUnknownTopic(3),
            ),
        ] {
            assert2::check!(classify_offset_fetch(&response) == expected, "case {name}");
        }
    }

    #[test]
    fn is_retriable_error_matches_kafka_retriable_exceptions() {
        let retriable = (-1..=140)
            .filter(|code| is_retriable_error(*code))
            .collect::<Vec<_>>();
        assert2::assert!(
            retriable
                == vec![
                    2, 3, 5, 6, 7, 9, 13, 14, 15, 16, 19, 20, 41, 51, 56, 70, 71, 72, 74, 75, 78,
                    80, 83, 84, 88, 89, 100, 103, 106, 122, 123, 133,
                ]
        );
    }

    #[test]
    fn parse_offset_fetch_reads_groups_with_name() {
        let resp = OffsetFetchResponse {
            groups: vec![OffsetFetchResponseGroup {
                group_id: "g".into(),
                topics: vec![OffsetFetchResponseTopics {
                    name: "t".into(),
                    partitions: vec![OffsetFetchResponsePartitions {
                        partition_index: 3,
                        committed_offset: 42,
                        ..Default::default()
                    }],
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        };
        let out = parse_offset_fetch(&resp);
        assert2::assert!(out == vec![("t".to_string(), 3, 42, -1)]);
    }

    #[test]
    fn parse_offset_fetch_falls_back_to_legacy_topics() {
        // v0-7: no groups, data in the top-level `topics` field.
        let resp = OffsetFetchResponse {
            topics: vec![OffsetFetchResponseTopic {
                name: "legacy".into(),
                partitions: vec![OffsetFetchResponsePartition {
                    partition_index: 1,
                    committed_offset: 11,
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        };
        let out = parse_offset_fetch(&resp);
        assert2::assert!(out == vec![("legacy".to_string(), 1, 11, -1)]);
    }

    #[test]
    fn build_commit_topics_names_each_topic() {
        let mut offsets = HashMap::new();
        offsets.insert(
            ("t".to_string(), 3),
            OffsetAndMetadata {
                offset: 100,
                leader_epoch: Some(5),
                metadata: "note".into(),
            },
        );
        let topics = build_commit_topics(offsets);
        assert2::assert!(
            topics
                == vec![OffsetCommitRequestTopic {
                    name: "t".to_string(),
                    topic_id: WireUuid::ZERO,
                    partitions: vec![OffsetCommitRequestPartition {
                        partition_index: 3,
                        committed_offset: 100,
                        committed_leader_epoch: 5,
                        committed_metadata: Some("note".into()),
                        unknown_tagged_fields: UnknownTaggedFields(vec![]),
                    }],
                    unknown_tagged_fields: UnknownTaggedFields(vec![]),
                }]
        );
    }

    #[test]
    fn id_to_name_inverts_the_map() {
        let mut ids = HashMap::new();
        ids.insert("t".to_string(), wire_uuid(7));
        let inv = id_to_name(&ids);
        assert2::assert!(inv.get(&wire_uuid(7)) == Some(&"t".to_string()));
    }
}