harmos-runtime 0.1.13

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

/* === Modules === */

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

/* === Imports === */

// Standard Library Imports
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::task::{Context, Poll};
use std::time::Duration;
// Third Party Imports
use tokio::sync::watch;
// Local Imports
use super::Runtime;
use crate::storage::StorageManager;
use crate::{Application, Result};

/* === Constants === */

/* === Macros === */

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

type StartupFuture<A> = Pin<Box<dyn Future<Output = std::result::Result<Runtime<A>, super::Incompatible>> + Send>>;
type CleanupFuture = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
type ReconcileFuture = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
type InitializeFuture = Pin<Box<dyn Future<Output = std::result::Result<(), Box<dyn std::error::Error + Send + Sync>>> + Send>>;
type Initialize<A> = Box<dyn FnOnce(super::Handle<A>) -> InitializeFuture + Send>;
pub(super) type Reconciliation<A> =
    Box<dyn FnOnce(super::StateManager<A>, StorageManager<crate::storage::ErasedCatalog>) -> ReconcileFuture + Send>;

/* === Structs === */

pub(super) struct Initializer<A: Application> {
    pub(super) timeout: Duration,
    pub(super) initialize: Initialize<A>,
}

#[derive(Clone)]
pub(super) struct Cleanup {
    close: Arc<dyn Fn() -> CleanupFuture + Send + Sync>,
    receipt: Weak<CleanupState>,
}

pub struct Startup<A: Application> {
    future: Option<StartupFuture<A>>,
    cleanup: BootstrapCleanup,
}

#[derive(Clone)]
pub struct BootstrapCleanup {
    shared: Arc<CleanupState>,
}

struct CleanupState {
    retained: Mutex<Retained>,
    status: watch::Sender<BootstrapStatus>,
}

struct Retained {
    owner: Option<Cleanup>,
    failure: Option<Arc<crate::Error>>,
}

pub(super) struct Boot<A: Application> {
    pub(super) application: Option<super::launch::ApplicationCleanup<A>>,
    pub(super) identity: Arc<()>,
    pub(super) storage: Option<StorageManager<crate::storage::ErasedCatalog>>,
    pub(super) runtime: Option<Runtime<A>>,
    pub(super) writer: Option<tokio::task::JoinHandle<()>>,
    pub(super) state: Option<super::StateManager<A>>,
    armed: bool,
    cleanup: BootstrapCleanup,
}

/* === Enums === */

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BootstrapStatus {
    Starting,
    Cleaning,
    Failed,
    Settled,
    HandedOff,
}

/* === Traits === */

/* === Implementations === */

impl<A: Application> Startup<A> {
    pub(super) fn new(builder: super::Builder<A>, storage: Option<StorageManager<crate::storage::ErasedCatalog>>) -> Self {
        let cleanup = builder.cleanup.clone();
        let receipt = cleanup.clone();
        let future = Box::pin(async move {
            let outcome = builder.boot(storage.as_ref(), receipt.clone()).await;
            receipt.settle_unowned();
            outcome
        });
        Self { future: Some(future), cleanup }
    }
}

impl<A: Application> Future for Startup<A> {
    type Output = std::result::Result<Runtime<A>, super::Incompatible>;
    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
        let future = self.future.as_mut().expect("startup is polled before completion");
        let outcome = future.as_mut().poll(context);
        if outcome.is_ready() {
            self.future.take();
        }
        outcome
    }
}

impl<A: Application> Drop for Startup<A> {
    fn drop(&mut self) {
        // Drop Boot First So It Can Transfer Every Acquired Owner
        self.future.take();
        self.cleanup.settle_unowned();
    }
}

impl BootstrapCleanup {
    pub(super) fn new() -> Self {
        let (status, _) = watch::channel(BootstrapStatus::Starting);
        Self {
            shared: Arc::new(CleanupState {
                retained: Mutex::new(Retained { owner: None, failure: None }),
                status,
            }),
        }
    }

    pub fn status(&self) -> BootstrapStatus {
        *self.shared.status.borrow()
    }

    pub fn failure(&self) -> Option<Arc<crate::Error>> {
        self.shared.retained.lock().expect("bootstrap cleanup lock").failure.clone()
    }

    pub async fn wait(&self) -> Result<()> {
        let mut status = self.shared.status.subscribe();
        loop {
            let current = *status.borrow_and_update();
            match current {
                BootstrapStatus::Settled | BootstrapStatus::HandedOff => return Ok(()),
                BootstrapStatus::Failed => {
                    let source = {
                        let retained = self.shared.retained.lock().expect("bootstrap cleanup lock");
                        if self.status() != BootstrapStatus::Failed {
                            continue;
                        }
                        retained.failure.clone().expect("failed cleanup retains its cause")
                    };
                    return Err(crate::Error::TaskFailed { domain: "bootstrap cleanup", source });
                }
                BootstrapStatus::Starting | BootstrapStatus::Cleaning => {}
            }
            status.changed().await.expect("receipt retains its status owner");
        }
    }

    pub async fn retry(&self) -> Result<()> {
        let owner = {
            let mut retained = self.shared.retained.lock().expect("bootstrap cleanup lock");
            if self.status() == BootstrapStatus::Failed {
                let owner = retained.owner.clone().expect("failed cleanup owns resources");
                retained.failure = None;
                self.shared.status.send_replace(BootstrapStatus::Cleaning);
                Some(owner)
            } else {
                None
            }
        };
        if let Some(owner) = owner {
            self.run(owner);
        }
        self.wait().await
    }

    fn run(&self, owner: Cleanup) {
        tokio::spawn(async move {
            let _ = owner.close().await;
        });
    }

    pub(super) fn failed(&self, owner: Option<Cleanup>, failure: Arc<crate::Error>) {
        let mut retained = self.shared.retained.lock().expect("bootstrap cleanup lock");
        retained.owner = owner;
        retained.failure = Some(failure);
        self.shared.status.send_replace(BootstrapStatus::Failed);
    }

    fn cancelled(&self, owner: Cleanup) {
        {
            let mut retained = self.shared.retained.lock().expect("bootstrap cleanup lock");
            retained.owner = Some(owner.clone());
            self.shared.status.send_replace(BootstrapStatus::Cleaning);
        }
        self.run(owner);
    }

    pub(super) fn settle_unowned(&self) {
        let retained = self.shared.retained.lock().expect("bootstrap cleanup lock");
        if retained.owner.is_none() && self.status() == BootstrapStatus::Starting {
            self.shared.status.send_replace(BootstrapStatus::Settled);
        }
    }
}

impl<A: Application> Initializer<A> {
    pub(super) fn new<F, Fut, E>(timeout: Duration, initialize: F) -> Self
    where
        F: FnOnce(super::Handle<A>) -> Fut + Send + 'static,
        Fut: Future<Output = std::result::Result<(), E>> + Send + 'static,
        E: std::error::Error + Send + Sync + 'static,
    {
        Self {
            timeout,
            initialize: Box::new(move |handle| Box::pin(async move { initialize(handle).await.map_err(|source| Box::new(source) as _) })),
        }
    }
}

impl Cleanup {
    pub(super) async fn close(&self) -> Result<()> {
        let outcome = (self.close)().await;
        let Some(receipt) = self.receipt.upgrade() else {
            return outcome;
        };
        let mut retained = receipt.retained.lock().expect("bootstrap cleanup lock");
        match outcome {
            Ok(()) => {
                retained.owner = None;
                retained.failure = None;
                receipt.status.send_replace(BootstrapStatus::Settled);
                Ok(())
            }
            Err(error) => {
                let source = Arc::new(error);
                retained.failure = Some(Arc::clone(&source));
                receipt.status.send_replace(BootstrapStatus::Failed);
                Err(crate::Error::TaskFailed { domain: "bootstrap cleanup", source })
            }
        }
    }
}

impl std::fmt::Debug for Cleanup {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.debug_struct("Cleanup").finish_non_exhaustive()
    }
}

impl<A: Application> Boot<A> {
    pub(super) fn new(storage: Option<&StorageManager<crate::storage::ErasedCatalog>>, cleanup: BootstrapCleanup) -> Result<Self> {
        let identity = Arc::new(());
        if let Some(storage) = storage {
            // Refusal must precede installing a cleanup owner: another runtime's
            // manager must never be closed by this failed opening attempt.
            storage.claim_owner(identity.clone())?;
        }

        Ok(Self {
            application: None,
            identity,
            storage: storage.cloned(),
            runtime: None,
            writer: None,
            state: None,
            armed: true,
            cleanup,
        })
    }

    pub(super) async fn acquire(&self) -> Result<()> {
        if let Some(storage) = &self.storage {
            storage.acquire().await?;
        }
        Ok(())
    }

