aws-durable-execution-sdk 1.0.0

Rust SDK for AWS Lambda Durable Functions
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
//! A recording plugin for the call-site tests of the user-function hooks.
//!
//! [`UfHooks::plugins`] registers two plugins, `p0` and `p1`, over one
//! log. Each records every operation, attempt and user-function hook call
//! and each enter and exit of its `wrap_user_function` (the records S, W
//! and E of the issue's fixtures), with what its probe returns then.

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use super::{
    AttemptEndInfo, AttemptInfo, HookWork, InvocationInfo, InvocationPlugins, OperationInfo,
    Plugin, PluginFactory,
};
use crate::error::WireError;
use crate::{BoxError, OperationStatus};

/// One hook or wrap call that [`UfHooks`] recorded.
#[derive(Clone, Debug)]
pub(crate) struct UfRecord {
    /// `p0` or `p1`.
    pub(crate) plugin: &'static str,
    /// `start`, `attempt_start`, `uf_start`, `wrap_enter`, `wrap_exit`,
    /// `uf_end`, `attempt_end` or `end`.
    pub(crate) hook: &'static str,
    pub(crate) name: Option<String>,
    /// The info's `id()`.
    pub(crate) id: String,
    /// The info's `sub_type()`.
    pub(crate) sub_type: Option<String>,
    /// The info's `parent_id()`.
    pub(crate) parent_id: Option<String>,
    /// The info's `is_virtual()`.
    pub(crate) is_virtual: bool,
    pub(crate) replay: bool,
    pub(crate) status: Option<OperationStatus>,
    /// The info's `attempt()`.
    pub(crate) info_attempt: Option<u32>,
    /// An attempt hook's attempt number.
    pub(crate) attempt: Option<u32>,
    pub(crate) has_start_timestamp: bool,
    /// The info's `result()`.
    pub(crate) result: Option<String>,
    /// `uf_end`: whether the outcome is `Ok`.
    pub(crate) ok: Option<bool>,
    /// `uf_end`: the outcome's wire; `attempt_end`: its error.
    pub(crate) wire: Option<WireError>,
    /// The address of the info, for `std::ptr::eq` checks.
    pub(crate) info_addr: usize,
    /// The info's `Debug` form, for equality checks.
    pub(crate) info: String,
    /// What the probe returned when the hook ran.
    pub(crate) probe: String,
}

type Probe = dyn Fn() -> String + Send + Sync;

/// Whether `hook` is a user-function hook or wrap record.
fn is_user_function(hook: &str) -> bool {
    matches!(hook, "uf_start" | "wrap_enter" | "wrap_exit" | "uf_end")
}

/// A root context over `records` whose transport is a fresh in-memory
/// client, with `hooks`' `p0` and `p1` as the invocation's plugins and no
/// checkpoint buffer window.
pub(crate) fn context(
    hooks: &UfHooks,
    records: Vec<(String, crate::engine::CheckpointRecord)>,
) -> (
    crate::DurableContext,
    Arc<crate::client::InMemoryExecutionClient>,
) {
    let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
    let ctx = context_over(
        hooks,
        records,
        Arc::clone(&client) as Arc<dyn crate::client::ExecutionClient>,
    );
    (ctx, client)
}

/// A root context over `records` and `client`, with `hooks`' `p0` and
/// `p1` as the invocation's plugins and no checkpoint buffer window.
pub(crate) fn context_over(
    hooks: &UfHooks,
    records: Vec<(String, crate::engine::CheckpointRecord)>,
    client: Arc<dyn crate::client::ExecutionClient>,
) -> crate::DurableContext {
    crate::DurableContext::new_root_with_client_and_defaults(
        "arn:test".to_owned(),
        lambda_runtime::Context::default(),
        Arc::new(crate::engine::CheckpointLog::from_records(records)),
        client,
        "token0".to_owned(),
        std::time::Duration::ZERO,
        0,
        Some(hooks.plugins()),
    )
}

/// The first update named `name` with `action` that `updates` holds.
pub(crate) fn update_of(
    updates: &[aws_sdk_lambda::types::OperationUpdate],
    name: &str,
    action: &aws_sdk_lambda::types::OperationAction,
) -> Option<aws_sdk_lambda::types::OperationUpdate> {
    updates
        .iter()
        .find(|u| u.name() == Some(name) && u.action() == action)
        .cloned()
}

/// `(type, message)` of a wire error.
pub(crate) fn type_and_message(wire: &WireError) -> (Option<String>, Option<String>) {
    (
        wire.error_type().map(str::to_owned),
        wire.error_message().map(str::to_owned),
    )
}

