Skip to content

Commit

Permalink
Lock on KV + FST databases acquire()
Browse files Browse the repository at this point in the history
This prevents two disk references to the same database to be open at the same time while 2 threads are concurring.

Signed-off-by: Valerian Saliou <valerian@valeriansaliou.name>
  • Loading branch information
valeriansaliou committed Mar 27, 2019
1 parent 48f0fa3 commit 2628077
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/store/fst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static LOOKUP_REGEX_RANGE_LATIN: &'static str = "[\\x{0000}-\\x{024F}]";

lazy_static! {
pub static ref GRAPH_ACCESS_LOCK: Arc<RwLock<bool>> = Arc::new(RwLock::new(false));
static ref GRAPH_ACQUIRE_LOCK: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
static ref GRAPH_REBUILD_LOCK: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
static ref GRAPH_POOL: Arc<RwLock<HashMap<StoreFSTKey, StoreFSTBox>>> =
Arc::new(RwLock::new(HashMap::new()));
Expand All @@ -99,6 +100,10 @@ impl StoreFSTPool {

let pool_key = StoreFSTKey::from_str(collection_str, bucket_str);

// Freeze acquire lock, and reference it in context
// Notice: this prevents two graphs on the same collection to be opened at the same time.
let _acquire = GRAPH_ACQUIRE_LOCK.lock().unwrap();

// Acquire a thread-safe store pool reference in read mode
let graph_pool_read = GRAPH_POOL.read().unwrap();

Expand Down
7 changes: 6 additions & 1 deletion src/store/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fmt;
use std::fs;
use std::io::Cursor;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::sync::{Arc, Mutex, RwLock};
use std::time::SystemTime;
use std::vec::Drain;

Expand Down Expand Up @@ -57,6 +57,7 @@ type StoreKVBox = Arc<StoreKV>;

lazy_static! {
pub static ref STORE_ACCESS_LOCK: Arc<RwLock<bool>> = Arc::new(RwLock::new(false));
static ref STORE_ACQUIRE_LOCK: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
static ref STORE_POOL: Arc<RwLock<HashMap<StoreKVKey, StoreKVBox>>> =
Arc::new(RwLock::new(HashMap::new()));
}
Expand All @@ -69,6 +70,10 @@ impl StoreKVPool {
let collection_str = collection.into();
let pool_key = StoreKVKey::from_str(collection_str);

// Freeze acquire lock, and reference it in context
// Notice: this prevents two databases on the same collection to be opened at the same time.
let _acquire = STORE_ACQUIRE_LOCK.lock().unwrap();

// Acquire a thread-safe store pool reference in read mode
let store_pool_read = STORE_POOL.read().unwrap();

Expand Down

0 comments on commit 2628077

Please # to comment.