Skip to content

Commit

Permalink
txn refactoring part 3 (tikv#5857)
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Cameron <nrc@ncameron.org>
  • Loading branch information
nrc authored and wangweizhen committed Nov 22, 2019
1 parent 88d3bad commit 6be7b30
Show file tree
Hide file tree
Showing 66 changed files with 2,216 additions and 1,479 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -113,6 +113,7 @@ mime = "0.3.13"
failure = "0.1"
prost = "0.5.0"
regex = "1.3"
derive-new = "0.5"
tipb = { git = "https://github.com/pingcap/tipb.git" }
kvproto = { git = "https://github.com/pingcap/kvproto.git" }

Expand Down
10 changes: 5 additions & 5 deletions benches/deadlock_detector/mod.rs
Expand Up @@ -13,7 +13,7 @@ struct DetectGenerator {
}

impl DetectGenerator {
pub fn new(range: u64) -> Self {
fn new(range: u64) -> Self {
Self {
rng: ThreadRng::default(),
range,
Expand All @@ -22,7 +22,7 @@ impl DetectGenerator {
}

/// Generates n detect requests with the same timestamp
pub fn generate(&mut self, n: u64) -> Vec<WaitForEntry> {
fn generate(&mut self, n: u64) -> Vec<WaitForEntry> {
let mut entries = Vec::with_capacity(n as usize);
(0..n).for_each(|_| {
let mut entry = WaitForEntry::new();
Expand All @@ -33,7 +33,7 @@ impl DetectGenerator {
if self.timestamp < self.range {
0
} else {
self.timestamp - self.range
(self.timestamp - self.range)
},
self.timestamp + self.range,
);
Expand All @@ -60,8 +60,8 @@ fn bench_detect(b: &mut Bencher, cfg: &Config) {
b.iter(|| {
for entry in generator.generate(cfg.n) {
detect_table.detect(
entry.get_txn(),
entry.get_wait_for_txn(),
entry.get_txn().into(),
entry.get_wait_for_txn().into(),
entry.get_key_hash(),
);
}
Expand Down
24 changes: 14 additions & 10 deletions benches/hierarchy/mvcc/mod.rs
Expand Up @@ -4,19 +4,23 @@ use criterion::{black_box, BatchSize, Bencher, Criterion};
use kvproto::kvrpcpb::Context;
use test_util::KvGenerator;
use tikv::storage::kv::Engine;
use tikv::storage::mvcc::{MvccReader, MvccTxn};
use tikv::storage::mvcc::{self, MvccReader, MvccTxn, TimeStamp};
use tikv::storage::{Key, Mutation, Options};

use super::{BenchConfig, EngineFactory, DEFAULT_ITERATIONS, DEFAULT_KV_GENERATOR_SEED};

fn setup_prewrite<E, F>(engine: &E, config: &BenchConfig<F>, start_ts: u64) -> (E::Snap, Vec<Key>)
fn setup_prewrite<E, F>(
engine: &E,
config: &BenchConfig<F>,
start_ts: impl Into<TimeStamp>,
) -> (E::Snap, Vec<Key>)
where
E: Engine,
F: EngineFactory<E>,
{
let ctx = Context::default();
let snapshot = engine.snapshot(&ctx).unwrap();
let mut txn = MvccTxn::new(snapshot, start_ts, true).unwrap();
let mut txn = MvccTxn::new(snapshot, start_ts.into(), true).unwrap();

let kvs = KvGenerator::with_seed(
config.key_length,
Expand Down Expand Up @@ -59,7 +63,7 @@ fn mvcc_prewrite<E: Engine, F: EngineFactory<E>>(b: &mut Bencher, config: &Bench
},
|(mutations, snapshot, option)| {
for (mutation, primary) in mutations {
let mut txn = MvccTxn::new(snapshot.clone(), 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot.clone(), 1, true);
txn.prewrite(mutation, &primary, option).unwrap();
}
},
Expand All @@ -73,8 +77,8 @@ fn mvcc_commit<E: Engine, F: EngineFactory<E>>(b: &mut Bencher, config: &BenchCo
|| setup_prewrite(&engine, &config, 1),
|(snapshot, keys)| {
for key in keys {
let mut txn = MvccTxn::new(snapshot.clone(), 1, true).unwrap();
black_box(txn.commit(key, 1)).unwrap();
let mut txn = mvcc::new_txn!(snapshot.clone(), 1, true);
black_box(txn.commit(key, 1.into())).unwrap();
}
},
BatchSize::SmallInput,
Expand All @@ -90,7 +94,7 @@ fn mvcc_rollback_prewrote<E: Engine, F: EngineFactory<E>>(
|| setup_prewrite(&engine, &config, 1),
|(snapshot, keys)| {
for key in keys {
let mut txn = MvccTxn::new(snapshot.clone(), 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot.clone(), 1, true);
black_box(txn.rollback(key)).unwrap();
}
},
Expand All @@ -107,7 +111,7 @@ fn mvcc_rollback_conflict<E: Engine, F: EngineFactory<E>>(
|| setup_prewrite(&engine, &config, 2),
|(snapshot, keys)| {
for key in keys {
let mut txn = MvccTxn::new(snapshot.clone(), 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot.clone(), 1, true);
black_box(txn.rollback(key)).unwrap();
}
},
Expand Down Expand Up @@ -135,7 +139,7 @@ fn mvcc_rollback_non_prewrote<E: Engine, F: EngineFactory<E>>(
},
|(snapshot, keys)| {
for key in keys {
let mut txn = MvccTxn::new(snapshot.clone(), 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot.clone(), 1, true);
black_box(txn.rollback(key)).unwrap();
}
},
Expand Down Expand Up @@ -196,7 +200,7 @@ fn mvcc_reader_seek_write<E: Engine, F: EngineFactory<E>>(
for key in &test_keys {
let mut reader =
MvccReader::new(snapshot.clone(), None, true, ctx.get_isolation_level());
black_box(reader.seek_write(&key, u64::max_value()).unwrap());
black_box(reader.seek_write(&key, TimeStamp::max()).unwrap());
}
},
BatchSize::SmallInput,
Expand Down
24 changes: 14 additions & 10 deletions benches/hierarchy/txn/mod.rs
Expand Up @@ -4,12 +4,16 @@ use criterion::{black_box, BatchSize, Bencher, Criterion};
use kvproto::kvrpcpb::Context;
use test_util::KvGenerator;
use tikv::storage::kv::Engine;
use tikv::storage::mvcc::MvccTxn;
use tikv::storage::{Key, Mutation, Options};
use tikv::storage::mvcc::{self, MvccTxn};
use tikv::storage::{Key, Mutation, Options, TimeStamp};

use super::{BenchConfig, EngineFactory, DEFAULT_ITERATIONS};

fn setup_prewrite<E, F>(engine: &E, config: &BenchConfig<F>, start_ts: u64) -> Vec<Key>
fn setup_prewrite<E, F>(
engine: &E,
config: &BenchConfig<F>,
start_ts: impl Into<TimeStamp>,
) -> Vec<Key>
where
E: Engine,
F: EngineFactory<E>,
Expand All @@ -18,7 +22,7 @@ where
let option = Options::default();

let snapshot = engine.snapshot(&ctx).unwrap();
let mut txn = MvccTxn::new(snapshot, start_ts, true).unwrap();
let mut txn = MvccTxn::new(snapshot, start_ts.into(), true).unwrap();
let kvs = KvGenerator::new(config.key_length, config.value_length).generate(DEFAULT_ITERATIONS);
for (k, v) in &kvs {
txn.prewrite(
Expand Down Expand Up @@ -51,7 +55,7 @@ fn txn_prewrite<E: Engine, F: EngineFactory<E>>(b: &mut Bencher, config: &BenchC
|(mutations, option)| {
for (mutation, primary) in mutations {
let snapshot = engine.snapshot(&ctx).unwrap();
let mut txn = MvccTxn::new(snapshot, 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot, 1, true);
txn.prewrite(mutation, &primary, option).unwrap();
let modifies = txn.into_modifies();
black_box(engine.write(&ctx, modifies)).unwrap();
Expand All @@ -69,8 +73,8 @@ fn txn_commit<E: Engine, F: EngineFactory<E>>(b: &mut Bencher, config: &BenchCon
|keys| {
for key in keys {
let snapshot = engine.snapshot(&ctx).unwrap();
let mut txn = MvccTxn::new(snapshot, 1, true).unwrap();
txn.commit(key, 2).unwrap();
let mut txn = mvcc::new_txn!(snapshot, 1, true);
txn.commit(key, 2.into()).unwrap();
let modifies = txn.into_modifies();
black_box(engine.write(&ctx, modifies)).unwrap();
}
Expand All @@ -87,7 +91,7 @@ fn txn_rollback_prewrote<E: Engine, F: EngineFactory<E>>(b: &mut Bencher, config
|keys| {
for key in keys {
let snapshot = engine.snapshot(&ctx).unwrap();
let mut txn = MvccTxn::new(snapshot, 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot, 1, true);
txn.rollback(key).unwrap();
let modifies = txn.into_modifies();
black_box(engine.write(&ctx, modifies)).unwrap();
Expand All @@ -105,7 +109,7 @@ fn txn_rollback_conflict<E: Engine, F: EngineFactory<E>>(b: &mut Bencher, config
|keys| {
for key in keys {
let snapshot = engine.snapshot(&ctx).unwrap();
let mut txn = MvccTxn::new(snapshot, 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot, 1, true);
txn.rollback(key).unwrap();
let modifies = txn.into_modifies();
black_box(engine.write(&ctx, modifies)).unwrap();
Expand All @@ -131,7 +135,7 @@ fn txn_rollback_non_prewrote<E: Engine, F: EngineFactory<E>>(
|keys| {
for key in keys {
let snapshot = engine.snapshot(&ctx).unwrap();
let mut txn = MvccTxn::new(snapshot, 1, true).unwrap();
let mut txn = mvcc::new_txn!(snapshot, 1, true);
txn.rollback(key).unwrap();
let modifies = txn.into_modifies();
black_box(engine.write(&ctx, modifies)).unwrap();
Expand Down
8 changes: 7 additions & 1 deletion benches/misc/storage/incremental_get.rs
Expand Up @@ -34,7 +34,13 @@ fn table_lookup_gen_data() -> (SnapshotStore<SyncSnapshot>, Vec<Key>) {
db.compact_range_cf(db.cf_handle("lock").unwrap(), None, None);

let snapshot = engine.snapshot(&Context::default()).unwrap();
let store = SnapshotStore::new(snapshot, 10, IsolationLevel::Si, true, Default::default());
let store = SnapshotStore::new(
snapshot,
10.into(),
IsolationLevel::Si,
true,
Default::default(),
);

// Keys are given in order, and are far away from each other to simulate a normal table lookup
// scenario.
Expand Down
2 changes: 1 addition & 1 deletion benches/misc/storage/scan.rs
Expand Up @@ -66,7 +66,7 @@ fn bench_tombstone_scan(b: &mut Bencher) {
None,
1,
false,
ts_generator.next().unwrap()
ts_generator.next().unwrap(),
)
.unwrap()
.is_empty())
Expand Down
1 change: 1 addition & 0 deletions components/backup/Cargo.toml
Expand Up @@ -31,6 +31,7 @@ serde = "1.0"
serde_derive = "1.0"
lazy_static = "1.3"
failure = "0.1"
keys = { path = "../keys" }
tikv_alloc = { path = "../tikv_alloc", default-features = false }
tidb_query = { path = "../tidb_query", default-features = false }

Expand Down

0 comments on commit 6be7b30

Please sign in to comment.