footing 0.5.1

Feature-gated foundations for Rust backend services
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
use std::{
    collections::VecDeque,
    sync::{
        Arc, Mutex,
        atomic::{AtomicUsize, Ordering},
    },
    time::Duration,
};

use async_trait::async_trait;
use tokio::{
    sync::watch,
    time::{self, Instant},
};

use crate::{
    clock::{Clock, SharedClock},
    observability::{MetricRegistry, metric_names},
    result::{ClassifyError, ErrorKind},
};

use super::*;

struct TokioClock;

impl Clock for TokioClock {
    fn now(&self) -> std::time::Instant {
        Instant::now().into_std()
    }
}

struct Cancellation<'a>(&'a AtomicUsize, bool);

impl Drop for Cancellation<'_> {
    fn drop(&mut self) {
        if !self.1 {
            self.0.fetch_add(1, Ordering::SeqCst);
        }
    }
}

struct TimedWorker {
    steps: Mutex<VecDeque<Duration>>,
    starts: Mutex<Vec<Instant>>,
    cancellations: AtomicUsize,
    shutdown: ShutdownTrigger,
}

impl TimedWorker {
    fn new(steps: impl IntoIterator<Item = Duration>, shutdown: ShutdownTrigger) -> Self {
        Self {
            steps: Mutex::new(steps.into_iter().collect()),
            starts: Mutex::new(Vec::new()),
            cancellations: AtomicUsize::new(0),
            shutdown,
        }
    }
}

#[async_trait]
impl Worker for TimedWorker {
    fn name(&self) -> &str {
        "timed"
    }

    async fn run_once(&self, _: WorkerContext) -> Result<WorkerTick, BoxWorkerError> {
        self.starts.lock().unwrap().push(Instant::now());
        let (delay, last) = {
            let mut steps = self.steps.lock().unwrap();
            (
                steps.pop_front().expect("unexpected tick"),
                steps.is_empty(),
            )
        };
        let mut cancellation = Cancellation(&self.cancellations, false);
        time::sleep(delay).await;
        cancellation.1 = true;
        if last {
            self.shutdown.shutdown();
        }
        Ok(WorkerTick::new(1))
    }
}

async fn phase(status: &mut watch::Receiver<WorkerStatus>, expected: WorkerPhase) -> WorkerStatus {
    loop {
        let snapshot = *status.borrow_and_update();
        if snapshot.phase() == expected {
            return snapshot;
        }
        status.changed().await.expect("runner stopped unexpectedly");
    }
}

