use link_cli::engine::{
ConcurrencyControl, Engine, EngineOptions, EngineTransaction, IsolationLevel, MemoryLog,
SerializableValidation,
};
use link_cli::LinksStorage;
use std::collections::BTreeMap;
#[derive(Clone, Copy, Debug)]
enum Step {
Read(u32),
Lock(u32),
Scan,
Write(u32),
Commit,
}
use Step::*;
const INITIAL: [(u32, u32); 2] = [(1, 3), (2, 4)];
struct Workload {
name: &'static str,
transactions: [&'static [Step]; 2],
}
const LOST_UPDATE: Workload = Workload {
name: "lost update",
transactions: [&[Read(1), Write(1), Commit], &[Read(1), Write(1), Commit]],
};
const READ_SKEW: Workload = Workload {
name: "read skew",
transactions: [&[Read(1), Read(2), Commit], &[Write(1), Write(2), Commit]],
};
const WRITE_SKEW: Workload = Workload {
name: "write skew",
transactions: [
&[Read(1), Read(2), Write(1), Commit],
&[Read(1), Read(2), Write(2), Commit],
],
};
const SCAN_SKEW: Workload = Workload {
name: "write skew through a scan",
transactions: [&[Scan, Write(1), Commit], &[Scan, Write(2), Commit]],
};
const LOCKED_WRITE_SKEW: Workload = Workload {
name: "write skew with locked reads",
transactions: [
&[Lock(1), Lock(2), Write(1), Commit],
&[Lock(1), Lock(2), Write(2), Commit],
],
};
const DISJOINT: Workload = Workload {
name: "disjoint read and write",
transactions: [&[Read(1), Write(1), Commit], &[Read(2), Write(2), Commit]],
};
type Session = EngineTransaction<u32, MemoryLog>;
#[derive(Default)]
struct Run {
session: Option<Session>,
reads: Vec<u32>,
aborted: bool,
committed: bool,
}
fn execute(
db: &Engine<u32, MemoryLog>,
run: &mut Run,
step: Step,
) -> Result<(), link_cli::LinkError> {
if run.session.is_none() {
run.session = Some(db.begin()?);
}
let session = run.session.as_mut().unwrap();
match step {
Read(id) => run.reads.push(session.get_link(id).unwrap().target),
Lock(id) => run.reads.push(session.lock_for_update(id)?.unwrap().target),
Scan => run
.reads
.push(session.all_links().iter().map(|link| link.target).sum()),
Write(id) => {
let value = 1 + run.reads.iter().sum::<u32>();
session.update_link(id, id, value)?;
}
Commit => {
session.commit()?;
run.committed = true;
}
}
Ok(())
}
fn serial(order: &[&[Step]]) -> (Vec<Vec<u32>>, BTreeMap<u32, u32>) {
let mut state = BTreeMap::from(INITIAL);
let reads = order
.iter()
.map(|steps| {
let mut reads = Vec::new();
for step in *steps {
match *step {
Read(id) | Lock(id) => reads.push(state[&id]),
Scan => reads.push(state.values().sum()),
Write(id) => {
state.insert(id, 1 + reads.iter().sum::<u32>());
}
Commit => {}
}
}
reads
})
.collect();
(reads, state)
}
fn interleavings(first: usize, second: usize) -> Vec<Vec<usize>> {
if first == 0 || second == 0 {
return vec![[0]
.repeat(first)
.into_iter()
.chain([1].repeat(second))
.collect()];
}
let mut all = Vec::new();
for (who, rest) in [
(0, interleavings(first - 1, second)),
(1, interleavings(first, second - 1)),
] {
for tail in rest {
all.push(std::iter::once(who).chain(tail).collect());
}
}
all
}
struct Outcome {
serializable: bool,
both_committed: bool,
}
fn run(options: &EngineOptions, workload: &Workload, schedule: &[usize]) -> Outcome {
let db = Engine::open(MemoryLog::default(), options.clone()).unwrap();
let mut seed = db.begin().unwrap();
for (id, target) in INITIAL {
seed.ensure_link_created(id).unwrap();
seed.update_link(id, id, target).unwrap();
}
seed.commit().unwrap();
let mut runs = [Run::default(), Run::default()];
let mut next = [0, 0];
for &who in schedule {
let step = workload.transactions[who][next[who]];
next[who] += 1;
let run = &mut runs[who];
if !run.aborted && execute(&db, run, step).is_err() {
run.aborted = true;
run.session.take();
}
}
for run in &mut runs {
run.session.take();
}
let committed: Vec<usize> = (0..2).filter(|who| runs[*who].committed).collect();
let actual: BTreeMap<u32, u32> = db
.snapshot()
.unwrap()
.links()
.iter()
.map(|link| (link.index, link.target))
.collect();
let orders: Vec<Vec<usize>> = match committed[..] {
[a, b] => vec![vec![a, b], vec![b, a]],
_ => vec![committed.clone()],
};
let serializable = orders.iter().any(|order| {
let steps: Vec<&[Step]> = order
.iter()
.map(|who| workload.transactions[*who])
.collect();
let (reads, state) = serial(&steps);
state == actual
&& order
.iter()
.zip(reads)
.all(|(who, reads)| runs[*who].reads == reads)
});
Outcome {
serializable,
both_committed: committed.len() == 2,
}
}
fn prevents(options: &EngineOptions, workload: &Workload) -> bool {
if options.concurrency == ConcurrencyControl::Exclusive
|| options.isolation == IsolationLevel::Serializable
{
return true;
}
match workload.name {
"lost update" | "write skew with locked reads" | "disjoint read and write" => true,
"read skew" => options.isolation == IsolationLevel::Snapshot,
_ => false,
}
}
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
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn every_interleaving_is_serializable_exactly_where_the_isolation_level_promises() {
let mut checked = 0;
for options in configurations() {
for workload in [
LOST_UPDATE,
READ_SKEW,
WRITE_SKEW,
SCAN_SKEW,
LOCKED_WRITE_SKEW,
DISJOINT,
] {
if matches!(workload.transactions[0][0], Lock(_))
&& options.concurrency != ConcurrencyControl::Pessimistic
{
continue;
}
let [first, second] = workload.transactions.map(<[Step]>::len);
let outcomes: Vec<(Vec<usize>, Outcome)> = interleavings(first, second)
.into_iter()
.map(|schedule| {
let outcome = run(&options, &workload, &schedule);
(schedule, outcome)
})
.collect();
let context = format!(
"{} under {:?} {:?} {:?}",
workload.name,
options.concurrency,
options.isolation,
options.serializable_validation
);
assert!(
outcomes.iter().any(|(_, outcome)| outcome.both_committed),
"{context}: no interleaving committed both transactions"
);
let anomaly = outcomes
.iter()
.find(|(_, outcome)| !outcome.serializable)
.map(|(schedule, _)| schedule);
if prevents(&options, &workload) {
assert_eq!(anomaly, None, "{context}: a non-serializable interleaving");
} else {
assert!(
anomaly.is_some(),
"{context}: the allowed anomaly never occurred"
);
}
let anomalies = outcomes.iter().filter(|(_, o)| !o.serializable).count();
let both = outcomes.iter().filter(|(_, o)| o.both_committed).count();
println!(
"{context}: {} interleavings, {both} committed both, {anomalies} not serializable",
outcomes.len()
);
if workload.name == DISJOINT.name
&& options.concurrency != ConcurrencyControl::Exclusive
&& (options.isolation != IsolationLevel::Serializable
|| options.serializable_validation == SerializableValidation::ReadSet)
{
assert_eq!(both, outcomes.len(), "{context}: refused independent work");
}
checked += outcomes.len();
}
}
assert!(checked > 1000, "{checked} interleavings");
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn the_checker_recognizes_serial_and_anomalous_histories() {
assert_eq!(
interleavings(2, 1),
vec![vec![0, 0, 1], vec![0, 1, 0], vec![1, 0, 0]]
);
assert_eq!(interleavings(4, 4).len(), 70);
let snapshot = EngineOptions {
isolation: IsolationLevel::Snapshot,
..Default::default()
};
let one_after_the_other = run(&snapshot, &WRITE_SKEW, &[0, 0, 0, 0, 1, 1, 1, 1]);
assert!(one_after_the_other.serializable && one_after_the_other.both_committed);
let overlapping = run(&snapshot, &WRITE_SKEW, &[0, 0, 1, 1, 0, 0, 1, 1]);
assert!(!overlapping.serializable && overlapping.both_committed);
}