Skip to content

Commit fe57a37

Browse files
committed
[miq_provision_virt_workflow.rb] Only use hash results
This significantly improves the speed of the method by: - Avoiding instantiating intermediate `ActiveRecord` objects that are just garbage collected - Reusing hash results to create the `MiqHashStruct` instantiate Benchmarks ---------- This tests against a AWS EMS with 100k+ templates (public images): **Before** | ms | queries | query (ms) | rows | | ---: | ---: | ---: | ---: | | 14344 | 33 | 1759.6 | 243133 | | 14631 | 33 | 1729.0 | 243133 | | 13405 | 33 | 1752.3 | 243133 | **After** | ms | queries | query (ms) | rows | | ---: | ---: | ---: | ---: | | 4102 | 33 | 1751.3 | 243133 | | 4124 | 33 | 1787.1 | 243133 | | 4133 | 33 | 1746.9 | 243133 |
1 parent 98075de commit fe57a37

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

app/models/miq_provision_virt_workflow.rb

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def allowed_templates(options = {})
348348
# Only select the colums we need
349349
vms = vms.select(:id, :type, :name, :guid, :uid_ems, :ems_id, :cloud_tenant_id)
350350

351-
allowed_templates_list = source_vm_rbac_filter(vms, condition, VM_OR_TEMPLATE_EXTRA_COLS).to_a
351+
sql = source_vm_rbac_filter(vms, condition, VM_OR_TEMPLATE_EXTRA_COLS).to_sql
352+
allowed_templates_list = ActiveRecord::Base.connection.select_all(sql).to_a
352353
@allowed_templates_filter = filter_id
353354
@allowed_templates_tag_filters = @values[:vm_tags]
354355
rails_logger('allowed_templates', 1)
@@ -1041,47 +1042,58 @@ def setup_parameters_for_visibility_service(options)
10411042
end
10421043

10431044
def create_hash_struct_from_vm_or_template(vm_or_template, options)
1044-
data_hash = {:id => vm_or_template.id,
1045-
:name => vm_or_template.name,
1046-
:guid => vm_or_template.guid,
1047-
:uid_ems => vm_or_template.uid_ems,
1048-
:platform => vm_or_template.platform,
1049-
:logical_cpus => vm_or_template.cpu_total_cores,
1050-
:mem_cpu => vm_or_template.mem_cpu,
1051-
:allocated_disk_storage => vm_or_template.allocated_disk_storage,
1052-
:v_total_snapshots => vm_or_template.v_total_snapshots,
1053-
:evm_object_class => :Vm}
1054-
data_hash[:cloud_tenant] = vm_or_template.cloud_tenant if vm_or_template.cloud_tenant_id
1055-
if vm_or_template.operating_system
1056-
data_hash[:operating_system] = MiqHashStruct.new(:product_name => vm_or_template.operating_system_product_name)
1057-
end
1058-
if options[:include_datacenter] == true
1059-
data_hash[:datacenter_name] = vm_or_template.owning_blue_folder.try(:parent_datacenter).try(:name)
1045+
if vm_or_template.kind_of?(Hash)
1046+
data_hash = vm_or_template
1047+
data_hash[:logical_cpus] = data_hash.delete(:cpu_total_cores)
1048+
if vm_or_template[:operating_system_product_name]
1049+
data_hash[:operating_system] = MiqHashStruct.new(:product_name => vm_or_template[:operating_system_product_name])
1050+
end
1051+
# TODO: solve owning_blue_folder for the "Hash method" (if needed...)
1052+
else
1053+
data_hash = {:id => vm_or_template.id,
1054+
:name => vm_or_template.name,
1055+
:guid => vm_or_template.guid,
1056+
:uid_ems => vm_or_template.uid_ems,
1057+
:platform => vm_or_template.platform,
1058+
:logical_cpus => vm_or_template.cpu_total_cores,
1059+
:mem_cpu => vm_or_template.mem_cpu,
1060+
:allocated_disk_storage => vm_or_template.allocated_disk_storage,
1061+
:v_total_snapshots => vm_or_template.v_total_snapshots}
1062+
data_hash[:cloud_tenant] = vm_or_template.cloud_tenant if vm_or_template[:cloud_tenant_id]
1063+
if vm_or_template.operating_system
1064+
data_hash[:operating_system] = MiqHashStruct.new(:product_name => vm_or_template.operating_system_product_name)
1065+
end
1066+
if vm_or_template[:ems_id]
1067+
data_hash[:ext_management_system] = MiqHashStruct.new(:name => vm_or_template.ext_management_system.name)
1068+
end
1069+
if options[:include_datacenter] == true
1070+
data_hash[:datacenter_name] = vm_or_template.owning_blue_folder.try(:parent_datacenter).try(:name)
1071+
end
10601072
end
10611073
assign_ems_data_to_data_hash(data_hash, vm_or_template)
10621074

10631075
MiqHashStruct.new(data_hash)
10641076
end
10651077

10661078
def assign_ems_data_to_data_hash(data_hash, vm_or_template)
1079+
data_hash[:evm_object_class] = :Vm
1080+
10671081
# Handle EMS data, either with a cache, or with the relation (assumes it is
10681082
# preloaded usually)
10691083
if @_ems_allowed_templates_cache
10701084
# don't have a key, so don't attempt to add to the data_hash
1071-
if vm_or_template.ems_id
1072-
ems_id = vm_or_template.ems_id
1085+
if vm_or_template[:ems_id]
1086+
ems_id = vm_or_template[:ems_id]
10731087

10741088
# only fetch the ems if not fetched previously
1075-
unless @_ems_allowed_templates_cache.key?(vm_or_template.ems_id)
1089+
unless @_ems_allowed_templates_cache.key?(vm_or_template[:ems_id])
10761090
@_ems_allowed_templates_cache[ems_id] = ExtManagementSystem.find(ems_id).try(:name)
10771091
end
10781092

10791093
if @_ems_allowed_templates_cache[ems_id]
10801094
data_hash[:ext_management_system] = MiqHashStruct.new(:name => @_ems_allowed_templates_cache[ems_id])
10811095
end
10821096
end
1083-
elsif vm_or_template.ems_id
1084-
data_hash[:ext_management_system] = MiqHashStruct.new(:name => vm_or_template.ext_management_system.name)
10851097
end
10861098
end
10871099

0 commit comments

Comments
 (0)