Skip to content

Commit

Permalink
Merge pull request #15380 from kbrock/cu_rollup_redux
Browse files Browse the repository at this point in the history
[cap&u] Condense perf_capture_timer logic
  • Loading branch information
chrisarcand authored Jul 31, 2017
2 parents 5aba12b + 69b974c commit ab91e89
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions app/models/metric/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def self.perf_capture_timer(zone = nil)
targets = Metric::Targets.capture_targets(zone)

targets_by_rollup_parent = calc_targets_by_rollup_parent(targets)
tasks_by_rollup_parent = calc_tasks_by_rollup_parent(targets_by_rollup_parent)
target_options = calc_target_options(zone, targets, targets_by_rollup_parent, tasks_by_rollup_parent)
target_options = calc_target_options(zone, targets_by_rollup_parent)
targets = filter_perf_capture_now(targets, target_options)
queue_captures(targets, target_options)

Expand Down Expand Up @@ -128,32 +127,40 @@ def self.perf_capture_health_check(zone)
end
private_class_method :perf_capture_health_check

# Collect realtime targets and group them by their rollup parent
#
# 1. Only calculate rollups for Hosts
# 2. Some Hosts have an EmsCluster as a parent, others have none.
# 3. Only Hosts with a parent are rolled up.
#
# @returns Hash<String,Array<Host>>
# e.g.: {"EmsCluster:4"=>[Host:4], "EmsCluster:5"=>[Host:1, Host:2]}
def self.calc_targets_by_rollup_parent(targets)
# Collect realtime targets and group them by their rollup parent, e.g. {"EmsCluster:4"=>[Host:4], "EmsCluster:5"=>[Host:1, Host:2]}
targets_by_rollup_parent = targets.inject({}) do |h, target|
next(h) unless target.kind_of?(Host) && perf_capture_now?(target)

interval_name = perf_target_to_interval_name(target)
next unless interval_name == "realtime"

target.perf_rollup_parents(interval_name).to_a.compact.each do |parent|
realtime_targets = targets.select do |target|
target.kind_of?(Host) &&
perf_target_to_interval_name(target) == "realtime" &&
perf_capture_now?(target)
end
realtime_targets.each_with_object({}) do |target, h|
target.perf_rollup_parents("realtime").to_a.compact.each do |parent|
pkey = "#{parent.class}:#{parent.id}"
h[pkey] ||= []
h[pkey] << "#{target.class}:#{target.id}"
(h[pkey] ||= []) << target
end

h
end
targets_by_rollup_parent
end
private_class_method :calc_targets_by_rollup_parent

def self.calc_tasks_by_rollup_parent(targets_by_rollup_parent)
# Determine queue options for each target
# Is only generating options for Vmware Hosts, which have a task for rollups.
# The rest just set the zone
def self.calc_target_options(zone, targets_by_rollup_parent)
task_end_time = Time.now.utc.iso8601
default_task_start_time = 1.hour.ago.utc.iso8601

target_options = Hash.new { |h, k| h[k] = {:zone => zone} }
# Create a new task for each rollup parent
tasks_by_rollup_parent = targets_by_rollup_parent.keys.inject({}) do |h, pkey|
# mark each target with the rollup parent
targets_by_rollup_parent.each_with_object(target_options) do |(pkey, targets), h|
name = "Performance rollup for #{pkey}"
prev_task = MiqTask.where(:identifier => pkey).order("id DESC").first
task_start_time = prev_task ? prev_task.context_data[:end] : default_task_start_time
Expand All @@ -168,37 +175,19 @@ def self.calc_tasks_by_rollup_parent(targets_by_rollup_parent)
:start => task_start_time,
:end => task_end_time,
:parent => pkey,
:targets => targets_by_rollup_parent[pkey],
:targets => targets.map { |target| "#{target.class}:#{target.id}" },
:complete => [],
:interval => "realtime"
}
)
_log.info "Created task id: [#{task.id}] for: [#{pkey}] with targets: #{targets_by_rollup_parent[pkey].inspect} for time range: [#{task_start_time} - #{task_end_time}]"
h[pkey] = task
h
end

tasks_by_rollup_parent
end
private_class_method :calc_tasks_by_rollup_parent

def self.calc_target_options(zone, targets, targets_by_rollup_parent, tasks_by_rollup_parent)
targets.each_with_object({}) do |target, all_options|
interval_name = perf_target_to_interval_name(target)

options = {:zone => zone}
target.perf_rollup_parents(interval_name).to_a.compact.each do |parent|
if tasks_by_rollup_parent.key?("#{parent.class}:#{parent.id}")
pkey = "#{parent.class}:#{parent.id}"
tkey = "#{target.class}:#{target.id}"
if targets_by_rollup_parent[pkey].include?(tkey)
# FIXME: check that this is still correct
options[:task_id] = tasks_by_rollup_parent[pkey].id
options[:force] = true # Force collection since we've already verified that capture should be done now
end
end
targets.each do |target|
h[target] = {
:task_id => task.id,
:force => true, # Force collection since we've already verified that capture should be done now
:zone => zone,
}
end
all_options[target] = options
end
end
private_class_method :calc_target_options
Expand Down

0 comments on commit ab91e89

Please sign in to comment.