Skip to content

fix(daemon): bound the worker queue and assign only unassigned thresholds - #892

Merged
TheWitness merged 5 commits into
Cacti:developfrom
somethingwithproof:fix/daemon-queue-memory
Dec 3, 2026
Merged

TheWitness merged 5 commits into
Cacti:developfrom
somethingwithproof:fix/daemon-queue-memory

Conversation

@somethingwithproof

@somethingwithproof somethingwithproof commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Fixes #889
Fixes #890

Related: #891 (one queue row per threshold) needs a decision and is not part of this change.

Problem

With the daemon enabled and a large number of thresholds, a worker's memory grows with its backlog:

  • each cycle read every queued row for the thread in one query;
  • a threshold with several queued samples got one breach check per sample;
  • the delete at the end of the cycle matched rows by the threshold's current thread, so a rebalance during the cycle left rows behind for the next one.

The 10-second rebalance made this worse. COUNT(DISTINCT thread_id) counts thread 0, so a single new threshold reset and reassigned every threshold, which moved rows between threads in the middle of a worker's cycle.

Change

includes/functions.php gains small queue helpers that the worker and the daemon call:

  • thold_daemon_queue_batch_ids(): the next 1000 queued threshold ids for a thread, after a cursor.
  • thold_daemon_queue_samples(): the queued samples for those ids, oldest first.
  • thold_daemon_queue_checks(): one breach-check row per threshold, read from thold_data.
  • thold_daemon_queue_delete(): deletes the rows the batch read, by id and queue time rather than by thread.
  • thold_daemon_queue_purge_orphans(): deletes queued rows whose threshold no longer exists. thold_api_thold_remove() already clears its own rows, but other deletes of thold_data do not.
  • thold_prime_distribution(): moved from thold_daemon.php and rewritten.

thold_process.php walks its queue in those batches and writes each batch's samples in one transaction. Queue rows are deleted only after the commit, so a worker that dies mid-batch leaves them to be read again. thold_daemon.php calls the purge on each heartbeat.

Both heartbeat steps read before they write. The purge selects orphaned ids and deletes only those, and the assignment counts thresholds per thread and writes nothing when every threshold is already placed. An UPDATE/DELETE ... JOIN that scans the table locks every row it reads, and in an earlier version of this branch that blocked the workers' sample writes every 10 seconds.

How thold_prime_distribution() assigns threads now:

  • It assigns only thresholds with thread_id = 0, plus any on a thread above the configured process count.
  • A device's thresholds stay on one thread. A threshold on a device that already has a thread joins that thread, and a new device goes to the thread with the fewest thresholds.
  • A changed process count still reassigns everything, as before.

There are no schema changes and no new translatable strings.

