Repository navigation
perf: stream the slow query log during import to fix the 10GB memory footprint #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0b129ea
perf: stream the slow query log during import instead of loading it a…
TheWitness e5e9cae
fix: fail the import when the slow log read errors mid-stream
TheWitness c23bb10
i18n: add the new import read-error string to cacti.pot
TheWitness 159d6ba
test: cover the CLI (no parent record) read-error branch of import_lo…
TheWitness File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php | ||
| /* | ||
| +-------------------------------------------------------------------------+ | ||
| | Copyright (C) 2004-2026 The Cacti Group | | ||
| | | | ||
| | This program is free software; you can redistribute it and/or | | ||
| | modify it under the terms of the GNU General Public License | | ||
| | as published by the Free Software Foundation; either version 2 | | ||
| | of the License, or (at your option) any later version. | | ||
| | | | ||
| | This program is distributed in the hope that it will be useful, | | ||
| | but WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | ||
| | GNU General Public License for more details. | | ||
| +-------------------------------------------------------------------------+ | ||
| | Cacti: The Complete RRDtool-based Graphing Solution | | ||
| +-------------------------------------------------------------------------+ | ||
| | http://www.cacti.net/ | | ||
| +-------------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| /* | ||
| * Covers import_logfile()'s read-error handling. Streaming the log with | ||
| * fgets() can't tell EOF from an I/O error - both return false - so a | ||
| * mid-file read failure must not be mistaken for a clean end-of-log and | ||
| * finalized as a successful (but truncated) import. | ||
| * | ||
| * SlowlogReadErrorStream simulates exactly that: fopen() succeeds, the first | ||
| * fgets() returns false, and feof() stays false (as it would on a real read | ||
| * error), so import_logfile() must take its failure path. | ||
| */ | ||
|
|
||
| uses(TestCase::class); | ||
|
|
||
| if (!class_exists('SlowlogReadErrorStream')) { | ||
| class SlowlogReadErrorStream { | ||
| /** @var resource|null */ | ||
| public $context; | ||
|
|
||
| public function stream_open($path, $mode, $options, &$opened_path) { | ||
| return true; | ||
| } | ||
|
|
||
| public function stream_read($count) { | ||
| // A failed read, not EOF: false (not '') keeps feof() false. | ||
| return false; | ||
| } | ||
|
|
||
| public function stream_eof() { | ||
| return false; | ||
| } | ||
|
|
||
| public function stream_stat() { | ||
| return array('size' => 1); | ||
| } | ||
|
|
||
| public function url_stat($path, $flags) { | ||
| return array('size' => 1); | ||
| } | ||
|
|
||
| public function stream_close() { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| beforeEach(function () { | ||
| TestCase::loadPluginSource('includes/slowlog_functions.php'); | ||
|
|
||
| if (in_array('slowlogfail', stream_get_wrappers(), true)) { | ||
| stream_wrapper_unregister('slowlogfail'); | ||
| } | ||
|
|
||
| stream_wrapper_register('slowlogfail', 'SlowlogReadErrorStream'); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| if (in_array('slowlogfail', stream_get_wrappers(), true)) { | ||
| stream_wrapper_unregister('slowlogfail'); | ||
| } | ||
| }); | ||
|
|
||
| it('marks the parent record as failed (status 3) when the log read errors mid-stream', function () { | ||
| // 4242 is a pre-created parent logid, as the web upload handler supplies. | ||
| import_logfile('slowlogfail://log', 'test', -1, '', false, false, null, 4242); | ||
|
|
||
| $failed = null; | ||
|
|
||
| foreach ($GLOBALS['__test_db_calls'] as $call) { | ||
| if ($call['fn'] === 'db_execute_prepared' && strpos($call['sql'], 'import_status = 3') !== false) { | ||
| $failed = $call; | ||
| } | ||
| } | ||
|
|
||
| expect($failed)->not->toBeNull(); | ||
| expect($failed['params'])->toContain(4242); | ||
| }); | ||
|
|
||
| it('does not finalize the import as successful (status 1) when the log read errors mid-stream', function () { | ||
| import_logfile('slowlogfail://log', 'test', -1, '', false, false, null, 4242); | ||
|
|
||
| foreach ($GLOBALS['__test_db_calls'] as $call) { | ||
| expect($call['sql'])->not->toContain('import_status = 1'); | ||
| } | ||
| }); | ||
|
|
||
| it('prints a FATAL read error for a CLI import with no pre-created parent record', function () { | ||
| ob_start(); | ||
| import_logfile('slowlogfail://log', 'test', -1, '', false, false, null, null); | ||
| $output = ob_get_clean(); | ||
|
|
||
| expect($output)->toContain('FATAL: Read error'); | ||
|
|
||
| // With no parent logid there is nothing to mark as failed - it must not | ||
| // have recorded a status-3 update either. | ||
| foreach ($GLOBALS['__test_db_calls'] as $call) { | ||
| expect($call['sql'])->not->toContain('import_status = 3'); | ||
| } | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.