Skip to main content

hydro_lang/location/
tick.rs

1//! Clock domains for batching streaming data into discrete time steps.
2//!
3//! In Hydro, a [`Tick`] represents a logical clock that can be used to batch
4//! unbounded streaming data into discrete, bounded time steps. This is essential
5//! for implementing iterative algorithms, synchronizing data across multiple
6//! streams, and performing aggregations over windows of data.
7//!
8//! A tick is created from a top-level location (such as [`super::Process`] or [`super::Cluster`])
9//! using [`Location::tick`]. Once inside a tick, bounded live collections can be
10//! manipulated with operations like fold, reduce, and cross-product, and the
11//! results can be emitted back to the unbounded stream using methods like
12//! `all_ticks()`.
13//!
14//! The [`Atomic`] wrapper provides atomicity guarantees within a tick, ensuring
15//! that reads and writes within a tick are serialized.
16
17use stageleft::{QuotedWithContext, q};
18
19#[cfg(stageleft_runtime)]
20use super::dynamic::DynLocation;
21use super::{Location, LocationId};
22use crate::compile::builder::{ClockId, FlowState};
23use crate::compile::ir::{HydroNode, HydroSource};
24#[cfg(stageleft_runtime)]
25use crate::forward_handle::{CycleCollection, CycleCollectionWithInitial};
26use crate::forward_handle::{TickCycle, TickCycleHandle};
27#[cfg(feature = "tokio")]
28use crate::live_collections::Singleton;
29use crate::live_collections::boundedness::Bounded;
30use crate::live_collections::optional::Optional;
31use crate::live_collections::stream::{ExactlyOnce, Stream, TotalOrder};
32use crate::location::TopLevel;
33#[cfg(feature = "tokio")]
34use crate::nondet::NonDet;
35use crate::nondet::nondet;
36
37/// A location wrapper that provides atomicity guarantees within a [`Tick`].
38///
39/// An `Atomic` context establishes a happens-before relationship between operations:
40/// - Downstream computations from `atomic()` are associated with an internal tick
41/// - Outputs from `end_atomic()` are held until all computations in the tick complete
42/// - Snapshots via `use::atomic` are guaranteed to reflect all updates from associated `end_atomic()`
43///
44/// This ensures read-after-write consistency: if a client receives an acknowledgement
45/// from `end_atomic()`, any subsequent `use::atomic` snapshot will include the effects
46/// of that acknowledged operation.
47#[derive(Clone)]
48pub struct Atomic<Loc> {
49    pub(crate) tick: Tick<Loc>,
50}
51
52impl<L: DynLocation> DynLocation for Atomic<L> {
53    fn dyn_id(&self) -> LocationId {
54        LocationId::Atomic(Box::new(self.tick.dyn_id()))
55    }
56
57    fn flow_state(&self) -> &FlowState {
58        self.tick.flow_state()
59    }
60
61    fn is_top_level() -> bool {
62        L::is_top_level()
63    }
64
65    fn multiversioned(&self) -> bool {
66        self.tick.multiversioned()
67    }
68
69    fn cluster_consistency() -> Option<super::dynamic::ClusterConsistency> {
70        L::cluster_consistency()
71    }
72}
73
74impl<'a, L> Location<'a> for Atomic<L>
75where
76    L: Location<'a>,
77{
78    type Root = L::Root;
79
80    type SimHookScope = L::SimHookScope;
81
82    type DropConsistency = Atomic<L::DropConsistency>;
83
84    fn consistency() -> Option<super::dynamic::ClusterConsistency> {
85        L::consistency()
86    }
87
88    fn root(&self) -> Self::Root {
89        self.tick.root()
90    }
91
92    fn drop_consistency(&self) -> Self::DropConsistency {
93        Atomic {
94            tick: self.tick.drop_consistency(),
95        }
96    }
97
98    fn from_drop_consistency(l2: Self::DropConsistency) -> Self {
99        Atomic {
100            tick: Tick::from_drop_consistency(l2.tick),
101        }
102    }
103}
104
105/// Trait for live collections that can be deferred by one tick.
106///
107/// When a collection implements `DeferTick`, calling `defer_tick` delays its
108/// values by one clock cycle. This is primarily used internally to implement
109/// tick-based cycles ([`Tick::cycle`]), ensuring that feedback loops advance
110/// by one tick to avoid infinite recursion within a single tick.
111pub trait DeferTick {
112    /// Returns a new collection whose values are delayed by one tick.
113    fn defer_tick(self) -> Self;
114}
115
116/// Marks the stream as being inside the single global clock domain.
117#[derive(Clone)]
118pub struct Tick<L> {
119    /// `None` if `l` is `Atomic`.
120    pub(crate) id: Option<ClockId>,
121    /// Location.
122    pub(crate) l: L,
123}
124
125impl<L: DynLocation> DynLocation for Tick<L> {
126    fn dyn_id(&self) -> LocationId {
127        LocationId::Tick {
128            tick: self.id,
129            parent_location: Box::new(self.l.dyn_id()),
130        }
131    }
132
133    fn flow_state(&self) -> &FlowState {
134        self.l.flow_state()
135    }
136
137    fn is_top_level() -> bool {
138        false
139    }
140
141    fn multiversioned(&self) -> bool {
142        self.l.multiversioned()
143    }
144
145    fn cluster_consistency() -> Option<super::dynamic::ClusterConsistency> {
146        L::cluster_consistency()
147    }
148}
149
150impl<'a, L> Location<'a> for Tick<L>
151where
152    L: Location<'a>,
153{
154    type Root = L::Root;
155
156    type SimHookScope = L::SimHookScope;
157
158    type DropConsistency = Tick<L::DropConsistency>;
159
160    fn consistency() -> Option<super::dynamic::ClusterConsistency> {
161        L::consistency()
162    }
163
164    fn root(&self) -> Self::Root {
165        self.l.root()
166    }
167
168    fn drop_consistency(&self) -> Self::DropConsistency {
169        Tick {
170            id: self.id,
171            l: self.l.drop_consistency(),
172        }
173    }
174
175    fn from_drop_consistency(l2: Self::DropConsistency) -> Self {
176        Tick {
177            id: l2.id,
178            l: L::from_drop_consistency(l2.l),
179        }
180    }
181}
182
183impl<'a, L> Tick<L>
184where
185    L: Location<'a>,
186{
187    /// Returns a reference to the parent location that this tick is located at.
188    ///
189    /// For example, if a `Tick` was created from a `Process`, this returns a reference
190    /// to that `Process`.
191    pub fn parent_location(&self) -> &L {
192        &self.l
193    }
194
195    /// Use [`Self::parent_location`] instead.
196    #[deprecated(note = "use `.parent_location()` instead")]
197    pub fn outer(&self) -> &L {
198        self.parent_location()
199    }
200
201    /// Creates a bounded stream of `()` values inside this tick, with a fixed batch size.
202    ///
203    /// This is useful for driving computations inside a tick that need to process
204    /// a specific number of elements per tick. Each tick will produce exactly
205    /// `batch_size` unit values.
206    pub fn spin_batch(
207        &self,
208        batch_size: impl QuotedWithContext<
209            'a,
210            usize,
211            crate::live_collections::OperatorContext<
212                L,
213                crate::live_collections::boundedness::Unbounded,
214            >,
215        > + Copy
216        + 'a,
217    ) -> Stream<(), Self, Bounded, TotalOrder, ExactlyOnce>
218    where
219        L: TopLevel<'a>,
220    {
221        let out = self
222            .l
223            .spin()
224            .flat_map_ordered(q!(move |_| 0..batch_size))
225            .map(q!(|_| ()));
226
227        let inner = out.batch(self, nondet!(/** at runtime, `spin` produces a single value per tick, so each batch is guaranteed to be the same size. */));
228        Stream::new(self.clone(), inner.ir_node.replace(HydroNode::Placeholder))
229    }
230
231    /// Creates an [`Optional`] which has a null value on every tick.
232    ///
233    /// # Example
234    /// ```rust
235    /// # #[cfg(feature = "deploy")] {
236    /// # use hydro_lang::prelude::*;
237    /// # use futures::StreamExt;
238    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
239    /// let tick = process.tick();
240    /// # // ticks are lazy by default, forces the tick to run
241    /// # tick.spin_batch(q!(1)).all_ticks().for_each(q!(|_| {}));
242    /// let optional = tick.none::<i32>();
243    /// optional.unwrap_or(tick.singleton(q!(123)))
244    /// # .all_ticks()
245    /// # }, |mut stream| async move {
246    /// // 123
247    /// # assert_eq!(stream.next().await.unwrap(), 123);
248    /// # }));
249    /// # }
250    /// ```
251    pub fn none<T>(&self) -> Optional<T, Self, Bounded> {
252        let e = q!([]);
253        let e = QuotedWithContext::<'a, [(); 0], Self>::splice_typed_ctx(e, self);
254
255        let unit_optional: Optional<(), Self, Bounded> = Optional::new(
256            self.clone(),
257            HydroNode::Source {
258                source: HydroSource::Iter(e.into()),
259                metadata: self.new_node_metadata(Optional::<(), Self, Bounded>::collection_kind()),
260            },
261        );
262
263        unit_optional.map(q!(|_| unreachable!())) // always empty
264    }
265
266    /// Creates an [`Optional`] which will have the provided static value on the first tick, and be
267    /// null on all subsequent ticks.
268    ///
269    /// This is useful for bootstrapping stateful computations which need an initial value.
270    ///
271    /// # Example
272    /// ```rust
273    /// # #[cfg(feature = "deploy")] {
274    /// # use hydro_lang::prelude::*;
275    /// # use futures::StreamExt;
276    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
277    /// let tick = process.tick();
278    /// // ticks are lazy by default, forces the second tick to run
279    /// tick.spin_batch(q!(1)).all_ticks().for_each(q!(|_| {}));
280    /// let optional = tick.optional_first_tick(q!(5));
281    /// optional.unwrap_or(tick.singleton(q!(123))).all_ticks()
282    /// # }, |mut stream| async move {
283    /// // 5, 123, 123, 123, ...
284    /// # assert_eq!(stream.next().await.unwrap(), 5);
285    /// # assert_eq!(stream.next().await.unwrap(), 123);
286    /// # assert_eq!(stream.next().await.unwrap(), 123);
287    /// # assert_eq!(stream.next().await.unwrap(), 123);
288    /// # }));
289    /// # }
290    /// ```
291    pub fn optional_first_tick<T: Clone>(
292        &self,
293        e: impl QuotedWithContext<'a, T, Tick<L>>,
294    ) -> Optional<T, Self, Bounded> {
295        let e = e.splice_untyped_ctx(self);
296
297        Optional::new(
298            self.clone(),
299            HydroNode::SingletonSource {
300                value: e.into(),
301                first_tick_only: true,
302                metadata: self.new_node_metadata(Optional::<T, Self, Bounded>::collection_kind()),
303            },
304        )
305    }
306
307    /// Returns the current wall-clock time as a [`Singleton`] containing a
308    /// [`tokio::time::Instant`].
309    ///
310    /// # Non-Determinism
311    /// Reading wall-clock time is inherently non-deterministic because the
312    /// value depends on when the tick executes. A [`NonDet`] guard is required
313    /// to acknowledge this.
314    #[cfg(feature = "tokio")]
315    pub fn current_tick_instant(
316        &self,
317        _nondet: NonDet,
318    ) -> Singleton<tokio::time::Instant, Tick<L::DropConsistency>, Bounded>
319    where
320        Self: Sized,
321    {
322        // TODO(shadaj): this is a simulator hole, should be reported as unsupported until it is
323        self.singleton(q!(tokio::time::Instant::now()))
324    }
325
326    /// Creates a feedback cycle within this tick for implementing iterative computations.
327    ///
328    /// Returns a handle that must be completed with the actual collection, and a placeholder
329    /// collection that represents the output of the previous tick (deferred by one tick).
330    /// This is useful for implementing fixed-point computations where the output of one
331    /// tick feeds into the input of the next.
332    ///
333    /// The cycle automatically defers values by one tick to prevent infinite recursion.
334    #[expect(
335        private_bounds,
336        reason = "only Hydro collections can implement ReceiverComplete"
337    )]
338    pub fn cycle<S, L2: Location<'a, DropConsistency = Tick<L::DropConsistency>>>(
339        &self,
340    ) -> (TickCycleHandle<'a, S>, S)
341    where
342        S: CycleCollection<'a, TickCycle, Location = L2> + DeferTick,
343    {
344        let cycle_id = self.flow_state().borrow_mut().next_cycle_id();
345        (
346            TickCycleHandle::new(cycle_id, Location::id(self)),
347            S::create_source(cycle_id, self.clone().with_consistency_of()).defer_tick(),
348        )
349    }
350
351    /// Creates a feedback cycle with an initial value for the first tick.
352    ///
353    /// Similar to [`Tick::cycle`], but allows providing an initial collection
354    /// that will be used as the value on the first tick before any feedback
355    /// is available. This is useful for bootstrapping iterative computations
356    /// that need a starting state.
357    #[expect(
358        private_bounds,
359        reason = "only Hydro collections can implement ReceiverComplete"
360    )]
361    pub fn cycle_with_initial<S, L2: Location<'a, DropConsistency = Tick<L::DropConsistency>>>(
362        &self,
363        initial: S,
364    ) -> (TickCycleHandle<'a, S>, S)
365    where
366        S: CycleCollectionWithInitial<'a, TickCycle, Location = L2>,
367    {
368        let cycle_id = self.flow_state().borrow_mut().next_cycle_id();
369        (
370            TickCycleHandle::new(cycle_id, Location::id(self)),
371            // no need to defer_tick, create_source_with_initial does it for us
372            S::create_source_with_initial(cycle_id, initial, self.clone().with_consistency_of()),
373        )
374    }
375}
376
377#[cfg(test)]
378mod tests {
379    #[cfg(feature = "sim")]
380    use stageleft::q;
381
382    #[cfg(feature = "sim")]
383    use crate::live_collections::sliced::sliced;
384    #[cfg(feature = "sim")]
385    use crate::location::Location;
386    #[cfg(feature = "sim")]
387    use crate::nondet::nondet;
388    #[cfg(feature = "sim")]
389    use crate::prelude::FlowBuilder;
390
391    #[cfg(feature = "sim")]
392    #[test]
393    fn sim_atomic_stream() {
394        let mut flow = FlowBuilder::new();
395        let node = flow.process::<()>();
396
397        let (write_send, write_req) = node.sim_input();
398        let (read_send, read_req) = node.sim_input::<(), _, _>();
399
400        let atomic_write = write_req.atomic();
401        let current_state = atomic_write.clone().fold(
402            q!(|| 0),
403            q!(|state: &mut i32, v: i32| {
404                *state += v;
405            }),
406        );
407
408        let write_ack_recv = atomic_write.end_atomic().sim_output();
409        let read_response_recv = sliced! {
410            let batch_of_req = use::batch(read_req, nondet!(/** test */));
411            let latest_singleton = use::atomic(current_state, nondet!(/** test */));
412            batch_of_req.cross_singleton(latest_singleton)
413        }
414        .sim_output();
415
416        let sim_compiled = flow.sim().compiled();
417        let instances = sim_compiled.exhaustive(async || {
418            write_send.send(1);
419            write_ack_recv.assert_yields([1]).await;
420            read_send.send(());
421            assert!(read_response_recv.next().await.1 >= 1);
422        });
423
424        assert_eq!(instances, 1);
425
426        let instances_read_before_write = sim_compiled.exhaustive(async || {
427            write_send.send(1);
428            read_send.send(());
429            write_ack_recv.assert_yields([1]).await;
430            let _ = read_response_recv.next().await;
431        });
432
433        assert_eq!(instances_read_before_write, 3); // read before write, write before read, both in same tick
434    }
435
436    #[cfg(feature = "sim")]
437    #[test]
438    #[should_panic]
439    fn sim_non_atomic_stream() {
440        // shows that atomic is necessary
441        let mut flow = FlowBuilder::new();
442        let node = flow.process::<()>();
443
444        let (write_send, write_req) = node.sim_input();
445        let (read_send, read_req) = node.sim_input::<(), _, _>();
446
447        let current_state = write_req.clone().fold(
448            q!(|| 0),
449            q!(|state: &mut i32, v: i32| {
450                *state += v;
451            }),
452        );
453
454        let write_ack_recv = write_req.sim_output();
455
456        let read_response_recv = sliced! {
457            let batch_of_req = use::batch(read_req, nondet!(/** test */));
458            let latest_singleton = use::snapshot(current_state, nondet!(/** test */));
459            batch_of_req.cross_singleton(latest_singleton)
460        }
461        .sim_output();
462
463        flow.sim().exhaustive(async || {
464            write_send.send(1);
465            write_ack_recv.assert_yields([1]).await;
466            read_send.send(());
467
468            let ((), v) = read_response_recv.next().await;
469            assert_eq!(v, 1);
470        });
471    }
472}