mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-25 08:15:44 +00:00
Fix db initialization for light client (#7130)
* Fix db initialization for light client * Fix cache distribution
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
/// A `Database` adapter for parity-db.
|
/// A `Database` adapter for parity-db.
|
||||||
|
|
||||||
use sp_database::{Database, Change, ColumnId, Transaction, error::DatabaseError};
|
use sp_database::{Database, Change, ColumnId, Transaction, error::DatabaseError};
|
||||||
use crate::utils::NUM_COLUMNS;
|
use crate::utils::{DatabaseType, NUM_COLUMNS};
|
||||||
use crate::columns;
|
use crate::columns;
|
||||||
|
|
||||||
struct DbAdapter(parity_db::Db);
|
struct DbAdapter(parity_db::Db);
|
||||||
@@ -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`
|
/// Wrap parity-db 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>>> {
|
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 config = parity_db::Options::with_columns(path, NUM_COLUMNS as u8);
|
||||||
let mut state_col = &mut config.columns[columns::STATE as usize];
|
if db_type == DatabaseType::Full {
|
||||||
state_col.ref_counted = true;
|
let mut state_col = &mut config.columns[columns::STATE as usize];
|
||||||
state_col.preimage = true;
|
state_col.ref_counted = true;
|
||||||
state_col.uniform = true;
|
state_col.preimage = true;
|
||||||
|
state_col.uniform = true;
|
||||||
|
}
|
||||||
let db = parity_db::Db::open(&config)?;
|
let db = parity_db::Db::open(&config)?;
|
||||||
Ok(std::sync::Arc::new(DbAdapter(db)))
|
Ok(std::sync::Arc::new(DbAdapter(db)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,31 +227,46 @@ pub fn open_database<Block: BlockT>(
|
|||||||
|
|
||||||
// and now open database assuming that it has the latest version
|
// and now open database assuming that it has the latest version
|
||||||
let mut db_config = kvdb_rocksdb::DatabaseConfig::with_columns(NUM_COLUMNS);
|
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()
|
let path = path.to_str()
|
||||||
.ok_or_else(|| sp_blockchain::Error::Backend("Invalid database path".into()))?;
|
.ok_or_else(|| sp_blockchain::Error::Backend("Invalid database path".into()))?;
|
||||||
|
|
||||||
for i in 0..NUM_COLUMNS {
|
let mut memory_budget = std::collections::HashMap::new();
|
||||||
if i == crate::columns::STATE {
|
match db_type {
|
||||||
memory_budget.insert(i, state_col_budget);
|
DatabaseType::Full => {
|
||||||
} else {
|
let state_col_budget = (*cache_size as f64 * 0.9) as usize;
|
||||||
memory_budget.insert(i, other_col_budget);
|
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;
|
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)
|
let db = kvdb_rocksdb::Database::open(&db_config, &path)
|
||||||
.map_err(|err| sp_blockchain::Error::Backend(format!("{}", err)))?;
|
.map_err(|err| sp_blockchain::Error::Backend(format!("{}", err)))?;
|
||||||
sp_database::as_database(db)
|
sp_database::as_database(db)
|
||||||
@@ -262,7 +277,7 @@ pub fn open_database<Block: BlockT>(
|
|||||||
},
|
},
|
||||||
#[cfg(feature = "with-parity-db")]
|
#[cfg(feature = "with-parity-db")]
|
||||||
DatabaseSettingsSrc::ParityDb { path } => {
|
DatabaseSettingsSrc::ParityDb { path } => {
|
||||||
crate::parity_db::open(&path)
|
crate::parity_db::open(&path, db_type)
|
||||||
.map_err(|e| sp_blockchain::Error::Backend(format!("{:?}", e)))?
|
.map_err(|e| sp_blockchain::Error::Backend(format!("{:?}", e)))?
|
||||||
},
|
},
|
||||||
#[cfg(not(feature = "with-parity-db"))]
|
#[cfg(not(feature = "with-parity-db"))]
|
||||||
|
|||||||
Reference in New Issue
Block a user