About: how pages are checked

tests/sims/aries.test.ts

The automated tests for the /simulators/wal-recovery simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.

  1. Recovery leaves exactly the committed updates, even after a second crash during undo.
// /simulators/wal-recovery: ARIES restart recovery.
import { test } from 'node:test';
import { recover, diskAtCrash, type Rec } from '../../src/lib/sims/aries.ts';
import { checker, lcg } from './check.ts';

// Generates a random log of updates, commits, aborts with rollback, page flushes and checkpoints, and
// the page values a correct recovery must leave: the last update by a committed transaction that was
// not later compensated.
function gen(r: () => number) {
	const log: Rec[] = [];
	let lsn = 0;
	const nx = () => (lsn += 1);
	const pages = ['P1', 'P2', 'P3', 'P4', 'P5'];
	const mem: Record<string, number> = {};
	const lock: Record<string, string | null> = {};
	const active: Record<string, { updates: Extract<Rec, { type: 'update' }>[]; state: 'run' | 'commit' }> = {};
	let tn = 0;
	const committed = new Set<string>();
	const steps = 10 + Math.floor(r() * 40);
	const rollback = (t: string, upto: number) => {
		const ups = active[t].updates;
		for (let k = 0; k < upto && ups.length; k++) {
			const u = ups.pop()!;
			const undoNext = ups.length ? ups[ups.length - 1].lsn : null;
			const clr: Rec = { lsn: nx(), type: 'clr', txn: t, page: u.page, after: u.before, undoNext, undoes: u.lsn };
			mem[u.page] = u.before;
			log.push(clr);
		}
	};
	for (let i = 0; i < steps; i++) {
		const x = r();
		const live = Object.keys(active).filter((t) => active[t].state === 'run');
		if (x < 0.15 || !live.length) {
			active['T' + ++tn] = { updates: [], state: 'run' };
			continue;
		}
		const t = live[Math.floor(r() * live.length)];
		if (x < 0.6) {
			const free = pages.filter((p) => !lock[p] || lock[p] === t);
			if (!free.length) continue;
			const p = free[Math.floor(r() * free.length)];
			lock[p] = t;
			const id = nx();
			const u = { lsn: id, type: 'update', txn: t, page: p, before: mem[p] ?? 0, after: id } as Extract<Rec, { type: 'update' }>;
			mem[p] = id;
			log.push(u);
			active[t].updates.push(u);
		} else if (x < 0.72) {
			log.push({ lsn: nx(), type: 'commit', txn: t });
			committed.add(t);
			active[t].state = 'commit';
			for (const p of pages) if (lock[p] === t) lock[p] = null;
			if (r() < 0.6) log.push({ lsn: nx(), type: 'end', txn: t });
		} else if (x < 0.8) {
			log.push({ lsn: nx(), type: 'abort', txn: t });
			const full = r() < 0.6;
			rollback(t, full ? 99 : 1);
			if (full) {
				log.push({ lsn: nx(), type: 'end', txn: t });
				delete active[t];
				for (const p of pages) if (lock[p] === t) lock[p] = null;
			} else active[t].state = 'run';
		} else if (x < 0.92) {
			const p = pages[Math.floor(r() * pages.length)];
			if (mem[p] !== undefined) log.push({ lsn: nx(), type: 'flush', page: p });
		} else log.push({ lsn: nx(), type: 'checkpoint', tt: {}, dpt: {} });
	}
	// fill each checkpoint with the transaction and dirty page tables at that point
	const tt: Record<string, { lastLSN: number; status: 'U' | 'C' }> = {};
	const dpt: Record<string, number> = {};
	for (const x of log) {
		if (x.type === 'update' || x.type === 'clr') {
			tt[x.txn] = { lastLSN: x.lsn, status: tt[x.txn]?.status ?? 'U' };
			if (dpt[x.page] === undefined) dpt[x.page] = x.lsn;
		} else if (x.type === 'commit') tt[x.txn] = { lastLSN: x.lsn, status: 'C' };
		else if (x.type === 'abort') tt[x.txn] = { lastLSN: x.lsn, status: tt[x.txn]?.status ?? 'U' };
		else if (x.type === 'end') delete tt[x.txn];
		else if (x.type === 'flush') delete dpt[x.page];
		else if (x.type === 'checkpoint') {
			x.tt = JSON.parse(JSON.stringify(tt));
			x.dpt = { ...dpt };
		}
	}
	const exp: Record<string, number> = {};
	for (const p of pages) exp[p] = 0;
	const compensated = new Set(log.filter((x) => x.type === 'clr').map((x) => (x as Extract<Rec, { type: 'clr' }>).undoes));
	for (const x of log) if (x.type === 'update' && committed.has(x.txn) && !compensated.has(x.lsn)) exp[x.page] = x.after;
	return { log, exp };
}

test('recovery leaves exactly the committed updates, even after a second crash during undo', () => {
	const { ok, done } = checker();
	const r = lcg(11);
	for (let k = 0; k < 2000; k++) {
		const { log, exp } = gen(r);
		const disk0 = diskAtCrash(log);
		const rec = recover(log, disk0, 1);
		const got: Record<string, number> = {};
		for (const p of Object.keys(exp)) got[p] = rec.disk[p]?.value ?? 0;
		ok(JSON.stringify(got) === JSON.stringify(exp), `case ${k}: ${JSON.stringify(got)} vs ${JSON.stringify(exp)}`);
		const clrs = rec.written;
		if (clrs.length > 1) {
			const cut = 1 + Math.floor(r() * (clrs.length - 1));
			const rec2 = recover([...log, ...clrs.slice(0, cut)], disk0, 1);
			const got2: Record<string, number> = {};
			for (const p of Object.keys(exp)) got2[p] = rec2.disk[p]?.value ?? 0;
			ok(JSON.stringify(got2) === JSON.stringify(exp), `second crash, case ${k}`);
		}
	}
	done();
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published