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

Commit 0dca1a9

Browse files
authored
Fix db initialization for light client (#7130)
* Fix db initialization for light client * Fix cache distribution
1 parent 64af157 commit 0dca1a9

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

client/db/src/parity_db.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/// A `Database` adapter for parity-db.
1919
2020
use sp_database::{Database, Change, ColumnId, Transaction, error::DatabaseError};
21-
use crate::utils::NUM_COLUMNS;
21+
use crate::utils::{DatabaseType, NUM_COLUMNS};
2222
use crate::columns;
2323

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

35-
/// Wrap RocksDb database into a trait object that implements `sp_database::Database`
36-
pub fn open<H: Clone>(path: &std::path::Path) -> parity_db::Result<std::sync::Arc<dyn Database<H>>> {
35+
/// Wrap parity-db database into a trait object that implements `sp_database::Database`
36+
pub fn open<H: Clone>(path: &std::path::Path, db_type: DatabaseType)
37+
-> parity_db::Result<std::sync::Arc<dyn Database<H>>>
38+
{
3739
let mut config = parity_db::Options::with_columns(path, NUM_COLUMNS as u8);
38-
let mut state_col = &mut config.columns[columns::STATE as usize];
39-
state_col.ref_counted = true;
40-
state_col.preimage = true;
41-
state_col.uniform = true;
40+
if db_type == DatabaseType::Full {
41+
let mut state_col = &mut config.columns[columns::STATE as usize];
42+
state_col.ref_counted = true;
43+
state_col.preimage = true;
44+
state_col.uniform = true;
45+
}
4246
let db = parity_db::Db::open(&config)?;
4347
Ok(std::sync::Arc::new(DbAdapter(db)))
4448
}

client/db/src/utils.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,31 +227,46 @@ pub fn open_database<Block: BlockT>(
227227

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

236-
for i in 0..NUM_COLUMNS {
237-
if i == crate::columns::STATE {
238-
memory_budget.insert(i, state_col_budget);
239-
} else {
240-
memory_budget.insert(i, other_col_budget);
233+
let mut memory_budget = std::collections::HashMap::new();
234+
match db_type {
235+
DatabaseType::Full => {
236+
let state_col_budget = (*cache_size as f64 * 0.9) as usize;
237+
let other_col_budget = (cache_size - state_col_budget) / (NUM_COLUMNS as usize - 1);
238+
239+
for i in 0..NUM_COLUMNS {
240+
if i == crate::columns::STATE {
241+
memory_budget.insert(i, state_col_budget);
242+
} else {
243+
memory_budget.insert(i, other_col_budget);
244+
}
245+
}
246+
log::trace!(
247+
target: "db",
248+
"Open RocksDB database at {}, state column budget: {} MiB, others({}) column cache: {} MiB",
249+
path,
250+
state_col_budget,
251+
NUM_COLUMNS,
252+
other_col_budget,
253+
);
254+
},
255+
DatabaseType::Light => {
256+
let col_budget = cache_size / (NUM_COLUMNS as usize);
257+
for i in 0..NUM_COLUMNS {
258+
memory_budget.insert(i, col_budget);
259+
}
260+
log::trace!(
261+
target: "db",
262+
"Open RocksDB light database at {}, column cache: {} MiB",
263+
path,
264+
col_budget,
265+
);
241266
}
242267
}
243-
244268
db_config.memory_budget = memory_budget;
245269

246-
log::trace!(
247-
target: "db",
248-
"Open RocksDB database at {}, state column budget: {} MiB, others({}) column cache: {} MiB",
249-
path,
250-
state_col_budget,
251-
NUM_COLUMNS,
252-
other_col_budget,
253-
);
254-
255270
let db = kvdb_rocksdb::Database::open(&db_config, &path)
256271
.map_err(|err| sp_blockchain::Error::Backend(format!("{}", err)))?;
257272
sp_database::as_database(db)
@@ -262,7 +277,7 @@ pub fn open_database<Block: BlockT>(
262277
},
263278
#[cfg(feature = "with-parity-db")]
264279
DatabaseSettingsSrc::ParityDb { path } => {
265-
crate::parity_db::open(&path)
280+
crate::parity_db::open(&path, db_type)
266281
.map_err(|e| sp_blockchain::Error::Backend(format!("{:?}", e)))?
267282
},
268283
#[cfg(not(feature = "with-parity-db"))]

0 commit comments

Comments
 (0)