mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Undoing a 10,500-card sprint close took 282 s and redoing it 578 s, about 27 ms a card: each record of a History row cost some thirty database round trips - a read per record in the preflight, two ownership checks (two reads each) around every read, the same again around a readback after every write, and the whole row read back record by record twice more at the end. Now a row's records are read in one query per type (readTargets), and inspectScrumHistoryWrites takes that as readMany, bracketed by one ownership check before and one after: the worker's claim and the source row only ever go from current to displaced, so current before and after the read means current throughout it. Every write keeps its ownership check right before it, after the read it is computed from; the readback after a write confirms the value landed and leaves ownership to the next write's fence and the batch's final readback. A sprint or release that thousands of cards name is looked up once, not once a card. The permission check stays per card. The 10,500-card start / close / undo / redo test went from 14 min 27 s to 6 min 7 s on the same machine; all 18 Scrum tests pass. tests/scrumHistoryWriteState.test.cjs pins one read and three checks for a bracketed read, ownership lost during it, a short answer, a missing record and identities checked before any read (negative), and that every write in the path is still fenced after its read. Thanks to xet7 !
186 lines
13 KiB
JavaScript
186 lines
13 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { scrumHistoryWriteState: state } = require('../server/lib/scrumHistoryWriteState');
|
|
const { historyDocument } = require('../models/lib/scrumHistory');
|
|
for (const type of ['board', 'card', 'list', 'swimlane', 'scrum-sprint', 'scrum-release', 'scrum-event']) {
|
|
test(`${type}: unchanged, pending and applied states require the operation's revision`, () => {
|
|
const metadata = ['board', 'card', 'list', 'swimlane'].includes(type);
|
|
const field = metadata ? 'scrumRevision' : 'revision';
|
|
const current = { _id: 'item', boardId: 'board', [field]: 4, ...(metadata ? { scrum: { value: 'before' } } : { name: 'before' }) };
|
|
const changed = { ...current, [field]: 5, ...(metadata ? { scrum: { value: 'after' } } : { name: 'after' }) };
|
|
const before = historyDocument(type, current), after = historyDocument(type, changed);
|
|
const options = { type, current, before, after, revision: 4 };
|
|
assert.equal(state(options), 'pending');
|
|
assert.equal(state({ ...options, current: changed }), 'applied');
|
|
assert.equal(state({ ...options, after: before }), 'applied');
|
|
for (const value of [4, 6, -1, '5', Number.NaN]) {
|
|
assert.throws(() => state({ ...options, current: { ...changed, [field]: value } }), /revision conflict/);
|
|
}
|
|
assert.throws(() => state({ ...options, current: { ...current, [field]: 5 } }), /revision conflict/);
|
|
assert.throws(() => state({ ...options, after: before, current: { ...current, [field]: 5 } }), /revision conflict/);
|
|
});
|
|
}
|
|
test('creation/deletion and legacy revision zero distinguish acknowledged writes from later replacements', () => {
|
|
const after = { _id: 'sprint', boardId: 'board', name: 'Restored', startsAt: new Date(0) };
|
|
const options = { type: 'scrum-sprint', current: null, before: null, after, revision: null };
|
|
assert.equal(state(options), 'pending');
|
|
assert.equal(state({ ...options, current: { ...after, revision: 1 } }), 'applied');
|
|
assert.throws(() => state({ ...options, current: { ...after, revision: 2 } }), /conflict/);
|
|
assert.equal(state({ type: 'scrum-sprint', current: null, before: after, after: null, revision: 1 }), 'applied');
|
|
assert.equal(state({ type: 'scrum-sprint', current: after, before: after, after: null, revision: 0 }), 'pending');
|
|
assert.throws(() => state({ ...options, revision: 0 }), /conflict/);
|
|
});
|
|
|
|
const { verifyScrumHistoryWrites: verify } = require('../server/lib/scrumHistoryWriteState');
|
|
function verification() {
|
|
const rows = [1, 2].map(i => ({ _id: `card-${i}`, boardId: 'board', scrum: { value: 'after' }, scrumRevision: 2 }));
|
|
const targets = rows.map(row => ({ type: 'card', id: row._id, document: historyDocument('card', row) }));
|
|
return { rows, options: { targets, before: targets.map(entry => ({ ...entry, document: { ...entry.document, scrum: {} } })),
|
|
revisions: [1, 1], read: async entry => rows.find(row => row._id === entry.id), assertCurrent: async () => {} } };
|
|
}
|
|
test('confirmed batch readback rejects missing, unapplied and newer-revision values', async () => {
|
|
await verify(verification().options);
|
|
for (const damage of [f => { f.rows.pop(); }, f => { f.rows[1].scrum = {}; f.rows[1].scrumRevision = 1; },
|
|
f => { f.rows[0].scrumRevision = 3; }]) {
|
|
const f = verification(); damage(f); await assert.rejects(verify(f.options), /conflict/);
|
|
}
|
|
});
|
|
test('batch readback retains failures and checks ownership after reads', async () => {
|
|
const f = verification(); f.options.read = async () => { throw Error('read unavailable'); };
|
|
await assert.rejects(verify(f.options), /read unavailable/);
|
|
const g = verification(); let read = false;
|
|
g.options.read = async entry => { read = true; return g.rows.find(row => row._id === entry.id); };
|
|
g.options.assertCurrent = async () => { if (read) throw Error('ownership lost'); };
|
|
await assert.rejects(verify(g.options), /ownership lost/);
|
|
const h = verification(); h.options.before[1].id = 'other';
|
|
await assert.rejects(verify(h.options), /conflict/);
|
|
});
|
|
test('read-only preflight accepts mixed pending/applied steps and rejects later conflicts before writes', async () => {
|
|
const { inspectScrumHistoryWrites: inspect } = require('../server/lib/scrumHistoryWriteState');
|
|
const f = verification();
|
|
f.rows[0].scrum = {}; f.rows[0].scrumRevision = 1;
|
|
assert.deepEqual(await inspect(f.options), ['pending', 'applied']);
|
|
f.rows[1].scrumRevision = 3;
|
|
let applied = false;
|
|
await assert.rejects((async () => {
|
|
await inspect(f.options); applied = true;
|
|
})(), /conflict/);
|
|
assert.equal(applied, false);
|
|
assert.equal(f.rows[0].scrumRevision, 1);
|
|
assert.deepEqual(f.rows[0].scrum, {});
|
|
});
|
|
|
|
// Incarnations (maintainer decision of 2026-09-30): matching values and
|
|
// revision do not prove a record is the one this operation planned against or
|
|
// created - one deleted and recreated under the same _id can match both.
|
|
const { plannedIncarnations } = require('../server/lib/scrumHistoryWriteState');
|
|
test('a record recreated under the same _id is not taken for the original', () => {
|
|
const current = { _id: 's1', boardId: 'b', name: 'Sprint', revision: 3, incarnation: 'life-1' };
|
|
const before = historyDocument('scrum-sprint', current);
|
|
assert.equal(before.incarnation, undefined, 'History content never carries an incarnation');
|
|
// Deleting it: pending while the planned lifetime is there...
|
|
assert.equal(state({ type: 'scrum-sprint', current, before, after: null, revision: 3,
|
|
incarnation: { before: 'life-1', after: null } }), 'pending');
|
|
// ...but a same-looking record from ANOTHER lifetime must not be deleted (negative).
|
|
assert.throws(() => state({ type: 'scrum-sprint', current: { ...current, incarnation: 'life-2' }, before,
|
|
after: null, revision: 3, incarnation: { before: 'life-1', after: null } }), /revision conflict/);
|
|
// Creating it: only the incarnation this operation chose counts as its insert.
|
|
const created = { ...current, revision: 1, incarnation: 'planned' };
|
|
const plan = { type: 'scrum-sprint', before: null, after: before, revision: null,
|
|
incarnation: { before: null, after: 'planned' } };
|
|
assert.equal(state({ ...plan, current: null }), 'pending');
|
|
assert.equal(state({ ...plan, current: created }), 'applied');
|
|
assert.throws(() => state({ ...plan, current: { ...created, incarnation: 'someone-else' } }), /revision conflict/);
|
|
// An update keeps the same lifetime.
|
|
const updated = { ...current, name: 'Renamed', revision: 4 };
|
|
const change = { type: 'scrum-sprint', before, after: historyDocument('scrum-sprint', updated), revision: 3,
|
|
incarnation: { before: 'life-1', after: 'life-1' } };
|
|
assert.equal(state({ ...change, current: updated }), 'applied');
|
|
assert.throws(() => state({ ...change, current: { ...updated, incarnation: 'life-9' } }), /revision conflict/);
|
|
});
|
|
|
|
test('checkpoints and records from before incarnations keep working; bad shapes are refused', () => {
|
|
const legacy = { _id: 's1', boardId: 'b', name: 'Sprint', revision: 3 };
|
|
const doc = historyDocument('scrum-sprint', legacy);
|
|
assert.equal(state({ type: 'scrum-sprint', current: legacy, before: doc, after: null, revision: 3 }), 'pending',
|
|
'a checkpoint without incarnations is judged as before');
|
|
assert.equal(state({ type: 'scrum-sprint', current: legacy, before: doc, after: null, revision: 3,
|
|
incarnation: { before: null, after: null } }), 'pending', 'a record without one is lifetime null');
|
|
for (const incarnation of [null, {}, { before: 'x' }, { before: 1, after: null }, { before: '', after: null }]) {
|
|
assert.throws(() => state({ type: 'scrum-sprint', current: legacy, before: doc, after: null, revision: 3, incarnation }), /revision conflict/);
|
|
}
|
|
});
|
|
|
|
test('a new checkpoint plans the lifetime each target has and will have', () => {
|
|
let n = 0;
|
|
const planned = plannedIncarnations({ newId: () => `new-${++n}`,
|
|
targets: [{ type: 'scrum-sprint', document: { name: 'x' } }, { type: 'scrum-release', document: null },
|
|
{ type: 'scrum-event', document: { name: 'y' } }, { type: 'card', document: { scrum: {} } }],
|
|
current: [null, { incarnation: 'r-1' }, { incarnation: 'e-1' }, { _id: 'c' }] });
|
|
assert.deepEqual(planned, [{ before: null, after: 'new-1' }, { before: 'r-1', after: null },
|
|
{ before: 'e-1', after: 'e-1' }, { before: null, after: null }]);
|
|
assert.throws(() => plannedIncarnations({ targets: [{}], current: [], newId: () => 'x' }), /revision conflict/);
|
|
});
|
|
|
|
test('every place a Scrum record is created gives it a fresh incarnation', () => {
|
|
const fs = require('node:fs'), path = require('node:path');
|
|
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
|
assert.match(read('server/scrum.js'), /if \(!before\) \{[\s\S]*?after\.incarnation = Random\.id\(\);/);
|
|
assert.match(read('server/lib/scrumTransferImport.js'), /revision: 1,\n\s*incarnation: Random\.id\(\),/);
|
|
const history = read('server/lib/scrumHistory.js');
|
|
assert.match(history, /incarnations: plannedIncarnations\(\{ targets, current, newId: \(\) => Random\.id\(\) \}\)/);
|
|
assert.match(history, /\.\.\.\(incarnation\?\.after \? \{ incarnation: incarnation\.after \} : \{\}\)/);
|
|
// Negative: an update never unsets it, although History content leaves it out.
|
|
assert.match(history, /\['_id','revision','updatedAt','updatedBy','incarnation'\]\.includes\(key\)/);
|
|
assert.doesNotMatch(read('models/lib/scrum.js'), /incarnation/, 'a client can not supply one');
|
|
});
|
|
|
|
// Batch undo/redo speed (maintainer decision of 2026-10-14, "optimoi nyt"): a
|
|
// row's targets are read in one query per type (readMany), bracketed by the
|
|
// ownership checks, instead of one read and two checks per record.
|
|
test('readMany: one bracketed read gives the same states, and ownership is still checked after it', async () => {
|
|
const { inspectScrumHistoryWrites: inspect } = require('../server/lib/scrumHistoryWriteState');
|
|
const f = verification();
|
|
f.rows[0].scrum = {}; f.rows[0].scrumRevision = 1;
|
|
let reads = 0, checks = 0;
|
|
f.options.read = async () => { throw Error('per-record read used'); };
|
|
f.options.readMany = async targets => { reads++; return targets.map(t => f.rows.find(row => row._id === t.id)); };
|
|
f.options.assertCurrent = async () => { checks++; };
|
|
assert.deepEqual(await inspect(f.options), ['pending', 'applied']);
|
|
assert.equal(reads, 1, 'one read for the whole row');
|
|
assert.equal(checks, 3, 'before, after, and at the end - not two a record');
|
|
// Negative: ownership lost during the read is caught after it.
|
|
const g = verification(); let read = false;
|
|
g.options.readMany = async targets => { read = true; return targets.map(t => g.rows.find(row => row._id === t.id)); };
|
|
g.options.assertCurrent = async () => { if (read) throw Error('ownership lost'); };
|
|
await assert.rejects(verify(g.options), /ownership lost/);
|
|
// Negative: a short answer, a missing record, a wrong-identity target.
|
|
const h = verification(); h.options.readMany = async () => [h.rows[0]];
|
|
await assert.rejects(inspect(h.options), /conflict/);
|
|
const i = verification(); i.options.readMany = async targets => targets.map(() => undefined);
|
|
await assert.rejects(verify(i.options), /conflict/);
|
|
const j = verification(); j.options.readMany = async () => { throw Error('never read'); }; j.options.before[1].id = 'other';
|
|
await assert.rejects(inspect(j.options), /conflict/, 'identity is checked before any read');
|
|
const k = verification(); k.options.readMany = 'yes';
|
|
await assert.rejects(inspect(k.options), /conflict/);
|
|
});
|
|
|
|
test('the Scrum History write path reads in bulk and fences every write', () => {
|
|
const history = require('node:fs').readFileSync(require('node:path').join(__dirname, '../server/lib/scrumHistory.js'), 'utf8');
|
|
assert.match(history, /async function readTargets\(targets\) \{[\s\S]{0,500}find\(\{ _id: \{ \$in: \[\.\.\.ids\] \} \}\)/);
|
|
assert.equal((history.match(/const current = await readTargets\(targets\);/g) || []).length, 2);
|
|
assert.equal((history.match(/readMany: readTargets/g) || []).length, 2, 'preflight and whole-batch readback');
|
|
// Negative: no per-record read of the whole row is left.
|
|
assert.doesNotMatch(history, /Promise\.all\(targets\.map\(entry => collections\[entry\.type\]\.findOneAsync/);
|
|
// Every write still has the ownership check right before it.
|
|
const loop = history.slice(history.indexOf('for (let index = 0; index < targets.length; index += 1) {'), history.indexOf('Keep recovery durable'));
|
|
for (const write of ['removeAsync(selector)', 'updateAsync(selector, { $set: { scrum:', 'insertAsync({ ...EJSON.clone(entry.document)', 'updateAsync(selector, modifier)']) {
|
|
const at = loop.indexOf(write);
|
|
assert.ok(at > 0, write);
|
|
assert.ok(loop.lastIndexOf('await assertCurrent();', at) > loop.indexOf('scrumHistoryWriteState({ type: entry.type, current,'), `${write}: fenced after its read`);
|
|
}
|
|
// A shared sprint is read once, not once a card.
|
|
assert.match(history, /if \(!looked\.has\(key\)\) looked\.set\(key, await collections\[type\]\.findOneAsync\(id\)\);/);
|
|
});
|