Skip to content

Commit

Permalink
first it
Browse files Browse the repository at this point in the history
  • Loading branch information
dejankos committed Sep 3, 2020
1 parent 3a7d694 commit a09f2db
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ For service performance tuning check example service_config.toml and yes - ```wo
Example configuration is provided under ```project_root/config```

### TODO
- [ ] IT
- [ ] impl From for errors
- [ ] support for more rocksDb options in config (bloom, block cache..)
- [ ] db iterator for on startup init
- [ ] channel for expire
- [ ] IT
- [ ] test compaction
- [ ] code coverage
- [ ] performance ?
- [ ] docker
- [ ] range scan
- [ ] performance


## Licence
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct ServiceConfig {
}

#[derive(Debug)]
pub struct DbConfig(RocksDbConfig);
pub struct DbConfig(pub RocksDbConfig);

impl DbConfig {
fn new(rocks_cfg: RocksDbConfig) -> Self {
Expand Down
11 changes: 8 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ impl DbManager {
.set_compaction_filter("expiration-filter", compaction_filter);

let root_db = open_root_db(&db_cfg)?;
Ok(DbManager {
let db_manager = DbManager {
db_cfg,
root_db,
dbs: Arc::new(ShardedLock::new(HashMap::new())),
executor: Mutex::new(ThreadPoolExecutor::new(1)),
})
};
db_manager.init();

Ok(db_manager)
}

// will panic in main thread and prevent startup
pub fn init(&self) {
fn init(&self) {
info!("Initializing dbs from root ...");
//TODO db iterator
self.root_db
Expand Down Expand Up @@ -150,6 +153,8 @@ impl DbManager {
} else {
if let Some(db) = self.w_lock().remove(&db_name) {
info!("Closing db = {} ...", &db_name);
self.root_db.w_lock().delete(&db_name)?;
//possible expensive call moved to separate thread TODO channels
self.tp_mutex().execute(move || match db.close(&db_name) {
Ok(_) => info!("Db = {} closed", &db_name),
Err(e) => error!("Error closing db = {}, e = {}", &db_name, e),
Expand Down
60 changes: 59 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ async fn main() -> std::io::Result<()> {
info!("Loaded db configuration = {:#?}", &db_cfg);

let db_manager = DbManager::new(db_cfg)?;
db_manager.init();
let db_manager = web::Data::new(db_manager);

let prometheus = init_prometheus();
Expand Down Expand Up @@ -218,3 +217,62 @@ fn init_logger(log_path: &str, dev_mode: bool) {
WriteLogger::init(LevelFilter::Info, cfg, log_file).expect("Failed to init file logger")
}
}

#[cfg(test)]
mod tests {

use actix_web::dev::ServiceResponse;
use actix_web::http::StatusCode;
use actix_web::{test, web, App, Error};

use crate::config::{DbConfig, RocksDbConfig};
use crate::conversion::bytes_to_str;

use super::*;

impl DbConfig {
pub fn new_with_defaults() -> Self {
DbConfig(RocksDbConfig::default())
}
}

#[actix_rt::test]
async fn should_open_and_close_db() -> Result<(), Error> {
std::env::set_var("RUST_BACKTRACE", "full");

let db_manager = DbManager::new(DbConfig::new_with_defaults())?;
let mut app = test::init_service(
App::new()
.app_data(web::Data::new(db_manager))
.service(open)
.service(close),
)
.await;

let req = test::TestRequest::post().uri("/test_db").to_request();
let res = test::call_service(&mut app, req).await;
assert_eq!(
StatusCode::OK,
res.status(),
"Received payload:: {:?}",
response_as_str(res)
);

let req = test::TestRequest::delete().uri("/test_db").to_request();
let res = test::call_service(&mut app, req).await;
assert_eq!(
StatusCode::OK,
res.status(),
"Received payload:: {:?}",
response_as_str(res)
);
Ok(())
}

fn response_as_str(res: ServiceResponse<Body>) -> Conversion<String> {
match res.response().body().as_ref() {
Some(Body::Bytes(bytes)) => bytes_to_str(bytes),
_ => Ok("empty".to_string()),
}
}
}

0 comments on commit a09f2db

Please sign in to comment.