timely/dataflow/operators/core/
probe.rs

1//! Monitor progress at a `Stream`.
2
3use std::rc::Rc;
4use std::cell::RefCell;
5
6use crate::progress::Timestamp;
7use crate::progress::frontier::{AntichainRef, MutableAntichain};
8use crate::dataflow::channels::pushers::Counter as PushCounter;
9use crate::dataflow::channels::pushers::buffer::Buffer as PushBuffer;
10use crate::dataflow::channels::pact::Pipeline;
11use crate::dataflow::channels::pullers::Counter as PullCounter;
12use crate::dataflow::operators::generic::builder_raw::OperatorBuilder;
13
14
15use crate::dataflow::{StreamCore, Scope};
16use crate::{Container, Data};
17
18/// Monitors progress at a `Stream`.
19pub trait Probe<G: Scope, C: Container> {
20    /// Constructs a progress probe which indicates which timestamps have elapsed at the operator.
21    ///
22    /// # Examples
23    /// ```
24    /// use timely::*;
25    /// use timely::dataflow::Scope;
26    /// use timely::dataflow::operators::{Input, Probe, Inspect};
27    ///
28    /// // construct and execute a timely dataflow
29    /// timely::execute(Config::thread(), |worker| {
30    ///
31    ///     // add an input and base computation off of it
32    ///     let (mut input, probe) = worker.dataflow(|scope| {
33    ///         let (input, stream) = scope.new_input();
34    ///         let probe = stream.inspect(|x| println!("hello {:?}", x))
35    ///                           .probe();
36    ///         (input, probe)
37    ///     });
38    ///
39    ///     // introduce input, advance computation
40    ///     for round in 0..10 {
41    ///         input.send(round);
42    ///         input.advance_to(round + 1);
43    ///         worker.step_while(|| probe.less_than(input.time()));
44    ///     }
45    /// }).unwrap();
46    /// ```
47    fn probe(&self) -> Handle<G::Timestamp>;
48
49    /// Inserts a progress probe in a stream.
50    ///
51    /// # Examples
52    /// ```
53    /// use timely::*;
54    /// use timely::dataflow::Scope;
55    /// use timely::dataflow::operators::{Input, Probe, Inspect};
56    /// use timely::dataflow::operators::probe::Handle;
57    ///
58    /// // construct and execute a timely dataflow
59    /// timely::execute(Config::thread(), |worker| {
60    ///
61    ///     // add an input and base computation off of it
62    ///     let mut probe = Handle::new();
63    ///     let mut input = worker.dataflow(|scope| {
64    ///         let (input, stream) = scope.new_input();
65    ///         stream.probe_with(&mut probe)
66    ///               .inspect(|x| println!("hello {:?}", x));
67    ///
68    ///         input
69    ///     });
70    ///
71    ///     // introduce input, advance computation
72    ///     for round in 0..10 {
73    ///         input.send(round);
74    ///         input.advance_to(round + 1);
75    ///         worker.step_while(|| probe.less_than(input.time()));
76    ///     }
77    /// }).unwrap();
78    /// ```
79    fn probe_with(&self, handle: &Handle<G::Timestamp>) -> StreamCore<G, C>;
80}
81
82impl<G: Scope, C: Container + Data> Probe<G, C> for StreamCore<G, C> {
83    fn probe(&self) -> Handle<G::Timestamp> {
84
85        // the frontier is shared state; scope updates, handle reads.
86        let handle = Handle::<G::Timestamp>::new();
87        self.probe_with(&handle);
88        handle
89    }
90    fn probe_with(&self, handle: &Handle<G::Timestamp>) -> StreamCore<G, C> {
91
92        let mut builder = OperatorBuilder::new("Probe".to_owned(), self.scope());
93        let mut input = PullCounter::new(builder.new_input(self, Pipeline));
94        let (tee, stream) = builder.new_output();
95        let mut output = PushBuffer::new(PushCounter::new(tee));
96
97        let shared_frontier = Rc::downgrade(&handle.frontier);
98        let mut started = false;
99
100        builder.build(
101            move |progress| {
102
103                // surface all frontier changes to the shared frontier.
104                if let Some(shared_frontier) = shared_frontier.upgrade() {
105                    let mut borrow = shared_frontier.borrow_mut();
106                    borrow.update_iter(progress.frontiers[0].drain());
107                }
108
109                if !started {
110                    // discard initial capability.
111                    progress.internals[0].update(G::Timestamp::minimum(), -1);
112                    started = true;
113                }
114
115                while let Some(message) = input.next() {
116                    let time = &message.time;
117                    let data = &mut message.data;
118                    output.session(time).give_container(data);
119                }
120                output.cease();
121
122                // extract what we know about progress from the input and output adapters.
123                input.consumed().borrow_mut().drain_into(&mut progress.consumeds[0]);
124                output.inner().produced().borrow_mut().drain_into(&mut progress.produceds[0]);
125
126                false
127            },
128        );
129
130        stream
131    }
132}
133
134/// Reports information about progress at the probe.
135#[derive(Debug)]
136pub struct Handle<T:Timestamp> {
137    frontier: Rc<RefCell<MutableAntichain<T>>>
138}
139
140impl<T: Timestamp> Handle<T> {
141    /// Returns `true` iff the frontier is strictly less than `time`.
142    #[inline] pub fn less_than(&self, time: &T) -> bool { self.frontier.borrow().less_than(time) }
143    /// Returns `true` iff the frontier is less than or equal to `time`.
144    #[inline] pub fn less_equal(&self, time: &T) -> bool { self.frontier.borrow().less_equal(time) }
145    /// Returns `true` iff the frontier is empty.
146    #[inline] pub fn done(&self) -> bool { self.frontier.borrow().is_empty() }
147    /// Allocates a new handle.
148    #[inline] pub fn new() -> Self { Handle { frontier: Rc::new(RefCell::new(MutableAntichain::new())) } }
149
150    /// Invokes a method on the frontier, returning its result.
151    ///
152    /// This method allows inspection of the frontier, which cannot be returned by reference as
153    /// it is on the other side of a `RefCell`.
154    ///
155    /// # Examples
156    ///
157    /// ```
158    /// use timely::dataflow::operators::probe::Handle;
159    ///
160    /// let handle = Handle::<usize>::new();
161    /// let frontier = handle.with_frontier(|frontier| frontier.to_vec());
162    /// ```
163    #[inline]
164    pub fn with_frontier<R, F: FnMut(AntichainRef<T>)->R>(&self, mut function: F) -> R {
165        function(self.frontier.borrow().frontier())
166    }
167}
168
169impl<T: Timestamp> Clone for Handle<T> {
170    fn clone(&self) -> Self {
171        Handle {
172            frontier: Rc::clone(&self.frontier)
173        }
174    }
175}
176
177impl<T> Default for Handle<T>
178where
179    T: Timestamp,
180{
181    fn default() -> Self {
182        Self::new()
183    }
184}
185
186#[cfg(test)]
187mod tests {
188
189    use crate::Config;
190    use crate::dataflow::operators::{Input, Probe};
191
192    #[test]
193    fn probe() {
194
195        // initializes and runs a timely dataflow computation
196        crate::execute(Config::thread(), |worker| {
197
198            // create a new input, and inspect its output
199            let (mut input, probe) = worker.dataflow(move |scope| {
200                let (input, stream) = scope.new_input::<String>();
201                (input, stream.probe())
202            });
203
204            // introduce data and watch!
205            for round in 0..10 {
206                assert!(!probe.done());
207                assert!(probe.less_equal(&round));
208                assert!(probe.less_than(&(round + 1)));
209                input.advance_to(round + 1);
210                worker.step();
211            }
212
213            // seal the input
214            input.close();
215
216            // finish off any remaining work
217            worker.step();
218            worker.step();
219            worker.step();
220            worker.step();
221            assert!(probe.done());
222        }).unwrap();
223    }
224
225}