mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -05:00
An instance on 10.96 went offline at every restart. wekan-control ran bin/database-autopick synchronously and unbounded before anything opened the web port - and that comparison starts each database on a temporary port to read it, which on a real instance is minutes. Until it returned a browser got a connection timeout, and the reason was in `snap logs`, the last place somebody whose site is down thinks to look. That is #6592 one step earlier than #6592 was fixed. The database WAIT already serves a "waiting for its database" page; the helpers for it were defined 200 lines BELOW the comparison that needed them. They move up, and the comparison uses them - after the same grace period, so a comparison that finishes in seconds does not flash a page up and teach proxies to cache a 503 for a healthy site. And a bound: WEKAN_AUTOPICK_TIMEOUT, 900s by default. When it runs out WeKan says so, changes nothing, and starts on the database already selected. Stopping the comparison is safe - it never deletes anything on either side, and the merge it may be in the middle of only INSERTS what is missing, so a partial one is fewer documents copied and the next start finishes the job. Serving the site beats finishing the comparison. WEKAN_AUTOPICK was an env var and nothing else, so an admin whose site was down had no supported way to skip the comparison. It is a snap config key now, with the bound beside it: snap set wekan autopick=false snap set wekan autopick-timeout=1800 The timeout message names all three of those, because somebody reading it is somebody whose site just came up late. This is the other half of the mtime fix. Before that, the comparison ran at EVERY start - autopick's own mongod stamped the files the staleness check reads - so every restart took the site down for a comparison that could never conclude. One made it recur; this one makes it survivable. Thanks to xet7 !
126 lines
6.2 KiB
JavaScript
126 lines
6.2 KiB
JavaScript
'use strict';
|
|
|
|
// Guard: comparing the two copies of the database cannot take the site down.
|
|
// Run: node tests/autopickDoesNotBlockStartup.test.cjs
|
|
//
|
|
// THE OUTAGE. An instance on 10.96 went offline at every restart. bin/wekan-control
|
|
// ran bin/database-autopick synchronously, unbounded, before anything opened the
|
|
// web port - and the comparison starts each database on a temporary port, which on
|
|
// a real instance is minutes. Until it returned, a browser got a connection
|
|
// timeout and the reason was in `snap logs`, the last place somebody whose site is
|
|
// down thinks to look.
|
|
//
|
|
// That is #6592 exactly, one step earlier than where #6592 was fixed: the database
|
|
// WAIT already serves a "waiting for its database" page, but its machinery was
|
|
// defined 200 lines BELOW the comparison that needed it.
|
|
//
|
|
// It was also not a rare event. bin/database-autopick starts mongod to read
|
|
// MongoDB, and starting mongod stamps the files bin/ferretdb-migration-stale reads
|
|
// as evidence that MongoDB was written to - so the comparison ran at EVERY start
|
|
// and could never conclude (tests/autopickReadLeavesNoTrace.test.cjs is that half).
|
|
// Every restart took the site down for a comparison that had already been made.
|
|
//
|
|
// Three things have to hold, and this suite is about them:
|
|
// 1. the page functions are defined BEFORE the comparison uses them;
|
|
// 2. the comparison is BOUNDED, and a bound that runs out still starts WeKan;
|
|
// 3. the page is stopped before anything else binds the port.
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const ROOT = path.join(__dirname, '..');
|
|
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
|
|
const control = read('snap-src/bin/wekan-control');
|
|
const config = read('snap-src/bin/config');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
|
|
|
// The block that runs the comparison, from the autopick_rc that guards it to the
|
|
// end of that if.
|
|
const block = (() => {
|
|
const start = control.indexOf('autopick_rc=1');
|
|
assert.notStrictEqual(start, -1, 'wekan-control no longer runs database-autopick');
|
|
return control.slice(start, control.indexOf('if [ "$autopick_rc" -eq 0 ]', start));
|
|
})();
|
|
|
|
test('the waiting page is defined before the comparison that uses it', () => {
|
|
const defined = control.indexOf('start_db_wait_page() {');
|
|
const used = control.indexOf('bin/database-autopick');
|
|
assert.notStrictEqual(defined, -1, 'the page helper must exist');
|
|
assert.ok(defined < used,
|
|
'start_db_wait_page is defined after the comparison runs, so the comparison cannot '
|
|
+ 'serve a page and the site is simply down while it works');
|
|
});
|
|
|
|
test('the comparison serves that page rather than nothing', () => {
|
|
assert.ok(/start_db_wait_page /.test(block),
|
|
'nothing on the web port during a minutes-long comparison is the outage itself');
|
|
assert.ok(/stop_db_wait_page/.test(block),
|
|
'and it must be stopped, or it holds $PORT against WeKan');
|
|
assert.ok(block.indexOf('start_db_wait_page') < block.indexOf('stop_db_wait_page'),
|
|
'started before stopped');
|
|
});
|
|
|
|
test('the page waits out a grace period first (negative)', () => {
|
|
// A comparison that finishes in seconds should not flash a page up: that is
|
|
// noise on every restart, and it makes proxies cache a 503 for a healthy site.
|
|
assert.ok(/WEKAN_DB_WAIT_PAGE_SECONDS/.test(block),
|
|
'the comparison must use the same grace period as the database wait');
|
|
assert.ok(/kill -0 "\$_autopick_pid"/.test(block),
|
|
'which means watching the comparison rather than blocking on it');
|
|
});
|
|
|
|
test('the comparison is BOUNDED, and the bound is configurable', () => {
|
|
assert.ok(/WEKAN_AUTOPICK_TIMEOUT/.test(block), 'there must be a timeout');
|
|
assert.ok(/timeout "\$_autopick_timeout"/.test(block),
|
|
'and it must actually be applied to the command');
|
|
assert.ok(/command -v timeout/.test(block),
|
|
'with a fallback for a system without timeout(1), rather than failing to run at all');
|
|
});
|
|
|
|
test('a bound that runs out still starts WeKan, and says what to do', () => {
|
|
assert.ok(/_autopick_rc" -eq 124/.test(block),
|
|
"124 is timeout(1)'s exit code and has to be told apart from a real failure");
|
|
assert.ok(/NOTHING was changed or switched/.test(block),
|
|
'a stopped comparison must not leave the impression that it decided something');
|
|
for (const advice of ['database-compare', 'autopick-timeout=', 'autopick=false']) {
|
|
assert.ok(block.includes(advice),
|
|
`the message must name what to do next: ${advice}`);
|
|
}
|
|
});
|
|
|
|
test('the comparison can be switched off from snap config, not only the env', () => {
|
|
// WEKAN_AUTOPICK was an env var and nothing else, so an admin whose site was
|
|
// down had no supported way to skip the comparison.
|
|
assert.ok(/^keys="[^"]*\bWEKAN_AUTOPICK\b/m.test(config),
|
|
'WEKAN_AUTOPICK must be a registered snap config key');
|
|
assert.ok(/^KEY_WEKAN_AUTOPICK='autopick'$/m.test(config),
|
|
"and reachable as `snap set wekan autopick=false`");
|
|
assert.ok(/^DEFAULT_WEKAN_AUTOPICK="true"$/m.test(config),
|
|
'defaulting to on - it is worth doing, it just must not be able to hold the site down');
|
|
assert.ok(/^KEY_WEKAN_AUTOPICK_TIMEOUT='autopick-timeout'$/m.test(config),
|
|
'and the bound must be settable the same way');
|
|
const timeout = /^DEFAULT_WEKAN_AUTOPICK_TIMEOUT="(\d+)"$/m.exec(config);
|
|
assert.ok(timeout, 'the bound needs a default');
|
|
assert.ok(Number(timeout[1]) > 0 && Number(timeout[1]) <= 3600,
|
|
`a default bound of ${timeout[1]}s is not a bound worth having`);
|
|
});
|
|
|
|
test('every registered key has a description and a default (negative)', () => {
|
|
// wekan-read-settings exports ${DEFAULT_$key} for every name in `keys`, so a
|
|
// key registered without one silently exports the empty string.
|
|
const keys = /^keys="([^"]+)"/m.exec(config);
|
|
assert.ok(keys, 'config must declare keys');
|
|
for (const key of ['WEKAN_AUTOPICK', 'WEKAN_AUTOPICK_TIMEOUT']) {
|
|
assert.ok(keys[1].split(/\s+/).includes(key), `${key} must be in keys`);
|
|
for (const prefix of ['DESCRIPTION_', 'DEFAULT_', 'KEY_']) {
|
|
assert.ok(new RegExp(`^${prefix}${key}=`, 'm').test(config),
|
|
`${key} has no ${prefix}${key}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
console.log(`\nautopickDoesNotBlockStartup: ${passed} tests passed`);
|