Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion includes/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,16 @@ function mactrack_rebuild_aggregated_ports(): bool {
return false;
}

db_execute("DROP TABLE IF EXISTS $source");
// (e) Drop the drained source and record completion. Check the drop:
// if it fails, the leftover table would keep the poller paused and the
// web hook launching, yet the rebuilt flag would make every retry take
// the early return and never drop it again. Returning false here (before
// setting the flag) keeps the cleanup retryable.
if (!db_execute("DROP TABLE IF EXISTS $source")) {
cacti_log("MACTRACK: Could not drop the drained $source; the rebuild will be retried", false, 'SYSTEM');

return false;
}

set_config_option('mt_aggregated_ports_rebuilt', 'on');

Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/AggregatedPortsRebuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,31 @@ public function testPollerRunningIgnoresStaleRegistrations(): void {

$this->assertFalse(mactrack_poller_running());
}

/**
* A failed DROP of the drained source must not record completion: the
* leftover table would keep writers paused while the rebuilt flag made
* every retry skip the drop, so the pass reports failure to stay retryable.
*
* @return void
*/
public function testRebuildRetriesWhenDroppingTheDrainedSourceFails(): void {
// Resume shape (source present, destination missing) keeps the db_execute
// sequence to CREATE then DROP, so only the drop is forced to fail.
self::stubTable('mac_track_aggregated_ports', false);
self::stubTable('mac_track_aggregated_ports_upgrade', true);
self::stubLock();
self::stubColumnType('bigint(20) unsigned');
self::stubUniqueKey('mac_track_aggregated_ports', 0);
self::stubShowIndexes('mac_track_aggregated_ports_upgrade', [['Key_name' => 'date_last']]);
self::stubSourceCount(0);
// CREATE TABLE and the single column MODIFY succeed; DROP TABLE fails.
mactrack_test_queue_return('db_execute', true);
mactrack_test_queue_return('db_execute', true);
mactrack_test_queue_return('db_execute', false);

$this->assertFalse(mactrack_rebuild_aggregated_ports());
$this->assertCount(1, self::callsMatching('DROP TABLE IF EXISTS mac_track_aggregated_ports_upgrade'));
$this->assertNotSame('on', read_config_option('mt_aggregated_ports_rebuilt'));
}
}
Loading