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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

// Resident Services Use Independent Restart Credit And Bounded Stop Deadlines.

/* === Modules === */

mod restart;
mod runner;

#[cfg(feature = "plugin-sidecar")]
mod sidecar;

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

pub(crate) use runner::{Once, Typed};

#[cfg(feature = "plugin-sidecar")]
pub(crate) use sidecar::Endpoint as NativeEndpoint;
#[cfg(feature = "plugin-sidecar")]
pub(crate) use sidecar::RuntimeDriver;

#[cfg(feature = "plugin-sidecar")]
pub use sidecar::{DemandLease, DemandSource, Managed, ManagedError, Notice, SidecarError, Supervised};

/* === Imports === */

// Standard Library Imports
use std::future::Future;
use std::sync::Arc;
use std::task::Poll;
use std::time::Duration;
// Third Party Imports
use tokio::sync::watch;
// Local Imports
use crate::ServicePolicy;
use crate::operations::{
    Backoff, CallContext, ContextError, ContextValue, Jitter, Manager, OperationDeclaration, Response, RestartPolicy, Running,
};
use crate::supervision::executor;
use crate::{Application, Error, Result, evidence::Evidence, state::StateManager, streams::StreamManager};

/* === Constants === */

// Three Restart Credits Refill After Sustained Health.
pub const POLICY: ServicePolicy = ServicePolicy {
    stop_timeout: Duration::from_secs(5),
    restart: RestartPolicy::OnFailure {
        credit: 3,
        sustained_health: Duration::from_millis(250),
        backoff: Backoff::Exponential {
            initial: Duration::from_millis(10),
            maximum: Duration::from_secs(5),
            jitter: Jitter::Deterministic,
        },
    },
};

/* === Macros === */

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

pub(crate) type ServiceRunner =
    fn(ServiceDefinition, harmos_sdk::operations::ServiceFactory, ServiceOwner) -> Arc<dyn crate::operations::resident::Runner>;

#[derive(Clone)]
pub(crate) enum ServiceOwner {
    Host,
    #[cfg(any(feature = "plugin", feature = "plugin-guest", feature = "plugin-sidecar"))]
    Plugin(crate::plugin::PluginScope),
}

pub(crate) fn authored_runner<A: Application>(
    definition: ServiceDefinition, factory: harmos_sdk::operations::ServiceFactory, owner: ServiceOwner,
) -> Arc<dyn crate::operations::resident::Runner> {
    Arc::new(Typed::<A, (), _>::new(definition, move |_| AuthoredService {
        factory: factory.clone(),
        instance: None,
        owner: owner.clone(),
    }))
}

/* === Structs === */

#[derive(Clone, Debug)]
pub struct ServiceDefinition {
    pub id: String,
    pub policy: ServicePolicy,
}

// The runtime managers a service context lends as typed values.
pub(crate) struct Handles<A: Application> {
    pub(crate) storage: Arc<std::sync::OnceLock<crate::storage::StorageManager<crate::storage::ErasedCatalog>>>,
    pub(crate) state: StateManager<A>,
    pub(crate) records: crate::records::RecordManager<A>,
    pub(crate) streams: StreamManager<A>,
    pub(crate) evidence: Evidence<A>,
}

#[derive(Clone)]
pub struct ServiceManager {
    operations: Manager,
    owner: ServiceOwner,
}

struct AuthoredService {
    owner: ServiceOwner,
    factory: harmos_sdk::operations::ServiceFactory,
    instance: Option<Box<dyn harmos_sdk::operations::ServiceInstance>>,
}

pub struct ServiceContext<A: Application> {
    pub(crate) state: StateManager<A>,
    pub(crate) records: crate::records::RecordManager<A>,
    pub(crate) operations: Manager,
    pub(crate) streams: StreamManager<A>,
    pub(crate) evidence: Evidence<A>,
    pub(crate) stopping: watch::Receiver<bool>,
    pub(crate) ready: watch::Sender<bool>,

