Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions lib/cloud_controller/diego/reporters/instances_stats_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ def instances_for_processes(processes)
instances = {}
# Fill in the instances up to the max of desired instances and actual instances
[process.instances, newest_lrp_by_index.length].max.times do |idx|
lrp = newest_lrp_by_index[idx]
instances[idx] = if lrp
{
state: LrpStateTranslator.translate_lrp_state(lrp),
since: nanoseconds_to_seconds(current_time_since_epoch_ns - lrp.since)
}
else
{ state: VCAP::CloudController::Diego::LRP_DOWN }
end
instances[idx] = instance_hash(newest_lrp_by_index[idx], process, current_time_since_epoch_ns)
end

results[process.guid] = instances
Expand All @@ -68,6 +60,19 @@ def instances_for_processes(processes)

attr_reader :bbs_instances_client

def instance_hash(lrp, process, current_time_since_epoch_ns)
if lrp.nil?
{ state: VCAP::CloudController::Diego::LRP_DOWN }
elsif process.stopped?
{ state: app_instance_stopping_state }
else
{
state: LrpStateTranslator.translate_lrp_state(lrp),
since: nanoseconds_to_seconds(current_time_since_epoch_ns - lrp.since)
}
end
end

def get_stats(desired_lrp, process)
log_cache_data, log_cache_errors = envelopes(desired_lrp, process)
stats = formatted_process_stats(log_cache_data, Time.now.utc.to_datetime.rfc3339)
Expand Down Expand Up @@ -129,7 +134,7 @@ def handle_no_running_instances(process)
if bbs_instances_client.lrp_instances(process).empty?
[fill_unreported_instances_with_down_instances({}, process, flat: false), []]
else
state = Config.config.get(:app_instance_stopping_state) ? VCAP::CloudController::Diego::LRP_STOPPING : VCAP::CloudController::Diego::LRP_DOWN
state = app_instance_stopping_state
# case when no desired_lrp exists but an actual_lrp
logger.debug("Actual LRP found, setting state to #{state}", process_guid: process.guid)
actual_lrp_info(process, nil, nil, nil, nil, state)
Expand Down Expand Up @@ -249,6 +254,10 @@ def port_mapping_to_hash(port_mapping)
port_mapping.send(field_name)
end
end

def app_instance_stopping_state
@app_instance_stopping_state ||= Config.config.get(:app_instance_stopping_state) ? VCAP::CloudController::Diego::LRP_STOPPING : VCAP::CloudController::Diego::LRP_DOWN
end
end
end
end
2 changes: 1 addition & 1 deletion spec/request/processes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@
end

describe 'GET /v3/processes/:guid/process_instances' do
let(:process) { VCAP::CloudController::ProcessModel.make(:process, app: app_model) }
let(:process) { VCAP::CloudController::ProcessModel.make(:process, app: app_model, state: VCAP::CloudController::ProcessModel::STARTED) }
let(:two_days_ago_since_epoch_ns) { 2.days.ago.to_f * 1e9 }
let(:two_days_in_seconds) { 60 * 60 * 24 * 2 }
let(:second_in_ns) { 1_000_000_000 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Diego
RSpec.describe InstancesStatsReporter do
subject(:instances_reporter) { InstancesStatsReporter.new(bbs_instances_client, log_cache_client) }
let(:app) { AppModel.make }
let(:process) { ProcessModel.make(instances: desired_instances, app: app) }
let(:process) { ProcessModel.make(instances: desired_instances, app: app, state: ProcessModel::STARTED) }
let(:desired_instances) { 1 }
let(:bbs_instances_client) { instance_double(BbsInstancesClient) }
let(:log_cache_client) { instance_double(Logcache::ContainerMetricBatcher) }
Expand Down Expand Up @@ -749,6 +749,21 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
}
})
end

context 'when the process is in state STOPPED' do
before { process.update(state: ProcessModel::STOPPED) }

it 'returns all instances as STOPPING' do
instances = subject.instances_for_processes([process])
expect(instances).to eq({
process.guid => {
0 => { state: 'STOPPING' },
1 => { state: 'STOPPING' },
2 => { state: 'STOPPING' }
}
})
end
end
end

context 'with multiple actual lrps for the same index' do
Expand Down Expand Up @@ -784,7 +799,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
end

context 'with multiple processes' do
let(:second_process) { ProcessModel.make }
let(:second_process) { ProcessModel.make(state: ProcessModel::STARTED) }
let(:second_process_actual_lrp_0) do
::Diego::Bbs::Models::ActualLRP.new(
actual_lrp_key: ::Diego::Bbs::Models::ActualLRPKey.new(process_guid: second_process.guid + 'version', index: 0),
Expand Down