krafka 0.27.0

An async Apache Kafka client in pure Rust: producer, transactions, consumer groups, share groups and admin
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
//! Producers, transactions and the KRaft quorum.

use std::collections::{BTreeMap, HashMap};
use std::time::Duration;

use tracing::{info, warn};

use crate::BrokerId;
use crate::consumer::TopicPartition;
use crate::error::{ErrorCode, KrafkaError, ProtocolErrorKind, Result};
use crate::protocol::{
    ApiKey, DescribeProducersRequest, DescribeProducersResponse, DescribeProducersTopicRequest,
    DescribeQuorumPartitionRequest, DescribeQuorumRequest, DescribeQuorumResponse,
    DescribeQuorumTopicRequest, DescribeTransactionsRequest, DescribeTransactionsResponse,
    ListTransactionsRequest, ListTransactionsResponse, QuorumReplicaState, WritableTxnMarker,
    WritableTxnMarkerTopic, WriteTxnMarkersRequest, WriteTxnMarkersResponse, versions,
};

use super::driver::{Mode, Target, answer, exchange, negotiate};
use super::{AdminClient, validate_topics};

/// The KRaft metadata log partition.
const CLUSTER_METADATA_TOPIC: &str = "__cluster_metadata";

/// An active producer on a partition.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProducerState {
    /// Producer ID.
    pub producer_id: i64,
    /// Producer epoch.
    pub producer_epoch: i32,
    /// Last sequence number written; `-1` if unknown.
    pub last_sequence: i32,
    /// Timestamp of the last write; `-1` if unknown.
    pub last_timestamp: i64,
    /// Coordinator epoch of the producer's transaction coordinator.
    pub coordinator_epoch: i32,
    /// Start offset of the open transaction, if any.
    pub current_transaction_start_offset: Option<i64>,
}

/// A transaction as its coordinator describes it.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct TransactionDescription {
    /// State (`Ongoing`, `PrepareCommit`, `CompleteAbort`, …).
    pub state: String,
    /// Transaction timeout.
    pub timeout: Duration,
    /// Start time in milliseconds since the epoch; `-1` without an open
    /// transaction.
    pub start_time_ms: i64,
    /// Producer ID.
    pub producer_id: i64,
    /// Producer epoch.
    pub producer_epoch: i16,
    /// Partitions in the transaction.
    pub partitions: Vec<TopicPartition>,
}

/// A transaction from [`AdminClient::list_transactions`].
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionListing {
    /// Transactional ID.
    pub transactional_id: String,
    /// Producer ID.
    pub producer_id: i64,
    /// State.
    pub state: String,
}

/// The KRaft quorum of the cluster metadata log.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct QuorumInfo {
    /// Leader node ID; `-1` when there is none.
    pub leader_id: BrokerId,
    /// Leader epoch.
    pub leader_epoch: i32,
    /// High watermark.
    pub high_watermark: i64,
    /// Voters.
    pub voters: Vec<QuorumReplica>,
    /// Observers.
    pub observers: Vec<QuorumReplica>,
    /// Quorum nodes and their endpoints (KIP-853, v2+); empty on older
    /// brokers.
    pub nodes: Vec<QuorumNode>,
}

/// A voter or observer of the quorum.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuorumReplica {
    /// Replica node ID.
    pub replica_id: BrokerId,
    /// Directory ID of the replica's log directory (KIP-853, v2+).
    pub directory_id: Option<[u8; 16]>,
    /// Log end offset; `-1` if unknown.
    pub log_end_offset: i64,
    /// Leader time of the replica's last fetch, in epoch milliseconds
    /// (KIP-836, v1+); `None` when unknown.
    pub last_fetch_timestamp: Option<i64>,
    /// Leader time of the offset the replica last caught up to (KIP-836,
    /// v1+); `None` when unknown.
    pub last_caught_up_timestamp: Option<i64>,
}

/// A quorum node and its listeners (KIP-853).
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuorumNode {
    /// Node ID.
    pub node_id: BrokerId,
    /// Listener endpoints.
    pub listeners: Vec<QuorumListener>,
}

/// A listener endpoint of a [`QuorumNode`].
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuorumListener {
    /// Listener name, e.g. `CONTROLLER`.
    pub name: String,
    /// Host.
    pub host: String,
    /// Port.
    pub port: u16,
}

admin_options! {
    /// Options for [`AdminClient::describe_producers`].
    DescribeProducersOptions {}
}

