harmos-runtime 0.1.4

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
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

/* === Modules === */

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

#[path = "../fixtures/process_provider.rs"]
mod provider;

#[path = "../fixtures/process_client.rs"]
mod client;

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

/* === Imports === */

// Standard Library Imports
use std::collections::BTreeMap;
use std::future::Future;
use std::num::NonZeroUsize;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::task::{Context, Poll};
// Third Party Imports
use contract::{POLICY, Probe, Step};
use tokio::io::{AsyncRead, ReadBuf};
use tokio::sync::Notify;
// Local Imports
use super::{HostLanes, assembly, operations, service_status};
use crate::sidecar::support::Bank;
use crate::sidecar::witness::Witness;
use harmos_runtime::operations::OperationDeclaration;
use harmos_runtime::operations::OperationPolicy;
use harmos_runtime::sidecar::{Attach, Attached, Notice, Refusal, Supervised, Supervisor};
use harmos_runtime::{Message, Runtime, services::Service, services::ServiceStatus};
use harmos_sidecar::attach::{End, Ending};

/* === Constants === */

/* === Macros === */

macro_rules! forwarded {
    ($self:ident, $method:ident) => {
        match &$self.source {
            Source::Provider(source) => source.$method(),
            Source::Client(source) => source.$method(),
            Source::Independent(source) => source.$method(),
        }
    };
}

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

type Wait = Pin<Box<dyn Future<Output = ()> + Send>>;

/* === Structs === */

#[derive(Default)]
struct Gate {
    attached: AtomicUsize,
    starts: AtomicUsize,
    failures: AtomicUsize,
    joined: AtomicBool,
    finished: Notify,
    hold: AtomicBool,
    fail: AtomicBool,
    entered: Notify,
    release: Arc<Notify>,
    cut: Arc<Notify>,
}

#[derive(Clone)]
struct Joined {
    source: Source,
    gate: Arc<Gate>,
}

struct GatedEnding {
    original: Ending,
    gate: Arc<Gate>,
    started: bool,
    waiting: Option<Wait>,
}

struct CutRead {
    original: Box<dyn AsyncRead + Send + Unpin>,
    cut: Wait,
    closed: bool,
}

struct Independent {
    resources: Arc<Witness>,
}

struct Fixture {
    runtime: Runtime<Bank>,
    _lease: harmos_runtime::operations::OperationInstance<harmos_runtime::operations::resident::Activation>,
    marks: BTreeMap<&'static str, Arc<Witness>>,
    client: Arc<Gate>,
    provider: Arc<Gate>,
    independent: Arc<Gate>,
}

/* === Enums === */

#[derive(Clone)]
enum Source {
    Provider(harmos_sidecar::PluginNative<provider::Provider>),
    Client(harmos_sidecar::PluginNative<client::Client>),
    Independent(harmos_sidecar::PluginNative<Independent>),
}

/* === Traits === */

/* === Implementations === */

impl harmos_runtime::operations::lifecycle::Policy for Independent {
    const INITIALIZATION_POLICY: OperationPolicy = POLICY;
    const SHUTDOWN_POLICY: OperationPolicy = POLICY;
}

#[harmos_runtime::plugin(id = "process-independent", provides = (), requires = ())]
impl harmos_runtime::Plugin for Independent {
    type Context = Supervisor;
    type Config = String;
    type Error = Refusal;
    type Resources = Witness;
    fn prepare(key: String) -> Result<Self, Refusal> {
        Ok(Self { resources: Witness::find(&key).ok_or_else(|| Refusal::of("the story witness is gone"))? })
    }
    async fn initialize(&mut self, _: &Supervisor) -> Result<(), Refusal> {
        self.resources.mark("initialized");
        Ok(())
    }
    fn resources(&self) -> Arc<Witness> {
        self.resources.clone()
    }
    fn bind_operations(
        &self, _: &mut harmos_runtime::operations::WireTable<Supervisor>,
    ) -> Result<(), harmos_runtime::operations::ServingError> {
        Ok(())
    }
    async fn stop(&mut self, _: &Supervisor) -> Result<(), Refusal> {
        self.resources.mark("shutdown");
        Ok(())
    }
    async fn cleanup(&mut self) -> Result<(), Refusal> {
        self.resources.mark("cleaned");
        Ok(())
    }
}

