anyback 0.5.0

Backup and restore for Anytype
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
use std::{
    ffi::OsString,
    future::Future,
    sync::{
        Arc,
        atomic::{AtomicU8, Ordering},
    },
    time::Duration,
};

use anyhow::{Result, bail};
use anytype::process_watcher::ProcessWatcherTimeouts;
use tokio::time::Instant;

const WORKFLOW_DEFAULT: Duration = Duration::from_hours(1);
const WORKFLOW_MAXIMUM: Duration = Duration::from_hours(2);
const EVENT_CONNECT_MAXIMUM: Duration = Duration::from_mins(2);
const PROCESS_START_MAXIMUM: Duration = Duration::from_mins(5);
const PROCESS_IDLE_MAXIMUM: Duration = Duration::from_mins(5);
const PROCESS_DONE_MAXIMUM: Duration = Duration::from_hours(1);

/// One absolute backup or restore workflow deadline and its validated watcher limits.
#[derive(Clone, Copy, Debug)]
pub struct WorkflowDeadline {
    expires_at: Option<Instant>,
    configured: Duration,
    process_timeouts: ProcessWatcherTimeouts,
}

/// Caller-owned authority for one publication commit.
///
/// Preparation workers never receive this value, so a worker detached by a
/// timeout cannot change a caller-visible destination.
#[derive(Clone, Debug)]
pub(super) struct PublicationCommit {
    expires_at: Option<Instant>,
    configured: Duration,
    timeout_message: &'static str,
    state: Arc<AtomicU8>,
}

impl PublicationCommit {
    pub(super) fn ensure_remaining(&self) -> Result<()> {
        if self
            .expires_at
            .is_some_and(|deadline| Instant::now() >= deadline)
        {
            bail!(
                "{} after {} seconds",
                self.timeout_message,
                self.configured.as_secs()
            );
        }
        Ok(())
    }

    pub(super) fn commit<T>(self, operation: impl FnOnce() -> Result<T>) -> Result<T> {
        if self
            .expires_at
            .is_some_and(|deadline| Instant::now() >= deadline)
        {
            let _ = self
                .state
                .compare_exchange(0, 2, Ordering::AcqRel, Ordering::Acquire);
        }
        if self
            .state
            .compare_exchange(0, 1, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            bail!(
                "{} after {} seconds",
                self.timeout_message,
                self.configured.as_secs()
            );
        }
        let result = operation();
        self.state.store(3, Ordering::Release);
        result
    }
}

impl WorkflowDeadline {
    /// Reads and validates all backup timeout environment variables.
    ///
    /// Call this constructor before client construction so invalid
    /// configuration cannot precede network or filesystem mutation.
    pub fn from_env() -> Result<Self> {
        Self::from_lookup(|name| std::env::var_os(name))
    }

    fn from_lookup(mut lookup: impl FnMut(&str) -> Option<OsString>) -> Result<Self> {
        let workflow = parse_timeout_value(
            "ANYBACK_WORKFLOW_TIMEOUT_SECS",
            lookup("ANYBACK_WORKFLOW_TIMEOUT_SECS"),
            WORKFLOW_DEFAULT,
            WORKFLOW_MAXIMUM,
            true,
        )?;
        let defaults = ProcessWatcherTimeouts::default();
        let process_timeouts = ProcessWatcherTimeouts {
            event_stream_connect_timeout: parse_required_timeout_value(
                "ANYBACK_EVENT_STREAM_CONNECT_TIMEOUT",
                lookup("ANYBACK_EVENT_STREAM_CONNECT_TIMEOUT"),
                defaults.event_stream_connect_timeout,
                EVENT_CONNECT_MAXIMUM,
            )?,
            process_start_timeout: parse_required_timeout_value(
                "ANYBACK_PROCESS_START_TIMEOUT",
                lookup("ANYBACK_PROCESS_START_TIMEOUT"),
                defaults.process_start_timeout,
                PROCESS_START_MAXIMUM,
            )?,
            process_idle_timeout: parse_required_timeout_value(
                "ANYBACK_PROCESS_IDLE_TIMEOUT",
                lookup("ANYBACK_PROCESS_IDLE_TIMEOUT"),
                defaults.process_idle_timeout,
                PROCESS_IDLE_MAXIMUM,
            )?,
            process_done_timeout: parse_required_timeout_value(
                "ANYBACK_PROCESS_DONE_TIMEOUT",
                lookup("ANYBACK_PROCESS_DONE_TIMEOUT"),
                defaults.process_done_timeout,
                PROCESS_DONE_MAXIMUM,
            )?,
        };
        let expires_at = workflow
            .map(|duration| {
                Instant::now().checked_add(duration).ok_or_else(|| {
                    anyhow::anyhow!(
                        "ANYBACK_WORKFLOW_TIMEOUT_SECS cannot be represented as a deadline"
                    )
                })
            })
            .transpose()?;
        Ok(Self {
            expires_at,
            configured: workflow.unwrap_or(Duration::ZERO),
            process_timeouts,
        })
    }

