Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create NodeStorage from checkpoint #8876

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/store/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ pub trait Database: Sync + Send {

/// Returns statistics about the database if available.
fn get_store_statistics(&self) -> Option<StoreStatistics>;

/// Create checkpoint in provided path
fn create_checkpoint(&self, path: &std::path::Path) -> anyhow::Result<()>;
}

fn assert_no_overwrite(col: DBCol, key: &[u8], value: &[u8], old_value: &[u8]) {
Expand Down
4 changes: 4 additions & 0 deletions core/store/src/db/colddb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ impl Database for ColdDB {
fn get_store_statistics(&self) -> Option<crate::StoreStatistics> {
self.cold.get_store_statistics()
}

fn create_checkpoint(&self, path: &std::path::Path) -> anyhow::Result<()> {
self.cold.create_checkpoint(path)
}
}

/// Adjust database operation to be performed on cold storage.
Expand Down
6 changes: 6 additions & 0 deletions core/store/src/db/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ impl Database for RocksDB {
Some(result)
}
}

fn create_checkpoint(&self, path: &std::path::Path) -> anyhow::Result<()> {
let cp = ::rocksdb::checkpoint::Checkpoint::new(&self.db)?;
cp.create_checkpoint(path)?;
Ok(())
}
}

/// DB level options
Expand Down
5 changes: 5 additions & 0 deletions core/store/src/db/splitdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ impl Database for SplitDB {
log_assert_fail!("get_store_statistics is not allowed - the split storage has two stores");
None
}

fn create_checkpoint(&self, _path: &std::path::Path) -> anyhow::Result<()> {
log_assert_fail!("create_checkpoint is not allowed - the split storage has two stores");
Ok(())
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions core/store/src/db/testdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@ impl Database for TestDB {
fn get_store_statistics(&self) -> Option<StoreStatistics> {
self.stats.read().unwrap().clone()
}

fn create_checkpoint(&self, _path: &std::path::Path) -> anyhow::Result<()> {
Ok(())
}
}
4 changes: 3 additions & 1 deletion core/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub mod test_utils;
mod trie;

pub use crate::config::{Mode, StoreConfig};
pub use crate::opener::{StoreMigrator, StoreOpener, StoreOpenerError};
pub use crate::opener::{
checkpoint_hot_storage_and_cleanup_columns, StoreMigrator, StoreOpener, StoreOpenerError,
};

/// Specifies temperature of a storage.
///
Expand Down
48 changes: 47 additions & 1 deletion core/store/src/opener.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::sync::Arc;
use strum::IntoEnumIterator;

use crate::db::rocksdb::snapshot::{Snapshot, SnapshotError, SnapshotRemoveError};
use crate::db::rocksdb::RocksDB;
use crate::metadata::{DbKind, DbMetadata, DbVersion, DB_VERSION};
use crate::{Mode, NodeStorage, Store, StoreConfig, Temperature};
use crate::{DBCol, DBTransaction, Mode, NodeStorage, Store, StoreConfig, Temperature};

#[derive(Debug, thiserror::Error)]
pub enum StoreOpenerError {
Expand Down Expand Up @@ -569,3 +570,48 @@ pub trait StoreMigrator {
/// equal to [`DB_VERSION`].
fn migrate(&self, store: &Store, version: DbVersion) -> anyhow::Result<()>;
}

/// Creates checkpoint of hot storage in `home_dir.join(checkpoint_relative_path)`
///
/// If `columns_to_keep` is None doesn't cleanup columns.
/// Otherwise deletes all columns that are not in `columns_to_keep`.
///
/// Returns NodeStorage of checkpoint db.
///
/// `config` -- StoreConfig for hot storage (needed to open checkpoint).
/// `archive` -- is hot storage archival (needed to open checkpoint).
#[allow(dead_code)]
pub fn checkpoint_hot_storage_and_cleanup_columns(
db_storage: &NodeStorage,
home_dir: &std::path::Path,
checkpoint_relative_path: std::path::PathBuf,
columns_to_keep: Option<Vec<DBCol>>,
mut config: StoreConfig,
posvyatokum marked this conversation as resolved.
Show resolved Hide resolved
archive: bool,
) -> anyhow::Result<NodeStorage> {
let checkpoint_path = home_dir.join(checkpoint_relative_path);

db_storage.hot_storage.create_checkpoint(&checkpoint_path)?;

config.path = Some(checkpoint_path);
let opener = StoreOpener::new(home_dir, archive, &config, None);
let node_storage = opener.open()?;

if let Some(columns_to_keep) = columns_to_keep {
let columns_to_keep_set: std::collections::HashSet<
DBCol,
std::collections::hash_map::RandomState,
posvyatokum marked this conversation as resolved.
Show resolved Hide resolved
> = std::collections::HashSet::from_iter(columns_to_keep.into_iter());
let mut transaction = DBTransaction::new();

for col in DBCol::iter() {
if !columns_to_keep_set.contains(&col) {
transaction.delete_all(col);
}
}

node_storage.hot_storage.write(transaction)?;
}

Ok(node_storage)
}