-
Notifications
You must be signed in to change notification settings - Fork 900
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
Changes from all commits
898b63d
4d7af02
e574ad3
1876da3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
end | ||
|
||
connection_count = backup_type == :basebackup ? VmdbDatabaseConnection.unscoped.count : VmdbDatabaseConnection.count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because all it does when used with |
||
end | ||
|
||
def self.gc(options = {}) | ||
PostgresAdmin.gc(options) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