hydro_lang 0.17.0-alpha.4

A Rust framework for correct and performant distributed systems
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::any::type_name;
use std::cell::RefCell;
use std::marker::PhantomData;
use std::rc::Rc;

use slotmap::{SecondaryMap, SlotMap};

#[cfg(feature = "build")]
use super::compiled::CompiledFlow;
#[cfg(feature = "build")]
use super::deploy::{DeployFlow, DeployResult};
#[cfg(feature = "build")]
use super::deploy_provider::{ClusterSpec, Deploy, ExternalSpec, IntoProcessSpec};
use super::ir::HydroRoot;
use crate::location::{Cluster, External, LocationKey, LocationType, Process};

/// A compile-time directive to spawn a future on a location's `LocalSet`
/// alongside the DFIR scheduler.
pub enum Sidecar {
    /// A ready-to-go future expression (e.g. telemetry metrics collection).
    Simple {
        location_key: LocationKey,
        future_expr: Box<syn::Expr>,
    },
    /// A user-owned sidecar that returns a `(Stream, Sink)` pair to the framework.
    /// The closure is called at startup; the returned stream feeds items into the
    /// dataflow and the returned sink receives items from the dataflow.
    Bidi {
        location_key: LocationKey,
        sidecar_id: SidecarId,
        sidecar_closure: Box<syn::Expr>,
    },
}
#[cfg(feature = "sim")]
#[cfg(stageleft_runtime)]
use crate::sim::flow::SimFlow;
use crate::staging_util::Invariant;

#[stageleft::export(ExternalPortId, CycleId, ClockId, SidecarId, StmtId, HandoffId)]
crate::newtype_counter! {
    /// ID for an external output.
    pub struct ExternalPortId(usize);

    /// ID for a [`crate::location::Location::forward_ref`] cycle.
    pub struct CycleId(usize);

    /// ID for clocks (ticks).
    pub struct ClockId(usize);

    /// ID for user-owned sidecars.
    pub struct SidecarId(usize);

    /// ID for a statement in the emitted DFIR graph.
    pub struct StmtId(usize);

    /// ID for a handoff channel in the simulator.
    pub struct HandoffId(usize);
}

impl CycleId {
    #[cfg(feature = "build")]
    pub(crate) fn as_ident(&self) -> syn::Ident {
        syn::Ident::new(&format!("cycle_{}", self), proc_macro2::Span::call_site())
    }
}

impl SidecarId {
    /// Derives the two idents for a bidi sidecar: `(stream, sink)`.
    pub fn idents(&self) -> (syn::Ident, syn::Ident) {
        let span = proc_macro2::Span::call_site();
        (
            syn::Ident::new(&format!("__hydro_sidecar_{}_stream", self), span),
            syn::Ident::new(&format!("__hydro_sidecar_{}_sink", self), span),
        )
    }
}

pub(crate) type FlowState = Rc<RefCell<FlowStateInner>>;

pub(crate) struct FlowStateInner {
    /// Tracks the roots of the dataflow IR. This is referenced by
    /// `Stream` and `HfCycle` to build the IR. The inner option will
    /// be set to `None` when this builder is finalized.
    roots: Option<Vec<HydroRoot>>,

    /// Counter for generating unique external output identifiers.
    next_external_port: crate::Counter<ExternalPortId>,

    /// Counters for generating identifiers for cycles.
    next_cycle_id: crate::Counter<CycleId>,

    /// Counters for clock IDs.
    next_clock_id: crate::Counter<ClockId>,

    /// Counter for generating unique sidecar identifiers, not used for anything else.
    next_sidecar_id: crate::Counter<SidecarId>,

    /// Compile-time sidecar directives. Processed during compilation,
    /// not part of the dataflow IR.
    pub sidecars: Vec<Sidecar>,
}

impl FlowStateInner {
    pub fn next_external_port(&mut self) -> ExternalPortId {
        self.next_external_port.get_and_increment()
    }

    pub fn next_cycle_id(&mut self) -> CycleId {
        self.next_cycle_id.get_and_increment()
    }

    pub fn next_clock_id(&mut self) -> ClockId {
        self.next_clock_id.get_and_increment()
    }

    pub fn next_sidecar_id(&mut self) -> SidecarId {
        self.next_sidecar_id.get_and_increment()
    }

    pub fn push_root(&mut self, root: HydroRoot) {
        self.roots
            .as_mut()
            .expect("Attempted to add a root to a flow that has already been finalized. No roots can be added after the flow has been compiled.")
            .push(root);
    }

    pub fn try_push_root(&mut self, root: HydroRoot) {
        if let Some(roots) = self.roots.as_mut() {
            roots.push(root);
        }
    }
}

