harmos-runtime 0.1.2

Harmos runtime: managed state, undo, recovery, background work and typed streams.
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
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

/* === Modules === */

#[path = "../support/mod.rs"]
mod support;

#[path = "../streams/allocation.rs"]
mod allocation;

#[path = "retention.rs"]
mod retention;

#[cfg(any(feature = "plugin", feature = "plugin-guest", feature = "plugin-sidecar"))]
#[path = "../streams/listeners.rs"]
mod listeners;

#[path = "series.rs"]
mod series;

#[path = "stream_batches.rs"]
#[allow(dead_code, reason = "shared with the benchmark of the same domain")]
pub mod stream_batches;

#[path = "streams_macros.rs"]
mod streams_macros;

mod colliding {
    use serde::{Deserialize, Serialize};

    #[harmos_runtime::stream(id = "pulse", window = 1)]
    #[derive(Clone, Copy, Debug, Serialize, Deserialize)]
    pub struct Imposter;

    #[harmos_runtime::streams]
    pub enum Bank {
        Imposter(Imposter),
    }
}

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
// Third Party Imports
use serde::{Deserialize, Serialize};
// Local Imports
use harmos_runtime::{
    Runtime, state::At, state::Position, storage::Batch, storage::Cursor, storage::StreamConsumer, streams::BeneathWindow,
    streams::Definition, streams::Item, streams::Stream, streams::StreamManager,
};
use support::{Bank, Ledger, Pulse, Reading, deposit, pulse, streaming, streams};

/* === Constants === */

/* === Macros === */

/* === Type Aliases === */

/* === Structs === */

struct Selecting;

/* === Enums === */

/* === Traits === */

/* === Implementations === */

impl StreamConsumer<Bank> for Selecting {
    fn resume(&self) -> Cursor {
        Cursor::ORIGIN
    }

    fn selections(&self) -> Vec<Definition> {
        vec![Pulse::definition()]
    }

    async fn drain(&mut self, batch: Batch<Bank>) -> Cursor {
        batch.reaches()
    }
}

/* === Public Functions === */

/* === Private Functions === */

