Skip to content

Commit

Permalink
cleanup: use let-else to reduce nesting in backup_and_clear_blockstor…
Browse files Browse the repository at this point in the history
…e() (#2855)
  • Loading branch information
steviez authored Sep 6, 2024
1 parent 7f2013d commit 66c126b
Showing 1 changed file with 52 additions and 52 deletions.
104 changes: 52 additions & 52 deletions core/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2224,64 +2224,64 @@ fn backup_and_clear_blockstore(
start_slot,
expected_shred_version,
)?;
let Some(incorrect_shred_version) = incorrect_shred_version else {
info!("Only shreds with the correct version were found in the blockstore");
return Ok(());
};

if let Some(incorrect_shred_version) = incorrect_shred_version {
// .unwrap() safe because getting to this point implies blockstore has slots/shreds
let end_slot = blockstore.highest_slot()?.unwrap();

// Backing up the shreds that will be deleted from primary blockstore is
// not critical, so swallow errors from backup blockstore operations.
let backup_folder = format!(
"{}_backup_{}_{}_{}",
config
.ledger_column_options
.shred_storage_type
.blockstore_directory(),
incorrect_shred_version,
start_slot,
end_slot
);
match Blockstore::open_with_options(
&ledger_path.join(backup_folder),
blockstore_options_from_config(config),
) {
Ok(backup_blockstore) => {
info!("Backing up slots from {start_slot} to {end_slot}");
let mut timer = Measure::start("blockstore backup");

const PRINT_INTERVAL: Duration = Duration::from_secs(5);
let mut print_timer = Instant::now();
let mut num_slots_copied = 0;
let slot_meta_iterator = blockstore.slot_meta_iterator(start_slot)?;
for (slot, _meta) in slot_meta_iterator {
let shreds = blockstore.get_data_shreds_for_slot(slot, 0)?;
let _ = backup_blockstore.insert_shreds(shreds, None, true);
num_slots_copied += 1;

if print_timer.elapsed() > PRINT_INTERVAL {
info!("Backed up {num_slots_copied} slots thus far");
print_timer = Instant::now();
}
// .unwrap() safe because getting to this point implies blockstore has slots/shreds
let end_slot = blockstore.highest_slot()?.unwrap();

// Backing up the shreds that will be deleted from primary blockstore is
// not critical, so swallow errors from backup blockstore operations.
let backup_folder = format!(
"{}_backup_{}_{}_{}",
config
.ledger_column_options
.shred_storage_type
.blockstore_directory(),
incorrect_shred_version,
start_slot,
end_slot
);
match Blockstore::open_with_options(
&ledger_path.join(backup_folder),
blockstore_options_from_config(config),
) {
Ok(backup_blockstore) => {
info!("Backing up slots from {start_slot} to {end_slot}");
let mut timer = Measure::start("blockstore backup");

const PRINT_INTERVAL: Duration = Duration::from_secs(5);
let mut print_timer = Instant::now();
let mut num_slots_copied = 0;
let slot_meta_iterator = blockstore.slot_meta_iterator(start_slot)?;
for (slot, _meta) in slot_meta_iterator {
let shreds = blockstore.get_data_shreds_for_slot(slot, 0)?;
let _ = backup_blockstore.insert_shreds(shreds, None, true);
num_slots_copied += 1;

if print_timer.elapsed() > PRINT_INTERVAL {
info!("Backed up {num_slots_copied} slots thus far");
print_timer = Instant::now();
}

timer.stop();
info!("Backing up slots done. {timer}");
}
Err(err) => {
warn!("Unable to backup shreds with incorrect shred version: {err}");
}
}

info!("Purging slots {start_slot} to {end_slot} from blockstore");
let mut timer = Measure::start("blockstore purge");
blockstore.purge_from_next_slots(start_slot, end_slot);
blockstore.purge_slots(start_slot, end_slot, PurgeType::Exact);
timer.stop();
info!("Purging slots done. {timer}");
} else {
info!("Only shreds with the correct version were found in the blockstore");
timer.stop();
info!("Backing up slots done. {timer}");
}
Err(err) => {
warn!("Unable to backup shreds with incorrect shred version: {err}");
}
}

info!("Purging slots {start_slot} to {end_slot} from blockstore");
let mut timer = Measure::start("blockstore purge");
blockstore.purge_from_next_slots(start_slot, end_slot);
blockstore.purge_slots(start_slot, end_slot, PurgeType::Exact);
timer.stop();
info!("Purging slots done. {timer}");

Ok(())
}

Expand Down

0 comments on commit 66c126b

Please sign in to comment.