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
//! Background `ShareGroupHeartbeat` loop for a [`ShareConsumer`].
//!
//! This loop mirrors the classic [`coordinator::run`](coordinator::run)
//! shape: an interval ticker plus a `tokio::select!` that races each heartbeat
//! RPC against a shutdown token, so `close()` returns promptly. Each tick sends
//! a `ShareGroupHeartbeat` (API key 76) with the live member epoch. On success
//! the loop adopts the broker-returned epoch, and the new assignment when the
//! response carries one.
//!
//! Share-group membership has no Join/Sync handshake. The heartbeat *is* the
//! join. A from-scratch rejoin therefore only resets the member epoch to 0 and
//! re-sends `subscribed_topic_names`. The broker hands back a fresh epoch and
//! assignment on the next ok.

use std::{collections::HashMap, sync::Arc};

use krabka_client_core::Client;
use krabka_protocol::{
    owned::{
        share_group_heartbeat_request::ShareGroupHeartbeatRequest,
        share_group_heartbeat_response::Assignment,
    },
    primitives::uuid::Uuid as WireUuid,
};
use krabka_units::{Time, convert::TimeExt as _};
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;

use crate::{coordinator, error::ConsumerError};

/// `FENCED_MEMBER_EPOCH`. The member epoch is behind the broker's. Rejoin.
const FENCED_MEMBER_EPOCH: i16 = 110;
/// `UNKNOWN_MEMBER_ID`. The broker has forgotten this member because the
/// session expired. Rejoin.
const UNKNOWN_MEMBER_ID: i16 = 25;
/// `STALE_MEMBER_EPOCH`. Same family as fenced. Rejoin from scratch.
const STALE_MEMBER_EPOCH: i16 = 113;

/// State owned by the share-group heartbeat task.
///
/// The `Arc<Mutex<...>>` fields are shared with the parent [`ShareConsumer`],
/// so `poll()` sees the live member epoch and assignment as the broker
/// rebalances the group.
pub(crate) struct ShareCoordinatorState {
    pub client: Client,
    pub group_id: String,
    pub member_id: String,
    pub member_epoch: Arc<Mutex<i32>>,
    /// Live assignment as `(topic_id, topic_name, partition)`.
    pub assignment: Arc<Mutex<Vec<(WireUuid, String, i32)>>>,
    pub topic_names: Arc<Mutex<HashMap<WireUuid, String>>>,
    pub subscribe: Vec<String>,
    pub heartbeat_interval: Time,
    pub leave_heartbeat_timeout: Time,
    /// A fatal heartbeat error that the next `poll()` returns. This slot is
    /// shared with the parent [`ShareConsumer`].
    pub poll_error: coordinator::PollErrorSlot,
}

/// Outcome of a single `ShareGroupHeartbeat` RPC.
pub(crate) enum HeartbeatOutcome {
    /// `error_code == 0`, the steady state.
    Ok,
    /// Fenced, unknown-member, or stale-epoch. Reset to epoch 0 and re-send the
    /// subscription on the next tick, so the broker re-admits this member.
    RejoinFromScratch,
    /// Transport error or unexpected non-fatal code. Retry on the next tick.
    Transient,
}

fn build_leave_heartbeat_request(
    group_id: String,
    member_id: String,
) -> ShareGroupHeartbeatRequest {
    ShareGroupHeartbeatRequest {
        group_id,
        member_id,
        member_epoch: -1,
        ..Default::default()
    }
}

fn build_heartbeat_request(
    group_id: String,
    member_id: String,
    member_epoch: i32,
    subscribe: Option<Vec<String>>,
) -> ShareGroupHeartbeatRequest {
    ShareGroupHeartbeatRequest {
        group_id,
        member_id,
        member_epoch,
        subscribed_topic_names: subscribe,
        ..Default::default()
    }
}

fn heartbeat_result(error_code: i16) -> HeartbeatOutcome {
    if error_code == 0 {
        HeartbeatOutcome::Ok
    } else if is_rejoin_error(error_code) {
        HeartbeatOutcome::RejoinFromScratch
    } else {
        HeartbeatOutcome::Transient
    }
}

fn is_rejoin_error(error_code: i16) -> bool {
    matches!(
        error_code,
        FENCED_MEMBER_EPOCH | UNKNOWN_MEMBER_ID | STALE_MEMBER_EPOCH
    )
}

/// Drive the heartbeat loop until `shutdown` fires.
#[cfg_attr(test, mutants::skip)] // cargo-mutants: long-running I/O event loop, exercised by integration tests
pub(crate) async fn run(state: ShareCoordinatorState, shutdown: CancellationToken) {
    let mut ticker = tokio::time::interval(state.heartbeat_interval.to_std());
    ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
    // After a fence/unknown-member we must re-send `subscribed_topic_names` on
    // the next heartbeat (the broker treats epoch 0 + subscription as a join).
    let mut rejoining = false;

    loop {
        tokio::select! {
            () = shutdown.cancelled() => break,
            _ = ticker.tick() => {}
        }

        // Race the RPC against shutdown so `close()` is prompt even when the
        // broker is slow. The send is cancellation-safe: `Client` multiplexes
        // on correlation ids, so dropping an in-flight send only abandons its
        // pending response.
        tokio::select! {
            () = shutdown.cancelled() => break,
            outcome = heartbeat_once(&state, rejoining) => match outcome {
                HeartbeatOutcome::Ok => rejoining = false,
                HeartbeatOutcome::Transient => {}
                HeartbeatOutcome::RejoinFromScratch => {
                    *state.member_epoch.lock().await = 0;
                    rejoining = true;
                }
            },
        }
    }

    // Graceful departure: a leave heartbeat (`member_epoch = -1`) tells the
    // broker to evict us now rather than waiting out the session timeout.
    // Best-effort and bounded so a hung broker can't block `close()`.
    leave_group(&state).await;
}

async fn leave_group(state: &ShareCoordinatorState) {
    let leave = state.client.send(build_leave_heartbeat_request(
        state.group_id.clone(),
        state.member_id.clone(),
    ));
    let _ = tokio::time::timeout(state.leave_heartbeat_timeout.to_std(), leave).await;
}

/// Send one `ShareGroupHeartbeat` and translate the response into a directive.
///
/// When `rejoining` is true, this function re-sends `subscribed_topic_names`,
/// because the broker needs the subscription on a fresh join. Otherwise it
/// sends `None`, because the broker remembers the subscription across
/// steady-state heartbeats.
#[tracing::instrument(
    name = "share_consumer.heartbeat",
    level = "debug",
    skip_all,
    fields(group_id = %state.group_id, member_id = %state.member_id, rejoining)
)]
pub(crate) async fn heartbeat_once(
    state: &ShareCoordinatorState,
    rejoining: bool,
) -> HeartbeatOutcome {
    let epoch = *state.member_epoch.lock().await;
    let subscribed = if rejoining {
        Some(state.subscribe.clone())
    } else {
        None
    };
    let result = state
        .client
        .send(build_heartbeat_request(
            state.group_id.clone(),
            state.member_id.clone(),
            epoch,
            subscribed,
        ))
        .await;
    match result {
        Ok(r) => match heartbeat_result(r.error_code) {
            HeartbeatOutcome::Ok => {
                *state.member_epoch.lock().await = r.member_epoch;
                if let Some(assignment) = r.assignment {
                    update_assignment(state, assignment).await;
                }
                HeartbeatOutcome::Ok
            }
            HeartbeatOutcome::RejoinFromScratch => {
                tracing::warn!(
                    error_code = r.error_code,
                    "share heartbeat fenced; rejoining from epoch 0"
                );
                HeartbeatOutcome::RejoinFromScratch
            }
            HeartbeatOutcome::Transient => {
                tracing::warn!(
                    error_code = r.error_code,
                    "unexpected share heartbeat error"
                );
                HeartbeatOutcome::Transient
            }
        },
        Err(e) => {
            tracing::warn!(error = %e, "share heartbeat send failed");
            // Kafka's `AbstractHeartbeatRequestManager.onFailure` gives a
            // non-retriable error, such as an `AuthenticationException`, to
            // the application `poll` as an `ErrorEvent`.
            coordinator::report_rejoin_error(&state.poll_error, ConsumerError::Client(e));
            HeartbeatOutcome::Transient
        }
    }
}

