mirror of
https://github.com/Cacti/cacti.git
synced 2026-11-30 12:16:40 +09:00
* Stop Boost retries at failed writes and metadata reads Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Retain Boost archives when temporary copies fail Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Reuse Boost cleanup after failed temporary copies Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Acknowledge exact Boost keys without deleting late arrivals Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): cover native acknowledgement and archive handoff Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): match acknowledged keys by epoch, not session-local time Staging fetched keys with FROM_UNIXTIME() converts each epoch through the session time zone. On MySQL in a DST zone both instants of the fall-back hour render as the same local time and are stored back as the first one, so INSERT IGNORE collapses them and the second row is never deleted. It is then fetched, skipped by --skip-past-updates and retained on every run. Stage the epoch integers and compare them with UNIX_TIMESTAMP(time), which reads the TIMESTAMP column's stored value without a time zone conversion. Verified on MySQL 8.4 and MariaDB 11.8 with the session in America/New_York. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): stop bulk drain writes after a rejected flush The bulk drain kept writing a data source's later timestamps after one of its mid-stream flushes failed. That advanced the RRD past the rejected samples, so --skip-past-updates dropped them when the retained shard was retried. Skip the remaining rows of that data source instead. Match the on-demand update result exactly against OK. An error that only contains the letters, for example in an RRD file name such as BOOK_7.rrd, was counted as success and its rows were acknowledged without being written. Describe the acknowledgement DELETE's index use accurately and note why the fall-back fixture shifts only int parameters. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): publish bulk drain probe evidence to the changed-line gate The bulk drain changes in poller_boost.php are exercised only by BoostBulkDrainFailureProbe.php, which compiles the drain in a child process from a temporary file. Xdebug credited those hits to the temporary file, so the changed-line gate saw none of the drain's nine changed statements and the pull request measured 66.7 percent. Keep each loaded function at its original line numbers and map the copy back to its source file, let the drain probe publish request coverage, and run both drain scenarios from a request-coverage fixture whenever poller_boost.php or the probe changes. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): isolate a failing data source instead of its whole shard One data source whose RRD update always fails, for example a corrupt RRD, an unwritable file or a template mismatch, made its pass return -1. The child abandoned its remaining passes and the parent kept every archive table. Each later run added another archive table, re-read all of them, and failed at the same data source again, so data sources after it in that child were never written and work and disk use grew without bound. Park the data sources whose update was rejected on process handler 0 and acknowledge the rest of the shard, so the child carries on. Once every child has finished, the parent copies the parked data sources' samples into poller_output_boost and drops the archive tables as usual. Samples older than one day are discarded with a warning, so a data source that never succeeds cannot grow the queue forever. If parking or the copy fails, the shard or the archive tables are retained as before. Database and metadata failures still fail the whole shard. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): expect a rejected flush to park the data source and acknowledge the shard A rejected mid-stream flush still stops further writes to its data source so a later timestamp cannot advance the RRD past the rejected samples. The data source is now parked for the parent to carry forward instead of retaining the whole shard, so the bulk drain probe expects the shard to be acknowledged. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): run the Boost probes in the changed-line coverage gate Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
188 lines
7.7 KiB
PHP
188 lines
7.7 KiB
PHP
<?php
|
|
// Copyright (C) 2026 The Cacti Group. Licensed under GPL-2.0-or-later.
|
|
require_once dirname(__DIR__, 3) . '/Helpers/FakeMySQLPDO.php';
|
|
require_once dirname(__DIR__, 4) . '/lib/boost.php';
|
|
|
|
/**
|
|
* Statement that applies MySQL's session time zone to FROM_UNIXTIME() arguments.
|
|
*
|
|
* With time_zone set to America/New_York, MySQL renders both instants of the
|
|
* 2026-11-01 fall-back hour as the same local time and stores that local time
|
|
* back into a TIMESTAMP as the first (EDT) instant. SQLite has no session time
|
|
* zone, so the lossy round trip is modeled here on the bound epoch values.
|
|
*
|
|
* Only int parameters are shifted, and the fall-back test binds ints on
|
|
* purpose. Production rows carry UNIX_TIMESTAMP() back from PDO as strings, so
|
|
* changing the test keys to strings would stop the fixture from shifting and
|
|
* hide a reintroduced FROM_UNIXTIME(?) instead of catching it.
|
|
*/
|
|
class BoostAcknowledgementStatement extends PDOStatement {
|
|
/** First second of the repeated (EST) 01:00 hour on 2026-11-01. */
|
|
public const FALL_BACK_START = 1793512800;
|
|
|
|
protected function __construct(private bool $from_unixtime) {
|
|
}
|
|
|
|
/**
|
|
* Execute after collapsing repeated-hour epochs the way MySQL does.
|
|
*
|
|
* @param array<int|string, mixed>|null $params the bound values
|
|
*
|
|
* @return bool whether the statement executed
|
|
*/
|
|
public function execute(?array $params = null): bool {
|
|
if ($this->from_unixtime && $params !== null) {
|
|
foreach ($params as $index => $value) {
|
|
if (is_int($value) && $value >= self::FALL_BACK_START && $value < self::FALL_BACK_START + 3600) {
|
|
$params[$index] = $value - 3600;
|
|
}
|
|
}
|
|
}
|
|
|
|
return parent::execute($params);
|
|
}
|
|
}
|
|
|
|
class BoostAcknowledgementPDO extends FakeMySQLPDO {
|
|
public array $batch_sizes = [];
|
|
public string $reject = '';
|
|
|
|
public function prepare(string $query, array $options = []): PDOStatement|false {
|
|
if (str_starts_with($query, 'INSERT IGNORE INTO `poller_output_boost_acked_')) {
|
|
$this->batch_sizes[] = substr_count($query, '?') / 2;
|
|
}
|
|
|
|
if ($this->reject !== '' && str_contains($query, $this->reject)) {
|
|
// Prepares cleanly and fails on execution, like a server-side rejection.
|
|
$query = "INSERT INTO poller_output_boost VALUES (1, 'in', '2023-11-14 22:13:20')";
|
|
}
|
|
|
|
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [BoostAcknowledgementStatement::class, [str_contains($query, 'FROM_UNIXTIME(?)')]]);
|
|
|
|
return parent::prepare(str_replace(['FROM_UNIXTIME(?)', 'DROP TEMPORARY TABLE'], ["datetime(?, 'unixepoch')", 'DROP TABLE'], $query), $options);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Point the default database session at a fake connection for one test.
|
|
*
|
|
* @param BoostAcknowledgementPDO $connection the connection to use
|
|
*
|
|
* @return callable restores the previous session
|
|
*/
|
|
function boost_acknowledgement_use(BoostAcknowledgementPDO $connection): callable {
|
|
global $database_sessions, $database_hostname, $database_port, $database_default;
|
|
|
|
$key = "$database_hostname:$database_port:$database_default";
|
|
$previous = $database_sessions[$key] ?? null;
|
|
$database_sessions[$key] = $connection;
|
|
|
|
return function () use ($key, $previous) {
|
|
global $database_sessions;
|
|
|
|
if ($previous === null) {
|
|
unset($database_sessions[$key]);
|
|
} else {
|
|
$database_sessions[$key] = $previous;
|
|
}
|
|
};
|
|
}
|
|
|
|
test('Boost acknowledgement removes only the fetched keys in one statement', function (bool $failure) {
|
|
global $database_sessions, $database_hostname, $database_port, $database_default;
|
|
|
|
$connection = new BoostAcknowledgementPDO();
|
|
$connection->exec('CREATE TABLE poller_output_boost (local_data_id INTEGER, rrd_name TEXT, time TEXT, PRIMARY KEY (local_data_id, rrd_name, time))');
|
|
$insert = $connection->prepare('INSERT INTO poller_output_boost VALUES (?, ?, ?)');
|
|
$rows = [];
|
|
|
|
for ($index = 0; $index < 999; $index++) {
|
|
$timestamp = 1700000000 + $index;
|
|
$rows[] = ['timestamp' => $timestamp, 'rrd_name' => 'in'];
|
|
$insert->execute([1, 'in', gmdate('Y-m-d H:i:s', $timestamp)]);
|
|
}
|
|
|
|
foreach (['in', 'out'] as $name) {
|
|
$rows[] = ['timestamp' => 1700000999, 'rrd_name' => $name];
|
|
$insert->execute([1, $name, gmdate('Y-m-d H:i:s', 1700000999)]);
|
|
}
|
|
|
|
$insert->execute([1, 'late', gmdate('Y-m-d H:i:s', 1700001000)]);
|
|
$insert->execute([2, 'in', gmdate('Y-m-d H:i:s', 1700000000)]);
|
|
|
|
if ($failure) {
|
|
$connection->exec("CREATE TRIGGER reject_acknowledgement BEFORE DELETE ON poller_output_boost WHEN OLD.time = '" . gmdate('Y-m-d H:i:s', 1700000999) . "' BEGIN SELECT RAISE(ABORT, 'Injected acknowledgement failure.'); END");
|
|
}
|
|
$key = "$database_hostname:$database_port:$database_default";
|
|
$previous = $database_sessions[$key] ?? null;
|
|
$database_sessions[$key] = $connection;
|
|
|
|
try {
|
|
expect(boost_delete_output_keys(1, $rows))->toBe(!$failure);
|
|
expect($connection->batch_sizes)->toBe([500, 500, 1]);
|
|
$remaining = $connection->query('SELECT * FROM poller_output_boost ORDER BY local_data_id, time, rrd_name')->fetchAll();
|
|
// A rejected delete is atomic: every fetched row stays in poller_output_boost for the next run.
|
|
expect($remaining)->toHaveCount($failure ? 1003 : 2);
|
|
expect(array_column(array_slice($remaining, -2), 'rrd_name'))->toBe(['late', 'in']);
|
|
} finally {
|
|
if ($previous === null) {
|
|
unset($database_sessions[$key]);
|
|
} else {
|
|
$database_sessions[$key] = $previous;
|
|
}
|
|
}
|
|
})->with([false, true]);
|
|
|
|
test('Boost acknowledgement accepts an empty fetched batch', function () {
|
|
expect(boost_delete_output_keys(1, []))->toBeTrue();
|
|
});
|
|
|
|
test('Boost acknowledgement keeps every row when staging the keys fails', function (string $statement) {
|
|
global $database_sessions, $database_hostname, $database_port, $database_default;
|
|
|
|
$connection = new BoostAcknowledgementPDO();
|
|
$connection->exec('CREATE TABLE poller_output_boost (local_data_id INTEGER, rrd_name TEXT, time TEXT, PRIMARY KEY (local_data_id, rrd_name, time))');
|
|
$connection->exec("INSERT INTO poller_output_boost VALUES (1, 'in', '2023-11-14 22:13:20')");
|
|
$connection->reject = $statement;
|
|
$key = "$database_hostname:$database_port:$database_default";
|
|
$previous = $database_sessions[$key] ?? null;
|
|
$database_sessions[$key] = $connection;
|
|
|
|
try {
|
|
expect(boost_delete_output_keys(1, [['timestamp' => 1700000000, 'rrd_name' => 'in']]))->toBeFalse();
|
|
expect($connection->query('SELECT COUNT(*) FROM poller_output_boost')->fetchColumn())->toBe(1);
|
|
} finally {
|
|
if ($previous === null) {
|
|
unset($database_sessions[$key]);
|
|
} else {
|
|
$database_sessions[$key] = $previous;
|
|
}
|
|
}
|
|
})->with(['CREATE TEMPORARY TABLE', 'INSERT IGNORE INTO `poller_output_boost_acked_']);
|
|
|
|
test('Boost acknowledgement deletes both instants of a DST fall-back hour', function () {
|
|
$connection = new BoostAcknowledgementPDO();
|
|
$connection->exec('CREATE TABLE poller_output_boost (local_data_id INTEGER, rrd_name TEXT, time TEXT, PRIMARY KEY (local_data_id, rrd_name, time))');
|
|
$insert = $connection->prepare('INSERT INTO poller_output_boost VALUES (?, ?, ?)');
|
|
|
|
// 01:30 EDT and 01:30 EST share a local time but are an hour apart in UTC.
|
|
$edt = BoostAcknowledgementStatement::FALL_BACK_START - 1800;
|
|
$est = BoostAcknowledgementStatement::FALL_BACK_START + 1800;
|
|
$late = BoostAcknowledgementStatement::FALL_BACK_START + 5400;
|
|
|
|
foreach ([$edt, $est, $late] as $timestamp) {
|
|
$insert->execute([1, 'in', gmdate('Y-m-d H:i:s', $timestamp)]);
|
|
}
|
|
|
|
$restore = boost_acknowledgement_use($connection);
|
|
|
|
try {
|
|
$fetched = [['timestamp' => $edt, 'rrd_name' => 'in'], ['timestamp' => $est, 'rrd_name' => 'in']];
|
|
|
|
expect(boost_delete_output_keys(1, $fetched))->toBeTrue();
|
|
expect($connection->query('SELECT time FROM poller_output_boost')->fetchAll(PDO::FETCH_COLUMN))->toBe([gmdate('Y-m-d H:i:s', $late)]);
|
|
} finally {
|
|
$restore();
|
|
}
|
|
});
|