Skip to main content

datumesh_engine/
runtime.rs

1//! What a function's language plugs into: [`Runtime`], the [`Inputs`] it reads through and
2//! the [`Budget`] it charges.
3
4use std::collections::BTreeMap;
5use std::fmt;
6use std::sync::{Arc, Mutex, MutexGuard};
7
8use datumesh_core::{Args, Cell, FunctionDescriptor, Scalar};
9
10use crate::error::EvalError;
11
12/// The evaluator of one function language (`code.lang` of a descriptor).
13///
14/// `evaluate` computes the value of `cell` (a cell of the function `descriptor` describes;
15/// `cell.fn_hash` is the descriptor's hash) from the cell's arguments and from the values of
16/// other cells, which it reads through `inputs`. It must be a pure function of those (and of
17/// [`Inputs::asof`] for an asof-dependent function): no clock, no randomness, no I/O. It
18/// charges its work to `budget` and propagates every error `inputs` and `budget` return (see
19/// [`EvalError`]); it may catch an [`EvalError::Fault`] its language lets it catch.
20///
21/// It may be called more than once for one cell in one request: when it reads an input that
22/// is not available yet, the read returns [`EvalError::Suspended`], the engine evaluates the
23/// input, then calls `evaluate` again from the start (reads already made are then answered at
24/// once). So `evaluate` must not have side effects either. Each call is an attempt with a
25/// budget meter of its own: the fuel of a cell's evaluation is that of its last, completed
26/// call ([`Budget`]).
27pub trait Runtime: Send + Sync {
28    /// The language this runtime evaluates: a name (SPEC 2.2), e.g. `formula`. `stored` is
29    /// the engine's own (a lookup) and cannot be configured.
30    fn lang(&self) -> &str;
31
32    /// The value of `cell`, whose type must be the descriptor's declared return type; or
33    /// [`EvalError::Fault`]: the cell has no value (SPEC 15.12).
34    fn evaluate(
35        &self,
36        descriptor: &FunctionDescriptor,
37        cell: &Cell,
38        inputs: &mut dyn Inputs,
39        budget: &mut Budget,
40    ) -> Result<Scalar, EvalError>;
41
42    /// Whether the function is **asof-dependent** (SPEC 15.12: its body uses `asof()` or a
43    /// bounded `latest`), derived from the descriptor alone. Its receipts then have the
44    /// evaluation asof as their `asof`, one per requested asof, and the engine never reuses
45    /// one for another asof, from the memo or from the store. Provided: `false`.
46    ///
47    /// A function that reads [`Inputs::asof`] is treated as asof-dependent for that
48    /// evaluation even when this says `false`; its receipt is then not kept as a computed
49    /// version (a later request could not tell it apart), so declaring it is cheaper.
50    fn asof_dependent(&self, descriptor: &FunctionDescriptor) -> bool {
51        let _ = descriptor;
52        false
53    }
54}
55
56/// What a read gave: the value and the receipt stating it.
57#[non_exhaustive]
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub struct Input {
60    /// The value.
61    pub value: Scalar,
62    /// The hash of the receipt stating it (SPEC 8.3).
63    pub receipt_hash: String,
64    /// The receipt's `tx`.
65    pub tx: String,
66    /// The receipt's `asof`.
67    pub asof: String,
68}
69
70impl Input {
71    /// An input (for a runtime's own tests).
72    pub fn new(
73        value: Scalar,
74        receipt_hash: impl Into<String>,
75        tx: impl Into<String>,
76        asof: impl Into<String>,
77    ) -> Input {
78        Input { value, receipt_hash: receipt_hash.into(), tx: tx.into(), asof: asof.into() }
79    }
80}
81
82/// A member of a slice ([`Inputs::read_slice`]): the member cell's arguments and its input.
83#[non_exhaustive]
84#[derive(Clone, Debug, PartialEq, Eq)]
85pub struct Member {
86    /// Every argument of the member cell.
87    pub args: Args,
88    /// Its value and receipt.
89    pub input: Input,
90}
91
92impl Member {
93    /// A member (for a runtime's own tests).
94    pub fn new(args: Args, input: Input) -> Member {
95        Member { args, input }
96    }
97}
98
99/// One argument of a slice pattern ([`Inputs::read_slice`]).
100#[non_exhaustive]
101#[derive(Clone, Debug, PartialEq, Eq)]
102pub enum Pattern {
103    /// Bound to a value.
104    Value(Scalar),
105    /// Open (`*`): any value.
106    Open,
107    /// Within `[lo..hi]`, both ends inclusive, in the type's order (SPEC 15.13); `lo` after
108    /// `hi` is empty. Both ends are of the argument's type: `number`, `date`, `instant` or
109    /// `string`.
110    Range {
111        /// The low end.
112        lo: Scalar,
113        /// The high end.
114        hi: Scalar,
115    },
116}
117
118/// A slice pattern: every argument of the function, by name.
119pub type SlicePattern = BTreeMap<String, Pattern>;
120
121/// A function name's binding ([`Inputs::binding`]).
122#[non_exhaustive]
123#[derive(Clone, Debug)]
124pub struct FnBinding {
125    /// The function hash the name means.
126    pub fn_hash: String,
127    /// When the binding began (an instant).
128    pub start: String,
129    /// The function's descriptor.
130    pub descriptor: Arc<FunctionDescriptor>,
131}
132
133impl FnBinding {
134    /// A binding (for a runtime's own tests, without a store). Since 0.3.1.
135    pub fn new(fn_hash: impl Into<String>, start: impl Into<String>, descriptor: Arc<FunctionDescriptor>) -> FnBinding {
136        FnBinding { fn_hash: fn_hash.into(), start: start.into(), descriptor }
137    }
138}
139
140/// How a runtime reads the cells its function depends on, and the evaluation's asof.
141///
142/// Every read checks that the function read is in the descriptor's `inputs`
143/// ([`EvalError::UndeclaredInput`]) and resolves its name ([`EvalError::NameUnresolved`]),
144/// and is recorded: it becomes one of the computed receipt's `inputs` entries, and the name's
145/// binding is relied on (its start is a candidate for the receipt's `asof`, SPEC 8.2) as soon
146/// as the read is attempted. The fuel each costs is SPEC 15.12's.
147///
148/// Errors a runtime must propagate (with `?`): [`EvalError::Suspended`] (an input not
149/// evaluated yet: the engine evaluates it and calls the runtime again),
150/// [`EvalError::InputNotCovered`] (an input with no value: neither has the cell, whatever the
151/// runtime returns next), [`EvalError::BudgetExceeded`] and the engine's failures. A
152/// [`EvalError::Fault`] it may catch where its language allows (SPEC 15.12: `otherwise`
153/// catches `E_FORMULA_EMPTY` and `E_FORMULA_ABSENT`).
154pub trait Inputs {
155    /// The value of the cell of function `fn_name` (fully qualified, `<namespace>/<name>`)
156    /// with arguments `args`, as of the evaluation's asof: [`read_cell`](Inputs::read_cell)'s
157    /// value. Costs 1.
158    fn read(&mut self, fn_name: &str, args: &Args) -> Result<Scalar, EvalError>;
159
160    /// The cell of `fn_name` with `args` as of the evaluation's asof: its value and the
161    /// receipt that answers (hash, `tx`, `asof`). A computed cell is evaluated by the engine;
162    /// its fault, if it has one, is returned ([`EvalError::Fault`]). Costs 1.
163    fn read_cell(&mut self, fn_name: &str, args: &Args) -> Result<Input, EvalError>;
164
165    /// The cell of `fn_name` with `args` as of the transaction time `t` (SPEC 15.13,
166    /// `asof(cell, T)`): the name resolved as of `t`, the version answering as of `t`; an
167    /// ordinary single-cell input, and `t` a candidate for the receipt's `asof`. `t` after the
168    /// evaluation's asof is the fault `E_FORMULA_FUTURE`, before anything is read. Costs 1.
169    fn read_asof(&mut self, fn_name: &str, args: &Args, t: &str) -> Result<Input, EvalError>;
170
171    /// The members of the slice of `fn_name` with `pattern` (every declared argument; at least
172    /// one open or ranged) as of the evaluation's asof, in SPEC 15.13 key order: for a stored
173    /// function, its cells matching the pattern with a version answering then; for any other,
174    /// the cells of the declared `extent` (or, for a range over a `date` or integer `number`
175    /// argument without one, every day or integer in it) within the pattern, each evaluated
176    /// by the engine, and every one must have a value: else the read is the fault
177    /// `E_FORMULA_ABSENT`. One `inputs` entry `slice:<slice id>:<members hash>`, whose
178    /// manifest the engine stores. Costs 1 + members, charged once the members are obtained.
179    fn read_slice(&mut self, fn_name: &str, pattern: &SlicePattern) -> Result<Vec<Member>, EvalError>;
180
181    /// The versions of the stored cell of `fn_name` with `args` whose `tx` is in `[from, to]`,
182    /// as known as of the evaluation's asof, ascending by `tx` (SPEC 15.13 `versions`): a
183    /// version slice, one `inputs` entry, `to` a candidate for the receipt's `asof`. `to` after
184    /// the evaluation's asof is `E_FORMULA_FUTURE`. Costs 1 + versions.
185    fn read_versions(&mut self, fn_name: &str, args: &Args, from: &str, to: &str) -> Result<Vec<Input>, EvalError>;
186
187    /// The binding of `fn_name` as of the evaluation's asof, or as of `at`: its function hash,
188    /// when it began and the descriptor; `None` when it has none then. Relied on as a read's
189    /// name is. Free.
190    fn binding(&mut self, fn_name: &str, at: Option<&str>) -> Result<Option<FnBinding>, EvalError>;
191
192    /// The evaluation's asof (SPEC 15.12 `asof()`). Reading it makes the evaluation
193    /// asof-dependent (see [`Runtime::asof_dependent`]). Free.
194    fn asof(&mut self) -> String;
195
196    /// The binding of `fn_name` as of the evaluation's asof, or as of `at`, looked up without
197    /// being relied on: no asof candidate, no dependency of the answer on the name. For a
198    /// runtime that needs to know what a name means (to check a body against it, say) before,
199    /// or without, reading it; a read of the name still relies on it. Free. Since 0.3.1.
200    ///
201    /// Provided: [`binding`](Inputs::binding), which relies on it (the engine's inputs
202    /// override it).
203    fn binding_peek(&mut self, fn_name: &str, at: Option<&str>) -> Result<Option<FnBinding>, EvalError> {
204        self.binding(fn_name, at)
205    }
206
207    /// How the instant `t` compares with the evaluation's asof (`t.cmp(asof)`), without
208    /// making the evaluation asof-dependent when the answer cannot change at a later asof.
209    /// Free. Since 0.3.1.
210    ///
211    /// What the engine records: when `t` is at or before the asof (`Less` or `Equal`), `t` is
212    /// a candidate for the receipt's `asof` (as `T` of a transaction-time read is), so every
213    /// asof the receipt answers or is validated at is at or after `t` and the comparison gives
214    /// the same there; the receipt recomputes at its own asof. When `t` is after it
215    /// (`Greater`), the answer would change once the asof reaches `t`: the evaluation is then
216    /// asof-dependent, as by [`asof`](Inputs::asof) (and a runtime may report
217    /// `E_FORMULA_FUTURE` for it, even after an input without a value). A `t` that is not an
218    /// instant is [`EvalError::InvalidInput`].
219    ///
220    /// Provided: by [`asof`](Inputs::asof), which always makes the evaluation asof-dependent
221    /// (the engine's inputs override it).
222    fn asof_compare(&mut self, t: &str) -> Result<std::cmp::Ordering, EvalError> {
223        datumesh_core::lexical::check_instant(t, "$.t").map_err(EvalError::InvalidInput)?;
224        Ok(t.cmp(self.asof().as_str()))
225    }
226}
227
228/// The fuel of an evaluation attempt, on the request's meter.
229///
230/// Fuel is normative (SPEC 15.12): the fuel of a cell's evaluation is what its last,
231/// completed attempt charges ([`used`](Budget::used)), whatever number of times the engine
232/// restarted it. A request's normative fuel is the sum of the evaluations it completes, and
233/// its limit ([`limit`](Budget::limit)) is checked at every charge against a lower bound of
234/// that sum (each suspended evaluation will charge at least what its last attempt did), so a
235/// request fails with `BudgetExceeded` exactly when its normative fuel exceeds the limit.
236/// Separately the request's **work** (every attempt, those discarded by a restart included,
237/// and the engine's validation reads) has a limit of its own, which bounds the total cost.
238/// When either runs out, the request fails and stores nothing.
239pub struct Budget {
240    meter: Arc<Mutex<Meter>>,
241}
242
243#[derive(Debug)]
244pub(crate) struct Meter {
245    limit: u64,
246    work_limit: u64,
247    /// The normative fuel of the evaluations completed.
248    committed: u64,
249    /// The lower bounds of the suspended evaluations on the stack.
250    pending: u64,
251    /// The running attempt: its charges and its lower bound (its earlier attempt's charges).
252    attempt: u64,
253    floor: u64,
254    work: u64,
255    pub(crate) exhausted: Exhausted,
256}
257
258/// What ran out.
259#[derive(Clone, Copy, Debug, PartialEq, Eq)]
260pub(crate) enum Exhausted {
261    No,
262    Fuel,
263    Work,
264}
265
266impl Meter {
267    fn bound(&self) -> u64 {
268        self.committed.saturating_add(self.pending).saturating_add(self.attempt.max(self.floor))
269    }
270}
271
272impl Budget {
273    /// A budget of `fuel` units, with no work limit (for a runtime's own tests; the engine
274    /// makes the budget of a request from its configuration).
275    pub fn new(fuel: u64) -> Budget {
276        Budget::with_work(fuel, u64::MAX)
277    }
278
279    pub(crate) fn with_work(fuel: u64, work_limit: u64) -> Budget {
280        Budget {
281            meter: Arc::new(Mutex::new(Meter {
282                limit: fuel,
283                work_limit,
284                committed: 0,
285                pending: 0,
286                attempt: 0,
287                floor: 0,
288                work: 0,
289                exhausted: Exhausted::No,
290            })),
291        }
292    }
293
294    fn lock(&self) -> MutexGuard<'_, Meter> {
295        self.meter.lock().unwrap_or_else(|p| p.into_inner())
296    }
297
298    /// Charges `steps` units of work to this attempt. [`EvalError::BudgetExceeded`] when the
299    /// request's fuel (or its work) runs out: the budget is then exhausted for the rest of the
300    /// request.
301    pub fn charge(&mut self, steps: u64) -> Result<(), EvalError> {
302        self.charge_shared(steps)
303    }
304
305    /// The fuel this attempt has charged so far: at the end of a completed attempt, the
306    /// normative fuel of the evaluation.
307    pub fn used(&self) -> u64 {
308        self.lock().attempt
309    }
310
311    /// The fuel left: the limit less the lower bound of the request's normative fuel.
312    pub fn remaining(&self) -> u64 {
313        let m = self.lock();
314        if m.exhausted != Exhausted::No {
315            return 0;
316        }
317        m.limit.saturating_sub(m.bound())
318    }
319
320    /// The request's fuel limit.
321    pub fn limit(&self) -> u64 {
322        self.lock().limit
323    }
324
325    /// Whether a charge has failed.
326    pub fn is_exhausted(&self) -> bool {
327        self.lock().exhausted != Exhausted::No
328    }
329
330    pub(crate) fn exhausted(&self) -> Exhausted {
331        self.lock().exhausted
332    }
333
334    pub(crate) fn work_limit(&self) -> u64 {
335        self.lock().work_limit
336    }
337
338    /// Another handle on the same meter.
339    pub(crate) fn share(&self) -> Budget {
340        Budget { meter: Arc::clone(&self.meter) }
341    }
342
343    /// A normative charge of the running attempt.
344    pub(crate) fn charge_shared(&self, steps: u64) -> Result<(), EvalError> {
345        let mut m = self.lock();
346        if m.exhausted != Exhausted::No {
347            return Err(EvalError::BudgetExceeded { fuel: m.limit });
348        }
349        // The normative limit first: a charge exceeding both is BudgetExceeded.
350        m.work = m.work.saturating_add(steps);
351        m.attempt = m.attempt.saturating_add(steps);
352        if m.bound() > m.limit {
353            m.exhausted = Exhausted::Fuel;
354            return Err(EvalError::BudgetExceeded { fuel: m.limit });
355        }
356        if m.work > m.work_limit {
357            m.exhausted = Exhausted::Work;
358            return Err(EvalError::BudgetExceeded { fuel: m.limit });
359        }
360        Ok(())
361    }
362
363    /// Work that is not normative (validation reads, rechecks).
364    pub(crate) fn work(&self, steps: u64) -> Result<(), EvalError> {
365        let mut m = self.lock();
366        if m.exhausted != Exhausted::No {
367            return Err(EvalError::BudgetExceeded { fuel: m.limit });
368        }
369        m.work = m.work.saturating_add(steps);
370        if m.work > m.work_limit {
371            m.exhausted = Exhausted::Work;
372            return Err(EvalError::BudgetExceeded { fuel: m.limit });
373        }
374        Ok(())
375    }
376
377    /// Starts an attempt of a frame whose earlier attempts charged at most `floor` (0 for a
378    /// first attempt); the frame's floor is no longer pending.
379    pub(crate) fn begin_attempt(&self, floor: u64) {
380        let mut m = self.lock();
381        m.pending = m.pending.saturating_sub(floor);
382        m.attempt = 0;
383        m.floor = floor;
384    }
385
386    /// The running attempt suspended: returns the frame's new floor, now pending.
387    pub(crate) fn suspend_attempt(&self) -> u64 {
388        let mut m = self.lock();
389        let floor = m.attempt.max(m.floor);
390        m.pending = m.pending.saturating_add(floor);
391        m.attempt = 0;
392        m.floor = 0;
393        floor
394    }
395
396    /// The running attempt completed: its fuel is the evaluation's.
397    pub(crate) fn finish_attempt(&self) -> u64 {
398        let mut m = self.lock();
399        let used = m.attempt;
400        m.committed = m.committed.saturating_add(used);
401        m.attempt = 0;
402        m.floor = 0;
403        used
404    }
405}
406
407impl fmt::Debug for Budget {
408    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
409        let m = self.lock();
410        f.debug_struct("Budget")
411            .field("limit", &m.limit)
412            .field("used", &m.attempt)
413            .field("committed", &m.committed)
414            .field("exhausted", &(m.exhausted != Exhausted::No))
415            .finish()
416    }
417}