/// `(type, message)` of the error object of `update`.
pub(crate) fn update_error(
    update: &aws_sdk_lambda::types::OperationUpdate,
) -> (Option<String>, Option<String>) {
    let error = update.error().expect("the update carries an error object");
    (
        error.error_type().map(str::to_owned),
        error.error_message().map(str::to_owned),
    )
}

/// The recording plugin. Clones share the log.
#[derive(Clone)]
pub(crate) struct UfHooks {
    tag: &'static str,
    records: Arc<Mutex<Vec<UfRecord>>>,
    probe: Option<Arc<Probe>>,
}

impl Default for UfHooks {
    fn default() -> Self {
        Self {
            tag: "p0",
            records: Arc::default(),
            probe: None,
        }
    }
}

impl UfHooks {
    /// A recorder whose records carry what `probe` returns at each call.
    pub(crate) fn probing(probe: impl Fn() -> String + Send + Sync + 'static) -> Self {
        Self {
            probe: Some(Arc::new(probe)),
            ..Self::default()
        }
    }

    /// The plugins of an invocation whose plugins are `p0` and `p1`, both
    /// recording into this log.
    pub(crate) fn plugins(&self) -> Arc<InvocationPlugins> {
        let factories: Vec<(&'static str, Arc<dyn PluginFactory>)> = ["p0", "p1"]
            .into_iter()
            .map(|tag| {
                let factory: Arc<dyn PluginFactory> = Arc::new(Self {
                    tag,
                    ..self.clone()
                });
                ("test::UfHooks", factory)
            })
            .collect();
        super::create_plugins(&factories, None, info).expect("two factories are registered")
    }

    /// Every record, in call order.
    pub(crate) fn records(&self) -> Vec<UfRecord> {
        self.records.lock().unwrap().clone()
    }

    /// `p0`'s records of the operation named `name`. Asserts that `p1`
    /// recorded the same hooks for it.
    pub(crate) fn of(&self, name: &str) -> Vec<UfRecord> {
        let records = self.records();
        let of = |tag: &str| -> Vec<UfRecord> {
            records
                .iter()
                .filter(|r| r.plugin == tag && r.name.as_deref() == Some(name))
                .cloned()
                .collect()
        };
        let p0 = of("p0");
        let hooks = |records: &[UfRecord]| records.iter().map(|r| r.hook).collect::<Vec<_>>();
        assert_eq!(hooks(&p0), hooks(&of("p1")), "p1's hooks of {name}");
        p0
    }

    /// The hooks of [`Self::of`].
    pub(crate) fn hooks_of(&self, name: &str) -> Vec<&'static str> {
        self.of(name).into_iter().map(|r| r.hook).collect()
    }

    /// `p0`'s first `hook` record of `name`.
    #[expect(clippy::panic, reason = "a missing record fails the test")]
    pub(crate) fn find(&self, name: &str, hook: &str) -> UfRecord {
        self.of(name)
            .into_iter()
            .find(|r| r.hook == hook)
            .unwrap_or_else(|| panic!("no {hook} record of {name}"))
    }

    /// `p0`'s user-function records of `name`: its start hook, a wrap enter
    /// and exit per poll, and its end hook when `ended`. Asserts that
    /// shape.
    pub(crate) fn user_function_of(&self, name: &str, ended: bool) -> Vec<UfRecord> {
        let records: Vec<UfRecord> = self
            .of(name)
            .into_iter()
            .filter(|r| is_user_function(r.hook))
            .collect();
        let hooks: Vec<&str> = records.iter().map(|r| r.hook).collect();
        let wraps_end = if ended {
            hooks.len().saturating_sub(1)
        } else {
            hooks.len()
        };
        let wraps = hooks.get(1..wraps_end).unwrap_or_default();
        assert_eq!(hooks.first(), Some(&"uf_start"), "{name}: {hooks:?}");
        assert_eq!(
            hooks.last() == Some(&"uf_end"),
            ended,
            "{name} ended: {hooks:?}"
        );
        assert!(
            !wraps.is_empty()
                && wraps
                    .chunks(2)
                    .all(|pair| pair == ["wrap_enter", "wrap_exit"]),
            "{name}: {hooks:?}"
        );
        records
    }

    /// Whether any user-function hook or wrap ran.
    pub(crate) fn any_user_function_record(&self) -> bool {
        self.records().iter().any(|r| is_user_function(r.hook))
    }

