Files
wekan/tests/snapMigrationSwitchOrder.test.cjs
T
Lauri Ojansivu aebede877a Snap: a finished MongoDB -> FerretDB migration no longer leaves WeKan on 503.
Upgrading a Snap from 6.09 to 10.37 migrated successfully and then served 503
until the admin ran `snap restart wekan` by hand. WeKan sat in its startup loop
printing "MongoDB not ready yet, retrying in 5 seconds..." against a MongoDB the
migration had just shut down for good.

mongodb-control ends with `exec bash $SNAP/bin/migration-control`, so
migration-control IS the wekan.mongodb service process. The switch to FerretDB
ran, in this order:

  snapctl start --enable <snap>.ferretdb
  snapctl restart <snap>.ferretdb
  snapctl stop --disable <snap>.mongodb   <- stops the service THIS script is
  snapctl restart <snap>.wekan               running in: SIGTERM, dead here

so the restart that hands WeKan over to FerretDB never ran. The reporter's log
shows it exactly: "Migration succeeded. Switching to FerretDB (SQLite); MongoDB
will be shut down." at 13:15:12, systemd stopping snap.wekan.mongodb.service and
"[migration] Interrupted (snap refresh, stop or reboot)" one second later, then
minutes of wekan.wekan waiting for a MongoDB that was gone. The comment above
that block already noted the service can terminate us - the restart just needed
to be on the other side of it.

WeKan is now restarted onto FerretDB BEFORE MongoDB is stopped, and the stop is
the last command in the switch. The traps are disarmed just before it, so being
stopped there no longer logs "Interrupted (snap refresh, stop or reboot)" after a
migration that in fact SUCCEEDED, nor runs cleanup for a finished run.

The restart must not be the only way out of that loop either, since it is the
last act of a script being killed: wekan-control's MongoDB wait now re-reads the
live `database` setting each round and re-execs itself when it has become
ferretdb. A migration that finishes while WeKan waits therefore recovers on its
own within five seconds, with no admin action.

mongodb-control's equivalent switch ("a completed FerretDB migration exists")
already had the correct order; a test now pins that too.

The high FerretDB CPU right after migration is a separate matter and is expected:
WeKan creates its indexes on first start against the new database, and in this
report WeKan never reached FerretDB until the manual restart, so that work had
not even begun when the reporter looked.

Tests: tests/snapMigrationSwitchOrder.test.cjs pins the ordering, that no snapctl
command may follow the self-terminating stop, that the traps are disarmed first,
that the pre-switch verification still comes before any of it, and the
wekan-control self-recovery. It fails on the pre-fix scripts.

Thanks to S0QR2 and xet7 !

Fixes #6525,
2026-07-25 16:22:10 +03:00

129 lines
5.9 KiB
JavaScript

