Repository navigation
fix(daemon): bound the worker queue and assign only unassigned thresholds - #892
Merged
TheWitness merged 5 commits intoDec 3, 2026
Merged
Conversation
…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
previously approved these changes
Dec 2, 2026
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>
|
xmacan
approved these changes
Dec 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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:
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.phpgains 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 fromthold_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 ofthold_datado not.thold_prime_distribution(): moved fromthold_daemon.phpand rewritten.thold_process.phpwalks 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.phpcalls 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 ... JOINthat 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:thread_id = 0, plus any on a thread above the configured process count.There are no schema changes and no new translatable strings.
Testing
Unit:
tests/Unit/DaemonQueueTest.phpadds 17 tests. Changed-line coverage is 100% (83/83) undertests/bin/patch-coverage.php. The two entry points remain on its allowlist.Real database:
tests/bin/daemon-queue-db-check.phpruns the helpers against MariaDB, and CI runs it after the plugin install. It covers:It passed 50 of 50 locally on MariaDB 11.8 with Cacti 1.2.x.
Real worker in CI:
tests/bin/daemon-worker-check.phpseeds 1,200 thresholds with three samples each (two batches), starts the realthold_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=1against a seeded backlog, ondevelopand on this branch, each on a fresh install. Both drained the queue and stored the newest sample for every threshold. It also ran the realthold_daemon.phpfor 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.developdevelopOn
developthe 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:developFour 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.
developdevelopMost of the queries on both versions are settings lookups made inside each breach check: 8.35 M of 12.5 M on
developat 100,000 thresholds.developruns 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:
thold_prime_distribution()thold_daemon_queue_purge_orphans()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'sidindex or thethold_dataprimary key. The old per-cycle read scanned all ofthold_dataand 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 unmodifieddevelopin the same container, so it is not related to this change.