Files
wekan/server/lib/historyChainMigration.js
T
Lauri Ojansivu e191baef7e Recover abandoned History writers online, fenced, without stopping servers.
A legacy History writer holds a token on its board's gate until its write is
confirmed, with no expiry, so an uncertain write can never become invisible to
a chain migration. A writer that died kept its token forever, and the only way
to remove it was the offline tool with every server stopped. Maintainer
decision of 2026-09-30: build online recovery - liveness heartbeats and fencing
on History inserts - and keep the offline tool as the fallback.

Each writer on an upgraded server now writes a lease beside its token
(historyWriterLeases) and renews it while it works. Before every legacy
History insert it claims the row id in the lease; only an active lease can
claim. Recovery takes a lease only after it has been expired for another full
lease period (HISTORY_WRITER_LEASE_MS, default 30 s). It sets the lease to
fenced, so the old writer can neither renew nor claim. A claimed row id is
then fenced by a tombstone under that id, so the unique _id makes a late
insert fail, and a row that had already landed stays. Only then is the token
removed. Safety rests on the fence; clocks only decide when a lease may be
taken, and a slow writer that is taken loses its write with an error.
Tombstones use the reserved board id #history-writer-fence and are invisible
to every board reader. A Sync append refuses a tombstone as its planned row.

A chain migration takes abandoned writers by itself while the board drains,
a full gate does the same once, and releases/recover-history-writer.cjs
--recover-expired does it on demand, online. Tokens without a lease (older
servers) are listed and still need the offline procedure.

MongoDB tests with two clients: a writer that died after claiming a row is
taken and its late insert fails; a fenced slow writer cannot claim; a row
that landed before the fence is kept; live writers and lease-less tokens are
not taken; a migration drains a dead writer and completes on the real chain
head. A full-app test covers the production binding. 563 server tests pass.

Thanks to xet7 !
2026-09-30 11:03:37 +03:00

38 lines
2.3 KiB
JavaScript

'use strict';
const { beginHistoryMigration, finishHistoryMigration, recoverExpiredHistoryWriters } = require('./historyWriterGate');
const { initializeHistoryChain, inspectHistoryChain } = require('./historyChainBootstrap');
const { historyChainId, validateHistoryChainHead } = require('./historyChainAppend');
// Shared by the server binding and the offline maintenance command. Admission
// excludes participating writers; the deployment guard must exclude all others.
// With `leases`, writers that died holding a token are taken online while the
// board drains (historyWriterGate.js); a token without a lease still blocks.
async function migrateHistoryChain({ gates, heads, history, leases = null, graceMs = 30000, boardId, migrationId,
assertDeploymentExclusive }) {
if (typeof assertDeploymentExclusive !== 'function') throw new Error('history-writer-deployment-guard-required');
await assertDeploymentExclusive();
const options = { gates, boardId, migrationId };
const recoverWriters = leases
? async () => { await recoverExpiredHistoryWriters({ gates, leases, history, boardId, graceMs }); }
: null;
const migration = await beginHistoryMigration({ ...options, recoverWriters });
const readHead = async () => validateHistoryChainHead(await heads.findOne({ _id: historyChainId(boardId) }), boardId);
if (migration.complete) {
await readHead(); await assertDeploymentExclusive(); return migrationId;
}
const assertExclusive = async () => { await assertDeploymentExclusive(); await migration.assertExclusive(); };
await initializeHistoryChain({ heads, history, boardId, assertExclusive });
await finishHistoryMigration({ ...options, assertHeadReady: async () => {
await assertExclusive();
const head = await readHead();
// A pre-existing head can be stale after legacy writes or an interrupted
// rollout. Structural validity alone cannot authorize the permanent switch.
if (head.pending !== null) throw new Error('history-chain-migration-pending');
const actual = await inspectHistoryChain({ history, boardId, assertExclusive });
if (actual.hash !== head.hash) throw new Error('history-chain-migration-head-mismatch');
await assertExclusive();
} });
await assertDeploymentExclusive(); return migrationId;
}
module.exports = { migrateHistoryChain };