acta-core 0.1.0-alpha.1

Append-only columnar file format for time-series data
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
//! Point-in-time recovery inspection and explicit incomplete-tail repair.

use std::fs::{File, OpenOptions};
use std::io;
use std::path::Path;

use crate::error::{Error, ErrorContext, Result};
use crate::format::scan::FileScan;
use crate::limits::Limits;
use crate::lock::acquire_writer_lock;

/// The action a recovery inspection recommends.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RecoveryAction {
    /// The file ends at a complete, structurally validated frame boundary.
    None,
    /// The file ends inside one final data frame and may be truncated safely.
    TruncateIncompleteTail,
}

/// A read-only, point-in-time recovery decision for an Acta file.
///
/// Inspection captures the file length when it opens the file and validates
/// only that snapshot. The plan must not be treated as an authorization to
/// truncate later: [`repair_incomplete_tail`] independently opens, locks, and
/// rescans the current file before changing anything.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RecoveryPlan {
    action: RecoveryAction,
    file_size: u64,
    last_good_offset: u64,
    bytes_to_remove: u64,
}

impl RecoveryPlan {
    /// The action supported by this plan.
    pub fn action(&self) -> RecoveryAction {
        self.action
    }

    /// The file length observed by inspection.
    pub fn file_size(&self) -> u64 {
        self.file_size
    }

    /// The byte after the last complete, validated frame.
    pub fn last_good_offset(&self) -> u64 {
        self.last_good_offset
    }

    /// The number of bytes a matching repair would remove.
    pub fn bytes_to_remove(&self) -> u64 {
        self.bytes_to_remove
    }

    /// Whether the snapshot contains a repairable incomplete data-frame tail.
    pub fn requires_repair(&self) -> bool {
        self.action == RecoveryAction::TruncateIncompleteTail
    }
}

/// The result of successfully repairing one incomplete final data frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RecoverySummary {
    original_file_size: u64,
    repaired_file_size: u64,
    bytes_removed: u64,
}

impl RecoverySummary {
    /// The file length observed before the repair.
    pub fn original_file_size(&self) -> u64 {
        self.original_file_size
    }

    /// The file length after truncation and post-repair validation.
    pub fn repaired_file_size(&self) -> u64 {
        self.repaired_file_size
    }

    /// The number of bytes removed from the incomplete tail.
    pub fn bytes_removed(&self) -> u64 {
        self.bytes_removed
    }
}

/// Inspect recovery state without opening the file for writing.
///
/// This is a point-in-time snapshot. It never acquires the writer lock and
/// never changes file bytes. A plan that requests repair is only a description
/// of the snapshot; the repair operation independently locks and revalidates
/// the current file.
///
/// The scan is the structural, whole-frame pass
/// [`Writer::open`](crate::Writer::open) performs rather than
/// [`ValidationLevel::Full`](crate::ValidationLevel::Full), so a plan reporting
/// a complete file says the frames and their chains are intact, not that every
/// stream decodes.
pub fn inspect_recovery<P: AsRef<Path>>(path: P) -> Result<RecoveryPlan> {
    inspect_recovery_with_limits(path, Limits::default())
}

/// Inspect recovery state under caller-supplied structural limits.
pub fn inspect_recovery_with_limits<P: AsRef<Path>>(
    path: P,
    limits: Limits,
) -> Result<RecoveryPlan> {
    let scan = FileScan::open(path.as_ref(), limits)?;
    let (_file, plan) = discover(scan)?;
    Ok(plan)
}

/// Repair a verified incomplete final data frame.
///
/// Repair is destructive but narrowly bounded: it can remove only the bytes
/// after the last complete frame found by a strict structural scan. It refuses
/// complete corruption, an incomplete schema frame, and an already complete
/// file. The current file is opened read/write, locked before discovery,
/// rescanned on that same handle, and scanned once more after truncation. Both
/// scans are the structural, whole-frame pass
/// [`Writer::open`](crate::Writer::open) performs rather than
/// [`ValidationLevel::Full`](crate::ValidationLevel::Full): no stream is
/// decoded and no statistic is verified.
///
/// # Writer exclusion and its limits
///
/// This takes the same cooperative exclusive lock as
/// [`Writer::create`](crate::Writer::create), with the same scope and the same
/// limits; see that method. Those limits matter more here than they do for an
/// append, because this operation deletes bytes. The lock is a convention among
/// `acta` writers rather than part of the format, so it constrains neither
/// another implementation nor a process that simply opens the path, and it is
/// unreliable on network filesystems. The physical length is rechecked
/// immediately before the truncation and a length that moved is refused, but no
/// portable API makes that check and the truncation a single atomic step: a
/// writer that does not participate in the lock can still commit a frame inside
/// that window and lose it.
///
/// # Failure after truncation
///
/// Every failure before the truncation leaves the file byte for byte as it was,
/// and every one of them says so. A failure can also follow the truncation —
/// the synchronization, the rescan, or the post-repair validation — and every
/// one of those instead says the tail was already removed, whatever its
/// [`ErrorKind`](crate::ErrorKind). The two sets of messages never overlap, so
/// a caller can always tell which side of the mutation it is on. After a
/// post-truncation error the file may already be shorter while its durability
/// or its structure is unconfirmed.
pub fn repair_incomplete_tail<P: AsRef<Path>>(path: P) -> Result<RecoverySummary> {
    repair_incomplete_tail_with_limits(path, Limits::default())
}