    pub(super) async fn close(&mut self) -> Result<()> {
        if self.runtime.is_none()
            && let Some(application) = &self.application
        {
            application.settle().await.map_err(|source| crate::Error::TaskFailed { domain: "application cleanup", source })?;
        }
        if self.runtime.is_none() {
            if let Some(state) = self.state.take() {
                state.seal();
            }
            if let Some(writer) = self.writer.as_mut() {
                let _ = writer.await;
            }
            self.writer.take();
        }
        let result = if let Some(runtime) = &self.runtime {
            runtime.shutdown().await.map(|_| ())
        } else if let Some(storage) = &self.storage {
            storage.close().await
        } else {
            Ok(())
        };
        // Operation finalization may refuse before the state is joined.
        // Keep the original runtime until success or transfer it to the error.
        self.armed = false;
        if result.is_ok() {
            self.runtime.take();
        }
        result
    }

    pub(super) async fn reject(mut self, source: super::Incompatible) -> super::Incompatible {
        match self.close().await {
            Ok(()) => {
                self.cleanup.shared.status.send_replace(BootstrapStatus::Settled);
                source
            }
            Err(failure) => {
                let failure = Arc::new(failure);
                let retained = self.retain_cleanup(true);
                if retained.is_some() {
                    self.cleanup.failed(retained.clone(), Arc::clone(&failure));
                } else {
                    self.cleanup.shared.status.send_replace(BootstrapStatus::Settled);
                }
                super::Incompatible {
                    cause: super::Cause::Cleanup {
                        source: Box::new(source),
                        failure: crate::Error::TaskFailed {
                            domain: "bootstrap cleanup",
                            source: failure,
                        },
                    },
                    cleanup: retained,
                }
            }
        }
    }

    pub(super) fn retain_cleanup(&mut self, failure_reported: bool) -> Option<Cleanup> {
        let state = self.state.take();
        let writer = self.writer.take();
        let runtime = self.runtime.take().filter(|runtime| !failure_reported || !physically_settled(runtime));
        let application = self.application.take();
        let storage = self.storage.take().filter(|storage| !storage.status().closed);
        self.armed = false;
        if runtime.is_none() && storage.is_none() && writer.is_none() && state.is_none() && application.is_none() {
            return None;
        }
        let writer = Arc::new(tokio::sync::Mutex::new(writer));
        let runtime = runtime.map(Arc::new);
        let failure_reported = Arc::new(AtomicBool::new(false));
        Some(Cleanup {
            receipt: Arc::downgrade(&self.cleanup.shared),
            close: Arc::new(move || {
                let state = state.clone();
                let writer = Arc::clone(&writer);
                let runtime = runtime.clone();
                let storage = storage.clone();
                let application = application.clone();
                let failure_reported = failure_reported.clone();
                Box::pin(async move {
                    if runtime.is_none() {
                        if let Some(application) = application {
                            application
                                .settle()
                                .await
                                .map_err(|source| crate::Error::TaskFailed { domain: "application cleanup", source })?;
                        }
                        if let Some(state) = state {
                            state.seal();
                        }
                        let mut writer = writer.lock().await;
                        if let Some(task) = writer.as_mut() {
                            let _ = task.await;
                        }
                        writer.take();
                    }
                    if let Some(runtime) = runtime {
                        match runtime.shutdown().await {
                            Err(source) if physically_settled(&runtime) => {
                                if failure_reported.swap(true, Ordering::AcqRel) {
                                    Ok(())
                                } else {
                                    Err(source)
                                }
                            }
                            outcome => outcome.map(|_| ()),
                        }
                    } else if let Some(storage) = storage {
                        storage.close().await
                    } else {
                        Ok(())
                    }
                })
            }),
        })
    }

    pub(super) fn finish(mut self) -> Runtime<A> {
        self.armed = false;
        self.cleanup.shared.status.send_replace(BootstrapStatus::HandedOff);
        self.runtime.take().expect("successful boot owns a runtime")
    }
}

impl<A: Application> Drop for Boot<A> {
    fn drop(&mut self) {
        if !self.armed {
            return;
        }
        if let Some(owner) = self.retain_cleanup(false) {
            self.cleanup.cancelled(owner);
        }
    }
}

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

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

fn physically_settled<A: Application>(runtime: &Runtime<A>) -> bool {
    let stopping = runtime.stopping.lock().expect("runtime transition is healthy");
    matches!(&*stopping, super::Stopping::Closing { outcome, .. } if outcome.get().is_some())
}

/* === Tests === */