oxigdal-streaming 0.1.4

Real-time data processing and streaming pipelines for OxiGDAL
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
//! Core stream types and traits.

use crate::error::{Result, StreamingError};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use crossbeam_channel::{Receiver, Sender, bounded, unbounded};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;

/// A stream element containing data and metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamElement {
    /// The actual data payload
    pub data: Vec<u8>,

    /// Event timestamp
    pub event_time: DateTime<Utc>,

    /// Processing timestamp
    pub processing_time: DateTime<Utc>,

    /// Optional key for partitioning
    pub key: Option<Vec<u8>>,

    /// Metadata
    pub metadata: StreamMetadata,
}

impl StreamElement {
    /// Create a new stream element.
    pub fn new(data: Vec<u8>, event_time: DateTime<Utc>) -> Self {
        Self {
            data,
            event_time,
            processing_time: Utc::now(),
            key: None,
            metadata: StreamMetadata::default(),
        }
    }

    /// Create a new stream element with a key.
    pub fn with_key(mut self, key: Vec<u8>) -> Self {
        self.key = Some(key);
        self
    }

    /// Create a new stream element with metadata.
    pub fn with_metadata(mut self, metadata: StreamMetadata) -> Self {
        self.metadata = metadata;
        self
    }

    /// Get the size in bytes of this element.
    pub fn size_bytes(&self) -> usize {
        self.data.len() + self.key.as_ref().map_or(0, |k| k.len())
    }
}

/// Metadata associated with a stream element.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StreamMetadata {
    /// Source identifier
    pub source_id: Option<String>,

    /// Partition ID
    pub partition_id: Option<u32>,

    /// Sequence number
    pub sequence_number: Option<u64>,

    /// Custom attributes
    pub attributes: std::collections::HashMap<String, String>,
}

/// Message types in a stream.
#[derive(Debug, Clone)]
pub enum StreamMessage {
    /// Data element
    Data(StreamElement),

    /// Watermark for event time progress
    Watermark(DateTime<Utc>),

    /// Checkpoint barrier
    Checkpoint(u64),

    /// End of stream marker
    EndOfStream,
}

/// Configuration for a stream.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamConfig {
    /// Buffer size for the stream
    pub buffer_size: usize,

    /// Whether to use bounded channels
    pub bounded: bool,

    /// Timeout for operations
    pub timeout: Duration,

    /// Enable checkpointing
    pub enable_checkpointing: bool,

    /// Checkpoint interval
    pub checkpoint_interval: Duration,

    /// Parallelism level
    pub parallelism: usize,
}

impl Default for StreamConfig {
    fn default() -> Self {
        Self {
            buffer_size: 1024,
            bounded: true,
            timeout: Duration::from_secs(30),
            enable_checkpointing: false,
            checkpoint_interval: Duration::from_secs(60),
            parallelism: std::thread::available_parallelism()
                .map(|n| n.get())
                .unwrap_or(1),
        }
    }
}

/// A source that produces stream elements.
#[async_trait]
pub trait StreamSource: Send + Sync {
    /// Read the next element from the source.
    async fn next(&mut self) -> Result<Option<StreamMessage>>;

    /// Check if the source has more elements.
    async fn has_next(&self) -> bool;

    /// Close the source.
    async fn close(&mut self) -> Result<()>;
}

/// A sink that consumes stream elements.
#[async_trait]
pub trait StreamSink: Send + Sync {
    /// Write an element to the sink.
    async fn write(&mut self, element: StreamMessage) -> Result<()>;

    /// Flush buffered elements.
    async fn flush(&mut self) -> Result<()>;

    /// Close the sink.
    async fn close(&mut self) -> Result<()>;
}

/// A stream of data elements with transformation capabilities.
pub struct Stream {
    /// Configuration
    config: StreamConfig,

    /// Sender for stream messages
    sender: Sender<StreamMessage>,

    /// Receiver for stream messages
    receiver: Receiver<StreamMessage>,

    /// Stream state
    state: Arc<RwLock<StreamState>>,
}

