Make SnapshotPackagerService aware of Incremental Snapshots (#19254)

Add a field to SnapshotPackage that is an enum for SnapshotType, so archive_snapshot_package() will do the right thing.

Fixes #19166
This commit is contained in:
Brooks Prumo
2021-08-17 13:01:59 -05:00
committed by GitHub
parent 7481d9b66b
commit f9986c66b8
7 changed files with 74 additions and 26 deletions

View File

@ -16,14 +16,26 @@ use solana_sdk::hash::Hash;
use std::{
fs,
path::{Path, PathBuf},
sync::mpsc::{Receiver, SendError, Sender},
sync::{
mpsc::{Receiver, SendError, Sender},
Arc, Mutex,
},
};
use tempfile::TempDir;
/// The sender side of the AccountsPackage channel, used by AccountsBackgroundService
pub type AccountsPackageSender = Sender<AccountsPackage>;
/// The receiver side of the AccountsPackage channel, used by AccountsHashVerifier
pub type AccountsPackageReceiver = Receiver<AccountsPackage>;
/// The error type when sending an AccountsPackage over the channel fails
pub type AccountsPackageSendError = SendError<AccountsPackage>;
/// The PendingSnapshotPackage passes a SnapshotPackage from AccountsHashVerifier to
/// SnapshotPackagerService for archiving
pub type PendingSnapshotPackage = Arc<Mutex<Option<SnapshotPackage>>>;
#[derive(Debug)]
pub struct AccountsPackage {
pub slot: Slot,
@ -176,9 +188,11 @@ pub struct SnapshotPackage {
pub snapshot_links: TempDir,
pub storages: SnapshotStorages,
pub snapshot_version: SnapshotVersion,
pub snapshot_type: SnapshotType,
}
impl SnapshotPackage {
#[allow(clippy::too_many_arguments)]
pub fn new(
slot: Slot,
block_height: u64,
@ -189,6 +203,7 @@ impl SnapshotPackage {
hash: Hash,
archive_format: ArchiveFormat,
snapshot_version: SnapshotVersion,
snapshot_type: SnapshotType,
) -> Self {
Self {
snapshot_archive_info: SnapshotArchiveInfo {
@ -202,6 +217,7 @@ impl SnapshotPackage {
snapshot_links,
storages,
snapshot_version,
snapshot_type,
}
}
}
@ -211,3 +227,19 @@ impl SnapshotArchiveInfoGetter for SnapshotPackage {
&self.snapshot_archive_info
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SnapshotType {
FullSnapshot,
IncrementalSnapshot,
}
impl SnapshotType {
/// Get the string prefix of the snapshot type
pub fn to_prefix(&self) -> &'static str {
match self {
SnapshotType::FullSnapshot => TMP_FULL_SNAPSHOT_PREFIX,
SnapshotType::IncrementalSnapshot => TMP_INCREMENTAL_SNAPSHOT_PREFIX,
}
}
}

View File

@ -14,6 +14,7 @@ use {
},
snapshot_package::{
AccountsPackage, AccountsPackageSendError, AccountsPackageSender, SnapshotPackage,
SnapshotType,
},
sorted_storages::SortedStorages,
},
@ -239,10 +240,10 @@ pub fn remove_tmp_snapshot_archives(snapshot_archives_dir: &Path) {
}
}
/// Make a full snapshot archive out of the snapshot package
/// Make a snapshot archive out of the snapshot package
pub fn archive_snapshot_package(
snapshot_package: &SnapshotPackage,
maximum_snapshots_to_retain: usize,
maximum_snapshot_archives_to_retain: usize,
) -> Result<()> {
info!(
"Generating snapshot archive for slot {}",
@ -268,10 +269,11 @@ pub fn archive_snapshot_package(
.map_err(|e| SnapshotError::IoWithSource(e, "create archive path"))?;
// Create the staging directories
let staging_dir_prefix = snapshot_package.snapshot_type.to_prefix();
let staging_dir = tempfile::Builder::new()
.prefix(&format!(
"{}{}-",
TMP_FULL_SNAPSHOT_PREFIX,
staging_dir_prefix,
snapshot_package.slot()
))
.tempdir_in(tar_dir)
@ -323,7 +325,7 @@ pub fn archive_snapshot_package(
// Tar the staging directory into the archive at `archive_path`
let archive_path = tar_dir.join(format!(
"{}{}.{}",
TMP_FULL_SNAPSHOT_PREFIX,
staging_dir_prefix,
snapshot_package.slot(),
file_ext
));
@ -371,7 +373,7 @@ pub fn archive_snapshot_package(
fs::rename(&archive_path, &snapshot_package.path())
.map_err(|e| SnapshotError::IoWithSource(e, "archive path rename"))?;
purge_old_snapshot_archives(tar_dir, maximum_snapshots_to_retain);
purge_old_snapshot_archives(tar_dir, maximum_snapshot_archives_to_retain);
timer.stop();
info!(
@ -1794,6 +1796,11 @@ pub fn process_accounts_package(
),
};
let snapshot_type = match incremental_snapshot_base_slot {
None => SnapshotType::FullSnapshot,
Some(_) => SnapshotType::IncrementalSnapshot,
};
SnapshotPackage::new(
accounts_package.slot,
accounts_package.block_height,
@ -1804,6 +1811,7 @@ pub fn process_accounts_package(
hash,
accounts_package.archive_format,
accounts_package.snapshot_version,
snapshot_type,
)
}