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