crabka-client-streams 0.3.2

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
//! Type-erased node adapters: `ProcessorNode`, `SinkNode`, `SourceNode`.
//!
//! Each adapter carries the `TypeId` of the `(K, V)` pairs it consumes and/or
//! produces so graph construction can validate wiring without keeping the
//! concrete type parameters in scope.
//!
//! The three roles:
//! - [`ProcessorNode`] — downcasts `ErasedRecord`, runs the user-supplied
//!   [`Processor`], and any `forward` calls box the output back into
//!   `ErasedRecord` entries in the dispatch buffer.
//! - [`SinkNode`] — downcasts `ErasedRecord` and serializes it to
//!   [`OutputRecord`] bytes (no children).
//! - [`SourceNode`] — deserializes raw bytes into `ErasedRecord`; it is
//!   entered by the graph driver directly via `deserialize`, not as a child
//!   target of another node.

use std::any::{Any, type_name};

use async_trait::async_trait;

use super::api::{Processor, ProcessorContext, ProcessorSupplier};
use super::erased::{Dispatch, ErasedRecord, OutputRecord, ProcessorError};
use super::record::Record;
use super::serde::Serde;

// ──────────────────────────────────────────────────────────────────────────────
// ErasedNode trait
// ──────────────────────────────────────────────────────────────────────────────

/// Object-safe trait for a node slot in the execution graph.
///
/// Implemented by [`ProcessorNode`] and [`SinkNode`].  [`SourceNode`] does
/// **not** implement this trait because sources are entered via their own
/// `deserialize` method — they are never the target of a `forward` from a
/// parent node.
#[async_trait]
pub(crate) trait ErasedNode: Send {
    /// Called once before the first record (e.g. to open stores). Default is
    /// a no-op so sink nodes don't need to implement it.
    #[allow(dead_code)]
    async fn init(&mut self, _dispatch: &mut Dispatch<'_>) -> Result<(), ProcessorError> {
        Ok(())
    }

    /// Called once at task shutdown. Default is a no-op.
    async fn close(&mut self) {}

    /// Process one erased record: downcast, run inner logic, push results.
    async fn process(
        &mut self,
        dispatch: &mut Dispatch<'_>,
        record: ErasedRecord,
    ) -> Result<(), ProcessorError>;
}

// ──────────────────────────────────────────────────────────────────────────────
// ProcessorNode
// ──────────────────────────────────────────────────────────────────────────────

/// Wraps a user [`Processor`] and handles type-erasure at both the input
/// (downcast) and output (`ProcessorContext::forward` re-boxes).
pub(crate) struct ProcessorNode<KIn, VIn, KOut, VOut> {
    name: String,
    inner: Box<dyn Processor<KIn, VIn, KOut, VOut>>,
}

impl<KIn, VIn, KOut, VOut> ProcessorNode<KIn, VIn, KOut, VOut>
where
    KIn: Any + Send,
    VIn: Any + Send,
    KOut: Any + Send + Clone,
    VOut: Any + Send + Clone,
{
    pub(crate) fn new(
        name: String,
        supplier: &impl ProcessorSupplier<KIn, VIn, KOut, VOut>,
    ) -> Self {
        Self {
            name,
            inner: supplier.get(),
        }
    }
}