/// Internal state of a stream.
#[derive(Debug)]
struct StreamState {
    /// Is the stream closed?
    closed: bool,

    /// Current watermark
    watermark: Option<DateTime<Utc>>,

    /// Last checkpoint ID
    last_checkpoint: Option<u64>,

    /// Total elements processed
    elements_processed: u64,

    /// Total bytes processed
    bytes_processed: u64,
}

impl Stream {
    /// Create a new stream with default configuration.
    pub fn new() -> Self {
        Self::with_config(StreamConfig::default())
    }

    /// Create a new stream with custom configuration.
    pub fn with_config(config: StreamConfig) -> Self {
        let (sender, receiver) = if config.bounded {
            bounded(config.buffer_size)
        } else {
            unbounded()
        };

        Self {
            config,
            sender,
            receiver,
            state: Arc::new(RwLock::new(StreamState {
                closed: false,
                watermark: None,
                last_checkpoint: None,
                elements_processed: 0,
                bytes_processed: 0,
            })),
        }
    }

    /// Send a message to the stream.
    pub async fn send(&self, message: StreamMessage) -> Result<()> {
        let state = self.state.read().await;
        if state.closed {
            return Err(StreamingError::StreamClosed);
        }
        drop(state);

        self.sender
            .send(message)
            .map_err(|_| StreamingError::SendError)?;

        Ok(())
    }

    /// Receive a message from the stream.
    pub async fn recv(&self) -> Result<StreamMessage> {
        match self.receiver.recv_timeout(self.config.timeout) {
            Ok(msg) => {
                // Update state
                let mut state = self.state.write().await;
                match &msg {
                    StreamMessage::Data(elem) => {
                        state.elements_processed += 1;
                        state.bytes_processed += elem.size_bytes() as u64;
                    }
                    StreamMessage::Watermark(wm) => {
                        state.watermark = Some(*wm);
                    }
                    StreamMessage::Checkpoint(id) => {
                        state.last_checkpoint = Some(*id);
                    }
                    StreamMessage::EndOfStream => {
                        state.closed = true;
                    }
                }
                Ok(msg)
            }
            Err(crossbeam_channel::RecvTimeoutError::Timeout) => Err(StreamingError::Timeout),
            Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
                Err(StreamingError::RecvError)
            }
        }
    }

    /// Try to receive a message without blocking.
    pub fn try_recv(&self) -> Result<Option<StreamMessage>> {
        match self.receiver.try_recv() {
            Ok(msg) => Ok(Some(msg)),
            Err(crossbeam_channel::TryRecvError::Empty) => Ok(None),
            Err(crossbeam_channel::TryRecvError::Disconnected) => Err(StreamingError::RecvError),
        }
    }

    /// Get the current watermark.
    pub async fn watermark(&self) -> Option<DateTime<Utc>> {
        self.state.read().await.watermark
    }

    /// Get the last checkpoint ID.
    pub async fn last_checkpoint(&self) -> Option<u64> {
        self.state.read().await.last_checkpoint
    }

    /// Get the number of elements processed.
    pub async fn elements_processed(&self) -> u64 {
        self.state.read().await.elements_processed
    }

    /// Get the total bytes processed.
    pub async fn bytes_processed(&self) -> u64 {
        self.state.read().await.bytes_processed
    }

    /// Check if the stream is closed.
    pub async fn is_closed(&self) -> bool {
        self.state.read().await.closed
    }

    /// Close the stream.
    pub async fn close(&self) -> Result<()> {
        let mut state = self.state.write().await;
        state.closed = true;
        Ok(())
    }

    /// Get a clone of the sender.
    pub fn sender(&self) -> Sender<StreamMessage> {
        self.sender.clone()
    }

    /// Get a clone of the receiver.
    pub fn receiver(&self) -> Receiver<StreamMessage> {
        self.receiver.clone()
    }

    /// Get the stream configuration.
    pub fn config(&self) -> &StreamConfig {
        &self.config
    }
}

impl Default for Stream {
    fn default() -> Self {
        Self::new()
    }
}

/// A channel-based stream source.
pub struct ChannelSource {
    receiver: Receiver<StreamMessage>,
    closed: bool,
}

impl ChannelSource {
    /// Create a new channel source.
    pub fn new(receiver: Receiver<StreamMessage>) -> Self {
        Self {
            receiver,
            closed: false,
        }
    }
}

#[async_trait]
impl StreamSource for ChannelSource {
    async fn next(&mut self) -> Result<Option<StreamMessage>> {
        if self.closed {
            return Ok(None);
        }

        match self.receiver.try_recv() {
            Ok(msg) => {
                if matches!(msg, StreamMessage::EndOfStream) {
                    self.closed = true;
                }
                Ok(Some(msg))
            }
            Err(crossbeam_channel::TryRecvError::Empty) => Ok(None),
            Err(crossbeam_channel::TryRecvError::Disconnected) => {
                self.closed = true;
                Ok(None)
            }
        }
    }

    async fn has_next(&self) -> bool {
        !self.closed && !self.receiver.is_empty()
    }

    async fn close(&mut self) -> Result<()> {
        self.closed = true;
        Ok(())
    }
}

/// A channel-based stream sink.
pub struct ChannelSink {
    sender: Sender<StreamMessage>,
    buffer: Vec<StreamMessage>,
    buffer_size: usize,
}

impl ChannelSink {
    /// Create a new channel sink.
    pub fn new(sender: Sender<StreamMessage>) -> Self {
        Self::with_buffer_size(sender, 100)
    }

    /// Create a new channel sink with a custom buffer size.
    pub fn with_buffer_size(sender: Sender<StreamMessage>, buffer_size: usize) -> Self {
        Self {
            sender,
            buffer: Vec::with_capacity(buffer_size),
            buffer_size,
        }
    }
}

#[async_trait]
impl StreamSink for ChannelSink {
    async fn write(&mut self, element: StreamMessage) -> Result<()> {
        self.buffer.push(element);

        if self.buffer.len() >= self.buffer_size {
            self.flush().await?;
        }

        Ok(())
    }

    async fn flush(&mut self) -> Result<()> {
        for msg in self.buffer.drain(..) {
            self.sender
                .send(msg)
                .map_err(|_| StreamingError::SendError)?;
        }
        Ok(())
    }

    async fn close(&mut self) -> Result<()> {
        self.flush().await?;
        self.sender
            .send(StreamMessage::EndOfStream)
            .map_err(|_| StreamingError::SendError)?;
        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_stream_element_creation() {
        let now = Utc::now();
        let data = vec![1, 2, 3, 4];
        let elem = StreamElement::new(data.clone(), now);

        assert_eq!(elem.data, data);
        assert_eq!(elem.event_time, now);
        assert!(elem.key.is_none());
    }

    #[tokio::test]
    async fn test_stream_send_recv() {
        let stream = Stream::new();
        let now = Utc::now();
        let elem = StreamElement::new(vec![1, 2, 3], now);

        stream
            .send(StreamMessage::Data(elem.clone()))
            .await
            .expect("stream send should succeed");

        match stream.recv().await.expect("stream recv should succeed") {
            StreamMessage::Data(received) => {
                assert_eq!(received.data, elem.data);
            }
            _ => panic!("Expected data message"),
        }
    }

    #[tokio::test]
    async fn test_stream_watermark() {
        let stream = Stream::new();
        let now = Utc::now();

        stream
            .send(StreamMessage::Watermark(now))
            .await
            .expect("stream send should succeed");
        let _ = stream.recv().await.expect("stream recv should succeed");

        assert_eq!(stream.watermark().await, Some(now));
    }

    #[tokio::test]
    async fn test_stream_close() {
        let stream = Stream::new();
        assert!(!stream.is_closed().await);

        stream.close().await.expect("stream close should succeed");
        assert!(stream.is_closed().await);

        let result = stream.send(StreamMessage::EndOfStream).await;
        assert!(matches!(result, Err(StreamingError::StreamClosed)));
    }
}