use link_cli::engine::{
ConcurrencyControl, Engine, EngineOptions, EngineTransaction, IsolationLevel, MemoryLog,
SerializableValidation,
};
use link_cli::LinksStorage;
use std::collections::BTreeMap;
#[path = "engine_history/check.rs"]
mod check;
use check::{check, Anomaly, History, Transaction};
const CLIENTS: u64 = 4;
const KEYS: u64 = 3;
const TRANSACTIONS: usize = 60;
const SEEDS: u64 = 20;
const FIRST_WRITE: u32 = 1000;
struct Random(u64);
impl Random {
fn next(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
fn below(&mut self, bound: u64) -> u64 {
self.next() % bound
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Op {
Read(u32),
Lock(u32),
Scan,
Write(u32),
Commit,
Rollback,
}
fn plan(random: &mut Random, pessimistic: bool) -> Vec<Op> {
let length = 1 + random.below(4);
let mut ops = Vec::new();
for _ in 0..length {
let kind = random.below(10);
let key = 1 + random.below(KEYS) as u32;
ops.push(match kind {
0..=3 => Op::Read(key),
4..=6 | 9 => Op::Write(key),
7 => Op::Scan,
_ if pessimistic => Op::Lock(key),
_ => Op::Read(key),
});
}
ops.push(if random.below(20) == 0 {
Op::Rollback
} else {
Op::Commit
});
ops
}
type Session = EngineTransaction<u32, MemoryLog>;
fn observe(record: &mut Transaction, link: u32, target: u32) {
match record
.writes
.iter()
.rev()
.find(|(written, _)| *written == link)
{
Some(&(_, own)) => record.own_reads.push((link, target, own)),
None => record.reads.push((link, target)),
}
}
struct Client {
session: Option<Session>,
ops: Vec<Op>,
next: usize,
record: Transaction,
}
fn step(
db: &Engine<u32, MemoryLog>,
client: &mut Client,
op: Op,
value: &mut u32,
) -> Result<(), link_cli::LinkError> {
if client.session.is_none() {
client.session = Some(db.begin()?);
}
let session = client.session.as_mut().unwrap();
let record = &mut client.record;
match op {
Op::Read(link) => {
let target = session
.get_link(link)
.expect("links are never deleted")
.target;
observe(record, link, target);
}
Op::Lock(link) => {
let target = session.lock_for_update(link)?.expect("present").target;
observe(record, link, target);
}
Op::Scan => {
for found in session.all_links() {
observe(record, found.index, found.target);
}
}
Op::Write(link) => {
*value += 1;
session.update_link(link, link, *value)?;
record.writes.push((link, *value));
}
Op::Commit => record.revision = Some(session.commit()?),
Op::Rollback => {
client.session.take();
}
}
Ok(())
}
pub fn generate(options: &EngineOptions, seed: u64) -> History {
let db = Engine::open(MemoryLog::default(), options.clone()).unwrap();
let mut initial = Transaction::default();
let mut setup = db.begin().unwrap();
for link in 1..=KEYS as u32 {
setup.ensure_link_created(link).unwrap();
setup.update_link(link, link, link).unwrap();
initial.writes.push((link, link));
}
initial.revision = Some(setup.commit().unwrap());
let mut history = History {
transactions: vec![initial],
..Default::default()
};
let mut random = Random(seed);
let pessimistic = options.concurrency == ConcurrencyControl::Pessimistic;
let mut clients: Vec<Option<Client>> = (0..CLIENTS).map(|_| None).collect();
let (mut started, mut finished, mut now) = (0, 0, 0);
let mut value = FIRST_WRITE - 1;
while finished < TRANSACTIONS {
now += 1;
let slot = &mut clients[random.below(CLIENTS) as usize];
if slot.is_none() {
if started == TRANSACTIONS {
continue;
}
started += 1;
*slot = Some(Client {
session: None,
ops: plan(&mut random, pessimistic),
next: 0,
record: Transaction {
began: now,
..Default::default()
},
});
}
let client = slot.as_mut().unwrap();
let op = client.ops[client.next];
client.next += 1;
let failed = step(&db, client, op, &mut value).is_err();
if failed || client.next == client.ops.len() {
let mut client = slot.take().unwrap();
client.session.take();
if failed {
client.record.revision = None;
}
client.record.ended = now;
history.transactions.push(client.record);
finished += 1;
}
}
history.final_state = db
.snapshot()
.unwrap()
.links()
.iter()
.map(|link| (link.index, link.target))
.collect();
history
}
fn allowed(options: &EngineOptions) -> &'static [Anomaly] {
if options.concurrency == ConcurrencyControl::Exclusive
|| options.isolation == IsolationLevel::Serializable
{
return &[];
}
match options.isolation {
IsolationLevel::Snapshot => &[Anomaly::G2],
_ => &[Anomaly::GSingle, Anomaly::G2],
}
}
fn configurations() -> Vec<EngineOptions> {
let mut all = Vec::new();
for concurrency in [
ConcurrencyControl::Optimistic,
ConcurrencyControl::Pessimistic,
ConcurrencyControl::Exclusive,
] {
for (isolation, serializable_validation) in [
(
IsolationLevel::ReadCommitted,
SerializableValidation::Revision,
),
(IsolationLevel::Snapshot, SerializableValidation::Revision),
(
IsolationLevel::Serializable,
SerializableValidation::Revision,
),
(
IsolationLevel::Serializable,
SerializableValidation::ReadSet,
),
] {
all.push(EngineOptions {
concurrency,
isolation,
serializable_validation,
..Default::default()
});
}
}
all
}
const EXPECTED: [(usize, usize, usize); 12] = [
(847, 19, 0),
(847, 0, 20),
(573, 0, 0),
(662, 0, 0),
(635, 17, 2),
(635, 0, 19),
(453, 0, 0),
(515, 0, 0),
(148, 0, 0),
(148, 0, 0),
(148, 0, 0),
(148, 0, 0),
];
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn long_random_histories_show_only_the_anomalies_each_isolation_level_allows() {
let mut total = 0;
for (options, expected) in configurations().into_iter().zip(EXPECTED) {
let context = format!(
"{:?} {:?} {:?}",
options.concurrency, options.isolation, options.serializable_validation
);
let mut seen: BTreeMap<Anomaly, usize> = BTreeMap::new();
let mut committed = 0;
for seed in 1..=SEEDS {
let history = generate(&options, seed);
committed += history
.transactions
.iter()
.skip(1)
.filter(|t| t.revision.is_some())
.count();
for (anomaly, example) in check(&history) {
assert!(
allowed(&options).contains(&anomaly),
"{context}, seed {seed}: {anomaly:?} through transactions {example:?}: {:#?}",
example
.iter()
.map(|id| &history.transactions[*id])
.collect::<Vec<_>>()
);
*seen.entry(anomaly).or_default() += 1;
}
}
let attempted = SEEDS as usize * TRANSACTIONS;
println!("{context}: {committed} of {attempted} transactions committed, histories with anomalies {seen:?}");
let share = if options.concurrency == ConcurrencyControl::Exclusive {
10
} else {
4
};
assert!(
committed * share >= attempted,
"{context}: {committed} committed"
);
if let Some(anomaly) = allowed(&options).first() {
assert!(
seen.contains_key(anomaly),
"{context}: the allowed {anomaly:?} never occurred"
);
}
let count = |anomaly| seen.get(&anomaly).copied().unwrap_or(0);
assert_eq!(
(committed, count(Anomaly::GSingle), count(Anomaly::G2)),
expected,
"{context}: differs from the other ports"
);
total += attempted;
}
assert_eq!(total, 12 * SEEDS as usize * TRANSACTIONS);
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn the_same_seed_replays_the_same_history() {
let options = EngineOptions::default();
let first = generate(&options, 7);
let second = generate(&options, 7);
assert_eq!(first.transactions, second.transactions);
assert_eq!(first.final_state, second.final_state);
assert_ne!(first.transactions, generate(&options, 8).transactions);
let mut random = Random(1);
assert_eq!(random.next(), 0x910A_2DEC_8902_5CC1);
}
fn committed(revision: u64, reads: &[(u32, u32)], writes: &[(u32, u32)]) -> Transaction {
Transaction {
reads: reads.to_vec(),
writes: writes.to_vec(),
revision: Some(revision),
began: revision as usize * 10,
ended: revision as usize * 10 + 1,
..Default::default()
}
}
fn overlapping(mut transaction: Transaction) -> Transaction {
transaction.began = 0;
transaction.ended = 1000;
transaction
}
fn history(transactions: Vec<Transaction>, final_state: &[(u32, u32)]) -> History {
let mut all = vec![committed(0, &[], &[(1, 1), (2, 2)])];
all.extend(transactions);
History {
transactions: all,
final_state: final_state.iter().copied().collect(),
}
}
fn kinds(history: &History) -> Vec<Anomaly> {
check(history).into_keys().collect()
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn the_checker_names_each_anomaly() {
let serial = history(
vec![
committed(1, &[(1, 1)], &[(1, 1000)]),
committed(2, &[(1, 1000)], &[(2, 1001)]),
],
&[(1, 1000), (2, 1001)],
);
assert_eq!(kinds(&serial), vec![]);
let write_skew = history(
vec![
overlapping(committed(1, &[(1, 1), (2, 2)], &[(1, 1000)])),
overlapping(committed(2, &[(1, 1), (2, 2)], &[(2, 1001)])),
],
&[(1, 1000), (2, 1001)],
);
assert_eq!(kinds(&write_skew), vec![Anomaly::G2]);
let lost_update = history(
vec![
overlapping(committed(1, &[(1, 1)], &[(1, 1000)])),
overlapping(committed(2, &[(1, 1)], &[(1, 1001)])),
],
&[(1, 1001), (2, 2)],
);
assert_eq!(kinds(&lost_update), vec![Anomaly::GSingle]);
let read_skew = history(
vec![
overlapping(committed(1, &[], &[(1, 1000), (2, 1001)])),
overlapping(committed(2, &[(1, 1), (2, 1001)], &[])),
],
&[(1, 1000), (2, 1001)],
);
assert_eq!(kinds(&read_skew), vec![Anomaly::GSingle]);
let stale = history(
vec![
committed(1, &[], &[(1, 1000)]),
committed(2, &[(1, 1)], &[]),
],
&[(1, 1000), (2, 2)],
);
assert_eq!(kinds(&stale), vec![Anomaly::GSingle]);
let circular = history(
vec![
overlapping(committed(1, &[(2, 1001)], &[(1, 1000)])),
overlapping(committed(2, &[(1, 1000)], &[(2, 1001)])),
],
&[(1, 1000), (2, 1001)],
);
assert_eq!(kinds(&circular), vec![Anomaly::G1c]);
let mut aborted = committed(1, &[], &[(1, 1000)]);
aborted.revision = None;
let dirty = history(
vec![
aborted,
committed(2, &[], &[(2, 1001), (2, 1002)]),
committed(3, &[(1, 1000), (2, 1001), (1, 5)], &[]),
],
&[(1, 1), (2, 1002)],
);
assert_eq!(
kinds(&dirty),
vec![Anomaly::Garbage, Anomaly::G1a, Anomaly::G1b]
);
let mut own = committed(1, &[], &[(1, 1000), (1, 1001)]);
own.own_reads = vec![(1, 1001, 1001), (1, 1000, 1001)];
let lost = history(vec![own], &[(1, 1), (2, 2)]);
assert_eq!(kinds(&lost), vec![Anomaly::Internal, Anomaly::LostWrite]);
}