impl AsyncRead for CutRead {
    fn poll_read(self: Pin<&mut Self>, context: &mut Context<'_>, buffer: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
        let reader = self.get_mut();
        if reader.closed || reader.cut.as_mut().poll(context).is_ready() {
            reader.closed = true;
            return Poll::Ready(Ok(()));
        }
        Pin::new(&mut reader.original).poll_read(context, buffer)
    }
}

impl End for GatedEnding {
    fn settle(&mut self) -> Pin<Box<dyn Future<Output = std::io::Result<()>> + Send + '_>> {
        Box::pin(async move {
            if !self.started {
                self.started = true;
                self.gate.starts.fetch_add(1, Ordering::SeqCst);
                self.gate.entered.notify_one();
            }
            if self.gate.hold.load(Ordering::SeqCst) {
                if self.waiting.is_none() {
                    let release = self.gate.release.clone();
                    self.waiting = Some(Box::pin(async move { release.notified().await }));
                }
                self.waiting.as_mut().unwrap().await;
                self.waiting.take();
                self.gate.hold.store(false, Ordering::SeqCst);
            }
            if self.gate.fail.load(Ordering::SeqCst) {
                self.gate.failures.fetch_add(1, Ordering::SeqCst);
                return Err(std::io::Error::other("original fixture ending remains failed"));
            }
            self.original.settle().await?;
            self.gate.joined.store(true, Ordering::SeqCst);
            self.gate.finished.notify_one();
            Ok(())
        })
    }
}

impl Attach for Joined {
    type Error = std::io::Error;
    fn owner(&self) -> &str {
        forwarded!(self, owner)
    }
    fn operations(&self) -> harmos_domain::Result<Vec<harmos_runtime::operations::Definition>> {
        forwarded!(self, operations)
    }
    fn operation_contracts(&self) -> harmos_domain::Result<Option<Vec<harmos_runtime::operations::Contract>>> {
        forwarded!(self, operation_contracts)
    }
    fn required_contracts(&self) -> harmos_domain::Result<Option<Vec<harmos_domain::inventory::contracts::Required>>> {
        forwarded!(self, required_contracts)
    }
    fn sdk_lifecycle(&self) -> harmos_domain::Result<Option<[harmos_runtime::operations::lifecycle::Descriptor; 2]>> {
        forwarded!(self, sdk_lifecycle)
    }
    fn publications(&self) -> Vec<harmos_sidecar::StreamDeclaration> {
        forwarded!(self, publications)
    }
    fn observers(&self) -> Vec<harmos_domain::inventory::observers::Declaration> {
        forwarded!(self, observers)
    }
    fn observe_contract(&self) -> harmos_domain::Result<Option<harmos_runtime::operations::Contract>> {
        forwarded!(self, observe_contract)
    }
    fn listener_declarations(&self) -> harmos_domain::Result<Option<Vec<harmos_domain::listeners::Definition>>> {
        forwarded!(self, listener_declarations)
    }
    async fn attach(&self) -> Result<Attached, Self::Error> {
        self.gate.attached.fetch_add(1, Ordering::SeqCst);
        let attached = match &self.source {
            Source::Provider(source) => source.attach().await?,
            Source::Client(source) => source.attach().await?,
            Source::Independent(source) => source.attach().await?,
        };
        let (write, read, control, diagnostics, original) = attached.into_parts();
        let ending = Ending::new(GatedEnding {
            original: original.expect("original SDK physical ending"),
            gate: self.gate.clone(),
            started: false,
            waiting: None,
        });
        let cut = self.gate.cut.clone();
        let read = CutRead { original: read, cut: Box::pin(async move { cut.notified().await }), closed: false };
        let mut attached = Attached::new(write, read).ending(ending);
        if let Some(control) = control {
            attached = attached.control(control);
        }
        if let Some(diagnostics) = diagnostics {
            attached = attached.diagnostics(diagnostics);
        }
        Ok(attached)
    }
}

