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

Conversation

carbonin
Copy link
Member

@carbonin carbonin commented Feb 2, 2018

This PR moves much of the logic around how what to check for when we do a database restore into EvmDatabaseOps rather than PostgresAdmin

This allows us to use the models and native PG connections to do things like remove pglogical and check for connections to the database with particular application names.

This also brings all of the connection checking into one class (and repo) rather than doing some checking in the console, then some other checking in PostgresAdmin and also sometimes failing in db:drop

This change also fixes https://bugzilla.redhat.com/show_bug.cgi?id=1540686 which was caused by this confusing handling for different backup types.

@carbonin
Copy link
Member Author

carbonin commented Feb 2, 2018

Also marking this as WIP until I can work out specs and the correct merge ordering for this change and the other PRs related to this one.

sleep 5
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!

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.

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.

😆

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

private_class_method def self.without_connections
params = ActiveRecord::Base.remove_connection
Copy link
Member

Choose a reason for hiding this comment

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

do we want to call ActiveRecord::Base.connection_pool.disconnect! or somethign like that to disconnect all connections in the pool?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably. Is that the better way to do this?

Copy link
Member

Choose a reason for hiding this comment

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

@carbonin I think so. If other threads have connections, I don't believe remove_connection will disconnect those connections. I believe connection_pool.disconnect! is more what we want.

@jrafanie
Copy link
Member

jrafanie commented Feb 8, 2018

I like the direction that this is going. What remains to be done?

@carbonin carbonin force-pushed the make_db_restore_more_reliable branch from 36d3033 to c0fcb12 Compare February 9, 2018 20:18
@carbonin
Copy link
Member Author

carbonin commented Feb 9, 2018

I'll unwip this one. I think we can merge this first, then we can merge the others and release manageiq-appliance_console

@carbonin carbonin changed the title [WIP] Make db restore more reliable Make db restore more reliable Feb 9, 2018
@carbonin carbonin removed the wip label Feb 9, 2018
@carbonin carbonin force-pushed the make_db_restore_more_reliable branch from c0fcb12 to 3c9101e Compare February 9, 2018 20:21
This allows us to do more of the processing using existing code
rather than the current implementation which does the checks in
different places/repos and does a lot of shelling out to psql
rather than using a real database connection.

This also makes the error cases more explicit which fixes an issue
where a user could not restore a pg_dump backup when pglogical was
running.

https://bugzilla.redhat.com/show_bug.cgi?id=1540686
We need the rails environment to access the VmdbDatabaseConnection
model
To do this, we need to disconnect the connection we created when
requiring the :environment rake task to run this method.
@carbonin carbonin force-pushed the make_db_restore_more_reliable branch from 0740031 to 1876da3 Compare February 12, 2018 19:21
@miq-bot
Copy link
Member

miq-bot commented Feb 12, 2018

Checked commits carbonin/manageiq@898b63d~...1876da3 with ruby 2.3.3, rubocop 0.52.0, haml-lint 0.20.0, and yamllint 1.10.0
3 files checked, 0 offenses detected
Everything looks fine. 🍰

@bdunne bdunne merged commit 8d24b56 into ManageIQ:master Feb 28, 2018
@bdunne bdunne added this to the Sprint 81 Ending Mar 12, 2018 milestone Feb 28, 2018
@bdunne bdunne assigned bdunne and unassigned gtanzillo Feb 28, 2018
simaishi pushed a commit that referenced this pull request Mar 9, 2018
@simaishi
Copy link
Contributor

simaishi commented Mar 9, 2018

Gaprindashvili backport details:

$ git log -1
commit 524a3a0b9e9b7f8b0835d86dcfd405b31d5d46fd
Author: Brandon Dunne <[email protected]>
Date:   Wed Feb 28 11:17:57 2018 -0500

    Merge pull request #16942 from carbonin/make_db_restore_more_reliable
    
    Make db restore more reliable
    (cherry picked from commit 8d24b5684daffc96401535032302fd2faf7e4bb5)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1553903

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants