timely/dataflow/operators/core/capture/
event.rs

1//! Traits and types describing timely dataflow events.
2//!
3//! The `Event` type describes the information an operator can observe about a timely dataflow
4//! stream. There are two types of events, (i) the receipt of data and (ii) reports of progress
5//! of timestamps.
6
7use columnar::Columnar;
8use serde::{Deserialize, Serialize};
9
10/// Data and progress events of the captured stream.
11#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Deserialize, Serialize, Columnar)]
12pub enum Event<T, C> {
13    /// Progress received via `push_external_progress`.
14    Progress(Vec<(T, i64)>),
15    /// Messages received via the data stream.
16    Messages(T, C),
17}
18
19/// Iterates over contained `Event<T, C>`.
20///
21/// The `EventIterator` trait describes types that can iterate over `Cow`s of events,
22/// and which can be used to replay a stream into a new timely dataflow computation.
23///
24/// This method is not simply an iterator because of the lifetime in the result.
25pub trait EventIterator<T: Clone, C: Clone> {
26    /// Iterates over `Cow<Event<T, C>>` elements.
27    fn next(&mut self) -> Option<std::borrow::Cow<Event<T, C>>>;
28}
29
30/// Receives `Event<T, C>` events.
31pub trait EventPusher<T, C> {
32    /// Provides a new `Event<T, D>` to the pusher.
33    fn push(&mut self, event: Event<T, C>);
34}
35
36// implementation for the linked list behind a `Handle`.
37impl<T, C> EventPusher<T, C> for ::std::sync::mpsc::Sender<Event<T, C>> {
38    fn push(&mut self, event: Event<T, C>) {
39        // NOTE: An Err(x) result just means "data not accepted" most likely
40        //       because the receiver is gone. No need to panic.
41        let _ = self.send(event);
42    }
43}
44
45/// A linked-list event pusher and iterator.
46pub mod link {
47
48    use std::borrow::Cow;
49    use std::rc::Rc;
50    use std::cell::RefCell;
51
52    use super::{Event, EventPusher, EventIterator};
53
54    /// A linked list of Event<T, C>.
55    pub struct EventLink<T, C> {
56        /// An event, if one exists.
57        ///
58        /// An event might not exist, if either we want to insert a `None` and have the output iterator pause,
59        /// or in the case of the very first linked list element, which has no event when constructed.
60        pub event: Option<Event<T, C>>,
61        /// The next event, if it exists.
62        pub next: RefCell<Option<Rc<EventLink<T, C>>>>,
63    }
64
65    impl<T, C> EventLink<T, C> {
66        /// Allocates a new `EventLink`.
67        pub fn new() -> EventLink<T, C> {
68            EventLink { event: None, next: RefCell::new(None) }
69        }
70    }
71
72    // implementation for the linked list behind a `Handle`.
73    impl<T, C> EventPusher<T, C> for Rc<EventLink<T, C>> {
74        fn push(&mut self, event: Event<T, C>) {
75            *self.next.borrow_mut() = Some(Rc::new(EventLink { event: Some(event), next: RefCell::new(None) }));
76            let next = Rc::clone(self.next.borrow().as_ref().unwrap());
77            *self = next;
78        }
79    }
80
81    impl<T: Clone, C: Clone> EventIterator<T, C> for Rc<EventLink<T, C>> {
82        fn next(&mut self) -> Option<Cow<Event<T, C>>> {
83            let is_some = self.next.borrow().is_some();
84            if is_some {
85                let next = Rc::clone(self.next.borrow().as_ref().unwrap());
86                *self = next;
87                if let Some(this) = Rc::get_mut(self) {
88                    this.event.take().map(Cow::Owned)
89                }
90                else {
91                    self.event.as_ref().map(Cow::Borrowed)
92                }
93            }
94            else {
95                None
96            }
97        }
98    }
99
100    // Drop implementation to prevent stack overflow through naive drop impl.
101    impl<T, C> Drop for EventLink<T, C> {
102        fn drop(&mut self) {
103            while let Some(link) = self.next.replace(None) {
104                if let Ok(head) = Rc::try_unwrap(link) {
105                    *self = head;
106                }
107            }
108        }
109    }
110
111    impl<T, C> Default for EventLink<T, C> {
112        fn default() -> Self {
113            Self::new()
114        }
115    }
116
117    #[test]
118    fn avoid_stack_overflow_in_drop() {
119        #[cfg(miri)]
120        let limit = 1_000;
121        #[cfg(not(miri))]
122        let limit = 1_000_000;
123        let mut event1 = Rc::new(EventLink::<(),()>::new());
124        let _event2 = Rc::clone(&event1);
125        for _ in 0 .. limit {
126            event1.push(Event::Progress(vec![]));
127        }
128    }
129}
130
131/// A binary event pusher and iterator.
132pub mod binary {
133
134    use std::borrow::Cow;
135
136    use serde::{de::DeserializeOwned, Serialize};
137
138    use super::{Event, EventPusher, EventIterator};
139
140    /// A wrapper for `W: Write` implementing `EventPusher<T, C>`.
141    pub struct EventWriter<T, C, W: ::std::io::Write> {
142        stream: W,
143        phant: ::std::marker::PhantomData<(T, C)>,
144    }
145
146    impl<T, C, W: ::std::io::Write> EventWriter<T, C, W> {
147        /// Allocates a new `EventWriter` wrapping a supplied writer.
148        pub fn new(w: W) -> Self {
149            Self {
150                stream: w,
151                phant: ::std::marker::PhantomData,
152            }
153        }
154    }
155
156    impl<T: Serialize, C: Serialize, W: ::std::io::Write> EventPusher<T, C> for EventWriter<T, C, W> {
157        fn push(&mut self, event: Event<T, C>) {
158            // TODO: `push` has no mechanism to report errors, so we `unwrap`.
159            ::bincode::serialize_into(&mut self.stream, &event).expect("Event bincode/write failed");
160        }
161    }
162
163    /// A Wrapper for `R: Read` implementing `EventIterator<T, D>`.
164    pub struct EventReader<T, C, R: ::std::io::Read> {
165        reader: R,
166        decoded: Option<Event<T, C>>,
167    }
168
169    impl<T, C, R: ::std::io::Read> EventReader<T, C, R> {
170        /// Allocates a new `EventReader` wrapping a supplied reader.
171        pub fn new(r: R) -> Self {
172            Self {
173                reader: r,
174                decoded: None,
175            }
176        }
177    }
178
179    impl<T: DeserializeOwned + Clone, C: DeserializeOwned + Clone, R: ::std::io::Read> EventIterator<T, C> for EventReader<T, C, R> {
180        fn next(&mut self) -> Option<Cow<Event<T, C>>> {
181            self.decoded = ::bincode::deserialize_from(&mut self.reader).ok();
182            self.decoded.take().map(Cow::Owned)
183        }
184    }
185}