Merge branch 'ent-13720-italtel-problema-raro-de-desincronizacion-en-alta-disponibilidad-ha-guardia' into 'develop'

Double check the keepalive value when choosing a new master.

See merge request artica/pandorafms!7431
This commit is contained in:
Enrique Martin 2024-06-03 10:21:16 +00:00
commit 87f2996678
1 changed files with 56 additions and 17 deletions

View File

@ -434,7 +434,8 @@ sub ha_database_connect($) {
############################################################################### ###############################################################################
sub ha_database_connect_pandora($) { sub ha_database_connect_pandora($) {
my $conf = shift; my $conf = shift;
my $dbhost = $conf->{'dbhost'}; my $dbhost = "";
my $dbhost_ro = 1;
# Load the list of HA databases. # Load the list of HA databases.
ha_load_databases($conf); ha_load_databases($conf);
@ -444,6 +445,9 @@ sub ha_database_connect_pandora($) {
foreach my $ha_dbhost (@HA_DB_Hosts) { foreach my $ha_dbhost (@HA_DB_Hosts) {
# Assume the database is read-only.
my $ha_dbhost_ro = 1;
# Retry each database ha_connect_retries times. # Retry each database ha_connect_retries times.
for (my $i = 0; $i < $conf->{'ha_connect_retries'}; $i++) { for (my $i = 0; $i < $conf->{'ha_connect_retries'}; $i++) {
eval { eval {
@ -455,6 +459,9 @@ sub ha_database_connect_pandora($) {
$conf->{'ha_dbuser'}, $conf->{'ha_dbuser'},
$conf->{'ha_dbpass'}); $conf->{'ha_dbpass'});
log_message($conf, 'DEBUG', "Connected to database $ha_dbhost"); log_message($conf, 'DEBUG', "Connected to database $ha_dbhost");
# Check if the database is read-only.
$ha_dbhost_ro = ha_read_only($conf, $dbh);
}; };
log_message($conf, 'WARNING', $@) if ($@); log_message($conf, 'WARNING', $@) if ($@);
@ -468,30 +475,37 @@ sub ha_database_connect_pandora($) {
# No luck. Try the next database. # No luck. Try the next database.
next unless defined($dbh); next unless defined($dbh);
# Check if database is disabled.
if (defined(get_db_value($dbh, 'SELECT `id` FROM `tdatabase` WHERE `host` = "' . $ha_dbhost . '" AND disabled = 1')))
{
log_message($conf, 'LOG', "Ignoring disabled host: " . $ha_dbhost);
db_disconnect($dbh);
next;
}
eval { eval {
# Get the most recent utimestamp from the database. # Check if the database is disabled.
$utimestamp = get_db_value($dbh, 'SELECT UNIX_TIMESTAMP(MAX(keepalive)) FROM tserver'); if (defined(get_db_value($dbh, 'SELECT `id` FROM `tdatabase` WHERE `host` = "' . $ha_dbhost . '" AND disabled = 1'))) {
db_disconnect($dbh); log_message($conf, 'LOG', "Ignoring disabled host: " . $ha_dbhost);
} else {
# Did we find a more recent database? # Get the most recent utimestamps from the databases.
$utimestamp = 0 unless defined($utimestamp); $utimestamp = get_db_value($dbh, 'SELECT UNIX_TIMESTAMP(MAX(keepalive)) FROM tserver');
if ($utimestamp > $max_utimestamp) { $utimestamp = -1 unless defined($utimestamp);
$dbhost = $ha_dbhost; $max_utimestamp = ha_get_keepalive($conf, $dbhost);
$max_utimestamp = $utimestamp; $max_utimestamp = -1 unless defined($utimestamp);
}
# Did we find a more recent database? Or a writable database?
log_message($conf, 'DEBUG', "Utimestamp for $ha_dbhost (ro $ha_dbhost_ro): $utimestamp Max. utimestamp: $max_utimestamp (ro $dbhost_ro)");
if (($max_utimestamp == -1) ||
($dbhost_ro == $ha_dbhost_ro && $utimestamp > $max_utimestamp) ||
($dbhost_ro == 1 && $ha_dbhost_ro == 0)) {
log_message($conf, 'DEBUG', "Selected $ha_dbhost as the new master candidate...");
$dbhost = $ha_dbhost;
$dbhost_ro = $ha_dbhost_ro;
}
}
db_disconnect($dbh);
}; };
log_message($conf, 'WARNING', $@) if ($@); log_message($conf, 'WARNING', $@) if ($@);
} }
# Return a connection to the selected master. # Return a connection to the selected master.
eval { eval {
$dbhost = $conf->{'dbhost'} unless $dbhost ne "";
log_message($conf, 'DEBUG', "Connecting to selected master $dbhost..."); log_message($conf, 'DEBUG', "Connecting to selected master $dbhost...");
$dbh = db_connect('mysql', $dbh = db_connect('mysql',
$conf->{'dbname'}, $conf->{'dbname'},
@ -540,6 +554,31 @@ sub ha_restart_pandora($) {
} }
###############################################################################
# Get the server keepalive value for the given host.
###############################################################################
sub ha_get_keepalive($$) {
my ($conf, $host) = @_;
my $utimestamp = -1;
return -1 if ($host eq "");
eval {
my $dbh= db_connect('mysql',
$conf->{'dbname'},
$host,
$conf->{'dbport'},
$conf->{'ha_dbuser'},
$conf->{'ha_dbpass'});
$utimestamp = get_db_value($dbh, 'SELECT UNIX_TIMESTAMP(MAX(keepalive)) FROM tserver');
$utimestamp = -1 unless defined($utimestamp);
db_disconnect($dbh);
};
log_message($conf, 'WARNING', $@) if ($@);
return $utimestamp;
}
############################################################################### ###############################################################################
# Main (Pandora) # Main (Pandora)
############################################################################### ###############################################################################