Skip to content

Commit

Permalink
feat(db): Configurable maximum number of open RocksDB files (#1401)
Browse files Browse the repository at this point in the history
## What ❔

Added a field to RocksDBOptions, which is used as an argument to
rocksdb::Options::set_max_open_files().

## Why ❔

This allows Merkle tree users to limit the number of open files required
by RocksDB, which otherwise grows beyond normal OS limits (8192 files)
and requires large amounts of memory (currently, beyond 32GB).

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
  - current (integration) tests run as before
  - external program requiring the change is now running
- [x] Documentation comments have been added / updated.
  - all new fields and methods are documented
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.
  - no links added (nor modified)
  • Loading branch information
vbar committed Mar 14, 2024
1 parent bbe3813 commit b00c052
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/lib/storage/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
ffi::CStr,
fmt, iter,
marker::PhantomData,
num::NonZeroU32,
ops,
path::Path,
sync::{Arc, Condvar, Mutex},
Expand Down Expand Up @@ -274,6 +275,8 @@ pub struct RocksDBOptions {
/// Timeout to wait for the database to run compaction on stalled writes during startup or
/// when the corresponding RocksDB error is encountered.
pub stalled_writes_retries: StalledWritesRetries,
/// Number of open files that can be used by the DB. Default is None, for no limit.
pub max_open_files: Option<NonZeroU32>,
}

impl Default for RocksDBOptions {
Expand All @@ -282,6 +285,7 @@ impl Default for RocksDBOptions {
block_cache_capacity: None,
large_memtable_capacity: None,
stalled_writes_retries: StalledWritesRetries::new(Duration::from_secs(10)),
max_open_files: None,
}
}
}
Expand All @@ -304,7 +308,13 @@ impl<CF: NamedColumnFamily> RocksDB<CF> {

pub fn with_options(path: &Path, options: RocksDBOptions) -> Result<Self, rocksdb::Error> {
let caches = RocksDBCaches::new(options.block_cache_capacity);
let db_options = Self::rocksdb_options(None, None);
let mut db_options = Self::rocksdb_options(None, None);
let max_open_files = if let Some(non_zero) = options.max_open_files {
i32::try_from(non_zero.get()).unwrap_or(i32::MAX)
} else {
-1
};
db_options.set_max_open_files(max_open_files);
let existing_cfs = DB::list_cf(&db_options, path).unwrap_or_else(|err| {
tracing::warn!(
"Failed getting column families for RocksDB `{}` at `{}`, assuming CFs are empty; {err}",
Expand Down
1 change: 1 addition & 0 deletions core/lib/zksync_core/src/metadata_calculator/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ fn create_db_sync(
block_cache_capacity: Some(block_cache_capacity),
large_memtable_capacity: Some(memtable_capacity),
stalled_writes_retries: StalledWritesRetries::new(stalled_writes_timeout),
max_open_files: None,
},
)?;
if cfg!(test) {
Expand Down

0 comments on commit b00c052

Please sign in to comment.