Skip to content

Commit f567b12

Browse files
committed
Update Vm#find_all_by_mac_address_and_hostname_and_ipaddress
Instead of going to Hardware and mapping to Vm, just go to Vm. Instead of bringing back a bunch of Nics (guest_devices) and Networks, just us `joins`. Performance characteristics: - `Vm` (vs `VmOrTemplate`) tacked on a bunch of `type` values in the `WHERE` clause. - The `SELECT` only brings back `vms.*` rather than `hardwares.*`, `vms.*`, and at least one more: `devices.*` or `networks.*`. It is still performing 1 query, but 2 fewer extra tables. This is now a `scope` rather than an `Array`.
1 parent 05d5f4a commit f567b12

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

lib/workers/evm_server.rb

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -196,35 +196,15 @@ def set_local_server_vm
196196
end
197197

198198
def find_vms_by_mac_address_and_hostname_and_ipaddress(mac_address, hostname, ipaddress)
199-
return [] if mac_address.blank? && hostname.blank? && ipaddress.blank?
200-
201-
include = [:vm_or_template]
202-
references = []
203-
conds = [["hardwares.vm_or_template_id IS NOT NULL"]]
204-
if mac_address
205-
conds[0] << "guest_devices.address = ?"
206-
conds << mac_address
207-
include << :nics
208-
references << :guest_devices
209-
end
210-
if hostname
211-
conds[0] << "networks.hostname = ?"
212-
conds << hostname
213-
include << :networks
214-
references << :networks
215-
end
216-
if ipaddress
217-
conds[0] << "networks.ipaddress = ?"
218-
conds << ipaddress
219-
include << :networks
220-
references << :networks
221-
end
222-
conds[0] = "(#{conds[0].join(" AND ")})"
199+
return Vm.none if mac_address.blank? && hostname.blank? && ipaddress.blank?
200+
201+
scope = Vm
202+
# NOTE: nics are guest devices with an extra where()
203+
scope = scope.joins(:hardware => :nics).where(:guest_devices => {:address => mac_address}) if mac_address
204+
scope = scope.joins(:hardware => :networks).where(:networks => {:hostname => hostname}) if hostname
205+
scope = scope.joins(:hardware => :networks).where(:networks => {:ipaddress => ipaddress}) if ipaddress
223206

224-
Hardware.includes(include.uniq)
225-
.references(references.uniq)
226-
.where(conds)
227-
.map(&:vm_or_template).select { |vm| vm.kind_of?(Vm) }
207+
scope
228208
end
229209

230210
def reset_server_runtime_info

0 commit comments

Comments
 (0)