-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathoracle.rs
310 lines (245 loc) · 8.74 KB
/
oracle.rs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::{
collections::HashSet,
sync::{Arc, Mutex},
};
use yatp::{task::callback::TaskCell, Builder, ThreadPool};
use super::transaction::Transaction;
use crate::{closer::Closer, watermark::WaterMark, AgateOptions};
struct CommittedTxn {
ts: u64,
// Keeps track of the entries written at timestamp ts.
conflict_keys: HashSet<u64>,
}
#[derive(Default)]
struct CommitInfo {
next_txn_ts: u64,
/// Used by managed DB.
discard_ts: u64,
/// Contains all committed writes (contains fingerprints of keys written
/// and their latest commit counter).
committed_txns: Vec<CommittedTxn>,
last_cleanup_ts: u64,
}
impl CommitInfo {
fn cleanup_committed_transactions(&mut self, is_managed: bool, read_mark: Arc<WaterMark>) {
let max_read_ts = if is_managed {
self.discard_ts
} else {
read_mark.done_until()
};
assert!(max_read_ts >= self.last_cleanup_ts);
if max_read_ts == self.last_cleanup_ts {
return;
}
self.last_cleanup_ts = max_read_ts;
self.committed_txns.retain(|txn| txn.ts > max_read_ts);
}
fn has_conflict(&self, txn: &Transaction) -> bool {
let reads = txn.reads.lock().unwrap();
if reads.is_empty() {
false
} else {
// If the committed_txn.ts is less than txn.read_ts that implies that the
// committed_txn finished before the current transaction started.
// We don't need to check for conflict in that case.
// This change assumes linearizability. Lack of linearizability could
// cause the read ts of a new txn to be lower than the commit ts of
// a txn before it.
self.committed_txns
.iter()
.filter(|committed_txn| committed_txn.ts > txn.read_ts)
.any(|committed_txn| {
reads
.iter()
.any(|read| committed_txn.conflict_keys.contains(read))
})
}
}
}
/// Supplies read timestamp and commit timestamp for transaction.
/// If `detect_conflicts` is set to true, it will check if current transaction is
/// conflict with others when it attempts to commit.
pub struct Oracle {
pub(crate) is_managed: bool,
/// Determines if the txns should be checked for conflicts.
detect_conflicts: bool,
commit_info: Mutex<CommitInfo>,
/// Ensures that transactions go to the write channel in the same order
/// as their commit timestamps.
pub(crate) write_ch_lock: Mutex<()>,
/// Used to block `new_transaction`, so all previous commits are visible to a new read.
txn_mark: Arc<WaterMark>,
/// Used by DB.
read_mark: Arc<WaterMark>,
/// Used to stop watermarks.
closer: Closer,
pool: ThreadPool<TaskCell>,
}
impl Oracle {
pub fn new(opts: &AgateOptions) -> Self {
let pool = Builder::new("oracle_watermark")
.max_thread_count(2)
.build_callback_pool();
let closer = Closer::new();
let txn_mark = Arc::new(WaterMark::new("txn_ts".into()));
let read_mark = Arc::new(WaterMark::new("pending_reads".into()));
txn_mark.init(&pool, closer.clone());
read_mark.init(&pool, closer.clone());
Self {
is_managed: opts.managed_txns,
detect_conflicts: opts.detect_conflicts,
commit_info: Mutex::new(CommitInfo::default()),
write_ch_lock: Mutex::new(()),
txn_mark,
read_mark,
closer,
pool,
}
}
pub fn stop(&mut self) {
self.closer.close();
self.pool.shutdown();
}
pub(crate) fn read_ts(&self) -> u64 {
if self.is_managed {
panic!("read_ts should not be used in managed mode");
} else {
let read_ts = {
let commit_info = self.commit_info.lock().unwrap();
let read_ts = commit_info.next_txn_ts - 1;
self.read_mark.begin(read_ts);
read_ts
};
// Not waiting here could mean that some txns which have been
// committed would not be read.
self.txn_mark.wait_for_mark(read_ts);
read_ts
}
}
fn next_ts(&self) -> u64 {
let commit_info = self.commit_info.lock().unwrap();
commit_info.next_txn_ts
}
pub(crate) fn increment_next_ts(&self) {
let mut commit_info = self.commit_info.lock().unwrap();
commit_info.next_txn_ts += 1
}
/// Any deleted or invalid versions at or below ts would be discarded during
/// compaction to reclaim disk space in LSM tree and thence value log.
pub(crate) fn set_discard_ts(&self, discard_ts: u64) {
let mut commit_info = self.commit_info.lock().unwrap();
commit_info.discard_ts = discard_ts;
commit_info.cleanup_committed_transactions(self.is_managed, self.read_mark.clone());
}
pub(crate) fn discard_at_or_below(&self) -> u64 {
if self.is_managed {
let commit_info = self.commit_info.lock().unwrap();
commit_info.discard_ts
} else {
self.read_mark.done_until()
}
}
pub(crate) fn new_commit_ts(&self, txn: &mut Transaction) -> (u64, bool) {
let mut commit_info = self.commit_info.lock().unwrap();
if commit_info.has_conflict(txn) {
(0, true)
} else {
let ts = if !self.is_managed {
self.done_read(txn);
commit_info.cleanup_committed_transactions(self.is_managed, self.read_mark.clone());
let txn_ts = commit_info.next_txn_ts;
commit_info.next_txn_ts += 1;
self.txn_mark.begin(txn_ts);
txn_ts
} else {
txn.commit_ts
};
assert!(ts >= commit_info.last_cleanup_ts);
if self.detect_conflicts {
// We should ensure that txns are not added to committed_txns slice when
// conflict detection is disabled otherwise this slice would keep growing.
commit_info.committed_txns.push(CommittedTxn {
ts,
conflict_keys: txn.conflict_keys.clone(),
})
}
(ts, false)
}
}
pub(crate) fn done_read(&self, txn: &mut Transaction) {
if !txn.done_read {
txn.done_read = true;
self.read_mark.done(txn.read_ts);
}
}
pub(crate) fn done_commit(&self, commit_ts: u64) {
if !self.is_managed {
self.txn_mark.done(commit_ts);
}
}
}
impl Drop for Oracle {
fn drop(&mut self) {
self.stop()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::db::Core;
#[test]
fn test_basic() {
let opts = AgateOptions::default();
let _oracle = Oracle::new(&opts);
}
#[test]
fn test_read_ts() {
let opts = AgateOptions::default();
let oracle = Oracle::new(&opts);
oracle.increment_next_ts();
for i in 0..100 {
let read_ts = oracle.read_ts();
assert_eq!(read_ts, i);
oracle.read_mark.done(read_ts);
let commit_ts = oracle.next_ts();
assert_eq!(commit_ts, i + 1);
oracle.increment_next_ts();
oracle.txn_mark.begin(commit_ts);
oracle.done_commit(commit_ts);
}
oracle.txn_mark.wait_for_mark(100);
}
#[test]
fn test_detect_conflict() {
let opts = AgateOptions {
in_memory: true,
..Default::default()
};
let core = Arc::new(Core::new(&opts).unwrap());
let mut txn = Transaction::new(core.clone());
txn.conflict_keys = [11u64, 22, 33].iter().cloned().collect();
// No conflict.
assert_eq!(core.orc.new_commit_ts(&mut txn), (1, false));
txn.read_ts = 0;
txn.reads = Mutex::new(vec![11, 23]);
// Has conflict.
assert_eq!(core.orc.new_commit_ts(&mut txn), (0, true));
txn.reads = Mutex::new(vec![23]);
// No conflict.
assert_eq!(core.orc.new_commit_ts(&mut txn), (2, false));
}
#[test]
fn test_cleanup() {
let opts = AgateOptions {
in_memory: true,
managed_txns: true,
..Default::default()
};
let core = Arc::new(Core::new(&opts).unwrap());
let mut txn = Transaction::new(core.clone());
assert_eq!(core.orc.new_commit_ts(&mut txn), (0, false));
assert_eq!(core.orc.commit_info.lock().unwrap().committed_txns.len(), 1);
core.orc.set_discard_ts(1);
assert_eq!(core.orc.commit_info.lock().unwrap().committed_txns.len(), 0);
}
}