/// Repair a verified incomplete final data frame under structural `limits`.
pub fn repair_incomplete_tail_with_limits<P: AsRef<Path>>(
    path: P,
    limits: Limits,
) -> Result<RecoverySummary> {
    let file = OpenOptions::new()
        .read(true)
        .write(true)
        .open(path.as_ref())
        .map_err(|error| Error::io(error, None).with_context(ErrorContext::File))?;
    acquire_writer_lock(&file)?;
    repair_locked_file(file, limits, &FileRepairOperations)
}

/// The file operations recovery performs around its one mutation.
///
/// Naming them lets a test report a length that moved underneath a plan, or
/// truncate to the wrong length, or fail either call, which is the only way to
/// reach those paths without a disk that misbehaves. Production always acts on
/// the locked [`File`].
trait RepairOperations {
    fn physical_length(&self, file: &File) -> io::Result<u64>;
    fn set_len(&self, file: &File, length: u64) -> io::Result<()>;
    fn sync(&self, file: &File) -> io::Result<()>;
}

struct FileRepairOperations;

impl RepairOperations for FileRepairOperations {
    fn physical_length(&self, file: &File) -> io::Result<u64> {
        file.metadata().map(|metadata| metadata.len())
    }

    fn set_len(&self, file: &File, length: u64) -> io::Result<()> {
        file.set_len(length)
    }

    fn sync(&self, file: &File) -> io::Result<()> {
        file.sync_all()
    }
}

fn discover(mut scan: FileScan) -> Result<(File, RecoveryPlan)> {
    let walk = scan.walk_data_frames(|_frame, _block| Ok(()))?;
    let plan = RecoveryPlan::from_walk(
        scan.file_size(),
        walk.last_good_offset,
        walk.incomplete_tail,
    )?;
    Ok((scan.into_file(), plan))
}

fn repair_locked_file(
    file: File,
    limits: Limits,
    operations: &dyn RepairOperations,
) -> Result<RecoverySummary> {
    let scan = FileScan::from_file(file, limits)?;
    let (file, plan) = discover(scan)?;
    let current_size = operations
        .physical_length(&file)
        .map_err(|error| Error::io(error, None).with_context(ErrorContext::File))?;
    if current_size != plan.file_size {
        return Err(Error::io(
            io::Error::other("the file changed while recovery was being inspected"),
            Some(current_size),
        )
        .with_context(ErrorContext::File));
    }
    if !plan.requires_repair() {
        return Err(Error::invalid_argument(
            "the file is complete; there is no incomplete tail to repair",
        )
        .with_context(ErrorContext::File));
    }

    // The truncation is the point the file's state changes, so the failure on
    // either side of it says which side it is on. A caller that cannot tell
    // them apart cannot know whether the committed bytes it had a moment ago
    // are still all there.
    operations
        .set_len(&file, plan.last_good_offset)
        .map_err(|error| {
            Error::io(error, Some(plan.last_good_offset))
                .with_message_prefix(BEFORE_TRUNCATION)
                .with_context(ErrorContext::File)
        })?;

    // Synchronization is not the only step left — the file is rescanned and
    // revalidated after it — and any of those can fail on a disk that stops
    // cooperating or against a writer that ignored the lock. They all report a
    // file that is already shorter, so they all carry the same marker.
    finish_repair(file, limits, &plan, operations)
        .map_err(|error| error.with_message_prefix(AFTER_TRUNCATION))
}

/// The marker every failure that leaves the file untouched carries.
const BEFORE_TRUNCATION: &str = "the incomplete tail was not removed and the file is unchanged";

/// The marker every failure after the truncation carries, whatever its kind.
const AFTER_TRUNCATION: &str =
    "the incomplete tail was already removed, so the file may already be shorter than it was";

