aion-server 0.31.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
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
//! The lease-record seam (WA-010 R3): the one call that turns a transport's
//! "the worker took it" into the durable [`aion_core::Event::ActivityLeased`]
//! fact, on every transport and on both dispatch paths.
//!
//! # Where the call sits
//!
//! A transport arm calls [`DeliveryAccepted::accepted`] at the instant it knows
//! the worker HOLDS the attempt — the gRPC stream accepted the frame; the
//! liminal push was acknowledged — and BEFORE it waits for any reply. That
//! placement is the whole of the ordering guarantee on the liminal outbox arm,
//! where the reply IS the completion: recorded after the wait, the lease would
//! land after the terminal it is supposed to precede.
//!
//! On the gRPC arm the completion arrives on another task, so the placement
//! alone cannot order the two appends. [`LeaseHandoff`] therefore arms the
//! completion fences' lease gate before the push and settles it after the
//! record lands (or fails, or the delivery is abandoned — the guard settles on
//! drop), and the completion entry waits on that gate before the fence accepts
//! it. See [`CompletionFences::lease_settled`].
//!
//! # What a failed record means
//!
//! An append that fails cannot write its own failure into the history. The
//! ERROR line and [`LeaseRecordLedger`] (exported as
//! `aion_activity_lease_record_failures_total` and read back on the API
//! provenance, R4) are the ONLY record that this install knew the worker and
//! lost the fact — as opposed to a history written before the event existed,
//! which never knew. A failed record never fails or repeats the dispatch: the
//! worker holds the work, and the history simply reads that attempt as
//! unattributed.
//!
//! An UNINSTALLED seam is the same loss by a different route — a boot wired a
//! dispatcher without handing it a recorder — so it is counted on the ledger
//! and said out loud, never silently skipped.

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};

use aion_core::{ActivityId, RunId, WorkerAttribution, WorkflowId};
use async_trait::async_trait;

use super::envelope::{CompletionFences, CompletionToken};
use super::registry::WorkerHandle;
use super::task_delivery::DeliveryAccepted;
use crate::error::ServerError;
use crate::observability::Metrics;

/// The attempt a lease is recorded for.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LeaseKey {
    /// Owning workflow.
    pub workflow_id: WorkflowId,
    /// The run whose history carries the lease.
    pub run_id: RunId,
    /// The activity the attempt belongs to.
    pub activity_id: ActivityId,
    /// One-based delivery attempt, as stamped on the wire.
    pub attempt: u32,
}

impl std::fmt::Display for LeaseKey {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            formatter,
            "workflow {} run {} activity {} attempt {}",
            self.workflow_id, self.run_id, self.activity_id, self.attempt
        )
    }
}

/// Something that can append the lease fact for one attempt.
///
/// Production implements this over the engine ([`EngineLeaseRecorder`]) so the
/// append goes through the workflow's one `Recorder`; tests implement it over a
/// vector or a fault.
#[async_trait]
pub trait ActivityLeaseRecorder: Send + Sync + 'static {
    /// Append `ActivityLeased` for `key`, attributed to `worker`.
    ///
    /// # Errors
    ///
    /// Returns the recorder's own reason when the append did not land. The
    /// caller counts and logs it; it never retries the dispatch.
    async fn record(&self, key: &LeaseKey, worker: WorkerAttribution) -> Result<(), String>;
}

/// The production recorder: the engine's single-writer lease API.
pub struct EngineLeaseRecorder {
    engine: Arc<aion::Engine>,
}

impl EngineLeaseRecorder {
    /// Record leases through `engine`.
    #[must_use]
    pub fn new(engine: Arc<aion::Engine>) -> Self {
        Self { engine }
    }
}

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

#[async_trait]
impl ActivityLeaseRecorder for EngineLeaseRecorder {
    async fn record(&self, key: &LeaseKey, worker: WorkerAttribution) -> Result<(), String> {
        self.engine
            .record_activity_lease(
                &key.workflow_id,
                &key.run_id,
                key.activity_id.clone(),
                key.attempt,
                worker,
            )
            .await
            .map_err(|error| error.to_string())
    }
}

