timely/dataflow/operators/core/capture/
event.rs1use columnar::Columnar;
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Deserialize, Serialize, Columnar)]
12pub enum Event<T, C> {
13 Progress(Vec<(T, i64)>),
15 Messages(T, C),
17}
18
19pub trait EventIterator<T: Clone, C: Clone> {
26 fn next(&mut self) -> Option<std::borrow::Cow<'_, Event<T, C>>>;
28}
29
30pub trait EventPusher<T, C> {
32 fn push(&mut self, event: Event<T, C>);
34}
35
36impl<T, C> EventPusher<T, C> for ::std::sync::mpsc::Sender<Event<T, C>> {
38 fn push(&mut self, event: Event<T, C>) {
39 let _ = self.send(event);
42 }
43}
44
45pub 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 pub struct EventLink<T, C> {
56 pub event: Option<Event<T, C>>,
61 pub next: RefCell<Option<Rc<EventLink<T, C>>>>,
63 }
64
65 impl<T, C> EventLink<T, C> {
66 pub fn new() -> EventLink<T, C> {
68 EventLink { event: None, next: RefCell::new(None) }
69 }
70 }
71
72 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 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
131pub mod binary {
133
134 use std::borrow::Cow;
135 use std::io::ErrorKind;
136 use std::ops::DerefMut;
137 use std::sync::Arc;
138
139 use serde::{de::DeserializeOwned, Serialize};
140 use timely_communication::allocator::zero_copy::bytes_slab::{BytesRefill, BytesSlab};
141
142 use super::{Event, EventPusher, EventIterator};
143
144 pub struct EventWriter<T, C, W: ::std::io::Write> {
146 stream: W,
147 phant: ::std::marker::PhantomData<(T, C)>,
148 }
149
150 impl<T, C, W: ::std::io::Write> EventWriter<T, C, W> {
151 pub fn new(w: W) -> Self {
153 Self {
154 stream: w,
155 phant: ::std::marker::PhantomData,
156 }
157 }
158 }
159
160 impl<T: Serialize, C: Serialize, W: ::std::io::Write> EventPusher<T, C> for EventWriter<T, C, W> {
161 fn push(&mut self, event: Event<T, C>) {
162 let len = ::bincode::serialized_size(&event).expect("Event bincode failed");
164 self.stream.write_all(&len.to_le_bytes()).expect("Event write failed");
165 ::bincode::serialize_into(&mut self.stream, &event).expect("Event bincode failed");
166 }
167 }
168
169 pub struct EventReader<T, C, R: ::std::io::Read> {
171 reader: R,
172 buf: BytesSlab,
173 phant: ::std::marker::PhantomData<(T, C)>,
174 }
175
176 impl<T, C, R: ::std::io::Read> EventReader<T, C, R> {
177 pub fn new(r: R) -> Self {
179 let refill = BytesRefill {
180 logic: Arc::new(|size| {
181 Box::new(vec![0_u8; size]) as Box<dyn DerefMut<Target = [u8]>>
182 }),
183 limit: None,
184 };
185 Self {
186 reader: r,
187 buf: BytesSlab::new(20, refill),
188 phant: ::std::marker::PhantomData,
189 }
190 }
191 }
192
193 impl<T: DeserializeOwned + Clone, C: DeserializeOwned + Clone, R: ::std::io::Read> EventIterator<T, C> for EventReader<T, C, R> {
194 fn next(&mut self) -> Option<Cow<'_, Event<T, C>>> {
195 self.buf.ensure_capacity(1);
196 match self.reader.read(self.buf.empty()) {
198 Ok(n) => self.buf.make_valid(n),
199 Err(e) if e.kind() == ErrorKind::WouldBlock => {}
200 Err(e) => panic!("read failed: {e}"),
201 };
202
203 let valid = self.buf.valid();
204 if valid.len() >= 8 {
205 let event_len = u64::from_le_bytes([
206 valid[0], valid[1], valid[2], valid[3], valid[4], valid[5], valid[6], valid[7],
207 ]);
208 let required_bytes = (event_len + 8) as usize;
209 if valid.len() >= required_bytes {
210 let bytes = self.buf.extract(required_bytes);
211 let event = ::bincode::deserialize(&bytes[8..]).expect("Event decode failed");
212 Some(Cow::Owned(event))
213 } else {
214 None
215 }
216 } else {
217 None
218 }
219 }
220 }
221}