diff --git a/app/models/metric/capture.rb b/app/models/metric/capture.rb index 02a74dbb93a6..58a1393a312b 100644 --- a/app/models/metric/capture.rb +++ b/app/models/metric/capture.rb @@ -43,12 +43,13 @@ def self.alert_capture_threshold(target) ::Settings.performance.capture_threshold_with_alerts.default) end - def self.perf_capture_timer(zone = nil) + def self.perf_capture_timer(ems_id = nil) _log.info("Queueing performance capture...") - zone ||= MiqServer.my_server.zone + ems = ExtManagementSystem.find(ems_id) if ems_id + zone = ems.try(:zone) || MiqServer.my_server.zone perf_capture_health_check(zone) - targets = Metric::Targets.capture_targets(zone) + targets = Metric::Targets.capture_targets(ems || zone) targets_by_rollup_parent = calc_targets_by_rollup_parent(targets) target_options = calc_target_options(zone, targets_by_rollup_parent) @@ -65,6 +66,7 @@ def self.perf_capture_gap(start_time, end_time, zone_id = nil) _log.info("Queueing performance capture for range: [#{start_time} - #{end_time}]...") zone = Zone.find(zone_id) if zone_id + zone ||= MiqServer.my_server.zone targets = Metric::Targets.capture_targets(zone, :exclude_storages => true) targets.each { |target| target.perf_capture_queue('historical', :start_time => start_time, :end_time => end_time, :zone => zone) } diff --git a/app/models/metric/targets.rb b/app/models/metric/targets.rb index 3d144ce8a87a..d5b445200129 100644 --- a/app/models/metric/targets.rb +++ b/app/models/metric/targets.rb @@ -146,11 +146,18 @@ def self.capture_vm_targets(emses, hosts) # If a Cluster, standalone Host, or Storage is not enabled, skip it. # If a Cluster is enabled, capture all of its Hosts. # If a Host is enabled, capture all of its Vms. - def self.capture_targets(zone = nil, options = {}) - zone = MiqServer.my_server.zone if zone.nil? - zone = Zone.find(zone) if zone.kind_of?(Integer) - zone.ext_management_systems.flat_map do |ems| - capture_ems_targets(ems, options) || [] + # + # We used to run perf captures by zone, now we are running them by ems. + # For new code, please pass an ems into zone_or_ems + # + # @param zone_or_ems [Zone, ExtManagementSystem] + def self.capture_targets(zone_or_ems, options = {}) + if zone_or_ems.kind_of?(ExtManagementSystem) + capture_ems_targets(zone_or_ems, options) || [] + else + zone_or_ems.ext_management_systems.flat_map do |ems| + capture_ems_targets(ems, options) || [] + end end end end diff --git a/spec/models/metric_spec.rb b/spec/models/metric_spec.rb index e44b44370c7e..1f4c69c8b285 100644 --- a/spec/models/metric_spec.rb +++ b/spec/models/metric_spec.rb @@ -50,12 +50,12 @@ context "executing capture_targets" do it "should find enabled targets" do - targets = Metric::Targets.capture_targets + targets = Metric::Targets.capture_targets(@ems_vmware.reload) assert_infra_targets_enabled targets, %w(ManageIQ::Providers::Vmware::InfraManager::Vm ManageIQ::Providers::Vmware::InfraManager::Host ManageIQ::Providers::Vmware::InfraManager::Host ManageIQ::Providers::Vmware::InfraManager::Vm ManageIQ::Providers::Vmware::InfraManager::Host Storage) end it "should find enabled targets excluding storages" do - targets = Metric::Targets.capture_targets(nil, :exclude_storages => true) + targets = Metric::Targets.capture_targets(@ems_vmware.reload, :exclude_storages => true) assert_infra_targets_enabled targets, %w(ManageIQ::Providers::Vmware::InfraManager::Vm ManageIQ::Providers::Vmware::InfraManager::Host ManageIQ::Providers::Vmware::InfraManager::Host ManageIQ::Providers::Vmware::InfraManager::Vm ManageIQ::Providers::Vmware::InfraManager::Host) end end @@ -63,7 +63,7 @@ context "executing perf_capture_timer" do before do stub_settings_merge(:performance => {:history => {:initial_capture_days => 7}}) - Metric::Capture.perf_capture_timer + Metric::Capture.perf_capture_timer(@ems_vmware.id) end let(:expected_queue_items) do @@ -79,12 +79,12 @@ it "should queue up enabled targets" do expect(MiqQueue.group(:class_name, :method_name).count).to eq(expected_queue_items) - assert_metric_targets + assert_metric_targets(Metric::Targets.capture_targets(@ems_vmware.reload)) end context "executing capture_targets for realtime targets with parent objects" do before do - @expected_targets = Metric::Targets.capture_targets + @expected_targets = Metric::Targets.capture_targets(@ems_vmware) end it "should create tasks and queue callbacks" do @@ -120,7 +120,7 @@ end it "calling perf_capture_timer when existing capture messages are on the queue should merge messages and append new task id to cb args" do - Metric::Capture.perf_capture_timer + Metric::Capture.perf_capture_timer(@ems_vmware.id) @vmware_clusters.each do |cluster| expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) } next if expected_hosts.empty? @@ -162,14 +162,14 @@ messages = MiqQueue.where(:class_name => "Host", :method_name => 'capture_metrics_realtime') messages.each { |m| m.update_attribute(:state, "dequeue") } - Metric::Capture.perf_capture_timer + Metric::Capture.perf_capture_timer(@ems_vmware.id) messages = MiqQueue.where(:class_name => "Host", :method_name => 'capture_metrics_realtime') messages.each { |m| expect(m.lock_version).to eq(1) } end it "calling perf_capture_timer a second time should create another task with the correct time window" do - Metric::Capture.perf_capture_timer + Metric::Capture.perf_capture_timer(@ems_vmware.id) @vmware_clusters.each do |cluster| expected_hosts = cluster.hosts.select { |h| @expected_targets.include?(h) } @@ -191,10 +191,21 @@ Metric::Capture.perf_capture_gap(t - 7.days, t - 5.days) end - it "should queue up enabled targets for historical" do + it "should queue up enabled targets for historical by ems" do expect(MiqQueue.count).to eq(10) - expected_targets = Metric::Targets.capture_targets(nil, :exclude_storages => true) + expected_targets = Metric::Targets.capture_targets(@ems_vmware.reload, :exclude_storages => true) + expected = expected_targets.flat_map { |t| [[t, "historical"]] * 2 } # Vm, Host, Host, Vm, Host + + selected = queue_intervals(MiqQueue.all) + + expect(selected).to match_array(expected) + end + + it "should queue up enabled targets for historical by zone" do + expect(MiqQueue.count).to eq(10) + + expected_targets = Metric::Targets.capture_targets(@ems_vmware.zone, :exclude_storages => true) expected = expected_targets.flat_map { |t| [[t, "historical"]] * 2 } # Vm, Host, Host, Vm, Host selected = queue_intervals(MiqQueue.all) @@ -1032,7 +1043,7 @@ context "executing capture_targets" do it "should find enabled targets" do - targets = Metric::Targets.capture_targets + targets = Metric::Targets.capture_targets(@ems_openstack) assert_cloud_targets_enabled targets, %w(ManageIQ::Providers::Openstack::CloudManager::Vm ManageIQ::Providers::Openstack::CloudManager::Vm ManageIQ::Providers::Openstack::CloudManager::Vm ManageIQ::Providers::Openstack::CloudManager::Vm ManageIQ::Providers::Openstack::CloudManager::Vm) end end @@ -1040,11 +1051,11 @@ context "executing perf_capture_timer" do before do stub_settings(:performance => {:history => {:initial_capture_days => 7}}) - Metric::Capture.perf_capture_timer + Metric::Capture.perf_capture_timer(@ems_openstack.id) end it "should queue up enabled targets" do - expected_targets = Metric::Targets.capture_targets + expected_targets = Metric::Targets.capture_targets(@ems_openstack) expect(MiqQueue.group(:method_name).count).to eq('perf_capture_realtime' => expected_targets.count, 'perf_capture_historical' => expected_targets.count * 8, 'destroy_older_by_condition' => 1) @@ -1318,7 +1329,7 @@ def assert_perf_capture_now(target, mode) end end - def assert_metric_targets(expected_targets = Metric::Targets.capture_targets) + def assert_metric_targets(expected_targets) expected = expected_targets.flat_map do |t| # Storage is hourly only # Non-storage historical is expecting 7 days back, plus partial day = 8