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
use std::error::Error as StdError;
use std::fmt;
use std::pin::Pin;
use std::str;
use std::task::{Context, Poll};
use std::time::Instant;

use bytes::Bytes;
use futures::{ready, stream::Stream};
use pin_utils::{unsafe_pinned, unsafe_unpinned};
use serde::de::DeserializeOwned;

use crate::Error;

mod line_parser;

use crate::api::IoError;
use crate::components::streams::NakadiFrame;
use crate::instrumentation::Instrumentation;
use crate::nakadi_types::subscription::EventTypePartition;

use line_parser::{parse_line, LineItems, ParseBatchError};

/// A stream of analyzed Nakadi Frames
pub struct EventStream<St>
where
    St: Stream<Item = Result<NakadiFrame, IoError>>,
{
    frame_stream: St,
    _instrumentation: Instrumentation,
    is_source_done: bool,
}

impl<St> EventStream<St>
where
    St: Stream<Item = Result<NakadiFrame, IoError>>,
{
    unsafe_pinned!(frame_stream: St);
    unsafe_unpinned!(is_source_done: bool);

    pub fn new(frame_stream: St, instrumentation: Instrumentation) -> Self {
        Self {
            frame_stream,
            is_source_done: false,
            _instrumentation: instrumentation,
        }
    }
}

impl<St> Stream for EventStream<St>
where
    St: Stream<Item = Result<NakadiFrame, IoError>>,
{
    type Item = Result<EventStreamBatch, EventStreamError>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
        if self.is_source_done {
            Poll::Ready(None)
        } else {
            let next_frame = ready!(self.as_mut().frame_stream().poll_next(cx));

            match next_frame {
                Some(Ok(frame)) => match EventStreamBatch::try_from_frame(frame) {
                    Ok(line) => Poll::Ready(Some(Ok(line))),
                    Err(err) => {
                        *self.as_mut().is_source_done() = true;
                        Poll::Ready(Some(Err(err.into())))
                    }
                },
                Some(Err(err)) => {
                    *self.as_mut().is_source_done() = true;
                    Poll::Ready(Some(Err(err.into())))
                }
                None => Poll::Ready(None),
            }
        }
    }
}

impl<St> From<St> for EventStream<St>
where
    St: Stream<Item = Result<NakadiFrame, IoError>>,
{
    fn from(stream: St) -> Self {
        Self::new(stream, Instrumentation::default())
    }
}

/// An analyzed line (frame) from Nakadi
///
/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_SubscriptionEventStreamBatch)
#[derive(Debug)]
pub struct EventStreamBatch {
    bytes: Bytes,
    items: LineItems,
    frame_id: usize,
    /// Timestamp when the first byte was received
    frame_started_at: Instant,
    /// Timestamp when the frame was completed
    frame_completed_at: Instant,
}

#[derive(Debug)]
#[non_exhaustive]
pub struct EventStreamBatchStats {
    pub n_bytes: usize,
    pub n_events: usize,
    pub n_events_bytes: usize,
}

impl EventStreamBatch {
    #[allow(dead_code)]
    pub fn new<T: Into<Bytes>>(bytes: T) -> Result<EventStreamBatch, ParseBatchError> {
        let bytes = bytes.into();

        let items = parse_line(bytes.as_ref())?;

        if let Err(err) = items.validate() {
            return Err(ParseBatchError::new(format!("frame is invalid: {}", err)));
        }

        Ok(EventStreamBatch {
            bytes,
            items,
            frame_id: 0,
            frame_started_at: Instant::now(),
            frame_completed_at: Instant::now(),
        })
    }

    #[allow(dead_code)]
    pub fn try_from_slice<T: AsRef<[u8]>>(slice: T) -> Result<EventStreamBatch, ParseBatchError> {
        let items = parse_line(slice.as_ref())?;

        if let Err(err) = items.validate() {
            return Err(ParseBatchError::new(format!("frame  is invalid: {}", err)));
        }

        Ok(EventStreamBatch {
            bytes: Bytes::copy_from_slice(slice.as_ref()),
            items,
            frame_id: 0,
            frame_started_at: Instant::now(),
            frame_completed_at: Instant::now(),
        })
    }

    #[allow(dead_code)]
    pub fn try_from_frame(frame: NakadiFrame) -> Result<EventStreamBatch, ParseBatchError> {
        let items = parse_line(frame.as_ref())?;

        if let Err(err) = items.validate() {
            return Err(ParseBatchError::new(format!("frame is invalid: {}", err)));
        }

        Ok(EventStreamBatch {
            bytes: frame.bytes,
            items,
            frame_id: frame.frame_id,
            frame_started_at: frame.started_at,
            frame_completed_at: frame.completed_at,
        })
    }

    pub fn with_frame_id(mut self, frame_id: usize) -> Self {
        self.frame_id = frame_id;
        self
    }

