harmos-runtime 0.1.8

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

// One sidecar connection, run as a resident service instance the plugin
// manager owns. The instance runs the connection once and holds its cleanup
// until the plugin manager stops it; the manager polls the connection's
// outcome and, during settlement, the instance's cleanup inline with its own
// lifecycle steps.

/* === Modules === */

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

/* === Imports === */

// Standard Library Imports
use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::task::{Context, Poll, Wake, Waker};
// Third Party Imports
use futures_util::FutureExt;
use tokio::sync::watch;
// Local Imports
use super::{Notice, Supervised};
use crate::operations::resident::Lease;
use crate::operations::{Manager, RestartPolicy};
use crate::services::{Once, Resident, ServiceContext, ServiceDefinition};
use crate::{Application, Result};
use harmos_sidecar::Attach;

/* === Constants === */

static NEXT_CONNECTION: AtomicU64 = AtomicU64::new(1);

/* === Macros === */

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

type Outcome = std::result::Result<(), String>;
type Running = Pin<Box<dyn Future<Output = Outcome> + Send>>;
type Ending = Pin<Box<dyn Future<Output = std::result::Result<(), String>> + Send>>;
type Owner<A, S, H> = std::marker::PhantomData<fn() -> (A, S, H)>;

/* === Structs === */

pub(super) struct Physical<A: Application, S, H> {
    original: Arc<Mutex<Original>>,
    marker: Owner<A, S, H>,
}

pub(super) struct Polling<A: Application, S, H> {
    original: Weak<Mutex<Original>>,
    marker: Owner<A, S, H>,
}

struct Original {
    signals: Arc<Signals>,
    lease: Option<Lease>,
    end: watch::Sender<bool>,
    running_busy: bool,
    ending_busy: bool,
    cancelled: bool,
    running: Option<Running>,
    ending: Option<Ending>,
    settled: bool,
    ending_failure: Option<String>,
    outcome: Option<Outcome>,
}

#[derive(Default)]
struct Signals {
    revision: AtomicU64,
    waiting: Mutex<Vec<Waker>>,
}

// The resident the connection's service instance runs: the connection once,
// then nothing until the plugin manager stops the instance.
struct Connection<A: Application, S, H> {
    supervised: Supervised<A, S, H>,
    outcome: watch::Sender<Option<Outcome>>,
    // Set by the plugin manager. Instance cancellation waits for every
    // dependent observer, which retires only in this connection's cleanup.
    end: watch::Receiver<bool>,
}

/* === Enums === */

/* === Traits === */

/* === Implementations === */

impl Signals {
    fn register(&self, waker: &Waker) {
        let mut waiting = self.waiting.lock().expect("physical wake registry is healthy");
        if !waiting.iter().any(|current| current.will_wake(waker)) {
            waiting.push(waker.clone());
        }
    }

    fn notify(&self) {
        self.revision.fetch_add(1, Ordering::AcqRel);
        let waiting = std::mem::take(&mut *self.waiting.lock().expect("physical wake registry is healthy"));
        for waker in waiting {
            waker.wake();
        }
    }
}

impl Wake for Signals {
    fn wake(self: Arc<Self>) {
        self.notify();
    }

    fn wake_by_ref(self: &Arc<Self>) {
        self.notify();
    }
}

impl<A: Application, S, H> Clone for Polling<A, S, H> {
    fn clone(&self) -> Self {
        Self {
            original: self.original.clone(),
            marker: std::marker::PhantomData,
        }
    }
}

impl<A, S, H> Resident<A> for Connection<A, S, H>
where
    A: Application,
    S: Attach,
    H: Fn(&ServiceContext<A>, Notice) + Send + Sync + 'static,
{
    type Error = std::io::Error;

    async fn run(&mut self, context: ServiceContext<A>) -> std::result::Result<(), Self::Error> {
        let mut stopping = context.clone();
        let mut end = self.end.clone();
        // Ending Drops The Connection Attempt; Its Cleanup Runs Afterwards
        let running = AssertUnwindSafe(self.supervised.run(context)).catch_unwind();
        let ended = tokio::select! {
            ended = running => Some(ended.unwrap_or_else(|_| {
                Err(std::io::Error::other("native physical attempt panicked"))
            })),
            _ = end.wait_for(|ended| *ended) => None,
            () = stopping.stopping() => None,
        };
        if let Some(outcome) = ended {
            self.outcome.send_replace(Some(outcome.map_err(|error| error.to_string())));
            tokio::select! {
                _ = end.wait_for(|ended| *ended) => {}
                () = stopping.stopping() => {}
            }
        }
        Ok(())
    }

    async fn stop(&mut self) -> std::result::Result<(), Self::Error> {
        self.supervised.stop().await
    }

    async fn cleanup(&mut self) -> std::result::Result<(), Self::Error> {
        self.supervised.cleanup().await
    }
}

