Skip to content

fix: move the remaining named locks onto the process registry - #503

Open
somethingwithproof wants to merge 21 commits into
Cacti:developfrom
somethingwithproof:fix/502-registry-locks
Open

somethingwithproof wants to merge 21 commits into
Cacti:developfrom
somethingwithproof:fix/502-registry-locks

Conversation

@somethingwithproof

@somethingwithproof somethingwithproof commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Problem

GET_LOCK() is granted per database server. On a Galera cluster that is not behind a single-writer route, two nodes can both grant the same named lock (test results are in #502). Six places in the plugin still rely on one: the pipeline health check, partition create and remove, threshold recovery delivery, rule snapshot publishing and default dashboard seeding.

Fix

  • syslog_task_lock() / syslog_task_unlock() register the task with Cacti's register_process(). From Cacti 1.2.32 (fix(poller): [1.2.x] let only one process register a task cacti#9156) the registry is InnoDB with a unique task key, so its insert admits one holder across the cluster.
  • Each site keeps its old wait (0, 5 or 10 seconds) and its old behaviour when busy (skip, or the same log line and return value), and releases in finally.
  • A holder is never signalled. register_process_start() is deliberately not used here: it treats a holder running a different script as dead, and it sends SIGTERM to a holder that outlives its timeout, which for the health check would be poller.php and for a web caller a web server worker.
  • Liveness rule for an expired entry. The four sites that run only from the command line pass $pid_is_local = true: the pipeline health check, partition create, partition remove and recovery delivery. Their task id is the local poller id, so the entry's pid belongs to this host. For them an entry past its 1200 or 300 second expiry is retired only if its pid is no longer running, probed the way core's cacti_process_registry_prune() does: a bare signal-0 probe, EPERM counted as running, and without the posix extension every holder counted as running. A slow holder, such as a recovery command blocked in exec(), is therefore never replaced, so pending recoveries are not delivered twice and a second partition ALTER cannot queue behind the first. Rule snapshot publishing and dashboard seeding can run in a web request, where a PHP-FPM worker's pid outlives the request, so they keep the expiry on age alone (300 seconds).
  • Partition ALTERs: the REORGANIZE and DROP PARTITION statements at the two partition sites now run with the session lock_wait_timeout set to 30 seconds and restored afterwards, the idiom cli/upgrade_partitioning.php uses. Before, they could wait up to the server's default of 86400 seconds while holding the task. When the session value cannot be read or set, the ALTER runs as before; when the lock is free nothing else changes. On MariaDB 11.8 a partition ALTER behind a held metadata lock failed after twice the setting, so the cap is about a minute.
  • On a Cacti without cacti_process_registry_ensure_table() (1.2.23 to 1.2.31, and develop until #9156 is ported) each site keeps its named lock, unchanged. compat stays at 1.2.23.
  • syslog_replace_data() takes the target collector's id, so pushes to different collectors do not block each other.
Site Registry task (syslog, ...) Wait Expiry Expired entry retired
syslog_pipeline_health_check() health, poller id none 300 s once its process has exited
syslog_partition_create() partition_create_<table>, poller id 10 s 1200 s once its process has exited
syslog_partition_remove() partition_remove_<table>, poller id 10 s 1200 s once its process has exited
syslog_threshold_deliver_recoveries() recovery, source poller id none 1200 s once its process has exited
syslog_replace_data() rules_<table>, target collector id 5 s 300 s on age
syslog_dashboard_seed_default() dashboard, poller id 5 s 300 s on age

Verification

Tests were written first at each site: the named-lock behaviour was pinned on the unchanged code (busy; released after success, an early failure return and an exception), then a failing test for the registry path, then the change. The review fixes followed the same order.

  • tests/Unit/TaskLockTest.php covers both paths at all six sites and the helper, including an expired entry whose process still runs (kept at the four command line sites, which skip or wait and report busy as before), an expired entry whose process has exited (retired), and age-based expiry at the two web sites. Waits run on an overridable clock, so the configured wait is asserted exactly without sleeping.
  • tests/Unit/PartitionLockWaitTest.php covers the 30 second cap and its restore around both partition ALTERs, a failed ALTER, a failed restore, and the unchanged path when the session value cannot be read or set.
  • tests/regression/task_lock_database_test.php runs core 1.2.x's own lib/poller.php against MariaDB 11.8: two processes on separate connections, 20 rounds of two simultaneous starts with exactly one winner each, a holder still running past its expiry while a second process arrives (no delivery by the second process), retirement of an expired entry whose process has exited, a release by another process leaving the holder's entry, an acquire whose insert after an expiry sweep affected no rows reporting busy, a live holder not being signalled, real-time waits, conversion of an old MEMORY table on first use, and the named-lock fallback without the new core. The last two new checks were confirmed to fail against the corresponding mutations (unlock without the pid; ignoring the insert result after a sweep).
  • Local: Pest 997 passed, 1 skipped on PHP 8.3 and 998 passed on 8.5; php -l clean on 8.2 for the changed files; changed-line coverage 100% (62/62); no translatable string changes; the task lock regression script passes (47 registry checks, 6 named-lock checks), and the seven existing MariaDB regression scripts pass.
  • Not tested on a Galera cluster, and Pest was not run locally on PHP 8.2.

Notes for review

  • Scope: registry rows live in the Cacti database of the process that takes the lock. Two Cacti installs that process into one shared Syslog database were excluded from each other by the server-wide named lock and, on 1.2.32 and later, no longer are. That is the same scope as the master task since fix(process): coordinate Syslog processing through the process registry instead of GET_LOCK (#479) #495, and the documented layout is one processing host per Syslog database. If that layout needs to stay supported, say so and I will keep the named lock as a second guard at the two partition sites.
  • A process that exits from a signal handler or dies on a fatal error does not run finally, so its row stays until core's maintenance prune removes it, normally within one poller cycle. A web request that dies holds its task for the 300 second expiry, and a dashboard seed skipped that way is not retried until the next install or upgrade.
  • On Galera, two simultaneous inserts on different nodes should fail on one side with a deadlock error and be treated as busy; that path may log a database error and is untested.
  • SonarCloud's security rating on this PR comes from sha1/md5 used to build lock names and from SQL built with random database names in the regression script, not from request data.
  • An independent stress run (36 processes across three locks, 700 rounds, about 17,500 holds, mixed waits of 0, 5 and 10 seconds, including the MEMORY to InnoDB conversion race) found no exclusion violation, and 200 rounds of ten processes racing one expired row had exactly one winner each.
  • The contended unit tests now take about 0.1 seconds; the whole unit suite went from about 35 seconds to about 3 seconds on PHP 8.3.
  • The new regression script is not yet called from the workflow; it needs one line in the regression step, which I left for a decision here.

Refs #502. Still open there: porting Cacti/cacti#9156 to develop (this change picks it up automatically), and reading worker-tracking state from the writer (item 3 of the proposal).

Each site is run while another process holds its lock, and to success,
an early failure return and an exception, so a change of lock mechanism
for Cacti#502 can be checked against the current behaviour: the wait each
lock uses, what the caller returns and logs when it is busy, and that
the lock is released on every exit path.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Refs Cacti#502. GET_LOCK() is granted per database server, so two Galera nodes
can both hand out the same lock. syslog_task_lock() registers the task
through register_process(), whose insert admits one holder once Cacti
1.2.32 keeps the registry in InnoDB with a unique task key. It waits up
to a given number of seconds, retires an entry older than its timeout,
and never signals a holder. On a Cacti without that registry it returns
null so the caller keeps its named lock.

The unit bootstrap stubs the three core registry functions; the registry
reports itself unavailable unless a test opts in.

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

Refs Cacti#502. The per-collector health check tries once for task
('syslog', 'health', <poller id>) and skips the run when another process
holds it, as it did with GET_LOCK(..., 0). An entry left by a holder that
died expires after 300 seconds; the check itself makes a few queries and
sends at most one mail. Cacti before 1.2.32 keeps the named lock.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Refs Cacti#502. syslog_partition_create() now holds task
('syslog', 'partition_create_<table>', <poller id>). It waits up to 10
seconds for another holder, as GET_LOCK(..., 10) did, and logs the same
'Unable to acquire' line when it gives up. Partition maintenance runs
inside the processing master, whose own registration expires after 1200
seconds, so an abandoned entry expires after 1200 seconds too; until
then writes stay in dMaxValue. Cacti before 1.2.32 keeps the named lock.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Refs Cacti#502. syslog_partition_remove() now holds task
('syslog', 'partition_remove_<table>', <poller id>), waits up to 10
seconds as before, and returns 0 with the same log line when another
process holds it. An abandoned entry expires after 1200 seconds, which
delays retention pruning by at most that long. Cacti before 1.2.32 keeps
the named lock.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Refs Cacti#502. After retiring an expired entry the task lock retried at
once, without a bound, for as long as the sweep reported a removed row.
It now makes one immediate retry per sweep and otherwise waits for the
deadline. The lock tests also reset their overrides afterwards, so the
registry they enable no longer reaches tests in later files.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Refs Cacti#502. syslog_threshold_deliver_recoveries() tries once for task
('syslog', 'recovery', <source poller id>) and skips delivery when another
process holds it, as with GET_LOCK(..., 0). Recovery commands run through
exec() with no time limit inside the processing master, which the next
master replaces after 1200 seconds, so an abandoned entry expires after
1200 seconds. Cacti before 1.2.32 keeps the named lock.

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

Refs Cacti#502. syslog_replace_data() holds task
('syslog', 'rules_<table>', <collector>) where the collector owns the
target database: the remote collector for syslog_replicate_out(), this
collector for syslog_replicate_in(). Publishing to two collectors at once
stays independent, as it was with a lock on each target server. It waits
up to 5 seconds as before and returns false when another process holds
the task. Copying a rule table takes seconds, so an abandoned entry
expires after 300 seconds. Cacti before 1.2.32 keeps the named lock on
the target connection.

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

Refs Cacti#502. syslog_dashboard_seed_default() holds task
('syslog', 'dashboard', <poller id>), waits up to 5 seconds as before,
and leaves seeding to the holder when it cannot get the task. Seeding
inserts six rows; an entry left by a web request that ended without
releasing it stays until it expires after 300 seconds, because the web
server process keeps running. Cacti before 1.2.32 keeps the named lock.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Refs Cacti#502. With Cacti's lib/poller.php the script starts from the
MEMORY processes table older Cacti created and checks that the first lock
has Cacti convert it, that a second process on another connection is
refused or waits, that recovery delivery is skipped while another process
holds its task and runs after release, that a live holder is not
signalled, that only an entry past its timeout is retired, and that one
of two callers starting together wins in each of 20 rounds. Run with the
argument 'main named', it checks that without that core the named lock
still decides.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Comment thread includes/functions.php Fixed
Comment thread tests/Unit/TaskLockTest.php Fixed
Comment thread tests/Unit/TaskLockTest.php Fixed
Comment thread tests/Unit/TaskLockTest.php Fixed
The contended unit tests slept through every configured wait, about 32
seconds in all. syslog_task_lock() now pauses and reads the time through
syslog_task_lock_clock(), or through a callable in
$GLOBALS['syslog_task_lock_clock'] when one is set, so the unit tests
assert the exact wait on a fake clock without sleeping. The real-time
wait stays covered by the MariaDB regression script.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The task lock retired a registry entry once started + timeout had passed,
whether or not its process was still running. A recovery command that
blocked in exec() past 1200 seconds let the next processing run take the
task and deliver the same pending recoveries a second time, and a second
partition ALTER could queue behind the first and fail with a duplicate
partition name.

syslog_task_lock() takes a $pid_is_local flag. With it, an expired entry
is retired only if its pid is no longer running on this host, probed as
cacti_process_registry_prune() does: a bare signal-0 probe, EPERM counted
as running, and without posix_kill() every holder counted as running. The
pipeline health check, partition create and remove, and recovery delivery
pass it: they run only from the command line and their task id is the
local poller id, so the entry's pid belongs to this host. Rule snapshot
publishing and dashboard seeding can run in a web request, whose PHP-FPM
worker pid outlives the request, so they keep the expiry on age alone.

The comment that said the processing master is replaced after 1200
seconds is corrected.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The MariaDB regression script passed with two mutations of the task lock:
a release that drops the pid and so deletes another process's entry, and
an acquire that reports success after an expiry sweep although its own
insert affected no rows. It now checks that a release by another process
leaves the holder's entry and the task held, and, through a hook that runs
another process's insert right after the sweep, that such an acquire
reports busy with and without the process check.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The REORGANIZE and DROP PARTITION statements in syslog_partition_create()
and syslog_partition_remove() waited for a metadata lock up to the
server's lock_wait_timeout, 86400 seconds by default on MariaDB, while
holding the partition task. syslog_partition_alter() sets the session
value to 30 seconds around each ALTER and restores it afterwards, the
idiom cli/upgrade_partitioning.php uses for its own DDL. A failed restore
is logged. When the session value cannot be read or set, the ALTER runs
as before, and when the lock is free nothing else changes.

On MariaDB 11.8 a partition ALTER behind a held metadata lock failed
after twice the setting, so the cap is about a minute.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The health, recovery and dashboard lock names were rebuilt in the test with
the same hash calls as the code under test, so a change to how a name is
derived could pass. Older plugin versions take these exact names; spell them
out byte for byte instead.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The regression script spliced its database name into CREATE DATABASE, USE
and DROP DATABASE, and the holder pid into a COUNT query. A name cannot be
bound, so run those statements through one helper that accepts only
[A-Za-z0-9_] and backticks the name, and bind the COUNT query's values. The
child takes its database name from argv, so the check is not only cosmetic.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The named lock for the pipeline health check is unchanged, so its line
no longer shows as modified. Behaviour and the lock name are identical.

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

Mutation testing of the task lock helpers left three changes unnoticed by
the suite: syslog_task_lock_clock() without its pause or returning whole
seconds, the replacement registry entry taken after an expired holder was
retired carrying the wait in place of its timeout, and the TO_DAYS
REORGANIZE running without the capped lock wait.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The regression script rebuilt the named lock's name with its own hash
call. It now asks the database for the same value, so the script has no
hash call for the static analysis to report.

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

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants