Skip to content

Commit

Permalink
feat(storage): allow configurable cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Jun 23, 2024
1 parent 4014ee1 commit c5c4d52
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/bin/dolos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ pub fn open_data_stores(config: &crate::Config) -> Result<Stores, Error> {

std::fs::create_dir_all(root).map_err(Error::storage)?;

let wal = WalStore::open(root.join("wal")).map_err(Error::storage)?;
let ledger = LedgerStore::open(root.join("ledger")).map_err(Error::storage)?;
let wal = WalStore::open(root.join("wal"), config.storage.wal_cache).map_err(Error::storage)?;

let ledger = LedgerStore::open(root.join("ledger"), config.storage.ledger_cache)
.map_err(Error::storage)?;

Ok((wal, ledger))
}
Expand Down
9 changes: 9 additions & 0 deletions src/bin/dolos/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ struct Cli {
#[derive(Serialize, Deserialize)]
pub struct StorageConfig {
path: std::path::PathBuf,

/// Size (in Mb) of memory allocated for WAL caching
wal_cache: Option<usize>,

/// Size (in Mb) of memory allocated for ledger caching
ledger_cache: Option<usize>,

#[allow(dead_code)]
wal_size: Option<u64>,
}
Expand All @@ -73,6 +80,8 @@ impl Default for StorageConfig {
fn default() -> Self {
Self {
path: PathBuf::from("data"),
wal_cache: None,
ledger_cache: None,
wal_size: None,
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/ledger/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,18 @@ impl LedgerTable for ByAddressIndex {
}
}

const DEFAULT_CACHE_SIZE_MB: usize = 500;

#[derive(Clone)]
pub struct LedgerStore(Arc<redb::Database>);

impl LedgerStore {
pub fn open(path: impl AsRef<Path>) -> Result<Self, redb::Error> {
pub fn open(path: impl AsRef<Path>, cache_size: Option<usize>) -> Result<Self, redb::Error> {
let inner = redb::Database::builder()
.set_repair_callback(|x| {
warn!(progress = x.progress() * 100f64, "ledger db is repairing")
})
.set_cache_size(1024 * 1024 * cache_size.unwrap_or(DEFAULT_CACHE_SIZE_MB))
//.create_with_backend(redb::backends::InMemoryBackend::new())?;
.create(path)?;

Expand Down
5 changes: 4 additions & 1 deletion src/wal/redb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ where
}
}

const DEFAULT_CACHE_SIZE_MB: usize = 50;

/// Concrete implementation of WalStore using Redb
#[derive(Clone)]
pub struct WalStore {
Expand Down Expand Up @@ -123,9 +125,10 @@ impl WalStore {
Ok(out)
}

pub fn open(path: impl AsRef<Path>) -> Result<Self, WalError> {
pub fn open(path: impl AsRef<Path>, cache_size: Option<usize>) -> Result<Self, WalError> {
let inner = redb::Database::builder()
.set_repair_callback(|x| warn!(progress = x.progress() * 100f64, "wal db is repairing"))
.set_cache_size(1024 * 1024 * cache_size.unwrap_or(DEFAULT_CACHE_SIZE_MB))
.create(path)?;

let mut out = Self {
Expand Down

0 comments on commit c5c4d52

Please sign in to comment.