Skip to content

Commit

Permalink
Handle writable_storages in miq_request_workflow better
Browse files Browse the repository at this point in the history
The MiqPreloader.preload call was added prior to the usage of
`.writable_storages` for the miq_request_workflow.rb.  When
writable_storages was called, it would make a new call to the database
every time for each of the storages, which was not ideal.

This change makes it so that the writable_storages method will first
check to see if the proper data is already loaded, and if so, just use
that.  Otherwise, it will do the query as normal.

The other change was to change the MiqPreloader.preload to include the
`host_storages`, which properly applies the loaded records needed for
the changes to the `host` model to work.
  • Loading branch information
NickLaMuro committed Apr 26, 2018
1 parent 7f77ae9 commit b77f060
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions app/models/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1750,11 +1750,19 @@ def openstack_host? # TODO: This doesn't belong here
end

def writable_storages
storages.where(:host_storages => {:read_only => [false, nil]})
if host_storages.loaded? && host_storages.all? { |hs| hs.association(:storage).loaded? }
host_storages.reject(&:read_only).map(&:storage)
else
storages.where(:host_storages => {:read_only => [false, nil]})
end
end

def read_only_storages
storages.where(:host_storages => {:read_only => true})
if host_storages.loaded? && host_storages.all? { |hs| hs.association(:storage).loaded? }
host_storages.select(&:read_only).map(&:storage)
else
storages.where(:host_storages => {:read_only => true})
end
end

def archived?
Expand Down
2 changes: 1 addition & 1 deletion app/models/miq_request_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def allowed_storages(_options = {})

rails_logger('allowed_storages', 0)
st = Time.now
MiqPreloader.preload(hosts, :storages)
MiqPreloader.preload(hosts, :storages => {}, :host_storages => :storage)

storages = hosts.each_with_object({}) do |host, hash|
host.writable_storages.each { |s| hash[s.id] = s }
Expand Down

0 comments on commit b77f060

Please sign in to comment.