Skip to content

Commit a4ea2b0

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 39b3e6e commit a4ea2b0

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
@@ -346,7 +346,8 @@ def allowed_templates(options = {})
346346
# Only select the colums we need
347347
vms = vms.select(:id, :name, :guid, :uid_ems, :ems_id, :cloud_tenant_id)
348348

349-
allowed_templates_list = source_vm_rbac_filter(vms, condition, VM_OR_TEMPLATE_EXTRA_COLS).to_a
349+
sql = source_vm_rbac_filter(vms, condition, VM_OR_TEMPLATE_EXTRA_COLS).to_sql
350+
allowed_templates_list = ActiveRecord::Base.connection.select_all(sql).to_a
350351
@allowed_templates_filter = filter_id
351352
@allowed_templates_tag_filters = @values[:vm_tags]
352353
rails_logger('allowed_templates', 1)
@@ -1048,42 +1049,53 @@ def setup_parameters_for_visibility_service(options)
10481049
end
10491050

10501051
def create_hash_struct_from_vm_or_template(vm_or_template, options)
1051-
data_hash = {:id => vm_or_template.id,
1052-
:name => vm_or_template.name,
1053-
:guid => vm_or_template.guid,
1054-
:uid_ems => vm_or_template.uid_ems,
1055-
:platform => vm_or_template.platform,
1056-
:logical_cpus => vm_or_template.cpu_total_cores,
1057-
:mem_cpu => vm_or_template.mem_cpu,
1058-
:allocated_disk_storage => vm_or_template.allocated_disk_storage,
1059-
:v_total_snapshots => vm_or_template.v_total_snapshots,
1060-
:evm_object_class => :Vm}
1061-
data_hash[:cloud_tenant] = vm_or_template.cloud_tenant if vm_or_template.cloud_tenant_id
1062-
if vm_or_template.operating_system
1063-
data_hash[:operating_system] = MiqHashStruct.new(:product_name => vm_or_template.operating_system_product_name)
1064-
end
1065-
if options[:include_datacenter] == true
1066-
data_hash[:datacenter_name] = vm_or_template.owning_blue_folder.try(:parent_datacenter).try(:name)
1052+
if vm_or_template.kind_of?(Hash)
1053+
data_hash = vm_or_template
1054+
data_hash[:logical_cpus] = data_hash.delete(:cpu_total_cores)
1055+
if vm_or_template[:operating_system_product_name]
1056+
data_hash[:operating_system] = MiqHashStruct.new(:product_name => vm_or_template[:operating_system_product_name])
1057+
end
1058+
# TODO: solve owning_blue_folder for the "Hash method" (if needed...)
1059+
else
1060+
data_hash = {:id => vm_or_template.id,
1061+
:name => vm_or_template.name,
1062+
:guid => vm_or_template.guid,
1063+
:uid_ems => vm_or_template.uid_ems,
1064+
:platform => vm_or_template.platform,
1065+
:logical_cpus => vm_or_template.cpu_total_cores,
1066+
:mem_cpu => vm_or_template.mem_cpu,
1067+
:allocated_disk_storage => vm_or_template.allocated_disk_storage,
1068+
:v_total_snapshots => vm_or_template.v_total_snapshots}
1069+
data_hash[:cloud_tenant] = vm_or_template.cloud_tenant if vm_or_template[:cloud_tenant_id]
1070+
if vm_or_template.operating_system
1071+
data_hash[:operating_system] = MiqHashStruct.new(:product_name => vm_or_template.operating_system_product_name)
1072+
end
1073+
if vm_or_template[:ems_id]
1074+
data_hash[:ext_management_system] = MiqHashStruct.new(:name => vm_or_template.ext_management_system.name)
1075+
end
1076+
if options[:include_datacenter] == true
1077+
data_hash[:datacenter_name] = vm_or_template.owning_blue_folder.try(:parent_datacenter).try(:name)
1078+
end
10671079
end
10681080

1081+
data_hash[:evm_object_class] = :Vm
1082+
10691083
# Handle EMS data, either with a cache, or with the relation (assumes it is
10701084
# preloaded usually)
10711085
if @_ems_allowed_templates_cache
10721086
# don't have a key, so don't attempt to add to the data_hash
1073-
if vm_or_template.ems_id
1074-
ems_id = vm_or_template.ems_id
1087+
if vm_or_template[:ems_id]
1088+
ems_id = vm_or_template[:ems_id]
10751089

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

10811095
if @_ems_allowed_templates_cache[ems_id]
10821096
data_hash[:ext_management_system] = MiqHashStruct.new(:name => @_ems_allowed_templates_cache[ems_id])
10831097
end
10841098
end
1085-
elsif vm_or_template.ems_id
1086-
data_hash[:ext_management_system] = MiqHashStruct.new(:name => vm_or_template.ext_management_system.name)
10871099
end
10881100

10891101
MiqHashStruct.new(data_hash)

0 commit comments

Comments
 (0)