async fn next<S: Clone + 'static>(watching: &mut (impl futures_core::Stream<Item = Item<S>> + Unpin)) -> Option<Item<S>> {
    std::future::poll_fn(|context| Pin::new(&mut *watching).poll_next(context)).await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn rows_gaps_account() {
    let runtime = streaming().await;
    let mut live = runtime.streams.watch::<Pulse>();
    let streams = runtime.streams.clone();

    // A bare-thread publisher races an actively polling watcher. However the
    // two interleave, the invariant is total accounting: every published row
    // is either delivered or inside an exact gap count.
    let published: u64 = 50_000;
    let publisher = std::thread::spawn(move || {
        for sample in 1..=published {
            streams.publish(pulse(sample));
        }
    });

    let mut delivered: u64 = 0;
    let mut missed: u64 = 0;
    let mut last = 0;
    while delivered + missed < published {
        match next(&mut live).await.expect("the watch is live") {
            Item::Row(At { value, .. }) => {
                assert!(value.sample > last, "delivery is in publish order with no repeats: {} then {}", last, value.sample);
                last = value.sample;
                delivered += 1;
            }
            Item::Gap { missed: count } => missed += count,
        }
    }

    publisher.join().expect("the publisher panics nowhere");
    assert_eq!(delivered + missed, published, "no row is lost silently: what was not delivered was counted");
    assert_eq!(last, published, "the newest row is always delivered");

    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn watch_delivery_settles() {
    {
        let runtime = streaming().await;
        // A watcher that makes no progress at all while publishing runs, and a
        // snapshot taken before it: zero consumer progress of any kind.
        let mut stalled = runtime.streams.watch::<Pulse>();
        let held: Vec<At<Pulse>> = runtime.streams.query::<Pulse>(..).await.expect("nothing was evicted yet").collect();
        let streams = runtime.streams.clone();

        // A bare OS thread with no async runtime underneath: a publish that
        // parked, waited on capacity, or required polling could never complete
        // here. Totality under overrun is the whole claim, so no timing is read.
        let published: u64 = 10_000;
        let publisher = std::thread::spawn(move || {
            for sample in 1..=published {
                streams.publish(pulse(sample));
            }
        });
        publisher.join().expect("publish is sync and infallible");

        assert!(held.is_empty(), "the earlier snapshot is unmoved by publishes");

        // The stalled watcher, polled only now, accounts for every row: the exact
        // gap, then the newest window in order.
        let window = Pulse::WINDOW as u64;
        assert_eq!(next(&mut stalled).await, Some(Item::Gap { missed: published - window }));
        for sample in (published - window + 1)..=published {
            assert!(matches!(
                next(&mut stalled).await,
                Some(Item::Row(At { value, .. })) if value == pulse(sample)
            ));
        }

        runtime.stop().await.unwrap();
    }
    {
        let runtime = streaming().await;
        let mut live = runtime.streams.watch::<Pulse>();

        runtime.streams.publish(pulse(1));
        runtime.streams.publish(pulse(2));
        runtime.stop().await.unwrap();

        assert!(matches!(next(&mut live).await, Some(Item::Row(At { value, .. })) if value == pulse(1)));
        assert!(matches!(next(&mut live).await, Some(Item::Row(At { value, .. })) if value == pulse(2)));
        assert_eq!(next(&mut live).await, None, "every row published before the stop is delivered, then the watch ends");
    }
}

#[tokio::test]
async fn history_respects_cuts() {
    {
        let runtime = streaming().await;

        // Advance the journal between publishes so stamps genuinely move.
        for sample in 1..=3u64 {
            runtime.state.commit(deposit(1)).await.expect("accepted");
            runtime.streams.publish(pulse(sample));
        }

        let complete: Vec<u64> = runtime
            .streams
            .query::<Pulse>(..)
            .await
            .expect("a window that never evicted answers any range completely")
            .map(|At { value, .. }| value.sample)
            .collect();
        assert_eq!(complete, vec![1, 2, 3]);

        // Nine more pushes the first rows out of the window of eight.
        for sample in 4..=12u64 {
            runtime.state.commit(deposit(1)).await.expect("accepted");
            runtime.streams.publish(pulse(sample));
        }

        assert_eq!(
            runtime.streams.query::<Pulse>(..).await.expect_err("an unbounded start reaches beneath the window"),
            BeneathWindow::Unserved,
            "with no series adapter attached the window is the whole served history"
        );
        assert_eq!(
            runtime.streams.query::<Pulse>(Position::ORIGIN..).await.expect_err("the origin lies beneath the floor"),
            BeneathWindow::Unserved,
        );

        // A start the caller actually holds — the stamp of a row still retained —
        // is answerable, and completely so.
        let retained: Vec<u64> = runtime
            .streams
            .query::<Pulse>(Position(5)..)
            .await
            .expect("the range starts at a retained stamp above the floor")
            .map(|At { value, .. }| value.sample)
            .collect();
        assert_eq!(retained, vec![5, 6, 7, 8, 9, 10, 11, 12]);

        runtime.stop().await.unwrap();
    }
    {
        let runtime = streaming().await;

        let first = runtime.state.commit(deposit(10)).await.expect("accepted");
        // The journal now sits idle while rows keep flowing: two publishes at
        // different wall-clock moments carry the same unchanged stamp.
        runtime.streams.publish(pulse(1));
        runtime.streams.publish(pulse(2));

        let second = runtime.state.commit(deposit(20)).await.expect("accepted");
        runtime.streams.publish(pulse(3));

        let cut: Vec<u64> = runtime
            .streams
            .query::<Pulse>(..)
            .as_of(first.position)
            .await
            .expect("nothing was evicted")
            .map(|At { value, .. }| value.sample)
            .collect();
        assert_eq!(
            cut,
            vec![1, 2],
            "later wall-clock rows under the unchanged stamp witnessed state no \
             newer than the cutoff, so both are included and the later row is not"
        );

        let all: Vec<u64> = runtime
            .streams
            .query::<Pulse>(..)
            .as_of(second.position)
            .await
            .expect("nothing was evicted")
            .map(|At { value, .. }| value.sample)
            .collect();
        assert_eq!(all, vec![1, 2, 3]);

        runtime.stop().await.unwrap();
    }
}

#[tokio::test]
async fn demand_tracks_consumers() {
    {
        let runtime = streaming().await;
        let demand = runtime.streams.demand::<Pulse>();

        assert_eq!(*demand.borrow(), 0, "an unobserved stream has zero demand");

        let first = runtime.streams.watch::<Pulse>();
        let second = runtime.streams.watch::<Pulse>();
        let other = runtime.streams.watch::<Reading>();

        assert_eq!(*demand.borrow(), 2);
        assert_eq!(*runtime.streams.demand::<Reading>().borrow(), 1, "demand is per stream: the other feed's watcher is not this one's");

        drop(first);
        assert_eq!(*demand.borrow(), 1);
        drop(second);
        drop(other);
        assert_eq!(*demand.borrow(), 0, "demand falls back to zero on drop");

        runtime.stop().await.unwrap();
    }
    {
        let runtime = Runtime::<Bank>::builder(Ledger::default(), ())
            .register(support::catalog())
            .register(streams())
            .attach_streams(Selecting)
            .runtime()
            .await
            .expect("the catalog and attachment are honorable");
        let pulse_demand = runtime.streams.demand::<Pulse>();
        let reading_demand = runtime.streams.demand::<Reading>();

        assert_eq!(*pulse_demand.borrow(), 1, "the attached consumer counts for the stream it selected");
        assert_eq!(*reading_demand.borrow(), 0, "selection is per definition, not per facility");

        runtime.stop().await.unwrap();

        assert_eq!(*pulse_demand.borrow(), 0, "the consumer leaves demand when its supervisor joins");
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn demand_drives_capture() {
    let runtime = streaming().await;
    let capture = Arc::new(AtomicBool::new(false));

    // The capture loop from the spec, verbatim in shape: an application task
    // that turns its source on while anyone is looking and off when nobody is.
    // The acknowledgement channel stands in for the hardware call, so the test
    // observes transitions instead of sleeping.
    let (acknowledged, mut acknowledgements) = tokio::sync::watch::channel(false);
    let mut demand = runtime.streams.demand::<Pulse>();
    let looped = {
        let capture = Arc::clone(&capture);
        tokio::spawn(async move {
            while demand.changed().await.is_ok() {
                if *demand.borrow() > 0 {
                    capture.store(true, Ordering::Release);
                    acknowledged.send_replace(true);
                } else {
                    capture.store(false, Ordering::Release);
                    acknowledged.send_replace(false);
                }
            }
        })
    };

    let watching = runtime.streams.watch::<Pulse>();
    while !*acknowledgements.borrow_and_update() {
        acknowledgements.changed().await.expect("the loop is alive");
    }
    assert!(capture.load(Ordering::Acquire), "a watcher appearing turns capture on");

    drop(watching);
    while *acknowledgements.borrow_and_update() {
        acknowledgements.changed().await.expect("the loop is alive");
    }
    assert!(!capture.load(Ordering::Acquire), "the last watcher leaving turns capture off");

    // The loop ends when the facility is gone entirely — no lifecycle control
    // was ever exercised by the substrate itself. Stopping ends delivery; it is
    // dropping the last `Streams` handle that closes the demand channel, and
    // the runtime holds one for as long as it is alive.
    runtime.stop().await.unwrap();
    drop(runtime);
    looped.await.expect("the capture loop ends without panic");
}

#[tokio::test]
async fn definitions_refuse_invalid() {
    {
        let refused = Runtime::<Bank>::builder(Ledger::default(), ())
            .register(support::catalog())
            .register(streams())
            .register(colliding::Bank::catalog())
            .runtime()
            .await;

        let Err(refused) = refused else {
            panic!("two definitions share an id; the boot cannot honor both");
        };
        assert!(refused.to_string().contains("pulse"), "the refusal names the colliding id: {refused}");
    }
    {
        #[derive(Clone, Copy, Debug, Serialize, Deserialize)]
        struct Hollow;

        impl Stream<Bank> for Hollow {
            const ID: &'static str = "hollow";
            const VERSION: u32 = 1;
            const WINDOW: usize = 0;
        }

        let refused =
            Runtime::<Bank>::builder(Ledger::default(), ()).register(support::catalog()).register([Hollow::definition()]).runtime().await;

        let Err(refused) = refused else {
            panic!("a window of zero rows retains nothing and cannot be honored");
        };
        assert!(refused.to_string().contains("hollow"), "the refusal names the stream it cannot honor: {refused}");
    }
}

#[allow(dead_code)]
fn registration_takes_definitions(catalog: [Definition; 2], streams: &StreamManager<Bank>) {
    let _ = catalog;
    let _ = streams;
}

/* === Tests === */