Fixed a bug where if a database is pingable but the pgsql database is down, and it's the first database tested (or local), then the DB handle used to read / quote fails.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 3 years ago
parent 823c73d6d8
commit e40d0e2444
  1. 128
      Anvil/Tools/Database.pm
  2. 2
      Anvil/Tools/Server.pm
  3. 2
      tools/anvil-provision-server

@ -1347,67 +1347,6 @@ sub connect
} }
} }
# Before we try to connect, see if this is a local database and, if so, make sure it's setup.
my $is_local = $anvil->Network->is_local({debug => $debug, host => $host});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { is_local => $is_local }});
if ($is_local)
{
$anvil->data->{sys}{database}{read_uuid} = $uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::database::read_uuid" => $anvil->data->{sys}{database}{read_uuid} }});
# If requested, and if running with root access, set it up (or update it) if needed.
# This method just returns if nothing is needed.
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
check_if_configured => $check_if_configured,
real_uid => $<,
effective_uid => $>,
}});
if (($check_if_configured) && ($< == 0) && ($> == 0))
{
$anvil->Database->configure_pgsql({debug => $debug, uuid => $uuid});
}
}
elsif (not $anvil->data->{sys}{database}{read_uuid})
{
$anvil->data->{sys}{database}{read_uuid} = $uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::database::read_uuid" => $anvil->data->{sys}{database}{read_uuid} }});
}
# If this isn't a local database, read the target's Anvil! version (if available) and make
# sure it matches ours. If it doesn't, skip this database.
if (not $is_local)
{
my ($local_anvil_version, $local_schema_version) = $anvil->_anvil_version({debug => $debug});
my ($remote_anvil_version, $remote_schema_version) = $anvil->Get->anvil_version({
debug => $debug,
target => $host,
password => $password,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
's1:host' => $host,
's2:local_anvil_version' => $local_anvil_version,
's3:remote_anvil_version' => $remote_anvil_version,
's4:local_schema_version' => $local_schema_version,
's5:remote_schema_version' => $remote_schema_version,
}});
# TODO: Periodically, we fail to get the remote version. For now, we proceed if
# everything else is OK. Might be better to pause a re-try... To be determined.
if (($remote_schema_version) && ($remote_schema_version ne $local_schema_version))
{
# Version doesn't match,
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0145", variables => {
host => $host,
local_version => $local_schema_version,
target_version => $remote_schema_version,
}});
# Delete the information about this database. We'll try again on nexy
# ->connect().
delete $anvil->data->{database}{$uuid};
next;
}
}
# Connect! # Connect!
my $dbh = ""; my $dbh = "";
### NOTE: The Database->write() method, when passed an array, will automatically disable ### NOTE: The Database->write() method, when passed an array, will automatically disable
@ -1484,11 +1423,12 @@ sub connect
}; };
} }
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => $message_key, variables => $variables }); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => $message_key, variables => $variables });
next;
} }
elsif ($dbh =~ /^DBI::db=HASH/) elsif ($dbh =~ /^DBI::db=HASH/)
{ {
# Woot! # Woot!
$anvil->data->{sys}{database}{connections}++; $anvil->data->{sys}{database}{connections}++;
push @{$successful_connections}, $uuid; push @{$successful_connections}, $uuid;
$anvil->data->{cache}{database_handle}{$uuid} = $dbh; $anvil->data->{cache}{database_handle}{$uuid} = $dbh;
@ -1607,6 +1547,67 @@ sub connect
"sys::database::timestamp" => $anvil->data->{sys}{database}{timestamp}, "sys::database::timestamp" => $anvil->data->{sys}{database}{timestamp},
}}); }});
} }
# Before we try to connect, see if this is a local database and, if so, make sure it's setup.
my $is_local = $anvil->Network->is_local({debug => $debug, host => $host});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { is_local => $is_local }});
if ($is_local)
{
$anvil->data->{sys}{database}{read_uuid} = $uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::database::read_uuid" => $anvil->data->{sys}{database}{read_uuid} }});
# If requested, and if running with root access, set it up (or update it) if needed.
# This method just returns if nothing is needed.
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
check_if_configured => $check_if_configured,
real_uid => $<,
effective_uid => $>,
}});
if (($check_if_configured) && ($< == 0) && ($> == 0))
{
$anvil->Database->configure_pgsql({debug => $debug, uuid => $uuid});
}
}
elsif (not $anvil->data->{sys}{database}{read_uuid})
{
$anvil->data->{sys}{database}{read_uuid} = $uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::database::read_uuid" => $anvil->data->{sys}{database}{read_uuid} }});
}
# If this isn't a local database, read the target's Anvil! version (if available) and make
# sure it matches ours. If it doesn't, skip this database.
if (not $is_local)
{
my ($local_anvil_version, $local_schema_version) = $anvil->_anvil_version({debug => $debug});
my ($remote_anvil_version, $remote_schema_version) = $anvil->Get->anvil_version({
debug => $debug,
target => $host,
password => $password,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
's1:host' => $host,
's2:local_anvil_version' => $local_anvil_version,
's3:remote_anvil_version' => $remote_anvil_version,
's4:local_schema_version' => $local_schema_version,
's5:remote_schema_version' => $remote_schema_version,
}});
# TODO: Periodically, we fail to get the remote version. For now, we proceed if
# everything else is OK. Might be better to pause a re-try... To be determined.
if (($remote_schema_version) && ($remote_schema_version ne $local_schema_version))
{
# Version doesn't match,
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0145", variables => {
host => $host,
local_version => $local_schema_version,
target_version => $remote_schema_version,
}});
# Delete the information about this database. We'll try again on nexy
# ->connect().
delete $anvil->data->{database}{$uuid};
next;
}
}
} }
my $total = tv_interval ($start_time, [gettimeofday]); my $total = tv_interval ($start_time, [gettimeofday]);
@ -14991,7 +14992,7 @@ sub resync_databases
# Now read in the data from the different databases. # Now read in the data from the different databases.
foreach my $uuid (sort {$a cmp $b} keys %{$anvil->data->{cache}{database_handle}}) foreach my $uuid (sort {$a cmp $b} keys %{$anvil->data->{cache}{database_handle}})
{ {
# ... $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { uuid => $uuid }});
$anvil->data->{db_resync}{$uuid}{public}{sql} = []; $anvil->data->{db_resync}{$uuid}{public}{sql} = [];
$anvil->data->{db_resync}{$uuid}{history}{sql} = []; $anvil->data->{db_resync}{$uuid}{history}{sql} = [];
@ -15313,6 +15314,7 @@ sub resync_databases
# Do the INSERTs now and then release the memory. # Do the INSERTs now and then release the memory.
foreach my $uuid (sort {$a cmp $b} keys %{$anvil->data->{cache}{database_handle}}) foreach my $uuid (sort {$a cmp $b} keys %{$anvil->data->{cache}{database_handle}})
{ {
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { uuid => $uuid }});
# Merge the queries for both schemas into one array, with public schema # Merge the queries for both schemas into one array, with public schema
# queries being first, then delete the arrays holding them to free memory # queries being first, then delete the arrays holding them to free memory
# before we start the resync. # before we start the resync.

@ -1304,8 +1304,6 @@ sub parse_definition
} }
$anvil->data->{server}{$target}{$server}{$source}{parsed} = $server_xml; $anvil->data->{server}{$target}{$server}{$source}{parsed} = $server_xml;
#print Dumper $server_xml;
#die;
# Get the DRBD data that this server will almost certainly be using. # Get the DRBD data that this server will almost certainly be using.
$anvil->DRBD->get_devices({debug => $debug}); $anvil->DRBD->get_devices({debug => $debug});

@ -1124,7 +1124,7 @@ sub check_drbd_minor_and_port
resource_name => $anvil->data->{job}{server_name}, resource_name => $anvil->data->{job}{server_name},
}); });
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
'job::drbd_minor' => $anvil->data->{job}{drbd_minor}, 'job::drbd_minor' => $anvil->data->{job}{drbd_minor},
'job::drbd_tcp_port' => $anvil->data->{job}{drbd_tcp_port}, 'job::drbd_tcp_port' => $anvil->data->{job}{drbd_tcp_port},
}}); }});
} }

Loading…
Cancel
Save