pub struct FlowBuilder<'a> {
    /// Hydro IR and associated counters
    flow_state: FlowState,

    /// Locations and their type.
    locations: SlotMap<LocationKey, LocationType>,
    /// Map from raw location ID to name (including externals).
    location_names: SecondaryMap<LocationKey, String>,
    /// The program version each location belongs to. Every location has an entry (0 unless it is a
    /// `next_version` successor); populated eagerly at location creation.
    #[cfg(feature = "sim")]
    location_version: SecondaryMap<LocationKey, u32>,
    /// Maps each location to the root key of its cross-version correspondence group: version 0 of
    /// the same logical location. Every location has an entry (its own key unless it is a
    /// `next_version` successor); populated eagerly at location creation.
    #[cfg(feature = "sim")]
    location_version_group_root: SecondaryMap<LocationKey, LocationKey>,

    /// Application name used in telemetry.
    #[cfg_attr(
        not(feature = "build"),
        expect(dead_code, reason = "unused without build")
    )]
    flow_name: String,

    /// Tracks whether this flow has been finalized; it is an error to
    /// drop without finalizing.
    finalized: bool,

    /// 'a on a FlowBuilder is used to ensure that staged code does not
    /// capture more data that it is allowed to; 'a is generated at the
    /// entrypoint of the staged code and we keep it invariant here
    /// to enforce the appropriate constraints
    _phantom: Invariant<'a>,
}

impl Drop for FlowBuilder<'_> {
    fn drop(&mut self) {
        if !self.finalized && !std::thread::panicking() {
            panic!(
                "Dropped FlowBuilder without finalizing, you may have forgotten to call `with_default_optimize`, `optimize_with`, or `finalize`."
            );
        }
    }
}

#[expect(missing_docs, reason = "TODO")]
impl<'a> FlowBuilder<'a> {
    /// Creates a new `FlowBuilder` to construct a Hydro program, using the Cargo package name as the program name.
    #[expect(
        clippy::new_without_default,
        reason = "call `new` explicitly, not `default`"
    )]
    pub fn new() -> Self {
        let mut name = std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "unknown".to_owned());
        if let Ok(bin_path) = std::env::current_exe()
            && let Some(bin_name) = bin_path.file_stem()
        {
            name = format!("{}/{}", name, bin_name.display());
        }
        Self::with_name(name)
    }

    /// Creates a new `FlowBuilder` to construct a Hydro program, with the given program name.
    pub fn with_name(name: impl Into<String>) -> Self {
        Self {
            flow_state: Rc::new(RefCell::new(FlowStateInner {
                roots: Some(vec![]),
                next_external_port: crate::Counter::default(),
                next_cycle_id: crate::Counter::default(),
                next_clock_id: crate::Counter::default(),
                next_sidecar_id: crate::Counter::default(),
                sidecars: Vec::new(),
            })),
            locations: SlotMap::with_key(),
            location_names: SecondaryMap::new(),
            #[cfg(feature = "sim")]
            location_version: SecondaryMap::new(),
            #[cfg(feature = "sim")]
            location_version_group_root: SecondaryMap::new(),
            flow_name: name.into(),
            finalized: false,
            _phantom: PhantomData,
        }
    }

    pub(crate) fn flow_state(&self) -> &FlowState {
        &self.flow_state
    }

    fn insert_location(&mut self, ty: LocationType, name: String) -> LocationKey {
        let key = self.locations.insert(ty);
        self.location_names.insert(key, name);
        #[cfg(feature = "sim")]
        {
            self.location_version.insert(key, 0);
            self.location_version_group_root.insert(key, key);
        }
        key
    }

    pub fn process<P>(&mut self) -> Process<'a, P> {
        let key = self.insert_location(LocationType::Process, type_name::<P>().to_owned());
        Process {
            key,
            flow_state: self.flow_state().clone(),
            _phantom: PhantomData,
        }
    }

    pub fn cluster<C>(&mut self) -> Cluster<'a, C> {
        let key = self.insert_location(LocationType::Cluster, type_name::<C>().to_owned());
        Cluster {
            key,
            flow_state: self.flow_state().clone(),
            _phantom: PhantomData,
        }
    }

    pub fn external<E>(&mut self) -> External<'a, E> {
        let key = self.insert_location(LocationType::External, type_name::<E>().to_owned());
        External {
            key,
            flow_state: self.flow_state().clone(),
            _phantom: PhantomData,
        }
    }

    #[cfg(feature = "sim")]
    pub fn next_version<C>(&mut self, cluster: &Cluster<'a, C>) -> Cluster<'a, C> {
        let group_root = self.location_version_group_root[cluster.key];
        let version = self
            .location_version_group_root
            .values()
            .filter(|&&r| r == group_root)
            .count() as u32;
        let key = self.insert_location(LocationType::Cluster, type_name::<C>().to_owned());
        self.location_version.insert(key, version);
        self.location_version_group_root.insert(key, group_root);
        Cluster {
            key,
            flow_state: self.flow_state().clone(),
            _phantom: PhantomData,
        }
    }
}

#[cfg(feature = "build")]
#[cfg_attr(docsrs, doc(cfg(feature = "build")))]
#[expect(missing_docs, reason = "TODO")]
impl<'a> FlowBuilder<'a> {
    pub fn finalize(mut self) -> super::built::BuiltFlow<'a> {
        self.finalized = true;

