Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
add exclusive tx run method, #44
Browse files Browse the repository at this point in the history
  • Loading branch information
burmecia committed Jan 14, 2020
1 parent 330ed30 commit f90d382
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,10 @@ impl File {
let tx_handle = self.tx_handle.take().unwrap();
let mut end_pos = 0;

tx_handle.run(|| {
tx_handle.run_all_exclusive(|| {
end_pos = wtr.finish()?;
Ok(())
})?;
tx_handle.commit()?;

// set position
self.pos = SeekFrom::Start(end_pos as u64);
Expand Down Expand Up @@ -562,7 +561,7 @@ impl File {

let txmgr = self.handle.txmgr.upgrade().ok_or(Error::RepoClosed)?;
let tx_handle = TxMgr::begin_trans(&txmgr)?;
tx_handle.run_all(|| {
tx_handle.run_all_exclusive(|| {
Fnode::set_len(self.handle.clone(), len, tx_handle.txid)
})?;

Expand Down
6 changes: 3 additions & 3 deletions src/fs/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Fs {

let mut fnode = FnodeRef::default();
let tx_handle = TxMgr::begin_trans(&self.txmgr)?;
tx_handle.run_all(|| {
tx_handle.run_all_exclusive(|| {
fnode = Fnode::new_under(
&parent,
&name,
Expand Down Expand Up @@ -500,7 +500,7 @@ impl Fs {

// begin and run transaction
let tx_handle = TxMgr::begin_trans(&self.txmgr)?;
tx_handle.run_all(move || {
tx_handle.run_all_exclusive(move || {
Fnode::remove_from_parent(&fnode_ref, &self.txmgr)?;
let mut fnode = fnode_ref.write().unwrap();
fnode
Expand Down Expand Up @@ -612,7 +612,7 @@ impl Fs {
let (tgt_parent, name) = self.resolve_parent(to)?;

// begin and run transaction
TxMgr::begin_trans(&self.txmgr)?.run_all(|| {
TxMgr::begin_trans(&self.txmgr)?.run_all_exclusive(|| {
// remove from source
Fnode::remove_from_parent(&src, &self.txmgr)?;

Expand Down
17 changes: 16 additions & 1 deletion src/trans/txmgr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::sync::{Arc, RwLock, Weak};
use std::sync::{Arc, Mutex, RwLock, Weak};

use linked_hash_map::LinkedHashMap;

Expand Down Expand Up @@ -189,6 +189,11 @@ impl IntoRef for TxMgr {}
pub type TxMgrRef = Arc<RwLock<TxMgr>>;
pub type TxMgrWeakRef = Weak<RwLock<TxMgr>>;

// lock for running exclusive transactions
lazy_static! {
static ref EXCL_TX_LOCK: Arc<Mutex<usize>> = { Arc::new(Mutex::new(0)) };
}

// Transaction handle
#[derive(Debug, Default, Clone)]
pub struct TxHandle {
Expand Down Expand Up @@ -221,6 +226,16 @@ impl TxHandle {
}
}

/// Run operations in transaction exclusively and commit
#[inline]
pub fn run_all_exclusive<F>(&self, oper: F) -> Result<()>
where
F: FnOnce() -> Result<()>,
{
let _lock = EXCL_TX_LOCK.lock().unwrap();
self.run_all(oper)
}

/// Commit a transaction
#[inline]
pub fn commit(&self) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions tests/common/fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ impl Fuzzer {
// run the fuzz test
pub fn run(
fuzzer: Arc<RwLock<Fuzzer>>,
tester: Arc<RwLock<Testable>>,
tester: Arc<RwLock<dyn Testable>>,
rounds: usize,
worker_cnt: usize,
) {
Expand Down Expand Up @@ -799,7 +799,7 @@ impl Fuzzer {
}

// load fuzz test and re-run it
pub fn rerun(batch: &str, tester: Box<Testable>) {
pub fn rerun(batch: &str, tester: Box<dyn Testable>) {
// load fuzzer
let mut fuzzer = Fuzzer::load(batch);

Expand Down
5 changes: 3 additions & 2 deletions tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn file_read_write_st() {
OpenOptions::new()
.create(true)
.open(&mut repo, "/file/new")
.is_err();
.unwrap_err();

// write data to the file should okay
let mut f = OpenOptions::new()
Expand Down Expand Up @@ -400,7 +400,8 @@ fn file_read_write_mt() {
.create(true)
.open(&mut env.repo, &path)
.unwrap();
f.write_once(&buf[..]).unwrap();
f.write_all(&buf[..]).unwrap();
f.finish().unwrap();
}
}));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn repo_oper() {
assert_eq!(info.ops_limit(), OpsLimit::Moderate);
assert_eq!(info.mem_limit(), MemLimit::Interactive);
}
RepoOpener::new().open(&path, &pwd).is_err();
RepoOpener::new().open(&path, &pwd).unwrap_err();
let repo = RepoOpener::new().open(&path, &new_pwd).unwrap();
let info = repo.info().unwrap();
assert_eq!(info.ops_limit(), OpsLimit::Moderate);
Expand Down

0 comments on commit f90d382

Please sign in to comment.