Skip to content

Commit 55b3602

Browse files
committed
Applies suggestions from code review:
- Avoids duplicate validation of format upgrades at startup when db is already upgraded, - Minor refactors - Doc fixes and cleanups
1 parent 59c0b24 commit 55b3602

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

Diff for: zebra-state/src/service/finalized_state/disk_format/upgrade.rs

+30-38
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,21 @@ pub trait DiskFormatUpgrade {
8181
}
8282
}
8383

84-
fn format_upgrades() -> Vec<Box<dyn DiskFormatUpgrade>> {
85-
// Note: Disk format upgrades must be run in order.
86-
vec![
84+
fn format_upgrades(
85+
min_version: Option<Version>,
86+
) -> impl Iterator<Item = Box<dyn DiskFormatUpgrade>> {
87+
let min_version = move || min_version.clone().unwrap_or(Version::new(0, 0, 0));
88+
89+
// Note: Disk format upgrades must be run in order of database version.
90+
([
8791
Box::new(prune_trees::PruneTrees),
8892
Box::new(add_subtrees::AddSubtrees),
8993
Box::new(tree_keys_and_caches_upgrade::FixTreeKeyTypeAndCacheGenesisRoots),
9094
// Value balance upgrade
9195
Box::new(no_migration::NoMigration::new(26, 0, 0)),
92-
]
96+
] as [Box<dyn DiskFormatUpgrade>; 4])
97+
.into_iter()
98+
.filter(move |upgrade| upgrade.version() > min_version())
9399
}
94100

95101
/// The kind of database format change or validity check we're performing.
@@ -532,53 +538,30 @@ impl DbFormatChange {
532538
};
533539

534540
// Apply or validate format upgrades
535-
for upgrade in format_upgrades() {
536-
if older_disk_version >= &upgrade.version() {
541+
for upgrade in format_upgrades(Some(older_disk_version.clone())) {
542+
if upgrade.needs_migration() {
543+
let timer = CodeTimer::start();
544+
545+
upgrade.prepare(initial_tip_height, db, cancel_receiver, older_disk_version)?;
546+
upgrade.run(initial_tip_height, db, cancel_receiver)?;
547+
548+
// Before marking the state as upgraded, check that the upgrade completed successfully.
537549
upgrade
538550
.validate(db, cancel_receiver)?
539-
.expect("failed to validate db format");
540-
continue;
541-
}
551+
.expect("db should be valid after upgrade");
542552

543-
if !upgrade.needs_migration() {
544-
Self::mark_as_upgraded_to(db, &upgrade.version());
545-
continue;
553+
timer.finish(module_path!(), line!(), upgrade.description());
546554
}
547555

548-
let timer = CodeTimer::start();
549-
550-
upgrade.prepare(initial_tip_height, db, cancel_receiver, older_disk_version)?;
551-
upgrade.run(initial_tip_height, db, cancel_receiver)?;
552-
553-
// Before marking the state as upgraded, check that the upgrade completed successfully.
554-
upgrade
555-
.validate(db, cancel_receiver)?
556-
.expect("db should be valid after upgrade");
557-
558556
// Mark the database as upgraded. Zebra won't repeat the upgrade anymore once the
559557
// database is marked, so the upgrade MUST be complete at this point.
560558
info!(
561559
newer_running_version = ?upgrade.version(),
562560
"Zebra automatically upgraded the database format"
563561
);
564562
Self::mark_as_upgraded_to(db, &upgrade.version());
565-
566-
timer.finish(module_path!(), line!(), upgrade.description());
567563
}
568564

569-
let version_for_upgrading_value_balance_format =
570-
Version::parse("26.0.0").expect("hard-coded version string should be valid");
571-
572-
// Check if we need to do the upgrade.
573-
if older_disk_version < &version_for_upgrading_value_balance_format {
574-
Self::mark_as_upgraded_to(db, &version_for_upgrading_value_balance_format)
575-
}
576-
577-
info!(
578-
%newer_running_version,
579-
"Zebra automatically upgraded the database format to:"
580-
);
581-
582565
Ok(())
583566
}
584567

@@ -625,7 +608,7 @@ impl DbFormatChange {
625608
// Do the quick checks first, so we don't have to do this in every detailed check.
626609
results.push(Self::format_validity_checks_quick(db));
627610

628-
for upgrade in format_upgrades() {
611+
for upgrade in format_upgrades(None) {
629612
results.push(upgrade.validate(db, cancel_receiver)?);
630613
}
631614

@@ -842,3 +825,12 @@ impl Drop for DbFormatChangeThreadHandle {
842825
}
843826
}
844827
}
828+
829+
#[test]
830+
fn format_upgrades_are_in_version_order() {
831+
let mut last_version = Version::new(0, 0, 0);
832+
for upgrade in format_upgrades(None) {
833+
assert!(upgrade.version() > last_version);
834+
last_version = upgrade.version();
835+
}
836+
}

Diff for: zebra-state/src/service/finalized_state/disk_format/upgrade/no_migration.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::service::finalized_state::ZebraDb;
99

1010
use super::{CancelFormatChange, DiskFormatUpgrade};
1111

12-
/// Implements [`DiskFormatUpgrade`] for pruning duplicate Sapling and Orchard note commitment trees from database
12+
/// Implements [`DiskFormatUpgrade`] for in-place upgrades that do not involve any migration
13+
/// of existing data into the new format.
1314
pub struct NoMigration {
1415
version: Version,
1516
}

0 commit comments

Comments
 (0)