Skip to main content

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::{Cno, SegmentId};
58
59pub mod compact;
60pub mod s3;
61
62/// Operations supported by a hyperfile cleaner backend.
63///
64/// Both methods take a `[low, high]` range of **checkpoint numbers**
65/// ([`Cno`]). `high == 0` is a sentinel meaning "use the latest published
66/// checkpoint (`Inode::get_last_cno`)". `low` is inclusive; a typical value
67/// is `1` to cover the entire file history.
68///
69/// The range is inclusive at *both* ends, so `low == high` is valid and
70/// answered normally — which is what `collect(1, 0)` resolves to on a file with
71/// a single checkpoint. Only `low > high` is an error, and that also covers a
72/// file that has never been flushed (`get_last_cno() == 0`), since there is no
73/// metadata tree to walk.
74///
75/// What the methods *report and delete* is [`SegmentId`], which names a stored
76/// object rather than a checkpoint: a checkpoint written under
77/// `BlockPtrFormat::PartedSegment` is several objects — `<cno>.0`, `<cno>.1`, …
78/// — and they are not reclaimable together. Under every other format an object
79/// and a checkpoint are the same thing and a `SegmentId` prints as the plain
80/// number.
81pub trait Cleaner {
82    /// Walk the metadata tree of the checkpoint identified by `high`
83    /// (or the latest if `high == 0`) and return
84    /// `(referenced, unreferenced)` lists of segment objects, the
85    /// unreferenced one limited to checkpoints within `[low, high]`.
86    ///
87    /// Useful for dry-run inspection before invoking [`Cleaner::prune`].
88    fn collect(
89        &self,
90        low: Cno,
91        high: Cno,
92    ) -> impl Future<Output = Result<(Vec<SegmentId>, Vec<SegmentId>)>> + Send;
93
94    /// Walk the metadata tree as in [`Cleaner::collect`] and then
95    /// delete the unreferenced segment objects from the underlying
96    /// store.
97    ///
98    /// Errors if any individual delete batch fails; in that case
99    /// previously-deleted batches stay deleted (S3 has no
100    /// transactional batch). The returned error message mentions
101    /// the failed object count and a sample failed key.
102    fn prune(
103        &self,
104        low: Cno,
105        high: Cno,
106    ) -> impl Future<Output = Result<()>> + Send;
107}