'use strict';
// #6525: upgrading a Snap from 6.09 to 10.37 migrated MongoDB -> FerretDB
// successfully and then served 503 until the admin ran `snap restart wekan` by
// hand. WeKan sat in its startup loop printing "MongoDB not ready yet, retrying
// in 5 seconds..." against a MongoDB that the migration had just shut down for
// good.
//
// Cause: mongodb-control ends with `exec bash $SNAP/bin/migration-control`, so
// migration-control IS the wekan.mongodb service process. When the switch ran
// `snapctl stop --disable <snap>.mongodb`, systemd stopped the service THIS
// script runs in — SIGTERM, script dead at that line — and the
// `snapctl restart <snap>.wekan` that came AFTER it never ran. The reporter's log
// shows it exactly: "Migration succeeded. Switching to FerretDB" at 13:15:12,
// "Terminated" one second later, then minutes of WeKan waiting for MongoDB.
//
// Fix: hand WeKan over to FerretDB BEFORE stopping MongoDB — the one command that
// can terminate this process goes last — and have wekan-control notice a
// mid-wait switch itself, so the restart is not the only way out of that loop.
//
// Run: node tests/snapMigrationSwitchOrder.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
const root = path.join(__dirname, '..');
const read = p => fs.readFileSync(path.join(root, p), 'utf8');
const migration = read('snap-src/bin/migration-control');
const mongodbControl = read('snap-src/bin/mongodb-control');
const wekanControl = read('snap-src/bin/wekan-control');
// The switch block inside finish_success.
const finish = migration.slice(
migration.indexOf('finish_success() {'),
migration.indexOf('discard_partial_ferretdb()'));
console.log('snapMigrationSwitchOrder:');
test('migration-control really is the mongodb service (why order matters)', () => {
assert.ok(/exec bash "\$SNAP\/bin\/migration-control"/.test(mongodbControl),
'mongodb-control execs migration-control, so stopping wekan.mongodb kills it');
});
test('WeKan is handed to FerretDB BEFORE MongoDB is stopped', () => {
const startFerret = finish.indexOf('start --enable "${svc}.ferretdb"');
const restartWekan = finish.indexOf('restart "${svc}.wekan"');
const stopMongo = finish.indexOf('stop --disable "${svc}.mongodb"');
assert.ok(startFerret >= 0 && restartWekan >= 0 && stopMongo >= 0,
'all three service commands must be present');
assert.ok(startFerret < restartWekan,
'FerretDB must be running before WeKan is restarted onto it');
assert.ok(restartWekan < stopMongo,
'restarting WeKan must come BEFORE stopping MongoDB: the stop terminates this ' +
'very process, so anything after it never runs (that is #6525)');
});
test('stopping MongoDB is the LAST service command in the switch', () => {
const stopMongo = finish.indexOf('stop --disable "${svc}.mongodb"');
const after = finish.slice(stopMongo + 1);
assert.ok(!/snapctl (start|restart|stop) /.test(after),
'no snapctl service command may follow the stop that kills this process');
});
test('the traps are disarmed before the self-terminating stop', () => {
const disarm = finish.lastIndexOf('trap - EXIT INT TERM');
const stopMongo = finish.indexOf('stop --disable "${svc}.mongodb"');
assert.ok(disarm >= 0, 'finish_success must disarm the traps');
assert.ok(disarm < stopMongo,
'otherwise being stopped logs "Interrupted (snap refresh, stop or reboot)" ' +
'after a migration that SUCCEEDED, and runs cleanup for nothing');
});
test('the switch is still verified and recorded before any of it', () => {
// Guard the safety property the reorder must not have disturbed: nothing is
// switched until a non-empty FerretDB SQLite is confirmed.
const verify = finish.indexOf('ferretdb-has-data');
const setDb = finish.indexOf('snapctl set database=ferretdb');
const startFerret = finish.indexOf('start --enable "${svc}.ferretdb"');
assert.ok(verify >= 0 && verify < setDb && setDb < startFerret,
'verify the SQLite, then record database=ferretdb, then move the services');
});
test('wekan-control leaves the wait loop when the database is switched under it', () => {
const loop = wekanControl.slice(
wekanControl.indexOf('Waiting for MongoDB replica set primary...'),
wekanControl.indexOf('MongoDB replica set primary is ready.'));
assert.ok(/snapctl get database/.test(loop),
'the loop must re-read the live database setting, not only the value it ' +
'sampled at startup');
assert.ok(/exec "\$0"/.test(loop),
'on a switch it must re-exec onto FerretDB instead of waiting for a MongoDB ' +
'that is never coming back');
const check = loop.indexOf('snapctl get database');
const retry = loop.indexOf('MongoDB not ready yet, retrying');
assert.ok(check < retry,
'check for the switch before sleeping another round');
});
test('both scripts are still valid shell', () => {
for (const p of ['snap-src/bin/migration-control', 'snap-src/bin/wekan-control']) {
const r = spawnSync('bash', ['-n', path.join(root, p)], { encoding: 'utf8' });
assert.strictEqual(r.status, 0, `bash -n ${p} failed: ${r.stderr}`);
}
});
test('the other switch site already had the right order (negative)', () => {
// mongodb-control has the same three commands for the "a completed migration
// exists" case. It was already correct; keep it that way.
const block = mongodbControl.slice(
mongodbControl.indexOf('A completed FerretDB migration exists'));
const restartWekan = block.indexOf('restart "${svc}.wekan"');
const stopMongo = block.indexOf('stop --disable "${svc}.mongodb"');
assert.ok(restartWekan >= 0 && stopMongo >= 0);
assert.ok(restartWekan < stopMongo,
'mongodb-control must also restart WeKan before stopping MongoDB');
});
console.log(`\nsnapMigrationSwitchOrder: ${passed} tests passed`);