Skip to content

Commit dcbd016

Browse files
committed
Refactor fs garbage collection
1 parent d28605d commit dcbd016

File tree

1 file changed

+45
-33
lines changed
  • sequencer/src/persistence

1 file changed

+45
-33
lines changed

sequencer/src/persistence/fs.rs

+45-33
Original file line numberDiff line numberDiff line change
@@ -221,44 +221,56 @@ impl Inner {
221221

222222
fn collect_garbage(
223223
&mut self,
224-
view: ViewNumber,
225-
intervals: &[RangeInclusive<ViewNumber>],
224+
decided_view: ViewNumber,
225+
prune_intervals: &[RangeInclusive<ViewNumber>],
226226
) -> anyhow::Result<()> {
227-
let view_number = view;
228-
let prune_view = ViewNumber::new(view.saturating_sub(self.view_retention));
229-
230-
let delete_files = |intervals: &[RangeInclusive<ViewNumber>],
231-
keep,
232-
dir_path: PathBuf|
233-
-> anyhow::Result<()> {
234-
if !dir_path.is_dir() {
235-
return Ok(());
236-
}
227+
let prune_view = ViewNumber::new(decided_view.saturating_sub(self.view_retention));
237228

238-
for (v, path) in view_files(dir_path)? {
239-
// If the view is the anchor view, keep it no matter what.
240-
if let Some(keep) = keep {
241-
if keep == v {
242-
continue;
243-
}
244-
}
245-
// Otherwise, delete it if it is time to prune this view _or_ if the given
246-
// intervals, which we've already successfully processed, contain the view; in
247-
// this case we simply don't need it anymore.
248-
if v < prune_view || intervals.iter().any(|i| i.contains(&v)) {
249-
fs::remove_file(&path)?;
250-
}
251-
}
229+
self.prune_files(self.da_dir_path(), prune_view, None, prune_intervals)?;
230+
self.prune_files(self.vid_dir_path(), prune_view, None, prune_intervals)?;
231+
self.prune_files(
232+
self.quorum_proposals_dir_path(),
233+
prune_view,
234+
None,
235+
prune_intervals,
236+
)?;
252237

253-
Ok(())
254-
};
238+
// Save the most recent leaf as it will be our anchor point if the node restarts.
239+
self.prune_files(
240+
self.decided_leaf_path(),
241+
prune_view,
242+
Some(decided_view),
243+
prune_intervals,
244+
)?;
255245

256-
delete_files(intervals, None, self.da_dir_path())?;
257-
delete_files(intervals, None, self.vid_dir_path())?;
258-
delete_files(intervals, None, self.quorum_proposals_dir_path())?;
246+
Ok(())
247+
}
259248

260-
// Save the most recent leaf as it will be our anchor point if the node restarts.
261-
delete_files(intervals, Some(view_number), self.decided_leaf_path())?;
249+
fn prune_files(
250+
&mut self,
251+
dir_path: PathBuf,
252+
prune_view: ViewNumber,
253+
keep_decided_view: Option<ViewNumber>,
254+
prune_intervals: &[RangeInclusive<ViewNumber>],
255+
) -> anyhow::Result<()> {
256+
if !dir_path.is_dir() {
257+
return Ok(());
258+
}
259+
260+
for (file_view, path) in view_files(dir_path)? {
261+
// If the view is the anchor view, keep it no matter what.
262+
if let Some(decided_view) = keep_decided_view {
263+
if decided_view == file_view {
264+
continue;
265+
}
266+
}
267+
// Otherwise, delete it if it is time to prune this view _or_ if the given intervals,
268+
// which we've already successfully processed, contain the view; in this case we simply
269+
// don't need it anymore.
270+
if file_view < prune_view || prune_intervals.iter().any(|i| i.contains(&file_view)) {
271+
fs::remove_file(&path)?;
272+
}
273+
}
262274

263275
Ok(())
264276
}

0 commit comments

Comments
 (0)