Skip to main content

differential_dataflow/operators/arrange/
agent.rs

1//! Shared read access to a trace.
2
3use std::rc::{Rc, Weak};
4use std::cell::RefCell;
5use std::collections::VecDeque;
6
7use timely::dataflow::Scope;
8use timely::dataflow::operators::generic::{OperatorInfo, source};
9use timely::progress::Timestamp;
10use timely::progress::{Antichain, frontier::AntichainRef};
11use timely::dataflow::operators::CapabilitySet;
12
13use crate::trace::{Trace, TraceReader, BatchReader};
14use crate::trace::wrappers::rc::TraceBox;
15
16use timely::scheduling::Activator;
17
18use super::{TraceWriter, TraceAgentQueueWriter, TraceAgentQueueReader, Arranged};
19use super::TraceReplayInstruction;
20
21use crate::trace::wrappers::frontier::{TraceFrontier, BatchFrontier};
22
23
24/// A `TraceReader` wrapper which can be imported into other dataflows.
25///
26/// The `TraceAgent` is the default trace type produced by `arranged`, and it can be extracted
27/// from the dataflow in which it was defined, and imported into other dataflows.
28pub struct TraceAgent<Tr: TraceReader> {
29    trace: Rc<RefCell<TraceBox<Tr>>>,
30    queues: Weak<RefCell<Vec<TraceAgentQueueWriter<Tr>>>>,
31    logical_compaction: Antichain<Tr::Time>,
32    physical_compaction: Antichain<Tr::Time>,
33    temp_antichain: Antichain<Tr::Time>,
34
35    operator: OperatorInfo,
36    logging: Option<crate::logging::Logger>,
37}
38
39use crate::trace::implementations::WithLayout;
40impl<Tr: TraceReader> WithLayout for TraceAgent<Tr> {
41    type Layout = Tr::Layout;
42}
43
44impl<Tr: TraceReader> TraceReader for TraceAgent<Tr> {
45
46    type Batch = Tr::Batch;
47    type Storage = Tr::Storage;
48    type Cursor = Tr::Cursor;
49
50    fn set_logical_compaction(&mut self, frontier: AntichainRef<Tr::Time>) {
51        // This method does not enforce that `frontier` is greater or equal to `self.logical_compaction`.
52        // Instead, it determines the joint consequences of both guarantees and moves forward with that.
53        crate::lattice::antichain_join_into(&self.logical_compaction.borrow()[..], &frontier[..], &mut self.temp_antichain);
54        self.trace.borrow_mut().adjust_logical_compaction(self.logical_compaction.borrow(), self.temp_antichain.borrow());
55        ::std::mem::swap(&mut self.logical_compaction, &mut self.temp_antichain);
56        self.temp_antichain.clear();
57    }
58    fn get_logical_compaction(&mut self) -> AntichainRef<'_, Tr::Time> {
59        self.logical_compaction.borrow()
60    }
61    fn set_physical_compaction(&mut self, frontier: AntichainRef<'_, Tr::Time>) {
62        // This method does not enforce that `frontier` is greater or equal to `self.physical_compaction`.
63        // Instead, it determines the joint consequences of both guarantees and moves forward with that.
64        crate::lattice::antichain_join_into(&self.physical_compaction.borrow()[..], &frontier[..], &mut self.temp_antichain);
65        self.trace.borrow_mut().adjust_physical_compaction(self.physical_compaction.borrow(), self.temp_antichain.borrow());
66        ::std::mem::swap(&mut self.physical_compaction, &mut self.temp_antichain);
67        self.temp_antichain.clear();
68    }
69    fn get_physical_compaction(&mut self) -> AntichainRef<'_, Tr::Time> {
70        self.physical_compaction.borrow()
71    }
72    fn cursor_through(&mut self, frontier: AntichainRef<'_, Tr::Time>) -> Option<(Self::Cursor, Self::Storage)> {
73        self.trace.borrow_mut().trace.cursor_through(frontier)
74    }
75    fn map_batches<F: FnMut(&Self::Batch)>(&self, f: F) { self.trace.borrow().trace.map_batches(f) }
76}
77
78impl<Tr: TraceReader> TraceAgent<Tr> {
79    /// Creates a new agent from a trace reader.
80    pub fn new(trace: Tr, operator: OperatorInfo, logging: Option<crate::logging::Logger>) -> (Self, TraceWriter<Tr>)
81    where
82        Tr: Trace,
83    {
84        let trace = Rc::new(RefCell::new(TraceBox::new(trace)));
85        let queues = Rc::new(RefCell::new(Vec::new()));
86
87        if let Some(logging) = &logging {
88            logging.log(
89                crate::logging::TraceShare { operator: operator.global_id, diff: 1 }
90            );
91        }
92
93        let reader = TraceAgent {
94            trace: trace.clone(),
95            queues: Rc::downgrade(&queues),
96            logical_compaction: trace.borrow().logical_compaction.frontier().to_owned(),
97            physical_compaction: trace.borrow().physical_compaction.frontier().to_owned(),
98            temp_antichain: Antichain::new(),
99            operator,
100            logging,
101        };
102
103        let writer = TraceWriter::new(
104            vec![<Tr::Time as Timestamp>::minimum()],
105            Rc::downgrade(&trace),
106            queues,
107        );
108
109        (reader, writer)
110    }
111
112    /// Attaches a new shared queue to the trace.
113    ///
114    /// The queue is first populated with existing batches from the trace,
115    /// The queue will be immediately populated with existing historical batches from the trace, and until the reference
116    /// is dropped the queue will receive new batches as produced by the source `arrange` operator.
117    pub fn new_listener(&mut self, activator: Activator) -> TraceAgentQueueReader<Tr>
118    {
119        // create a new queue for progress and batch information.
120        let mut new_queue = VecDeque::new();
121
122        // add the existing batches from the trace
123        let mut upper = None;
124        self.trace
125            .borrow_mut()
126            .trace
127            .map_batches(|batch| {
128                new_queue.push_back(TraceReplayInstruction::Batch(batch.clone(), Some(<Tr::Time as Timestamp>::minimum())));
129                upper = Some(batch.upper().clone());
130            });
131
132        if let Some(upper) = upper {
133            new_queue.push_back(TraceReplayInstruction::Frontier(upper));
134        }
135
136        let reference = Rc::new((activator, RefCell::new(new_queue)));
137
138        // wraps the queue in a ref-counted ref cell and enqueue/return it.
139        if let Some(queue) = self.queues.upgrade() {
140            queue.borrow_mut().push(Rc::downgrade(&reference));
141        }
142        reference.0.activate();
143        reference
144    }
145
146    /// The [OperatorInfo] of the underlying Timely operator
147    pub fn operator(&self) -> &OperatorInfo {
148        &self.operator
149    }
150
151    /// Obtain a reference to the inner [`TraceBox`]. It is the caller's obligation to maintain
152    /// the trace box and this trace agent's invariants. Specifically, it is undefined behavior
153    /// to mutate the trace box. Keeping strong references can prevent resource reclamation.
154    ///
155    /// This method is subject to changes and removal and should not be considered part of a stable
156    /// interface.
157    pub fn trace_box_unstable(&self) -> Rc<RefCell<TraceBox<Tr>>> {
158        Rc::clone(&self.trace)
159    }
160}
161
162impl<Tr: TraceReader+'static> TraceAgent<Tr> {
163    /// Copies an existing collection into the supplied scope.
164    ///
165    /// This method creates an `Arranged` collection that should appear indistinguishable from applying `arrange`
166    /// directly to the source collection brought into the local scope. The only caveat is that the initial state
167    /// of the collection is its current state, and updates occur from this point forward. The historical changes
168    /// the collection experienced in the past are accumulated, and the distinctions from the initial collection
169    /// are no longer evident.
170    ///
171    /// The current behavior is that the introduced collection accumulates updates to some times less or equal
172    /// to `self.get_logical_compaction()`. There is *not* currently a guarantee that the updates are accumulated *to*
173    /// the frontier, and the resulting collection history may be weirdly partial until this point. In particular,
174    /// the historical collection may move through configurations that did not actually occur, even if eventually
175    /// arriving at the correct collection. This is probably a bug; although we get to the right place in the end,
176    /// the intermediate computation could do something that the original computation did not, like diverge.
177    ///
178    /// I would expect the semantics to improve to "updates are advanced to `self.get_logical_compaction()`", which
179    /// means the computation will run as if starting from exactly this frontier. It is not currently clear whose
180    /// responsibility this should be (the trace/batch should only reveal these times, or an operator should know
181    /// to advance times before using them).
182    ///
183    /// # Examples
184    ///
185    /// ```
186    /// use timely::Config;
187    /// use differential_dataflow::input::Input;
188    /// use differential_dataflow::trace::Trace;
189    /// use differential_dataflow::trace::implementations::{ValBuilder, ValSpine};
190    ///
191    /// ::timely::execute(Config::thread(), |worker| {
192    ///
193    ///     // create a first dataflow
194    ///     let mut trace = worker.dataflow::<u32,_,_>(|scope| {
195    ///         // create input handle and collection.
196    ///         scope.new_collection_from(0 .. 10).1
197    ///              .arrange_by_self()
198    ///              .trace
199    ///     });
200    ///
201    ///     // do some work.
202    ///     worker.step();
203    ///     worker.step();
204    ///
205    ///     // create a second dataflow
206    ///     worker.dataflow(move |scope| {
207    ///         trace.import(scope)
208    ///              .reduce_abelian::<_,ValBuilder<_,_,_,_>,ValSpine<_,_,_,_>>("Reduce", |_key, src, dst| dst.push((*src[0].0, 1)))
209    ///              .as_collection(|k,v| (k.clone(), v.clone()));
210    ///     });
211    ///
212    /// }).unwrap();
213    /// ```
214    pub fn import<G>(&mut self, scope: &G) -> Arranged<G, TraceAgent<Tr>>
215    where
216        G: Scope<Timestamp=Tr::Time>,
217    {
218        self.import_named(scope, "ArrangedSource")
219    }
220
221    /// Same as `import`, but allows to name the source.
222    pub fn import_named<G>(&mut self, scope: &G, name: &str) -> Arranged<G, TraceAgent<Tr>>
223    where
224        G: Scope<Timestamp=Tr::Time>,
225    {
226        // Drop ShutdownButton and return only the arrangement.
227        self.import_core(scope, name).0
228    }
229
230    /// Imports an arrangement into the supplied scope.
231    ///
232    /// # Examples
233    ///
234    /// ```
235    /// use timely::Config;
236    /// use timely::dataflow::ProbeHandle;
237    /// use timely::dataflow::operators::Probe;
238    /// use differential_dataflow::input::InputSession;
239    /// use differential_dataflow::trace::Trace;
240    ///
241    /// ::timely::execute(Config::thread(), |worker| {
242    ///
243    ///     let mut input = InputSession::<_,(),isize>::new();
244    ///     let mut probe = ProbeHandle::new();
245    ///
246    ///     // create a first dataflow
247    ///     let mut trace = worker.dataflow::<u32,_,_>(|scope| {
248    ///         // create input handle and collection.
249    ///         input.to_collection(scope)
250    ///              .arrange_by_self()
251    ///              .trace
252    ///     });
253    ///
254    ///     // do some work.
255    ///     worker.step();
256    ///     worker.step();
257    ///
258    ///     // create a second dataflow
259    ///     let mut shutdown = worker.dataflow(|scope| {
260    ///         let (arrange, button) = trace.import_core(scope, "Import");
261    ///         arrange.stream.probe_with(&mut probe);
262    ///         button
263    ///     });
264    ///
265    ///     worker.step();
266    ///     worker.step();
267    ///     assert!(!probe.done());
268    ///
269    ///     shutdown.press();
270    ///
271    ///     worker.step();
272    ///     worker.step();
273    ///     assert!(probe.done());
274    ///
275    /// }).unwrap();
276    /// ```
277    pub fn import_core<G>(&mut self, scope: &G, name: &str) -> (Arranged<G, TraceAgent<Tr>>, ShutdownButton<CapabilitySet<Tr::Time>>)
278    where
279        G: Scope<Timestamp=Tr::Time>,
280    {
281        let trace = self.clone();
282
283        let mut shutdown_button = None;
284
285        let stream = {
286
287            let shutdown_button_ref = &mut shutdown_button;
288            source(scope, name, move |capability, info| {
289
290                let capabilities = Rc::new(RefCell::new(Some(CapabilitySet::new())));
291
292                let activator = scope.activator_for(Rc::clone(&info.address));
293                let queue = self.new_listener(activator);
294
295                let activator = scope.activator_for(info.address);
296                *shutdown_button_ref = Some(ShutdownButton::new(capabilities.clone(), activator));
297
298                capabilities.borrow_mut().as_mut().unwrap().insert(capability);
299
300                move |output| {
301
302                    let mut capabilities = capabilities.borrow_mut();
303                    if let Some(ref mut capabilities) = *capabilities {
304
305                        let mut borrow = queue.1.borrow_mut();
306                        for instruction in borrow.drain(..) {
307                            match instruction {
308                                TraceReplayInstruction::Frontier(frontier) => {
309                                    capabilities.downgrade(&frontier.borrow()[..]);
310                                },
311                                TraceReplayInstruction::Batch(batch, hint) => {
312                                    if let Some(time) = hint {
313                                        if !batch.is_empty() {
314                                            let delayed = capabilities.delayed(&time);
315                                            output.session(&delayed).give(batch);
316                                        }
317                                    }
318                                }
319                            }
320                        }
321                    }
322                }
323            })
324        };
325
326        (Arranged { stream, trace }, shutdown_button.unwrap())
327    }
328
329    /// Imports an arrangement into the supplied scope.
330    ///
331    /// This variant of import uses the `get_logical_compaction` to forcibly advance timestamps in updates.
332    ///
333    /// # Examples
334    ///
335    /// ```
336    /// use timely::Config;
337    /// use timely::progress::frontier::AntichainRef;
338    /// use timely::dataflow::ProbeHandle;
339    /// use timely::dataflow::operators::Probe;
340    /// use timely::dataflow::operators::Inspect;
341    /// use differential_dataflow::input::InputSession;
342    /// use differential_dataflow::trace::Trace;
343    /// use differential_dataflow::trace::TraceReader;
344    /// use differential_dataflow::input::Input;
345    ///
346    /// ::timely::execute(Config::thread(), |worker| {
347    ///
348    ///     let mut probe = ProbeHandle::new();
349    ///
350    ///     // create a first dataflow
351    ///     let (mut handle, mut trace) = worker.dataflow::<u32,_,_>(|scope| {
352    ///         // create input handle and collection.
353    ///         let (handle, stream) = scope.new_collection();
354    ///         let trace = stream.arrange_by_self().trace;
355    ///         (handle, trace)
356    ///     });
357    ///
358    ///     handle.insert(0); handle.advance_to(1); handle.flush(); worker.step();
359    ///     handle.remove(0); handle.advance_to(2); handle.flush(); worker.step();
360    ///     handle.insert(1); handle.advance_to(3); handle.flush(); worker.step();
361    ///     handle.remove(1); handle.advance_to(4); handle.flush(); worker.step();
362    ///     handle.insert(0); handle.advance_to(5); handle.flush(); worker.step();
363    ///
364    ///     trace.set_logical_compaction(AntichainRef::new(&[5]));
365    ///
366    ///     // create a second dataflow
367    ///     let mut shutdown = worker.dataflow(|scope| {
368    ///         let (arrange, button) = trace.import_frontier(scope, "Import");
369    ///         arrange
370    ///             .as_collection(|k,v| (*k,*v))
371    ///             .inner
372    ///             .inspect(|(d,t,r)| {
373    ///                 assert!(t >= &5);
374    ///             })
375    ///             .probe_with(&mut probe);
376    ///
377    ///         button
378    ///     });
379    ///
380    ///     worker.step();
381    ///     worker.step();
382    ///     assert!(!probe.done());
383    ///
384    ///     shutdown.press();
385    ///
386    ///     worker.step();
387    ///     worker.step();
388    ///     assert!(probe.done());
389    ///
390    /// }).unwrap();
391    /// ```
392    pub fn import_frontier<G>(&mut self, scope: &G, name: &str) -> (Arranged<G, TraceFrontier<TraceAgent<Tr>>>, ShutdownButton<CapabilitySet<Tr::Time>>)
393    where
394        G: Scope<Timestamp=Tr::Time>,
395        Tr: TraceReader,
396    {
397        // This frontier describes our only guarantee on the compaction frontier.
398        let since = self.get_logical_compaction().to_owned();
399        self.import_frontier_core(scope, name, since, Antichain::new())
400    }
401
402    /// Import a trace restricted to a specific time interval `[since, until)`.
403    ///
404    /// All updates present in the input trace will be first advanced to `since`, and then either emitted,
405    /// or if greater or equal to `until`, suppressed. Once all times are certain to be greater or equal
406    /// to `until` the operator capability will be dropped.
407    ///
408    /// Invoking this method with an `until` of `Antichain::new()` will perform no filtering, as the empty
409    /// frontier indicates the end of times.
410    pub fn import_frontier_core<G>(&mut self, scope: &G, name: &str, since: Antichain<Tr::Time>, until: Antichain<Tr::Time>) -> (Arranged<G, TraceFrontier<TraceAgent<Tr>>>, ShutdownButton<CapabilitySet<Tr::Time>>)
411    where
412        G: Scope<Timestamp=Tr::Time>,
413        Tr: TraceReader,
414    {
415        let trace = self.clone();
416        let trace = TraceFrontier::make_from(trace, since.borrow(), until.borrow());
417
418        let mut shutdown_button = None;
419
420        let stream = {
421
422            let shutdown_button_ref = &mut shutdown_button;
423            source(scope, name, move |capability, info| {
424
425                let capabilities = Rc::new(RefCell::new(Some(CapabilitySet::new())));
426
427                let activator = scope.activator_for(Rc::clone(&info.address));
428                let queue = self.new_listener(activator);
429
430                let activator = scope.activator_for(info.address);
431                *shutdown_button_ref = Some(ShutdownButton::new(capabilities.clone(), activator));
432
433                capabilities.borrow_mut().as_mut().unwrap().insert(capability);
434
435                move |output| {
436
437                    let mut capabilities = capabilities.borrow_mut();
438                    if let Some(ref mut capabilities) = *capabilities {
439                        let mut borrow = queue.1.borrow_mut();
440                        for instruction in borrow.drain(..) {
441                            // If we have dropped the capabilities due to `until`, attempt no further work.
442                            // Without the capabilities, we should soon be shut down (once this loop ends).
443                            if !capabilities.is_empty() {
444                                match instruction {
445                                    TraceReplayInstruction::Frontier(frontier) => {
446                                        if timely::PartialOrder::less_equal(&until, &frontier) {
447                                            // It might be nice to actively *drop* `capabilities`, but it seems
448                                            // complicated logically (i.e. we'd have to break out of the loop).
449                                            capabilities.downgrade(&[]);
450                                        } else {
451                                            capabilities.downgrade(&frontier.borrow()[..]);
452                                        }
453                                    },
454                                    TraceReplayInstruction::Batch(batch, hint) => {
455                                        if let Some(time) = hint {
456                                            if !batch.is_empty() {
457                                                let delayed = capabilities.delayed(&time);
458                                                output.session(&delayed).give(BatchFrontier::make_from(batch, since.borrow(), until.borrow()));
459                                            }
460                                        }
461                                    }
462                                }
463                            }
464                        }
465                    }
466                }
467            })
468        };
469
470        (Arranged { stream, trace }, shutdown_button.unwrap())
471    }
472}
473
474
475
476/// Wrapper than can drop shared references.
477pub struct ShutdownButton<T> {
478    reference: Rc<RefCell<Option<T>>>,
479    activator: Activator,
480}
481
482impl<T> ShutdownButton<T> {
483    /// Creates a new ShutdownButton.
484    pub fn new(reference: Rc<RefCell<Option<T>>>, activator: Activator) -> Self {
485        Self { reference, activator }
486    }
487    /// Push the shutdown button, dropping the shared objects.
488    pub fn press(&mut self) {
489        *self.reference.borrow_mut() = None;
490        self.activator.activate();
491    }
492    /// Hotwires the button to one that is pressed if dropped.
493    pub fn press_on_drop(self) -> ShutdownDeadmans<T> {
494        ShutdownDeadmans {
495            button: self
496        }
497    }
498}
499
500/// A deadman's switch version of a shutdown button.
501///
502/// This type hosts a shutdown button and will press it when dropped.
503pub struct ShutdownDeadmans<T> {
504    button: ShutdownButton<T>,
505}
506
507impl<T> Drop for ShutdownDeadmans<T> {
508    fn drop(&mut self) {
509        self.button.press();
510    }
511}
512
513impl<Tr: TraceReader> Clone for TraceAgent<Tr> {
514    fn clone(&self) -> Self {
515
516        if let Some(logging) = &self.logging {
517            logging.log(
518                crate::logging::TraceShare { operator: self.operator.global_id, diff: 1 }
519            );
520        }
521
522        // increase counts for wrapped `TraceBox`.
523        let empty_frontier = Antichain::new();
524        self.trace.borrow_mut().adjust_logical_compaction(empty_frontier.borrow(), self.logical_compaction.borrow());
525        self.trace.borrow_mut().adjust_physical_compaction(empty_frontier.borrow(), self.physical_compaction.borrow());
526
527        TraceAgent {
528            trace: self.trace.clone(),
529            queues: self.queues.clone(),
530            logical_compaction: self.logical_compaction.clone(),
531            physical_compaction: self.physical_compaction.clone(),
532            operator: self.operator.clone(),
533            logging: self.logging.clone(),
534            temp_antichain: Antichain::new(),
535        }
536    }
537}
538
539impl<Tr: TraceReader> Drop for TraceAgent<Tr> {
540    fn drop(&mut self) {
541
542        if let Some(logging) = &self.logging {
543            logging.log(
544                crate::logging::TraceShare { operator: self.operator.global_id, diff: -1 }
545            );
546        }
547
548        // decrement borrow counts to remove all holds
549        let empty_frontier = Antichain::new();
550        self.trace.borrow_mut().adjust_logical_compaction(self.logical_compaction.borrow(), empty_frontier.borrow());
551        self.trace.borrow_mut().adjust_physical_compaction(self.physical_compaction.borrow(), empty_frontier.borrow());
552    }
553}