Testing

  • Unit: tests/Unit/DaemonQueueTest.php adds 17 tests. Changed-line coverage is 100% (83/83) under tests/bin/patch-coverage.php. The two entry points remain on its allowlist.

  • Real database: tests/bin/daemon-queue-db-check.php runs the helpers against MariaDB, and CI runs it after the plugin install. It covers:

    • batch order;
    • the cutoff;
    • one check per threshold;
    • a delete that still removes rows after the threshold changed thread;
    • the orphan purge;
    • all three assignment cases.

    It passed 50 of 50 locally on MariaDB 11.8 with Cacti 1.2.x.

  • Real worker in CI: tests/bin/daemon-worker-check.php seeds 1,200 thresholds with three samples each (two batches), starts the real thold_process.php --thread=1, and checks three things: the queue drains, every threshold stores its newest sample, and none is left waiting for a breach check. It passes locally in about 7 seconds, and passes again when rerun, which confirms its cleanup.

  • End to end: this ran the real thold_process.php --thread=1 against a seeded backlog, on develop and on this branch, each on a fresh install. Both drained the queue and stored the newest sample for every threshold. It also ran the real thold_daemon.php for 40 seconds with a third of the thresholds unassigned: no PHP errors, every threshold assigned, and no device split across threads.

  • Load: this ran on a DigitalOcean c-4 (4 dedicated vCPU Xeon Platinum 8358, 8 GB RAM, Ubuntu 24.04), with PHP 8.4 and MariaDB 11.8 in Docker. Each run used a fresh install, then seeded 20,000 thresholds on 200 devices with five queued samples each, 100,000 queue rows in all. It timed one worker on thread 1 until it went idle. The busy time and peak RSS were read from /proc, with no database queries during the run.

    Run Version Busy time Peak RSS Queue left Newest sample stored
    1 develop 275 s 1,056 MB 0 20,000 / 20,000
    1 this branch 93 s 62 MB 0 20,000 / 20,000
    2 develop 269 s 1,057 MB 0 20,000 / 20,000

    On develop the worker's memory grows with the backlog, about 10 KB per queued row. On this branch it stays flat at about 60 MB, because it holds at most one batch at a time. With a 500,000-row backlog (100,000 thresholds × 5), one run each, before the transaction commit:

    Version Busy time Peak RSS Queue left
    develop 1,346 s 5,240 MB 0
    this branch 501 s 63 MB 0

    Four workers. Same droplet and setup, with four workers draining at once and statistics analyzed on every seeded table before each run. Each run timed the drain until every worker was idle, and read the server's status counters before and after. Every run ended with an empty queue, the newest sample stored for every threshold, and no threshold left waiting for a breach check.

    Thresholds Version Drain Peak worker RSS Queries Row lock waits
    20,000 develop 140 s 297 MB 2.50 M 6
    20,000 this branch without the transaction (ce80676), 3 runs 44.2 / 44.9 / 46.8 s 61 MB 0.66 M 0–1
    20,000 this branch (58726af), 3 runs 38.1 / 38.1 / 38.4 s 61 MB 0.66 M 0–1
    20,000 first version of the heartbeat (d584964), heartbeat every 1 s 47.9 s 62 MB 0.66 M 329 (9.4 s)
    20,000 ce80676, heartbeat every 1 s 46.0 s 61 MB 0.66 M 0
    100,000 develop 704 s 1,311 MB 12.5 M 6
    100,000 ce80676 241 s 62 MB 3.30 M 0
    100,000 this branch (58726af) 206 s 61 MB 3.30 M 0

    Most of the queries on both versions are settings lookups made inside each breach check: 8.35 M of 12.5 M on develop at 100,000 thresholds. develop runs a breach check per queued sample and this branch one per threshold, which accounts for most of the difference. These counts are inflated on a fresh install, because core does not cache a setting that has no row yet.

    Heartbeat cost at 100,000 thresholds on 4 threads, median of 20 ticks with every threshold already placed:

    Step d584964 this branch
    thold_prime_distribution() 217 ms 35 ms
    thold_daemon_queue_purge_orphans() 378 ms 265 ms

    The purge still scans the queue's id index once per heartbeat, but it takes no locks unless there are orphans to delete.

    Query plans (MariaDB 11.8, ANALYZE): the batch, sample, check and delete queries all use the queue's id index or the thold_data primary key. The old per-cycle read scanned all of thold_data and joined every queued row for the thread. It took 0.38 s alone at 100,000 thresholds, and up to 98 s per call with four workers reading at once in a local run.

The full suite also has one failure, NotificationQueueClaimTest > default process identity probe is timezone independent. It fails the same way on unmodified develop in the same container, so it is not related to this change.

…olds

Workers read their queue in batches of 1000 ids, fold
each threshold's queued samples, run one breach check per threshold, and
delete exactly the rows they read by id and queue time, so a rebalance
during a cycle no longer strands rows. The daemon purges queued rows whose
threshold is gone.

Thread assignment now only touches unassigned thresholds and thresholds on
a thread above the process count. A device keeps its thread and a new
device goes to the least-loaded one; a changed process count still
reassigns everything.

tests/bin/daemon-queue-db-check.php runs the helpers against the CI
database after the plugin install.

Fixes Cacti#889
Fixes Cacti#890

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
thold_process.php cannot be loaded by the unit suite. The new check seeds
1,200 thresholds with three queued samples each, starts the worker on
thread 1, and checks that the queue drains, each threshold stores its
newest sample, and no breach check is left pending.

Refs Cacti#889

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
thold_prime_distribution() ran UPDATE ... WHERE thread_id > ? on every
heartbeat. thread_id has no index, so the statement scanned thold_data and
locked each row it read, holding up the workers' sample and tcheck updates.
It now reads the per-thread counts and returns when every threshold is on a
valid thread, writing only when there is something to assign.

thold_daemon_queue_purge_orphans() used DELETE ... LEFT JOIN over the whole
queue, which locked every queued row it scanned against the poller's inserts
and the workers' deletes. It now finds orphans with a plain read and deletes
them by id.

The worker check stops the worker with SIGKILL: its SIGTERM handler logs a
WARNING, which the CI log check rejects.

Refs Cacti#889, Cacti#890

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Each sample is an UPDATE of thold_data, and as an autocommit each one
waited for its own log flush. Commit once per batch instead. Queue rows
are deleted only after the commit, so a worker that dies mid-batch
leaves them to be read again.

With 20000 thresholds x 5 samples and 4 workers, the queue drained in
38.1-38.4 s instead of 44.2-46.8 s over three runs each.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
bmfmancini
bmfmancini previously approved these changes Dec 2, 2026
Comment thread includes/functions.php Resolved
Resolve the CI workflow conflict by keeping both daemon database checks:
the queue/worker check from this branch runs first, followed by the
graph-alert worker check added on develop (Cacti#894).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@TheWitness
TheWitness merged commit ff6812b into Cacti:develop Dec 3, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

4 participants