MVCC in TiKV

Introduction to concurrency control

MVCC in TiKV

Timestamp Oracle(TSO)

Storage

pub struct Storage {
engine: Box<Engine>,
sendch: SendCh<Msg>,
handle: Arc<Mutex<StorageHandle>>,
}
impl Storage {
pub fn start(&mut self, config: &Config) -> Result<()> {
let mut handle = self.handle.lock().unwrap();
if handle.handle.is_some() {
return Err(box_err!("scheduler is already running"));
}
let engine = self.engine.clone();
let builder = thread::Builder::new().name(thd_name!("storage-scheduler"));
let mut el = handle.event_loop.take().unwrap();
let sched_concurrency = config.sched_concurrency;
let sched_worker_pool_size = config.sched_worker_pool_size;
let sched_too_busy_threshold = config.sched_too_busy_threshold;
let ch = self.sendch.clone();
let h = try!(builder.spawn(move || {
let mut sched = Scheduler::new(engine,
ch,
sched_concurrency,
sched_worker_pool_size,
sched_too_busy_threshold);
if let Err(e) = el.run(&mut sched) {
panic!("scheduler run err:{:?}", e);
}
info!("scheduler stopped");
}));
handle.handle = Some(h);
Ok(())
}
}

Engine

StorageHandle

MVCC

  • Prewrite
  1. The transaction starts. The client obtains the current timestamp (startTS) from TSO.
  2. Select one row as the primary row, the others as the secondary rows.
  3. Check whether there is another lock on this row or whether there are any commits located after startTS. These two situations will lead to conflicts. If either happens, the commit fails and rollback will be called.
  4. Lock the primary row.
  5. Repeat the steps above on secondary rows.
  • Commit
  1. Obtain the commit timestamp commit_ts from TSO.
  2. Check whether the lock on the primary row still exists. Proceed if the lock is still there. Roll back if not.
  3. Write to column CF_WRITE with commit_ts.
  4. Clear the corresponding primary lock.
  5. Repeat the steps above on secondary rows.

--

--

--

PingCAP is the team behind TiDB, an open-source MySQL compatible NewSQL database. Official website: https://pingcap.com/ GitHub: https://github.com/pingcap

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Maverick Testnet’s Test

Micro frontends in sennder

How do I design high-frequency trading systems and its architecture. Part 1/3

Athlon’s Front-end Process 2018

Graphic: A Flutter Data Visualization Library Base on Grammar of Graphics

CCIE Lab exam — A Successful engineer’s Feedback.

90% OFF — Mastering ARKit for iOS — Expires 09/30/2017

SETTING COOKIES IN PHP

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
PingCAP

PingCAP

PingCAP is the team behind TiDB, an open-source MySQL compatible NewSQL database. Official website: https://pingcap.com/ GitHub: https://github.com/pingcap

More from Medium

Using the TiDB Upgrade Toolkit to Guarantee a Safe Database Upgrade

Setting up Gravitee API Management behind Istio Service Mesh

What is OpenTelemetry? A Straightforward Guide for Devs

Fast(er) System Time in Rust