#[async_trait]
impl<KIn, VIn, KOut, VOut> ErasedNode for ProcessorNode<KIn, VIn, KOut, VOut>
where
    KIn: Any + Send,
    VIn: Any + Send,
    KOut: Any + Send + Clone,
    VOut: Any + Send + Clone,
{
    async fn init(&mut self, dispatch: &mut Dispatch<'_>) -> Result<(), ProcessorError> {
        let mut ctx = ProcessorContext::<'_, '_, KOut, VOut>::new(dispatch);
        self.inner.init(&mut ctx).await;
        Ok(())
    }

    async fn close(&mut self) {
        self.inner.close().await;
    }

    async fn process(
        &mut self,
        dispatch: &mut Dispatch<'_>,
        rec: ErasedRecord,
    ) -> Result<(), ProcessorError> {
        // Downcast the value (required).
        let value = *rec
            .value
            .downcast::<VIn>()
            .map_err(|_| ProcessorError::Downcast {
                node: self.name.clone(),
                expected: type_name::<VIn>(),
            })?;

        // Downcast the key (optional — None key is valid).
        let key: Option<KIn> = match rec.key {
            None => None,
            Some(boxed) => {
                let k = *boxed
                    .downcast::<KIn>()
                    .map_err(|_| ProcessorError::Downcast {
                        node: self.name.clone(),
                        expected: type_name::<KIn>(),
                    })?;
                Some(k)
            }
        };

        let record = Record::new(key, value, rec.timestamp);
        let mut ctx = ProcessorContext::<'_, '_, KOut, VOut>::new(dispatch);
        self.inner.process(&mut ctx, record).await;
        Ok(())
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// SinkNode
// ──────────────────────────────────────────────────────────────────────────────

/// Deserializes an [`ErasedRecord`] and pushes the resulting bytes to
/// `Dispatch::output`. This is a terminal node — it has no children.
pub(crate) struct SinkNode<K, V, KS, VS> {
    name: String,
    topic: String,
    key_serde: KS,
    value_serde: VS,
    _pd: std::marker::PhantomData<fn(K, V)>,
}

impl<K, V, KS, VS> SinkNode<K, V, KS, VS>
where
    K: Any + Send,
    V: Any + Send,
    KS: Serde<K>,
    VS: Serde<V>,
{
    pub(crate) fn new(name: String, topic: String, key_serde: KS, value_serde: VS) -> Self {
        Self {
            name,
            topic,
            key_serde,
            value_serde,
            _pd: std::marker::PhantomData,
        }
    }
}

#[async_trait]
impl<K, V, KS, VS> ErasedNode for SinkNode<K, V, KS, VS>
where
    K: Any + Send,
    V: Any + Send,
    KS: Serde<K> + Send,
    VS: Serde<V> + Send,
{
    async fn process(
        &mut self,
        dispatch: &mut Dispatch<'_>,
        rec: ErasedRecord,
    ) -> Result<(), ProcessorError> {
        // Downcast value.
        let value = *rec
            .value
            .downcast::<V>()
            .map_err(|_| ProcessorError::Downcast {
                node: self.name.clone(),
                expected: type_name::<V>(),
            })?;

        // Downcast key (optional).
        let key: Option<K> = match rec.key {
            None => None,
            Some(boxed) => {
                let k = *boxed
                    .downcast::<K>()
                    .map_err(|_| ProcessorError::Downcast {
                        node: self.name.clone(),
                        expected: type_name::<K>(),
                    })?;
                Some(k)
            }
        };

        let key_bytes = key.as_ref().map(|k| self.key_serde.serialize(k));
        let value_bytes = Some(self.value_serde.serialize(&value));

        dispatch.output.push(OutputRecord {
            topic: self.topic.clone(),
            key: key_bytes,
            value: value_bytes,
            timestamp: rec.timestamp,
        });

        Ok(())
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// SourceNode
// ──────────────────────────────────────────────────────────────────────────────

/// Deserializes raw bytes from an input topic into a boxed `ErasedRecord`.
/// The graph driver calls `deserialize` directly — `SourceNode` does **not**
/// implement `ErasedNode` because it is never the target of a `forward`.
pub(crate) struct SourceNode<K, V, KS, VS> {
    name: String,
    key_serde: KS,
    value_serde: VS,
    _pd: std::marker::PhantomData<fn(K, V)>,
}

impl<K, V, KS, VS> SourceNode<K, V, KS, VS>
where
    K: Any + Send + Clone,
    V: Any + Send + Clone,
    KS: Serde<K>,
    VS: Serde<V>,
{
    pub(crate) fn new(name: String, key_serde: KS, value_serde: VS) -> Self {
        Self {
            name,
            key_serde,
            value_serde,
            _pd: std::marker::PhantomData,
        }
    }

    /// Deserialize raw bytes into a type-erased `ErasedRecord`.
    pub(crate) fn deserialize(
        &self,
        key: Option<&[u8]>,
        value: &[u8],
        timestamp: i64,
    ) -> Result<ErasedRecord, ProcessorError> {
        let k: Option<Box<dyn Any + Send>> = match key {
            None => None,
            Some(kb) => {
                let k = self
                    .key_serde
                    .deserialize(kb)
                    .map_err(|e| ProcessorError::Serde {
                        node: self.name.clone(),
                        message: e.to_string(),
                    })?;
                Some(Box::new(k) as Box<dyn Any + Send>)
            }
        };

        let v = self
            .value_serde
            .deserialize(value)
            .map_err(|e| ProcessorError::Serde {
                node: self.name.clone(),
                message: e.to_string(),
            })?;

        Ok(ErasedRecord::new(
            k,
            Box::new(v) as Box<dyn Any + Send>,
            timestamp,
        ))
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::processor::api::{Processor, ProcessorContext};
    use crate::processor::erased::{Dispatch, ErasedRecord};
    use crate::processor::record::{Record, RecordContext};
    use crate::processor::serde::{I64Serde, StringSerde};
    use assert2::check;
    use std::collections::VecDeque;

    struct Upper;
    #[async_trait]
    impl Processor<String, String, String, String> for Upper {
        async fn process(
            &mut self,
            ctx: &mut ProcessorContext<'_, '_, String, String>,
            r: Record<String, String>,
        ) {
            ctx.forward(Record::new(r.key, r.value.to_uppercase(), r.timestamp));
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn make_dispatch<'a>(
        buffer: &'a mut VecDeque<(usize, ErasedRecord)>,
        children: &'a [usize],
        output: &'a mut Vec<crate::processor::erased::OutputRecord>,
        rc: &'a RecordContext,
        stores: &'a mut crate::store::registry::StoreRegistry,
        globals: &'a crate::runtime::global::GlobalStateManager,
        schedules: &'a mut Vec<crate::processor::punctuation::ScheduleEntry>,
    ) -> Dispatch<'a> {
        Dispatch {
            buffer,
            children,
            output,
            record_ctx: rc,
            stores,
            globals,
            node_idx: 0,
            schedules,
            sched_stream_time: i64::MIN,
            sched_wall_clock: 0,
        }
    }

    fn default_rc() -> RecordContext {
        RecordContext {
            topic: "t".into(),
            partition: 0,
            offset: 0,
            timestamp: 1,
        }
    }

    #[tokio::test]
    async fn processor_node_downcasts_runs_forwards() {
        let mut node = ProcessorNode::new("upcase".into(), &(|| Upper));
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = default_rc();
        let children = [9usize];
        let mut stores = crate::store::registry::StoreRegistry::default();
        let globals = crate::runtime::global::GlobalStateManager::default();
        let mut scheds = Vec::new();
        let mut d = make_dispatch(
            &mut buffer,
            &children,
            &mut output,
            &rc,
            &mut stores,
            &globals,
            &mut scheds,
        );
        let rec = ErasedRecord::new(
            Some(Box::new("k".to_string())),
            Box::new("hi".to_string()),
            1,
        );
        node.process(&mut d, rec).await.unwrap();
        let (_c, out) = buffer.pop_front().unwrap();
        check!(*out.value.downcast::<String>().unwrap() == "HI");
    }

    #[tokio::test]
    async fn processor_node_none_key_passes_through() {
        let mut node = ProcessorNode::new("upcase".into(), &(|| Upper));
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = default_rc();
        let children = [0usize];
        let mut stores = crate::store::registry::StoreRegistry::default();
        let globals = crate::runtime::global::GlobalStateManager::default();
        let mut scheds = Vec::new();
        let mut d = make_dispatch(
            &mut buffer,
            &children,
            &mut output,
            &rc,
            &mut stores,
            &globals,
            &mut scheds,
        );
        let rec = ErasedRecord::new(None, Box::new("hi".to_string()), 1);
        node.process(&mut d, rec).await.unwrap();
        let (_c, out) = buffer.pop_front().unwrap();
        check!(out.key.is_none());
        check!(*out.value.downcast::<String>().unwrap() == "HI");
    }

    #[tokio::test]
    async fn processor_node_downcast_error() {
        let mut node = ProcessorNode::new("p".into(), &(|| Upper));
        let mut buffer: VecDeque<(usize, ErasedRecord)> = VecDeque::new();
        let mut output = Vec::new();
        let rc = default_rc();
        let mut stores = crate::store::registry::StoreRegistry::default();
        let globals = crate::runtime::global::GlobalStateManager::default();
        let mut scheds = Vec::new();
        let mut d = make_dispatch(
            &mut buffer,
            &[],
            &mut output,
            &rc,
            &mut stores,
            &globals,
            &mut scheds,
        );
        // value is i32, not String — must fail
        let bad = ErasedRecord::new(None, Box::new(7i32), 0);
        check!(node.process(&mut d, bad).await.is_err());
    }

    #[tokio::test]
    async fn sink_node_serializes_to_output() {
        let mut node = SinkNode::new("out".into(), "out-topic".into(), StringSerde, StringSerde);
        let mut buffer = VecDeque::new();
        let mut output = Vec::new();
        let rc = default_rc();
        let mut stores = crate::store::registry::StoreRegistry::default();
        let globals = crate::runtime::global::GlobalStateManager::default();
        let mut scheds = Vec::new();
        let mut d = make_dispatch(
            &mut buffer,
            &[],
            &mut output,
            &rc,
            &mut stores,
            &globals,
            &mut scheds,
        );
        let rec = ErasedRecord::new(
            Some(Box::new("k".to_string())),
            Box::new("V".to_string()),
            1,
        );
        node.process(&mut d, rec).await.unwrap();
        check!(output.len() == 1);
        check!(output[0].topic == "out-topic");
        check!(output[0].value.as_ref().unwrap().as_ref() == b"V");
    }

    #[tokio::test]
    async fn sink_node_none_key_produces_none_key_bytes() {
        let mut node = SinkNode::new("s".into(), "out-topic".into(), StringSerde, StringSerde);
        let mut buffer = VecDeque::new();
        let mut output = Vec::new();
        let rc = default_rc();
        let mut stores = crate::store::registry::StoreRegistry::default();
        let globals = crate::runtime::global::GlobalStateManager::default();
        let mut scheds = Vec::new();
        let mut d = make_dispatch(
            &mut buffer,
            &[],
            &mut output,
            &rc,
            &mut stores,
            &globals,
            &mut scheds,
        );
        let rec = ErasedRecord::new(None, Box::new("v".to_string()), 0);
        node.process(&mut d, rec).await.unwrap();
        check!(output.len() == 1);
        check!(output[0].key.is_none());
        check!(output[0].value.as_ref().unwrap().as_ref() == b"v");
    }

    #[tokio::test]
    async fn sink_node_downcast_error() {
        let mut node = SinkNode::new("s".into(), "out".into(), StringSerde, StringSerde);
        let mut buffer = VecDeque::new();
        let mut output = Vec::new();
        let rc = default_rc();
        let mut stores = crate::store::registry::StoreRegistry::default();
        let globals = crate::runtime::global::GlobalStateManager::default();
        let mut scheds = Vec::new();
        let mut d = make_dispatch(
            &mut buffer,
            &[],
            &mut output,
            &rc,
            &mut stores,
            &globals,
            &mut scheds,
        );
        // value is i32, not String — must fail
        let bad = ErasedRecord::new(None, Box::new(7i32), 0);
        check!(node.process(&mut d, bad).await.is_err());
    }

    #[test]
    fn source_node_deserializes() {
        let node = SourceNode::new("src".into(), StringSerde, StringSerde);
        let er = node.deserialize(Some(b"k"), b"v", 3).unwrap();
        check!(*er.value.downcast::<String>().unwrap() == "v");
    }

    #[test]
    fn source_node_deserialize_error_and_none_key() {
        // bad key length for I64Serde → error
        let node = SourceNode::new("src".into(), I64Serde, I64Serde);
        check!(
            node.deserialize(Some(&[0, 1]), &[0, 0, 0, 0, 0, 0, 0, 1], 0)
                .is_err()
        );

        // None key path → key is None in erased record
        let ok = SourceNode::new("s2".into(), StringSerde, StringSerde);
        let er = ok.deserialize(None, b"v", 0).unwrap();
        check!(er.key.is_none());
    }
}