harmos-runtime 0.1.6

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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

/* === Modules === */

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

/* === Imports === */

// Standard Library Imports
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
// Third Party Imports
use tokio::sync::Notify;
// Local Imports
use super::{
    Lanes, assembly, service_status,
    support::{Bank, Noted},
};
use harmos_runtime::operations::{Context, Operation, OperationPolicy, RestartPolicy, RetryPolicy};
use harmos_runtime::{Error, Refusal, services::Fault, services::ServiceExit, services::ServiceStatus, state::Position};

/* === Constants === */

/* === Macros === */

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

/* === Structs === */

struct Sequence {
    events: Arc<Mutex<Vec<&'static str>>>,
    started: Arc<Notify>,
}

struct Ordered {
    running: bool,
    sequence: Arc<Sequence>,
}

struct Dropped(Arc<AtomicBool>);

#[derive(Default)]
struct Cleanup {
    attempts: AtomicUsize,
    finalized: AtomicUsize,
    stop_future_dropped: AtomicBool,
    dropped: AtomicBool,
    failed: AtomicBool,
    running: Notify,
    finalizing: Notify,
    release: Notify,
}

struct Owned(Arc<Cleanup>);
struct PhysicalStop(Arc<Cleanup>);
struct StopWait(Arc<Cleanup>);

/* === Enums === */

#[harmos_runtime::message]
#[harmos_runtime::operation(
        response = (),
        id = "shutdown.ordered",
        version = 1,
        policy = OperationPolicy {
            timeout: Duration::from_secs(2),
            attempt_timeout: Duration::from_secs(1),
            retries: RetryPolicy::Never,
            restart: RestartPolicy::Never,
        },
    )]
struct OrderedContract {
    running: bool,
}

#[harmos_runtime::catalog]
enum Operations {
    OrderedContract(OrderedContract),
}

/* === Traits === */

/* === Implementations === */

impl Operation for Ordered {
    type Declaration = OrderedContract;
    type Resources = Sequence;
    type Error = std::convert::Infallible;
    type Endpoints = ();
    type Context<'a> = Context<'a>;

    async fn initialize<'ctx>(sequence: Arc<Sequence>, _: Self::Context<'ctx>) -> Result<Self, Self::Error> {
        Ok(Self { running: false, sequence })
    }

    async fn run<'ctx>(
        &mut self, _context: Self::Context<'ctx>, request: &mut OrderedContract, _endpoints: &mut Self::Endpoints,
    ) -> Result<harmos_runtime::operations::Response<()>, Self::Error> {
        let result: ::core::result::Result<(), Self::Error> = async move {
            self.running = request.running;
            if self.running {
                self.sequence.events.lock().unwrap().push("job-start");
                self.sequence.started.notify_one();
                std::future::pending::<()>().await;
            } else {
                self.sequence.events.lock().unwrap().push("queued");
            }
            Ok(())
        }
        .await;
        result.map(::core::convert::Into::into)
    }

    async fn cleanup(&mut self) -> Result<(), Self::Error> {
        if self.running {
            self.sequence.events.lock().unwrap().push("job-end");
        }
        Ok(())
    }
}

impl harmos_runtime::services::Resident<super::Bank> for Owned {
    type Error = std::io::Error;

    async fn run(&mut self, mut context: harmos_runtime::services::Service<super::Bank>) -> Result<(), Fault<Self::Error>> {
        context.ready();
        self.0.running.notify_one();
        context.stopping().await;
        Ok(())
    }

    async fn cleanup(&mut self) -> Result<(), Self::Error> {
        self.0.attempts.fetch_add(1, Ordering::SeqCst);
        if self.0.failed.load(Ordering::SeqCst) {
            return Err(std::io::Error::other("physical cleanup failed"));
        }
        self.0.finalizing.notify_one();
        self.0.release.notified().await;
        Ok(())
    }
}

impl harmos_runtime::services::Resident<Bank> for PhysicalStop {
    type Error = std::io::Error;

    async fn run(&mut self, mut context: harmos_runtime::services::Service<Bank>) -> Result<(), Fault<Self::Error>> {
        context.ready();
        self.0.running.notify_one();
        context.stopping().await;
        Ok(())
    }

    async fn stop(&mut self) -> Result<(), Self::Error> {
        let _original = StopWait(self.0.clone());
        self.0.attempts.fetch_add(1, Ordering::SeqCst);
        self.0.finalizing.notify_one();
        self.0.release.notified().await;
        Ok(())
    }

