Files
abomonation
abomonation_derive
ansi_term
async_trait
atty
bincode
bitflags
byteorder
bytes
cfg_if
chrono
clap
dirs
dirs_sys
erdos
fixedbitset
fnv
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
indexmap
iovec
lazy_static
libc
log
memchr
mio
net2
num_cpus
num_integer
num_traits
petgraph
pin_project_lite
pin_utils
proc_macro2
proc_macro_hack
proc_macro_nested
quote
rand
rand_chacha
rand_core
rand_hc
rand_isaac
rand_jitter
rand_os
rand_pcg
rand_xorshift
serde
serde_derive
sha1
slab
slog
slog_term
strsim
syn
synstructure
term
textwrap
thread_local
time
tokio
future
io
loom
macros
net
park
runtime
stream
sync
task
time
util
tokio_macros
tokio_serde
tokio_serde_bincode
tokio_util
unicode_width
unicode_xid
uuid
vec_map
  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
/// Makes a callback which automatically flows watermarks to downstream
/// operators.
///
/// Note: this is intended as an internal macro invoked by
/// [`make_operator_executor`].
#[doc(hidden)]
#[macro_export]
macro_rules! flow_watermarks {
    (($($rs:ident),+), ($($ws:ident),+)) => {
        $crate::add_watermark_callback!(($($rs.add_state(())),+), ($($ws),+), (|timestamp, $($rs),+, $($ws),+| {
            $(
                match $ws.send(Message::new_watermark(timestamp.clone())) {
                    Ok(_) => (),
                    Err(_) => eprintln!("Error flowing watermark"),
                }
            )+
        }));
    };
    // Cases in which the system doesn't need to flow watermarks
    (($($rs:ident),+), ()) => ();
    ((), ($($ws:ident),+)) => ();
    ((), ()) => ();
}

/// Calls `Operator::new(config, rs1, rs2, ..., ws1, ws2, ...)`
/// and returns the operator instance.
///
/// Note: this is an internal macro called by [`make_operator_executor`].
#[doc(hidden)]
#[macro_export]
macro_rules! make_operator {
    ($t:ty, $config:expr, ($($rs:ident),+), ($($ws:ident),*)) => {
        <$t>::new($config.clone(), $($rs.clone()),+, $($ws.clone()),*)
    };

    ($t:ty, $config:expr, (), ($($ws:ident),+)) => {
        <$t>::new($config.clone(), $($ws.clone()),*)
    };

    ($t:ty, $config:expr, (), ()) => {
        <$t>::new($config.clone())
    };
}

/// Makes a closure that initializes the operator and returns a corresponding
/// [`OperatorExecutor`](crate::node::operator_executor::OperatorExecutor).
///
/// Note: this is intended as an internal macro called by `connect_x_write`.
#[doc(hidden)]
#[macro_export]
macro_rules! make_operator_executor {
    ($t:ty, $config:expr, ($($rs:ident),*), ($($ws:ident),*)) => {{
        // Copy IDs to avoid moving streams into closure
        // Before: $rs is an identifier pointing to a read stream
        // $ws is an identifier pointing to a write stream
        $(
            let $rs = ($rs.get_id());
        )*
        $(
            let $ws = ($ws.get_id());
        )*
        // After: $rs is an identifier pointing to a read stream's StreamId
        // $ws is an identifier pointing to a write stream's StreamId
        move |channel_manager: Arc<Mutex<ChannelManager>>, control_sender: UnboundedSender<ControlMessage>, mut control_receiver: UnboundedReceiver<ControlMessage>| {
            let mut op_ex_streams: Vec<Box<dyn OperatorExecutorStreamT>> = Vec::new();
            // Before: $rs is an identifier pointing to a read stream's StreamId
            // $ws is an identifier pointing to a write stream's StreamId
            $(
                let $rs = {
                    let recv_endpoint = channel_manager.lock().unwrap().take_recv_endpoint($rs).unwrap();
                    let read_stream = ReadStream::from(InternalReadStream::from_endpoint(recv_endpoint, $rs));
                    op_ex_streams.push(
                        Box::new(OperatorExecutorStream::from(&read_stream))
                    );
                    read_stream
                };
            )*
            $(
                let $ws = {
                    let send_endpoints = channel_manager.lock().unwrap().get_send_endpoints($ws).unwrap();
                    WriteStream::from_endpoints(send_endpoints, $ws)
                };
            )*
            // After: $rs is an identifier pointing to ReadStream
            // $ws is an identifier pointing to WriteStream
            let mut config = $config.clone();
            config.node_id = channel_manager.lock().unwrap().node_id();
            let flow_watermarks = config.flow_watermarks;
            let logger = $crate::get_terminal_logger();
            // TODO: set operator name?
            let mut op = $crate::make_operator!($t, config.clone(), ($($rs),*), ($($ws),*));
            // Pass on watermarks
            if flow_watermarks {
                $crate::flow_watermarks!(($($rs),*), ($($ws),*));
            }
            // Notify node that operator is done setting up
            if let Err(e) = control_sender.send(ControlMessage::OperatorInitialized(config.id)) {
                slog::error!(
                    logger,
                    "Error sending OperatorInitialized message to control handler: {:?}", e
                );
            }
            let mut op_executor = OperatorExecutor::new(op, config, op_ex_streams, control_receiver, logger);
            op_executor
        }
    }};
}

