Skip to content

Commit

Permalink
database: use rayon for service's reader thread-pool (#93)
Browse files Browse the repository at this point in the history
* add `rayon 1.9.0`

* service: re-impl reader threadpool with `rayon`

* service: impl `tower::Service` for writer

* backend: create db dir in `Env::open`

* service: read + write request/response tests

* docs, name changes

* service: always return `Poll::Ready` in writer

* service: use `spawn()` instead of `install()`

* service: replace `DatabaseReader` with free functions

* cargo: add `tokio-utils`

* service: acquire permit before `call()` for read requests

* service: acquire permit in tests

* docs

* service: use loop for write request tests

* service: use `ready!()`
  • Loading branch information
hinto-janai authored Mar 21, 2024
1 parent 93372fa commit 004bb15
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 176 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ paste = { version = "1.0.14", default-features = false }
pin-project = { version = "1.1.3", default-features = false }
randomx-rs = { git = "https://github.com/Cuprate/randomx-rs.git", rev = "0028464", default-features = false }
rand = { version = "0.8.5", default-features = false }
rayon = { version = "1.8.0", default-features = false }
rayon = { version = "1.9.0", default-features = false }
serde_bytes = { version = "0.11.12", default-features = false }
serde_json = { version = "1.0.108", default-features = false }
serde = { version = "1.0.190", default-features = false }
Expand Down
15 changes: 9 additions & 6 deletions database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/database"
keywords = ["cuprate", "database"]

[features]
default = ["heed", "redb", "service"]
# default = ["redb", "service"]
# default = ["heed", "redb", "service"]
default = ["redb", "service"]
heed = ["dep:heed"]
redb = ["dep:redb"]
service = ["dep:crossbeam", "dep:tokio", "dep:tower"]
service = ["dep:crossbeam", "dep:futures", "dep:tokio", "dep:tokio-util", "dep:tower", "dep:rayon"]

[dependencies]
bytemuck = { version = "1.14.3", features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] }
Expand All @@ -27,9 +27,12 @@ page_size = { version = "0.6.0" } # Needed for database resizes, they must
thiserror = { workspace = true }

# `service` feature.
crossbeam = { workspace = true, features = ["std"], optional = true }
tokio = { workspace = true, features = ["full"], optional = true }
tower = { workspace = true, features = ["full"], optional = true }
crossbeam = { workspace = true, features = ["std"], optional = true }
futures = { workspace = true, optional = true }
tokio = { workspace = true, features = ["full"], optional = true }
tokio-util = { workspace = true, features = ["full"], optional = true }
tower = { workspace = true, features = ["full"], optional = true }
rayon = { workspace = true, optional = true }

# Optional features.
heed = { version = "0.20.0-alpha.9", optional = true }
Expand Down
2 changes: 2 additions & 0 deletions database/src/backend/heed/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ impl Env for ConcreteEnv {
reader_threads + 16
});

// Create the database directory if it doesn't exist.
std::fs::create_dir_all(config.db_directory())?;
// Open the environment in the user's PATH.
let env = env_open_options.open(config.db_directory())?;

Expand Down
3 changes: 3 additions & 0 deletions database/src/backend/redb/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ impl Env for ConcreteEnv {
// TODO: we can set cache sizes with:
// env_builder.set_cache(bytes);

// Create the database directory if it doesn't exist.
std::fs::create_dir_all(config.db_directory())?;

// Open the database file, create if needed.
let db_file = std::fs::OpenOptions::new()
.read(true)
Expand Down
2 changes: 1 addition & 1 deletion database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
keyword_idents,
non_ascii_idents,
variant_size_differences,
unused_mut, // Annoying when debugging, maybe put in allow.
// Probably can be put into `#[deny]`.
future_incompatible,
Expand All @@ -168,6 +167,7 @@
clippy::pedantic,
clippy::nursery,
clippy::cargo,
unused_mut,
missing_docs,
deprecated,
unused_comparisons,
Expand Down
11 changes: 4 additions & 7 deletions database/src/service/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use std::sync::Arc;
use crate::{
config::Config,
error::InitError,
service::{
read::DatabaseReader, write::DatabaseWriter, DatabaseReadHandle, DatabaseWriteHandle,
},
service::{write::DatabaseWriter, DatabaseReadHandle, DatabaseWriteHandle},
ConcreteEnv, Env,
};

Expand Down Expand Up @@ -39,11 +37,10 @@ pub fn init(config: Config) -> Result<(DatabaseReadHandle, DatabaseWriteHandle),
let db: Arc<ConcreteEnv> = Arc::new(ConcreteEnv::open(config)?);

// Spawn the Reader thread pool and Writer.
let readers = DatabaseReader::init(&db, reader_threads);
let writers = DatabaseWriter::init(db);
let readers = DatabaseReadHandle::init(&db, reader_threads);
let writer = DatabaseWriteHandle::init(db);

// Return the handles to those pools.
Ok((readers, writers))
Ok((readers, writer))
}

//---------------------------------------------------------------------------------------------------- Tests
Expand Down
2 changes: 1 addition & 1 deletion database/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//!
//! Upon dropping the [`crate::ConcreteEnv`]:
//! - All un-processed database transactions are completed
//! - All data gets flushed to disk (caused by [`Drop::drop`] impl of [`crate::ConcreteEnv`])
//! - All data gets flushed to disk (caused by [`Drop::drop`] impl on [`crate::ConcreteEnv`])
//!
//! ## Request and Response
//! To interact with the database (whether reading or writing data),
Expand Down
Loading

0 comments on commit 004bb15

Please sign in to comment.