impl Fixture {
    async fn new() -> Self {
        let fixture = Self::start(false).await;
        service_status(fixture.runtime.operations.watch_residents(), "local-graph", |status| {
            matches!(status, ServiceStatus::Running { .. })
        })
        .await;
        fixture
    }

    // Submit The Graph Without Awaiting Admission; `held` Holds The Client's Initialization.
    async fn start(held: bool) -> Self {
        let marks: BTreeMap<_, _> =
            ["process-provider", "process-client", "process-independent"].into_iter().map(|owner| (owner, Witness::open())).collect();
        if held {
            marks["process-client"].mark("hold-initialization");
        }
        let capacity = NonZeroUsize::new(2).unwrap();
        let provider = Arc::new(Gate::default());
        let client = Arc::new(Gate::default());
        let independent = Arc::new(Gate::default());
        let sources = [
            Joined { source: Source::Provider(harmos_sidecar::PluginNative::new(capacity).unwrap()), gate: provider.clone() },
            Joined { source: Source::Client(harmos_sidecar::PluginNative::new(capacity).unwrap()), gate: client.clone() },
            Joined { source: Source::Independent(harmos_sidecar::PluginNative::new(capacity).unwrap()), gate: independent.clone() },
        ];
        let definitions = sources
            .iter()
            .map(|source| {
                let key = marks[source.owner()].key();
                operations::definition(source, serde_json::to_vec(key).unwrap())
            })
            .collect::<Vec<_>>();
        let encoding_bytes = definitions.iter().map(|definition| definition.configuration.len() * 2).max().unwrap()
            + std::mem::size_of::<harmos_runtime::operations::lifecycle::Initialize>()
            + 84;
        assert!(encoding_bytes <= 512);
        let definitions = harmos_runtime::plugin::PluginDefinitions::new(definitions, []).unwrap();
        let residents = sources.map(|source| Supervised::new(source, notices as fn(&Service<Bank>, Notice)));
        let runtime = assembly()
            .operation_settings(harmos_runtime::operations::Settings {
                preparation_bytes: Some(NonZeroUsize::new(4096).unwrap()),
                ..Default::default()
            })
            .runtime()
            .await
            .unwrap();
        runtime
            .operations
            .register_service(
                harmos_runtime::services::ServiceDefinition {
                    id: "local-graph".into(),
                    policy: harmos_runtime::operations::OperationPolicy {
                        restart: harmos_runtime::operations::RestartPolicy::Never,
                        ..harmos_runtime::services::POLICY
                    },
                },
                move |_: Arc<()>| {
                    harmos_runtime::sidecar::Managed::new_graph(
                        residents.clone(),
                        definitions.clone(),
                        HostLanes::Open,
                        capacity,
                        NonZeroUsize::new(encoding_bytes).unwrap(),
                    )
                    .unwrap()
                },
            )
            .unwrap();
        let lease = runtime.operations.submit_resident("local-graph").await.unwrap();
        Self { runtime, _lease: lease, marks, client, provider, independent }
    }
    fn marks(&self, owner: &str) -> &Witness {
        &self.marks[owner]
    }
    fn settled(&self) {
        for gate in [&self.client, &self.provider, &self.independent] {
            assert_eq!(gate.attached.load(Ordering::SeqCst), 1);
            assert_eq!(gate.starts.load(Ordering::SeqCst), 1);
            assert!(gate.joined.load(Ordering::SeqCst));
        }
        for owner in ["process-provider", "process-client", "process-independent"] {
            assert_eq!(self.marks(owner).count("shutdown"), 1);
            assert_eq!(self.marks(owner).count("cleaned"), 1);
        }
        assert_eq!(self.marks("process-provider").count("read-finalized-2"), 1);
    }
}

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

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