/// Imports crates needed to run [`register`].
///
/// Note: this is intended as an internal macro called by [`register`].
#[doc(hidden)]
#[macro_export]
macro_rules! imports {
    () => {
        use std::{
            cell::RefCell,
            rc::Rc,
            sync::{Arc, Mutex},
            thread,
            time::Duration,
        };
        use $crate::slog;
        use $crate::tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
        use $crate::{
            self,
            communication::ControlMessage,
            dataflow::graph::default_graph,
            dataflow::stream::{InternalReadStream, WriteStreamT},
            dataflow::{Message, Operator, ReadStream, WriteStream},
            node::operator_executor::{
                OperatorExecutor, OperatorExecutorStream, OperatorExecutorStreamT,
            },
            scheduler::channel_manager::ChannelManager,
            OperatorId,
        };
    };
}

/// Registers and operator and streams produced by that operator to the
/// dataflow graph and the stream manager.
///
/// Note: this is intended as an internal macro called by connect_x_write!
#[doc(hidden)]
#[macro_export]
macro_rules! register {
    ($t:ty, $config:expr, ($($rs:ident),*), ($($ws:ident),*)) => {{
        // Import necesary structs, modules, and functions.
        $crate::imports!();

        let mut config = $config.clone();
        config.id = OperatorId::new_deterministic();
        let config_copy = config.clone();

        // No-op that throws compile-time error if types in `new` and `connect` don't match.
        if false {
            let mut op = $crate::make_operator!($t, config.clone(), ($($rs),*), ($($ws),*));
            Operator::run(&mut op)
        }

        // Add operator to dataflow graph.
        let read_stream_ids = vec![$($rs.get_id()),*];
        let write_stream_ids = vec![$($ws.get_id()),*];
        let op_runner = $crate::make_operator_executor!($t, config_copy, ($($rs),*), ($($ws),*));
        default_graph::add_operator(config.id, config.name.clone(), config.node_id, read_stream_ids, write_stream_ids, op_runner);
        $(
            default_graph::add_operator_stream(config.id, &$ws);
        )*
        // Register streams with stream manager.
        ($(ReadStream::from(&$ws)),*)
    }};
}

/// Connects read streams to an operator that writes on 0 streams.
///
/// Use:
/// ```ignore
/// connect_0_write!(MyOp, arg, read_stream_1, read_stream_2, ...);
/// ```
#[macro_export]
macro_rules! connect_0_write {
    ($t:ty, $config:expr $(,$s:ident)*) => {{
        // Cast streams to read streams to avoid type errors.
        $(
            let $s = (&$s).into();
        )*
        <$t>::connect($(&$s),*);
        $crate::register!($t, $config, ($($s),*), ())
    }};
}

/// Connects read streams to an operator that writes on 1 stream.
///
/// Use:
/// ```ignore
/// let read_stream_3 = connect_3_write!(MyOp, arg, read_stream_1, read_stream_2, ...);
/// ```
#[macro_export]
macro_rules! connect_1_write {
    ($t:ty, $config:expr $(,$s:ident)*) => {{
        // Cast streams to read streams to avoid type errors.
        $(
            let $s = (&$s).into();
        )*
        let ws = <$t>::connect($(&$s),*);
        $crate::register!($t, $config, ($($s),*), (ws))
    }};
}