/// The install-lifetime count of leases this server knew and failed to record.
///
/// Shared by clone: every dispatcher on a boot increments the same count, and
/// the API provenance (R4) reads it back so a reader can tell "never recorded
/// here" from "recorded here with N losses".
#[derive(Clone, Debug, Default)]
pub struct LeaseRecordLedger {
    failures: Arc<AtomicU64>,
}

impl LeaseRecordLedger {
    /// Leases lost on this install since boot.
    #[must_use]
    pub fn failures(&self) -> u64 {
        self.failures.load(Ordering::SeqCst)
    }

    fn increment(&self) {
        self.failures.fetch_add(1, Ordering::SeqCst);
    }
}

struct Installed {
    recorder: Arc<dyn ActivityLeaseRecorder>,
    metrics: Option<Metrics>,
}

/// The shared, late-installed recorder every dispatch path records through.
///
/// Built with the worker seams before the engine exists and installed once the
/// engine has booted — the same shape as the outbox delivery callback. Cloning
/// shares the installation and the ledger.
#[derive(Clone, Default)]
pub struct LeaseRecorderSeam {
    installed: Arc<OnceLock<Installed>>,
    ledger: LeaseRecordLedger,
}

impl std::fmt::Debug for LeaseRecorderSeam {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("LeaseRecorderSeam")
            .field("installed", &self.installed.get().is_some())
            .field("failures", &self.ledger.failures())
            .finish()
    }
}

impl LeaseRecorderSeam {
    /// Install the recorder (and the metrics the failure counter exports on).
    ///
    /// Returns `false`, and keeps the first installation, when one is already
    /// in place — a second install is a wiring bug and is said so.
    pub fn install(
        &self,
        recorder: Arc<dyn ActivityLeaseRecorder>,
        metrics: Option<Metrics>,
    ) -> bool {
        if self.installed.set(Installed { recorder, metrics }).is_err() {
            tracing::warn!("activity lease recorder already installed; ignoring duplicate install");
            return false;
        }
        true
    }

    /// Whether a recorder has been installed.
    #[must_use]
    pub fn is_installed(&self) -> bool {
        self.installed.get().is_some()
    }

    /// The shared loss count.
    #[must_use]
    pub const fn ledger(&self) -> &LeaseRecordLedger {
        &self.ledger
    }

    /// Record the lease for `key`, counting and logging a loss instead of
    /// failing: the worker already holds the work.
    pub async fn record(&self, key: &LeaseKey, worker: WorkerAttribution) {
        let Some(installed) = self.installed.get() else {
            self.lost(
                key,
                &worker,
                None,
                "no activity lease recorder is installed on this dispatcher",
            );
            return;
        };
        if let Err(reason) = installed.recorder.record(key, worker.clone()).await {
            self.lost(key, &worker, installed.metrics.as_ref(), &reason);
        }
    }

    /// [`Self::record`] from a thread that cannot await — the engine-seam
    /// bridge, which dispatches synchronously.
    ///
    /// Runs the record on `handle` (or the ambient runtime). Inside a
    /// multi-thread runtime the wait is hosted with `block_in_place`; a
    /// current-thread runtime cannot host it without starving the recorder,
    /// and a thread with no runtime in reach cannot run it at all — both are
    /// counted as losses by name rather than deadlocked or skipped.
    pub fn record_blocking(
        &self,
        handle: Option<&tokio::runtime::Handle>,
        key: &LeaseKey,
        worker: WorkerAttribution,
    ) {
        match tokio::runtime::Handle::try_current() {
            Ok(ambient) => match ambient.runtime_flavor() {
                tokio::runtime::RuntimeFlavor::MultiThread => {
                    let runner = handle.cloned().unwrap_or(ambient);
                    tokio::task::block_in_place(|| runner.block_on(self.record(key, worker)));
                }
                flavor => self.lost(
                    key,
                    &worker,
                    self.installed.get().and_then(|i| i.metrics.as_ref()),
                    &format!(
                        "the lease cannot be recorded from inside a {flavor:?} tokio runtime: \
                         blocking here would starve the recorder of its only thread"
                    ),
                ),
            },
            Err(_) => match handle {
                Some(runner) => runner.block_on(self.record(key, worker)),
                None => self.lost(
                    key,
                    &worker,
                    self.installed.get().and_then(|i| i.metrics.as_ref()),
                    "no tokio runtime is reachable from this thread to record the lease on",
                ),
            },
        }
    }

