Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fix db initialization for light client #7130

Merged
2 commits merged into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 11 additions & 7 deletions client/db/src/parity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/// A `Database` adapter for parity-db.

use sp_database::{Database, Change, ColumnId, Transaction, error::DatabaseError};
use crate::utils::NUM_COLUMNS;
use crate::utils::{DatabaseType, NUM_COLUMNS};
use crate::columns;

struct DbAdapter(parity_db::Db);
Expand All @@ -32,13 +32,17 @@ fn handle_err<T>(result: parity_db::Result<T>) -> T {
}
}

/// Wrap RocksDb database into a trait object that implements `sp_database::Database`
pub fn open<H: Clone>(path: &std::path::Path) -> parity_db::Result<std::sync::Arc<dyn Database<H>>> {
/// Wrap parity-db database into a trait object that implements `sp_database::Database`
pub fn open<H: Clone>(path: &std::path::Path, db_type: DatabaseType)
-> parity_db::Result<std::sync::Arc<dyn Database<H>>>
{
let mut config = parity_db::Options::with_columns(path, NUM_COLUMNS as u8);
let mut state_col = &mut config.columns[columns::STATE as usize];
state_col.ref_counted = true;
state_col.preimage = true;
state_col.uniform = true;
if db_type == DatabaseType::Full {
let mut state_col = &mut config.columns[columns::STATE as usize];
state_col.ref_counted = true;
state_col.preimage = true;
state_col.uniform = true;
}
let db = parity_db::Db::open(&config)?;
Ok(std::sync::Arc::new(DbAdapter(db)))
}
Expand Down
53 changes: 34 additions & 19 deletions client/db/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,31 +227,46 @@ pub fn open_database<Block: BlockT>(

// and now open database assuming that it has the latest version
let mut db_config = kvdb_rocksdb::DatabaseConfig::with_columns(NUM_COLUMNS);
let state_col_budget = (*cache_size as f64 * 0.9) as usize;
let other_col_budget = (cache_size - state_col_budget) / (NUM_COLUMNS as usize - 1);
let mut memory_budget = std::collections::HashMap::new();
let path = path.to_str()
.ok_or_else(|| sp_blockchain::Error::Backend("Invalid database path".into()))?;

for i in 0..NUM_COLUMNS {
if i == crate::columns::STATE {
memory_budget.insert(i, state_col_budget);
} else {
memory_budget.insert(i, other_col_budget);
let mut memory_budget = std::collections::HashMap::new();
match db_type {
DatabaseType::Full => {
let state_col_budget = (*cache_size as f64 * 0.9) as usize;
let other_col_budget = (cache_size - state_col_budget) / (NUM_COLUMNS as usize - 1);

for i in 0..NUM_COLUMNS {
if i == crate::columns::STATE {
memory_budget.insert(i, state_col_budget);
} else {
memory_budget.insert(i, other_col_budget);
}
}
log::trace!(
target: "db",
"Open RocksDB database at {}, state column budget: {} MiB, others({}) column cache: {} MiB",
path,
state_col_budget,
NUM_COLUMNS,
other_col_budget,
);
},
DatabaseType::Light => {
let col_budget = cache_size / (NUM_COLUMNS as usize);
for i in 0..NUM_COLUMNS {
memory_budget.insert(i, col_budget);
}
log::trace!(
target: "db",
"Open RocksDB light database at {}, column cache: {} MiB",
path,
col_budget,
);
}
}

db_config.memory_budget = memory_budget;

log::trace!(
target: "db",
"Open RocksDB database at {}, state column budget: {} MiB, others({}) column cache: {} MiB",
path,
state_col_budget,
NUM_COLUMNS,
other_col_budget,
);

let db = kvdb_rocksdb::Database::open(&db_config, &path)
.map_err(|err| sp_blockchain::Error::Backend(format!("{}", err)))?;
sp_database::as_database(db)
Expand All @@ -262,7 +277,7 @@ pub fn open_database<Block: BlockT>(
},
#[cfg(feature = "with-parity-db")]
DatabaseSettingsSrc::ParityDb { path } => {
crate::parity_db::open(&path)
crate::parity_db::open(&path, db_type)
.map_err(|e| sp_blockchain::Error::Backend(format!("{:?}", e)))?
},
#[cfg(not(feature = "with-parity-db"))]
Expand Down