impl<A, S, H> Physical<A, S, H>
where
    A: Application,
    S: Attach,
    H: Fn(&ServiceContext<A>, Notice) + Send + Sync + 'static,
{
    // Start the connection as a service instance; the plugin manager restarts
    // plugins, so the instance itself never restarts.
    pub(super) fn new(resident: Supervised<A, S, H>, operations: &Manager) -> Result<Self> {
        let number = NEXT_CONNECTION.fetch_add(1, Ordering::Relaxed);
        let definition = ServiceDefinition {
            id: format!("harmos.sidecar.{}.{number}", resident.attach.owner()),
            policy: crate::ServicePolicy {
                restart: RestartPolicy::Never,
                ..crate::services::POLICY
            },
        };
        let (outcome, mut finished) = watch::channel(None);
        let (end, ending) = watch::channel(false);
        // Clear The Previous Attempt's Refusal Before The New Task Is Scheduled.
        resident.calls.begin();
        let connection = Connection {
            supervised: resident,
            outcome,
            end: ending,
        };
        let lease = operations.start_service::<A>(Once::new(definition, connection), true, false)?;
        let running: Running = Box::pin(async move {
            match finished.wait_for(Option::is_some).await {
                Ok(outcome) => outcome.clone().expect("a finished connection has an outcome"),
                Err(_) => Err("native physical attempt was interrupted".into()),
            }
        });
        Ok(Self {
            original: Arc::new(Mutex::new(Original {
                signals: Arc::default(),
                lease: Some(lease),
                end,
                running_busy: false,
                ending_busy: false,
                cancelled: false,
                running: Some(running),
                ending: None,
                settled: false,
                ending_failure: None,
                outcome: None,
            })),
            marker: std::marker::PhantomData,
        })
    }

    pub(super) fn polling(&self) -> Polling<A, S, H> {
        Polling {
            original: Arc::downgrade(&self.original),
            marker: std::marker::PhantomData,
        }
    }

    fn signals(&self, context: &Context<'_>) -> Arc<Signals> {
        let signals = self.original.lock().unwrap_or_else(|error| error.into_inner()).signals.clone();
        signals.register(context.waker());
        signals
    }

    pub(super) fn poll(&mut self, context: &mut Context<'_>) -> Poll<Outcome> {
        let signals = self.signals(context);
        let mut running = {
            let mut original = self.original.lock().unwrap_or_else(|error| error.into_inner());
            if let Some(outcome) = &original.outcome {
                return Poll::Ready(outcome.clone());
            }
            if original.running_busy {
                return Poll::Pending;
            }
            let Some(running) = original.running.take() else {
                return Poll::Ready(Err("native physical attempt was interrupted".into()));
            };
            original.running_busy = true;
            running
        };
        let revision = signals.revision.load(Ordering::Acquire);
        let waker = Waker::from(signals.clone());
        let mut polling = Context::from_waker(&waker);
        let result = running.as_mut().poll(&mut polling);
        let mut original = self.original.lock().unwrap_or_else(|error| error.into_inner());
        original.running_busy = false;
        match result {
            Poll::Pending => {
                if !original.cancelled {
                    original.running = Some(running);
                }
                drop(original);
                if signals.revision.load(Ordering::Acquire) != revision {
                    signals.notify();
                }
                Poll::Pending
            }
            Poll::Ready(outcome) => {
                original.outcome = Some(outcome.clone());
                drop(original);
                drop(running);
                signals.notify();
                Poll::Ready(outcome)
            }
        }
    }

    // The plugin started: its connection instance runs.
    pub(super) fn ready(&self) {
        let original = self.original.lock().unwrap_or_else(|error| error.into_inner());
        if let Some(lease) = &original.lease {
            lease.transition(crate::operations::OperationPhase::Running);
        }
    }

    pub(super) fn is_running(&self) -> bool {
        let original = self.original.lock().unwrap_or_else(|error| error.into_inner());
        original.running.is_some() || original.running_busy
    }

    // A sibling poller (dependency or progress) may have observed the attempt end first.
    pub(super) fn has_outcome(&self) -> bool {
        self.original.lock().unwrap_or_else(|error| error.into_inner()).outcome.is_some()
    }

    // Stop the connection's instance; its cleanup settles through `poll_ending`.
    pub(super) fn cancel(&mut self) {
        let signals = {
            let mut original = self.original.lock().unwrap_or_else(|error| error.into_inner());
            original.cancelled = true;
            original.running.take();
            original.end.send_replace(true);
            original.signals.clone()
        };
        signals.notify();
    }

    pub(super) fn retry_ending(&mut self) {
        self.original.lock().unwrap_or_else(|error| error.into_inner()).ending_failure.take();
    }

    pub(super) fn poll_ending(&mut self, context: &mut Context<'_>) -> Poll<std::result::Result<(), String>> {
        let signals = self.signals(context);
        let mut ending = {
            let mut original = self.original.lock().unwrap_or_else(|error| error.into_inner());
            if original.settled {
                return Poll::Ready(Ok(()));
            }
            if let Some(failure) = &original.ending_failure {
                return Poll::Ready(Err(failure.clone()));
            }
            if original.ending_busy || original.running_busy || original.running.is_some() {
                return Poll::Pending;
            }
            original.ending_busy = true;
            match original.ending.take() {
                Some(ending) => ending,
                None => {
                    original.end.send_replace(true);
                    let Some(lease) = original.lease.as_mut() else {
                        original.ending_busy = false;
                        original.settled = true;
                        return Poll::Ready(Ok(()));
                    };
                    let retiring = lease.retire();
                    Box::pin(async move { retiring.await.map_err(|error| error.to_string()) })
                }
            }
        };
        let revision = signals.revision.load(Ordering::Acquire);
        let waker = Waker::from(signals.clone());
        let mut polling = Context::from_waker(&waker);
        let result = ending.as_mut().poll(&mut polling);
        let mut original = self.original.lock().unwrap_or_else(|error| error.into_inner());
        original.ending_busy = false;
        match result {
            Poll::Pending => {
                original.ending = Some(ending);
                drop(original);
                if signals.revision.load(Ordering::Acquire) != revision {
                    signals.notify();
                }
                Poll::Pending
            }
            Poll::Ready(outcome) => {
                match &outcome {
                    Ok(()) => {
                        original.settled = true;
                        original.lease.take();
                    }
                    Err(failure) => original.ending_failure = Some(failure.clone()),
                }
                drop(original);
                drop(ending);
                signals.notify();
                Poll::Ready(outcome)
            }
        }
    }
}