    fn lost(
        &self,
        key: &LeaseKey,
        worker: &WorkerAttribution,
        metrics: Option<&Metrics>,
        why: &str,
    ) {
        self.ledger.increment();
        if let Some(metrics) = metrics {
            metrics.activity_lease_record_failed();
        }
        tracing::error!(
            workflow_id = %key.workflow_id,
            run_id = %key.run_id,
            activity_id = %key.activity_id,
            attempt = key.attempt,
            worker_identity = %worker.identity,
            task_queue = %worker.task_queue,
            transport = worker.transport.name(),
            lease_record_failures_total = self.ledger.failures(),
            reason = why,
            "activity lease was not recorded; the worker holds the attempt and the history reads \
             it as unattributed"
        );
    }
}

/// The attribution a lease carries for `worker`, read off its registration.
#[must_use]
pub fn attribution_for(worker: &WorkerHandle) -> WorkerAttribution {
    WorkerAttribution {
        identity: worker.identity().to_owned(),
        task_queue: worker.task_queue().to_owned(),
        node: worker.node().map(str::to_owned),
        deployment: worker
            .instance()
            .map(|instance| instance.deployment.clone()),
        instance_id: worker
            .instance()
            .map(|instance| instance.instance_id.clone()),
        transport: worker.delivery().transport(),
    }
}

/// One delivery's lease: armed on the fences before the push, recorded at the
/// transport's accept point, settled on the gate either way.
///
/// Dropping an unaccepted handoff (the push failed, the candidate was skipped)
/// settles the gate without recording, so a completion that can never come is
/// never waited for and a later candidate arms its own.
pub struct LeaseHandoff {
    seam: LeaseRecorderSeam,
    key: LeaseKey,
    worker: WorkerAttribution,
    fences: CompletionFences,
    token: CompletionToken,
    /// Fired at acceptance, starting the engine's authored per-attempt bound.
    ///
    /// Held HERE, beside the durable lease record, so the clock and the record
    /// cannot disagree about when the worker took the attempt: one object
    /// answers both, and neither can be moved without the other.
    lease: aion::LeaseSignal,
    settled: AtomicBool,
}

impl LeaseHandoff {
    /// The attempt this handoff leases.
    #[must_use]
    pub fn key(&self) -> &LeaseKey {
        &self.key
    }

    /// The completion token the lease gate is armed under.
    #[must_use]
    pub fn token(&self) -> &CompletionToken {
        &self.token
    }
}

impl std::fmt::Debug for LeaseHandoff {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("LeaseHandoff")
            .field("key", &self.key)
            .field("settled", &self.settled.load(Ordering::SeqCst))
            .finish_non_exhaustive()
    }
}

impl LeaseHandoff {
    /// Arm the fences' lease gate for `token` and hold the lease to record.
    ///
    /// `lease` is fired at the same instant the lease is RECORDED, so the
    /// engine's authored per-attempt bound and the durable `ActivityLeased`
    /// event share one anchor by construction. A caller with no per-attempt
    /// timer above it — the outbox push leg, whose dispatches are not driven by
    /// the engine's retry loop — passes
    /// [`LeaseSignal::none`](aion::LeaseSignal::none), which is a statement
    /// that nothing is timing this attempt rather than a value standing in for
    /// one.
    ///
    /// # Errors
    ///
    /// Returns lock poison from the fences.
    pub fn arm(
        seam: LeaseRecorderSeam,
        key: LeaseKey,
        worker: WorkerAttribution,
        fences: CompletionFences,
        token: CompletionToken,
        lease: aion::LeaseSignal,
    ) -> Result<Self, ServerError> {
        fences.arm_lease(&token)?;
        Ok(Self {
            seam,
            key,
            worker,
            fences,
            token,
            lease,
            settled: AtomicBool::new(false),
        })
    }

