Files
cacti/tests/Unit/Core/Boost/BoostArchiveDropGateTest.php
Thomas VincentandTheWitness 3c20f4ce7e Harden Boost data handoffs and graph cache writes (#7729)
* fix(boost): harden data handoffs and cache writes

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* Define $updates_ok when there is nothing to flush

PHPStan level 6 also flags the poller ownership check, which is the POLLER_ID
constant case the file already annotates above.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* Use a line comment to match the fixer

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* Record the boost process columns in the audit schema

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: resolve includes through Cacti path constants

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* Fix Boost handoff review concerns

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: seed settings in Boost database fixture

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: refresh SQL interpolation baseline

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs: record Boost handoff hardening

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: stop boost discarding a cycle and wedging a shard

Three defects in the handoff hardening.

A rejected ownership handoff returned false, which the poller reads as boost
having consumed the data, so it skipped the RRD write. The poller_output rows
were already deleted by then, so a stale poller_item copy on a remote poller
silently lost the whole cycle. Return true instead and let the poller write
directly, matching the database failure path.

Any non-OK RRDtool response retained the shard, and the retry then replayed rows
already written, which RRDtool rejects as an illegal update time. That failed
again on every pass, so the archive tables accumulated and boost never drained.
Treat an at-or-behind timestamp as applied, since the sample is in the RRD;
genuine errors still retain the shard.

The two new poller_output_boost_processes columns passed 'null' where
db_install_update_table reads 'NULL', so NOT NULL was never emitted and upgraded
installs diverged from cacti.sql.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: spell the NULL column key so upgrades emit NOT NULL

db_add_column() reads $column['NULL'] and PHP array keys are case sensitive, so
the thirteen definitions in the 1.3.0 upgrade that passed a lowercase 'null' key
had the flag discarded and created nullable columns. Fresh installs built from
cacti.sql got NOT NULL, upgraded installs did not, and Cacti's own schema audit
reports the difference.

The same file already spells the key in upper case 144 times, and the wrapper's
own docblock example uses the upper case form.

Guard the class with a test over every upgrade file rather than only the one
that was wrong.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: assert the NULL key plainly

toContain() takes needles rather than a failure message.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): close remaining handoff review gaps

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): guard nullable graph output

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): keep in-flight completion rows through the 1.3.0 upgrade

The truncate only existed so the run_child UNIQUE key could build over rows
that predate run_id; deleting just those rows leaves a running boost its
completion records.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* refactor(boost): drop the poller ownership gate

Its boost_poller_on_demand() call sits behind the boost_redirect early return
that poller.php forces on for every multi-collector install, and its recovery
call turns a device deletion on main into a recovery that never brings the
collector back online.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): bound atomic cache write allocations

Guard upgrade source reads so failures report clearly, write graph cache data in fixed-size chunks, and exercise a multi-chunk payload.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): support Windows cache replacement

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(boost): expand handoff failure coverage

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(boost): support ANSI_QUOTES upgrade checks

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): stop staging after chunk failure

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(recovery): use numeric poller status

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(sql): support ANSI_QUOTES in recovery paths

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): preserve cache on replacement failure

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): retain readable cache permissions

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): repair completion schema at runtime

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(recovery): bound timestamp batch fetches

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): satisfy runtime schema analysis

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(recovery): restore status after transfer failure

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(boost): rely on unit bootstrap helpers

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): refresh generated cache secret

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix(boost): normalize cache path separators

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(boost): fail clearly on unreadable fixtures

* fix(boost): name ignored duplicate sample keys

The staging warning only said rows were dropped, so a retry could not be told from a colliding writer.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* Lack of pre-commit hook execution allowed lint failure

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Co-authored-by: TheWitness <thewitness@cacti.net>
2026-09-12 20:17:35 -04:00

75 lines
3.6 KiB
PHP

<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
/*
* Regression test for #7520: the archive-table DROP in poller_boost.php was
* gated on `$rrd_updates > 0`, an aggregate SUM(status) over whatever
* completion rows landed. If 3 of 4 shards succeeded and the 4th child
* crashed before writing a completion row, the sum from the 3 successful
* shards was still positive and every archive table for the run -- including
* the crashed child's entire unprocessed shard -- got dropped.
*
* The drain loop a few lines above already computes the correct signal
* (boost_completed_children() < $expected_children) to detect a crashed
* child; the fix wires that same completeness check into the drop decision.
*/
$boostPollerPath = CACTI_PATH_BASE . '/poller_boost.php';
test('archive-table drop is gated on shard completeness, not the status sum', function () use ($boostPollerPath) {
$contents = file_get_contents($boostPollerPath);
$rrd_updates_pos = strpos($contents, 'COALESCE(SUM(CAST(status AS SIGNED)), 0)');
expect($rrd_updates_pos)->not->toBeFalse();
$drop_pos = strpos($contents, 'DROP TABLE IF EXISTS `$table`', $rrd_updates_pos);
expect($drop_pos)->not->toBeFalse();
$segment = substr($contents, $rrd_updates_pos, $drop_pos - $rrd_updates_pos);
// The completeness check must appear between the SUM() read and the
// actual DROP, gating it.
expect($segment)->toContain('boost_completed_children($run_id) >= $expected_children');
expect($segment)->toContain('$failed_children === 0');
// A bare "if ($rrd_updates > 0) { ... DROP ... }" with nothing else
// between them is exactly the bug: the drop must not be reachable
// without the completeness check in between.
$gate_pos = strpos($segment, 'boost_completed_children($run_id) >= $expected_children');
expect($gate_pos)->toBeGreaterThan(0);
});
test('partial-completion path logs a warning instead of silently keeping the tables', function () use ($boostPollerPath) {
$contents = file_get_contents($boostPollerPath);
$drop_pos = strpos($contents, 'DROP TABLE IF EXISTS `$table`');
expect($drop_pos)->not->toBeFalse();
// Search a window around the drop for the else branch that must log when
// completeness is not met.
$window = substr($contents, max(0, $drop_pos - 200), 1200);
expect($window)->toContain('leaving archive tables in place for the next run to pick up');
});
test('boost_completed_children and expected_children are still computed the same way the drain loop uses', function () use ($boostPollerPath) {
$contents = file_get_contents($boostPollerPath);
// The drain loop's own completeness check must still be present and
// unchanged; the drop-gate fix reuses these exact symbols rather than
// inventing a parallel accounting mechanism that could drift from it.
expect($contents)->toContain('boost_completed_children($run_id) < $expected_children');
expect(substr_count($contents, '$expected_children'))->toBeGreaterThanOrEqual(4);
});