krafka 0.19.0

A pure Rust, async-native Apache Kafka client
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
//! AdminClient operations: ListOffsets and consumer group lag.

use std::collections::HashMap;

use tracing::{debug, warn};

use crate::error::{ErrorCode, KrafkaError, ProtocolErrorKind, Result};
use crate::protocol::{
    ApiKey, ListOffsetsRequest, ListOffsetsRequestPartition, ListOffsetsRequestTopic,
    ListOffsetsResponse, VersionedDecode, VersionedEncode, validate_topic_names, versions,
};

#[allow(clippy::wildcard_imports)]
use super::*;

impl AdminClient {
    /// List offsets for one or more topic-partitions.
    ///
    /// Each request is routed to the partition's current leader.  Metadata
    /// is refreshed once on `NotLeaderForPartition` errors before retrying.
    ///
    /// # Arguments
    ///
    /// * `topic_partitions` — slice of `(topic_name, partition_ids)` pairs.
    /// * `spec` — which offset to fetch (`Earliest`, `Latest`, or `Timestamp`).
    ///
    /// # Example
    ///
    /// ```ignore
    /// use krafka::admin::{AdminClient, OffsetSpec};
    ///
    /// let results = admin
    ///     .list_offsets(&[("my-topic", &[0, 1, 2])], OffsetSpec::Latest)
    ///     .await?;
    /// for r in &results {
    ///     println!("{}/{}: offset={}", r.topic, r.partition, r.offset);
    /// }
    /// ```
    pub async fn list_offsets(
        &self,
        topic_partitions: &[(&str, &[i32])],
        spec: OffsetSpec,
    ) -> Result<Vec<ListOffsetResult>> {
        self.check_not_closed()?;

        let topics: Vec<&str> = topic_partitions.iter().map(|(t, _)| *t).collect();
        validate_topic_names(topics.iter().copied())?;

        let timestamp = spec.as_timestamp();

        for attempt in 0u8..2 {
            if attempt == 1 {
                // Await a *real* refresh before retrying. A rate-limited
                // refresh returns `RateLimited` without contacting a broker; if
                // that were treated as success the retry would re-issue against
                // byte-identical stale metadata and reproduce the same
                // NotLeaderForPartition forever.
                self.refresh_topics_for_retry(&topics, "ListOffsets").await;
            }

            let brokers = self.metadata.brokers();
            if brokers.is_empty() {
                return Err(KrafkaError::broker(
                    ErrorCode::UnknownServerError,
                    "no brokers available",
                ));
            }

            // Group partitions by their leader broker, carrying each
            // partition's cached leader epoch alongside its index.
            let mut leader_map: HashMap<i32, HashMap<String, Vec<(i32, i32)>>> = HashMap::new();
            let fallback_broker_id = brokers[0].id();

            for &(topic, partitions) in topic_partitions {
                for &partition in partitions {
                    let leader_id = self
                        .metadata
                        .leader(topic, partition)
                        .unwrap_or(fallback_broker_id);
                    // Send the epoch we believe is current so the broker can
                    // fence the request (KIP-320). `-1` disables fencing
                    // entirely and is used only when the epoch is unknown
                    // (Metadata < v7, or the partition is not in the cache).
                    let current_leader_epoch =
                        self.metadata.leader_epoch(topic, partition).unwrap_or(-1);
                    leader_map
                        .entry(leader_id)
                        .or_default()
                        .entry(topic.to_string())
                        .or_default()
                        .push((partition, current_leader_epoch));
                }
            }

            let mut results: Vec<ListOffsetResult> = Vec::new();
            let mut has_stale_leader = false;

            for (broker_id, topics_map) in leader_map {
                let broker = brokers
                    .iter()
                    .find(|b| b.id() == broker_id)
                    .unwrap_or(&brokers[0]);
                let conn = self
                    .pool
                    .get_connection_by_id(broker.id(), broker.address())
                    .await?;

                let request = ListOffsetsRequest {
                    replica_id: -1,     // -1 = consumer
                    isolation_level: 0, // read_uncommitted
                    topics: topics_map
                        .into_iter()
                        .map(|(name, partitions)| ListOffsetsRequestTopic {
                            name,
                            partitions: partitions
                                .into_iter()
                                .map(|(partition_index, current_leader_epoch)| {
                                    ListOffsetsRequestPartition {
                                        partition_index,
                                        current_leader_epoch,
                                        timestamp,
                                    }
                                })
                                .collect(),
                        })
                        .collect(),
                    timeout_ms: None,
                };

                let version = conn
                    .negotiate_api_version(
                        ApiKey::ListOffsets,
                        versions::LIST_OFFSETS_MAX,
                        versions::LIST_OFFSETS_MIN,
                    )
                    .ok_or_else(|| {
                        KrafkaError::protocol_kind(
                            ProtocolErrorKind::UnknownApiVersion,
                            "no mutually supported ListOffsets API version",
                        )
                    })?;

                // The sentinel specs are negative timestamps, so a broker that
                // predates one does not reject it — it answers as if the value
                // were an ordinary timestamp, which for a negative number
                // means the log start. That is a plausible-looking answer to a
                // question the broker never understood, so the mismatch is
                // caught here rather than surfacing as silently wrong data.
                let required = spec.min_api_version();
                if version < required {
                    return Err(KrafkaError::protocol_kind(
                        ProtocolErrorKind::UnknownApiVersion,
                        format!(
                            "OffsetSpec::{} needs ListOffsets v{required}, but the broker \
                             negotiated v{version}; it would answer as though the sentinel \
                             were an ordinary timestamp",
                            spec.name()
                        ),
                    ));
                }

                let response_bytes = conn
                    .send_request(ApiKey::ListOffsets, version, |buf| {
                        request.encode_versioned(version, buf)
                    })
                    .await?;

                let mut buf = response_bytes;
                let response = ListOffsetsResponse::decode_versioned(version, &mut buf)?;

                for topic in response.topics {
                    for partition in topic.partitions {
                        // Now that a real `current_leader_epoch` is sent, the
                        // broker can also reject the request with a fenced or
                        // unknown epoch. All three mean "your metadata is
                        // stale" and are cured by the same refresh.
                        if matches!(
                            partition.error_code,
                            ErrorCode::NotLeaderForPartition
                                | ErrorCode::FencedLeaderEpoch
                                | ErrorCode::UnknownLeaderEpoch
                        ) {
                            has_stale_leader = true;
                        }
                        results.push(ListOffsetResult {
                            topic: topic.name.clone(),
                            partition: partition.partition_index,
                            offset: partition.offset,
                            timestamp: partition.timestamp,
                            error: if partition.error_code.is_ok() {
                                None
                            } else {
                                Some(format!("{:?}", partition.error_code))
                            },
                        });
                    }
                }
            }

            if has_stale_leader && attempt == 0 {
                warn!("stale leader metadata in ListOffsets response, retrying after a refresh");
                continue;
            }

            debug!("ListOffsets returned {} partition result(s)", results.len());
            return Ok(results);
        }

        Err(KrafkaError::protocol_kind(
            ProtocolErrorKind::Malformed,
            "ListOffsets retry loop exhausted after metadata refresh",
        ))
    }

