aion-core 0.29.0

Pure domain model and shared vocabulary for Aion durable workflows.
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
//! Which worker holds a run's open attempts, folded from its own history.
//!
//! Exactly like [`crate::WorkflowStatus`] and [`crate::current_step`], the
//! answer is a PROJECTION of the authoritative event history. There is one
//! transition function, [`apply_lease_transition`], and every reader uses it:
//! the whole-history fold ([`outstanding_leases`]) and the engine's
//! per-append visibility touch apply the same function to the same events,
//! so a stored row and a summary built from history cannot disagree about
//! who is working on what.
//!
//! # The fold, precisely
//!
//! Only the CURRENT lease segment counts — everything after the last
//! [`Event::WorkflowStarted`] or [`Event::WorkflowReopened`]. A reopen
//! supersedes the terminals of the activities it names and re-dispatches
//! them, so a lease recorded before it belongs to a delivery that is no
//! longer the one in flight; the next delivery records its own.
//!
//! - [`Event::ActivityLeased`] for `(activity, attempt)` REPLACES any earlier
//!   lease of the same attempt and moves it to the end of the list. The
//!   transport is at-least-once, so the same attempt can be delivered twice;
//!   the LAST lease is the worker that holds it now.
//! - `ActivityCompleted` / `ActivityFailed` / `ActivityCancelled` for
//!   `(activity, attempt)` remove that attempt's lease. There is no
//!   activity-level timeout event; an attempt that times out is recorded as
//!   `ActivityFailed`.
//! - Every other event leaves the list as it was — including
//!   [`Event::ActivityStarted`], which is recorded at DISPATCH, before any
//!   worker holds the work (WA-010 §2.5), and
//!   [`Event::ActivityAdoptionOffered`], which re-offers an attempt the
//!   worker may still hold.
//!
//! `current_worker` is the worker of the LAST outstanding lease: the attempt
//! most recently taken. When two attempts are outstanding and the later one
//! terminates, the earlier one is shown again — it is still being worked.
//!
//! # What "absent" means
//!
//! An empty list (and a `None` current worker) is UNATTRIBUTED, and that word
//! covers two different facts a reader must not conflate: a history recorded
//! before lease events existed, and an install that knew the worker and
//! failed to record it. History alone cannot tell them apart, which is why
//! every read surface also carries the install's
//! [`crate::ReadProvenance::lease_record_failures_total`].

use serde::{Deserialize, Serialize};

use crate::{ActivityId, Event, WorkerAttribution};

/// One attempt a worker holds: the LAST lease recorded for it with no
/// terminal since.
#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq, Eq)]
pub struct OutstandingLease {
    /// The activity ordinal the attempt belongs to.
    pub activity_id: ActivityId,
    /// One-based attempt number — the same `attempt` its `ActivityStarted`
    /// and `ActivityLeased` carry.
    pub attempt: u32,
    /// The worker that holds it, by the durable names its lease recorded.
    pub worker: WorkerAttribution,
}

/// Applies ONE event to a list of outstanding leases, in place.
///
/// This is the single transition every reader shares — see the module
/// documentation for the rules. Events that say nothing about leases leave
/// the list untouched. Callers folding a whole history are responsible for
/// resetting the list at a segment boundary; this function does not, so the
/// engine's per-append touch (which re-projects the whole row on lifecycle
/// events) can apply it to a stored row event by event.
pub fn apply_lease_transition(leases: &mut Vec<OutstandingLease>, event: &Event) {
    match event {
        Event::ActivityLeased {
            activity_id,
            attempt,
            worker,
            ..
        } => {
            leases
                .retain(|lease| !(lease.activity_id == *activity_id && lease.attempt == *attempt));
            leases.push(OutstandingLease {
                activity_id: activity_id.clone(),
                attempt: *attempt,
                worker: worker.clone(),
            });
        }
        Event::ActivityCompleted {
            activity_id,
            attempt,
            ..
        }
        | Event::ActivityFailed {
            activity_id,
            attempt,
            ..
        }
        | Event::ActivityCancelled {
            activity_id,
            attempt,
            ..
        } => {
            leases
                .retain(|lease| !(lease.activity_id == *activity_id && lease.attempt == *attempt));
        }
        _ => {}
    }
}

/// Every attempt a worker currently holds in `events`' current lease
/// segment, in the order the leases were recorded (earliest first).
#[must_use]
pub fn outstanding_leases(events: &[Event]) -> Vec<OutstandingLease> {
    let segment_start = events
        .iter()
        .rposition(|event| {
            matches!(
                event,
                Event::WorkflowStarted { .. } | Event::WorkflowReopened { .. }
            )
        })
        .map_or(0, |index| index + 1);
    let mut leases = Vec::new();
    for event in &events[segment_start..] {
        apply_lease_transition(&mut leases, event);
    }
    leases
}

/// What a run's WHOLE history says about lease recording, as counts a
/// reader can hold an unattributed attempt against (WA-010 R4 follow-up).
///
/// A console over a windowed history cannot tell "no lease in view" from
/// "no lease ever", and no fold can tell "recorded before lease events
/// existed" from "never recorded" — the history carries the package version a
/// run started under, not the server version. So the server states the two
/// things it can count over the full history, and the reader says "no lease
/// recorded across N dispatched attempts" rather than guessing why.
#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct LeaseRecording {
    /// `ActivityLeased` events in the whole history.
    pub leases_recorded: u64,
    /// `ActivityStarted` events in the whole history — every dispatched
    /// attempt, whether or not a lease was recorded for it.
    pub dispatched_attempts: u64,
    /// Distinct `(activity, attempt)` keys with at least one lease. The
    /// transport is at-least-once, so `leases_recorded` can exceed this;
    /// "every attempt was attributed" is `attempts_with_a_lease ==
    /// dispatched_attempts`, never a comparison against `leases_recorded`.
    pub attempts_with_a_lease: u64,
}

/// Counts leases and dispatched attempts over the WHOLE history, every
/// segment — a statement of the record, not of the current segment.
#[must_use]
pub fn lease_recording(events: &[Event]) -> LeaseRecording {
    let mut counts = LeaseRecording::default();
    let mut attributed: std::collections::HashSet<(&ActivityId, u32)> =
        std::collections::HashSet::new();
    for event in events {
        match event {
            Event::ActivityLeased {
                activity_id,
                attempt,
                ..
            } => {
                counts.leases_recorded += 1;
                if attributed.insert((activity_id, *attempt)) {
                    counts.attempts_with_a_lease += 1;
                }
            }
            Event::ActivityStarted { .. } => counts.dispatched_attempts += 1,
            _ => {}
        }
    }
    counts
}

/// The worker holding the run's most recently leased open attempt, or `None`
/// when the run is UNATTRIBUTED (no outstanding lease — see the module
/// documentation for what that does and does not mean).
#[must_use]
pub fn current_worker(leases: &[OutstandingLease]) -> Option<WorkerAttribution> {
    leases.last().map(|lease| lease.worker.clone())
}

#[cfg(test)]
mod tests {
    use chrono::{DateTime, Utc};

    use super::{OutstandingLease, apply_lease_transition, current_worker, outstanding_leases};
    use crate::{
        ActivityError, ActivityErrorKind, ActivityId, Event, EventEnvelope, Payload, RunId,
        WorkerAttribution, WorkerTransport, WorkflowId,
    };

    fn workflow_id() -> WorkflowId {
        WorkflowId::new(uuid::Uuid::from_u128(7))
    }

    fn envelope(seq: u64) -> EventEnvelope {
        EventEnvelope {
            seq,
            recorded_at: DateTime::<Utc>::from_timestamp(
                1_700_000_000 + i64::try_from(seq).unwrap_or(0),
                0,
            )
            .unwrap_or_default(),
            workflow_id: workflow_id(),
        }
    }

    fn worker(identity: &str) -> WorkerAttribution {
        WorkerAttribution {
            identity: identity.to_owned(),
            task_queue: String::from("billing"),
            node: Some(String::from("n1")),
            deployment: None,
            instance_id: None,
            transport: WorkerTransport::Grpc,
        }
    }

    fn started(seq: u64) -> Result<Event, Box<dyn std::error::Error>> {
        Ok(Event::WorkflowStarted {
            envelope: envelope(seq),
            workflow_type: String::from("checkout"),
            input: Payload::from_json(&serde_json::json!({}))?,
            run_id: RunId::new(uuid::Uuid::from_u128(1)),
            parent_run_id: None,
            parent_workflow_id: None,
            package_version: crate::PackageVersion::new("a".repeat(64)),
        })
    }

    fn leased(seq: u64, activity: u64, attempt: u32, identity: &str) -> Event {
        Event::ActivityLeased {
            envelope: envelope(seq),
            activity_id: ActivityId::from_sequence_position(activity),
            attempt,
            worker: worker(identity),
        }
    }

    fn completed(
        seq: u64,
        activity: u64,
        attempt: u32,
    ) -> Result<Event, Box<dyn std::error::Error>> {
        Ok(Event::ActivityCompleted {
            envelope: envelope(seq),
            activity_id: ActivityId::from_sequence_position(activity),
            attempt,
            result: Payload::from_json(&serde_json::json!({}))?,
        })
    }

    fn failed(seq: u64, activity: u64, attempt: u32) -> Event {
        Event::ActivityFailed {
            envelope: envelope(seq),
            activity_id: ActivityId::from_sequence_position(activity),
            attempt,
            error: ActivityError {
                kind: ActivityErrorKind::Terminal,
                message: String::from("boom"),
                details: None,
            },
        }
    }

    fn identities(leases: &[OutstandingLease]) -> Vec<(u64, u32, &str)> {
        leases
            .iter()
            .map(|lease| {
                (
                    lease.activity_id.sequence_position(),
                    lease.attempt,
                    lease.worker.identity.as_str(),
                )
            })
            .collect()
    }

