Skip to content

Commit

Permalink
Merge pull request #19506 from kbrock/cu_capture_gap
Browse files Browse the repository at this point in the history
metrics capture gap by ems
  • Loading branch information
agrare authored Nov 19, 2019
2 parents 9d5d833 + 2fa123c commit 45e4ea2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
44 changes: 31 additions & 13 deletions app/models/metric/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,45 @@ 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)
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}]...")

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)
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

_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)
Expand Down Expand Up @@ -202,8 +220,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}")
Expand Down
6 changes: 2 additions & 4 deletions app/models/metric/ci_mixin/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,16 +31,15 @@ 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
zone = zone.name if zone.respond_to?(:name)
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
Expand Down
14 changes: 3 additions & 11 deletions app/models/metric/targets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/models/metric/capture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tools/metrics_capture_gap.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/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}")
puts 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")

0 comments on commit 45e4ea2

Please sign in to comment.