-
Notifications
You must be signed in to change notification settings - Fork 128
/
buffer.rs
487 lines (445 loc) · 16.1 KB
/
buffer.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
use crate::{BoundRange, Key, KvPair, Result, Value};
use std::{
collections::{BTreeMap, HashMap},
future::Future,
};
use tikv_client_proto::kvrpcpb;
use tokio::sync::{Mutex, MutexGuard};
#[derive(Default)]
struct InnerBuffer {
primary_key: Option<Key>,
entry_map: BTreeMap<Key, BufferEntry>,
}
impl InnerBuffer {
fn insert(&mut self, key: impl Into<Key>, entry: BufferEntry) {
let key = key.into();
if !matches!(entry, BufferEntry::Cached(_)) {
self.primary_key.get_or_insert_with(|| key.clone());
}
self.entry_map.insert(key, entry);
}
pub fn get_primary_key_or(&mut self, key: &Key) -> &Key {
self.primary_key.get_or_insert(key.clone())
}
}
/// A caching layer which buffers reads and writes in a transaction.
#[derive(Default)]
pub struct Buffer {
mutations: Mutex<InnerBuffer>,
}
impl Buffer {
/// Get the primary key of the buffer.
pub async fn get_primary_key(&self) -> Option<Key> {
self.mutations.lock().await.primary_key.clone()
}
/// Get the primary key of the buffer, if not exists, use `key` as the primary key.
pub async fn get_primary_key_or(&self, key: &Key) -> Key {
self.mutations.lock().await.get_primary_key_or(key).clone()
}
/// Get a value from the buffer.
/// If the returned value is None, it means the key doesn't exist in buffer yet.
pub async fn get(&self, key: &Key) -> Option<Value> {
match self.get_from_mutations(key).await {
MutationValue::Determined(value) => value,
MutationValue::Undetermined => None,
}
}
/// Get a value from the buffer. If the value is not present, run `f` to get
/// the value.
pub async fn get_or_else<F, Fut>(&self, key: Key, f: F) -> Result<Option<Value>>
where
F: FnOnce(Key) -> Fut,
Fut: Future<Output = Result<Option<Value>>>,
{
match self.get_from_mutations(&key).await {
MutationValue::Determined(value) => Ok(value),
MutationValue::Undetermined => {
let value = f(key.clone()).await?;
let mut mutations = self.mutations.lock().await;
Self::update_cache(&mut mutations, key, value.clone());
Ok(value)
}
}
}
/// Get multiple values from the buffer. If any are not present, run `f` to
/// get the missing values.
///
/// only used for snapshot read (i.e. not for `batch_get_for_update`)
pub async fn batch_get_or_else<F, Fut>(
&self,
keys: impl Iterator<Item = Key>,
f: F,
) -> Result<impl Iterator<Item = KvPair>>
where
F: FnOnce(Box<dyn Iterator<Item = Key>>) -> Fut,
Fut: Future<Output = Result<Vec<KvPair>>>,
{
let (cached_results, undetermined_keys) = {
let mutations = self.mutations.lock().await;
// Partition the keys into those we have buffered and those we have to
// get from the store.
let (undetermined_keys, cached_results): (
Vec<(Key, MutationValue)>,
Vec<(Key, MutationValue)>,
) = keys
.map(|key| {
let value = mutations
.entry_map
.get(&key)
.map(BufferEntry::get_value)
.unwrap_or(MutationValue::Undetermined);
(key, value)
})
.partition(|(_, v)| *v == MutationValue::Undetermined);
let cached_results = cached_results
.into_iter()
.filter_map(|(k, v)| v.unwrap().map(|v| KvPair(k, v)));
let undetermined_keys = undetermined_keys.into_iter().map(|(k, _)| k);
(cached_results, undetermined_keys)
};
let fetched_results = f(Box::new(undetermined_keys)).await?;
let mut mutations = self.mutations.lock().await;
for kvpair in &fetched_results {
let key = kvpair.0.clone();
let value = Some(kvpair.1.clone());
Self::update_cache(&mut mutations, key, value);
}
let results = cached_results.chain(fetched_results.into_iter());
Ok(results)
}
/// Run `f` to fetch entries in `range` from TiKV. Combine them with mutations in local buffer. Returns the results.
pub async fn scan_and_fetch<F, Fut>(
&self,
range: BoundRange,
limit: u32,
f: F,
) -> Result<impl Iterator<Item = KvPair>>
where
F: FnOnce(BoundRange, u32) -> Fut,
Fut: Future<Output = Result<Vec<KvPair>>>,
{
// read from local buffer
let mut mutations = self.mutations.lock().await;
let mutation_range = mutations.entry_map.range(range.clone());
// fetch from TiKV
// fetch more entries because some of them may be deleted.
let redundant_limit = limit
+ mutation_range
.clone()
.filter(|(_, m)| matches!(m, BufferEntry::Del))
.count() as u32;
let mut results = f(range, redundant_limit)
.await?
.into_iter()
.map(|pair| pair.into())
.collect::<HashMap<Key, Value>>();
// override using local data
for (k, m) in mutation_range {
match m {
BufferEntry::Put(v) => {
results.insert(k.clone(), v.clone());
}
BufferEntry::Del => {
results.remove(k);
}
_ => {}
}
}
// update local buffer
for (k, v) in &results {
Self::update_cache(&mut mutations, k.clone(), Some(v.clone()));
}
let mut res = results
.into_iter()
.map(|(k, v)| KvPair::new(k, v))
.collect::<Vec<_>>();
res.sort_by_cached_key(|x| x.key().clone());
Ok(res.into_iter().take(limit as usize))
}
/// Lock the given key if necessary.
pub async fn lock(&self, key: Key) {
let mutations = &mut self.mutations.lock().await;
mutations.primary_key.get_or_insert(key.clone());
let value = mutations
.entry_map
.entry(key)
// Mutated keys don't need a lock.
.or_insert(BufferEntry::Locked(None));
// But values which we have only read, but not written, do.
if let BufferEntry::Cached(v) = value {
*value = BufferEntry::Locked(Some(v.take()))
}
}
/// Insert a value into the buffer (does not write through).
pub async fn put(&self, key: Key, value: Value) {
self.mutations
.lock()
.await
.insert(key, BufferEntry::Put(value));
}
/// Mark a value as Insert mutation into the buffer (does not write through).
pub async fn insert(&self, key: Key, value: Value) {
self.mutations
.lock()
.await
.insert(key, BufferEntry::Insert(value));
}
/// Mark a value as deleted.
pub async fn delete(&self, key: Key) {
let mut mutations = self.mutations.lock().await;
let value = mutations
.entry_map
.entry(key.clone())
.or_insert(BufferEntry::Del);
let new_value: BufferEntry;
if let BufferEntry::Insert(_) = value {
new_value = BufferEntry::CheckNotExist
} else {
new_value = BufferEntry::Del
}
mutations.insert(key, new_value);
}
/// Converts the buffered mutations to the proto buffer version
pub async fn to_proto_mutations(&self) -> Vec<kvrpcpb::Mutation> {
self.mutations
.lock()
.await
.entry_map
.iter()
.filter_map(|(key, mutation)| mutation.to_proto_with_key(key))
.collect()
}
async fn get_from_mutations(&self, key: &Key) -> MutationValue {
self.mutations
.lock()
.await
.entry_map
.get(&key)
.map(BufferEntry::get_value)
.unwrap_or(MutationValue::Undetermined)
}
fn update_cache(buffer: &mut MutexGuard<InnerBuffer>, key: Key, value: Option<Value>) {
match buffer.entry_map.get(&key) {
Some(BufferEntry::Locked(None)) => {
buffer
.entry_map
.insert(key, BufferEntry::Locked(Some(value)));
}
None => {
buffer.entry_map.insert(key, BufferEntry::Cached(value));
}
Some(BufferEntry::Cached(v)) | Some(BufferEntry::Locked(Some(v))) => {
assert!(&value == v);
}
Some(BufferEntry::Put(v)) => {
assert!(value.as_ref() == Some(v))
}
Some(BufferEntry::Del) => {
assert!(value.is_none());
}
Some(BufferEntry::Insert(v)) => {
assert!(value.as_ref() == Some(v))
}
Some(BufferEntry::CheckNotExist) => {
assert!(value.is_none());
}
}
}
}
// The state of a key-value pair in the buffer.
// It includes two kinds of state:
//
// Mutations:
// - `Put`
// - `Del`
// - `Insert`
// - `CheckNotExist`, a constraint to ensure the key doesn't exist. See https://github.com/pingcap/tidb/pull/14968.
// Cache of read requests:
// - `Cached`, generated by normal read requests
// - `ReadLockCached`, generated by lock commands (`lock_keys`, `get_for_update`) and optionally read requests
//
#[derive(Debug, Clone)]
enum BufferEntry {
// The value has been read from the server. None means there is no entry.
// Also means the entry isn't locked.
Cached(Option<Value>),
// Key is locked.
//
// Cached value:
// - Outer Option: Whether there is cached value
// - Inner Option: Whether the value is empty
// - Note: The cache is not what the lock request reads, but what normal read (`get`) requests read.
//
// In optimistic transaction:
// The key is locked by `lock_keys`.
// It means letting the server check for conflicts when committing
//
// In pessimistic transaction:
// The key is locked by `get_for_update` or `batch_get_for_update`
Locked(Option<Option<Value>>),
// Value has been written.
Put(Value),
// Value has been deleted.
Del,
// Key should be check not exists before.
Insert(Value),
// Key should be check not exists before.
CheckNotExist,
}
impl BufferEntry {
fn to_proto_with_key(&self, key: &Key) -> Option<kvrpcpb::Mutation> {
let mut pb = kvrpcpb::Mutation::default();
match self {
BufferEntry::Cached(_) => return None,
BufferEntry::Put(v) => {
pb.set_op(kvrpcpb::Op::Put);
pb.set_value(v.clone());
}
BufferEntry::Del => pb.set_op(kvrpcpb::Op::Del),
BufferEntry::Locked(_) => pb.set_op(kvrpcpb::Op::Lock),
BufferEntry::Insert(v) => {
pb.set_op(kvrpcpb::Op::Insert);
pb.set_value(v.clone());
}
BufferEntry::CheckNotExist => pb.set_op(kvrpcpb::Op::CheckNotExists),
};
pb.set_key(key.clone().into());
Some(pb)
}
fn get_value(&self) -> MutationValue {
match self {
BufferEntry::Cached(value) => MutationValue::Determined(value.clone()),
BufferEntry::Put(value) => MutationValue::Determined(Some(value.clone())),
BufferEntry::Del => MutationValue::Determined(None),
BufferEntry::Locked(None) => MutationValue::Undetermined,
BufferEntry::Locked(Some(value)) => MutationValue::Determined(value.clone()),
BufferEntry::Insert(value) => MutationValue::Determined(Some(value.clone())),
BufferEntry::CheckNotExist => MutationValue::Determined(None),
}
}
}
// The state of a value as known by the buffer.
#[derive(Eq, PartialEq, Debug)]
enum MutationValue {
// The buffer can determine the value.
Determined(Option<Value>),
// The buffer cannot determine the value.
Undetermined,
}
impl MutationValue {
fn unwrap(self) -> Option<Value> {
match self {
MutationValue::Determined(v) => v,
MutationValue::Undetermined => unreachable!(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use futures::{executor::block_on, future::ready};
#[tokio::test]
#[allow(unreachable_code)]
async fn set_and_get_from_buffer() {
let buffer = Buffer::default();
buffer
.put(b"key1".to_vec().into(), b"value1".to_vec())
.await;
buffer
.put(b"key2".to_vec().into(), b"value2".to_vec())
.await;
assert_eq!(
block_on(buffer.get_or_else(b"key1".to_vec().into(), move |_| ready(panic!())))
.unwrap()
.unwrap(),
b"value1".to_vec()
);
buffer.delete(b"key2".to_vec().into()).await;
buffer.put(b"key1".to_vec().into(), b"value".to_vec()).await;
assert_eq!(
block_on(buffer.batch_get_or_else(
vec![b"key2".to_vec().into(), b"key1".to_vec().into()].into_iter(),
move |_| ready(Ok(vec![])),
))
.unwrap()
.collect::<Vec<_>>(),
vec![KvPair(
Key::from(b"key1".to_vec()),
Value::from(b"value".to_vec()),
),]
);
}
#[tokio::test]
#[allow(unreachable_code)]
async fn insert_and_get_from_buffer() {
let buffer = Buffer::default();
buffer
.insert(b"key1".to_vec().into(), b"value1".to_vec())
.await;
buffer
.insert(b"key2".to_vec().into(), b"value2".to_vec())
.await;
assert_eq!(
block_on(buffer.get_or_else(b"key1".to_vec().into(), move |_| ready(panic!())))
.unwrap()
.unwrap(),
b"value1".to_vec()
);
buffer.delete(b"key2".to_vec().into()).await;
buffer
.insert(b"key1".to_vec().into(), b"value".to_vec())
.await;
assert_eq!(
block_on(buffer.batch_get_or_else(
vec![b"key2".to_vec().into(), b"key1".to_vec().into()].into_iter(),
move |_| ready(Ok(vec![])),
))
.unwrap()
.collect::<Vec<_>>(),
vec![KvPair(Key::from(b"key1".to_vec()), b"value".to_vec()),]
);
}
#[test]
#[allow(unreachable_code)]
fn repeat_reads_are_cached() {
let k1: Key = b"key1".to_vec().into();
let k1_ = k1.clone();
let k2: Key = b"key2".to_vec().into();
let k2_ = k2.clone();
let v1: Value = b"value1".to_vec();
let v1_ = v1.clone();
let v1__ = v1.clone();
let v2: Value = b"value2".to_vec();
let v2_ = v2.clone();
let buffer = Buffer::default();
let r1 = block_on(buffer.get_or_else(k1.clone(), move |_| ready(Ok(Some(v1_)))));
let r2 = block_on(buffer.get_or_else(k1.clone(), move |_| ready(panic!())));
assert_eq!(r1.unwrap().unwrap(), v1);
assert_eq!(r2.unwrap().unwrap(), v1);
let buffer = Buffer::default();
let r1 = block_on(
buffer.batch_get_or_else(vec![k1.clone(), k2.clone()].into_iter(), move |_| {
ready(Ok(vec![(k1_, v1__).into(), (k2_, v2_).into()]))
}),
);
let r2 = block_on(buffer.get_or_else(k2.clone(), move |_| ready(panic!())));
let r3 = block_on(
buffer.batch_get_or_else(vec![k1.clone(), k2.clone()].into_iter(), move |_| {
ready(Ok(vec![]))
}),
);
assert_eq!(
r1.unwrap().collect::<Vec<_>>(),
vec![
KvPair(k1.clone(), v1.clone()),
KvPair(k2.clone(), v2.clone())
]
);
assert_eq!(r2.unwrap().unwrap(), v2);
assert_eq!(
r3.unwrap().collect::<Vec<_>>(),
vec![KvPair(k1, v1), KvPair(k2, v2)]
);
}
}