    #[test]
    fn a_lease_sets_the_current_worker_and_its_terminal_clears_it()
    -> Result<(), Box<dyn std::error::Error>> {
        let mut history = vec![started(1)?, leased(2, 2, 1, "w-a")];
        let leases = outstanding_leases(&history);
        assert_eq!(identities(&leases), vec![(2, 1, "w-a")]);
        assert_eq!(
            current_worker(&leases).map(|w| w.identity),
            Some(String::from("w-a"))
        );

        history.push(completed(3, 2, 1)?);
        let leases = outstanding_leases(&history);
        assert!(
            leases.is_empty(),
            "the completion clears the attempt's lease"
        );
        assert_eq!(current_worker(&leases), None);
        Ok(())
    }

    #[test]
    fn a_second_lease_of_the_same_attempt_replaces_the_first()
    -> Result<(), Box<dyn std::error::Error>> {
        let history = vec![started(1)?, leased(2, 2, 1, "w-a"), leased(3, 2, 1, "w-b")];
        let leases = outstanding_leases(&history);
        assert_eq!(
            identities(&leases),
            vec![(2, 1, "w-b")],
            "last lease per attempt wins"
        );
        Ok(())
    }

    #[test]
    fn a_terminal_for_another_attempt_leaves_the_lease_standing()
    -> Result<(), Box<dyn std::error::Error>> {
        // Attempt 1 failed after attempt 2 was leased (a late terminal from a
        // retried delivery): attempt 2's lease is untouched.
        let history = vec![started(1)?, leased(2, 2, 2, "w-b"), failed(3, 2, 1)];
        assert_eq!(
            identities(&outstanding_leases(&history)),
            vec![(2, 2, "w-b")]
        );
        Ok(())
    }

    #[test]
    fn when_the_latest_attempt_terminates_the_earlier_outstanding_one_is_shown()
    -> Result<(), Box<dyn std::error::Error>> {
        let history = vec![
            started(1)?,
            leased(2, 2, 1, "w-a"),
            leased(3, 3, 1, "w-b"),
            completed(4, 3, 1)?,
        ];
        let leases = outstanding_leases(&history);
        assert_eq!(identities(&leases), vec![(2, 1, "w-a")]);
        assert_eq!(
            current_worker(&leases).map(|w| w.identity),
            Some(String::from("w-a"))
        );
        Ok(())
    }

    #[test]
    fn a_history_with_no_lease_is_unattributed() -> Result<(), Box<dyn std::error::Error>> {
        let history = vec![started(1)?];
        assert!(outstanding_leases(&history).is_empty());
        assert_eq!(current_worker(&[]), None);
        Ok(())
    }

    #[test]
    fn lease_recording_counts_the_whole_history_across_segments()
    -> Result<(), Box<dyn std::error::Error>> {
        let started_attempt = |seq: u64, activity: u64| Event::ActivityStarted {
            envelope: envelope(seq),
            activity_id: ActivityId::from_sequence_position(activity),
            attempt: 1,
        };
        let history = vec![
            started(1)?,
            started_attempt(2, 2),
            leased(3, 2, 1, "w-a"),
            Event::WorkflowReopened {
                envelope: envelope(4),
                run_id: RunId::new(uuid::Uuid::from_u128(1)),
                reopened: vec![ActivityId::from_sequence_position(2)],
            },
            started_attempt(5, 2),
        ];
        let counts = super::lease_recording(&history);
        assert_eq!(
            counts.leases_recorded, 1,
            "the lease before the reopen still counts"
        );
        assert_eq!(counts.dispatched_attempts, 2, "both dispatches count");
        assert_eq!(counts.attempts_with_a_lease, 1);
        // At-least-once: a second lease for the SAME attempt is a lease
        // recorded, not another attempt attributed.
        let mut redelivered = history.clone();
        redelivered.push(leased(6, 2, 1, "w-b"));
        let counts = super::lease_recording(&redelivered);
        assert_eq!(
            (counts.leases_recorded, counts.attempts_with_a_lease),
            (2, 1)
        );
        assert!(
            super::outstanding_leases(&history).is_empty(),
            "while the current segment's attribution is empty"
        );
        Ok(())
    }

    #[test]
    fn a_reopen_starts_a_fresh_segment() -> Result<(), Box<dyn std::error::Error>> {
        let history = vec![
            started(1)?,
            leased(2, 2, 1, "w-a"),
            Event::WorkflowReopened {
                envelope: envelope(3),
                run_id: RunId::new(uuid::Uuid::from_u128(1)),
                reopened: vec![ActivityId::from_sequence_position(2)],
            },
        ];
        assert!(
            outstanding_leases(&history).is_empty(),
            "a lease recorded before the reopen belongs to a superseded delivery"
        );
        Ok(())
    }

    #[test]
    fn the_incremental_transition_matches_the_fold() -> Result<(), Box<dyn std::error::Error>> {
        let history = vec![
            started(1)?,
            leased(2, 2, 1, "w-a"),
            leased(3, 3, 1, "w-b"),
            leased(4, 2, 1, "w-c"),
            failed(5, 3, 1),
        ];
        let mut incremental = Vec::new();
        for event in &history[1..] {
            apply_lease_transition(&mut incremental, event);
        }
        assert_eq!(incremental, outstanding_leases(&history));
        assert_eq!(identities(&incremental), vec![(2, 1, "w-c")]);
        Ok(())
    }
}