/// Everything after the one mutation: synchronize, rescan, and validate.
fn finish_repair(
    file: File,
    limits: Limits,
    plan: &RecoveryPlan,
    operations: &dyn RepairOperations,
) -> Result<RecoverySummary> {
    operations.sync(&file).map_err(|error| {
        Error::io(error, Some(plan.last_good_offset))
            .with_message_prefix("the repaired file could not be synchronized")
            .with_context(ErrorContext::File)
    })?;

    let post_scan = FileScan::from_file(file, limits)?;
    let (file, post_plan) = discover(post_scan)?;
    if post_plan.action != RecoveryAction::None
        || post_plan.file_size != plan.last_good_offset
        || post_plan.last_good_offset != plan.last_good_offset
    {
        return Err(Error::corruption(
            "post-repair validation did not produce the expected complete file",
            Some(plan.last_good_offset),
        )
        .with_context(ErrorContext::File));
    }
    drop(file);

    let bytes_removed = plan
        .file_size
        .checked_sub(post_plan.file_size)
        .ok_or_else(|| {
            Error::corruption(
                "repaired file grew beyond its original size",
                Some(post_plan.file_size),
            )
            .with_context(ErrorContext::File)
        })?;
    Ok(RecoverySummary {
        original_file_size: plan.file_size,
        repaired_file_size: post_plan.file_size,
        bytes_removed,
    })
}

