From 25ca045dcadddb741b238b4e0410ef75cfb1ffe5 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Thu, 7 Nov 2019 19:08:13 -0500 Subject: [PATCH 1/4] perf_capture_gap - support ems_id currently the ui sends a zone to collect metric gaps this allows the ui to transition from zone to ems --- app/models/metric/capture.rb | 16 ++++++++++++---- app/models/metric/targets.rb | 14 +++----------- spec/models/metric/capture_spec.rb | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/models/metric/capture.rb b/app/models/metric/capture.rb index df6de689b6a..4ad9ecb7a8a 100644 --- a/app/models/metric/capture.rb +++ b/app/models/metric/capture.rb @@ -63,12 +63,20 @@ def self.perf_capture_timer(ems_id) _log.info("Queueing performance capture...Complete") end - def self.perf_capture_gap(start_time, end_time, zone_id = nil) + def self.perf_capture_gap(start_time, end_time, zone_id = nil, ems_id = nil) _log.info("Queueing performance capture for range: [#{start_time} - #{end_time}]...") - zone = Zone.find(zone_id) if zone_id - 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) } + emses = if ems_id + [ExtManagementSystem.find(ems_id)] + elsif zone_id + Zone.find(zone_id).ems_metrics_collectable + else + MiqServer.my_server.zone.ems_metrics_collectable + end + emses.each do |ems| + targets = Metric::Targets.capture_ems_targets(ems, :exclude_storages => true) + targets.each { |target| target.perf_capture_queue('historical', :start_time => start_time, :end_time => end_time, :zone => zone) } + end _log.info("Queueing performance capture for range: [#{start_time} - #{end_time}]...Complete") end diff --git a/app/models/metric/targets.rb b/app/models/metric/targets.rb index c859fde9cf4..bf8a5f17db7 100644 --- a/app/models/metric/targets.rb +++ b/app/models/metric/targets.rb @@ -21,6 +21,9 @@ def self.capture_ems_targets(ems, options = {}) end end + # 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_infra_targets(emses, options) load_infra_targets_data(emses, options) all_hosts = capture_host_targets(emses) @@ -147,15 +150,4 @@ def self.capture_vm_targets(emses, hosts) enabled_host_ids = hosts.select(&:perf_capture_enabled?).index_by(&:id) emses.flat_map { |e| e.vms.select { |v| enabled_host_ids.key?(v.host_id) && v.state == 'on' && v.supports_capture? } } end - - # 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.ems_metrics_collectable.flat_map do |ems| - capture_ems_targets(ems, options) || [] - end - end end diff --git a/spec/models/metric/capture_spec.rb b/spec/models/metric/capture_spec.rb index ea007ebb989..1f24521c74e 100644 --- a/spec/models/metric/capture_spec.rb +++ b/spec/models/metric/capture_spec.rb @@ -161,7 +161,7 @@ context "executing perf_capture_gap" do before do t = Time.now.utc - Metric::Capture.perf_capture_gap(t - 7.days, t - 5.days) + Metric::Capture.perf_capture_gap(t - 7.days, t - 5.days, nil, @ems_vmware.id) end it "should queue up enabled targets for historical" do From fba1e1fd888b23f5f9c310f3d66efbb214dfacda Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 18 Nov 2019 15:23:04 -0500 Subject: [PATCH 2/4] perf_capture_gap_queue ems_id update callers of perf_capture_gap to also support ems_id --- app/models/metric/capture.rb | 20 +++++++++++++------- tools/metrics_capture_gap.rb | 8 ++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/models/metric/capture.rb b/app/models/metric/capture.rb index 4ad9ecb7a8a..809d58d2d71 100644 --- a/app/models/metric/capture.rb +++ b/app/models/metric/capture.rb @@ -81,17 +81,23 @@ def self.perf_capture_gap(start_time, end_time, zone_id = nil, ems_id = nil) _log.info("Queueing performance capture for range: [#{start_time} - #{end_time}]...Complete") end - def self.perf_capture_gap_queue(start_time, end_time, zone = nil) - item = { + # called by the UI + # @param zone [Zone] zone where the ems resides + # @param ems [ExtManagementSystem] ems to capture collect + # + # pass at least one of these, since we need to specify which ems needs a gap to run + # Prefer to use the ems over the zone for perf_capture_gap + def self.perf_capture_gap_queue(start_time, end_time, zone, ems = nil) + zone ||= ems.zone + + MiqQueue.put( :class_name => name, :method_name => "perf_capture_gap", :role => "ems_metrics_coordinator", :priority => MiqQueue::HIGH_PRIORITY, - :args => [start_time, end_time, zone.try(:id)] - } - item[:zone] = zone.name if zone - - MiqQueue.put(item) + :zone => zone.name, + :args => [start_time, end_time, zone.id, ems&.id] + ) end def self.filter_perf_capture_now(targets, target_options) diff --git a/tools/metrics_capture_gap.rb b/tools/metrics_capture_gap.rb index f617f21165a..d92c24c7fcd 100755 --- a/tools/metrics_capture_gap.rb +++ b/tools/metrics_capture_gap.rb @@ -1,13 +1,13 @@ #!/usr/bin/env ruby require File.expand_path('../config/environment', __dir__) -if ARGV.length != 2 - puts "Usage: rails runner #{$0} start_date end_date" +if ARGV.length < 2 || ARGV.length > 3 + puts "Usage: rails runner #{$PROGRAM_NAME} start_date end_date [ems_id]" puts puts " start_date and end_date must be in iso8601 format (e.g. 2010-02-15T00:00:00Z)." exit 1 end -start_date, end_date = *ARGV +start_date, end_date, ems_id = *ARGV def log(msg) $log.info("MIQ(#{__FILE__}) #{msg}") @@ -15,5 +15,5 @@ def log(msg) end log("Queueing metrics capture for [#{start_date}..#{end_date}]...") -Metric::Capture.perf_capture_gap(Time.parse(start_date), Time.parse(end_date)) +Metric::Capture.perf_capture_gap(Time.parse(start_date).utc, Time.parse(end_date).utc, nil, ems_id&.to_i) log("Queueing metrics capture for [#{start_date}..#{end_date}]...Complete") From 6e0a689cf3c2cfd46f4b95e2917ec506dbfbd6df Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Wed, 13 Nov 2019 16:38:47 -0500 Subject: [PATCH 3/4] metrics: gap use queue_captures --- app/models/metric/capture.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/metric/capture.rb b/app/models/metric/capture.rb index 809d58d2d71..9c8280a2203 100644 --- a/app/models/metric/capture.rb +++ b/app/models/metric/capture.rb @@ -75,7 +75,8 @@ def self.perf_capture_gap(start_time, end_time, zone_id = nil, ems_id = nil) end emses.each do |ems| targets = Metric::Targets.capture_ems_targets(ems, :exclude_storages => true) - targets.each { |target| target.perf_capture_queue('historical', :start_time => start_time, :end_time => end_time, :zone => zone) } + target_options = Hash.new { |_n, _v| {:start_time => start_time, :end_time => end_time, :zone => ems.zone, :interval => 'historical'} } + queue_captures(targets, target_options) end _log.info("Queueing performance capture for range: [#{start_time} - #{end_time}]...Complete") @@ -216,8 +217,8 @@ def self.queue_captures(targets, target_options) use_historical = historical_days != 0 targets.each do |target| - interval_name = perf_target_to_interval_name(target) - options = target_options[target] + options = target_options[target] || {} + interval_name = options[:interval] || perf_target_to_interval_name(target) target.perf_capture_queue(interval_name, options) rescue => err _log.warn("Failed to queue perf_capture for target [#{target.class.name}], [#{target.id}], [#{target.name}]: #{err}") From 2fa123ce1fab9096f5cf76bf9a335cc49c728dbf Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Wed, 13 Nov 2019 16:47:47 -0500 Subject: [PATCH 4/4] metrics: move capture date logic to gap call --- app/models/metric/capture.rb | 5 ++++- app/models/metric/ci_mixin/capture.rb | 6 ++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/models/metric/capture.rb b/app/models/metric/capture.rb index 9c8280a2203..90e73f4e2d9 100644 --- a/app/models/metric/capture.rb +++ b/app/models/metric/capture.rb @@ -64,6 +64,9 @@ def self.perf_capture_timer(ems_id) end def self.perf_capture_gap(start_time, end_time, zone_id = nil, ems_id = nil) + raise ArgumentError, "end_time and start_time must be specified" if start_time.nil? || end_time.nil? + raise _("Start time must be earlier than End time") if start_time > end_time + _log.info("Queueing performance capture for range: [#{start_time} - #{end_time}]...") emses = if ems_id @@ -75,7 +78,7 @@ def self.perf_capture_gap(start_time, end_time, zone_id = nil, ems_id = nil) end emses.each do |ems| targets = Metric::Targets.capture_ems_targets(ems, :exclude_storages => true) - target_options = Hash.new { |_n, _v| {:start_time => start_time, :end_time => end_time, :zone => ems.zone, :interval => 'historical'} } + target_options = Hash.new { |_n, _v| {:start_time => start_time.utc, :end_time => end_time.utc, :zone => ems.zone, :interval => 'historical'} } queue_captures(targets, target_options) end diff --git a/app/models/metric/ci_mixin/capture.rb b/app/models/metric/ci_mixin/capture.rb index 18f3d41ba95..8a21c6a4edb 100644 --- a/app/models/metric/ci_mixin/capture.rb +++ b/app/models/metric/ci_mixin/capture.rb @@ -18,7 +18,6 @@ def ems_for_capture_target end def split_capture_intervals(interval_name, start_time, end_time, threshold = 1.day) - raise _("Start time must be earlier than End time") if start_time > end_time # Create an array of ordered pairs from start_time and end_time so that each ordered pair is contained # within the threshold. Then, reverse it so the newest ordered pair is first: # start_time = 2017/01/01 12:00:00, end_time = 2017/01/04 12:00:00 @@ -32,8 +31,8 @@ def split_capture_intervals(interval_name, start_time, end_time, threshold = 1.d private :split_capture_intervals def perf_capture_queue(interval_name, options = {}) - start_time = options[:start_time]&.utc - end_time = options[:end_time]&.utc + start_time = options[:start_time] + end_time = options[:end_time] priority = options[:priority] || Metric::Capture.interval_priority(interval_name) task_id = options[:task_id] zone = options[:zone] || my_zone @@ -41,7 +40,6 @@ def perf_capture_queue(interval_name, options = {}) ems = ems_for_capture_target raise ArgumentError, "invalid interval_name '#{interval_name}'" unless Metric::Capture::VALID_CAPTURE_INTERVALS.include?(interval_name) - raise ArgumentError, "end_time cannot be specified if start_time is nil" if start_time.nil? && !end_time.nil? raise ArgumentError, "target does not have an ExtManagementSystem" if ems.nil? # cb is the task used to group cluster realtime metrics