Skip to content
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

Make db restore more reliable #16942

Merged
merged 4 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions lib/evm_database_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,60 @@ def self.restore(db_opts, connect_opts = {})
db_opts[:local_file] = session.uri_to_local_path(uri)
end

backup = PostgresAdmin.restore(db_opts)
prepare_for_restore(db_opts[:local_file])

# remove all the connections before we restore; AR will reconnect on the next query
ActiveRecord::Base.connection_pool.disconnect!
backup_file = PostgresAdmin.restore(db_opts)
ensure
session.disconnect if session
end

uri ||= backup
uri ||= backup_file
_log.info("[#{db_opts[:dbname]}] database has been restored from file: [#{uri}]")
uri
end

private_class_method def self.prepare_for_restore(filename)
backup_type = validate_backup_file_type(filename)

if application_connections?
message = "Database restore failed. Shut down all evmserverd processes before attempting a database restore"
_log.error(message)
raise message
end

MiqRegion.replication_type = :none
60.times do
break if VmdbDatabaseConnection.where("application_name LIKE 'pglogical manager%'").count.zero?
_log.info("Waiting for pglogical connections to close...")
sleep 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

end

connection_count = backup_type == :basebackup ? VmdbDatabaseConnection.unscoped.count : VmdbDatabaseConnection.count
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unscoped, nice!

if connection_count > 1
message = "Database restore failed. #{connection_count - 1} connections remain to the database."
_log.error(message)
raise message
end
end

private_class_method def self.validate_backup_file_type(filename)
if PostgresAdmin.base_backup_file?(filename)
:basebackup
elsif PostgresAdmin.pg_dump_file?(filename)
:pgdump
else
message = "#{filename} is not in a recognized database backup format"
_log.error(message)
raise message
end
end

private_class_method def self.application_connections?
VmdbDatabaseConnection.all.map(&:application_name).any? { |app_name| app_name.start_with?("MIQ") }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should unscoped be used here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because all it does when used with .all is remove the database restriction, but since this one is for application connections we can only query against the configured database.

end

def self.gc(options = {})
PostgresAdmin.gc(options)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/evm_dba.rake
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ namespace :evm do

namespace :restore do
desc 'Restore the local ManageIQ EVM Database (VMDB) from a local backup file'
task :local do
task :local => :environment do
require 'trollop'
opts = Trollop.options(EvmRakeHelper.extract_command_options) do
opt :local_file, "Destination file", :type => :string, :required => true
Expand All @@ -215,7 +215,7 @@ namespace :evm do
end

desc 'Restore the local ManageIQ EVM Database (VMDB) from a remote backup file'
task :remote do
task :remote => :environment do
require 'trollop'
opts = Trollop.options(EvmRakeHelper.extract_command_options) do
opt :uri, "Destination depot URI", :type => :string, :required => true
Expand Down
1 change: 1 addition & 0 deletions spec/lib/evm_database_ops_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
allow_any_instance_of(MiqSmbSession).to receive(:settings_mount_point).and_return(Rails.root.join("tmp"))
allow(PostgresAdmin).to receive(:runcmd_with_logging)
allow(PostgresAdmin).to receive(:pg_dump_file?).and_return(true)
allow(PostgresAdmin).to receive(:base_backup_file?).and_return(false)
end

it "from local backup" do
Expand Down