Skip to content

Commit

Permalink
fix: avoid crashing on WAL pruning edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Oct 12, 2024
1 parent b2c056e commit b107a71
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/wal/redb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ impl WalStore {
///
/// # Returns
///
/// Returns `Ok(())` if the operation was successful, or a `WalError` if an
/// error occurred.
/// Returns `Ok` if the operation was successful, or a `WalError` if an
/// error occurred. If the target slot is not found, it logs a warning and
/// returns `Ok`.
///
/// # Notes
///
Expand Down Expand Up @@ -248,27 +249,39 @@ impl WalStore {
}
};

let delta = last_slot - start_slot - max_slots;
let delta = last_slot - start_slot;
let excess = delta - max_slots;

debug!(delta, last_slot, start_slot, "wal history delta computed");
debug!(
delta,
excess, last_slot, start_slot, "wal history delta computed"
);

if delta <= max_slots {
debug!(delta, max_slots, "no pruning necessary");
if excess <= 0 {

Check failure on line 260 in src/wal/redb.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
debug!(delta, max_slots, excess, "no pruning necessary");
return Ok(());
}

let max_prune = match max_prune {
Some(max) => core::cmp::min(delta, max),
None => delta,
Some(max) => core::cmp::min(excess, max),
None => excess,
};

let prune_before = start_slot + max_prune;

info!(cutoff_slot = prune_before, "pruning wal for excess history");
info!(
cutoff_slot = prune_before,
start_slot, excess, "pruning wal for excess history"
);

self.remove_before(prune_before)?;

Ok(())
match self.remove_before(prune_before) {
Err(WalError::SlotNotFound(_)) => {
warn!("pruning target slot not found, skipping");
Ok(())
}
Err(e) => Err(e),
Ok(_) => Ok(()),
}
}

const MAX_PRUNE_SLOTS_PER_HOUSEKEEPING: u64 = 10_000;
Expand Down Expand Up @@ -363,7 +376,7 @@ impl WalStore {
///
/// # Examples
///
/// ```
/// ```ignore
/// let result = wal.approximate_slot_with_retry(
/// slot,
/// |retry| slot - 100 * retry..=slot + 100 * retry
Expand Down

0 comments on commit b107a71

Please sign in to comment.