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
//! The ack book: acknowledgements waiting for, or riding on, a request.
//!
//! Acknowledgements are kept per partition and per **acquiring node**. A
//! record's acquisition belongs to the broker that handed it out, so its
//! acknowledgement is only ever sent there; when that broker no longer leads
//! the partition the acknowledgement fails locally instead of being re-routed
//! to a broker that never acquired the record.

use std::sync::Arc;
use tokio::time::Instant;

use ahash::AHashMap as HashMap;

use super::commit::CommitWaiter;
use crate::consumer::TopicPartition;
use crate::error::{ErrorCode, KrafkaError};
use crate::protocol::ShareAcknowledgementBatch;
use crate::{BrokerId, Offset};

/// KIP-932 acknowledgement types, by wire value (KIP-1222 adds `Renew`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AckType {
    /// The client takes no delivery of an acquired offset that holds no
    /// deliverable record (a control record, or an offset compacted away).
    Gap = 0,
    Accept = 1,
    Release = 2,
    Reject = 3,
    /// Extend the acquisition lock without completing the record.
    Renew = 4,
}

/// A run of offsets acknowledged with one type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct AckRange {
    pub first: Offset,
    pub last: Offset,
    pub kind: AckType,
}

impl AckRange {
    pub(crate) fn one(offset: Offset, kind: AckType) -> Self {
        Self {
            first: offset,
            last: offset,
            kind,
        }
    }
}

/// How a failed acknowledgement is handled.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AckFailure {
    /// The request did not reach a usable session: send it again to the same
    /// node once its session is re-established.
    Retry,
    /// The node no longer leads the partition: report it and refresh metadata.
    NotLeader,
    /// The broker refused it: report it and drop it.
    Permanent,
}

/// Classify an acknowledgement error.
pub(crate) fn classify(error: &KrafkaError) -> AckFailure {
    match error {
        KrafkaError::Network(_) | KrafkaError::Timeout { .. } => AckFailure::Retry,
        KrafkaError::Broker { code, .. } => classify_code(*code),
        _ => AckFailure::Permanent,
    }
}

/// Classify a broker error code on a share request or partition.
pub(crate) fn classify_code(code: ErrorCode) -> AckFailure {
    if is_session_error(code) || code == ErrorCode::RequestTimedOut {
        AckFailure::Retry
    } else if is_leader_error(code) {
        AckFailure::NotLeader
    } else {
        AckFailure::Permanent
    }
}

/// A code that means the node's share session is gone or out of step.
pub(crate) fn is_session_error(code: ErrorCode) -> bool {
    matches!(
        code,
        ErrorCode::ShareSessionNotFound
            | ErrorCode::InvalidShareSessionEpoch
            | ErrorCode::ShareSessionLimitReached
    )
}

/// A code that means the node does not lead the partition (any more).
pub(crate) fn is_leader_error(code: ErrorCode) -> bool {
    matches!(
        code,
        ErrorCode::NotLeaderForPartition
            | ErrorCode::FencedLeaderEpoch
            | ErrorCode::UnknownLeaderEpoch
            | ErrorCode::UnknownTopicOrPartition
            | ErrorCode::UnknownTopicId
    )
}

/// The acknowledgements of one partition acquired from one node.
#[derive(Debug)]
pub(crate) struct AckEntry {
    pub topic_id: [u8; 16],
    /// Waiting for the node's next request.
    pub pending: Vec<AckRange>,
    /// Carried by the node's request in flight.
    pub in_flight: Vec<AckRange>,
    /// When the oldest pending acknowledgement was queued.
    pub queued_at: Instant,
    /// `commit()`/`close()` calls waiting for this entry to empty.
    pub waiters: Vec<Arc<CommitWaiter>>,
}

impl AckEntry {
    fn new(topic_id: [u8; 16]) -> Self {
        Self {
            topic_id,
            pending: Vec::new(),
            in_flight: Vec::new(),
            queued_at: Instant::now(),
            waiters: Vec::new(),
        }
    }

    fn is_empty(&self) -> bool {
        self.pending.is_empty() && self.in_flight.is_empty()
    }
}

/// Key of an [`AckEntry`]: the acquiring node and the partition.
pub(crate) type AckKey = (BrokerId, TopicPartition);

/// The outcome of a resolved acknowledgement, for reporting.
#[derive(Debug)]
pub(crate) struct Resolved {
    pub partition: TopicPartition,
    pub ranges: Vec<AckRange>,
    pub result: Result<(), KrafkaError>,
}