    pub(super) fn local_command() -> Self {
        Self {
            expires_at: None,
            configured: Duration::ZERO,
            process_timeouts: ProcessWatcherTimeouts::default(),
        }
    }

    #[cfg(test)]
    pub(super) fn new(
        workflow: Option<Duration>,
        process_timeouts: ProcessWatcherTimeouts,
    ) -> Self {
        Self {
            expires_at: workflow.and_then(|duration| Instant::now().checked_add(duration)),
            configured: workflow.unwrap_or(Duration::ZERO),
            process_timeouts,
        }
    }

    pub(super) fn ensure_read_remaining(self) -> Result<()> {
        if self.expired() {
            bail!("backup workflow timed out; read was aborted");
        }
        Ok(())
    }

    pub(super) fn ensure_mutation_remaining(self) -> Result<()> {
        if self.expired() {
            bail!("restore workflow timed out; mutation outcome is indeterminate");
        }
        Ok(())
    }

    pub(super) fn ensure_restore_preflight_remaining(self) -> Result<()> {
        if self.expired() {
            bail!("restore workflow timed out before mutation dispatch");
        }
        Ok(())
    }

    pub(super) async fn run_read<F, T>(self, future: F) -> Result<T>
    where
        F: Future<Output = T>,
    {
        self.run("backup workflow timed out; read was aborted", future)
            .await
    }

    pub(super) async fn run_export<F, T>(self, future: F) -> Result<T>
    where
        F: Future<Output = T>,
    {
        self.run(
            "backup workflow timed out; read was aborted and a server-side export artifact may exist",
            future,
        )
        .await
    }

    pub(super) async fn run_mutation<F, T>(self, future: F) -> Result<T>
    where
        F: Future<Output = T>,
    {
        self.run(
            "restore workflow timed out; mutation outcome is indeterminate",
            future,
        )
        .await
    }

    pub(super) async fn run_restore_preflight<F, T>(self, future: F) -> Result<T>
    where
        F: Future<Output = T>,
    {
        self.run(
            "restore workflow timed out before mutation dispatch",
            future,
        )
        .await
    }

    pub(super) async fn run_read_publication<P, T, Prepare, Commit>(
        self,
        timeout_message: &'static str,
        prepare: Prepare,
        commit: Commit,
    ) -> Result<T>
    where
        Prepare: FnOnce() -> Result<P> + Send + 'static,
        P: Send + 'static,
        Commit: FnOnce(P, PublicationCommit) -> Result<T>,
    {
        self.run_publication(timeout_message, prepare, commit).await
    }

    pub(super) async fn run_mutation_publication<P, T, Prepare, Commit>(
        self,
        prepare: Prepare,
        commit: Commit,
    ) -> Result<T>
    where
        Prepare: FnOnce() -> Result<P> + Send + 'static,
        P: Send + 'static,
        Commit: FnOnce(P, PublicationCommit) -> Result<T>,
    {
        self.run_publication(
            "restore workflow timed out; mutation outcome is indeterminate",
            prepare,
            commit,
        )
        .await
    }

    pub(super) fn process_timeouts(self) -> Result<ProcessWatcherTimeouts> {
        let Some(remaining) = self.remaining() else {
            return Ok(self.process_timeouts);
        };
        if remaining.is_zero() {
            bail!("restore workflow timed out before mutation dispatch");
        }
        Ok(ProcessWatcherTimeouts {
            event_stream_connect_timeout: self
                .process_timeouts
                .event_stream_connect_timeout
                .min(remaining),
            process_start_timeout: self.process_timeouts.process_start_timeout.min(remaining),
            process_idle_timeout: self.process_timeouts.process_idle_timeout.min(remaining),
            process_done_timeout: self.process_timeouts.process_done_timeout.min(remaining),
        })
    }

    fn expired(self) -> bool {
        self.expires_at
            .is_some_and(|deadline| Instant::now() >= deadline)
    }

    fn remaining(self) -> Option<Duration> {
        self.expires_at
            .map(|deadline| deadline.saturating_duration_since(Instant::now()))
    }

    async fn run<F, T>(self, message: &str, future: F) -> Result<T>
    where
        F: Future<Output = T>,
    {
        let Some(deadline) = self.expires_at else {
            return Ok(future.await);
        };
        if Instant::now() >= deadline {
            bail!("{message} after {} seconds", self.configured.as_secs());
        }
        tokio::time::timeout_at(deadline, future)
            .await
            .map_err(|_| anyhow::anyhow!("{message} after {} seconds", self.configured.as_secs()))
    }

    async fn run_publication<P, T, Prepare, Commit>(
        self,
        timeout_message: &'static str,
        prepare: Prepare,
        commit: Commit,
    ) -> Result<T>
    where
        Prepare: FnOnce() -> Result<P> + Send + 'static,
        P: Send + 'static,
        Commit: FnOnce(P, PublicationCommit) -> Result<T>,
    {
        let state = Arc::new(AtomicU8::new(0));
        let authority = PublicationCommit {
            expires_at: self.expires_at,
            configured: self.configured,
            timeout_message,
            state: Arc::clone(&state),
        };
        authority.ensure_remaining()?;
        let task = tokio::task::spawn_blocking(prepare);
        let joined = match self.expires_at {
            Some(deadline) => {
                if let Ok(joined) = tokio::time::timeout_at(deadline, task).await {
                    joined
                } else {
                    let _ = state.compare_exchange(0, 2, Ordering::AcqRel, Ordering::Acquire);
                    bail!(
                        "{timeout_message} after {} seconds",
                        self.configured.as_secs()
                    );
                }
            }
            None => task.await,
        };
        let prepared = joined.map_err(|_| anyhow::anyhow!("local publication worker failed"))??;
        authority.ensure_remaining()?;
        commit(prepared, authority)
    }
}

fn parse_required_timeout_value(
    name: &str,
    raw: Option<OsString>,
    default: Duration,
    maximum: Duration,
) -> Result<Duration> {
    parse_timeout_value(name, raw, default, maximum, false)?
        .ok_or_else(|| anyhow::anyhow!("{name} cannot disable its process safety boundary"))
}