    /// Compute consumer group lag for the specified topics.
    ///
    /// Lag is defined as `end_offset − committed_offset` for each
    /// topic-partition.  Partitions with no committed offset have
    /// `committed_offset = None` and `lag = None`.
    ///
    /// Partitions whose end offset could not be fetched report
    /// `end_offset = None`, `lag = None`, and the reason in
    /// [`ConsumerGroupLag::end_offset_error`] — never `lag = 0`, which would
    /// make a stalled consumer look healthy.
    ///
    /// This method issues two parallel-ish requests:
    /// 1. [`describe_consumer_group_offsets`] for the committed positions.
    /// 2. [`list_offsets`] with [`OffsetSpec::Latest`] for the end offsets.
    ///
    /// The consumer group does **not** need to be stopped.
    ///
    /// # Arguments
    ///
    /// * `group_id` — consumer group ID.
    /// * `topic_partitions` — which partitions to measure; pass `None` to
    ///   measure all partitions that the group has committed offsets for.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let lag = admin
    ///     .consumer_group_lag("my-group", Some(&[("my-topic", &[0, 1, 2])]))
    ///     .await?;
    /// for entry in &lag {
    ///     println!(
    ///         "{}/{}: lag={:?}",
    ///         entry.topic, entry.partition, entry.lag
    ///     );
    /// }
    /// ```
    ///
    /// [`describe_consumer_group_offsets`]: AdminClient::describe_consumer_group_offsets
    /// [`list_offsets`]: AdminClient::list_offsets
    pub async fn consumer_group_lag(
        &self,
        group_id: &str,
        topic_partitions: Option<&[(&str, &[i32])]>,
    ) -> Result<Vec<ConsumerGroupLag>> {
        self.check_not_closed()?;

        // 1. Fetch committed offsets.
        //
        // `IncludeUnstable`: lag is a monitoring signal, and reporting the
        // freshest committed position is more useful here than refusing to
        // answer because some partition has a transaction in flight. A lag
        // number that dips when a transaction aborts is the honest shape of
        // the underlying data; an error is not.
        let committed = self
            .describe_consumer_group_offsets(
                group_id,
                topic_partitions,
                OffsetVisibility::IncludeUnstable,
            )
            .await?;

        if committed.is_empty() {
            return Ok(Vec::new());
        }

        // 2. Build the (topic, partitions) list for list_offsets.
        //    Group by topic, collect unique partition IDs.
        let mut by_topic: HashMap<String, Vec<i32>> = HashMap::new();
        for entry in &committed {
            by_topic
                .entry(entry.topic.clone())
                .or_default()
                .push(entry.partition);
        }

        // Deduplicate partition lists (describe_consumer_group_offsets may
        // return duplicate entries if the same partition appears multiple times
        // in a group's state, which is rare but possible during rebalance).
        for partitions in by_topic.values_mut() {
            partitions.sort_unstable();
            partitions.dedup();
        }

        let topic_partition_refs: Vec<(&str, &[i32])> = by_topic
            .iter()
            .map(|(t, ps)| (t.as_str(), ps.as_slice()))
            .collect();

        // 3. Fetch end offsets.
        let end_offsets = self
            .list_offsets(&topic_partition_refs, OffsetSpec::Latest)
            .await?;

        // 4. Build a lookup map: (topic, partition) → Ok(end_offset) | Err(reason).
        //    Both the per-partition error and a negative sentinel offset mean
        //    the end offset is unknown; neither may be silently coerced to 0.
        let mut end_map: HashMap<(&str, i32), std::result::Result<i64, String>> = HashMap::new();
        for r in &end_offsets {
            let value = match &r.error {
                Some(e) => Err(e.clone()),
                None if r.offset < 0 => {
                    Err(format!("ListOffsets returned sentinel offset {}", r.offset))
                }
                None => Ok(r.offset),
            };
            end_map.insert((r.topic.as_str(), r.partition), value);
        }

        // 5. Compute lag for each committed entry.
        let mut lag_results = Vec::with_capacity(committed.len());
        for entry in &committed {
            // Treat -1 as "no committed offset" (Kafka wire sentinel).
            let committed_offset = if entry.committed_offset == -1 {
                None
            } else {
                Some(entry.committed_offset)
            };

            let (end_offset, end_offset_error) =
                match end_map.get(&(entry.topic.as_str(), entry.partition)) {
                    Some(Ok(offset)) => (Some(*offset), None),
                    Some(Err(reason)) => (None, Some(reason.clone())),
                    None => (
                        None,
                        Some("no ListOffsets result for this partition".to_string()),
                    ),
                };

            // Lag is only meaningful when *both* ends are known. An unknown end
            // offset must not report lag 0 — that hides a stalled consumer from
            // alerting.
            let lag = match (committed_offset, end_offset) {
                (Some(co), Some(eo)) => Some((eo - co).max(0)),
                _ => None,
            };

            if let Some(ref reason) = end_offset_error {
                warn!(
                    topic = %entry.topic,
                    partition = entry.partition,
                    "end offset unknown, lag cannot be computed: {reason}"
                );
            }

            lag_results.push(ConsumerGroupLag {
                topic: entry.topic.clone(),
                partition: entry.partition,
                committed_offset,
                end_offset,
                end_offset_error,
                lag,
            });
        }

        debug!(
            "consumer_group_lag for group {group_id}: {} partition(s)",
            lag_results.len()
        );
        Ok(lag_results)
    }
}

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

    #[test]
    fn test_offset_spec_as_timestamp() {
        assert_eq!(OffsetSpec::Earliest.as_timestamp(), -2);
        assert_eq!(OffsetSpec::Latest.as_timestamp(), -1);
        assert_eq!(
            OffsetSpec::Timestamp(1_705_276_800).as_timestamp(),
            1_705_276_800
        );
    }

    #[test]
    fn test_consumer_group_lag_struct_fields() {
        let lag = ConsumerGroupLag {
            topic: "test".to_string(),
            partition: 0,
            committed_offset: Some(100),
            end_offset: Some(150),
            end_offset_error: None,
            lag: Some(50),
        };
        assert_eq!(lag.lag, Some(50));
        assert_eq!(lag.end_offset, Some(150));
        assert_eq!(lag.committed_offset, Some(100));
    }

    #[test]
    fn test_list_offset_result_struct() {
        let r = ListOffsetResult {
            topic: "my-topic".to_string(),
            partition: 1,
            offset: 42,
            timestamp: -1,
            error: None,
        };
        assert_eq!(r.offset, 42);
        assert!(r.error.is_none());
    }

    /// `end_offset` previously defaulted to -1 when ListOffsets failed,
    /// and `lag = (end_offset - committed).max(0)` then reported 0 — making a
    /// stalled consumer look perfectly healthy to alerting.
    #[test]
    fn test_unknown_end_offset_reports_unknown_lag_not_zero() {
        let committed = 100i64;

        // What the old code did.
        let old_end_offset = -1i64;
        let old_lag = (old_end_offset - committed).max(0);
        assert_eq!(old_lag, 0, "this is the bug being fixed");

        // What the new code does: no end offset means no lag.
        let end_offset: Option<i64> = None;
        let new_lag = match (Some(committed), end_offset) {
            (Some(co), Some(eo)) => Some((eo - co).max(0)),
            _ => None,
        };
        assert_eq!(new_lag, None);
    }

    #[test]
    fn test_lag_is_computed_when_both_ends_are_known() {
        let lag = match (Some(100i64), Some(150i64)) {
            (Some(co), Some(eo)) => Some((eo - co).max(0)),
            _ => None,
        };
        assert_eq!(lag, Some(50));
    }

    /// A commit ahead of the watermark (e.g. after a manual offset reset)
    /// clamps to zero rather than reporting negative lag.
    #[test]
    fn test_lag_clamps_negative_to_zero() {
        let lag = match (Some(200i64), Some(150i64)) {
            (Some(co), Some(eo)) => Some((eo - co).max(0)),
            _ => None,
        };
        assert_eq!(lag, Some(0));
    }

    /// A negative offset from ListOffsets is a sentinel, not a position, and
    /// must be treated as "unknown" just like an explicit error.
    #[test]
    fn test_negative_sentinel_offset_counts_as_unknown() {
        let classify = |error: Option<&str>, offset: i64| -> std::result::Result<i64, String> {
            match error {
                Some(e) => Err(e.to_string()),
                None if offset < 0 => Err(format!("ListOffsets returned sentinel offset {offset}")),
                None => Ok(offset),
            }
        };

        assert_eq!(classify(None, 150), Ok(150));
        assert!(
            classify(None, -1).is_err(),
            "sentinel must not be a position"
        );
        assert!(classify(Some("NotLeaderForPartition"), 150).is_err());
    }

    /// A partition missing entirely from the ListOffsets response must also
    /// report unknown, not silently inherit a default.
    #[test]
    fn test_missing_partition_reports_unknown_end_offset() {
        let end_map: HashMap<(&str, i32), std::result::Result<i64, String>> = HashMap::new();

        let (end_offset, err) = match end_map.get(&("orders", 0)) {
            Some(Ok(o)) => (Some(*o), None),
            Some(Err(e)) => (None, Some(e.clone())),
            None => (
                None,
                Some("no ListOffsets result for this partition".to_string()),
            ),
        };

        assert_eq!(end_offset, None);
        assert!(err.is_some());
    }

    /// The cached leader epoch must be sent so the broker can fence a stale
    /// request (KIP-320). Pinning -1 disabled that protection entirely.
    #[test]
    fn test_list_offsets_request_carries_the_leader_epoch() {
        let request = ListOffsetsRequest {
            replica_id: -1,
            isolation_level: 0,
            topics: vec![ListOffsetsRequestTopic {
                name: "orders".into(),
                partitions: vec![
                    ListOffsetsRequestPartition {
                        partition_index: 0,
                        current_leader_epoch: 42,
                        timestamp: OffsetSpec::Latest.as_timestamp(),
                    },
                    ListOffsetsRequestPartition {
                        partition_index: 1,
                        // -1 only when the epoch is genuinely unknown.
                        current_leader_epoch: -1,
                        timestamp: OffsetSpec::Latest.as_timestamp(),
                    },
                ],
            }],
            timeout_ms: None,
        };

        assert_eq!(request.topics[0].partitions[0].current_leader_epoch, 42);
        assert_eq!(request.topics[0].partitions[1].current_leader_epoch, -1);

        let mut buf = Vec::new();
        assert!(
            request
                .encode_versioned(versions::LIST_OFFSETS_MAX, &mut buf)
                .is_ok(),
            "ListOffsets must encode"
        );
        assert!(!buf.is_empty());
    }

    /// Now that a real epoch is sent, the broker can reject with a fenced or
    /// unknown epoch; all three codes mean "metadata is stale" and must trigger
    /// the same refresh-and-retry.
    #[test]
    fn test_stale_leader_detection_covers_epoch_errors() {
        let is_stale = |c: ErrorCode| {
            matches!(
                c,
                ErrorCode::NotLeaderForPartition
                    | ErrorCode::FencedLeaderEpoch
                    | ErrorCode::UnknownLeaderEpoch
            )
        };

        assert!(is_stale(ErrorCode::NotLeaderForPartition));
        assert!(is_stale(ErrorCode::FencedLeaderEpoch));
        assert!(is_stale(ErrorCode::UnknownLeaderEpoch));

        assert!(!is_stale(ErrorCode::None));
        assert!(!is_stale(ErrorCode::OffsetOutOfRange));
        assert!(!is_stale(ErrorCode::TopicAuthorizationFailed));
    }
}