Skip to content

Commit 0e0e3c7

Browse files
authored
Merge pull request #2742 from ljedrz/feat/db_checkpoints
[Feat] Database checkpoints
2 parents 8dbb0b5 + e9b09b6 commit 0e0e3c7

6 files changed

Lines changed: 51 additions & 80 deletions

File tree

Cargo.lock

Lines changed: 15 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ledger/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
241241
Ok(ledger)
242242
}
243243

244+
/// Creates a rocksdb checkpoint in the specified directory, which needs to not exist at the
245+
/// moment of calling. The checkpoints are based on hard links, which means they can both be
246+
/// incremental (i.e. they aren't full physical copies), and used as full rollback points
247+
/// (a checkpoint can be used to completely replace the original ledger).
248+
#[cfg(feature = "rocks")]
249+
pub fn backup_database<P: AsRef<std::path::Path>>(&self, path: P) -> Result<()> {
250+
self.vm.block_store().backup_database(path).map_err(|err| anyhow!(err))
251+
}
252+
244253
/// Loads the provers and the number of solutions they have submitted for the current epoch.
245254
pub fn load_epoch_provers(&self) -> IndexMap<Address<N>, u32> {
246255
// Fetch the block heights that belong to the current epoch.

ledger/store/src/block/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,9 @@ pub trait BlockStorage<N: Network>: 'static + Clone + Send + Sync {
10211021
aborted_transaction_ids,
10221022
)?))
10231023
}
1024+
1025+
#[cfg(feature = "rocks")]
1026+
fn backup_database<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), String>;
10241027
}
10251028

10261029
/// The block store.
@@ -1193,6 +1196,11 @@ impl<N: Network, B: BlockStorage<N>> BlockStore<N, B> {
11931196
pub fn unpause_atomic_writes<const DISCARD_BATCH: bool>(&self) -> Result<()> {
11941197
self.storage.unpause_atomic_writes::<DISCARD_BATCH>()
11951198
}
1199+
1200+
#[cfg(feature = "rocks")]
1201+
pub fn backup_database<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), String> {
1202+
self.storage.backup_database(path)
1203+
}
11961204
}
11971205

11981206
impl<N: Network, B: BlockStorage<N>> BlockStore<N, B> {

ledger/store/src/helpers/memory/block.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,9 @@ impl<N: Network> BlockStorage<N> for BlockMemory<N> {
211211
fn transaction_store(&self) -> &TransactionStore<N, Self::TransactionStorage> {
212212
&self.transaction_store
213213
}
214+
215+
#[cfg(feature = "rocks")]
216+
fn backup_database<P: AsRef<std::path::Path>>(&self, _path: P) -> Result<(), String> {
217+
Err("Unavailable in memory-only mode".to_owned())
218+
}
214219
}

ledger/store/src/helpers/rocksdb/block.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,10 @@ impl<N: Network> BlockStorage<N> for BlockDB<N> {
218218
fn transaction_store(&self) -> &TransactionStore<N, Self::TransactionStorage> {
219219
&self.transaction_store
220220
}
221+
222+
#[cfg(feature = "rocks")]
223+
fn backup_database<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), String> {
224+
// Any map can be used to retrieve the common RocksDB instance.
225+
self.id_map().backup_database(path)
226+
}
221227
}

ledger/store/src/helpers/rocksdb/internal/map.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::helpers::{Map, MapRead};
2121
use core::{fmt, fmt::Debug, hash::Hash, mem};
2222
use indexmap::IndexMap;
2323
use smallvec::SmallVec;
24-
use std::{borrow::Cow, ops::Deref, sync::atomic::Ordering};
24+
use std::{borrow::Cow, ops::Deref, path::Path, sync::atomic::Ordering};
2525
use tracing::error;
2626

2727
#[derive(Clone)]
@@ -48,6 +48,13 @@ pub struct InnerDataMap<K: Serialize + DeserializeOwned, V: Serialize + Deserial
4848
pub(super) checkpoints: Mutex<Vec<usize>>,
4949
}
5050

51+
impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> InnerDataMap<K, V> {
52+
pub fn backup_database<P: AsRef<Path>>(&self, path: P) -> Result<(), String> {
53+
let checkpoint = rocksdb::checkpoint::Checkpoint::new(&self.database)?;
54+
checkpoint.create_checkpoint(path).map_err(|e| e.into_string())
55+
}
56+
}
57+
5158
impl<
5259
'a,
5360
K: 'a + Copy + Clone + Debug + PartialEq + Eq + Hash + Serialize + DeserializeOwned + Send + Sync,

0 commit comments

Comments
 (0)