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

Fixes #21691 - clean installed packages on upgrade #7069

Merged
merged 1 commit into from Nov 17, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 1 addition & 22 deletions app/models/katello/concerns/host_managed_extensions.rb
Expand Up @@ -65,13 +65,7 @@ def content_and_puppet_match?
end

def import_package_profile(simple_packages)
if Setting[:bulk_query_installed_packages]
#this method is slow if the clean_installed_packages rake task has not been run, but faster if it has
found = import_package_profile_in_bulk(simple_packages)
else
found = import_package_profile_individually(simple_packages)
end

found = import_package_profile_in_bulk(simple_packages)
sync_package_associations(found.map(&:id))
end

Expand All @@ -89,21 +83,6 @@ def import_package_profile_in_bulk(simple_packages)
found
end

def import_package_profile_individually(simple_packages)
found = []
simple_packages.each do |simple_package|
::Katello::Util::Support.active_record_retry do
#use limit(1)[0] here to avoid a sort by id
pkg = InstalledPackage.where(:nvra => simple_package.nvra, :name => simple_package.name).limit(1)[0]
if pkg.nil?
pkg = InstalledPackage.create!(:nvra => simple_package.nvra, :name => simple_package.name)
end
found << pkg
end
end
found
end

def sync_package_associations(new_installed_package_ids)
old_associated_ids = self.installed_package_ids
table_name = self.host_installed_packages.table_name
Expand Down
2 changes: 0 additions & 2 deletions app/models/setting/content.rb
Expand Up @@ -40,8 +40,6 @@ def self.load_defaults
false, N_('Restrict Composite Content View promotion')),
self.set('check_services_before_actions', N_("Whether or not to check the status of backend services such as pulp and candlepin prior to performing some actions."),
true, N_('Check services before actions')),
self.set('bulk_query_installed_packages', N_("Use a bulk query when looking up installed packages."),
false, N_('Bulk query for installed packages.')),
self.set('force_post_sync_actions', N_("Force post sync actions such as indexing and email even if no content was available."),
false, N_('Force post-sync actions')),
self.set('default_download_policy', N_("Default download policy for repositories (either 'immediate', 'on_demand', or 'background')"), "on_demand",
Expand Down
55 changes: 55 additions & 0 deletions db/migrate/20171114150937_cleanup_installed_packages.rb
@@ -0,0 +1,55 @@
class CleanupInstalledPackages < ActiveRecord::Migration
def up
create_table "katello_installed_packages_new", force: :cascade do |t|
t.string "name", limit: 255, null: false
t.string "nvra", limit: 255, null: false
Copy link
Contributor

Choose a reason for hiding this comment

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

How about adding a unique index to this column to speed up the search

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure that it would help, but i can time it and see

Copy link
Member Author

Choose a reason for hiding this comment

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

tested and with the index it took about ~4 minutes more.

end

create_table "katello_host_installed_packages_new", force: :cascade do |t|
t.integer "host_id", null: false
t.integer "installed_package_id", null: false
end

Copy link
Contributor

Choose a reason for hiding this comment

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

How about a unique index for the combination columns here. This should speed up the lookup.

Copy link
Member Author

Choose a reason for hiding this comment

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

There arne't any lookups being done on this table?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. I guess add it on the katello_installed_packages_new

#Copy unique entires to new table
execute('insert into katello_installed_packages_new(nvra, name)
select distinct(nvra) as nvra, name from katello_installed_packages')

#copy associations
execute('
insert into katello_host_installed_packages_new (host_id, installed_package_id)
select distinct katello_host_installed_packages.host_id, katello_installed_packages_new.id
from katello_installed_packages
inner join katello_host_installed_packages on katello_host_installed_packages.installed_package_id = katello_installed_packages.id
inner join katello_installed_packages_new on katello_installed_packages_new.nvra = katello_installed_packages.nvra')

remove_foreign_key :katello_host_installed_packages, name: "katello_host_installed_packages_installed_package_id"
remove_foreign_key :katello_host_installed_packages, name: "katello_host_installed_packages_host_id"

drop_table "katello_installed_packages"
drop_table "katello_host_installed_packages"

rename_table "katello_installed_packages_new", "katello_installed_packages"
rename_table "katello_host_installed_packages_new", "katello_host_installed_packages"

add_index :katello_installed_packages, [:name, :nvra]

add_foreign_key "katello_host_installed_packages", "hosts",
:name => "katello_host_installed_packages_host_id", :column => "host_id"

add_foreign_key "katello_host_installed_packages", "katello_installed_packages",
:name => "katello_host_installed_packages_installed_package_id", :column => "installed_package_id"

#At this point, everything should be back to where it was (sans-duplicates)
# Now do new things
add_index :katello_installed_packages, [:nvra], :unique => true
add_index :katello_host_installed_packages, [:host_id, :installed_package_id], :unique => true, :name => :katello_host_installed_packages_h_id_ip_id
Setting.where(:name => 'bulk_query_installed_packages', :category => 'Setting::Content').delete_all
end

def down
Copy link
Contributor

Choose a reason for hiding this comment

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

I am debating if you should do

    def down
        raise ActiveRecord::IrreversibleMigration
    end

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that is needed? The db should be in the same state as before, just with de-duplicate data.

#only revert new things
remove_index :katello_installed_packages, [:nvra]
remove_index :katello_host_installed_packages, :name => :katello_host_installed_packages_h_id_ip_id
#Setting will be recreated on startup
end
end
71 changes: 0 additions & 71 deletions lib/katello/tasks/clean_installed_packages.rake

This file was deleted.

30 changes: 0 additions & 30 deletions test/lib/tasks/clean_installed_packages_test.rb

This file was deleted.

9 changes: 0 additions & 9 deletions test/models/concerns/host_managed_extensions_test.rb
Expand Up @@ -127,7 +127,6 @@ def test_non_matching_puppet_environment
class HostInstalledPackagesTest < HostManagedExtensionsTestBase
def setup
super
Setting[:bulk_query_installed_packages] = false
package_json = {:name => "foo", :version => "1", :release => "1.el7", :arch => "x86_64"}
@foreman_host.import_package_profile([::Katello::Pulp::SimplePackage.new(package_json)])
@nvra = 'foo-1-1.el7.x86_64'
Expand All @@ -140,15 +139,7 @@ def test_installed_packages
assert_equal @nvra, @foreman_host.installed_packages.first.nvra
end

def test_import_package_profile_adds_removes
package_json = {:name => "betterfoo", :version => "1", :release => "1.el7", :arch => "x86_64"}
@foreman_host.import_package_profile([::Katello::Pulp::SimplePackage.new(package_json)])
assert_equal 1, @foreman_host.installed_packages.count
assert_equal 'betterfoo', @foreman_host.installed_packages.first.name
end

def test_import_package_profile_adds_removes_bulk
Setting[:bulk_query_installed_packages] = true
packages = [::Katello::Pulp::SimplePackage.new(:name => "betterfoo", :version => "1", :release => "1.el7", :arch => "x86_64")]
@foreman_host.import_package_profile(packages)
assert_equal 1, @foreman_host.installed_packages.count
Expand Down