fn notices(_: &Service<Bank>, _: Notice) {}

#[tokio::test]
async fn cancelled_stop_retains() {
    let fixture = Fixture::new().await;
    fixture.client.hold.store(true, Ordering::SeqCst);
    {
        let stop = fixture.runtime.stop();
        tokio::pin!(stop);
        tokio::select! {
            result = &mut stop => panic!("stop returned before original ending: {result:?}"),
            _ = async {
                fixture.client.entered.notified().await;
                fixture.independent.finished.notified().await;
            } => {}
        }
    }
    assert!(fixture.independent.joined.load(Ordering::SeqCst));
    assert!(!fixture.client.joined.load(Ordering::SeqCst));
    assert!(!fixture.provider.joined.load(Ordering::SeqCst));
    assert!(!fixture.marks("process-provider").seen("shutdown"));
    assert_eq!(fixture.marks("process-provider").count("read-finalized-2"), 1);
    fixture.client.release.notify_one();
    fixture.runtime.stop().await.unwrap();
    fixture.settled();
}

#[tokio::test]
async fn failed_ending_retires() {
    let fixture = Fixture::new().await;
    fixture.client.fail.store(true, Ordering::SeqCst);
    let result = fixture.runtime.stop().await;
    assert!(matches!(result, Err(harmos_runtime::Error::TaskFailed { domain: "service", .. })));
    assert!(!fixture.client.joined.load(Ordering::SeqCst));
    assert!(!fixture.provider.joined.load(Ordering::SeqCst));
    assert!(!fixture.marks("process-provider").seen("shutdown"));
    assert!(fixture.independent.joined.load(Ordering::SeqCst));
    assert_eq!(fixture.marks("process-independent").count("cleaned"), 1);
    let failures = fixture.client.failures.load(Ordering::SeqCst);
    assert!(failures > 0);
    let result = fixture.runtime.stop().await;
    assert!(matches!(result, Err(harmos_runtime::Error::TaskFailed { domain: "service", .. })));
    assert!(fixture.client.failures.load(Ordering::SeqCst) > failures);
    assert_eq!(fixture.client.starts.load(Ordering::SeqCst), 1);
    assert_eq!(fixture.marks("process-independent").count("cleaned"), 1);
    assert!(!fixture.marks("process-provider").seen("shutdown"));
    let failures = fixture.client.failures.load(Ordering::SeqCst);
    fixture.client.fail.store(false, Ordering::SeqCst);
    fixture.runtime.stop().await.unwrap();
    fixture.settled();
    assert_eq!(fixture.client.failures.load(Ordering::SeqCst), failures);
}

#[tokio::test]
async fn unrelated_failure_retires() {
    let fixture = Fixture::new().await;
    fixture.independent.fail.store(true, Ordering::SeqCst);
    let result = fixture.runtime.stop().await;
    assert!(matches!(result, Err(harmos_runtime::Error::TaskFailed { domain: "service", .. })));
    assert!(!fixture.independent.joined.load(Ordering::SeqCst));
    assert!(fixture.client.joined.load(Ordering::SeqCst));
    assert!(fixture.provider.joined.load(Ordering::SeqCst));
    assert_eq!(fixture.marks("process-provider").count("read-finalized-2"), 1);
    for owner in ["process-client", "process-provider"] {
        assert_eq!(fixture.marks(owner).count("shutdown"), 1);
        assert_eq!(fixture.marks(owner).count("cleaned"), 1);
    }
    fixture.independent.fail.store(false, Ordering::SeqCst);
    fixture.runtime.stop().await.unwrap();
    fixture.settled();
}

