crabka-client-streams 0.3.6

KIP-1071 Kafka Streams rebalance-protocol 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
//! `KTableSuppressProcessor` — KIP suppression. Buffers `Change` updates and emits
//! each buffered record once stream-time passes `buffer_time(record) + wait_ms`.
//! Unifies `untilWindowCloses` (`buffer_time` = window.end, wait = grace) and
//! `untilTimeLimitElapsed` (`buffer_time` = record ts, wait = the duration) behind a
//! `fn(&K, i64) -> i64` buffer-time pointer.
//!
//! The pending-change buffer is NOT owned by the processor: it lives in a
//! registered, changelog-backed [`SuppressBytesStore`](crate::store::suppress_store)
//! reached per-record via [`ProcessorContext::get_suppress_store`]. The processor is
//! pure control flow over that store.
use std::marker::PhantomData;

use async_trait::async_trait;

use crate::dsl::processors::change::Change;
use crate::processor::api::{Processor, ProcessorContext};
use crate::processor::record::Record;
use crate::store::suppress_bufval::SuppressRecordCtx;

type Marker<T> = PhantomData<fn() -> T>;

pub(crate) struct KTableSuppressProcessor<K, V> {
    pub store_name: String,
    pub observed_stream_time: i64,
    pub wait_ms: i64,
    pub buffer_time: fn(&K, i64) -> i64,
    pub max_records: Option<usize>,
    pub max_bytes: Option<usize>,
    pub emit_early: bool,
    pub _pd: Marker<(K, V)>,
}

impl<K, V> KTableSuppressProcessor<K, V> {
    pub(crate) fn new(
        store_name: String,
        wait_ms: i64,
        buffer_time: fn(&K, i64) -> i64,
        max_records: Option<usize>,
        max_bytes: Option<usize>,
        emit_early: bool,
    ) -> Self {
        Self {
            store_name,
            observed_stream_time: i64::MIN,
            wait_ms,
            buffer_time,
            max_records,
            max_bytes,
            emit_early,
            _pd: PhantomData,
        }
    }
}

/// The buffer is "full" if it exceeds either configured cap (records or bytes).
fn over_caps(
    len: usize,
    byte_size: usize,
    max_records: Option<usize>,
    max_bytes: Option<usize>,
) -> bool {
    max_records.is_some_and(|c| len > c) || max_bytes.is_some_and(|c| byte_size > c)
}

#[async_trait]
impl<K, V> Processor<K, Change<V>, K, Change<V>> for KTableSuppressProcessor<K, V>
where
    // `K: Clone` + `V: Clone` are required by `ProcessorContext::forward`
    // (`KOut: Any + Send + Clone`, `VOut = Change<V>: Any + Send + Clone`); `Sync`
    // is required by `get_suppress_store::<K, V>` (`K: Send + Sync + 'static`).
    K: std::any::Any + Send + Sync + Clone,
    V: std::any::Any + Send + Clone,
{
    async fn process(
        &mut self,
        ctx: &mut ProcessorContext<'_, '_, K, Change<V>>,
        r: Record<K, Change<V>>,
    ) {
        let key = r.key.expect("suppress requires a non-null key");
        self.observed_stream_time = self.observed_stream_time.max(r.timestamp);
        let bt = (self.buffer_time)(&key, r.timestamp);

        // Snapshot the source record context for the changelog VALUE (drop the
        // immutable record_context borrow before the mutable store borrow).
        let rec_ctx = {
            let rc = ctx.record_context();
            SuppressRecordCtx {
                topic: rc.topic.clone(),
                partition: rc.partition,
                offset: rc.offset,
                timestamp: rc.timestamp,
            }
        };

        // Buffer (replace-by-key) the pending change.
        {
            let store = ctx
                .get_suppress_store::<K, V>(&self.store_name)
                .expect("suppress store not found");
            store.put(key, bt, r.value, rec_ctx).await;
        }

        // Emit every entry whose buffer_time has elapsed (stream_time - wait_ms).
        let threshold = self.observed_stream_time - self.wait_ms;
        let due = {
            let store = ctx
                .get_suppress_store::<K, V>(&self.store_name)
                .expect("suppress store not found");
            store.evict_while(threshold).await
        };
        for (k, change, rts) in due {
            ctx.forward(Record::new(Some(k), change, rts));
        }

        // Overflow: maxRecords and/or maxBytes.
        if self.max_records.is_some() || self.max_bytes.is_some() {
            if self.emit_early {
                // emitEarlyWhenFull: evict + emit the oldest until back within caps.
                loop {
                    let evicted = {
                        let store = ctx
                            .get_suppress_store::<K, V>(&self.store_name)
                            .expect("suppress store not found");
                        if over_caps(
                            store.len(),
                            store.byte_size(),
                            self.max_records,
                            self.max_bytes,
                        ) {
                            store.evict_oldest().await
                        } else {
                            None
                        }
                    };
                    match evicted {
                        Some((k, change, rts)) => ctx.forward(Record::new(Some(k), change, rts)),
                        None => break,
                    }
                }
            } else {
                // shutDownWhenFull (strict): fatal if over either cap.
                let store = ctx
                    .get_suppress_store::<K, V>(&self.store_name)
                    .expect("suppress store not found");
                if let Some(cap) = self.max_records {
                    assert!(
                        store.len() <= cap,
                        "suppress buffer exceeded its max capacity of {cap} records (shutDownWhenFull)"
                    );
                }
                if let Some(cap) = self.max_bytes {
                    assert!(
                        store.byte_size() <= cap,
                        "suppress buffer exceeded its max capacity of {cap} bytes (shutDownWhenFull)"
                    );
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::VecDeque;

    use super::*;
    use crate::dsl::windows::{TimeWindowedSerde, Window, Windowed};
    use crate::processor::api::ProcessorContext;
    use crate::processor::erased::{Dispatch, ErasedRecord};
    use crate::processor::record::{Record, RecordContext};
    use crate::processor::serde::{I64Serde, StringSerde};
    use crate::store::registry::StoreRegistry;
    use crate::store::suppress_store::SuppressBytesStore;

    fn windowed(key: &str, start: i64, end: i64) -> Windowed<String> {
        Windowed {
            key: key.into(),
            window: Window { start, end },
        }
    }

    /// Seed a `Windowed<String> -> i64` suppress store named "sup" (size 10 matches
    /// every test window → the serde round-trips `Window{start,end}`).
    fn seed_windowed_store(stores: &mut StoreRegistry) {
        stores.insert(Box::new(
            SuppressBytesStore::<Windowed<String>, i64>::in_memory(
                "sup".into(),
                Box::new(TimeWindowedSerde::new(StringSerde, 10)),
                Box::new(I64Serde),
                "app-sup-changelog".into(),
            ),
        ));
    }

    /// Construct a window-close processor (`buffer_time` = window.end, strict).
    fn window_close_proc(
        grace_ms: i64,
        max_records: Option<usize>,
    ) -> KTableSuppressProcessor<Windowed<String>, i64> {
        KTableSuppressProcessor::new(
            "sup".into(),
            grace_ms,
            |k: &Windowed<String>, _ts| k.window.end,
            max_records,
            None,
            false,
        )
    }

    #[tokio::test]
    async fn buffers_until_window_closes_then_emits_once() {
        let mut stores = StoreRegistry::default();
        seed_windowed_store(&mut stores);
        let children = [0usize];
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = RecordContext {
            topic: "in".into(),
            partition: 0,
            offset: 0,
            timestamp: 0,
        };

        let mut proc = window_close_proc(0, None);

        // Two updates for window [0,10): count 1 then 2. ts in [0,10) < window end.
        for (cnt, ts) in [(1i64, 1i64), (2, 3)] {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            let change = if cnt == 1 {
                Change::update(None, 1)
            } else {
                Change::update(Some(1), 2)
            };
            proc.process(
                &mut ctx,
                Record::new(Some(windowed("a", 0, 10)), change, ts),
            )
            .await;
        }
        // Nothing emitted yet (stream_time = 3 < window end 10).
        assert!(buffer.is_empty());

        // A record for window [20,30) advances stream_time to 25 ≥ 10 → [0,10) closes.
        {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some(windowed("a", 20, 30)), Change::update(None, 1), 25),
            )
            .await;
        }
        // Exactly the [0,10) final value (2) emits; [20,30) stays buffered.
        assert_eq!(buffer.len(), 1);
        let (_, rec) = buffer.pop_front().unwrap();
        let k = rec.key.unwrap().downcast::<Windowed<String>>().unwrap();
        assert_eq!(k.window, Window { start: 0, end: 10 });
        assert_eq!(rec.value.downcast::<Change<i64>>().unwrap().new, Some(2));
    }

    #[tokio::test]
    async fn grace_delays_close() {
        let mut stores = StoreRegistry::default();
        seed_windowed_store(&mut stores);
        let children = [0usize];
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = RecordContext {
            topic: "in".into(),
            partition: 0,
            offset: 0,
            timestamp: 0,
        };

        let mut proc = window_close_proc(5, None); // grace 5

        {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some(windowed("a", 0, 10)), Change::update(None, 1), 5),
            )
            .await;
        }
        // stream_time 12 → threshold 12-5=7 < window end 10 → NOT closed.
        {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some(windowed("b", 10, 20)), Change::update(None, 1), 12),
            )
            .await;
        }
        assert!(buffer.is_empty());
        // stream_time 16 → threshold 11 >= 10 → [0,10) closes.
        {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some(windowed("c", 20, 30)), Change::update(None, 1), 16),
            )
            .await;
        }
        assert_eq!(buffer.len(), 1);
        let (_, rec) = buffer.pop_front().unwrap();
        assert_eq!(
            rec.key
                .unwrap()
                .downcast::<Windowed<String>>()
                .unwrap()
                .window,
            Window { start: 0, end: 10 }
        );
    }

    #[tokio::test]
    #[should_panic(expected = "max capacity")]
    async fn exceeding_max_records_shuts_down() {
        let mut stores = StoreRegistry::default();
        seed_windowed_store(&mut stores);
        let children = [0usize];
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = RecordContext {
            topic: "in".into(),
            partition: 0,
            offset: 0,
            timestamp: 0,
        };
        let mut proc = window_close_proc(0, Some(2)); // cap 2
        // Three distinct keys in the SAME open window [0,10) (ts < 10 → none close).
        for (k, ts) in [("a", 1i64), ("b", 2), ("c", 3)] {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            // the third put brings len() to 3 > cap 2 → panic
            proc.process(
                &mut ctx,
                Record::new(Some(windowed(k, 0, 10)), Change::update(None, 1), ts),
            )
            .await;
        }
    }

    #[tokio::test]
    #[should_panic(expected = "bytes")]
    async fn exceeding_max_bytes_shuts_down() {
        let mut stores = StoreRegistry::default();
        seed_windowed_store(&mut stores);
        let children = [0usize];
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = RecordContext {
            topic: "in".into(),
            partition: 0,
            offset: 0,
            timestamp: 0,
        };
        // Each entry's byte_size = key_bytes (TimeWindowedSerde: 1-char key + 8-byte
        // window-start = 9 bytes) + value_bytes (8-byte i64) = 17 bytes. A 20-byte cap
        // holds one entry (17 <= 20) but the second (34 > 20) shuts down.
        let mut proc = KTableSuppressProcessor::<Windowed<String>, i64>::new(
            "sup".into(),
            0,
            |k: &Windowed<String>, _ts| k.window.end,
            None,
            Some(20),
            false,
        );
        for (k, ts) in [("a", 1i64), ("b", 2)] {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some(windowed(k, 0, 10)), Change::update(None, 1), ts),
            )
            .await;
        }
    }

    #[tokio::test]
    async fn at_capacity_does_not_panic_and_closes_normally() {
        let mut stores = StoreRegistry::default();
        seed_windowed_store(&mut stores);
        let children = [0usize];
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = RecordContext {
            topic: "in".into(),
            partition: 0,
            offset: 0,
            timestamp: 0,
        };
        let mut proc = window_close_proc(0, Some(2)); // cap 2
        // Two keys in [0,10): len == cap, not over → no panic.
        for (k, ts) in [("a", 1i64), ("b", 2)] {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some(windowed(k, 0, 10)), Change::update(None, 1), ts),
            )
            .await;
        }
        assert!(buffer.is_empty()); // nothing closed yet
        // A record in window [10,20) at ts=15 closes [0,10): the close-eviction runs
        // BEFORE the cap check, so len drops to 1 (the new window) → no panic, and
        // both [0,10) entries emit.
        {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, Windowed<String>, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some(windowed("z", 10, 20)), Change::update(None, 1), 15),
            )
            .await;
        }
        assert_eq!(buffer.len(), 2); // a@[0,10] and b@[0,10] emitted
    }

    #[tokio::test]
    async fn until_time_limit_newer_record_resets_timer() {
        let mut stores = StoreRegistry::default();
        stores.insert(Box::new(SuppressBytesStore::<String, i64>::in_memory(
            "sup".into(),
            Box::new(StringSerde),
            Box::new(I64Serde),
            "app-sup-changelog".into(),
        )));
        let children = [0usize];
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = RecordContext {
            topic: "in".into(),
            partition: 0,
            offset: 0,
            timestamp: 0,
        };
        // Rate-limiter, wait 50: buffer_time = record ts (any key).
        let mut proc = KTableSuppressProcessor::<String, i64>::new(
            "sup".into(),
            50,
            |_k: &String, ts| ts,
            None,
            None,
            false,
        );

        // "a"@10 buffers (old timer would fire at 10+50=60). "a"@40 replaces it,
        // re-stamping buffer_time to 40 → new timer 90. "b"@60 advances stream-time
        // to 60 (threshold 10): a@40 is NOT due, proving the old 60 timer was reset.
        for (k, change, ts) in [
            ("a", Change::update(None, 1), 10i64),
            ("a", Change::update(Some(1), 2), 40),
            ("b", Change::update(None, 1), 60),
        ] {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, String, Change<i64>>::new(&mut d);
            proc.process(&mut ctx, Record::new(Some(k.to_string()), change, ts))
                .await;
        }
        assert!(
            buffer.is_empty(),
            "old timer (60) must not fire after the reset"
        );

        // stream-time 90 (threshold 40): a@40 is now due → emits the NEWER value (2).
        {
            let globals = crate::runtime::global::GlobalStateManager::default();
            let mut scheds = Vec::new();
            let mut d = Dispatch {
                buffer: &mut buffer,
                children: &children,
                output: &mut output,
                record_ctx: &rc,
                stores: &mut stores,
                globals: &globals,
                node_idx: 0,
                schedules: &mut scheds,
                sched_stream_time: i64::MIN,
                sched_wall_clock: 0,
            };
            let mut ctx = ProcessorContext::<'_, '_, String, Change<i64>>::new(&mut d);
            proc.process(
                &mut ctx,
                Record::new(Some("c".to_string()), Change::update(None, 1), 90),
            )
            .await;
        }
        assert_eq!(buffer.len(), 1);
        let (_, rec) = buffer.pop_front().unwrap();
        assert_eq!(rec.key.unwrap().downcast::<String>().unwrap().as_str(), "a");
        // The newest value (2), not the stale 1 → the reset kept the latest update.
        assert_eq!(rec.value.downcast::<Change<i64>>().unwrap().new, Some(2));
    }
}