    /// Record the lease from a thread that cannot await, then settle the gate.
    pub fn accepted_blocking(&self, handle: Option<&tokio::runtime::Handle>) {
        if self.settled.load(Ordering::SeqCst) {
            tracing::warn!(key = %self.key, "lease handoff accepted twice; the second is ignored");
            return;
        }
        self.seam
            .record_blocking(handle, &self.key, self.worker.clone());
        // The worker holds the attempt from here, so the engine's per-attempt
        // bound starts from here. Fired beside the durable record rather than
        // at some site that also knows about acceptance, because "when the
        // lease happened" must have exactly one answer.
        self.lease.fire();
        self.settle();
    }

    fn settle(&self) {
        if self.settled.swap(true, Ordering::SeqCst) {
            return;
        }
        if let Err(error) = self.fences.settle_lease(&self.token) {
            tracing::error!(
                key = %self.key,
                %error,
                "lease gate could not be settled; completions for this attempt may wait on a \
                 poisoned fence"
            );
        }
    }
}

impl Drop for LeaseHandoff {
    fn drop(&mut self) {
        self.settle();
    }
}

#[async_trait]
impl DeliveryAccepted for LeaseHandoff {
    async fn accepted(&self) {
        if self.settled.load(Ordering::SeqCst) {
            tracing::warn!(key = %self.key, "lease handoff accepted twice; the second is ignored");
            return;
        }
        self.seam.record(&self.key, self.worker.clone()).await;
        self.lease.fire();
        self.settle();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use aion_core::WorkerTransport;
    use std::sync::Mutex;
    use uuid::Uuid;

    struct Recording(Mutex<Vec<(LeaseKey, WorkerAttribution)>>);

    #[async_trait]
    impl ActivityLeaseRecorder for Recording {
        async fn record(&self, key: &LeaseKey, worker: WorkerAttribution) -> Result<(), String> {
            self.0
                .lock()
                .map_err(|_| "recording lock poisoned".to_owned())?
                .push((key.clone(), worker));
            Ok(())
        }
    }

    struct Faulting;

    #[async_trait]
    impl ActivityLeaseRecorder for Faulting {
        async fn record(&self, _key: &LeaseKey, _worker: WorkerAttribution) -> Result<(), String> {
            Err("store fault injected".to_owned())
        }
    }

    fn key() -> LeaseKey {
        LeaseKey {
            workflow_id: WorkflowId::new(Uuid::new_v4()),
            run_id: RunId::new(Uuid::new_v4()),
            activity_id: ActivityId::from_sequence_position(3),
            attempt: 1,
        }
    }

    fn worker() -> WorkerAttribution {
        WorkerAttribution {
            identity: "w-1".to_owned(),
            task_queue: "q".to_owned(),
            node: None,
            deployment: None,
            instance_id: None,
            transport: WorkerTransport::Grpc,
        }
    }

    #[tokio::test]
    async fn an_installed_recorder_receives_the_lease_and_the_ledger_stays_at_zero() {
        let seam = LeaseRecorderSeam::default();
        let recording = Arc::new(Recording(Mutex::new(Vec::new())));
        assert!(seam.install(recording.clone(), None));
        let key = key();
        seam.record(&key, worker()).await;
        let seen = recording
            .0
            .lock()
            .map(|seen| seen.clone())
            .unwrap_or_default();
        assert_eq!(seen.len(), 1);
        assert_eq!(seen[0].0, key);
        assert_eq!(seen[0].1.identity, "w-1");
        assert_eq!(seam.ledger().failures(), 0);
    }

    #[tokio::test]
    async fn a_failed_record_counts_on_the_ledger_and_on_the_metrics_surface() {
        let seam = LeaseRecorderSeam::default();
        let metrics = Metrics::new().ok();
        assert!(seam.install(Arc::new(Faulting), metrics.clone()));
        seam.record(&key(), worker()).await;
        seam.record(&key(), worker()).await;
        assert_eq!(seam.ledger().failures(), 2);
        if let Some(metrics) = metrics {
            let text = String::from_utf8(metrics.encode().unwrap_or_default()).unwrap_or_default();
            assert!(
                text.contains("aion_activity_lease_record_failures_total 2"),
                "the metrics surface must carry the same count; got:\n{text}"
            );
        }
    }

    #[tokio::test]
    async fn an_uninstalled_seam_counts_the_loss_instead_of_skipping_it() {
        let seam = LeaseRecorderSeam::default();
        assert!(!seam.is_installed());
        seam.record(&key(), worker()).await;
        assert_eq!(seam.ledger().failures(), 1);
    }

    #[tokio::test]
    async fn a_second_install_keeps_the_first() {
        let seam = LeaseRecorderSeam::default();
        let recording = Arc::new(Recording(Mutex::new(Vec::new())));
        assert!(seam.install(recording.clone(), None));
        assert!(!seam.install(Arc::new(Faulting), None));
        seam.record(&key(), worker()).await;
        assert_eq!(seam.ledger().failures(), 0);
        assert_eq!(recording.0.lock().map_or(0, |s| s.len()), 1);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn a_handoff_arms_the_gate_and_settles_it_after_recording() -> Result<(), ServerError> {
        let seam = LeaseRecorderSeam::default();
        let recording = Arc::new(Recording(Mutex::new(Vec::new())));
        seam.install(recording.clone(), None);
        let fences = CompletionFences::default();
        let k = key();
        let token = fences.issue(&k.workflow_id, &k.run_id, &k.activity_id, k.attempt)?;
        let handoff = LeaseHandoff::arm(
            seam.clone(),
            k,
            worker(),
            fences.clone(),
            token.clone(),
            aion::LeaseSignal::none(),
        )?;
        assert!(fences.lease_pending(&token)?);
        handoff.accepted().await;
        assert!(!fences.lease_pending(&token)?);
        assert_eq!(recording.0.lock().map_or(0, |s| s.len()), 1);
        Ok(())
    }

    #[tokio::test]
    async fn dropping_an_unaccepted_handoff_settles_without_recording() -> Result<(), ServerError> {
        let seam = LeaseRecorderSeam::default();
        let recording = Arc::new(Recording(Mutex::new(Vec::new())));
        seam.install(recording.clone(), None);
        let fences = CompletionFences::default();
        let k = key();
        let token = fences.issue(&k.workflow_id, &k.run_id, &k.activity_id, k.attempt)?;
        let handoff = LeaseHandoff::arm(
            seam,
            k,
            worker(),
            fences.clone(),
            token.clone(),
            aion::LeaseSignal::none(),
        )?;
        assert!(fences.lease_pending(&token)?);
        drop(handoff);
        assert!(!fences.lease_pending(&token)?);
        assert_eq!(recording.0.lock().map_or(0, |s| s.len()), 0);
        Ok(())
    }

    #[tokio::test]
    async fn recording_from_a_current_thread_runtime_is_a_named_loss_not_a_deadlock() {
        let seam = LeaseRecorderSeam::default();
        seam.install(Arc::new(Recording(Mutex::new(Vec::new()))), None);
        seam.record_blocking(None, &key(), worker());
        assert_eq!(seam.ledger().failures(), 1);
    }

    #[test]
    fn recording_from_a_plain_thread_runs_on_the_given_handle() {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(1)
            .enable_all()
            .build();
        let Ok(runtime) = runtime else {
            return;
        };
        let seam = LeaseRecorderSeam::default();
        let recording = Arc::new(Recording(Mutex::new(Vec::new())));
        seam.install(recording.clone(), None);
        seam.record_blocking(Some(runtime.handle()), &key(), worker());
        assert_eq!(seam.ledger().failures(), 0);
        assert_eq!(recording.0.lock().map_or(0, |s| s.len()), 1);
    }
}