From 265e3c74d6ee185a2ca8e6b386207f0e108a7e93 Mon Sep 17 00:00:00 2001 From: Digimer Date: Tue, 30 Mar 2021 16:20:04 -0400 Subject: [PATCH] * Updated Database->connect to track previous connected DB count to current one (only useful for daemons). If the connection count has not changed, a check for resync is not performed. * Updated Database->_find_behind_databases() to not trigger a resync if the only difference in a table is the last-updated time and the difference is less than ten seconds. This should dramatically cut back on unnecessary resyncs and reduce load. Signed-off-by: Digimer --- Anvil/Tools/Database.pm | 84 +++++++++++++++++-------- scancore-agents/scan-drbd/scan-drbd.xml | 2 + 2 files changed, 60 insertions(+), 26 deletions(-) diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index c14f513c..a0937a8a 100644 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -445,7 +445,7 @@ sub check_agent_data }; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0181", variables => $variables}); $anvil->Alert->register({ - debug => 2, + debug => $debug, alert_level => "warning", message => "message_0181", variables => $variables, @@ -1663,15 +1663,41 @@ sub connect # For now, we just find which DBs are behind and let each agent deal with bringing their tables up to # date. + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::database::connections" => $anvil->data->{sys}{database}{connections} }}); if ($anvil->data->{sys}{database}{connections} > 1) { - $anvil->Database->_find_behind_databases({ - debug => $debug, - source => $source, - tables => $tables, - }); + # If we have a "last_db_count" and it's the same as the current number of connections, skip + # checking for a resync. This is done because the databases change constantly so tables like + # jobs, which scancore and anvil-daemon constantly change, doesn't trigger a resync when + # records change mid-check. + my $check = 1; + if (exists $anvil->data->{sys}{database}{last_db_count}) + { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + "sys::database::last_db_count" => $anvil->data->{sys}{database}{last_db_count}, + "sys::database::connections" => $anvil->data->{sys}{database}{connections}, + }}); + if ($anvil->data->{sys}{database}{last_db_count} eq $anvil->data->{sys}{database}{connections}) + { + $check = 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { check => $check }}); + } + } + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { check => $check }}); + if ($check) + { + $anvil->Database->_find_behind_databases({ + debug => $debug, + source => $source, + tables => $tables, + }); + } } + $anvil->data->{sys}{database}{last_db_count} = $anvil->data->{sys}{database}{connections}; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::database::last_db_count" => $anvil->data->{sys}{database}{last_db_count} }}); + # Hold if a lock has been requested. $anvil->Database->locking({debug => $debug}); @@ -4778,7 +4804,7 @@ sub initialize # Now that I am ready, disable autocommit, write and commit. $anvil->Database->write({ - debug => 2, + debug => $debug, uuid => $uuid, query => $sql, source => $THIS_FILE, @@ -6589,7 +6615,7 @@ SET WHERE health_uuid = ".$anvil->Database->quote($health_uuid)." ;"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__}); $query = " @@ -6598,7 +6624,7 @@ DELETE FROM WHERE health_uuid = ".$anvil->Database->quote($health_uuid)." ;"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); $anvil->Database->write({uuid => $uuid, query => $query, source => $file ? $file." -> ".$THIS_FILE : $THIS_FILE, line => $line ? $line." -> ".__LINE__ : __LINE__}); } else @@ -11954,7 +11980,7 @@ SET WHERE temperature_uuid = ".$anvil->Database->quote($temperature_uuid)."; "; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); if (ref($cache) eq "ARRAY") { push @{$cache}, $query; @@ -15214,7 +15240,7 @@ sub _archive_table # We don't archive the OUI table, it generally has more entries than needed to trigger the archive, but it's needed. if (($table eq "oui") or ($table eq "states")) { - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, priority => "err", key => "log_0459", variables => { table => $table }}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, priority => "err", key => "log_0459", variables => { table => $table }}); return(0); } @@ -15697,24 +15723,30 @@ ORDER BY }}); if ($anvil->data->{sys}{database}{table}{$table}{last_updated} > $anvil->data->{sys}{database}{table}{$table}{uuid}{$uuid}{last_updated}) { - ### TODO: This triggers with extremely high numbers. Somewhere, the time - ### isn't being updated properly. + ### NOTE: This triggers often with just a few seconds difference, which is + ### more likely caused by one database being reads, something changes, + ### and the next database is read. As such, we won't trigger unless + ### the difference is more than 10 seconds. # Resync needed. - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { - "sys::database::table::${table}::last_updated" => $anvil->data->{sys}{database}{table}{$table}{last_updated}, - "sys::database::table::${table}::uuid::${uuid}::last_updated" => $anvil->data->{sys}{database}{table}{$table}{uuid}{$uuid}{last_updated}, - }}); my $difference = $anvil->Convert->add_commas({number => ($anvil->data->{sys}{database}{table}{$table}{last_updated} - $anvil->data->{sys}{database}{table}{$table}{uuid}{$uuid}{last_updated}) }); - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => "log_0106", variables => { - seconds => $difference, - table => $table, - uuid => $uuid, - host => $anvil->Get->host_name_from_uuid({host_uuid => $uuid}), + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + "s1:difference" => $difference, + "s2:sys::database::table::${table}::last_updated" => $anvil->data->{sys}{database}{table}{$table}{last_updated}, + "s3:sys::database::table::${table}::uuid::${uuid}::last_updated" => $anvil->data->{sys}{database}{table}{$table}{uuid}{$uuid}{last_updated}, }}); - - # Mark it as behind. - $anvil->Database->_mark_database_as_behind({debug => $debug, uuid => $uuid}); - last; + if ($difference > 10) + { + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => "log_0106", variables => { + seconds => $difference, + table => $table, + uuid => $uuid, + host => $anvil->Get->host_name_from_uuid({host_uuid => $uuid}), + }}); + + # Mark it as behind. + $anvil->Database->_mark_database_as_behind({debug => $debug, uuid => $uuid}); + last; + } } elsif ($anvil->data->{sys}{database}{table}{$table}{row_count} > $anvil->data->{sys}{database}{table}{$table}{uuid}{$uuid}{row_count}) { diff --git a/scancore-agents/scan-drbd/scan-drbd.xml b/scancore-agents/scan-drbd/scan-drbd.xml index 4fc8c057..32bca334 100644 --- a/scancore-agents/scan-drbd/scan-drbd.xml +++ b/scancore-agents/scan-drbd/scan-drbd.xml @@ -202,6 +202,8 @@ The DRBD resource was not found in the database, but appears to have been in the + Down + The resource is stopped. StandAlone No network configuration available. The resource has not yet been connected, or has been administratively disconnected (using drbdadm disconnect), or has dropped its connection due to failed authentication or split brain. Connecting