Skip to content

fix(mactrack): restore the unique aggregated ports key on upgrade - #418

Merged
TheWitness merged 3 commits into
developfrom
fix/aggregated-ports-unique-key
Nov 12, 2026
Merged

TheWitness merged 3 commits into
developfrom
fix/aggregated-ports-unique-key

Conversation

@somethingwithproof

Copy link
Copy Markdown
Member

Upgrading an install whose mac_track_aggregated_ports held duplicate rows failed with Duplicate entry ... for key 'port_number' on every page load. This replaces #417.

How the duplicates got there: the 4.9 upgrade block recreated the port_number key with ADD INDEX (non-unique). Its guard, $keys != 7, compares an array with an int, so it is always true and the block ran on every upgrade. With a non-unique key, the poller's ON DUPLICATE KEY UPDATE count_rec=count_rec+1 never matches, so each scan inserted a new row. Cacti 1.2.x now refreshes existing plugin tables during install, and that refresh fails when it tries to put the unique key back.

Changes:

  • Merge each duplicate group into its newest row (summed count_rec, earliest first_scan_date, latest date_last) in one transaction, then rebuild port_number as UNIQUE. This runs before plugin_mactrack_install() refreshes the table, and also without a version change. It handles a table whose index was dropped by hand.
  • Run the MAC separator migration once, gated on a setting instead of the index comparison.
  • Take an advisory lock around the version upgrade so concurrent page loads don't run it in parallel. That race caused the Can't DROP INDEX port_number and Duplicate key name 'site_id_device_id' errors.
  • Widen row_id to bigint(20) unsigned, in the fresh schema and on upgrade.
  • Bump the version to 4.11.

Verification:

  • Pest suite: 306 passed, 100% patch coverage.

  • Upgrade scenarios on Cacti 1.2.x with MariaDB 10.11 and MySQL 8.4:

    • duplicates left by the 4.9 block
    • a failed refresh that already recorded 4.10
    • pre-4.9 MACs that collide once separators are stripped
    • a fresh install followed by an upgrade
    • the index dropped by hand

    Each ends with one row per key and the unique index restored, and the next poller pass increments count_rec instead of adding a row.

  • Two concurrent index.php loads against 320k duplicate rows over 20k keys finished in about 9 seconds with no errors and the count_rec total preserved.

Fixes #359

The 4.9 upgrade dropped the mac_track_aggregated_ports port_number key
and recreated it as a plain index. Its guard compared the SHOW INDEXES
result array with 7, which is always unequal, so this ran on every
upgrade. With a plain index the poller's ON DUPLICATE KEY UPDATE never
matches and each pass inserts a new row. Since Cacti refreshes existing
plugin tables during install, the refresh now tries to restore the
UNIQUE key and fails with "Duplicate entry ... for key 'port_number'".

Merge each duplicate group into its newest row (summed count_rec,
earliest first_scan_date, latest date_last) in one transaction, then
rebuild the key as UNIQUE. mactrack_check_upgrade() runs this before
plugin_mactrack_install() refreshes the table, and also without a
version change, because installs whose failed refresh already recorded
the new version never re-enter the upgrade. Gate the separator
migration on a one-time setting instead of the index comparison.

Fixes #359

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
mactrack_check_upgrade() runs on every index.php, plugins.php and
mactrack_devices.php load until the new version is recorded at the end,
so concurrent page loads run the whole upgrade in parallel. They race on
the same DDL and log errors such as "Can't DROP INDEX `port_number`",
"Duplicate key name 'site_id_device_id'" and "Table ... already exists".

Take a session-scoped advisory lock before upgrading. A request that
cannot get it skips the hook; one that gets it rechecks the recorded
version so it does not repeat an upgrade that just finished.

Ref #359

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
INSERT ... ON DUPLICATE KEY UPDATE consumes an auto-increment value for
every row it offers, including rows it merges, so a long-lived
mac_track_aggregated_ports table can exhaust an int(10) row_id. Declare
it bigint(20) unsigned and widen existing tables during the upgrade,
since cores before 1.2.32 do not refresh existing plugin tables.

Bump the version to 4.11 so installs that already recorded 4.10 after
the failed refresh run the upgrade again.

Ref #359

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

sonarqubecloud Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
2 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

Comment thread includes/database.php Dismissed
Comment thread setup.php Dismissed
@TheWitness
TheWitness merged commit aca68ce into develop Nov 12, 2026
9 checks passed
@TheWitness
TheWitness deleted the fix/aggregated-ports-unique-key branch November 12, 2026 01:51
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.

bug: make the 4.9 aggregated-port index migration safe and schema-consistent

3 participants