/// Connects read streams to an operator that writes on 2 streams.
///
/// Use:
/// ```ignore
/// let (read_stream_3, read_stream_4) = connect_3_write!(MyOp, arg, read_stream_1, read_stream_2, ...);
/// ```
#[macro_export]
macro_rules! connect_2_write {
    ($t:ty, $config:expr $(,$s:ident)*) => {{
        // Cast streams to read streams to avoid type errors.
        $(
            let $s = (&$s).into();
        )*
        let (ws1, ws2) = <$t>::connect($(&$s),*);
        $crate::register!($t, $config, ($($s),*), (ws1, ws2))
    }};
}

/// Connects read streams to an operator that writes on 3 streams.
///
/// Use:
/// ```ignore
/// let (read_stream_3, read_stream_4, read_stream_5) = connect_3_write!(MyOp, arg, read_stream_1, read_stream_2, ...);
/// ```
#[macro_export]
macro_rules! connect_3_write {
    ($t:ty, $config:expr $(,$s:ident)*) => {{
        // Cast streams to read streams to avoid type errors.
        $(
            let $s = (&$s).into();
        )*
        let (ws1, ws2, ws3) = <$t>::connect($(&$s),*);
        $crate::register!($t, $config, ($($s),*), (ws1, ws2, ws3))
    }};
}

/// Makes a callback builder that can register watermark callbacks across multiple streams.
///
/// Note: an internal macro invoked by `add_watermark_callback`.
#[doc(hidden)]
#[macro_export]
macro_rules! make_callback_builder {
    // Base case: 1 read stream, 0 write streams, state
    (($rs_head:expr), (), $state:expr) => {{
        use std::{cell::RefCell, rc::Rc};
        Rc::new(RefCell::new($rs_head.add_state($state)))
    }};

    // Base case: 1 read stream
    (($rs_head:expr), ($($ws:expr),*)) => {{
        use std::{cell::RefCell, rc::Rc};
        use $crate::dataflow::callback_builder::MultiStreamEventMaker;


        let cb_builder = Rc::new(RefCell::new($rs_head));
        $(
            let cb_builder = cb_builder.borrow_mut().add_write_stream(&$ws);
        )*
        cb_builder
    }};

    // Entry point: multiple read streams, state
    (($($rs:expr),+), ($($ws:expr),*), $state:expr) => {{
        use $crate::dataflow::callback_builder::MultiStreamEventMaker;

        make_callback_builder!(($($rs),+), ($($ws),*)).borrow_mut().add_state($state)
    }};

    // Recursive call: multiple read streams
    (($rs_head:expr, $($rs:expr),*), ($($ws:expr),*)) => {{
        use std::{cell::RefCell, rc::Rc};

        let cb_builder = Rc::new(RefCell::new($rs_head));
        $(
            let cb_builder = cb_builder.borrow_mut().add_read_stream(&$rs);
        )*
        $(
            let cb_builder = cb_builder.borrow_mut().add_write_stream(&$ws);
        )*
        cb_builder
    }};
}

/// Adds a watermark callback across several read streams.
///
/// Watermark callbacks are invoked in deterministic order.
/// Optionally add a state that is shared across callbacks.
///
/// Use:
/// ```ignore
/// add_watermark_callback!((read_stream_1, read_stream_2, ...),
///                        (write_stream_1, write_stream_2, ...)
///                        (callback_1, callback_2, ...), state?);
/// ```
#[macro_export]
macro_rules! add_watermark_callback {
    (($($rs:expr),+), ($($ws:expr),*), ($($cb:expr),+), $state:expr) => (
        let cb_builder = $crate::make_callback_builder!(($($rs),+), ($($ws),*), $state);
        $(
            cb_builder.borrow_mut().add_watermark_callback($cb);
        )+
    );
    (($($rs:expr),+), ($($ws:expr),*), ($($cb:expr),+)) => (
        let cb_builder = $crate::make_callback_builder!(($($rs),+), ($($ws),*));
        $(
            cb_builder.borrow_mut().add_watermark_callback($cb);
        )+
    );
}