Files
XC_VM/tests/Unit/DaemonLoopTest.php
T
obscuremind 5914d8a71a fix(cluster): keep the daemons in one process instead of re-execing each pass
queue, scanner, signals and watchdog each had their whole pass inside a while
whose last statement was an unconditional break, so every pass fell out of the
loop and restartDaemon() re-executed console.php: a fresh bootstrap, settings
read and database connect per pass, per node — four times a second in the
signals daemon. The plan's 24 h RSS and queries/s acceptance cannot be measured
on a process that never lives a second.

Every break that means something (a code change, nginx stopped, MAIN's
database gone) stays, and restartDaemon() still re-execs then, which is how a
deploy is picked up. Two things fell out of it: the signals daemon paced itself
inside the branch that read `signals`, so a node whose query failed spun as
fast as MariaDB would answer; and the watchdog's CPU sample was a delta over
its own 2 s sleep every pass, because a fresh process never had a previous
read of /proc/stat.

The numeric cluster settings render as number inputs with the bounds the save
clamps to, instead of text fields that no digits-only script covered.
2026-09-27 15:31:55 +00:00

58 lines
2.0 KiB
PHP

<?php
use PHPUnit\Framework\TestCase;
/**
* A daemon's loop must not end in an unconditional `break`.
*
* Four of them did (`queue`, `scanner`, `signals`, `watchdog`): the whole pass
* sat in a `while` whose last statement was `break`, so every pass fell out of
* the loop and `restartDaemon()` re-executed `console.php` — a fresh bootstrap,
* settings read and database connect per pass, per node, four times a second in
* the signals daemon's case. The plan's 24 h RSS and queries/s acceptance
* cannot be measured on a process that never lives that long.
*
* A `break` inside an `if` is how a daemon is meant to stop (a code change, an
* nginx restart, a dead database): only the unconditional one at the end of the
* body is refused.
*/
final class DaemonLoopTest extends TestCase {
public function testNoDaemonLoopEndsWithAnUnconditionalBreak(): void {
$rChecked = 0;
foreach (glob(dirname(__DIR__, 2) . '/src/Cli/Commands/*Command.php') ?: [] as $rFile) {
$rLines = file($rFile) ?: [];
if (!str_contains(implode('', $rLines), 'restartDaemon(')) {
continue;
}
foreach ($rLines as $rNo => $rLine) {
if (!preg_match('/^(\t+)(?:\} )?while \(/', $rLine, $rM)) {
continue;
}
$rChecked++;
$rEnd = self::closes($rLines, $rNo, strlen($rM[1]));
$rLast = $rEnd === null ? '' : trim($rLines[$rEnd - 1]);
$this->assertNotSame(
'break;',
$rLast,
basename($rFile) . ':' . $rEnd . ": the loop's last statement is an unconditional break, so the daemon re-executes itself every pass"
);
}
}
$this->assertGreaterThan(4, $rChecked, 'the daemon loops were not found at all');
}
/** The line (0-based) of the `}` that closes a block opened at $rNo, indented with $rTabs tabs. */
private static function closes(array $rLines, int $rNo, int $rTabs): ?int {
$rClose = str_repeat("\t", $rTabs) . '}';
for ($i = $rNo + 1; $i < count($rLines); $i++) {
if (str_starts_with($rLines[$i], $rClose)) {
return $i;
}
}
return null;
}
}