Files
wekan/server/lib/syncEffects.js
T
Lauri Ojansivu f06ecdee09 Share guard results within one check, and make rule archive a durable Sync action.
Stored Sync stages nest - a rule archive's delivery inside the rule stage,
inside activity delivery, inside the Sync journal - and every layer's guard
checks before and after by calling the guard below it twice. One innermost
check therefore re-ran every outer guard 2^depth times, and one Sync-created
card archived by a rule took 36 s, which is why archive was held back from
durable Sync (1e40e7e06).

reuseWithinEvaluation (server/lib/syncGuardWindow.js) runs each guard at most
once per evaluation: calls made while another guard's check is running share
the results of guards already checked in it. A call from ordinary work -
between stages, after an adapter or a write - starts a new evaluation and
checks everything afresh, so a policy change or a lost lease between stages
still stops the next one. A first attempt with a 100 ms reuse window missed
exactly that and was dropped. Failures never carry over, a guard reached
again through its own check runs fresh instead of waiting on itself, and the
journal's ownership read is left as it was.

With that, archive and unarchive are registered as durable rule adapters
through the stored archive runner, and boards whose rules archive or
unarchive now take durable Sync. The full-app durable Sync test runs an
"archive every created card" rule end to end: 1.1 s for the whole test. The
rule selection test's example of an action without an adapter is now
moveCardToTop. 565 server tests pass.

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

102 lines
6.1 KiB
JavaScript

'use strict';
const { EJSON } = require('bson');
const { validateSyncEffectPolicy, assertSyncEffectPolicy } = require('./syncEffectPolicy');
const { reuseWithinEvaluation } = require('./syncGuardWindow');
const { syncOperationEffectId, applySyncOperationStep } = require('./syncOperationApply');
const { createSyncHistoryPlanner, validateSyncFieldHistory, persistSyncFieldHistory } = require('./syncHistoryBatch');
const { prepareSyncCreationActivity, validateSyncCreationActivity, persistSyncCreationActivity } = require('./syncCreationActivity');
const { prepareSyncUpdateActivities, validateSyncUpdateActivities, persistSyncUpdateActivities } = require('./syncUpdateActivities');
const copy = value => EJSON.parse(EJSON.stringify(value), { relaxed: true });
const fail = () => { throw new Error('sync-effects-invalid'); };
// Capture all display metadata before the journal begins applying cards.
// Context changes during replay must not rewrite an already persisted event.
function createSyncEffectPlanner({ userId, username, createdAt, list, swimlanes, redoRows = [], policy }) {
policy = validateSyncEffectPolicy(policy);
const captured = copy({ userId, username, createdAt, list, swimlanes });
if (!captured.list || !Array.isArray(captured.swimlanes) || captured.swimlanes.length > 10000) fail();
const lanes = new Map();
for (const lane of captured.swimlanes) {
if (!lane || typeof lane._id !== 'string' || !lane._id || lanes.has(lane._id) ||
lane.boardId !== captured.list.boardId || typeof lane.title !== 'string') fail();
lanes.set(lane._id, lane);
}
// History rows are linked when appended, not planned (syncHistoryBatch.js).
const historyPlanner = createSyncHistoryPlanner({ userId, createdAt, redoRows });
return (step, context) => {
const effectId = syncOperationEffectId(context.operationId, context.index);
// Validate activities first: a failed reference must not advance the
// History planner and make a corrected retry of this index impossible.
const activities = policy.activities ? (step.kind === 'create' ? prepareSyncCreationActivity : prepareSyncUpdateActivities)({
...captured, step, effectId, swimlane: lanes.get(step.after.swimlaneId) }) : null;
const plan = { version: 2, policy: { ...policy }, history: historyPlanner(step, context), activities };
validateSyncEffects(plan, step, effectId);
return plan;
};
}
function validateSyncEffects(plan, step, effectId) {
if (!plan || ![1, 2].includes(plan.version) || Object.keys(plan).sort().join(',') !==
(plan.version === 1 ? 'activities,history,version' : 'activities,history,policy,version')) fail();
const policy = planPolicy(plan);
validateSyncFieldHistory(plan.history, step, effectId);
if (policy.activities) {
(step.kind === 'create' ? validateSyncCreationActivity : validateSyncUpdateActivities)(plan.activities, step, effectId);
const actor = step.kind === 'create' ? plan.activities.activity : plan.activities.context;
if (plan.history.userId !== actor.userId || plan.history.rows.some(row => row.createdAt.getTime() !== actor.createdAt.getTime())) fail();
} else if (plan.activities !== null) fail();
if (Buffer.byteLength(EJSON.stringify(plan)) > 15 * 1024 * 1024) fail();
return true;
}
function planPolicy(plan) {
// Version-one plans predate policy capture and always include activities.
return plan.version === 1 ? { activities: true, notifications: true } : validateSyncEffectPolicy(plan.policy);
}
// Internal coordinator: callers still supply durable rules/notification
// delivery and coordinate ordinary hooks. Changed flags pause the saved plan;
// replay never silently replaces its captured policy with current defaults.
function createEffectGuard({ history, activities, plan, step, effectId, assertCurrent, completeDelivery, readPolicy }) {
validateSyncEffects(plan, step, effectId);
const policy = planPolicy(plan);
if (typeof assertCurrent !== 'function' || typeof readPolicy !== 'function' ||
!['findOneAsync','insertAsync','updateAsync'].every(key => typeof history?.[key] === 'function') ||
(policy.activities && (typeof completeDelivery !== 'function' ||
!['findOneAsync','insertAsync'].every(key => typeof activities?.[key] === 'function')))) fail();
return reuseWithinEvaluation(async () => {
await assertCurrent();
await assertSyncEffectPolicy(policy, readPolicy);
await assertCurrent();
});
}
async function persistSyncEffects(options) {
const { history, activities, step, effectId, completeDelivery } = options;
const plan = copy(options.plan);
const policy = planPolicy(plan);
const guard = createEffectGuard({ ...options, plan });
await guard();
await persistSyncFieldHistory({ history, plan: plan.history, assertCurrent: guard });
if (policy.activities) {
await (step.kind === 'create' ? persistSyncCreationActivity : persistSyncUpdateActivities)({
activities, plan: plan.activities, step, effectId, assertCurrent: guard,
completeDelivery: context => completeDelivery({ ...context, policy: { ...policy } }) });
}
await guard();
return effectId;
}
// Apply one saved card unit only after validating its entire effect plan,
// persisted actor, live feature policy and all required adapters. Ordinary
// collection hooks and durable delivery remain the production caller's job.
async function applySyncEffectsStep({ cards, history, activities, step, effects, operationId, index,
userId, assertCurrent, completeDelivery, readPolicy }) {
const effectId = syncOperationEffectId(operationId, index);
const plan = copy(effects), savedStep = copy(step);
validateSyncEffects(plan, savedStep, effectId);
if (typeof userId !== 'string' || !userId || plan.history.userId !== userId) fail();
const options = { history, activities, plan, step: savedStep, effectId, assertCurrent, completeDelivery, readPolicy };
const guard = createEffectGuard(options);
return applySyncOperationStep({ cards, step: savedStep, operationId, index, assertCurrent: guard,
completeEffects: () => persistSyncEffects(options) });
}
module.exports = { createSyncEffectPlanner, validateSyncEffects, persistSyncEffects, applySyncEffectsStep };