lenso-kernel 0.1.0

Portable Kernel runtime for Lenso vNext applications.
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
use std::{
    any::Any,
    cell::{Cell, RefCell},
    collections::BTreeMap,
    rc::Rc,
    time::Duration,
};

use futures::FutureExt;
use lenso_app_plan::{
    AppComposition, CapabilityBinding, CapabilityEndpointPlan, CapabilityRequirementPlan,
    ModuleInstancePlan, ResolvedAppPlan,
};
use lenso_kernel::{
    ActivateContext, DeactivateContext, DeterministicDriver, InvocationContext, Kernel,
    ManagedResource, ModuleLifecycle, NativeExecutionAdapter, NativeRequestEndpoint,
    PrepareContext, PreparedBinding, PreparedNativeApp, PreparedNativeModule, ResourceFuture,
    RuntimeDriver, RuntimeFailure, ShutdownOutcome,
};

#[derive(Debug)]
struct ShutdownEndpoint;

impl NativeRequestEndpoint for ShutdownEndpoint {
    fn capability_id(&self) -> &'static str {
        "capability.shutdown"
    }

    fn descriptor_version(&self) -> &'static str {
        "1.0.0"
    }

    fn operations(&self) -> &'static [&'static str] {
        &["shutdown.call"]
    }

    fn invoke(
        &self,
        _operation: &str,
        _request: Box<dyn Any>,
        _context: InvocationContext,
    ) -> futures::future::LocalBoxFuture<
        'static,
        Result<Result<Box<dyn Any>, Box<dyn Any>>, RuntimeFailure>,
    > {
        futures::future::ready(Ok(Ok(Box::new(()) as Box<dyn Any>))).boxed_local()
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum Event {
    Cancelled(String, bool),
    Deactivate(String),
    ResourceReleaseStarted(String),
    ResourceReleased(String),
}

#[derive(Clone, Copy, Debug)]
enum DeactivationMode {
    Clean,
    Failure,
    Blocked,
}

#[derive(Debug)]
struct RecordingResource {
    name: String,
    events: Rc<RefCell<Vec<Event>>>,
    fail: bool,
}

impl ManagedResource for RecordingResource {
    fn release(&self) -> ResourceFuture {
        self.events
            .borrow_mut()
            .push(Event::ResourceReleaseStarted(self.name.clone()));
        let name = self.name.clone();
        let events = self.events.clone();
        let fail = self.fail;
        Box::pin(async move {
            events.borrow_mut().push(Event::ResourceReleased(name));
            if fail {
                Err(RuntimeFailure::InvalidResolvedPlan {
                    detail: "resource release failure".to_owned(),
                })
            } else {
                Ok(())
            }
        })
    }
}

#[derive(Debug)]
struct RecordingLifecycle {
    instance_key: String,
    events: Rc<RefCell<Vec<Event>>>,
    resources_released: Rc<Cell<usize>>,
    fail_prepare: bool,
    deactivation: DeactivationMode,
    resource_failure: bool,
}

impl ModuleLifecycle for RecordingLifecycle {
    fn prepare(&self, _context: PrepareContext) -> lenso_kernel::ModuleFuture {
        let fail = self.fail_prepare;
        Box::pin(async move {
            if fail {
                Err(RuntimeFailure::InvalidResolvedPlan {
                    detail: "startup failure".to_owned(),
                })
            } else {
                Ok(())
            }
        })
    }

    fn activate(&self, context: ActivateContext) -> lenso_kernel::ModuleFuture {
        let cancellation = context.cancellation();
        let admission = context.admission();
        let events = self.events.clone();
        let instance_key = self.instance_key.clone();
        let resource_name = instance_key.clone();
        let resource_events = self.events.clone();
        context
            .resources()
            .register(RecordingResource {
                name: resource_name,
                events: resource_events,
                fail: self.resource_failure,
            })
            .expect("the generation should accept resources before shutdown");
        context
            .tasks()
            .spawn_local(Box::pin(async move {
                cancellation.cancelled().await;
                events
                    .borrow_mut()
                    .push(Event::Cancelled(instance_key, admission.is_closed()));
            }))
            .expect("the generation should accept tasks before shutdown");
        Box::pin(async { Ok(()) })
    }

    fn deactivate(&self, context: DeactivateContext) -> lenso_kernel::ModuleFuture {
        assert!(matches!(
            context.reason(),
            lenso_kernel::DeactivationReason::Shutdown
                | lenso_kernel::DeactivationReason::StartupRollback
        ));
        let events = self.events.clone();
        let instance_key = self.instance_key.clone();
        let resources_released = self.resources_released.clone();
        match self.deactivation {
            DeactivationMode::Clean => Box::pin(async move {
                events.borrow_mut().push(Event::Deactivate(instance_key));
                resources_released.set(resources_released.get() + 1);
                Ok(())
            }),
            DeactivationMode::Failure => Box::pin(async move {
                events.borrow_mut().push(Event::Deactivate(instance_key));
                Err(RuntimeFailure::InvalidResolvedPlan {
                    detail: "deactivation failure".to_owned(),
                })
            }),
            DeactivationMode::Blocked => Box::pin(async move {
                let _ = (events, instance_key, resources_released);
                futures::future::pending::<()>().await;
                Ok(())
            }),
        }
    }
}

#[derive(Debug)]
struct RecordingAdapter {
    modules: BTreeMap<String, Rc<dyn ModuleLifecycle>>,
}

impl NativeExecutionAdapter for RecordingAdapter {
    fn prepare(&self, _plan: &ResolvedAppPlan) -> Result<PreparedNativeApp, RuntimeFailure> {
        let endpoint: Rc<dyn NativeRequestEndpoint> = Rc::new(ShutdownEndpoint);
        let generations = self
            .modules
            .iter()
            .map(|(instance_key, lifecycle)| {
                let endpoints = if instance_key == "provider" {
                    vec![endpoint.clone()]
                } else {
                    Vec::new()
                };
                (
                    instance_key.clone(),
                    PreparedNativeModule::with_lifecycle(endpoints, lifecycle.clone()),
                )
            })
            .collect();
        Ok(PreparedNativeApp::new(
            vec![PreparedBinding::new("consumer", "provider", endpoint)],
            generations,
        ))
    }
}

#[derive(Debug)]
struct BlockedTaskLifecycle;

impl ModuleLifecycle for BlockedTaskLifecycle {
    fn activate(&self, context: ActivateContext) -> lenso_kernel::ModuleFuture {
        context
            .tasks()
            .spawn_local(Box::pin(futures::future::pending()))
            .expect("the generation should accept the blocked task before shutdown");
        Box::pin(async { Ok(()) })
    }
}

fn plan() -> ResolvedAppPlan {
    AppComposition::new(
        vec![
            ModuleInstancePlan::new("provider", "package.provider").with_capability(
                CapabilityEndpointPlan::new("capability.shutdown", "1.0.0", ["shutdown.call"]),
            ),
            ModuleInstancePlan::new("consumer", "package.consumer").with_requirement(
                CapabilityRequirementPlan::one("capability.shutdown", "1.0.0"),
            ),
        ],
        vec![CapabilityBinding::new(
            "consumer",
            "capability.shutdown",
            "1.0.0",
            "provider",
        )],
    )
    .resolve()
    .expect("the shutdown plan should resolve")
}

fn adapter(
    events: &Rc<RefCell<Vec<Event>>>,
    resources_released: &Rc<Cell<usize>>,
    deactivation: DeactivationMode,
    resource_failure: bool,
    fail_prepare: bool,
) -> RecordingAdapter {
    let modules = ["provider", "consumer"]
        .into_iter()
        .map(|instance_key| {
            (
                instance_key.to_owned(),
                Rc::new(RecordingLifecycle {
                    instance_key: instance_key.to_owned(),
                    events: events.clone(),
                    resources_released: resources_released.clone(),
                    fail_prepare: fail_prepare && instance_key == "provider",
                    deactivation,
                    resource_failure,
                }) as Rc<dyn ModuleLifecycle>,
            )
        })
        .collect();
    RecordingAdapter { modules }
}

#[test]
fn shutdown_cancels_managed_work_releases_resources_once_and_deactivates_in_reverse_order() {
    let events = Rc::new(RefCell::new(Vec::new()));
    let resources_released = Rc::new(Cell::new(0));
    let driver = DeterministicDriver::new();
    let app = driver
        .run(Kernel::start_native(
            plan(),
            driver.clone(),
            adapter(
                &events,
                &resources_released,
                DeactivationMode::Clean,
                false,
                false,
            ),
        ))
        .expect("the App should start");

    assert!(app.is_accepting());
    app.request_shutdown();
    assert!(!app.is_accepting());
    let outcome = driver.run(app.shutdown(Duration::from_secs(1)));

    assert_eq!(outcome, ShutdownOutcome::Clean);
    assert_eq!(
        driver.run(app.shutdown(Duration::from_secs(1))),
        ShutdownOutcome::Clean
    );
    assert_eq!(resources_released.get(), 2);
    assert_eq!(
        *events.borrow(),
        vec![
            Event::Cancelled("consumer".to_owned(), true),
            Event::Cancelled("provider".to_owned(), true),
            Event::Deactivate("consumer".to_owned()),
            Event::ResourceReleaseStarted("consumer".to_owned()),
            Event::ResourceReleased("consumer".to_owned()),
            Event::Deactivate("provider".to_owned()),
            Event::ResourceReleaseStarted("provider".to_owned()),
            Event::ResourceReleased("provider".to_owned()),
        ]
    );
}

#[test]
fn concurrent_and_dropped_shutdown_callers_share_one_cleanup_run() {
    let events = Rc::new(RefCell::new(Vec::new()));
    let resources_released = Rc::new(Cell::new(0));
    let driver = DeterministicDriver::new();
    let app = driver
        .run(Kernel::start_native(
            plan(),
            driver.clone(),
            adapter(
                &events,
                &resources_released,
                DeactivationMode::Clean,
                false,
                false,
            ),
        ))
        .expect("the App should start");

    let mut abandoned = Box::pin(app.shutdown(Duration::from_secs(1)));
    assert!(abandoned.as_mut().now_or_never().is_none());
    drop(abandoned);

    let first = app.clone();
    let (first, second) = driver.run(futures::future::join(
        first.shutdown(Duration::from_secs(1)),
        app.shutdown(Duration::from_secs(1)),
    ));

    assert_eq!(first, ShutdownOutcome::Clean);
    assert_eq!(second, ShutdownOutcome::Clean);
    assert_eq!(resources_released.get(), 2);
    assert_eq!(
        events
            .borrow()
            .iter()
            .filter(|event| matches!(event, Event::Deactivate(_)))
            .count(),
        2
    );
}

#[test]
fn shutdown_reports_cleanup_failure_after_deactivating_and_releasing_every_instance() {
    let events = Rc::new(RefCell::new(Vec::new()));
    let resources_released = Rc::new(Cell::new(0));
    let driver = DeterministicDriver::new();
    let app = driver
        .run(Kernel::start_native(
            plan(),
            driver.clone(),
            adapter(
                &events,
                &resources_released,
                DeactivationMode::Failure,
                true,
                false,
            ),
        ))
        .expect("the App should start");

    let outcome = driver.run(app.shutdown(Duration::from_secs(1)));

    assert!(matches!(
        outcome,
        ShutdownOutcome::RuntimeFailure {
            error: RuntimeFailure::InvalidResolvedPlan { detail }
        } if detail == "deactivation failure"
    ));
    assert_eq!(resources_released.get(), 0);
    assert_eq!(
        events
            .borrow()
            .iter()
            .filter(|event| matches!(event, Event::ResourceReleased(_)))
            .count(),
        2
    );
}

#[test]
fn shutdown_reports_a_managed_resource_release_failure() {
    let events = Rc::new(RefCell::new(Vec::new()));
    let resources_released = Rc::new(Cell::new(0));
    let driver = DeterministicDriver::new();
    let app = driver
        .run(Kernel::start_native(
            plan(),
            driver.clone(),
            adapter(
                &events,
                &resources_released,
                DeactivationMode::Clean,
                true,
                false,
            ),
        ))
        .expect("the App should start");

    let outcome = driver.run(app.shutdown(Duration::from_secs(1)));

    assert!(matches!(
        outcome,
        ShutdownOutcome::RuntimeFailure {
            error: RuntimeFailure::InvalidResolvedPlan { detail }
        } if detail == "resource release failure"
    ));
    assert_eq!(resources_released.get(), 2);
}

#[test]
fn shutdown_timeout_terminates_blocked_deactivation_at_the_global_deadline() {
    let events = Rc::new(RefCell::new(Vec::new()));
    let resources_released = Rc::new(Cell::new(0));
    let driver = DeterministicDriver::new();
    let app = driver
        .run(Kernel::start_native(
            plan(),
            driver.clone(),
            adapter(
                &events,
                &resources_released,
                DeactivationMode::Blocked,
                false,
                false,
            ),
        ))
        .expect("the App should start");
    let advance_driver = driver.clone();
    driver
        .spawn_local(Box::pin(async move {
            advance_driver.yield_now().await;
            advance_driver.advance(Duration::from_millis(10));
        }))
        .expect("the deterministic Driver should accept the clock task");

    let outcome = driver.run(app.shutdown(Duration::from_millis(10)));

    assert_eq!(outcome, ShutdownOutcome::Timeout);
    assert_eq!(driver.now(), Duration::from_millis(10));
    assert!(events.borrow().iter().all(|event| !matches!(
        event,
        Event::ResourceReleaseStarted(_) | Event::ResourceReleased(_)
    )));
}

#[test]
fn start_native_preserves_the_startup_failure_classification() {
    let events = Rc::new(RefCell::new(Vec::new()));
    let resources_released = Rc::new(Cell::new(0));
    let failing_modules = adapter(
        &events,
        &resources_released,
        DeactivationMode::Clean,
        false,
        true,
    )
    .modules;
    let driver = DeterministicDriver::new();

    let outcome = driver.run(Kernel::start_native(
        plan(),
        driver.clone(),
        RecordingAdapter {
            modules: failing_modules,
        },
    ));

    assert!(matches!(
        outcome,
        Err(RuntimeFailure::InvalidResolvedPlan { detail }) if detail == "startup failure"
    ));
}

#[test]
fn shutdown_timeout_terminates_a_blocked_managed_task() {
    let events = Rc::new(RefCell::new(Vec::new()));
    let resources_released = Rc::new(Cell::new(0));
    let mut modules = adapter(
        &events,
        &resources_released,
        DeactivationMode::Clean,
        false,
        false,
    )
    .modules;
    modules.insert("provider".to_owned(), Rc::new(BlockedTaskLifecycle));
    let driver = DeterministicDriver::new();
    let app = driver
        .run(Kernel::start_native(
            plan(),
            driver.clone(),
            RecordingAdapter { modules },
        ))
        .expect("the App should start");
    let advance_driver = driver.clone();
    driver
        .spawn_local(Box::pin(async move {
            advance_driver.yield_now().await;
            advance_driver.advance(Duration::from_millis(10));
        }))
        .expect("the deterministic Driver should accept the clock task");

    assert_eq!(
        driver.run(app.shutdown(Duration::from_millis(10))),
        ShutdownOutcome::Timeout
    );
    assert_eq!(driver.now(), Duration::from_millis(10));
}