Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
HealthManager varz changes
Browse files Browse the repository at this point in the history
- Fixed varz stats keeping frameworks/runtimes around that are no longer in the DB
- Added started apps and instances
- Aggregated queries to reduce number of requests

Change-Id: I03ee8368e1e2f06819f40cb1cc74ced949f8a79f
  • Loading branch information
Vadim Spivak committed Aug 12, 2011
1 parent 97727bf commit 6034ef4
Showing 1 changed file with 63 additions and 39 deletions.
102 changes: 63 additions & 39 deletions health_manager/lib/health_manager.rb
Expand Up @@ -247,8 +247,8 @@ def analyze_app(app_id, droplet_entry, stats)
framework_stats = stats[:frameworks][droplet_entry[:framework]] ||= create_runtime_metrics
runtime_stats = stats[:runtimes][droplet_entry[:runtime]] ||= create_runtime_metrics

framework_stats[:running_apps] += 1
runtime_stats[:running_apps] += 1
framework_stats[:apps] += 1
runtime_stats[:apps] += 1

framework_stats[:crashes] += droplet_entry[:crashes].length
runtime_stats[:crashes] += droplet_entry[:crashes].length
Expand Down Expand Up @@ -304,7 +304,7 @@ def analyze_app(app_id, droplet_entry, stats)
def analyze_all_apps(collect_stats = true)
start = Time.now
instances = crashed = 0
stats = { :running => 0, :down => 0, :frameworks => {}, :runtimes => {} }
stats = {:running => 0, :down => 0, :frameworks => {}, :runtimes => {}}

@droplets.each do |id, droplet_entry|
analyze_app(id, droplet_entry, stats) if collect_stats
Expand All @@ -318,18 +318,9 @@ def analyze_all_apps(collect_stats = true)

if collect_stats
VCAP::Component.varz[:running_instances] = stats[:running]
VCAP::Component.varz[:down_instances] = stats[:down]

[:frameworks, :runtimes].each do |key|
VCAP::Component.varz[key].each do |name, metrics|
metrics.merge!(create_runtime_metrics) unless stats[key].include?(name)
end

stats[key].each do |name, metrics|
(VCAP::Component.varz[key][name] ||= {}).merge!(metrics)
end
end

VCAP::Component.varz[:down_instances] = stats[:down]
VCAP::Component.varz[:running][:frameworks] = stats[:frameworks]
VCAP::Component.varz[:running][:runtimes] = stats[:runtimes]
@logger.info("Analyzed #{stats[:running]} running and #{stats[:down]} down apps in #{elapsed_time_in_ms(start)}")
else
@logger.info("Analyzed #{@droplets.size} apps in #{elapsed_time_in_ms(start)}")
Expand All @@ -338,14 +329,25 @@ def analyze_all_apps(collect_stats = true)

def create_runtime_metrics
{
:running_apps => 0,
:apps => 0,
:crashes => 0,
:running_instances => 0,
:missing_instances => 0,
:flapping_instances => 0
}
end

def create_db_metrics
{
:apps => 0,
:started_apps => 0,
:instances => 0,
:started_instances => 0,
:memory => 0,
:started_memory => 0
}
end

def elapsed_time_in_ms(start)
elapsed_ms = (Time.now - start) * 1000
"#{'%.1f' % elapsed_ms}ms"
Expand Down Expand Up @@ -548,34 +550,49 @@ def update_from_db
@logger.info("Database scan took #{elapsed_time_in_ms(start)} and found #{@droplets.size} apps")

start = Time.now
App.count(:group => "framework").each do |framework, count|
framework_stats = VCAP::Component.varz[:frameworks][framework] ||= {}
framework_stats[:total_apps] = count
end

App.count(:group => "runtime").each do |runtime, count|
runtime_stats = VCAP::Component.varz[:runtimes][runtime] ||= {}
runtime_stats[:total_apps] = count
end
VCAP::Component.varz[:total] = {
:frameworks => {},
:runtimes => {}
}

App.sum(:instances, :group => "framework").each do |framework, count|
framework_stats = VCAP::Component.varz[:frameworks][framework] ||= {}
framework_stats[:total_instances] = count
end
App.count(:group => ["framework", "runtime", "state"]).each do |grouping, count|
framework, runtime, state = grouping

framework_stats = VCAP::Component.varz[:total][:frameworks][framework] ||= create_db_metrics
framework_stats[:apps] += count
framework_stats[:started_apps] += count if state == "STARTED"

App.sum(:instances, :group => "runtime").each do |runtime, count|
runtime_stats = VCAP::Component.varz[:runtimes][runtime] ||= {}
runtime_stats[:total_instances] = count
runtime_stats = VCAP::Component.varz[:total][:runtimes][runtime] ||= create_db_metrics
runtime_stats[:apps] += count
runtime_stats[:started_apps] += count if state == "STARTED"
end

App.sum("instances * memory", :group => "framework").each do |framework, count|
framework_stats = VCAP::Component.varz[:frameworks][framework] ||= {}
framework_stats[:total_memory] = count
App.sum(:instances, :group => ["framework", "runtime", "state"]).each do |grouping, count|
framework, runtime, state = grouping

framework_stats = VCAP::Component.varz[:total][:frameworks][framework] ||= create_db_metrics
framework_stats[:instances] += count
framework_stats[:started_instances] += count if state == "STARTED"

runtime_stats = VCAP::Component.varz[:total][:runtimes][runtime] ||= create_db_metrics
runtime_stats[:instances] += count
runtime_stats[:started_instances] += count if state == "STARTED"
end

App.sum("instances * memory", :group => "runtime").each do |runtime, count|
runtime_stats = VCAP::Component.varz[:runtimes][runtime] ||= {}
runtime_stats[:total_memory] = count
App.sum("instances * memory", :group => ["framework", "runtime", "state"]).each do |grouping, count|
# memory is stored as a string
count = count.to_i
framework, runtime, state = grouping

framework_stats = VCAP::Component.varz[:total][:frameworks][framework] ||= create_db_metrics
framework_stats[:memory] += count
framework_stats[:started_memory] += count if state == "STARTED"


runtime_stats = VCAP::Component.varz[:total][:runtimes][runtime] ||= create_db_metrics
runtime_stats[:memory] += count
runtime_stats[:started_memory] += count if state == "STARTED"
end

@logger.info("Database stat scan took #{elapsed_time_in_ms(start)}")
Expand Down Expand Up @@ -678,8 +695,15 @@ def register_as_component
VCAP::Component.varz[:crashed_instances] = -1
VCAP::Component.varz[:down_instances] = -1

VCAP::Component.varz[:frameworks] = {}
VCAP::Component.varz[:runtimes] = {}
VCAP::Component.varz[:total] = {
:frameworks => {},
:runtimes => {}
}

VCAP::Component.varz[:running] = {
:frameworks => {},
:runtimes => {}
}

VCAP::Component.varz[:nats_latency] = VCAP::RollingMetric.new(60)

Expand Down

0 comments on commit 6034ef4

Please sign in to comment.