mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
A Scrum History undo, redo or restore keeps a checkpoint that blocks every Scrum edit on its board until it finishes, and only its author could retry it, with the same plan. When somebody else had changed one of its records, its History row changed or its author lost write access, every retry failed the same way, the checkpoint could not be discarded online or offline, and the board's Scrum edits stayed blocked for good. A retry that fails for a reason the next one would meet again now marks its checkpoint. A board administrator sees such a checkpoint in the History recovery notice and can roll it back, which restores every record it wrote to the saved "before" value and revision and is offered only when nobody changed those records since and none of its History was written, or keep the board as it is, which removes the checkpoint and writes nothing while reporting how much of the operation had been written. A checkpoint that has not stopped on a conflict stays its author's to retry. The resolution names the checkpoint it resolves, so a repeat is a no-op; it claims the checkpoint by compare-and-set, fencing the author's workers, waits for an in-flight write, and checks every record before and after its own writes. Each one is recorded in Admin Panel / Problems / Recovery. The offline command releases/recover-scrum-history.cjs does the same with every writer stopped. Unit, integration (against MongoDB 7), Meteor and Playwright tests cover rollback, discard, refusals, concurrency and the offline command. New text is in English, pending Transifex. Thanks to xet7 !
78 lines
5.0 KiB
JavaScript
78 lines
5.0 KiB
JavaScript
'use strict';
|
|
const { randomUUID } = require('node:crypto');
|
|
const fail = () => { throw new Error('Scrum History checkpoint conflict'); };
|
|
function scrumHistorySelector(journal) {
|
|
if (!journal || !['_id', 'rowId', 'userId'].every(key => typeof journal[key] === 'string' && journal[key]) ||
|
|
!['undo', 'redo', 'restore'].includes(journal.direction) ||
|
|
!Array.isArray(journal.content?.records) || !Array.isArray(journal.before?.records) ||
|
|
!Array.isArray(journal.revisions) || journal.content.records.length !== journal.before.records.length ||
|
|
journal.content.records.length !== journal.revisions.length ||
|
|
(Object.hasOwn(journal, 'operationId') && (typeof journal.operationId !== 'string' || !journal.operationId)) ||
|
|
(Object.hasOwn(journal, 'incarnations') && (!Array.isArray(journal.incarnations) ||
|
|
journal.incarnations.length !== journal.revisions.length))) fail();
|
|
// A checkpoint saved before incarnations existed has none, and gets no key
|
|
// here: the completion's planHash digests this selector, so adding one would
|
|
// change the identity of a checkpoint that was pending across the upgrade.
|
|
return { _id: journal._id, rowId: journal.rowId, userId: journal.userId, direction: journal.direction,
|
|
operationId: Object.hasOwn(journal, 'operationId') ? journal.operationId : { $exists: false },
|
|
content: { $eq: journal.content }, before: { $eq: journal.before }, revisions: { $eq: journal.revisions },
|
|
...(Object.hasOwn(journal, 'incarnations') ? { incarnations: { $eq: journal.incarnations } } : {}) };
|
|
}
|
|
// A checkpoint a board administrator or an operator is resolving
|
|
// (scrumHistoryRecovery.js) is no longer its author's: it is neither resumed
|
|
// nor finished. `resolving` stays out of scrumHistorySelector for the same
|
|
// reason `worker` does: the completion's planHash digests that selector.
|
|
const unresolved = { resolving: { $exists: false } };
|
|
async function assertScrumHistoryOperation(pending, journal) {
|
|
if (!journal.operationId || !await pending.findOneAsync({ ...scrumHistorySelector(journal), ...unresolved })) fail();
|
|
}
|
|
// Upgrade old checkpoints with compare-and-set, then adopt only the winning ID
|
|
// for the unchanged plan. Concurrent workers never overwrite each other's ID.
|
|
async function ensureScrumHistoryOperation(pending, journal, newId = randomUUID) {
|
|
const selector = scrumHistorySelector(journal);
|
|
if (journal.operationId) { await assertScrumHistoryOperation(pending, journal); return journal; }
|
|
const operationId = newId();
|
|
if (typeof operationId !== 'string' || !operationId) fail();
|
|
let error;
|
|
try { await pending.updateAsync(selector, { $set: { operationId } }); }
|
|
catch (failure) { error = failure; }
|
|
const { operationId: absent, ...plan } = selector;
|
|
let saved;
|
|
try { saved = await pending.findOneAsync(plan); } catch (failure) { throw error || failure; }
|
|
if (!saved || typeof saved.operationId !== 'string' || !saved.operationId) throw error || new Error('Scrum History checkpoint unconfirmed');
|
|
await assertScrumHistoryOperation(pending, saved);
|
|
return saved;
|
|
}
|
|
// Same-operation worker serialization. The board lock in server/scrum.js is a
|
|
// per-process queue, so two server processes could resume ONE operation at the
|
|
// same time and interleave its writes. Each worker now claims the checkpoint
|
|
// with its own id; the newest claim wins, and a displaced worker stops at its
|
|
// next guard instead of writing on. `worker` is deliberately not part of
|
|
// scrumHistorySelector: the completion's planHash digests that selector, and
|
|
// who resumed a plan is not part of the plan.
|
|
async function claimScrumHistoryWorker(pending, journal, worker = randomUUID()) {
|
|
if (!journal?.operationId || typeof worker !== 'string' || !worker) fail();
|
|
const selector = scrumHistorySelector(journal);
|
|
let error;
|
|
try { await pending.updateAsync({ ...selector, ...unresolved }, { $set: { worker } }); }
|
|
catch (failure) { error = failure; }
|
|
let saved;
|
|
try { saved = await pending.findOneAsync({ ...selector, worker }); } catch (failure) { throw error || failure; }
|
|
if (!saved) throw error || new Error('Scrum History checkpoint conflict');
|
|
return worker;
|
|
}
|
|
// Checked before every write of an operation, so it must not send the whole
|
|
// plan each time: a batch of a thousand records sent its plan a thousand
|
|
// times. The worker id is random and was set only on the checkpoint that
|
|
// matched the whole plan (claimScrumHistoryWorker), and nothing ever rewrites
|
|
// a checkpoint's plan - only operationId and worker are set - so the plan's
|
|
// identity and the worker id find the same checkpoint.
|
|
async function assertScrumHistoryWorker(pending, journal, worker) {
|
|
scrumHistorySelector(journal);
|
|
if (!journal?.operationId || typeof worker !== 'string' || !worker ||
|
|
!await pending.findOneAsync({ _id: journal._id, rowId: journal.rowId, userId: journal.userId,
|
|
direction: journal.direction, operationId: journal.operationId, worker }, { fields: { _id: 1 } })) fail();
|
|
}
|
|
module.exports = { scrumHistorySelector, assertScrumHistoryOperation, ensureScrumHistoryOperation,
|
|
claimScrumHistoryWorker, assertScrumHistoryWorker };
|