Skip to content

Commit 22889ed

Browse files
authored
Enabling snapshot generation when loading from ledger tool (anza-xyz#10064)
* When creating a snapshot from the ledger tool, ensure that snapshot generation and loading is enabled * Seperated out common code in ledger_utils Replaced assert!(matches!()) with assert_eq()
1 parent 5210fef commit 22889ed

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

ledger-tool/src/ledger_utils.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,19 @@ pub fn load_and_process_ledger(
408408
};
409409

410410
let (snapshot_request_sender, snapshot_request_receiver) = crossbeam_channel::unbounded();
411+
412+
// If snapshot_slot is present, then ledger-tool is attempting to generate a snapshot. Setting
413+
// new_generate_snapshots_externally ensures the accounts database retains zero lamport
414+
// accounts needed to correctly generate incremental snapshots
415+
let snapshot_config = if arg_matches.is_present("snapshot_slot") {
416+
SnapshotConfig::new_generate_snapshots_externally()
417+
} else {
418+
SnapshotConfig::new_load_only()
419+
};
420+
411421
let snapshot_controller = Arc::new(SnapshotController::new(
412422
snapshot_request_sender,
413-
SnapshotConfig::new_load_only(),
423+
snapshot_config,
414424
bank_forks.read().unwrap().root(),
415425
));
416426
let pending_snapshot_packages = Arc::new(Mutex::new(PendingSnapshotPackages::default()));

ledger-tool/src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,15 @@ fn main() {
21752175
exit(1);
21762176
});
21772177

2178+
// If we are creating an incremental snapshot, it must be based on a full snapshot
2179+
if is_incremental {
2180+
assert!(bank
2181+
.accounts()
2182+
.accounts_db
2183+
.latest_full_snapshot_slot()
2184+
.is_some());
2185+
}
2186+
21782187
// Snapshot creation will implicitly perform AccountsDb
21792188
// flush and clean operations. These operations cannot be
21802189
// run concurrently, so ensure ABS is stopped to avoid that

snapshots/src/snapshot_config.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ impl SnapshotConfig {
8989
}
9090
}
9191

92+
/// A new snapshot config used for loading at startup and generating
93+
/// snapshots during runtime. Snapshot generation intervals are disabled
94+
/// to indicate that snapshots will be generated externally.
95+
pub fn new_generate_snapshots_externally() -> Self {
96+
Self {
97+
usage: SnapshotUsage::LoadAndGenerate,
98+
full_snapshot_archive_interval: SnapshotInterval::Disabled,
99+
incremental_snapshot_archive_interval: SnapshotInterval::Disabled,
100+
..Self::default()
101+
}
102+
}
103+
92104
/// A new snapshot config used to disable snapshot generation and loading at
93105
/// startup
94106
pub fn new_disabled() -> Self {
@@ -123,3 +135,62 @@ pub enum SnapshotUsage {
123135
/// generate). This enables taking snapshots.
124136
LoadAndGenerate,
125137
}
138+
139+
#[cfg(test)]
140+
mod tests {
141+
use super::*;
142+
143+
#[test]
144+
fn test_new_load_only() {
145+
let cfg = SnapshotConfig::new_load_only();
146+
147+
assert!(!cfg.should_generate_snapshots());
148+
assert!(cfg.should_load_snapshots());
149+
150+
assert_eq!(
151+
cfg.full_snapshot_archive_interval,
152+
SnapshotInterval::Disabled
153+
);
154+
assert_eq!(
155+
cfg.incremental_snapshot_archive_interval,
156+
SnapshotInterval::Disabled
157+
);
158+
}
159+
160+
#[test]
161+
fn test_new_disabled() {
162+
let cfg = SnapshotConfig::new_disabled();
163+
164+
assert!(!cfg.should_generate_snapshots());
165+
assert!(!cfg.should_load_snapshots());
166+
167+
assert_eq!(
168+
cfg.full_snapshot_archive_interval,
169+
SnapshotInterval::Disabled
170+
);
171+
assert_eq!(
172+
cfg.incremental_snapshot_archive_interval,
173+
SnapshotInterval::Disabled
174+
);
175+
}
176+
177+
#[test]
178+
fn test_new_generate_snapshots_externally() {
179+
let cfg = SnapshotConfig::new_generate_snapshots_externally();
180+
181+
// Even though intervals are disabled (snapshots handled externally),
182+
// usage indicates load+generate semantics.
183+
assert_eq!(cfg.usage, SnapshotUsage::LoadAndGenerate);
184+
assert!(cfg.should_generate_snapshots());
185+
assert!(cfg.should_load_snapshots());
186+
187+
assert_eq!(
188+
cfg.full_snapshot_archive_interval,
189+
SnapshotInterval::Disabled
190+
);
191+
assert_eq!(
192+
cfg.incremental_snapshot_archive_interval,
193+
SnapshotInterval::Disabled
194+
);
195+
}
196+
}

0 commit comments

Comments
 (0)