#[tokio::test]
async fn eof_retains_child() {
    let fixture = Fixture::new().await;
    let provider = fixture.marks("process-provider");
    provider.mark("hold-read-2");
    fixture.provider.hold.store(true, Ordering::SeqCst);
    {
        let stop = fixture.runtime.stop();
        tokio::pin!(stop);
        tokio::select! {
            result = &mut stop => panic!("stop returned before child request: {result:?}"),
            _ = provider.marked("read-started-2") => {}
        }
        fixture.provider.cut.notify_one();
        tokio::select! {
            result = &mut stop => panic!("stop returned before physical ending: {result:?}"),
            _ = fixture.provider.entered.notified() => {}
        }
        assert!(!fixture.provider.joined.load(Ordering::SeqCst));
        assert!(!fixture.client.joined.load(Ordering::SeqCst));
        assert!(!fixture.marks("process-client").seen("dependency-shutdown"));
        // Cancel Only The Wait; The Same Original Child And Ending Remain Owned.
    }
    fixture.provider.fail.store(true, Ordering::SeqCst);
    fixture.provider.release.notify_one();
    let failed = fixture.runtime.stop().await;
    assert!(matches!(failed, Err(harmos_runtime::Error::TaskFailed { domain: "service", .. })));
    assert!(fixture.provider.failures.load(Ordering::SeqCst) > 0);
    assert!(!fixture.provider.joined.load(Ordering::SeqCst));
    assert!(!fixture.client.joined.load(Ordering::SeqCst));
    fixture.provider.fail.store(false, Ordering::SeqCst);
    for _ in 0..3 {
        let result = fixture.runtime.stop().await;
        assert!(result.is_ok() || matches!(result, Err(harmos_runtime::Error::TaskFailed { domain: "service", .. })));
        if fixture.provider.joined.load(Ordering::SeqCst) && fixture.client.joined.load(Ordering::SeqCst) {
            break;
        }
    }
    for gate in [&fixture.client, &fixture.provider, &fixture.independent] {
        assert!(gate.joined.load(Ordering::SeqCst));
        assert_eq!(gate.attached.load(Ordering::SeqCst), 1);
        assert_eq!(gate.starts.load(Ordering::SeqCst), 1);
    }
    assert_eq!(provider.count("read-prepared-2"), 1);
    assert_eq!(provider.count("read-started-2"), 1);
    assert_eq!(provider.count("read-finalized-2"), 1);
    for owner in ["process-provider", "process-client", "process-independent"] {
        assert_eq!(fixture.marks(owner).count("cleaned"), 1);
    }
}

#[tokio::test]
async fn dependency_precedes_admission() {
    let fixture = Fixture::start(true).await;
    let client = fixture.marks("process-client");
    let provider = fixture.marks("process-provider");

    // The Lifecycle Loan Reaches Its Provider Before Ordinary Admission
    client.marked("dependency-initialized").await;
    assert_eq!(provider.count("initialized"), 1);
    assert_eq!(provider.count("read-finalized-1"), 1);
    let status = service_status(fixture.runtime.operations.watch_residents(), "local-graph", |_| true).await;
    assert_eq!(status, ServiceStatus::Starting);
    assert!(matches!(
        fixture.runtime.operations.submit_encoded(Probe::ID, Step { phase: 3 }.encode_to_vec()),
        Err(harmos_runtime::Error::NotReady { domain: "operation plugin" }),
    ));
    assert!(!client.seen("probe-initialized"));

    // Released Initialization Admits Ordinary Calls
    client.mark("release-initialization");
    service_status(fixture.runtime.operations.watch_residents(), "local-graph", |status| matches!(status, ServiceStatus::Running { .. }))
        .await;
    let response = fixture.runtime.operations.call_encoded(Probe::ID, Step { phase: 3 }.encode_to_vec()).await.unwrap();
    assert_eq!(Step::decode(&response).unwrap().phase, 3);
    assert_eq!(client.count("probe-initialized"), 1);
    assert_eq!(client.count("probe-finalized"), 1);

    // Reverse Retirement Keeps The Provider Through The Client's Shutdown Call
    fixture.runtime.stop().await.unwrap();
    assert_eq!(client.count("dependency-shutdown"), 1);
    assert_eq!(provider.count("read-started-2"), 1);
    fixture.settled();
}

/* === Tests === */