/// Replace the shared assignment with the broker-returned topic/partition set.
///
/// This function resolves topic names from the cached `topic_names` map. A
/// topic id that the map does not know yet falls back to the id's hex form, so
/// `poll()` still has a stable display name. A later Metadata refresh fixes it.
async fn update_assignment(state: &ShareCoordinatorState, assignment: Assignment) {
    let names = state.topic_names.lock().await;
    let mut next: Vec<(WireUuid, String, i32)> = Vec::new();
    for tp in &assignment.topic_partitions {
        let name = names
            .get(&tp.topic_id)
            .cloned()
            .unwrap_or_else(|| hex_topic_id(tp.topic_id));
        for &partition in &tp.partitions {
            next.push((tp.topic_id, name.clone(), partition));
        }
    }
    drop(names);
    *state.assignment.lock().await = next;
}

/// Hex display for a topic id whose name is not resolved yet.
///
/// `Uuid` has no `Display`. This function gives `poll()` a stable, non-empty
/// placeholder name until a Metadata refresh fills the real one in.
fn hex_topic_id(id: WireUuid) -> String {
    use std::fmt::Write as _;
    id.0.iter().fold(String::with_capacity(32), |mut s, b| {
        let _ = write!(s, "{b:02x}");
        s
    })
}

#[cfg(test)]
mod tests {
    use std::{
        sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        },
        time::Duration,
    };

    use bytes::BytesMut;
    use krabka_client_core::MockBroker;
    use krabka_protocol::{
        Encode,
        owned::{
            api_versions_request,
            api_versions_response::{ApiVersion, ApiVersionsResponse},
            common::share_group_heartbeat_response::topic_partitions::TopicPartitions,
            share_group_heartbeat_request,
        },
        tagged_fields::UnknownTaggedFields,
    };

    use super::*;

    fn id(n: u8) -> WireUuid {
        let mut b = [0u8; 16];
        b[15] = n;
        WireUuid(b)
    }

    fn api_versions_for_share_leave() -> Vec<u8> {
        let response = ApiVersionsResponse {
            error_code: 0,
            api_keys: vec![
                ApiVersion {
                    api_key: api_versions_request::API_KEY,
                    min_version: 0,
                    max_version: 3,
                    ..Default::default()
                },
                ApiVersion {
                    api_key: share_group_heartbeat_request::API_KEY,
                    min_version: share_group_heartbeat_request::MIN_VERSION,
                    max_version: share_group_heartbeat_request::MAX_VERSION,
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        let mut buffer = BytesMut::new();
        response
            .encode(&mut buffer, 0)
            .expect("encode API versions");
        buffer.to_vec()
    }

    #[tokio::test]
    async fn leave_group_uses_configured_timeout_for_one_best_effort_request() {
        let requests = Arc::new(AtomicUsize::new(0));
        let observed = Arc::clone(&requests);
        let mock = MockBroker::start(move |api_key, _version, _corr_id, _body| {
            if api_key == api_versions_request::API_KEY {
                Some(api_versions_for_share_leave())
            } else if api_key == share_group_heartbeat_request::API_KEY {
                observed.fetch_add(1, Ordering::SeqCst);
                None
            } else {
                panic!("unexpected API key {api_key}");
            }
        })
        .await;
        let client = Client::builder()
            .bootstrap(mock.addr.to_string())
            .client_id("share-leave-timeout-test")
            .request_timeout(krabka_units::secs(5))
            .build()
            .await
            .expect("client");
        let state = ShareCoordinatorState {
            client,
            group_id: "group-a".into(),
            member_id: "member-a".into(),
            member_epoch: Arc::new(Mutex::new(3)),
            assignment: Arc::new(Mutex::new(Vec::new())),
            topic_names: Arc::new(Mutex::new(HashMap::new())),
            subscribe: vec!["topic-a".into()],
            heartbeat_interval: krabka_units::secs(1),
            leave_heartbeat_timeout: krabka_units::millis(37),
            poll_error: coordinator::PollErrorSlot::default(),
        };

        tokio::time::timeout(Duration::from_secs(1), leave_group(&state))
            .await
            .expect("configured leave deadline bounds shutdown");

        mock.stop();
        assert2::assert!(requests.load(Ordering::SeqCst) == 1);
    }

    async fn state() -> ShareCoordinatorState {
        let mut names = HashMap::new();
        names.insert(id(7), "topic-a".to_string());
        ShareCoordinatorState {
            client: Client::builder()
                .bootstrap("127.0.0.1:1")
                .client_id("share-coordinator-test")
                .build()
                .await
                .unwrap(),
            group_id: "group-a".into(),
            member_id: "member-a".into(),
            member_epoch: Arc::new(Mutex::new(3)),
            assignment: Arc::new(Mutex::new(Vec::new())),
            topic_names: Arc::new(Mutex::new(names)),
            subscribe: vec!["topic-a".into()],
            heartbeat_interval: krabka_units::secs(1),
            leave_heartbeat_timeout: krabka_units::secs(5),
            poll_error: coordinator::PollErrorSlot::default(),
        }
    }

    #[test]
    fn heartbeat_requests_preserve_group_member_epoch_and_subscription() {
        let leave = build_leave_heartbeat_request("group-a".into(), "member-a".into());
        assert2::assert!(
            leave
                == ShareGroupHeartbeatRequest {
                    group_id: "group-a".into(),
                    member_id: "member-a".into(),
                    member_epoch: -1,
                    rack_id: None,
                    subscribed_topic_names: None,
                    unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                }
        );

        let heartbeat = build_heartbeat_request(
            "group-a".into(),
            "member-a".into(),
            4,
            Some(vec!["topic-a".into()]),
        );
        assert2::assert!(
            heartbeat
                == ShareGroupHeartbeatRequest {
                    group_id: "group-a".into(),
                    member_id: "member-a".into(),
                    member_epoch: 4,
                    rack_id: None,
                    subscribed_topic_names: Some(vec!["topic-a".into()]),
                    unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                }
        );
    }

    #[test]
    fn heartbeat_result_classifies_success_rejoin_and_transient_errors() {
        for (_name, code, expected) in [
            ("success", 0, HeartbeatOutcome::Ok),
            (
                "fenced epoch",
                FENCED_MEMBER_EPOCH,
                HeartbeatOutcome::RejoinFromScratch,
            ),
            (
                "unknown member",
                UNKNOWN_MEMBER_ID,
                HeartbeatOutcome::RejoinFromScratch,
            ),
            (
                "stale epoch",
                STALE_MEMBER_EPOCH,
                HeartbeatOutcome::RejoinFromScratch,
            ),
            ("transient", 17, HeartbeatOutcome::Transient),
        ] {
            assert2::assert!(
                std::mem::discriminant(&heartbeat_result(code))
                    == std::mem::discriminant(&expected)
            );
        }
    }

    #[tokio::test]
    async fn update_assignment_resolves_names_and_hex_fallbacks() {
        let state = state().await;
        update_assignment(
            &state,
            Assignment {
                topic_partitions: vec![
                    TopicPartitions {
                        topic_id: id(7),
                        partitions: vec![1],
                        ..Default::default()
                    },
                    TopicPartitions {
                        topic_id: id(9),
                        partitions: vec![2],
                        ..Default::default()
                    },
                ],
                ..Default::default()
            },
        )
        .await;

        let assignment = state.assignment.lock().await.clone();
        assert2::assert!(
            assignment
                == vec![
                    (id(7), "topic-a".into(), 1),
                    (id(9), hex_topic_id(id(9)), 2)
                ]
        );
    }

    #[test]
    fn hex_topic_id_formats_all_uuid_bytes() {
        assert2::assert!(hex_topic_id(id(9)) == "00000000000000000000000000000009");
    }

    /// Kafka's `AbstractHeartbeatRequestManager.onFailure` gives a
    /// non-retriable heartbeat error to the application `poll`. A SASL
    /// rejection on the share heartbeat connection must reach
    /// `ShareConsumer::poll`, also with an empty assignment.
    #[tokio::test]
    async fn share_heartbeat_sasl_rejection_is_returned_from_poll() {
        use krabka_client_core::{
            AuthenticationError, ClientError, MockReply, MockSaslAnswer, SaslAuthenticationError,
            security::{ClientSecurity, SaslCredentials},
        };
        use krabka_security::ListenerProtocol;

        use crate::share::{
            DEFAULT_SHARE_CONSUMER_FETCH_MAX, DEFAULT_SHARE_CONSUMER_FETCH_MAX_RECORDS,
            DEFAULT_SHARE_CONSUMER_FETCH_MIN, ShareAckMode, ShareAcquireMode, ShareConsumer,
        };

        let handshakes = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&handshakes);
        let answer = MockSaslAnswer::AuthenticateError(58);
        let mock = MockBroker::start_with_replies(move |api_key, version, _corr, _body| {
            if api_key == krabka_protocol::owned::sasl_handshake_request::API_KEY {
                counter.fetch_add(1, Ordering::SeqCst);
            }
            answer.reply(api_key, version).unwrap_or(MockReply::Silent)
        })
        .await;
        let client = |bootstrap: String| async move {
            Client::builder()
                .bootstrap(bootstrap)
                .security(ClientSecurity {
                    protocol: ListenerProtocol::SaslPlaintext,
                    tls: None,
                    sasl: Some(SaslCredentials::Plain {
                        username: "alice".into(),
                        password: "secret".into(),
                    }),
                    sasl_host: None,
                })
                .build()
                .await
                .unwrap()
        };
        let poll_error = coordinator::PollErrorSlot::default();
        let mut state = state().await;
        state.client = client(mock.addr.to_string()).await;
        state.poll_error = Arc::clone(&poll_error);
        let mut consumer = ShareConsumer {
            client: client(mock.addr.to_string()).await,
            group_id: "group-a".into(),
            member_id: "member-a".into(),
            member_epoch: Arc::new(Mutex::new(3)),
            assignment: Arc::new(Mutex::new(Vec::new())),
            topic_names: Arc::new(Mutex::new(HashMap::new())),
            share_session_epoch: 0,
            fetch_min: DEFAULT_SHARE_CONSUMER_FETCH_MIN,
            fetch_max: DEFAULT_SHARE_CONSUMER_FETCH_MAX,
            fetch_max_records: DEFAULT_SHARE_CONSUMER_FETCH_MAX_RECORDS,
            acquire_mode: ShareAcquireMode::BatchOptimized,
            ack_mode: ShareAckMode::Explicit,
            pending_acks: Vec::new(),
            prev_delivered: Vec::new(),
            shutdown: CancellationToken::new(),
            hb_handle: None,
            poll_error,
        };

        let outcome = heartbeat_once(&state, false).await;
        let rejection = match consumer.poll(krabka_units::millis(1)).await {
            Err(ConsumerError::Client(ClientError::Authentication {
                source: AuthenticationError::Sasl(error),
                ..
            })) => Some(error),
            _ => None,
        };
        let handshakes = handshakes.load(Ordering::SeqCst);

        mock.stop();
        assert2::assert!(matches!(outcome, HeartbeatOutcome::Transient));
        assert2::assert!(
            (rejection, handshakes)
                == (
                    Some(SaslAuthenticationError::Failed(
                        "SaslAuthenticate(PLAIN) error_code=58 \
                         error_message=Some(\"rejected by mock broker\")"
                            .into()
                    )),
                    1
                )
        );
    }
}