    #[cfg(feature = "plugin-sidecar")]
    pub(crate) provider: Arc<crate::supervision::instance::Instance>,
    #[cfg(feature = "plugin-sidecar")]
    deactivation: Arc<std::sync::atomic::AtomicBool>,
}

/* === Enums === */

#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum ServiceExit {
    #[error("application: {0}")]
    Application(String),

    #[error("resident panicked")]
    Panicked,

    #[error("restart credit exhausted")]
    CreditExhausted,

    #[error("resident stop deadline exceeded")]
    StopTimedOut,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ServiceStatus {
    Starting,
    Running { credit: usize },
    Stopping,
    BackingOff,
    Stopped,
    Inactive { cause: ServiceExit, history: Vec<String> },
}

/* === Traits === */

// A resident runs until stopped. Each run is stopped and cleaned up before the
// next restart or before the instance stops.
pub trait Resident<A: Application>: Send + 'static {
    type Error: std::error::Error + Send + Sync + 'static;

    fn run(&mut self, context: ServiceContext<A>) -> impl Future<Output = std::result::Result<(), Self::Error>> + Send;

    fn stop(&mut self) -> impl Future<Output = std::result::Result<(), Self::Error>> + Send {
        async { Ok(()) }
    }

    fn cleanup(&mut self) -> impl Future<Output = std::result::Result<(), Self::Error>> + Send;
}

/* === Implementations === */

impl ServiceManager {
    pub(crate) fn new(operations: Manager) -> Self {
        Self { operations, owner: ServiceOwner::Host }
    }
    #[cfg(any(feature = "plugin", feature = "plugin-guest", feature = "plugin-sidecar"))]
    pub(crate) fn scoped(operations: Manager, scope: crate::plugin::PluginScope) -> Self {
        Self {
            operations,
            owner: ServiceOwner::Plugin(scope),
        }
    }
    fn owner(&self) -> &str {
        match &self.owner {
            ServiceOwner::Host => "host",
            #[cfg(any(feature = "plugin", feature = "plugin-guest", feature = "plugin-sidecar"))]
            ServiceOwner::Plugin(scope) => scope.owner(),
        }
    }

    pub async fn register(&self, batch: harmos_sdk::RegistrationBatch) -> Result<()> {
        let (staged, ids) = self.stage(batch)?;
        staged.commit()?;
        self.activate(&ids).await
    }

    pub(crate) fn stage(&self, batch: harmos_sdk::RegistrationBatch) -> Result<(crate::operations::resident::Staged, Vec<String>)> {
        if !batch.operations.is_empty() {
            return Err(Error::InvalidInput {
                field: "service registration",
                reason: "operations belong to the operation manager",
            });
        }
        let mut entries = Vec::with_capacity(batch.services.len());
        let mut ids = Vec::with_capacity(batch.services.len());
        for factory in batch.services {
            let id = crate::QualifiedId::qualify(self.owner(), &factory.local_id).map_err(|_| Error::InvalidInput {
                field: "service identity",
                reason: "invalid local service identifier",
            })?;
            let definition = ServiceDefinition {
                id: id.as_str().to_owned(),
                policy: factory.policy,
            };
            validate(&definition)?;
            ids.push(definition.id.clone());
            let runner = (self.operations.registry.service_runner)(definition.clone(), factory, self.owner.clone());
            entries.push((crate::operations::resident::Definition::Service(definition), runner));
        }
        let staged = self.operations.stage_services(
            entries,
            #[cfg(any(feature = "plugin", feature = "plugin-guest", feature = "plugin-sidecar"))]
            match &self.owner {
                ServiceOwner::Host => None,
                ServiceOwner::Plugin(scope) => Some(scope.clone()),
            },
        )?;
        Ok((staged, ids))
    }