impl<A, S, H> Polling<A, S, H>
where
    A: Application,
    S: Attach,
    H: Fn(&ServiceContext<A>, Notice) + Send + Sync + 'static,
{
    pub(super) fn poll(&self, context: &mut Context<'_>) -> Poll<Outcome> {
        let Some(original) = self.original.upgrade() else {
            return Poll::Pending;
        };
        let mut physical = Physical::<A, S, H> {
            original,
            marker: std::marker::PhantomData,
        };
        if physical.is_running() { physical.poll(context) } else { Poll::Pending }
    }

    pub(super) fn retry_ending(&self) {
        if let Some(original) = self.original.upgrade() {
            Physical::<A, S, H> {
                original,
                marker: std::marker::PhantomData,
            }
            .retry_ending();
        }
    }

    pub(super) fn poll_ending(&self, context: &mut Context<'_>) -> Poll<std::result::Result<(), String>> {
        let Some(original) = self.original.upgrade() else {
            return Poll::Pending;
        };
        let mut physical = Physical::<A, S, H> {
            original,
            marker: std::marker::PhantomData,
        };
        if physical.is_running() || physical.original.lock().unwrap_or_else(|error| error.into_inner()).settled {
            return Poll::Pending;
        }
        physical.poll_ending(context)
    }

    pub(super) fn same(&self, physical: &Physical<A, S, H>) -> bool {
        self.original.ptr_eq(&Arc::downgrade(&physical.original))
    }
}

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

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

/* === Tests === */