Skip to content

Commit

Permalink
QA: #5829 - Change to per local_data_id process
Browse files Browse the repository at this point in the history
* Use temporary tables when boost is running.
  • Loading branch information
TheWitness committed Sep 19, 2024
1 parent b40ad3b commit 87e78ed
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions lib/boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,59 +733,64 @@ function boost_process_poller_output($local_data_id, $rrdtool_pipe = null) {
$timestamp = time() - 10;
}

$query_string = 'SELECT * FROM (';
$query_string = '';
$query_string_suffix = 'ORDER BY local_data_id ASC, timestamp ASC, rrd_name ASC';
$sub_query_string = '';
$sql_params = array();
$locks = false;
$temp_table = false;

if (cacti_count($archive_tables)) {
foreach($archive_tables as $index => $table) {
$lock[$table] = db_execute("LOCK TABLE $table READ LOCAL", false);

$locks = true;
$temp_table = 'poller_output_boost_temp_' . time();

if (!$lock[$table]) {
/* lock will fail if the table is gone */
unset($archive_tables[$index]);
} else {
$sub_query_string .= ($sub_query_string != '' ? ' UNION ALL ':'') .
" SELECT $table.local_data_id, dl.data_template_id, UNIX_TIMESTAMP(time) AS timestamp, rrd_name, output
FROM $table
WHERE $table.local_data_id = ?";
db_execute("CREATE TEMPORARY TABLE $temp_table LIKE poller_output_boost");

$sql_params[] = $local_data_id;
}
foreach($archive_tables as $index => $table) {
db_execute_prepared("INSERT INTO $temp_table
SELECT *
FROM $table
WHERE local_data_id = ?",
array($local_data_id), false);
}
}

if ($locks) {
db_execute("LOCK TABLE poller_output_boost AS po READ LOCAL, LOCK TABLE data_local AS dl READ LOCAL");
if ($temp_table !== false) {
db_execute_prepared("INSERT INTO $temp_table
SELECT *
FROM poller_output_boost
WHERE local_data_id = ?
AND time < FROM_UNIXTIME(?)",
array($local_data_id, $timestamp), false);

$query_string = "SELECT po.local_data_id, dl.data_template_id,
UNIX_TIMESTAMP(po.time) AS timestamp, po.rrd_name, po.output
FROM $temp_table AS po
INNER JOIN data_local AS dl
ON po.local_data_id = dl.id
WHERE po.local_data_id = ?
AND po.time < FROM_UNIXTIME(?)
ORDER BY time ASC, rrd_name ASC";
} else {
$query_string = "SELECT po.local_data_id, dl.data_template_id,
UNIX_TIMESTAMP(po.time) AS timestamp, po.rrd_name, po.output
FROM poller_output_boost AS po
INNER JOIN data_local AS dl
ON po.local_data_id = dl.id
WHERE po.local_data_id = ?
AND po.time < FROM_UNIXTIME(?)
ORDER BY time ASC, rrd_name ASC";
}

$sub_query_string .= ($sub_query_string != '' ? ' UNION ALL ':'') .
" SELECT po.local_data_id, dl.data_template_id,
UNIX_TIMESTAMP(po.time) AS timestamp, po.rrd_name, po.output
FROM poller_output_boost AS po
INNER JOIN data_local AS dl
ON po.local_data_id = dl.id
WHERE po.local_data_id = ?
AND po.time < FROM_UNIXTIME(?)
ORDER BY time ASC, rrd_name ASC";

$sql_params[] = $local_data_id;
$sql_params[] = $timestamp;

$query_string = $query_string . $sub_query_string . ') t ' . $query_string_suffix;

boost_timer('get_records', BOOST_TIMER_START);
$results = db_fetch_assoc_prepared($query_string, $sql_params);
boost_timer('get_records', BOOST_TIMER_END);

$boost_results = cacti_sizeof($results);

if ($locks) {
db_execute('UNLOCK TABLES');
if ($temp_table !== false) {
db_execute("DROP TEMPORARY TABLE $temp_table");
}

cacti_log('Local Data ID: ' . $local_data_id . ', Boost Results: ' . $boost_results, false, 'BOOST', POLLER_VERBOSITY_MEDIUM);
Expand All @@ -795,17 +800,18 @@ function boost_process_poller_output($local_data_id, $rrdtool_pipe = null) {

if (cacti_count($archive_tables)) {
foreach($archive_tables as $table) {
db_execute_prepared('DELETE IGNORE FROM ' . $table . '
WHERE local_data_id = ?',
array($local_data_id));
db_execute_prepared("DELETE IGNORE
FROM $table
WHERE local_data_id = ?",
array($local_data_id), false);
}
}

if (cacti_sizeof($results)) {
db_execute_prepared('DELETE FROM poller_output_boost
WHERE local_data_id = ?
AND time < FROM_UNIXTIME(?)',
array($local_data_id, $timestamp));
array($local_data_id, $timestamp), false);
}

boost_timer('delete', BOOST_TIMER_END);
Expand Down

0 comments on commit 87e78ed

Please sign in to comment.