Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit fa22389

Browse files
authored
Add version and state_complete flag into bank snapshot (#30099)
* Add version and state_complate flag into bank snapshot * Add test_get_highest_full_snapshot_slot_and_path * fix build checks * fix slot_deltas panic in test_get_highest_full_snapshot_slot_and_path * make bank rooted for test_get_highest_full_snapshot_slot_and_path * fix bank scope problem in test_get_highest_full_snapshot_slot_and_path * minor cleanup * misc review issues * remove unneeded error defs and functions * Fix state_complete macro naming * fix snapshot_version
1 parent ce4251b commit fa22389

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

runtime/src/snapshot_utils.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ mod snapshot_storage_rebuilder;
7070
pub use archive_format::*;
7171

7272
pub const SNAPSHOT_STATUS_CACHE_FILENAME: &str = "status_cache";
73+
pub const SNAPSHOT_VERSION_FILENAME: &str = "version";
74+
pub const SNAPSHOT_STATE_COMPLETE_FILENAME: &str = "state_complete";
7375
pub const SNAPSHOT_ARCHIVE_DOWNLOAD_DIR: &str = "remote";
7476
pub const DEFAULT_FULL_SNAPSHOT_ARCHIVE_INTERVAL_SLOTS: Slot = 25_000;
7577
pub const DEFAULT_INCREMENTAL_SNAPSHOT_ARCHIVE_INTERVAL_SLOTS: Slot = 100;
@@ -430,6 +432,18 @@ pub fn remove_tmp_snapshot_archives(snapshot_archives_dir: impl AsRef<Path>) {
430432
}
431433
}
432434

435+
/// Write the snapshot version as a file into the bank snapshot directory
436+
pub fn write_snapshot_version_file(
437+
version_file: impl AsRef<Path>,
438+
version: SnapshotVersion,
439+
) -> Result<()> {
440+
let mut f = fs::File::create(version_file)
441+
.map_err(|e| SnapshotError::IoWithSource(e, "create version file"))?;
442+
f.write_all(version.as_str().as_bytes())
443+
.map_err(|e| SnapshotError::IoWithSource(e, "write version file"))?;
444+
Ok(())
445+
}
446+
433447
/// Make a snapshot archive out of the snapshot package
434448
pub fn archive_snapshot_package(
435449
snapshot_package: &SnapshotPackage,
@@ -466,7 +480,7 @@ pub fn archive_snapshot_package(
466480

467481
let staging_accounts_dir = staging_dir.path().join("accounts");
468482
let staging_snapshots_dir = staging_dir.path().join("snapshots");
469-
let staging_version_file = staging_dir.path().join("version");
483+
let staging_version_file = staging_dir.path().join(SNAPSHOT_VERSION_FILENAME);
470484
fs::create_dir_all(&staging_accounts_dir).map_err(|e| {
471485
SnapshotError::IoWithSourceAndFile(e, "create staging path", staging_accounts_dir.clone())
472486
})?;
@@ -498,14 +512,7 @@ pub fn archive_snapshot_package(
498512
}
499513
}
500514

501-
// Write version file
502-
{
503-
let mut f = fs::File::create(&staging_version_file).map_err(|e| {
504-
SnapshotError::IoWithSourceAndFile(e, "create version file", staging_version_file)
505-
})?;
506-
f.write_all(snapshot_package.snapshot_version.as_str().as_bytes())
507-
.map_err(|e| SnapshotError::IoWithSource(e, "write version file"))?;
508-
}
515+
write_snapshot_version_file(staging_version_file, snapshot_package.snapshot_version)?;
509516

510517
// Tar the staging directory into the archive at `archive_path`
511518
let archive_path = tar_dir.join(format!(
@@ -522,7 +529,10 @@ pub fn archive_snapshot_package(
522529
let mut archive = tar::Builder::new(encoder);
523530
// Serialize the version and snapshots files before accounts so we can quickly determine the version
524531
// and other bank fields. This is necessary if we want to interleave unpacking with reconstruction
525-
archive.append_path_with_name(staging_dir.as_ref().join("version"), "version")?;
532+
archive.append_path_with_name(
533+
staging_dir.as_ref().join(SNAPSHOT_VERSION_FILENAME),
534+
SNAPSHOT_VERSION_FILENAME,
535+
)?;
526536
for dir in ["snapshots", "accounts"] {
527537
archive.append_dir_all(dir, staging_dir.as_ref().join(dir))?;
528538
}
@@ -1045,6 +1055,13 @@ pub fn add_bank_snapshot(
10451055
let status_cache_path = bank_snapshot_dir.join(SNAPSHOT_STATUS_CACHE_FILENAME);
10461056
serialize_status_cache(slot, &slot_deltas, &status_cache_path)?;
10471057

1058+
let version_path = bank_snapshot_dir.join(SNAPSHOT_VERSION_FILENAME);
1059+
write_snapshot_version_file(version_path, snapshot_version).unwrap();
1060+
1061+
// Mark this directory complete so it can be used. Check this flag first before selecting for deserialization.
1062+
let state_complete_path = bank_snapshot_dir.join(SNAPSHOT_STATE_COMPLETE_FILENAME);
1063+
fs::File::create(state_complete_path)?;
1064+
10481065
// Monitor sizes because they're capped to MAX_SNAPSHOT_DATA_FILE_SIZE
10491066
datapoint_info!(
10501067
"snapshot-bank-file",
@@ -2346,6 +2363,16 @@ pub fn verify_snapshot_archive<P, Q, R>(
23462363
}
23472364
std::fs::remove_dir_all(accounts_hardlinks_dir).unwrap();
23482365
}
2366+
2367+
let version_path = snapshot_slot_dir.join(SNAPSHOT_VERSION_FILENAME);
2368+
if version_path.is_file() {
2369+
std::fs::remove_file(version_path).unwrap();
2370+
}
2371+
2372+
let state_complete_path = snapshot_slot_dir.join(SNAPSHOT_STATE_COMPLETE_FILENAME);
2373+
if state_complete_path.is_file() {
2374+
std::fs::remove_file(state_complete_path).unwrap();
2375+
}
23492376
}
23502377

23512378
assert!(!dir_diff::is_different(&snapshots_to_verify, unpacked_snapshots).unwrap());

0 commit comments

Comments
 (0)