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

Set default max WAL size per RocksDB and allow configuration via env var #118

Merged
merged 2 commits into from
Aug 11, 2022
Merged
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
56 changes: 49 additions & 7 deletions crates/typed-store/src/rocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ use bincode::Options;
use collectable::TryExtend;
use rocksdb::{ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, WriteBatch};
use serde::{de::DeserializeOwned, Serialize};
use std::{borrow::Borrow, marker::PhantomData, path::Path, sync::Arc};
use tracing::instrument;
use std::{borrow::Borrow, env, marker::PhantomData, path::Path, sync::Arc};
use tracing::{info, instrument};

use self::{iter::Iter, keys::Keys, values::Values};
pub use errors::TypedStoreError;

// The default bytes limit on total memtable size per RocksDB.
const DB_WRITE_BUFFER_SIZE: usize = 512 * 1024 * 1024;
// Write buffer size per RocksDB instance can be set via the env var below.
// If the env var is not set, use the default value in MiB.
const ENV_VAR_DB_WRITE_BUFFER_SIZE: &str = "MYSTEN_DB_WRITE_BUFFER_SIZE_MB";
const DEFAULT_DB_WRITE_BUFFER_SIZE: usize = 512;

// Write ahead log size per RocksDB instance can be set via the env var below.
// If the env var is not set, use the default value in MiB.
const ENV_VAR_DB_WAL_SIZE: &str = "MYSTEN_DB_WAL_SIZE_MB";
const DEFAULT_DB_WAL_SIZE: usize = 1024;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -430,11 +437,46 @@ where
}
}

/// Creates a default Rocksdb option.
fn read_size_from_env(var_name: &str) -> Option<usize> {
match env::var(var_name) {
Ok(val) => match val.parse::<usize>() {
Ok(i) => Some(i),
Err(e) => {
info!(
"Env var {} does not contain valid usize integer: {}",
var_name, e
);
Option::None
}
},
Err(e) => {
info!("Env var {} is not set: {}", var_name, e);
Option::None
}
}
Comment on lines +442 to +456
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Option::None is pedantic, None would suffice.
  2. Consider using and_then:
    env::var(var_name).and_then(|v| v.parse::<usize>()).ok()
  3. and then to implement your rich logging, you can use tap, specifically tap_err.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I turned on auto merge. Will address the feedback in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, those are stylistic recommendations, and aside from the first, not really binding.

}

/// Creates a default RocksDB option, to be used when RocksDB option is not specified..
pub fn default_rocksdb_options() -> rocksdb::Options {
let mut opt = rocksdb::Options::default();
// Limit the total memtable usage per db.
opt.set_db_write_buffer_size(DB_WRITE_BUFFER_SIZE);
// Sui uses multiple RocksDB in a node, so total sizes of write buffers and WAL can be higher
// than the limits below.
//
// RocksDB also exposes the option to configure total write buffer size across multiple instances
// via `write_buffer_manager`. But the write buffer flush policy (flushing the buffer receiving
// the next write) may not work well. So sticking to per-db write buffer size limit for now.
//
// The environment variables are only meant to be emergency overrides. They may go away in future.
// If you need to modify an option, either update the default value, or override the option in
// Sui / Narwhal.
opt.set_db_write_buffer_size(
read_size_from_env(ENV_VAR_DB_WRITE_BUFFER_SIZE).unwrap_or(DEFAULT_DB_WRITE_BUFFER_SIZE)
* 1024
* 1024,
);
opt.set_max_total_wal_size(
read_size_from_env(ENV_VAR_DB_WAL_SIZE).unwrap_or(DEFAULT_DB_WAL_SIZE) as u64 * 1024 * 1024,
);
opt
}

Expand Down