最近看完了 Percolator的论文, 想把其中MVCC的原理跟Tikv的源码对应起来. Percolator的2-pc写入原理如下:
- Percolator 的Cell结构 : row, col, st时间戳, Lock锁, Write指针.
- prewrite阶段 检查锁冲突,写入Value, 写入Lock
- commit阶段 写入write指针,清除Primary Lock
刚看到源码中 MvccReader, Cursor, SnapShot 三个概念就产生了疑惑, 能否讲一下这些结构体之间的关系、成员变量的含义, 以及对应到上面Percolator概念中的那些环节吗?
pub struct MvccReader<S: Snapshot> {
snapshot: S,
statistics: Statistics,
// cursors are used for speeding up scans.
data_cursor: Option<Cursor<S::Iter>>,
lock_cursor: Option<Cursor<S::Iter>>,
write_cursor: Option<Cursor<S::Iter>>,
scan_mode: Option<ScanMode>,
key_only: bool,
fill_cache: bool,
isolation_level: IsolationLevel,
}
pub struct Cursor<I: Iterator> {
iter: I,
scan_mode: ScanMode,
// the data cursor can be seen will be
min_key: Option<Vec<u8>>,
max_key: Option<Vec<u8>>,
// Use `Cell` to wrap these flags to provide interior mutability, so that `key()` and
// `value()` don't need to have `&mut self`.
cur_key_has_read: Cell<bool>,
cur_value_has_read: Cell<bool>,
}
pub trait Snapshot: Send + Clone {}
pub struct Statistics {
pub lock: CfStatistics,
pub write: CfStatistics,
pub data: CfStatistics,
}
pub struct CfStatistics {
// How many keys that's effective to user. This counter should be increased
// by the caller.
pub processed: usize,
pub get: usize,
pub next: usize,
pub prev: usize,
pub seek: usize,
pub seek_for_prev: usize,
pub over_seek_bound: usize,
pub flow_stats: FlowStatistics,
}