1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
use super::*;
pub struct LockManager {
lock_file: fs::File,
excl_lock_file: fs::File,
state: LockState,
}
#[derive(Debug, PartialEq, Eq)]
enum LockState {
NotLocked,
Shared,
Exclusive,
}
impl LockManager {
pub fn new(data_dir_path: PathBuf) -> DBResult<LockManager> {
let lock_file = fs::File::create(data_dir_path.join(LOCK_FILENAME))?;
let excl_lock_file = fs::File::create(data_dir_path.join(EXCL_LOCK_REQ_FILENAME))?;
Ok(LockManager {
lock_file,
excl_lock_file,
state: LockState::NotLocked,
})
}
fn is_exclusive_lock_requested(&self) -> DBResult<bool> {
// Attempt to acquire a shared lock on the lock request file
// If the file is already locked, return false
match fs2::FileExt::try_lock_shared(&self.excl_lock_file) {
Err(e) => {
if e.kind() == fs2::lock_contended_error().kind() {
return Ok(true);
}
return Err(DBError::IOError(e));
}
Ok(_) => {
fs2::FileExt::unlock(&self.excl_lock_file)?;
return Ok(false);
}
}
}
pub fn lock_shared(&mut self) -> DBResult<()> {
if self.state == LockState::Shared {
return Err(DBError::LockRequestError(
"Already holding a shared lock".to_owned(),
));
} else if self.state == LockState::Exclusive {
return Err(DBError::LockRequestError(
"Cannot acquire shared lock while holding an exclusive lock".to_owned(),
));
}
let mut timeout = 5;
loop {
if self.is_exclusive_lock_requested()? {
debug!(
"Exclusive lock requested, waiting for {}ms before requesting a shared lock again",
timeout
);
thread::sleep(std::time::Duration::from_millis(timeout));
timeout *= 2;
if timeout > LOCK_WAIT_MAX_MS {
return Err(DBError::LockRequestError(
"Acquisition of shared lock timed out after {LOCK_WAIT_MAX_MS}".to_owned(),
));
}
} else {
fs2::FileExt::lock_shared(&self.lock_file)?;
self.state = LockState::Shared;
return Ok(());
}
}
}
pub fn lock_exclusive(&mut self) -> DBResult<()> {
if self.state == LockState::Exclusive {
return Err(DBError::LockRequestError(
"Already holding an exclusive lock".to_owned(),
));
} else if self.state == LockState::Shared {
return Err(DBError::LockRequestError(
"Cannot acquire exclusive lock while holding a shared lock".to_owned(),
));
}
// Create a lock on the exclusive lock request file to signal to readers that they should wait
// This will block until the lock is acquired
fs2::FileExt::lock_exclusive(&self.excl_lock_file)?;
// Acquire an exclusive lock on the actual lock files
fs2::FileExt::lock_exclusive(&self.lock_file)?;
self.state = LockState::Exclusive;
// Unlock the request file
fs2::FileExt::unlock(&self.excl_lock_file)?;
Ok(())
}
pub fn unlock(&mut self) -> DBResult<()> {
if self.state == LockState::NotLocked {
return Err(DBError::LockRequestError(
"Not holding any locks".to_owned(),
));
}
fs2::FileExt::unlock(&self.lock_file)?;
self.state = LockState::NotLocked;
Ok(())
}
}
|