admin_options! {
    /// Options for [`AdminClient::describe_transactions`].
    DescribeTransactionsOptions {}
}

admin_options! {
    /// Options for [`AdminClient::list_transactions`]. Empty filters match
    /// everything.
    ListTransactionsOptions {
        /// Only transactions in these states.
        states: Vec<String>,
        /// Only transactions of these producer IDs.
        producer_ids: Vec<i64>,
    }
    optional {
        /// Only transactions open at least this long.
        min_duration: Duration,
        /// Only transactional IDs matching this regular expression
        /// (KIP-1152, `ListTransactions` v2, Kafka 4.1+).
        transactional_id_pattern: String,
    }
}

admin_options! {
    /// Options for [`AdminClient::abort_transaction`].
    AbortTransactionOptions {}
    optional {
        /// The coordinator epoch to write the markers with. Default: read
        /// from the producer state on the transaction's partitions.
        coordinator_epoch: i32,
    }
}

admin_options! {
    /// Options for [`AdminClient::describe_metadata_quorum`].
    DescribeMetadataQuorumOptions {}
}

fn quorum_replica(state: QuorumReplicaState) -> QuorumReplica {
    QuorumReplica {
        replica_id: state.replica_id,
        directory_id: state.replica_directory_id,
        log_end_offset: state.log_end_offset,
        last_fetch_timestamp: (state.last_fetch_timestamp >= 0)
            .then_some(state.last_fetch_timestamp),
        last_caught_up_timestamp: (state.last_caught_up_timestamp >= 0)
            .then_some(state.last_caught_up_timestamp),
    }
}

impl AdminClient {
    /// Describe the active producers of partitions, at each partition's
    /// leader. Returns a result per partition.
    ///
    /// # Errors
    ///
    /// The call fails for a closed client or an invalid topic name.
    pub async fn describe_producers(
        &self,
        partitions: impl IntoIterator<Item = TopicPartition>,
        options: DescribeProducersOptions,
    ) -> Result<HashMap<TopicPartition, Result<Vec<ProducerState>>>> {
        let mut partitions: Vec<TopicPartition> = partitions.into_iter().collect();
        validate_topics(partitions.iter().map(|tp| tp.topic.as_str()))?;
        partitions.dedup();
        let call = self.call("DescribeProducers", Mode::Read, options.timeout)?;
        Ok(call
            .fan_out(
                partitions,
                |tp| Target::Leader(tp.clone()),
                |conn, partitions| async move {
                    let mut topics: BTreeMap<String, Vec<i32>> = BTreeMap::new();
                    for tp in &partitions {
                        topics
                            .entry(tp.topic.clone())
                            .or_default()
                            .push(tp.partition);
                    }
                    let request = DescribeProducersRequest {
                        topics: topics
                            .into_iter()
                            .map(|(name, partition_indexes)| DescribeProducersTopicRequest {
                                name,
                                partition_indexes,
                            })
                            .collect(),
                    };
                    let version = negotiate(
                        &conn,
                        ApiKey::DescribeProducers,
                        versions::DESCRIBE_PRODUCERS_MIN,
                        versions::DESCRIBE_PRODUCERS_MAX,
                    )?;
                    let response: DescribeProducersResponse =
                        exchange(&conn, ApiKey::DescribeProducers, version, &request).await?;
                    Ok(response
                        .topics
                        .into_iter()
                        .flat_map(|t| {
                            let name = t.name;
                            t.partitions.into_iter().map(move |p| {
                                let producers = answer(p.error_code, p.error_message).map(|()| {
                                    p.active_producers
                                        .into_iter()
                                        .map(|s| ProducerState {
                                            producer_id: s.producer_id,
                                            producer_epoch: s.producer_epoch,
                                            last_sequence: s.last_sequence,
                                            last_timestamp: s.last_timestamp,
                                            coordinator_epoch: s.coordinator_epoch,
                                            current_transaction_start_offset: (s
                                                .current_txn_start_offset
                                                >= 0)
                                                .then_some(s.current_txn_start_offset),
                                        })
                                        .collect()
                                });
                                (
                                    TopicPartition::new(name.clone(), p.partition_index),
                                    producers,
                                )
                            })
                        })
                        .collect())
                },
            )
            .await)
    }

