-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add rlimit dependency and refactor database connection builder Added the `rlimit` package as a new dependency. Rlimit dynamically determines the file limit based on the host platform, and is relevant for situations where the maximum file limit may exceed 1024. Furthermore, the open_db function in `db` modules was refactored and moved to a new module called `conn_builder`. This restructuring allows for greater adaptability and customization when establishing a database connection. It is now possible to set various parameters such as enabling statistics, setting memory budgets, adjusting Parallelism and a toggle for database creation if none exists. * Enhance rocksdb configuration in the simulation environment SimPa's network.rs file and many test files have been modified to provide more flexibility in configuring the underlying rocksdb. The number of threads, the memory budget, file descriptors limit and the statistics period can now be tweaked at runtime via command line arguments. Functions to create and load database instances were also refactored into macros for better maintainability. Lastly, some unused import statements were cleaned up. All these changes provide better control over the simulation and test environment and help optimize resource usage. * Refactor database creation and stats handling Refactored the code for creating and setting up the RocksDB databases to use match expressions instead of nested if-let statements. This simplification enhances code readability and maintainability. It also makes it easier to track the conditions for creating permanent and temporary databases and enabling their statistics. The similar refactoring was also extended to the logic for loading existing database. * Refactor ConnBuilder struct and remove StatsPeriod trait The ConnBuilder struct has been refactored to use struct update syntax reducing redundancy in method definitions whilst simultaneously improving code readability. The StatsPeriod trait has also been removed since it was no longer needed, which simplifies the codebase. The associated implementation for stats_period trait in ConnBuilder methods were removed accordingly.
- Loading branch information
1 parent
ce3e231
commit c69ac13
Showing
18 changed files
with
291 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ tempfile.workspace = true | |
|
||
enum-primitive-derive = "0.2.2" | ||
num-traits = "0.2.15" | ||
rlimit = "0.10.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
use crate::db::DB; | ||
use rlimit::Resource; | ||
use std::cmp::min; | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct Unspecified; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ConnBuilder<Path: Clone, const STATS_ENABLED: bool, StatsPeriod: Clone> { | ||
db_path: Path, | ||
create_if_missing: bool, | ||
parallelism: usize, | ||
files_limit: i32, | ||
mem_budget: usize, | ||
stats_period: StatsPeriod, | ||
} | ||
|
||
impl Default for ConnBuilder<Unspecified, false, Unspecified> { | ||
fn default() -> Self { | ||
ConnBuilder { | ||
db_path: Unspecified, | ||
create_if_missing: true, | ||
parallelism: 1, | ||
files_limit: 500, | ||
mem_budget: 64 * 1024 * 1024, | ||
stats_period: Unspecified, | ||
} | ||
} | ||
} | ||
|
||
impl<Path: Clone, const STATS_ENABLED: bool, StatsPeriod: Clone> ConnBuilder<Path, STATS_ENABLED, StatsPeriod> { | ||
pub fn with_db_path(self, db_path: PathBuf) -> ConnBuilder<PathBuf, STATS_ENABLED, StatsPeriod> { | ||
ConnBuilder { | ||
db_path, | ||
create_if_missing: self.create_if_missing, | ||
parallelism: self.parallelism, | ||
files_limit: self.files_limit, | ||
mem_budget: self.mem_budget, | ||
stats_period: self.stats_period, | ||
} | ||
} | ||
pub fn with_create_if_missing(self, create_if_missing: bool) -> ConnBuilder<Path, STATS_ENABLED, StatsPeriod> { | ||
ConnBuilder { create_if_missing, ..self } | ||
} | ||
pub fn with_parallelism(self, parallelism: impl Into<usize>) -> ConnBuilder<Path, STATS_ENABLED, StatsPeriod> { | ||
ConnBuilder { parallelism: parallelism.into(), ..self } | ||
} | ||
pub fn with_files_limit(self, files_limit: impl Into<i32>) -> ConnBuilder<Path, STATS_ENABLED, StatsPeriod> { | ||
ConnBuilder { files_limit: files_limit.into(), ..self } | ||
} | ||
pub fn with_mem_budget(self, mem_budget: impl Into<usize>) -> ConnBuilder<Path, STATS_ENABLED, StatsPeriod> { | ||
ConnBuilder { mem_budget: mem_budget.into(), ..self } | ||
} | ||
} | ||
|
||
impl<Path: Clone> ConnBuilder<Path, false, Unspecified> { | ||
pub fn enable_stats(self) -> ConnBuilder<Path, true, Unspecified> { | ||
ConnBuilder { | ||
db_path: self.db_path, | ||
create_if_missing: self.create_if_missing, | ||
parallelism: self.parallelism, | ||
files_limit: self.files_limit, | ||
mem_budget: self.mem_budget, | ||
stats_period: self.stats_period, | ||
} | ||
} | ||
} | ||
|
||
impl<Path: Clone, StatsPeriod: Clone> ConnBuilder<Path, true, StatsPeriod> { | ||
pub fn disable_stats(self) -> ConnBuilder<Path, false, Unspecified> { | ||
ConnBuilder { | ||
db_path: self.db_path, | ||
create_if_missing: self.create_if_missing, | ||
parallelism: self.parallelism, | ||
files_limit: self.files_limit, | ||
mem_budget: self.mem_budget, | ||
stats_period: Unspecified, | ||
} | ||
} | ||
pub fn with_stats_period(self, stats_period: impl Into<u32>) -> ConnBuilder<Path, true, u32> { | ||
ConnBuilder { | ||
db_path: self.db_path, | ||
create_if_missing: self.create_if_missing, | ||
parallelism: self.parallelism, | ||
files_limit: self.files_limit, | ||
mem_budget: self.mem_budget, | ||
stats_period: stats_period.into(), | ||
} | ||
} | ||
} | ||
|
||
macro_rules! default_opts { | ||
($self: expr) => {{ | ||
let mut opts = rocksdb::Options::default(); | ||
if $self.parallelism > 1 { | ||
opts.increase_parallelism($self.parallelism as i32); | ||
} | ||
opts.optimize_level_style_compaction($self.mem_budget); | ||
|
||
#[cfg(target_os = "windows")] | ||
let files_limit = rlimit::getmaxstdio() as i32; | ||
#[cfg(any(target_os = "macos", target_os = "linux"))] | ||
let files_limit = rlimit::getrlimit(Resource::NOFILE).unwrap().0 as i32; | ||
// In most linux environments the limit is set to 1024, so we use 500 to give sufficient slack. | ||
// TODO: fine-tune this parameter and additional parameters related to max file size | ||
opts.set_max_open_files(min(files_limit, $self.files_limit)); | ||
opts.create_if_missing($self.create_if_missing); | ||
opts | ||
}}; | ||
} | ||
|
||
impl ConnBuilder<PathBuf, false, Unspecified> { | ||
pub fn build(self) -> Arc<DB> { | ||
let opts = default_opts!(self); | ||
let db = Arc::new(DB::open(&opts, self.db_path.to_str().unwrap()).unwrap()); | ||
db | ||
} | ||
} | ||
|
||
impl ConnBuilder<PathBuf, true, Unspecified> { | ||
pub fn build(self) -> Arc<DB> { | ||
let mut opts = default_opts!(self); | ||
opts.enable_statistics(); | ||
let db = Arc::new(DB::open(&opts, self.db_path.to_str().unwrap()).unwrap()); | ||
db | ||
} | ||
} | ||
|
||
impl ConnBuilder<PathBuf, true, u32> { | ||
pub fn build(self) -> Arc<DB> { | ||
let mut opts = default_opts!(self); | ||
opts.enable_statistics(); | ||
opts.set_report_bg_io_stats(true); | ||
opts.set_stats_dump_period_sec(self.stats_period); | ||
let db = Arc::new(DB::open(&opts, self.db_path.to_str().unwrap()).unwrap()); | ||
db | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.