fn parse_timeout_value(
    name: &str,
    raw: Option<OsString>,
    default: Duration,
    maximum: Duration,
    zero_disables: bool,
) -> Result<Option<Duration>> {
    let Some(raw) = raw else {
        return Ok(Some(default));
    };
    let raw = raw
        .into_string()
        .map_err(|_| anyhow::anyhow!("{name} must be valid Unicode ASCII decimal"))?;
    if raw == "0" && zero_disables {
        return Ok(None);
    }
    if raw.is_empty() || raw.starts_with('0') || !raw.bytes().all(|byte| byte.is_ascii_digit()) {
        bail!("{name} must use canonical ASCII decimal seconds");
    }
    let seconds = raw
        .parse::<u64>()
        .map_err(|_| anyhow::anyhow!("{name} is outside the supported range"))?;
    if seconds == 0 || seconds > maximum.as_secs() {
        bail!(
            "{name} must be between 1 and {} seconds{}",
            maximum.as_secs(),
            if zero_disables {
                ", or exactly 0 to disable the outer workflow deadline"
            } else {
                ""
            }
        );
    }
    Ok(Some(Duration::from_secs(seconds)))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn timeout_values_use_canonical_bounded_decimal_grammar() {
        let default = Duration::from_secs(30);
        let maximum = Duration::from_mins(1);
        assert_eq!(
            parse_timeout_value("TEST", None, default, maximum, true).expect("default"),
            Some(default)
        );
        assert_eq!(
            parse_timeout_value("TEST", Some("60".into()), default, maximum, true)
                .expect("maximum"),
            Some(maximum)
        );
        assert_eq!(
            parse_timeout_value("TEST", Some("0".into()), default, maximum, true)
                .expect("disabled"),
            None
        );
        for invalid in ["", "00", "01", "+1", "-1", " 1", "1 ", "1.0", "61"] {
            assert!(
                parse_timeout_value("TEST", Some(invalid.into()), default, maximum, true).is_err(),
                "accepted invalid value {invalid:?}"
            );
        }
        assert!(parse_timeout_value("TEST", Some("0".into()), default, maximum, false).is_err());
        for (name, maximum) in [
            ("ANYBACK_WORKFLOW_TIMEOUT_SECS", 7200),
            ("ANYBACK_EVENT_STREAM_CONNECT_TIMEOUT", 120),
            ("ANYBACK_PROCESS_START_TIMEOUT", 300),
            ("ANYBACK_PROCESS_IDLE_TIMEOUT", 300),
            ("ANYBACK_PROCESS_DONE_TIMEOUT", 3600),
        ] {
            let above = maximum + 1;
            assert!(
                parse_timeout_value(
                    name,
                    Some(above.to_string().into()),
                    Duration::from_secs(1),
                    Duration::from_secs(maximum),
                    name == "ANYBACK_WORKFLOW_TIMEOUT_SECS",
                )
                .is_err(),
                "accepted over-maximum value for {name}"
            );
        }
    }

    #[cfg(unix)]
    #[test]
    fn timeout_values_reject_non_unicode() {
        use std::os::unix::ffi::OsStringExt as _;

        assert!(
            parse_timeout_value(
                "TEST",
                Some(OsString::from_vec(vec![0xff])),
                Duration::from_secs(1),
                Duration::from_secs(2),
                false,
            )
            .is_err()
        );
    }

    #[test]
    fn watcher_limits_are_clamped_to_the_outer_remaining_budget() {
        let deadline = WorkflowDeadline::new(
            Some(Duration::from_secs(1)),
            ProcessWatcherTimeouts {
                event_stream_connect_timeout: Duration::from_secs(10),
                process_start_timeout: Duration::from_secs(20),
                process_idle_timeout: Duration::from_secs(30),
                process_done_timeout: Duration::from_secs(40),
            },
        );
        let timeouts = deadline.process_timeouts().expect("remaining budget");
        assert!(timeouts.event_stream_connect_timeout <= Duration::from_secs(1));
        assert!(timeouts.process_start_timeout <= Duration::from_secs(1));
        assert!(timeouts.process_idle_timeout <= Duration::from_secs(1));
        assert!(timeouts.process_done_timeout <= Duration::from_secs(1));
    }

    #[test]
    fn invalid_timeout_configuration_prevents_later_side_effects() {
        let side_effect_ran = std::cell::Cell::new(false);
        let configured = WorkflowDeadline::from_lookup(|name| {
            (name == "ANYBACK_PROCESS_IDLE_TIMEOUT").then(|| OsString::from("301"))
        });
        let result = configured.map(|_| {
            side_effect_ran.set(true);
        });
        assert!(result.is_err());
        assert!(!side_effect_ran.get());
    }

    #[tokio::test(start_paused = true)]
    async fn expired_process_configuration_is_pre_dispatch() {
        let deadline = WorkflowDeadline::new(
            Some(Duration::from_secs(1)),
            ProcessWatcherTimeouts::default(),
        );
        tokio::time::advance(Duration::from_secs(1)).await;
        let error = deadline
            .process_timeouts()
            .expect_err("expired workflow must reject subscription")
            .to_string();
        assert!(error.contains("before mutation dispatch"));
        assert!(!error.contains("indeterminate"));
    }

    // Paused time keeps the second wait from completing in the same coarse
    // timer tick as the deadline (Windows timers), which `timeout_at` would
    // report as success.
    #[tokio::test(start_paused = true)]
    async fn one_absolute_deadline_is_not_reset_between_waits() {
        let deadline = WorkflowDeadline::new(
            Some(Duration::from_millis(50)),
            ProcessWatcherTimeouts::default(),
        );
        deadline
            .run_read(tokio::time::sleep(Duration::from_millis(30)))
            .await
            .expect("first phase");
        assert!(
            deadline
                .run_read(tokio::time::sleep(Duration::from_millis(30)))
                .await
                .is_err()
        );
    }
}