    /// Describe transactions at their coordinators. Returns a result per
    /// transactional ID.
    ///
    /// # Errors
    ///
    /// The call fails for a closed client.
    pub async fn describe_transactions<I, S>(
        &self,
        transactional_ids: I,
        options: DescribeTransactionsOptions,
    ) -> Result<HashMap<String, Result<TransactionDescription>>>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut ids: Vec<String> = transactional_ids
            .into_iter()
            .map(|s| s.as_ref().to_string())
            .collect();
        ids.sort_unstable();
        ids.dedup();
        let call = self.call("DescribeTransactions", Mode::Read, options.timeout)?;
        Ok(call
            .fan_out(
                ids,
                |id| Target::TransactionCoordinator(id.clone()),
                |conn, ids| async move {
                    let request = DescribeTransactionsRequest {
                        transactional_ids: ids,
                    };
                    let version = negotiate(
                        &conn,
                        ApiKey::DescribeTransactions,
                        versions::DESCRIBE_TRANSACTIONS_MIN,
                        versions::DESCRIBE_TRANSACTIONS_MAX,
                    )?;
                    let response: DescribeTransactionsResponse =
                        exchange(&conn, ApiKey::DescribeTransactions, version, &request).await?;
                    Ok(response
                        .transaction_states
                        .into_iter()
                        .map(|s| {
                            let description =
                                answer(s.error_code, None).map(|()| TransactionDescription {
                                    state: s.transaction_state,
                                    timeout: Duration::from_millis(
                                        u64::try_from(s.transaction_timeout_ms).unwrap_or(0),
                                    ),
                                    start_time_ms: s.transaction_start_time_ms,
                                    producer_id: s.producer_id,
                                    producer_epoch: s.producer_epoch,
                                    partitions: s
                                        .topics
                                        .into_iter()
                                        .flat_map(|t| {
                                            let topic = t.topic;
                                            t.partitions
                                                .into_iter()
                                                .map(move |p| TopicPartition::new(topic.clone(), p))
                                        })
                                        .collect(),
                                });
                            (s.transactional_id, description)
                        })
                        .collect())
                },
            )
            .await)
    }

    /// List transactions on every broker. Each broker knows the transactions
    /// it coordinates, so the result is per broker.
    ///
    /// # Errors
    ///
    /// The call fails for a closed client, or when the broker list cannot be
    /// fetched. A broker too old for `transactional_id_pattern` is an `Err`
    /// in its entry rather than an unfiltered listing.
    pub async fn list_transactions(
        &self,
        options: ListTransactionsOptions,
    ) -> Result<HashMap<BrokerId, Result<Vec<TransactionListing>>>> {
        let call = self.call("ListTransactions", Mode::Read, options.timeout)?;
        let brokers = self.broker_ids(&call).await?;
        let request = ListTransactionsRequest {
            state_filters: options.states.clone(),
            producer_id_filters: options.producer_ids.clone(),
            duration_filter: options
                .min_duration
                .map_or(-1, crate::util::duration_to_millis_i64),
            transactional_id_pattern: options.transactional_id_pattern.clone(),
        };
        let request = &request;
        Ok(call
            .fan_out(
                brokers,
                |id| Target::Broker(*id),
                |conn, ids| async move {
                    let version = negotiate(
                        &conn,
                        ApiKey::ListTransactions,
                        versions::LIST_TRANSACTIONS_MIN,
                        versions::LIST_TRANSACTIONS_MAX,
                    )?;
                    let listing = if request.transactional_id_pattern.is_some() && version < 2 {
                        Err(KrafkaError::protocol_kind(
                            ProtocolErrorKind::UnknownApiVersion,
                            format!(
                                "transactional_id_pattern needs ListTransactions v2 (KIP-1152); \
                             the broker negotiated v{version}"
                            ),
                        ))
                    } else {
                        let response: ListTransactionsResponse =
                            exchange(&conn, ApiKey::ListTransactions, version, request).await?;
                        if !response.unknown_state_filters.is_empty() {
                            warn!(
                                unknown = ?response.unknown_state_filters,
                                "ListTransactions: the broker does not know these state filters"
                            );
                        }
                        answer(response.error_code, None).map(|()| {
                            response
                                .transaction_states
                                .into_iter()
                                .map(|s| TransactionListing {
                                    transactional_id: s.transactional_id,
                                    producer_id: s.producer_id,
                                    state: s.transaction_state,
                                })
                                .collect()
                        })
                    };
                    Ok(ids.into_iter().map(|id| (id, listing.clone())).collect())
                },
            )
            .await)
    }

    /// Abort a hanging transaction by writing ABORT markers to each of its
    /// partitions' leaders.
    ///
    /// The partitions and producer come from the transaction's coordinator.
    /// The coordinator epoch comes from
    /// [`AbortTransactionOptions::coordinator_epoch`], or else from the
    /// producer state on those partitions; a partition leader with no cached
    /// epoch accepts any epoch, so one is never invented. Returns a result per
    /// partition; a transaction with no partitions returns none.
    ///
    /// # Errors
    ///
    /// The transaction's describe error; a `Config` error when no epoch was
    /// given and the producer state does not reveal one; a retriable
    /// `CONCURRENT_TRANSACTIONS` when the partitions disagree on the epoch,
    /// which means the transaction is mid-transition.
    pub async fn abort_transaction(
        &self,
        transactional_id: impl Into<String>,
        options: AbortTransactionOptions,
    ) -> Result<HashMap<TopicPartition, Result<()>>> {
        let transactional_id = transactional_id.into();
        let call = self.call("AbortTransaction", Mode::Write, options.timeout)?;

        let description = self
            .describe_transactions(
                [transactional_id.clone()],
                DescribeTransactionsOptions::default().timeout(call.remaining()),
            )
            .await?
            .remove(&transactional_id)
            .unwrap_or_else(|| Err(KrafkaError::timeout("DescribeTransactions")))?;
        if description.partitions.is_empty() {
            return Ok(HashMap::new());
        }

        let coordinator_epoch = match options.coordinator_epoch {
            Some(epoch) => epoch,
            None => {
                let producers = self
                    .describe_producers(
                        description.partitions.clone(),
                        DescribeProducersOptions::default().timeout(call.remaining()),
                    )
                    .await?;
                coordinator_epoch_of(&transactional_id, description.producer_id, producers)?
            }
        };

        info!(
            transactional_id,
            producer_id = description.producer_id,
            coordinator_epoch,
            "aborting transaction by writing ABORT markers"
        );
        let description = &description;
        Ok(call
            .fan_out(
                description.partitions.clone(),
                |tp| Target::Leader(tp.clone()),
                |conn, partitions| async move {
                    let mut topics: BTreeMap<String, Vec<i32>> = BTreeMap::new();
                    for tp in &partitions {
                        topics
                            .entry(tp.topic.clone())
                            .or_default()
                            .push(tp.partition);
                    }
                    let request = WriteTxnMarkersRequest {
                        markers: vec![WritableTxnMarker {
                            producer_id: description.producer_id,
                            producer_epoch: description.producer_epoch,
                            transaction_result: false,
                            topics: topics
                                .into_iter()
                                .map(|(name, partition_indexes)| WritableTxnMarkerTopic {
                                    name,
                                    partition_indexes,
                                })
                                .collect(),
                            coordinator_epoch,
                            // TV2 markers are written by the coordinator; an
                            // administrative abort uses the legacy encoding.
                            transaction_version: WritableTxnMarker::legacy_transaction_version(),
                        }],
                    };
                    let version = negotiate(
                        &conn,
                        ApiKey::WriteTxnMarkers,
                        versions::WRITE_TXN_MARKERS_MIN,
                        versions::WRITE_TXN_MARKERS_MAX,
                    )?;
                    let response: WriteTxnMarkersResponse =
                        exchange(&conn, ApiKey::WriteTxnMarkers, version, &request).await?;
                    Ok(response
                        .markers
                        .into_iter()
                        .flat_map(|m| m.topics)
                        .flat_map(|t| {
                            let name = t.name;
                            t.partitions.into_iter().map(move |p| {
                                (
                                    TopicPartition::new(name.clone(), p.partition_index),
                                    answer(p.error_code, None),
                                )
                            })
                        })
                        .collect())
                },
            )
            .await)
    }

    /// Describe the KRaft quorum of the cluster metadata log (any broker).
    ///
    /// # Errors
    ///
    /// The broker's error, a closed client, or the deadline.
    pub async fn describe_metadata_quorum(
        &self,
        options: DescribeMetadataQuorumOptions,
    ) -> Result<QuorumInfo> {
        let call = self.call("DescribeQuorum", Mode::Read, options.timeout)?;
        call.single(Target::AnyBroker, |conn| async move {
            let request = DescribeQuorumRequest {
                topics: vec![DescribeQuorumTopicRequest {
                    topic_name: CLUSTER_METADATA_TOPIC.to_string(),
                    partitions: vec![DescribeQuorumPartitionRequest { partition_index: 0 }],
                }],
            };
            let version = negotiate(
                &conn,
                ApiKey::DescribeQuorum,
                versions::DESCRIBE_QUORUM_MIN,
                versions::DESCRIBE_QUORUM_MAX,
            )?;
            let response: DescribeQuorumResponse =
                exchange(&conn, ApiKey::DescribeQuorum, version, &request).await?;
            answer(response.error_code, response.error_message)?;
            let partition = response
                .topics
                .into_iter()
                .flat_map(|t| t.partitions)
                .next()
                .ok_or_else(|| {
                    KrafkaError::protocol_kind(
                        ProtocolErrorKind::Malformed,
                        "DescribeQuorum returned no partition",
                    )
                })?;
            answer(partition.error_code, partition.error_message)?;
            Ok(QuorumInfo {
                leader_id: partition.leader_id,
                leader_epoch: partition.leader_epoch,
                high_watermark: partition.high_watermark,
                voters: partition
                    .current_voters
                    .into_iter()
                    .map(quorum_replica)
                    .collect(),
                observers: partition
                    .observers
                    .into_iter()
                    .map(quorum_replica)
                    .collect(),
                nodes: response
                    .nodes
                    .into_iter()
                    .map(|n| QuorumNode {
                        node_id: n.node_id,
                        listeners: n
                            .listeners
                            .into_iter()
                            .map(|l| QuorumListener {
                                name: l.name,
                                host: l.host,
                                port: l.port,
                            })
                            .collect(),
                    })
                    .collect(),
            })
        })
        .await
    }
}