    pub(crate) async fn activate(&self, ids: &[String]) -> Result<()> {
        for id in ids {
            if let Err(error) = self.operations.start_registered_service(id, &self.owner).await {
                for id in ids {
                    self.operations.resident.unregister(id).await?;
                }
                return Err(error);
            }
        }
        Ok(())
    }

    pub async fn unregister(&self, id: &crate::QualifiedId) -> Result<()> {
        if id.as_str().split_once('.').map(|(owner, _)| owner) != Some(self.owner()) {
            return Err(Error::PermissionDenied);
        }
        self.operations.resident.unregister(id.as_str()).await
    }
}

impl<A: Application> Resident<A> for AuthoredService {
    type Error = harmos_sdk::ServingError;

    async fn run(&mut self, context: ServiceContext<A>) -> std::result::Result<(), Self::Error> {
        let mut stopping = context.stopping.clone();
        let context = Arc::new(context);
        let mut dependencies = harmos_sdk::Dependencies::new();
        match &self.owner {
            ServiceOwner::Host => dependencies.insert(context.clone()).map_err(|_| harmos_sdk::ServingError::Catalog)?,
            #[cfg(any(feature = "plugin", feature = "plugin-guest", feature = "plugin-sidecar"))]
            ServiceOwner::Plugin(scope) => dependencies.insert(Arc::new(scope.clone())).map_err(|_| harmos_sdk::ServingError::Catalog)?,
        }
        let call = harmos_sdk::ExecutionContext::new(dependencies, harmos_sdk::operations::Host::context(&context.operations).clone());
        self.instance = Some(self.factory.initialize(call.clone()).await?);
        context.ready();
        tokio::select! {
            biased;
            _ = stopping.wait_for(|stopping| *stopping) => Ok(()),
            result = self.instance.as_mut().expect("initialized service").run(call) => result,
        }
    }

    async fn stop(&mut self) -> std::result::Result<(), Self::Error> {
        if let Some(instance) = &mut self.instance {
            instance.stop().await?;
        }
        Ok(())
    }

    async fn cleanup(&mut self) -> std::result::Result<(), Self::Error> {
        if let Some(instance) = &mut self.instance {
            instance.cleanup().await?;
        }
        self.instance = None;
        Ok(())
    }
}

impl ServiceDefinition {
    pub fn new(id: impl Into<String>) -> Self {
        Self { id: id.into(), policy: POLICY }
    }
}

impl<A: Application> Handles<A> {
    pub(crate) fn context(
        &self, operations: Manager, stopping: watch::Receiver<bool>, ready: watch::Sender<bool>,
        #[cfg(feature = "plugin-sidecar")] provider: Arc<crate::supervision::instance::Instance>,
    ) -> ServiceContext<A> {
        ServiceContext {
            state: self.state.clone(),
            records: self.records.clone(),
            operations,
            streams: self.streams.clone(),
            evidence: self.evidence.clone(),
            stopping,
            ready,
            #[cfg(feature = "plugin-sidecar")]
            provider,
            #[cfg(feature = "plugin-sidecar")]
            deactivation: Arc::default(),
        }
    }
}

impl<A: Application> Clone for Handles<A> {
    fn clone(&self) -> Self {
        Self {
            storage: self.storage.clone(),
            state: self.state.clone(),
            records: self.records.clone(),
            streams: self.streams.clone(),
            evidence: self.evidence.clone(),
        }
    }
}

impl<A: Application> ServiceContext<A> {
    pub fn ready(&self) {
        if !self.is_stopping() {
            self.ready.send_replace(true);
        }
    }

    // An owned host handle for calls made while this context is borrowed,
    // such as inside `until_stopping`.
    pub fn host(&self) -> crate::operations::OperationManager<A::Lanes> {
        crate::operations::OperationManager::new(self.operations.clone())
    }

    pub fn records(&self) -> &crate::records::RecordManager<A> {
        &self.records
    }

    pub fn state(&self) -> &StateManager<A> {
        &self.state
    }

    pub fn streams(&self) -> &StreamManager<A> {
        &self.streams
    }

