Skip to content

Commit

Permalink
Add uniq on datacenters in #host_to_folder
Browse files Browse the repository at this point in the history
The more stripped down version of this method, prior to the change, is
as follows:

    def host_to_folder(src)
      sources = src[:host].nil? ? allowed_hosts_obj : [src[:host]]
      datacenters = sources.collect { |h|
        find_datacenter_for_ci(h)
      end.compact
      datacenters.each_with_object({}) do |dc, folders|
        folders.merge!(get_ems_folders(dc))
      end
    end

The `find_datacenter_for_ci` method basically looks up the datacenter
for the matching host object in the xml tree that has been generated.
In most (all) cases though, their will be more than one host per
datacenter, and that wasn't taken into account for this method since the
next block blindly takes those datacenters and merges the folder
structure it gets from `get_ems_folders` for each datacenter.

By adding a `.uniq` prior to the assignment of datacenters, this saves a
significant number of unnecessary CPU cycles and object allocations to
fetch the same data and apply it to the `folders` variable, without
changing the end result.

Note:  Ideally we would want a better algorithm for finding the
datacenters for a collection of hosts, one that allowed for ejecting
early from find_datacenter_for_ci when we knew an existing datacenter
for a given host had already been found.  But the time taken to get the
datacenters from the hosts is far less in CPU time than the time taken
to call `#get_ems_folders` per datacenter object and merge the hash with
the existing results.
  • Loading branch information
NickLaMuro committed May 14, 2018
1 parent c2e01c9 commit 1bc8c3f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/miq_request_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ def host_to_folder(src)
result = find_datacenter_for_ci(h)
rails_logger("host_to_folder for host #{h.name}", 1)
result
end.compact
end.compact.uniq
datacenters.each_with_object({}) do |dc, folders|
rails_logger("host_to_folder for dc #{dc.name}", 0)
folders.merge!(get_ems_folders(dc))
Expand Down

0 comments on commit 1bc8c3f

Please sign in to comment.