/// The one coordinator epoch the partitions' producer state reports for
/// `producer_id`.
fn coordinator_epoch_of(
    transactional_id: &str,
    producer_id: i64,
    producers: HashMap<TopicPartition, Result<Vec<ProducerState>>>,
) -> Result<i32> {
    let mut found: Option<i32> = None;
    for (tp, states) in producers {
        for state in states? {
            if state.producer_id != producer_id {
                continue;
            }
            match found {
                None => found = Some(state.coordinator_epoch),
                Some(epoch) if epoch == state.coordinator_epoch => {}
                Some(epoch) => {
                    return Err(KrafkaError::broker(
                        ErrorCode::ConcurrentTransactions,
                        format!(
                            "transaction '{transactional_id}' reports coordinator epochs {epoch} \
                             and {} (on {}-{}); it is mid-transition",
                            state.coordinator_epoch, tp.topic, tp.partition
                        ),
                    ));
                }
            }
        }
    }
    found.ok_or_else(|| {
        KrafkaError::config(format!(
            "no producer state for producer {producer_id} on the partitions of transaction \
             '{transactional_id}'; pass AbortTransactionOptions::coordinator_epoch"
        ))
    })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    fn state(producer_id: i64, coordinator_epoch: i32) -> ProducerState {
        ProducerState {
            producer_id,
            producer_epoch: 0,
            last_sequence: 0,
            last_timestamp: 0,
            coordinator_epoch,
            current_transaction_start_offset: Some(0),
        }
    }

    #[test]
    fn the_coordinator_epoch_is_read_from_the_producer_state() {
        let producers = HashMap::from([
            (
                TopicPartition::new("t", 0),
                Ok(vec![state(7, 3), state(8, 9)]),
            ),
            (TopicPartition::new("t", 1), Ok(vec![state(7, 3)])),
        ]);
        assert_eq!(coordinator_epoch_of("tx", 7, producers).unwrap(), 3);
    }

    #[test]
    fn disagreeing_epochs_are_a_retriable_conflict() {
        let producers = HashMap::from([
            (TopicPartition::new("t", 0), Ok(vec![state(7, 3)])),
            (TopicPartition::new("t", 1), Ok(vec![state(7, 4)])),
        ]);
        let err = coordinator_epoch_of("tx", 7, producers).unwrap_err();
        assert!(matches!(
            err,
            KrafkaError::Broker {
                code: ErrorCode::ConcurrentTransactions,
                ..
            }
        ));
        assert!(err.is_retriable());
    }

    #[test]
    fn no_producer_state_never_invents_an_epoch() {
        let producers = HashMap::from([(TopicPartition::new("t", 0), Ok(vec![state(8, 1)]))]);
        let err = coordinator_epoch_of("tx", 7, producers).unwrap_err();
        assert!(matches!(err, KrafkaError::Config { .. }));
    }
}