    fn push(
        &self,
        hook: &'static str,
        info: &OperationInfo,
        attempt: Option<u32>,
        outcome: Option<Result<(), &WireError>>,
        error: Option<&WireError>,
    ) {
        let probe = self.probe.as_ref().map(|probe| probe()).unwrap_or_default();
        let wire = match outcome {
            Some(Err(wire)) => Some(wire.clone()),
            _ => error.cloned(),
        };
        self.records.lock().unwrap().push(UfRecord {
            plugin: self.tag,
            hook,
            name: info.name().map(str::to_owned),
            id: info.id().to_owned(),
            sub_type: info.sub_type().map(str::to_owned),
            parent_id: info.parent_id().map(str::to_owned),
            is_virtual: info.is_virtual(),
            replay: info.is_replay(),
            status: info.status(),
            info_attempt: info.attempt(),
            attempt,
            has_start_timestamp: info.start_timestamp().is_some(),
            result: info.result().map(str::to_owned),
            ok: outcome.map(|o| o.is_ok()),
            wire,
            info_addr: std::ptr::from_ref(info) as usize,
            info: format!("{info:?}"),
            probe,
        });
    }
}

impl Plugin for UfHooks {
    fn name(&self) -> &'static str {
        self.tag
    }

    fn on_operation_start(&self, info: &OperationInfo) -> HookWork {
        self.push("start", info, None, None, None);
        Ok(None)
    }

    fn on_operation_end(&self, info: &OperationInfo) -> HookWork {
        self.push("end", info, None, None, None);
        Ok(None)
    }

    fn on_attempt_start(&self, info: &AttemptInfo) -> HookWork {
        self.push(
            "attempt_start",
            info.operation(),
            Some(info.attempt()),
            None,
            None,
        );
        Ok(None)
    }

    fn on_attempt_end(&self, info: &AttemptEndInfo) -> HookWork {
        self.push(
            "attempt_end",
            info.operation(),
            Some(info.attempt()),
            None,
            info.error(),
        );
        Ok(None)
    }

    fn wrap_user_function(&self, info: &OperationInfo, next: &mut dyn FnMut()) {
        self.push("wrap_enter", info, None, None, None);
        next();
        self.push("wrap_exit", info, None, None, None);
    }

    fn on_user_function_start(&self, info: &OperationInfo) -> HookWork {
        self.push("uf_start", info, None, None, None);
        Ok(None)
    }

    fn on_user_function_end(
        &self,
        info: &OperationInfo,
        outcome: Result<(), &WireError>,
    ) -> HookWork {
        self.push("uf_end", info, None, Some(outcome), None);
        Ok(None)
    }
}

impl PluginFactory for UfHooks {
    fn create_plugin(&self, _info: &InvocationInfo) -> Result<Option<Box<dyn Plugin>>, BoxError> {
        Ok(Some(Box::new(self.clone())))
    }
}

/// The info of a first invocation with no operations.
fn info() -> InvocationInfo {
    InvocationInfo::new(
        "r".to_owned(),
        "arn".to_owned(),
        "{}".to_owned(),
        std::collections::BTreeMap::new(),
        Vec::new(),
        None,
        true,
        None,
    )
}

type OnDisplay = dyn Fn() + Send + Sync;

/// Dc: an error whose `Display` counts its calls and runs `on_display`
/// at each.
#[derive(Clone)]
pub(crate) struct Counted {
    calls: Arc<AtomicUsize>,
    on_display: Option<Arc<OnDisplay>>,
}

impl Counted {
    pub(crate) fn new() -> Self {
        Self {
            calls: Arc::default(),
            on_display: None,
        }
    }

    /// A Dc that runs `on_display` at each `Display` call.
    pub(crate) fn calling(on_display: impl Fn() + Send + Sync + 'static) -> Self {
        Self {
            on_display: Some(Arc::new(on_display)),
            ..Self::new()
        }
    }

    /// A Dc that shares this one's count and runs `on_display` at each
    /// `Display` call.
    pub(crate) fn sharing(&self, on_display: impl Fn() + Send + Sync + 'static) -> Self {
        Self {
            calls: Arc::clone(&self.calls),
            on_display: Some(Arc::new(on_display)),
        }
    }

    /// The `Display` calls so far.
    pub(crate) fn calls(&self) -> usize {
        self.calls.load(Ordering::SeqCst)
    }

    /// A boxed clone, as a body returns it.
    pub(crate) fn boxed(&self) -> BoxError {
        Box::new(self.clone())
    }
}

impl std::fmt::Debug for Counted {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Counted")
    }
}

impl std::fmt::Display for Counted {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.calls.fetch_add(1, Ordering::SeqCst);
        if let Some(on_display) = &self.on_display {
            on_display();
        }
        f.write_str("counted")
    }
}

impl std::error::Error for Counted {}