Repository navigation
fix(mactrack): restore the unique aggregated ports key on upgrade - #418
Merged
Merged
Conversation
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>
|
TheWitness
approved these changes
Nov 12, 2026
This was referenced Nov 12, 2026
Closed
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.

Upgrading an install whose
mac_track_aggregated_portsheld duplicate rows failed withDuplicate 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_numberkey withADD 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'sON DUPLICATE KEY UPDATE count_rec=count_rec+1never 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:
count_rec, earliestfirst_scan_date, latestdate_last) in one transaction, then rebuildport_numberas UNIQUE. This runs beforeplugin_mactrack_install()refreshes the table, and also without a version change. It handles a table whose index was dropped by hand.Can't DROP INDEX port_numberandDuplicate key name 'site_id_device_id'errors.row_idtobigint(20) unsigned, in the fresh schema and on upgrade.Verification:
Pest suite: 306 passed, 100% patch coverage.
Upgrade scenarios on Cacti 1.2.x with MariaDB 10.11 and MySQL 8.4:
Each ends with one row per key and the unique index restored, and the next poller pass increments
count_recinstead of adding a row.Two concurrent
index.phploads against 320k duplicate rows over 20k keys finished in about 9 seconds with no errors and thecount_rectotal preserved.Fixes #359