    pub fn frame_id(&self) -> usize {
        self.frame_id
    }

    pub fn frame_started_at(&self) -> Instant {
        self.frame_started_at
    }

    pub fn frame_completed_at(&self) -> Instant {
        self.frame_completed_at
    }

    pub fn bytes(&self) -> Bytes {
        self.bytes.clone()
    }

    pub fn cursor_str(&self) -> &str {
        self.items.cursor_str(self.bytes.as_ref())
    }

    pub fn cursor_bytes(&self) -> Bytes {
        self.items.cursor_bytes(&self.bytes)
    }

    pub fn partition_bytes(&self) -> Bytes {
        self.items.cursor().partition_bytes(&self.bytes)
    }

    pub fn partition_str(&self) -> &str {
        self.items.cursor().partition_str(self.bytes.as_ref())
    }

    pub fn event_type_bytes(&self) -> Bytes {
        self.items.cursor().event_type_bytes(&self.bytes)
    }

    pub fn event_type_str(&self) -> &str {
        self.items.cursor().event_type_str(self.bytes.as_ref())
    }

    pub fn to_event_type_partition(&self) -> EventTypePartition {
        EventTypePartition::new(self.event_type_str(), self.partition_str())
    }

    pub fn events_bytes(&self) -> Option<Bytes> {
        self.items.events_bytes(&self.bytes)
    }

    pub fn events_str(&self) -> Option<&str> {
        self.items.events_str(self.bytes.as_ref())
    }

    pub fn info_bytes(&self) -> Option<Bytes> {
        self.items.info_bytes(&self.bytes)
    }

    pub fn info_str(&self) -> Option<&str> {
        self.items.info_str(self.bytes.as_ref())
    }

    pub fn is_keep_alive_line(&self) -> bool {
        !self.items.has_events()
    }

    pub fn has_events(&self) -> bool {
        self.items.has_events()
    }

    pub fn n_events(&self) -> usize {
        self.items.n_events()
    }

    pub fn has_info(&self) -> bool {
        self.items.has_info()
    }

    pub fn cursor_deserialized<T: DeserializeOwned>(&self) -> Result<T, Error> {
        Ok(serde_json::from_slice(self.cursor_bytes().as_ref())?)
    }

    pub fn stats(&self) -> EventStreamBatchStats {
        EventStreamBatchStats {
            n_bytes: self.bytes.len(),
            n_events: self.items.n_events(),
            n_events_bytes: self.events_bytes().map(|b| b.len()).unwrap_or(0),
        }
    }
}

#[derive(Debug)]
pub struct EventStreamError {
    message: String,
    kind: EventStreamErrorKind,
}

impl EventStreamError {
    pub fn new<T: Into<String>>(message: T, kind: EventStreamErrorKind) -> Self {
        Self {
            message: message.into(),
            kind,
        }
    }

    pub fn kind(&self) -> EventStreamErrorKind {
        self.kind
    }
}

impl StdError for EventStreamError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        None
    }
}

impl fmt::Display for EventStreamError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind {
            EventStreamErrorKind::Io => {
                write!(f, "io error - ")?;
            }
            EventStreamErrorKind::Parser => {
                write!(f, "parser error - ")?;
            }
        }
        write!(f, "{}", self.message)?;

        Ok(())
    }
}

impl From<IoError> for EventStreamError {
    fn from(err: IoError) -> Self {
        Self::new(err.to_string(), EventStreamErrorKind::Io)
    }
}

