hyperfile_cleaner/lib.rs
1//! `hyperfile-cleaner` enumerates and removes unreferenced
2//! segments from a [hyperfile](https://crates.io/crates/hyperfile).
3//!
4//! ## Model
5//!
6//! A hyperfile is stored on S3 as a single inode object plus a
7//! sequence of segment objects. Each segment is keyed by a
8//! monotonically increasing `SegmentId` and contains both the
9//! data blocks written in that flush and the metadata B+tree
10//! nodes that index them.
11//!
12//! Because hyperfile is copy-on-write, segments accumulate as
13//! the file is modified. Once a segment is no longer referenced
14//! by the latest published checkpoint's metadata tree, its
15//! storage can be reclaimed.
16//!
17//! The [`Cleaner`] trait provides two operations:
18//!
19//! - [`Cleaner::collect`] walks the metadata tree of the latest
20//! (or a caller-specified) checkpoint and returns
21//! `(referenced, unreferenced)` segment lists for a
22//! `[low, high]` segid range. Useful for dry-run / inspection.
23//! - [`Cleaner::prune`] performs the same walk and additionally
24//! deletes the unreferenced segments via S3 `DeleteObjects`.
25//!
26//! ## Concurrency caveat
27//!
28//! The cleaner does not coordinate with in-flight writers. It
29//! takes a snapshot of the inode at the start of the call and
30//! operates on that. New segments published after that point are
31//! always outside the configured range (because `high` was fixed
32//! at snapshot time), so the prune is safe against concurrent
33//! appends. It is, however, the caller's responsibility to ensure
34//! `low` is not also being actively reused.
35//!
36//! ## Example
37//!
38//! ```no_run
39//! use hyperfile_cleaner::{Cleaner, s3::S3Cleaner};
40//!
41//! # async fn run() -> std::io::Result<()> {
42//! let cleaner = S3Cleaner::new("s3://my-bucket/path/to/file").await?;
43//!
44//! // Inspect what would be deleted (dry-run): pass high=0 to
45//! // mean "use the latest checkpoint".
46//! let (refed, unrefed) = cleaner.collect(1, 0).await?;
47//! println!("referenced : {:?}", refed);
48//! println!("unreferenced: {:?}", unrefed);
49//!
50//! // Actually delete the unreferenced range.
51//! cleaner.prune(1, 0).await?;
52//! # Ok(())
53//! # }
54//! ```
55
56use std::io::Result;
57use hyperfile::SegmentId;
58
59pub mod s3;
60
61/// Operations supported by a hyperfile cleaner backend.
62///
63/// Both methods take a `[low, high]` segment-id range. `high == 0`
64/// is a sentinel meaning "use the latest published checkpoint
65/// (`Inode::get_last_seq`)". `low` is inclusive; a typical value is
66/// `1` to cover the entire file history.
67///
68/// The range is inclusive at *both* ends, so `low == high` is valid and
69/// answered normally — which is what `collect(1, 0)` resolves to on a file with
70/// a single checkpoint. Only `low > high` is an error, and that also covers a
71/// file that has never been flushed (`get_last_seq() == 0`), since there is no
72/// metadata tree to walk.
73pub trait Cleaner {
74 /// Walk the metadata tree of the checkpoint identified by `high`
75 /// (or the latest if `high == 0`) and return
76 /// `(referenced, unreferenced)` segment id lists within
77 /// `[low, high]`.
78 ///
79 /// Useful for dry-run inspection before invoking [`Cleaner::prune`].
80 fn collect(
81 &self,
82 low: SegmentId,
83 high: SegmentId,
84 ) -> impl Future<Output = Result<(Vec<SegmentId>, Vec<SegmentId>)>> + Send;
85
86 /// Walk the metadata tree as in [`Cleaner::collect`] and then
87 /// delete the unreferenced segment objects from the underlying
88 /// store.
89 ///
90 /// Errors if any individual delete batch fails; in that case
91 /// previously-deleted batches stay deleted (S3 has no
92 /// transactional batch). The returned error message mentions
93 /// the failed object count and a sample failed key.
94 fn prune(
95 &self,
96 low: SegmentId,
97 high: SegmentId,
98 ) -> impl Future<Output = Result<()>> + Send;
99}