        let mut flow_state = self.flow_state.borrow_mut();
        let mut ir = flow_state.roots.take().unwrap();
        let sidecars = std::mem::take(&mut flow_state.sidecars);
        drop(flow_state);

        super::ir::unify_atomic_ticks(&mut ir);

        super::built::BuiltFlow {
            ir,
            locations: std::mem::take(&mut self.locations),
            location_names: std::mem::take(&mut self.location_names),
            sidecars,
            flow_name: std::mem::take(&mut self.flow_name),
            #[cfg(feature = "sim")]
            location_version: std::mem::take(&mut self.location_version),
            #[cfg(feature = "sim")]
            location_version_group_root: std::mem::take(&mut self.location_version_group_root),
            _phantom: PhantomData,
        }
    }

    pub fn with_default_optimize<D: Deploy<'a>>(self) -> DeployFlow<'a, D> {
        self.finalize().with_default_optimize()
    }

    pub fn optimize_with(self, f: impl FnOnce(&mut [HydroRoot])) -> super::built::BuiltFlow<'a> {
        self.finalize().optimize_with(f)
    }

    pub fn with_process<P, D: Deploy<'a>>(
        self,
        process: &Process<P>,
        spec: impl IntoProcessSpec<'a, D>,
    ) -> DeployFlow<'a, D> {
        self.with_default_optimize().with_process(process, spec)
    }

    pub fn with_remaining_processes<D: Deploy<'a>, S: IntoProcessSpec<'a, D> + 'a>(
        self,
        spec: impl Fn() -> S,
    ) -> DeployFlow<'a, D> {
        self.with_default_optimize().with_remaining_processes(spec)
    }

    pub fn with_external<P, D: Deploy<'a>>(
        self,
        process: &External<P>,
        spec: impl ExternalSpec<'a, D>,
    ) -> DeployFlow<'a, D> {
        self.with_default_optimize().with_external(process, spec)
    }

    pub fn with_remaining_externals<D: Deploy<'a>, S: ExternalSpec<'a, D> + 'a>(
        self,
        spec: impl Fn() -> S,
    ) -> DeployFlow<'a, D> {
        self.with_default_optimize().with_remaining_externals(spec)
    }

    pub fn with_cluster<C, D: Deploy<'a>>(
        self,
        cluster: &Cluster<C>,
        spec: impl ClusterSpec<'a, D>,
    ) -> DeployFlow<'a, D> {
        self.with_default_optimize().with_cluster(cluster, spec)
    }

    pub fn with_remaining_clusters<D: Deploy<'a>, S: ClusterSpec<'a, D> + 'a>(
        self,
        spec: impl Fn() -> S,
    ) -> DeployFlow<'a, D> {
        self.with_default_optimize().with_remaining_clusters(spec)
    }

    pub fn compile<D: Deploy<'a, InstantiateEnv = ()>>(self) -> CompiledFlow<'a> {
        self.with_default_optimize::<D>().compile()
    }

    pub fn deploy<D: Deploy<'a>>(self, env: &mut D::InstantiateEnv) -> DeployResult<'a, D> {
        self.with_default_optimize().deploy(env)
    }

    #[cfg(feature = "sim")]
    /// Creates a simulation for this builder, which can be used to run deterministic simulations
    /// of the Hydro program.
    pub fn sim(self) -> SimFlow<'a> {
        self.finalize().sim()
    }

    pub fn from_built<'b>(built: &super::built::BuiltFlow) -> FlowBuilder<'b> {
        FlowBuilder {
            flow_state: Rc::new(RefCell::new(FlowStateInner {
                roots: None,
                next_external_port: crate::Counter::default(),
                next_cycle_id: crate::Counter::default(),
                next_clock_id: crate::Counter::default(),
                next_sidecar_id: crate::Counter::default(),
                sidecars: Vec::new(),
            })),
            locations: built.locations.clone(),
            location_names: built.location_names.clone(),
            #[cfg(feature = "sim")]
            location_version: built.location_version.clone(),
            #[cfg(feature = "sim")]
            location_version_group_root: built.location_version_group_root.clone(),
            flow_name: built.flow_name.clone(),
            finalized: false,
            _phantom: PhantomData,
        }
    }

    #[doc(hidden)] // TODO(mingwei): This is an unstable API for now
    pub fn replace_ir(&mut self, roots: Vec<HydroRoot>) {
        self.flow_state.borrow_mut().roots = Some(roots);
    }

    #[doc(hidden)] // TODO(mingwei): This is an unstable API for now
    pub fn next_clock_id(&mut self) -> ClockId {
        self.flow_state.borrow_mut().next_clock_id()
    }

    #[doc(hidden)] // TODO(mingwei): This is an unstable API for now
    pub fn next_cycle_id(&mut self) -> CycleId {
        self.flow_state.borrow_mut().next_cycle_id()
    }
}