impl From<ParseBatchError> for EventStreamError {
    fn from(err: ParseBatchError) -> Self {
        Self::new(err.to_string(), EventStreamErrorKind::Parser)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventStreamErrorKind {
    Io,
    Parser,
}

#[test]
fn parse_subscription_batch_line_with_info() {
    let line_sample = r#"{"cursor":{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"},"events":[{"metadata":"#
        + r#"{"occurred_at":"1996-10-15T16:39:57+07:00","eid":"1f5a76d8-db49-4144-ace7"#
        + r#"-e683e8ff4ba4","event_type":"aruha-test-hila","partition":"5","#
        + r#""received_at":"2016-09-30T09:19:00.525Z","flow_id":"blahbloh"},"#
        + r#""data_op":"C","data":{"order_number":"abc","id":"111"},"#
        + r#""data_type":"blah"}],"info":{"debug":"Stream started"}}"#;

    let cursor_sample = r#"{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"}"#;

    let events_sample = r#"[{"metadata":"#.to_owned()
        + r#"{"occurred_at":"1996-10-15T16:39:57+07:00","eid":"1f5a76d8-db49-4144-ace7"#
        + r#"-e683e8ff4ba4","event_type":"aruha-test-hila","partition":"5","#
        + r#""received_at":"2016-09-30T09:19:00.525Z","flow_id":"blahbloh"},"#
        + r#""data_op":"C","data":{"order_number":"abc","id":"111"},"#
        + r#""data_type":"blah"}]"#;

    let info_sample = r#"{"debug":"Stream started"}"#;

    let line = EventStreamBatch::try_from_slice(line_sample.as_bytes()).unwrap();

    assert_eq!(line.bytes(), line_sample.as_bytes());
    assert_eq!(line.cursor_bytes(), cursor_sample.as_bytes());
    assert_eq!(line.partition_str(), "6", "partition");
    assert_eq!(line.event_type_str(), "order.ORDER_RECEIVED");
    assert_eq!(line.events_str(), Some(events_sample.as_ref()));
    assert_eq!(line.info_str(), Some(info_sample));
    assert_eq!(line.is_keep_alive_line(), false);
}

#[test]
fn parse_subscription_batch_line_without_info() {
    let line_sample = r#"{"cursor":{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"},"events":[{"metadata":"#
        + r#"{"occurred_at":"1996-10-15T16:39:57+07:00","eid":"1f5a76d8-db49-4144-ace7"#
        + r#"-e683e8ff4ba4","event_type":"aruha-test-hila","partition":"5","#
        + r#""received_at":"2016-09-30T09:19:00.525Z","flow_id":"blahbloh"},"#
        + r#""data_op":"C","data":{"order_number":"abc","id":"111"},"#
        + r#""data_type":"blah"}]}"#;

    let cursor_sample = r#"{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"}"#;

    let events_sample = r#"[{"metadata":"#.to_owned()
        + r#"{"occurred_at":"1996-10-15T16:39:57+07:00","eid":"1f5a76d8-db49-4144-ace7"#
        + r#"-e683e8ff4ba4","event_type":"aruha-test-hila","partition":"5","#
        + r#""received_at":"2016-09-30T09:19:00.525Z","flow_id":"blahbloh"},"#
        + r#""data_op":"C","data":{"order_number":"abc","id":"111"},"#
        + r#""data_type":"blah"}]"#;

    let line = EventStreamBatch::try_from_slice(line_sample.as_bytes()).unwrap();

    assert_eq!(line.bytes(), line_sample.as_bytes());
    assert_eq!(line.cursor_bytes(), cursor_sample.as_bytes());
    assert_eq!(line.partition_str(), "6", "partition");
    assert_eq!(line.event_type_str(), "order.ORDER_RECEIVED");
    assert_eq!(line.events_str(), Some(events_sample.as_ref()));
    assert_eq!(line.info_bytes(), None);
    assert_eq!(line.is_keep_alive_line(), false);
}

#[test]
fn parse_subscription_batch_line_keep_alive_with_info() {
    let line_sample = r#"{"cursor":{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"},"info":{"debug":"Stream started"}}"#;

    let cursor_sample = r#"{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"}"#;

    let info_sample = r#"{"debug":"Stream started"}"#;

    let line = EventStreamBatch::try_from_slice(line_sample.as_bytes()).unwrap();

    assert_eq!(line.bytes(), line_sample.as_bytes());
    assert_eq!(line.cursor_bytes(), cursor_sample.as_bytes());
    assert_eq!(line.partition_str(), "6");
    assert_eq!(line.event_type_str(), "order.ORDER_RECEIVED");
    assert_eq!(line.info_str(), Some(info_sample));
    assert_eq!(line.is_keep_alive_line(), true);
}

#[test]
fn parse_subscription_batch_line_keep_alive_without_info() {
    let line_sample = r#"{"cursor":{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"}}"#;

    let cursor_sample = r#"{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"}"#;

    let line = EventStreamBatch::try_from_slice(line_sample.as_bytes()).unwrap();

    assert_eq!(line.bytes(), line_sample.as_bytes(), "line bytes");
    assert_eq!(
        line.cursor_bytes(),
        cursor_sample.as_bytes(),
        "cursor bytes"
    );
    assert_eq!(line.partition_str(), "6");
    assert_eq!(line.event_type_str(), "order.ORDER_RECEIVED");
    assert_eq!(line.info_bytes(), None);
    assert_eq!(line.is_keep_alive_line(), true);
}

#[test]
fn deserialize_subscription_cursor() {
    use crate::nakadi_types::subscription::SubscriptionCursor;
    let line_sample = r#"{"cursor":{"partition":"6","offset":"543","#.to_owned()
        + r#""event_type":"order.ORDER_RECEIVED","cursor_token":"#
        + r#""b75c3102-98a4-4385-a5fd-b96f1d7872f2"}}"#;

    let line = EventStreamBatch::try_from_slice(line_sample.as_bytes()).unwrap();

    let _ = line.cursor_deserialized::<SubscriptionCursor>().unwrap();
}