Skip to content

Commit 6fb7e31

Browse files
committed
Add STOPPING state for process instances
If a process is STOPPED, but actual_lrps still exist, process instances are set to STOPPING.
1 parent 9621038 commit 6fb7e31

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

lib/cloud_controller/diego/reporters/instances_stats_reporter.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,7 @@ def instances_for_processes(processes)
4747
instances = {}
4848
# Fill in the instances up to the max of desired instances and actual instances
4949
[process.instances, newest_lrp_by_index.length].max.times do |idx|
50-
lrp = newest_lrp_by_index[idx]
51-
instances[idx] = if lrp
52-
{
53-
state: LrpStateTranslator.translate_lrp_state(lrp),
54-
since: nanoseconds_to_seconds(current_time_since_epoch_ns - lrp.since)
55-
}
56-
else
57-
{ state: VCAP::CloudController::Diego::LRP_DOWN }
58-
end
50+
instances[idx] = instance_hash(newest_lrp_by_index[idx], process, current_time_since_epoch_ns)
5951
end
6052

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

6961
attr_reader :bbs_instances_client
7062

63+
def instance_hash(lrp, process, current_time_since_epoch_ns)
64+
if lrp.nil?
65+
{ state: VCAP::CloudController::Diego::LRP_DOWN }
66+
elsif process.stopped?
67+
{ state: app_instance_stopping_state }
68+
else
69+
{
70+
state: LrpStateTranslator.translate_lrp_state(lrp),
71+
since: nanoseconds_to_seconds(current_time_since_epoch_ns - lrp.since)
72+
}
73+
end
74+
end
75+
7176
def get_stats(desired_lrp, process)
7277
log_cache_data, log_cache_errors = envelopes(desired_lrp, process)
7378
stats = formatted_process_stats(log_cache_data, Time.now.utc.to_datetime.rfc3339)
@@ -129,7 +134,7 @@ def handle_no_running_instances(process)
129134
if bbs_instances_client.lrp_instances(process).empty?
130135
[fill_unreported_instances_with_down_instances({}, process, flat: false), []]
131136
else
132-
state = Config.config.get(:app_instance_stopping_state) ? VCAP::CloudController::Diego::LRP_STOPPING : VCAP::CloudController::Diego::LRP_DOWN
137+
state = app_instance_stopping_state
133138
# case when no desired_lrp exists but an actual_lrp
134139
logger.debug("Actual LRP found, setting state to #{state}", process_guid: process.guid)
135140
actual_lrp_info(process, nil, nil, nil, nil, state)
@@ -249,6 +254,10 @@ def port_mapping_to_hash(port_mapping)
249254
port_mapping.send(field_name)
250255
end
251256
end
257+
258+
def app_instance_stopping_state
259+
@app_instance_stopping_state ||= Config.config.get(:app_instance_stopping_state) ? VCAP::CloudController::Diego::LRP_STOPPING : VCAP::CloudController::Diego::LRP_DOWN
260+
end
252261
end
253262
end
254263
end

spec/request/processes_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@
773773
end
774774

775775
describe 'GET /v3/processes/:guid/process_instances' do
776-
let(:process) { VCAP::CloudController::ProcessModel.make(:process, app: app_model) }
776+
let(:process) { VCAP::CloudController::ProcessModel.make(:process, app: app_model, state: VCAP::CloudController::ProcessModel::STARTED) }
777777
let(:two_days_ago_since_epoch_ns) { 2.days.ago.to_f * 1e9 }
778778
let(:two_days_in_seconds) { 60 * 60 * 24 * 2 }
779779
let(:second_in_ns) { 1_000_000_000 }

spec/unit/lib/cloud_controller/diego/reporters/instances_stats_reporter_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Diego
66
RSpec.describe InstancesStatsReporter do
77
subject(:instances_reporter) { InstancesStatsReporter.new(bbs_instances_client, log_cache_client) }
88
let(:app) { AppModel.make }
9-
let(:process) { ProcessModel.make(instances: desired_instances, app: app) }
9+
let(:process) { ProcessModel.make(instances: desired_instances, app: app, state: ProcessModel::STARTED) }
1010
let(:desired_instances) { 1 }
1111
let(:bbs_instances_client) { instance_double(BbsInstancesClient) }
1212
let(:log_cache_client) { instance_double(Logcache::ContainerMetricBatcher) }
@@ -749,6 +749,21 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:)
749749
}
750750
})
751751
end
752+
753+
context 'when the process is in state STOPPED' do
754+
before { process.update(state: ProcessModel::STOPPED) }
755+
756+
it 'returns all instances as STOPPING' do
757+
instances = subject.instances_for_processes([process])
758+
expect(instances).to eq({
759+
process.guid => {
760+
0 => { state: 'STOPPING' },
761+
1 => { state: 'STOPPING' },
762+
2 => { state: 'STOPPING' }
763+
}
764+
})
765+
end
766+
end
752767
end
753768

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

786801
context 'with multiple processes' do
787-
let(:second_process) { ProcessModel.make }
802+
let(:second_process) { ProcessModel.make(state: ProcessModel::STARTED) }
788803
let(:second_process_actual_lrp_0) do
789804
::Diego::Bbs::Models::ActualLRP.new(
790805
actual_lrp_key: ::Diego::Bbs::Models::ActualLRPKey.new(process_guid: second_process.guid + 'version', index: 0),

0 commit comments

Comments
 (0)