Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ledger tool halts at slot passed to create-snapshot #118

Merged
merged 7 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion core/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,12 @@ fn load_blockstore(

let blockstore = Arc::new(blockstore);
let blockstore_root_scan = BlockstoreRootScan::new(config, &blockstore, exit);
let halt_at_slot = config.halt_at_slot.or_else(|| highest_slot(&blockstore));
let mut halt_at_slot = config.halt_at_slot.or_else(|| highest_slot(&blockstore));
// Snapshot loading respects halt_at_slot, so we don't want to pass slot 0
// Passing None in process_options will load highest snapshot by default.
if let Some(0) = halt_at_slot {
halt_at_slot = None;
}

let process_options = blockstore_processor::ProcessOptions {
poh_verify: config.poh_verify,
Expand Down
2 changes: 2 additions & 0 deletions core/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ fn restore_from_snapshots_and_check_banks_are_equal(
false,
Some(ACCOUNTS_DB_CONFIG_FOR_TESTING),
None,
None,
)?;

assert_eq!(bank, &deserialized_bank);
Expand Down Expand Up @@ -1037,6 +1038,7 @@ fn test_snapshots_with_background_services(
false,
Some(ACCOUNTS_DB_CONFIG_FOR_TESTING),
None,
None,
)
.unwrap();

Expand Down
27 changes: 22 additions & 5 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,13 +932,15 @@ fn load_bank_forks(
snapshot_archive_path.unwrap_or_else(|| blockstore.ledger_path().to_path_buf());
let incremental_snapshot_archives_dir =
incremental_snapshot_archive_path.unwrap_or_else(|| full_snapshot_archives_dir.clone());
if let Some(full_snapshot_slot) =
snapshot_utils::get_highest_full_snapshot_archive_slot(&full_snapshot_archives_dir)
{
if let Some(full_snapshot_slot) = snapshot_utils::get_highest_full_snapshot_archive_slot(
&full_snapshot_archives_dir,
process_options.halt_at_slot,
) {
let incremental_snapshot_slot =
snapshot_utils::get_highest_incremental_snapshot_archive_slot(
&incremental_snapshot_archives_dir,
full_snapshot_slot,
process_options.halt_at_slot,
)
.unwrap_or_default();
starting_slot = std::cmp::max(full_snapshot_slot, incremental_snapshot_slot);
Expand All @@ -959,8 +961,8 @@ fn load_bank_forks(
// - This will not catch the case when loading from genesis without a full slot 0.
if !blockstore.slot_range_connected(starting_slot, halt_slot) {
eprintln!(
"Unable to load bank forks at slot {} due to disconnected blocks.",
halt_slot,
"Unable to load bank forks [start {} end {}] due to disconnected blocks.",
starting_slot, halt_slot,
);
exit(1);
}
Expand Down Expand Up @@ -2822,6 +2824,21 @@ fn main() {
"snapshot slot doesn't exist"
);

if let Ok(metas) = blockstore.slot_meta_iterator(0) {
let slots: Vec<_> = metas.map(|(slot, _)| slot).collect();
if slots.is_empty() {
eprintln!("Ledger is empty, can't create snapshot");
exit(1);
} else {
let first = slots.first().unwrap();
let last = slots.last().unwrap_or(first);
if first > &snapshot_slot || &snapshot_slot > last {
eprintln!("Slot {} is out of bounds of ledger [{}, {}], cannot create snapshot", &snapshot_slot, first, last);
exit(1);
}
}
}

let ending_slot = if is_minimized {
let ending_slot = value_t_or_exit!(arg_matches, "ending_slot", Slot);
if ending_slot <= snapshot_slot {
Expand Down
2 changes: 2 additions & 0 deletions ledger/src/bank_forks_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub fn load_bank_forks(

if snapshot_utils::get_highest_full_snapshot_archive_info(
&snapshot_config.full_snapshot_archives_dir,
process_options.halt_at_slot,
)
.is_some()
{
Expand Down Expand Up @@ -214,6 +215,7 @@ fn bank_forks_from_snapshot(
process_options.verify_index,
process_options.accounts_db_config.clone(),
accounts_update_notifier,
process_options.halt_at_slot,
)
.expect("Load from snapshot failed");

Expand Down
6 changes: 5 additions & 1 deletion local-cluster/src/local_cluster_snapshot_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ impl LocalCluster {
);
loop {
if let Some(full_snapshot_archive_info) =
snapshot_utils::get_highest_full_snapshot_archive_info(&full_snapshot_archives_dir)
snapshot_utils::get_highest_full_snapshot_archive_info(
&full_snapshot_archives_dir,
None,
)
{
match next_snapshot_type {
NextSnapshotType::FullSnapshot => {
Expand All @@ -92,6 +95,7 @@ impl LocalCluster {
snapshot_utils::get_highest_incremental_snapshot_archive_info(
incremental_snapshot_archives_dir.as_ref().unwrap(),
full_snapshot_archive_info.slot(),
None,
)
{
if incremental_snapshot_archive_info.slot() >= last_slot {
Expand Down
12 changes: 10 additions & 2 deletions local-cluster/tests/local_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ fn test_incremental_snapshot_download_with_crossing_full_snapshot_interval_at_st
validator_snapshot_test_config
.full_snapshot_archives_dir
.path(),
None,
)
.unwrap();
info!(
Expand Down Expand Up @@ -881,6 +882,7 @@ fn test_incremental_snapshot_download_with_crossing_full_snapshot_interval_at_st
.incremental_snapshot_archives_dir
.path(),
full_snapshot_archive.slot(),
None,
)
.unwrap();
info!(
Expand Down Expand Up @@ -1023,6 +1025,7 @@ fn test_incremental_snapshot_download_with_crossing_full_snapshot_interval_at_st
validator_snapshot_test_config
.full_snapshot_archives_dir
.path(),
None,
)
.unwrap();

Expand Down Expand Up @@ -1084,6 +1087,7 @@ fn test_incremental_snapshot_download_with_crossing_full_snapshot_interval_at_st
validator_snapshot_test_config
.full_snapshot_archives_dir
.path(),
None,
)
.unwrap();

Expand Down Expand Up @@ -1113,6 +1117,7 @@ fn test_incremental_snapshot_download_with_crossing_full_snapshot_interval_at_st
validator_snapshot_test_config
.full_snapshot_archives_dir
.path(),
None,
) {
if full_snapshot_slot >= validator_next_full_snapshot_slot {
if let Some(incremental_snapshot_slot) =
Expand All @@ -1121,6 +1126,7 @@ fn test_incremental_snapshot_download_with_crossing_full_snapshot_interval_at_st
.incremental_snapshot_archives_dir
.path(),
full_snapshot_slot,
None,
)
{
if incremental_snapshot_slot >= validator_next_incremental_snapshot_slot {
Expand Down Expand Up @@ -1338,8 +1344,10 @@ fn test_snapshots_blockstore_floor() {
trace!("Waiting for snapshot tar to be generated with slot",);

let archive_info = loop {
let archive =
snapshot_utils::get_highest_full_snapshot_archive_info(&full_snapshot_archives_dir);
let archive = snapshot_utils::get_highest_full_snapshot_archive_info(
&full_snapshot_archives_dir,
None,
);
if archive.is_some() {
trace!("snapshot exists");
break archive.unwrap();
Expand Down
10 changes: 7 additions & 3 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2677,13 +2677,16 @@ pub mod rpc_minimal {
})
.unwrap();

let full_snapshot_slot =
snapshot_utils::get_highest_full_snapshot_archive_slot(&full_snapshot_archives_dir)
.ok_or(RpcCustomError::NoSnapshot)?;
let full_snapshot_slot = snapshot_utils::get_highest_full_snapshot_archive_slot(
&full_snapshot_archives_dir,
None,
)
.ok_or(RpcCustomError::NoSnapshot)?;
let incremental_snapshot_slot =
snapshot_utils::get_highest_incremental_snapshot_archive_slot(
&incremental_snapshot_archives_dir,
full_snapshot_slot,
None,
);

Ok(RpcSnapshotSlotInfo {
Expand Down Expand Up @@ -4355,6 +4358,7 @@ pub mod rpc_deprecated_v1_9 {
.and_then(|snapshot_config| {
snapshot_utils::get_highest_full_snapshot_archive_slot(
&snapshot_config.full_snapshot_archives_dir,
None,
)
})
.ok_or_else(|| RpcCustomError::NoSnapshot.into())
Expand Down
2 changes: 2 additions & 0 deletions rpc/src/rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl RequestMiddleware for RpcRequestMiddleware {
let full_snapshot_archive_info =
snapshot_utils::get_highest_full_snapshot_archive_info(
&snapshot_config.full_snapshot_archives_dir,
None,
);
let snapshot_archive_info =
if let Some(full_snapshot_archive_info) = full_snapshot_archive_info {
Expand All @@ -262,6 +263,7 @@ impl RequestMiddleware for RpcRequestMiddleware {
snapshot_utils::get_highest_incremental_snapshot_archive_info(
&snapshot_config.incremental_snapshot_archives_dir,
full_snapshot_archive_info.slot(),
None,
)
.map(|incremental_snapshot_archive_info| {
incremental_snapshot_archive_info
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ index_list = "0.2.7"
itertools = "0.10.3"
lazy_static = "1.4.0"
log = "0.4.17"
lz4 = "1.23.3"
lz4 = "1.24.0"
memmap2 = "0.5.3"
num-derive = { version = "0.3" }
num-traits = { version = "0.2" }
Expand Down
30 changes: 24 additions & 6 deletions runtime/src/snapshot_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,12 +888,13 @@ pub fn bank_fields_from_snapshot_archives(
incremental_snapshot_archives_dir: impl AsRef<Path>,
) -> Result<BankFieldsToDeserialize> {
let full_snapshot_archive_info =
get_highest_full_snapshot_archive_info(&full_snapshot_archives_dir)
get_highest_full_snapshot_archive_info(&full_snapshot_archives_dir, None)
.ok_or(SnapshotError::NoSnapshotArchives)?;

let incremental_snapshot_archive_info = get_highest_incremental_snapshot_archive_info(
&incremental_snapshot_archives_dir,
full_snapshot_archive_info.slot(),
None,
);

let temp_dir = tempfile::Builder::new()
Expand Down Expand Up @@ -1026,18 +1027,20 @@ pub fn bank_from_latest_snapshot_archives(
verify_index: bool,
accounts_db_config: Option<AccountsDbConfig>,
accounts_update_notifier: Option<AccountsUpdateNotifier>,
halt_at_slot: Option<Slot>,
) -> Result<(
Bank,
FullSnapshotArchiveInfo,
Option<IncrementalSnapshotArchiveInfo>,
)> {
let full_snapshot_archive_info =
get_highest_full_snapshot_archive_info(&full_snapshot_archives_dir)
get_highest_full_snapshot_archive_info(&full_snapshot_archives_dir, halt_at_slot)
.ok_or(SnapshotError::NoSnapshotArchives)?;

let incremental_snapshot_archive_info = get_highest_incremental_snapshot_archive_info(
&incremental_snapshot_archives_dir,
full_snapshot_archive_info.slot(),
halt_at_slot,
);

info!(
Expand Down Expand Up @@ -1395,8 +1398,9 @@ pub fn get_incremental_snapshot_archives(
/// Get the highest slot of the full snapshot archives in a directory
pub fn get_highest_full_snapshot_archive_slot(
full_snapshot_archives_dir: impl AsRef<Path>,
halt_at_slot: Option<Slot>,
) -> Option<Slot> {
get_highest_full_snapshot_archive_info(full_snapshot_archives_dir)
get_highest_full_snapshot_archive_info(full_snapshot_archives_dir, halt_at_slot)
.map(|full_snapshot_archive_info| full_snapshot_archive_info.slot())
}

Expand All @@ -1405,19 +1409,26 @@ pub fn get_highest_full_snapshot_archive_slot(
pub fn get_highest_incremental_snapshot_archive_slot(
incremental_snapshot_archives_dir: impl AsRef<Path>,
full_snapshot_slot: Slot,
halt_at_slot: Option<Slot>,
) -> Option<Slot> {
get_highest_incremental_snapshot_archive_info(
incremental_snapshot_archives_dir,
full_snapshot_slot,
halt_at_slot,
)
.map(|incremental_snapshot_archive_info| incremental_snapshot_archive_info.slot())
}

/// Get the path (and metadata) for the full snapshot archive with the highest slot in a directory
pub fn get_highest_full_snapshot_archive_info(
full_snapshot_archives_dir: impl AsRef<Path>,
halt_at_slot: Option<Slot>,
) -> Option<FullSnapshotArchiveInfo> {
let mut full_snapshot_archives = get_full_snapshot_archives(full_snapshot_archives_dir);
if let Some(halt_at_slot) = halt_at_slot {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I assume retain keeps order but just to be safe can retain above sort

full_snapshot_archives
.retain(|archive| archive.snapshot_archive_info().slot <= halt_at_slot);
}
full_snapshot_archives.sort_unstable();
full_snapshot_archives.into_iter().rev().next()
}
Expand All @@ -1427,6 +1438,7 @@ pub fn get_highest_full_snapshot_archive_info(
pub fn get_highest_incremental_snapshot_archive_info(
incremental_snapshot_archives_dir: impl AsRef<Path>,
full_snapshot_slot: Slot,
halt_at_slot: Option<Slot>,
) -> Option<IncrementalSnapshotArchiveInfo> {
// Since we want to filter down to only the incremental snapshot archives that have the same
// full snapshot slot as the value passed in, perform the filtering before sorting to avoid
Expand All @@ -1438,6 +1450,9 @@ pub fn get_highest_incremental_snapshot_archive_info(
incremental_snapshot_archive_info.base_slot() == full_snapshot_slot
})
.collect::<Vec<_>>();
if let Some(halt_at_slot) = halt_at_slot {
incremental_snapshot_archives.retain(|archive| archive.slot() <= halt_at_slot);
}
incremental_snapshot_archives.sort_unstable();
incremental_snapshot_archives.into_iter().rev().next()
}
Expand Down Expand Up @@ -2867,7 +2882,7 @@ mod tests {
);

assert_eq!(
get_highest_full_snapshot_archive_slot(full_snapshot_archives_dir.path()),
get_highest_full_snapshot_archive_slot(full_snapshot_archives_dir.path(), None),
Some(max_slot - 1)
);
}
Expand All @@ -2894,7 +2909,8 @@ mod tests {
assert_eq!(
get_highest_incremental_snapshot_archive_slot(
incremental_snapshot_archives_dir.path(),
full_snapshot_slot
full_snapshot_slot,
None,
),
Some(max_incremental_snapshot_slot - 1)
);
Expand All @@ -2903,7 +2919,8 @@ mod tests {
assert_eq!(
get_highest_incremental_snapshot_archive_slot(
incremental_snapshot_archives_dir.path(),
max_full_snapshot_slot
max_full_snapshot_slot,
None,
),
None
);
Expand Down Expand Up @@ -3606,6 +3623,7 @@ mod tests {
false,
Some(ACCOUNTS_DB_CONFIG_FOR_TESTING),
None,
None,
)
.unwrap();

Expand Down
Loading