/// Every acknowledgement the consumer has not resolved yet.
#[derive(Debug, Default)]
pub(crate) struct AckBook {
    entries: HashMap<AckKey, AckEntry>,
}

impl AckBook {
    /// Queue `range` for the partition's acquiring `node`. A later
    /// acknowledgement of an offset replaces a pending earlier one, so a
    /// renewed record that is then accepted is sent once, as accepted.
    pub(crate) fn add(
        &mut self,
        node: BrokerId,
        partition: TopicPartition,
        topic_id: [u8; 16],
        range: AckRange,
    ) {
        let entry = self
            .entries
            .entry((node, partition))
            .or_insert_with(|| AckEntry::new(topic_id));
        if entry.pending.is_empty() {
            entry.queued_at = Instant::now();
        }
        insert_range(&mut entry.pending, range);
    }

    /// Whether `node` has acknowledgements waiting to be sent.
    pub(crate) fn has_pending(&self, node: BrokerId) -> bool {
        self.entries
            .iter()
            .any(|((n, _), e)| *n == node && !e.pending.is_empty())
    }

    /// The nodes that have acknowledgements.
    pub(crate) fn nodes(&self) -> Vec<BrokerId> {
        let mut nodes: Vec<BrokerId> = self.entries.keys().map(|(n, _)| *n).collect();
        nodes.sort_unstable();
        nodes.dedup();
        nodes
    }

    /// Iterate the pending entries of `node`.
    pub(crate) fn pending_for(
        &self,
        node: BrokerId,
    ) -> impl Iterator<Item = (&TopicPartition, &AckEntry)> {
        self.entries
            .iter()
            .filter(move |((n, _), e)| *n == node && !e.pending.is_empty())
            .map(|((_, tp), e)| (tp, e))
    }

    /// Move the pending acknowledgements of `node` for `partitions` (all of
    /// them when `None`) in flight, returning what to put on the wire.
    pub(crate) fn take(
        &mut self,
        node: BrokerId,
        partitions: Option<&[TopicPartition]>,
    ) -> Vec<(TopicPartition, [u8; 16], Vec<AckRange>)> {
        let mut out = Vec::new();
        for ((n, tp), entry) in &mut self.entries {
            if *n != node || entry.pending.is_empty() || !entry.in_flight.is_empty() {
                continue;
            }
            if partitions.is_some_and(|ps| !ps.contains(tp)) {
                continue;
            }
            entry.in_flight = std::mem::take(&mut entry.pending);
            out.push((tp.clone(), entry.topic_id, entry.in_flight.clone()));
        }
        out.sort_by(|a, b| (&a.0.topic, a.0.partition).cmp(&(&b.0.topic, b.0.partition)));
        out
    }

    /// Settle the in-flight acknowledgements of one entry.
    ///
    /// `Ok`, and failures that are not retried, resolve them. A failure of the
    /// [`AckFailure::Retry`] class puts them back in front of anything queued
    /// since, provided `retry_cutoff` is given and they were queued after it;
    /// otherwise it resolves them as timed out.
    pub(crate) fn settle(
        &mut self,
        node: BrokerId,
        partition: &TopicPartition,
        result: Result<(), KrafkaError>,
        retry_cutoff: Option<Instant>,
    ) -> Option<Resolved> {
        let key = (node, partition.clone());
        let entry = self.entries.get_mut(&key)?;
        let in_flight = std::mem::take(&mut entry.in_flight);
        if in_flight.is_empty() {
            return None;
        }
        let result = match result {
            Err(error) if classify(&error) == AckFailure::Retry => {
                if retry_cutoff.is_some_and(|cutoff| entry.queued_at >= cutoff) {
                    let mut retried = in_flight;
                    for range in std::mem::take(&mut entry.pending) {
                        insert_range(&mut retried, range);
                    }
                    entry.pending = retried;
                    return None;
                }
                Err(KrafkaError::timeout(format!(
                    "acknowledging {}-{} on node {node} ({error})",
                    partition.topic, partition.partition
                )))
            }
            other => other,
        };
        let resolved = Resolved {
            partition: partition.clone(),
            ranges: in_flight,
            result,
        };
        self.finish(&key, &resolved);
        Some(resolved)
    }