    async fn cleanup(&mut self) -> Result<(), Self::Error> {
        self.0.finalized.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}

impl Drop for PhysicalStop {
    fn drop(&mut self) {
        self.0.dropped.store(true, Ordering::SeqCst);
    }
}
impl Drop for StopWait {
    fn drop(&mut self) {
        self.0.stop_future_dropped.store(true, Ordering::SeqCst);
    }
}

impl Drop for Owned {
    fn drop(&mut self) {
        self.0.dropped.store(true, Ordering::SeqCst);
    }
}

impl Drop for Dropped {
    fn drop(&mut self) {
        self.0.store(true, Ordering::SeqCst);
    }
}

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

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

#[tokio::test]
async fn service_stop_observer() {
    struct Blocked;
    impl harmos_runtime::observers::ObserverBackend for Blocked {
        type Resources = Cleanup;
        fn definition(&self) -> harmos_runtime::Result<harmos_runtime::observers::ObserverDefinition> {
            Ok(harmos_runtime::observers::ObserverDefinition {
                id: "blocked-observer".into(),
                interval: Duration::from_secs(10),
                streams: vec![],
                demand_keys: vec![],
            })
        }
        async fn run(
            &self, gates: Arc<Cleanup>, mut activation: harmos_runtime::observers::ObserverActivation,
        ) -> harmos_runtime::Result<()> {
            activation.ready();
            gates.running.notify_one();
            activation.stopping().await;
            gates.finalizing.notify_one();
            gates.release.notified().await;
            Ok(())
        }
    }
    let service = Arc::new(Cleanup::default());
    let observer = Arc::new(Cleanup::default());
    let runtime = assembly().service("independent", PhysicalStop(service.clone())).runtime().await.unwrap();
    service.running.notified().await;
    runtime.operations.register_observer(Blocked).unwrap();
    let _lease = runtime.operations.submit_with_resources_at("blocked-observer", observer.clone()).await.unwrap();
    observer.running.notified().await;
    {
        let stop = runtime.stop();
        tokio::pin!(stop);
        (async {
            tokio::select! {
                result = &mut stop => panic!("cleanup remains held: {result:?}"),
                _ = async {
                    observer.finalizing.notified().await;
                    service.finalizing.notified().await;
                } => {}
            }
        })
        .await;
    }
    assert_eq!(service.attempts.load(Ordering::SeqCst), 1);
    assert_eq!(service.finalized.load(Ordering::SeqCst), 0);
    observer.release.notify_one();
    service.release.notify_one();
    runtime.stop().await.unwrap();
    assert_eq!(service.finalized.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn interruption_retains_stop() {
    let cleanup = Arc::new(Cleanup::default());
    let runtime = assembly().service("physical-stop", PhysicalStop(cleanup.clone())).runtime().await.unwrap();
    cleanup.running.notified().await;
    let mut participant = runtime.stop_participant();
    {
        let stopping = runtime.stop();
        tokio::pin!(stopping);
        (async {
            tokio::select! {
                result = &mut stopping => panic!("physical stop is gated: {result:?}"),
                _ = cleanup.finalizing.notified() => {},
            }
        })
        .await;
    }
    assert!(!cleanup.stop_future_dropped.load(Ordering::SeqCst));
    assert!(!cleanup.dropped.load(Ordering::SeqCst));
    assert_eq!(cleanup.finalized.load(Ordering::SeqCst), 0);
    let statuses = runtime.operations.residents();
    let state = statuses.iter().find(|(key, _)| key.definition == "physical-stop").unwrap().1;
    assert_eq!(state.lifecycle.phase(), harmos_runtime::operations::OperationPhase::Stopping);
    assert!(state.lifecycle.settled_at().is_none());

    cleanup.release.notify_one();
    (participant.stopping()).await;
    participant.finish();
    (runtime.stop()).await.unwrap();
    assert_eq!(cleanup.attempts.load(Ordering::SeqCst), 1);
    assert_eq!(cleanup.finalized.load(Ordering::SeqCst), 1);
    assert!(cleanup.stop_future_dropped.load(Ordering::SeqCst));
    assert!(cleanup.dropped.load(Ordering::SeqCst));
}

#[tokio::test]
async fn cleanup_releases_resident() {
    let cleanup = Arc::new(Cleanup::default());
    let runtime = assembly().service("owned", Owned(cleanup.clone())).runtime().await.unwrap();
    cleanup.running.notified().await;
    {
        let stopping = runtime.stop();
        tokio::pin!(stopping);
        (async {
            tokio::select! {
                result = &mut stopping => panic!("cleanup is gated: {result:?}"),
                _ = cleanup.finalizing.notified() => {},
            }
        })
        .await;
        assert!(!cleanup.dropped.load(Ordering::SeqCst));
        cleanup.release.notify_one();
        (&mut stopping).await.unwrap();
    }
    assert!(cleanup.dropped.load(Ordering::SeqCst));
    runtime.stop().await.unwrap();
    assert_eq!(cleanup.attempts.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn cleanup_retry_survives() {
    let cleanup = Arc::new(Cleanup::default());
    cleanup.failed.store(true, Ordering::SeqCst);
    let runtime = assembly().service("owned", Owned(Arc::clone(&cleanup))).runtime().await.unwrap();
    cleanup.running.notified().await;
    let mut participant = runtime.stop_participant();
    let failure = runtime.stop().await.unwrap_err();
    let failure = std::iter::successors(Some(&failure as &(dyn std::error::Error + 'static)), |error| error.source())
        .map(ToString::to_string)
        .collect::<Vec<_>>()
        .join(": ");
    assert!(failure.contains("physical cleanup failed"), "{failure}");
    assert!(!cleanup.dropped.load(Ordering::SeqCst));
    assert_eq!(cleanup.attempts.load(Ordering::SeqCst), 1);

    cleanup.failed.store(false, Ordering::SeqCst);
    {
        let waiting = runtime.stop();
        tokio::pin!(waiting);
        (async {
            tokio::select! {
                result = &mut waiting => panic!("physical cleanup is gated: {result:?}"),
                _ = cleanup.finalizing.notified() => {},
            }
        })
        .await;
    }
    assert!(!cleanup.dropped.load(Ordering::SeqCst));
    cleanup.release.notify_one();
    (participant.stopping()).await;
    participant.finish();
    (runtime.stop()).await.unwrap();
    assert_eq!(cleanup.attempts.load(Ordering::SeqCst), 2);
    assert!(cleanup.dropped.load(Ordering::SeqCst));
}

#[tokio::test(start_paused = true)]
async fn uncooperative_resident_aborted() {
    let entered = Arc::new(Notify::new());
    let dropped = Arc::new(AtomicBool::new(false));
    let runtime = assembly()
        .service_with(
            "stalled",
            harmos_runtime::operations::OperationPolicy { timeout: Duration::from_millis(10), ..harmos_runtime::services::POLICY },
            {
                let entered = entered.clone();
                let dropped = dropped.clone();
                move |_context| {
                    let entered = entered.clone();
                    let dropped = dropped.clone();
                    async move {
                        let _guard = Dropped(dropped);
                        entered.notify_one();
                        std::future::pending::<Result<(), Fault<&str>>>().await
                    }
                }
            },
        )
        .runtime()
        .await
        .unwrap();
    entered.notified().await;
    let statuses = runtime.operations.watch_residents();

    assert!(matches!((runtime.stop()).await, Err(harmos_runtime::Error::TaskFailed { domain: "service", .. })));

    assert!(dropped.load(Ordering::SeqCst), "shutdown joined cancellation before returning");
    assert!(matches!(
        statuses.borrow().get(&harmos_runtime::operations::resident::Key::root("stalled")).and_then(|item| item.health.as_ref()),
        Some(ServiceStatus::Inactive { cause: ServiceExit::StopTimedOut, .. })
    ));
}

#[tokio::test(start_paused = true)]
async fn cleanup_finish_within() {
    let entered = Arc::new(Notify::new());
    let cleaned = Arc::new(AtomicBool::new(false));
    let runtime = assembly()
        .service_with(
            "cleanup",
            harmos_runtime::operations::OperationPolicy { timeout: Duration::from_millis(50), ..harmos_runtime::services::POLICY },
            {
                let entered = entered.clone();
                let cleaned = cleaned.clone();
                move |mut context: harmos_runtime::services::Service<super::Bank>| {
                    let entered = entered.clone();
                    let cleaned = cleaned.clone();
                    async move {
                        entered.notify_one();
                        context.stopping().await;
                        tokio::time::sleep(Duration::from_millis(20)).await;
                        cleaned.store(true, Ordering::SeqCst);
                        Ok::<(), Fault<&str>>(())
                    }
                }
            },
        )
        .runtime()
        .await
        .unwrap();
    entered.notified().await;
    let statuses = runtime.operations.watch_residents();

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

    assert!(cleaned.load(Ordering::SeqCst));
    assert!(matches!(
        statuses.borrow().get(&harmos_runtime::operations::resident::Key::root("cleanup")).and_then(|item| item.health.as_ref()),
        Some(ServiceStatus::Stopped)
    ));
}

#[tokio::test(start_paused = true)]
async fn unregister_retains_ownership() {
    let entered = Arc::new(Notify::new());
    let stopping = Arc::new(Notify::new());
    let dropped = Arc::new(AtomicBool::new(false));
    let runtime = Arc::new(assembly().runtime().await.unwrap());
    runtime
        .operations
        .register_service(
            harmos_runtime::services::ServiceDefinition {
                id: "tenant".into(),
                policy: harmos_runtime::operations::OperationPolicy {
                    timeout: Duration::from_millis(10),
                    ..harmos_runtime::services::POLICY
                },
            },
            {
                let entered = entered.clone();
                let stopping = stopping.clone();
                let dropped = dropped.clone();
                move |_: Arc<()>| {
                    let entered = entered.clone();
                    let stopping = stopping.clone();
                    let dropped = dropped.clone();
                    move |mut context: harmos_runtime::services::Service<super::Bank>| {
                        let entered = entered.clone();
                        let stopping = stopping.clone();
                        let dropped = dropped.clone();
                        async move {
                            let _guard = Dropped(dropped);
                            entered.notify_one();
                            context.stopping().await;
                            stopping.notify_one();
                            std::future::pending::<Result<(), Fault<&str>>>().await
                        }
                    }
                }
            },
        )
        .unwrap();
    let _lease = runtime.operations.submit_resident("tenant").await.unwrap();
    entered.notified().await;
    let waiter = tokio::spawn({
        let runtime = runtime.clone();
        async move {
            runtime.operations.unregister_resident("tenant").await.unwrap_err();
        }
    });
    stopping.notified().await;
    waiter.abort();
    assert!(waiter.await.unwrap_err().is_cancelled());

    (runtime.operations.unregister_resident("tenant")).await.unwrap_err();

    assert!(dropped.load(Ordering::SeqCst));
    assert!(!runtime.operations.watch_residents().borrow().contains_key(&harmos_runtime::operations::resident::Key::root("tenant")));
    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn domains_outlives_clones() {
    let events = Arc::new(Mutex::new(Vec::new()));
    let service_events = Arc::clone(&events);
    let started = Arc::new(Notify::new());
    let sequence = Arc::new(Sequence { events: events.clone(), started: started.clone() });
    let runtime = assembly()
        .operation::<Ordered>("app", Lanes::Serial, sequence)
        .service("resident", move |mut context: harmos_runtime::services::Service<Bank>| {
            let events = Arc::clone(&service_events);
            async move {
                context.ready();
                assert!(context.rest(Duration::from_secs(60)).await);
                events.lock().expect("event lock is healthy").push("service");
                Ok::<(), Fault<&'static str>>(())
            }
        })
        .runtime()
        .await
        .expect("the work declarations are valid");
    service_status(runtime.operations.watch_residents(), "resident", |status| matches!(status, ServiceStatus::Running { .. })).await;

    let running = runtime.operations.submit(OrderedContract { running: true }).unwrap();
    started.notified().await;
    let queued = runtime.operations.submit(OrderedContract { running: false }).unwrap();

    let mut participant = runtime.stop_participant();
    let participant_events = Arc::clone(&events);
    let host = tokio::spawn(async move {
        participant.stopping().await;
        participant_events.lock().expect("event lock is healthy").push("participant");
        participant.finish();
    });
    runtime.stop().await.unwrap();
    host.await.expect("the host participant does not panic");
    assert!(matches!(running.await, Err(Error::OperationRefused(Refusal::Cancelled))));
    assert!(matches!(queued.await, Err(Error::OperationRefused(Refusal::Cancelled))));
    let trace = events.lock().expect("event lock is healthy").clone();
    assert!(
        matches!(trace.as_slice(), ["job-start", "job-end", "service", "participant"] | ["job-start", "service", "job-end", "participant"]),
        "unexpected shutdown trace: {trace:?}"
    );

    // A retained handle does not hold shutdown open: the stop seals the journal
    // rather than waiting for the last clone to go, so a host that keeps one by
    // design still stops, and stops promptly.
    let runtime = Arc::new(assembly().runtime().await.expect("the boot is valid"));
    let retained = runtime.handle();
    let stopping = tokio::spawn({
        let runtime = Arc::clone(&runtime);

        async move { runtime.stop().await.unwrap() }
    });

    assert_eq!((stopping).await.expect("runtime stop does not panic"), Position::ORIGIN);
    assert!(
        matches!(retained.records.publish(Noted("after".to_owned())).await, Err(harmos_runtime::Error::Unavailable)),
        "a sealed journal refuses a late write rather than leaving it hanging"
    );
    assert_eq!(runtime.stop().await.unwrap(), Position::ORIGIN, "a repeat stop reports the frontier the first one observed");
}

/* === Tests === */