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

Load Lans effeciently in MiqProvisionVirtWorkflow #17381

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/models/miq_provision_virt_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ def available_vlans_and_hosts(options = {})
hosts = get_selected_hosts(src)
unless @vlan_options[:vlans] == false
rails_logger('allowed_vlans', 0)
# TODO: Use Active Record to preload this data?
MiqPreloader.preload(hosts, :lans => :switches)
Copy link
Contributor

Choose a reason for hiding this comment

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

@NickLaMuro - Is pulling this out a dependency on #17354?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, don't think so, though maybe I am not understanding the question.

That was how hosts, lans, and switches were being loaded previously, and how "zero SQL queries" were being made in the test previously. This was just using the MiqPreloader.preload code that already existed, and I was just removing it since it was no longer needed.

Did that answer the question?

Copy link
Member Author

Choose a reason for hiding this comment

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

Basically, in the previous versions of the code, this prevented N+1's in the load_hosts_vlans method. This is no longer needed, and avoids a lot of unneeded object allocations.

load_allowed_vlans(hosts, vlans)
rails_logger('allowed_vlans', 1)
end
Expand All @@ -244,9 +242,11 @@ def load_allowed_vlans(hosts, vlans)
end

def load_hosts_vlans(hosts, vlans)
hosts.each do |h|
h.lans.each { |l| vlans[l.name] = l.name unless l.switch.shared? }
end
Lan.joins(:switch => :hosts)
.where(:hosts => {:id => hosts.map(&:id)})
.where(:switches => {:shared => [nil, false]})
.distinct.pluck(:name)
.each_with_object(vlans) { |v, h| h[v] = v }
end

def filter_by_tags(target, options)
Expand Down
26 changes: 17 additions & 9 deletions spec/models/miq_provision_virt_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@

context 'network selection' do
before do
@ems = FactoryGirl.create(:ems_vmware)
@host1 = FactoryGirl.create(:host_vmware, :ems_id => @ems.id)
@src_vm = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_id => @ems.id)
@ems = FactoryGirl.create(:ems_vmware)
@host1 = FactoryGirl.create(:host_vmware, :ems_id => @ems.id)
@host2 = FactoryGirl.create(:host_vmware, :ems_id => @ems.id)
@src_vm = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_id => @ems.id)
@other_vm = FactoryGirl.create(:vm_vmware, :host => @host2, :ems_id => @ems.id)
allow(Rbac).to receive(:search) do |hash|
[Array.wrap(hash[:targets])]
end
Expand All @@ -84,24 +86,30 @@
s11 = FactoryGirl.create(:switch, :name => "A")
s12 = FactoryGirl.create(:switch, :name => "B")
s13 = FactoryGirl.create(:switch, :name => "C")
@src_vm.host.switches = [s11, s12, s13]
s14 = FactoryGirl.create(:switch, :name => "D")
@src_vm.host.switches = [s11, s12, s13]
@other_vm.host.switches = [s14]
@lan11 = FactoryGirl.create(:lan, :name => "lan_A", :switch_id => s11.id)
@lan12 = FactoryGirl.create(:lan, :name => "lan_B", :switch_id => s12.id)
@lan13 = FactoryGirl.create(:lan, :name => "lan_C", :switch_id => s13.id)
@lan14 = FactoryGirl.create(:lan, :name => "lan_D", :switch_id => s14.id)
Copy link
Contributor

Choose a reason for hiding this comment

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

@NickLaMuro - Were the @lan14 and @other_vm additions to shore up the spec test cases or for another reason?

Copy link
Member Author

Choose a reason for hiding this comment

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

@syncrou Took me a bit to remember why I did it myself!

I was actually to make sure that this test:

expect { workflow.load_hosts_vlans(hosts, {}) }.not_to exceed_query_limit(1)

Was actually being exercised properly when multiple hosts were present. If I left the data as it was, I would have technically passed that spec without actually avoiding an N+1 because we were only testing against 1 host (note, the previous code did a hosts.each. Just wanted to make sure there wasn't a hidden N+1 in there by testing against multiple hosts.

end

it '#allowed_vlans' do
allow(workflow).to receive(:allowed_hosts).with(no_args).and_return([workflow.host_to_hash_struct(@host1)])
allowed_hosts = [
workflow.host_to_hash_struct(@host1),
workflow.host_to_hash_struct(@host2)
]
allow(workflow).to receive(:allowed_hosts).with(no_args).and_return(allowed_hosts)
vlans = workflow.allowed_vlans(:vlans => true, :dvs => false)
lan_keys = [@lan11.name, @lan13.name, @lan12.name]
lan_keys = [@lan11.name, @lan13.name, @lan12.name, @lan14.name]
expect(vlans.keys).to match_array(lan_keys)
expect(vlans.values).to match_array(lan_keys)
end

it '#load_hosts_vlans' do
hosts = [@host1]
MiqPreloader.preload(hosts, :lans => :switches)
expect { workflow.load_hosts_vlans(hosts, {}) }.not_to exceed_query_limit(0)
hosts = [@host1, @host2]
expect { workflow.load_hosts_vlans(hosts, {}) }.not_to exceed_query_limit(1)
end
end
end
Expand Down