    pub fn evidence(&self) -> &Evidence<A> {
        &self.evidence
    }

    pub fn is_stopping(&self) -> bool {
        *self.stopping.borrow()
    }

    pub async fn stopping(&mut self) {
        while !*self.stopping.borrow_and_update() {
            if self.stopping.changed().await.is_err() {
                break;
            }
        }
    }

    pub async fn rest(&mut self, duration: Duration) -> bool {
        self.until_stopping(executor::sleep(duration)).await.is_none()
    }

    pub async fn until_stopping<T>(&mut self, future: impl Future<Output = T>) -> Option<T> {
        if self.is_stopping() {
            return None;
        }
        let mut future = Box::pin(future);
        let mut stop = Box::pin(self.stopping.changed());

        std::future::poll_fn(|context| {
            if let Poll::Ready(Ok(()) | Err(_)) = stop.as_mut().poll(context) {
                return Poll::Ready(None);
            }
            future.as_mut().poll(context).map(Some)
        })
        .await
    }
}

impl<A: Application> harmos_sdk::operations::Host for ServiceContext<A> {
    fn publish_on<S: harmos_domain::StreamSpec>(
        &self, row: &S::Row,
    ) -> impl std::future::Future<Output = std::result::Result<(), Self::Error>> + Send
    where
        S::Row: harmos_domain::Message,
    {
        use harmos_domain::Message;
        let published = self.call::<harmos_operations::streams::Publish>(harmos_operations::streams::Publish {
            stream: S::ID.to_owned(),
            schema: S::VERSION,
            sequence: None,
            published_unix_micros: None,
            context: None,
            payload: row.encode(),
            following: Vec::new(),
        });
        async move {
            published.await?;
            Ok(())
        }
    }

    type Error = Error;
    type Instance<O: OperationDeclaration> = Running<O>;

    fn context(&self) -> &CallContext {
        harmos_sdk::operations::Host::context(&self.operations)
    }

    fn with_value<T: ContextValue>(&self, value: T) -> std::result::Result<Self, ContextError> {
        let operations = harmos_sdk::operations::Host::with_value(&self.operations, value)?;
        let mut child = self.clone();
        child.operations = operations;
        Ok(child)
    }

    fn submit<O: OperationDeclaration>(&self, request: O::Request) -> impl Future<Output = Result<Running<O>>> + Send {
        harmos_sdk::operations::Host::submit::<O>(&self.operations, request)
    }

    fn submit_owned<O: OperationDeclaration>(
        &self, request: O::Request, claims: crate::operations::RequestClaims,
    ) -> impl Future<Output = Result<Running<O>>> + Send {
        harmos_sdk::operations::Host::submit_owned::<O>(&self.operations, request, claims)
    }

    fn call<O: OperationDeclaration>(&self, request: O::Request) -> impl Future<Output = Result<Response<O::Response>>> + Send {
        harmos_sdk::operations::Host::call::<O>(&self.operations, request)
    }
}

impl<A: Application> Clone for ServiceContext<A> {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
            records: self.records.clone(),
            operations: self.operations.clone(),
            streams: self.streams.clone(),
            evidence: self.evidence.clone(),
            stopping: self.stopping.clone(),
            ready: self.ready.clone(),
            #[cfg(feature = "plugin-sidecar")]
            provider: self.provider.clone(),
            #[cfg(feature = "plugin-sidecar")]
            deactivation: self.deactivation.clone(),
        }
    }
}

impl<A, F, Fut, E> Resident<A> for F
where
    A: Application,
    F: FnMut(ServiceContext<A>) -> Fut + Send + 'static,
    Fut: Future<Output = std::result::Result<(), E>> + Send + 'static,
    E: std::error::Error + Send + Sync + 'static,
{
    type Error = E;

    fn run(&mut self, context: ServiceContext<A>) -> impl Future<Output = std::result::Result<(), Self::Error>> + Send {
        self(context)
    }

    // Closures must not own asynchronous resources beyond their run future.
    // Resource-owning services implement Resident and retain their cleanup state.
    async fn cleanup(&mut self) -> std::result::Result<(), Self::Error> {
        Ok(())
    }
}

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

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

pub(crate) fn validate(definition: &ServiceDefinition) -> Result<()> {
    if definition.id.is_empty() {
        return Err(Error::InvalidInput {
            field: "service name",
            reason: "must not be empty",
        });
    }
    if definition.id == crate::evidence::SOURCE {
        return Err(Error::InvalidInput {
            field: "service name",
            reason: "artifact is reserved for evidence facts",
        });
    }
    definition.policy.validate().map_err(|source| Error::OperationDeclaration { source: source.into() })
}

/* === Tests === */

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[crate::lanes]
    enum Lanes {
        #[default]
        Default,
    }

    struct Model;
    impl Application for Model {
        type Lanes = Lanes;
        type State = ();
        type Transaction = crate::state::Never;
        type Record = ();
        type Streams = ();
        type Resources = ();
    }

    #[derive(Default)]
    struct Counts {
        initialized: AtomicUsize,
        stopped: AtomicUsize,
        cleaned: AtomicUsize,
        running: tokio::sync::Notify,
    }

    struct Worker {
        counts: Arc<Counts>,
        attempt: usize,
    }

    impl harmos_sdk::Service for Worker {
        type Error = crate::Error;

        async fn initialize(context: harmos_sdk::ExecutionContext) -> Result<Self> {
            let counts = context.resource::<Counts>()?;
            let attempt = counts.initialized.fetch_add(1, Ordering::SeqCst);
            Ok(Self { counts, attempt })
        }

        async fn run(&mut self, context: harmos_sdk::ExecutionContext) -> Result<()> {
            if self.attempt == 0 {
                return Err(Error::InvalidInput {
                    field: "fixture",
                    reason: "first attempt fails",
                });
            }
            let context = context.resource::<ServiceContext<Model>>()?;
            self.counts.running.notify_one();
            let mut context = (*context).clone();
            context.stopping().await;
            Ok(())
        }

        async fn stop(&mut self) -> Result<()> {
            self.counts.stopped.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }

        async fn cleanup(&mut self) -> Result<()> {
            self.counts.cleaned.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
    }

    #[tokio::test]
    async fn authored_service_reconstructs_after_cleanup_and_stops_on_withdrawal() {
        use crate::RegistrationExt;

        let runtime = crate::Runtime::<Model>::builder((), ()).register(Lanes::catalog()).runtime().await.unwrap();
        let counts = Arc::new(Counts::default());
        let mut dependencies = harmos_sdk::Dependencies::new();
        dependencies.insert(counts.clone()).unwrap();
        let policy = ServicePolicy {
            stop_timeout: Duration::from_secs(1),
            restart: RestartPolicy::OnFailure {
                credit: 1,
                sustained_health: Duration::from_secs(1),
                backoff: Backoff::Fixed {
                    delay: Duration::from_millis(1),
                    jitter: Jitter::None,
                },
            },
        };
        let batch = harmos_sdk::RegistrationBatch::new(dependencies).service::<Worker>("worker", policy);
        runtime.register(batch.defaults()).await.unwrap();
        executor::timeout(Duration::from_secs(2), counts.running.notified()).await.unwrap();
        assert_eq!(counts.initialized.load(Ordering::SeqCst), 2);
        assert_eq!(counts.cleaned.load(Ordering::SeqCst), 1);

        runtime.unregister(&crate::QualifiedId::parse("host.worker").unwrap()).await.unwrap();
        assert_eq!(counts.stopped.load(Ordering::SeqCst), 2);
        assert_eq!(counts.cleaned.load(Ordering::SeqCst), 2);
        runtime.stop().await.unwrap();
        assert_eq!(counts.initialized.load(Ordering::SeqCst), 2);
    }
}