    /// Resolve every pending acknowledgement of the entries `select` picks
    /// with `error`, without sending them.
    pub(crate) fn fail_pending(
        &mut self,
        mut select: impl FnMut(&AckKey, &AckEntry) -> bool,
        error: impl Fn(&AckKey) -> KrafkaError,
    ) -> Vec<Resolved> {
        let keys: Vec<AckKey> = self
            .entries
            .iter()
            .filter(|(k, e)| !e.pending.is_empty() && select(k, e))
            .map(|(k, _)| k.clone())
            .collect();
        let mut out = Vec::new();
        for key in keys {
            let Some(entry) = self.entries.get_mut(&key) else {
                continue;
            };
            let resolved = Resolved {
                partition: key.1.clone(),
                ranges: std::mem::take(&mut entry.pending),
                result: Err(error(&key)),
            };
            self.finish(&key, &resolved);
            out.push(resolved);
        }
        out
    }

    /// Attach `waiter` to every entry, returning how many it waits for.
    pub(crate) fn attach(&mut self, waiter: &Arc<CommitWaiter>) -> usize {
        for entry in self.entries.values_mut() {
            entry.waiters.push(Arc::clone(waiter));
        }
        self.entries.len()
    }

    /// Detach `waiter` from every entry.
    pub(crate) fn detach(&mut self, waiter: &Arc<CommitWaiter>) {
        for entry in self.entries.values_mut() {
            entry.waiters.retain(|w| !Arc::ptr_eq(w, waiter));
        }
    }

    /// The partitions of the entries `waiter` is attached to.
    pub(crate) fn waited_by(&self, waiter: &Arc<CommitWaiter>) -> Vec<TopicPartition> {
        self.entries
            .iter()
            .filter(|(_, e)| e.waiters.iter().any(|w| Arc::ptr_eq(w, waiter)))
            .map(|((_, tp), _)| tp.clone())
            .collect()
    }

    /// Whether any pending entry of `node` has a waiter attached.
    pub(crate) fn has_waiters(&self, node: BrokerId) -> bool {
        self.pending_for(node).any(|(_, e)| !e.waiters.is_empty())
    }

    /// Record a resolution with the entry's waiters and drop the entry once
    /// nothing of it is left.
    fn finish(&mut self, key: &AckKey, resolved: &Resolved) {
        let Some(entry) = self.entries.get(key) else {
            return;
        };
        for waiter in &entry.waiters {
            waiter.record(&resolved.partition, &resolved.result);
        }
        if entry.is_empty()
            && let Some(entry) = self.entries.remove(key)
        {
            for waiter in entry.waiters {
                waiter.entry_done();
            }
        }
    }
}

/// Insert `range` into `ranges`, replacing whatever it overlaps.
fn insert_range(ranges: &mut Vec<AckRange>, range: AckRange) {
    if let Some(last) = ranges.last_mut() {
        if last.last < range.first {
            if last.kind == range.kind && last.last + 1 == range.first {
                last.last = range.last;
            } else {
                ranges.push(range);
            }
            return;
        }
    } else {
        ranges.push(range);
        return;
    }
    let mut out = Vec::with_capacity(ranges.len() + 1);
    for existing in ranges.drain(..) {
        if existing.last < range.first || existing.first > range.last {
            out.push(existing);
            continue;
        }
        if existing.first < range.first {
            out.push(AckRange {
                last: range.first - 1,
                ..existing
            });
        }
        if existing.last > range.last {
            out.push(AckRange {
                first: range.last + 1,
                ..existing
            });
        }
    }
    out.push(range);
    out.sort_unstable_by_key(|r| r.first);
    *ranges = out;
}

/// The wire form of `ranges`: sorted by offset, contiguous runs of one type
/// merged, which is the form the Java client sends.
pub(crate) fn to_batches(ranges: &[AckRange]) -> Vec<ShareAcknowledgementBatch> {
    let mut sorted = ranges.to_vec();
    sorted.sort_unstable_by_key(|r| r.first);
    let mut batches: Vec<ShareAcknowledgementBatch> = Vec::with_capacity(sorted.len());
    for range in sorted {
        let kind = range.kind as i8;
        match batches.last_mut() {
            Some(prev)
                if prev.last_offset.checked_add(1) == Some(range.first)
                    && prev.acknowledge_types == [kind] =>
            {
                prev.last_offset = range.last;
            }
            _ => batches.push(ShareAcknowledgementBatch {
                first_offset: range.first,
                last_offset: range.last,
                acknowledge_types: vec![kind],
            }),
        }
    }
    batches
}

/// Whether any range renews a lock.
pub(crate) fn has_renew(ranges: &[AckRange]) -> bool {
    ranges.iter().any(|r| r.kind == AckType::Renew)
}