#[test]
fn tick_timeout_configuration_is_optional_validated_and_round_trips() {
    for json in ["{}", r#"{"tick_timeout":null}"#] {
        let settings: WorkerSettings = serde_json::from_str(json).unwrap();
        assert_eq!(settings.build().unwrap().tick_timeout(), None);
    }
    let settings: WorkerSettings = serde_json::from_str(r#"{"tick_timeout":"250ms"}"#).unwrap();
    let config = settings.build().unwrap();
    assert_eq!(config.tick_timeout(), Some(Duration::from_millis(250)));
    let wire = serde_json::to_value(WorkerSettings::from(config)).unwrap();
    assert_eq!(wire["tick_timeout"], "250ms");
    for timeout in [Duration::ZERO, Duration::MAX] {
        let error = WorkerSettings::default()
            .with_tick_timeout(Some(timeout))
            .build()
            .unwrap_err();
        assert!(
            error
                .issues()
                .iter()
                .any(|issue| issue.path() == "worker.tick_timeout")
        );
    }
    assert_eq!(WorkerTickTimeout.error_kind(), ErrorKind::Timeout);
    assert_eq!(WorkerTickTimeout.error_code(), "worker_tick_timed_out");
}

#[tokio::test(start_paused = true)]
async fn timed_out_tick_records_failure_and_retries_only_after_backoff() {
    let (trigger, shutdown) = shutdown_channel();
    let worker = Arc::new(TimedWorker::new(
        [Duration::from_secs(60), Duration::ZERO],
        trigger,
    ));
    let metrics = Arc::new(MetricRegistry::new());
    let wakeup = WorkerWakeup::new();
    let mut options = WorkerRunOptions::new()
        .with_clock(Arc::new(TokioClock))
        .with_metrics(Some(metrics.clone()))
        .with_wakeup(wakeup.clone());
    let mut status = options.subscribe_status();
    let started_at = Instant::now();
    let task = tokio::spawn(run_worker_until_shutdown_with_options(
        worker.clone(),
        WorkerSettings::default()
            .with_tick_timeout(Some(Duration::from_secs(2)))
            .with_failure_backoff(BackoffSettings::new(
                Duration::from_secs(5),
                Duration::from_secs(5),
                1,
            ))
            .build()
            .unwrap(),
        shutdown,
        options,
    ));
    let failed = phase(&mut status, WorkerPhase::Backoff).await;
    assert_eq!(Instant::now() - started_at, Duration::from_secs(2));
    assert_eq!(
        failed.last_tick_finished_at(),
        Some(Instant::now().into_std())
    );
    assert_eq!(failed.last_tick_outcome(), Some(WorkerTickOutcome::Failure));
    assert_eq!(worker.cancellations.load(Ordering::SeqCst), 1);
    wakeup.notify();
    time::advance(Duration::from_secs(4)).await;
    tokio::task::yield_now().await;
    assert_eq!(worker.starts.lock().unwrap().len(), 1);
    assert_eq!(status.borrow().phase(), WorkerPhase::Backoff);
    time::advance(Duration::from_secs(1)).await;
    let summary = task.await.unwrap().unwrap();
    assert_eq!(summary.successes(), 1);
    assert_eq!(summary.failures(), 1);
    let starts = worker.starts.lock().unwrap();
    assert_eq!(starts[1] - starts[0], Duration::from_secs(7));
    let snapshot = metrics.snapshot().unwrap();
    let failures = snapshot
        .samples()
        .iter()
        .find(|sample| {
            sample.name() == metric_names::WORKER_TICKS_TOTAL
                && sample
                    .labels()
                    .iter()
                    .any(|(key, value)| key == "outcome" && value == "failure")
        })
        .unwrap();
    assert_eq!(failures.value(), 1.0);
}

#[tokio::test(start_paused = true)]
async fn default_keeps_long_ticks_unbounded() {
    let (trigger, shutdown) = shutdown_channel();
    let worker = Arc::new(TimedWorker::new([Duration::from_secs(60)], trigger));
    let started_at = Instant::now();
    let summary = run_worker_until_shutdown(worker.clone(), WorkerConfig::default(), shutdown)
        .await
        .unwrap();
    assert_eq!(Instant::now() - started_at, Duration::from_secs(60));
    assert_eq!(summary.successes(), 1);
    assert_eq!(worker.cancellations.load(Ordering::SeqCst), 0);
}

#[tokio::test(start_paused = true)]
async fn shutdown_and_tick_deadlines_use_the_earlier_deadline() {
    for tick_seconds in [2, 10] {
        let (trigger, shutdown) = shutdown_channel();
        let worker = Arc::new(TimedWorker::new([Duration::from_secs(60)], trigger.clone()));
        let mut options = WorkerRunOptions::new();
        let mut status = options.subscribe_status();
        let started_at = Instant::now();
        let task = tokio::spawn(run_worker_until_shutdown_with_options(
            worker.clone(),
            WorkerSettings::default()
                .with_tick_timeout(Some(Duration::from_secs(tick_seconds)))
                .with_shutdown_timeout(Duration::from_secs(5))
                .build()
                .unwrap(),
            shutdown,
            options,
        ));
        phase(&mut status, WorkerPhase::Running).await;
        trigger.shutdown();
        let result = task.await.unwrap();
        assert_eq!(
            Instant::now() - started_at,
            Duration::from_secs(tick_seconds.min(5))
        );
        if tick_seconds < 5 {
            assert_eq!(result.unwrap().failures(), 1);
            assert_eq!(status.borrow().phase(), WorkerPhase::Stopped);
            assert_eq!(
                status.borrow().last_tick_outcome(),
                Some(WorkerTickOutcome::Failure)
            );
        } else {
            assert!(matches!(result, Err(WorkerRunError::ShutdownTimedOut)));
            assert_eq!(status.borrow().phase(), WorkerPhase::Failed);
            assert_eq!(status.borrow().last_tick_outcome(), None);
        }
        assert_eq!(worker.starts.lock().unwrap().len(), 1);
        assert_eq!(worker.cancellations.load(Ordering::SeqCst), 1);
    }
}

#[derive(Clone, Copy)]
enum Renewal {
    Succeed,
    Hang,
    FailAfter(Duration),
    LoseAfter(Duration),
}

struct LeaseStore {
    local: LocalWorkerLeaseStore,
    renewal: Renewal,
    acquire_delay: Duration,
    renewals: AtomicUsize,
    cancelled_renewals: AtomicUsize,
    releases: AtomicUsize,
}

impl LeaseStore {
    fn new(clock: SharedClock, renewal: Renewal) -> Self {
        Self {
            local: LocalWorkerLeaseStore::with_clock(clock),
            renewal,
            acquire_delay: Duration::ZERO,
            renewals: AtomicUsize::new(0),
            cancelled_renewals: AtomicUsize::new(0),
            releases: AtomicUsize::new(0),
        }
    }
}

#[async_trait]
impl WorkerLeaseStore for LeaseStore {
    async fn try_acquire(
        &self,
        worker: &str,
        owner: &str,
        config: WorkerLeaseConfig,
    ) -> Result<Option<WorkerLease>, WorkerLeaseError> {
        time::sleep(self.acquire_delay).await;
        self.local.try_acquire(worker, owner, config).await
    }

    async fn renew(
        &self,
        lease: &WorkerLease,
        config: WorkerLeaseConfig,
    ) -> Result<Option<WorkerLease>, WorkerLeaseError> {
        self.renewals.fetch_add(1, Ordering::SeqCst);
        let mut cancellation = Cancellation(&self.cancelled_renewals, false);
        let result = match self.renewal {
            Renewal::Succeed => self.local.renew(lease, config).await,
            Renewal::Hang => std::future::pending().await,
            Renewal::FailAfter(delay) => {
                time::sleep(delay).await;
                Err(WorkerLeaseError::Store(
                    std::io::Error::other("renewal failed").into(),
                ))
            }
            Renewal::LoseAfter(delay) => {
                time::sleep(delay).await;
                Ok(None)
            }
        };
        cancellation.1 = true;
        result
    }

    async fn release(&self, lease: &WorkerLease) -> Result<(), WorkerLeaseError> {
        self.releases.fetch_add(1, Ordering::SeqCst);
        self.local.release(lease).await
    }
}

fn leased_timeout(timeout: u64) -> WorkerConfig {
    WorkerSettings::default()
        .with_tick_timeout(Some(Duration::from_secs(timeout)))
        .with_lease(Some(WorkerLeaseSettings::new(
            Duration::from_secs(10),
            Duration::from_secs(6),
            Duration::from_secs(5),
        )))
        .build()
        .unwrap()
}

#[tokio::test(start_paused = true)]
async fn tick_deadline_keeps_renewals_live_and_cancels_pending_renewal_before_shutdown_release() {
    for (renewal, timeout) in [(Renewal::Succeed, 11), (Renewal::Hang, 5)] {
        let clock = Arc::new(TokioClock);
        let store = Arc::new(LeaseStore::new(clock.clone(), renewal));
        let (trigger, shutdown) = shutdown_channel();
        let worker = Arc::new(TimedWorker::new([Duration::from_secs(60)], trigger.clone()));
        let mut options = WorkerRunOptions::new()
            .with_clock(clock)
            .with_lease_store("owner", store.clone());
        let mut status = options.subscribe_status();
        let started_at = Instant::now();
        let task = tokio::spawn(run_worker_until_shutdown_with_options(
            worker.clone(),
            leased_timeout(timeout),
            shutdown,
            options,
        ));
        phase(&mut status, WorkerPhase::Backoff).await;
        assert_eq!(Instant::now() - started_at, Duration::from_secs(timeout));
        assert_eq!(worker.cancellations.load(Ordering::SeqCst), 1);
        if matches!(renewal, Renewal::Hang) {
            assert_eq!(store.renewals.load(Ordering::SeqCst), 1);
            assert_eq!(store.cancelled_renewals.load(Ordering::SeqCst), 1);
        } else {
            assert_eq!(store.renewals.load(Ordering::SeqCst), 2);
        }
        assert_eq!(store.releases.load(Ordering::SeqCst), 0);
        trigger.shutdown();
        assert_eq!(task.await.unwrap().unwrap().failures(), 1);
        assert_eq!(store.releases.load(Ordering::SeqCst), 1);
        assert_eq!(worker.starts.lock().unwrap().len(), 1);
    }
}

#[tokio::test(start_paused = true)]
async fn lease_failures_keep_priority_and_do_not_release_an_uncertain_active_lease() {
    for (renewal, timeout, elapsed) in [
        (Renewal::FailAfter(Duration::from_secs(1)), 5, 5),
        (Renewal::LoseAfter(Duration::from_secs(1)), 5, 5),
        (Renewal::Hang, 12, 9),
    ] {
        let clock = Arc::new(TokioClock);
        let store = Arc::new(LeaseStore::new(clock.clone(), renewal));
        let (trigger, shutdown) = shutdown_channel();
        let worker = Arc::new(TimedWorker::new([Duration::from_secs(60)], trigger));
        let mut options = WorkerRunOptions::new()
            .with_clock(clock)
            .with_lease_store("owner", store.clone());
        let status = options.subscribe_status();
        let started_at = Instant::now();
        let result = run_worker_until_shutdown_with_options(
            worker.clone(),
            leased_timeout(timeout),
            shutdown,
            options,
        )
        .await;
        match renewal {
            Renewal::FailAfter(_) => assert!(matches!(result, Err(WorkerRunError::Lease(_)))),
            Renewal::LoseAfter(_) => assert!(matches!(result, Err(WorkerRunError::LeaseLost))),
            Renewal::Hang => assert!(matches!(
                result,
                Err(WorkerRunError::LeaseOperationTimedOut)
            )),
            Renewal::Succeed => unreachable!(),
        }
        assert_eq!(Instant::now() - started_at, Duration::from_secs(elapsed));
        assert_eq!(store.releases.load(Ordering::SeqCst), 0);
        assert_eq!(worker.cancellations.load(Ordering::SeqCst), 1);
        assert_eq!(status.borrow().phase(), WorkerPhase::Failed);
        assert_eq!(status.borrow().last_tick_outcome(), None);
    }
}

#[tokio::test(start_paused = true)]
async fn delayed_renewal_cannot_keep_a_tick_running_past_lease_expiry() {
    let clock = Arc::new(TokioClock);
    let store = Arc::new(LeaseStore::new(clock.clone(), Renewal::Hang));
    let (trigger, shutdown) = shutdown_channel();
    let worker = Arc::new(TimedWorker::new([Duration::from_secs(60)], trigger));
    let mut options = WorkerRunOptions::new()
        .with_clock(clock)
        .with_lease_store("owner", store.clone());
    let mut status = options.subscribe_status();
    let task = tokio::spawn(run_worker_until_shutdown_with_options(
        worker.clone(),
        leased_timeout(30),
        shutdown,
        options,
    ));
    phase(&mut status, WorkerPhase::Running).await;

    // Simulate a busy executor that cannot poll the renewal timer until only
    // two seconds remain in the ten-second lease. The storage timeout is five.
    time::advance(Duration::from_secs(8)).await;
    tokio::task::yield_now().await;
    assert_eq!(store.renewals.load(Ordering::SeqCst), 1);
    time::advance(Duration::from_secs(2)).await;
    tokio::task::yield_now().await;

    assert!(task.is_finished(), "the lease expiry must bound renewal");
    assert!(matches!(
        task.await.unwrap(),
        Err(WorkerRunError::LeaseLost)
    ));
    assert_eq!(worker.cancellations.load(Ordering::SeqCst), 1);
    assert_eq!(store.cancelled_renewals.load(Ordering::SeqCst), 1);
    assert_eq!(store.releases.load(Ordering::SeqCst), 0);
    assert_eq!(status.borrow().phase(), WorkerPhase::Failed);
}

#[tokio::test(start_paused = true)]
async fn expired_lease_takes_priority_over_a_ready_tick() {
    let clock = Arc::new(TokioClock);
    let store = Arc::new(LeaseStore::new(clock.clone(), Renewal::Hang));
    let (trigger, shutdown) = shutdown_channel();
    let worker = Arc::new(TimedWorker::new([Duration::from_secs(10)], trigger));
    let mut options = WorkerRunOptions::new()
        .with_clock(clock)
        .with_lease_store("owner", store.clone());
    let mut status = options.subscribe_status();
    let task = tokio::spawn(run_worker_until_shutdown_with_options(
        worker.clone(),
        leased_timeout(30),
        shutdown,
        options,
    ));
    phase(&mut status, WorkerPhase::Running).await;

    // Both the tick and the overdue renewal become ready after this stall.
    // Resuming the tick would execute its final effects under an expired lease.
    time::advance(Duration::from_secs(10)).await;
    assert!(matches!(
        task.await.unwrap(),
        Err(WorkerRunError::LeaseLost)
    ));
    assert_eq!(worker.cancellations.load(Ordering::SeqCst), 1);
    assert_eq!(store.renewals.load(Ordering::SeqCst), 0);
    assert_eq!(store.releases.load(Ordering::SeqCst), 0);
    assert_eq!(status.borrow().last_tick_outcome(), None);
}

#[tokio::test(start_paused = true)]
async fn startup_and_lease_acquisition_do_not_consume_the_tick_budget() {
    let clock = Arc::new(TokioClock);
    let mut store = LeaseStore::new(clock.clone(), Renewal::Succeed);
    store.acquire_delay = Duration::from_secs(3);
    let (trigger, shutdown) = shutdown_channel();
    let worker = Arc::new(TimedWorker::new([Duration::from_secs(1)], trigger));
    let started_at = Instant::now();
    let config = WorkerSettings::from(leased_timeout(2))
        .with_startup_delay(Duration::from_secs(4))
        .build()
        .unwrap();
    let summary = run_worker_until_shutdown_with_options(
        worker.clone(),
        config,
        shutdown,
        WorkerRunOptions::new()
            .with_clock(clock)
            .with_lease_store("owner", Arc::new(store)),
    )
    .await
    .unwrap();
    assert_eq!(Instant::now() - started_at, Duration::from_secs(8));
    assert_eq!(summary.successes(), 1);
    assert_eq!(worker.cancellations.load(Ordering::SeqCst), 0);
}