impl RecoveryPlan {
    fn from_walk(file_size: u64, last_good_offset: u64, incomplete_tail: bool) -> Result<Self> {
        let bytes_to_remove = if incomplete_tail {
            if last_good_offset >= file_size {
                return Err(Error::corruption(
                    "an incomplete tail does not extend beyond the last complete frame",
                    Some(last_good_offset),
                )
                .with_context(ErrorContext::File));
            }
            file_size.checked_sub(last_good_offset).ok_or_else(|| {
                Error::corruption(
                    "the last complete frame is beyond the captured file extent",
                    Some(last_good_offset),
                )
                .with_context(ErrorContext::File)
            })?
        } else {
            if last_good_offset != file_size {
                return Err(Error::corruption(
                    "a complete recovery walk did not reach the file extent",
                    Some(last_good_offset),
                )
                .with_context(ErrorContext::File));
            }
            0
        };
        let action = if incomplete_tail {
            RecoveryAction::TruncateIncompleteTail
        } else {
            RecoveryAction::None
        };
        Ok(Self {
            action,
            file_size,
            last_good_offset,
            bytes_to_remove,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::sync::atomic::{AtomicU64, Ordering};

    /// Operations that behave normally except where a test asks otherwise.
    #[derive(Default)]
    struct FailingOperations {
        /// Reported instead of the real length, to move the file underneath a
        /// plan that has already been discovered.
        length: Option<u64>,
        fail_length: bool,
        fail_set_len: bool,
        fail_sync: bool,
        /// Added to the verified target, to truncate somewhere the plan did not
        /// authorize.
        length_delta: i64,
    }

    impl RepairOperations for FailingOperations {
        fn physical_length(&self, file: &File) -> io::Result<u64> {
            if self.fail_length {
                return Err(io::Error::other("injected metadata failure"));
            }
            match self.length {
                Some(length) => Ok(length),
                None => file.metadata().map(|metadata| metadata.len()),
            }
        }

        fn set_len(&self, file: &File, length: u64) -> io::Result<()> {
            if self.fail_set_len {
                return Err(io::Error::other("injected set_len failure"));
            }
            file.set_len(length.wrapping_add(self.length_delta as u64))
        }

        fn sync(&self, file: &File) -> io::Result<()> {
            if self.fail_sync {
                return Err(io::Error::other("injected sync failure"));
            }
            file.sync_all()
        }
    }

    #[test]
    fn plan_requires_removal_only_for_an_incomplete_data_tail() {
        let complete = RecoveryPlan::from_walk(10, 10, false).unwrap();
        assert_eq!(complete.action(), RecoveryAction::None);
        assert_eq!(complete.bytes_to_remove(), 0);

        let incomplete = RecoveryPlan::from_walk(14, 10, true).unwrap();
        assert_eq!(incomplete.action(), RecoveryAction::TruncateIncompleteTail);
        assert!(incomplete.requires_repair());
        assert_eq!(incomplete.bytes_to_remove(), 4);
    }

    #[test]
    fn injected_set_len_failure_preserves_the_file() {
        let fixture = Fixture::incomplete();
        let error = fixture.repair(FailingOperations {
            fail_set_len: true,
            ..FailingOperations::default()
        });
        assert_eq!(error.kind(), crate::ErrorKind::Io);
        assert!(error.message().starts_with(BEFORE_TRUNCATION), "{error}");
        assert!(!error.message().contains(AFTER_TRUNCATION), "{error}");
        assert_eq!(fixture.length(), fixture.original_size);
        assert_eq!(fixture.bytes(), fixture.original_bytes);
    }

    #[test]
    fn injected_sync_failure_does_not_claim_durability() {
        let fixture = Fixture::incomplete();
        let error = fixture.repair(FailingOperations {
            fail_sync: true,
            ..FailingOperations::default()
        });
        assert_eq!(error.kind(), crate::ErrorKind::Io);
        // The failures on either side of the truncation leave opposite states,
        // so a caller has to be able to tell them apart from the error alone.
        assert!(error.message().starts_with(AFTER_TRUNCATION), "{error}");
        assert!(!error.message().contains(BEFORE_TRUNCATION), "{error}");
        assert!(fixture.length() < fixture.original_size);
    }

    #[test]
    fn a_length_that_moved_under_the_plan_refuses_before_truncating() {
        for reported in [0, 1, u64::MAX] {
            let fixture = Fixture::incomplete();
            let error = fixture.repair(FailingOperations {
                length: Some(reported),
                ..FailingOperations::default()
            });
            assert_eq!(error.kind(), crate::ErrorKind::Io);
            assert!(error.message().contains("changed"), "{error}");
            assert_eq!(fixture.bytes(), fixture.original_bytes);
        }
    }

    #[test]
    fn an_unreadable_length_refuses_before_truncating() {
        let fixture = Fixture::incomplete();
        let error = fixture.repair(FailingOperations {
            fail_length: true,
            ..FailingOperations::default()
        });
        assert_eq!(error.kind(), crate::ErrorKind::Io);
        assert_eq!(fixture.bytes(), fixture.original_bytes);
    }

    /// The last guard before success is reported: a truncation that lands
    /// anywhere but the verified boundary must not be called a repair.
    ///
    /// This failure is a `Corruption`, not the `Io` a failed synchronization
    /// produces, and it still arrives after the file has been shortened, so it
    /// has to carry the same marker: the distinction a caller needs is which
    /// side of the mutation the failure is on, not which kind it is.
    #[test]
    fn a_wrong_truncation_target_fails_post_repair_validation() {
        for delta in [-64_i64, -8, -1, 1, 8, 64] {
            let fixture = Fixture::incomplete();
            let error = fixture.repair(FailingOperations {
                length_delta: delta,
                ..FailingOperations::default()
            });
            assert_eq!(error.kind(), crate::ErrorKind::Corruption, "delta {delta}");
            assert!(
                error.message().starts_with(AFTER_TRUNCATION),
                "delta {delta}: {error}"
            );
            assert!(!error.message().contains(BEFORE_TRUNCATION), "{error}");
        }
    }

    /// A file that removes itself, so a failing assertion cannot leave one
    /// behind for the next run to collide with.
    struct Fixture {
        path: PathBuf,
        original_bytes: Vec<u8>,
        original_size: u64,
    }

    impl Fixture {
        /// The reference fixture with one byte of an unfinished frame after it.
        fn incomplete() -> Self {
            static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0);

            let mut bytes = include_bytes!("../spec/v0.2/fixtures/minimal/minimal.acta").to_vec();
            bytes.push(0);
            let path = std::env::temp_dir().join(format!(
                "acta-recovery-unit-{}-{}.acta",
                std::process::id(),
                NEXT_FIXTURE.fetch_add(1, Ordering::Relaxed)
            ));
            let _ = std::fs::remove_file(&path);
            std::fs::write(&path, &bytes).unwrap();
            Self {
                path,
                original_size: bytes.len() as u64,
                original_bytes: bytes,
            }
        }

        /// Run the production repair over a locked handle and expect a failure.
        fn repair(&self, operations: FailingOperations) -> Error {
            let file = OpenOptions::new()
                .read(true)
                .write(true)
                .open(&self.path)
                .unwrap();
            acquire_writer_lock(&file).unwrap();
            match repair_locked_file(file, Limits::default(), &operations) {
                Ok(summary) => panic!("repair unexpectedly succeeded: {summary:?}"),
                Err(error) => error,
            }
        }

        fn length(&self) -> u64 {
            std::fs::metadata(&self.path).unwrap().len()
        }

        fn bytes(&self) -> Vec<u8> {
            std::fs::read(&self.path).unwrap()
        }
    }

    impl Drop for Fixture {
        fn drop(&mut self) {
            let _ = std::fs::remove_file(&self.path);
        }
    }
}