Skip to content

Commit cee89e1

Browse files
committed
Tolerate missing valkey socket in RedisStore
When valkey is stopped ahead of cloud_controller_ng draining during an api-group rolling update, its unix socket is removed while CC-NG still serves traffic. Every request then hit RedisStore on the dead socket and raised Redis::CannotConnectError, which surfaced as a 500 with a leaked backtrace. Wrap the RedisStore metrics writes so connection errors are logged and swallowed — a metrics write must never fail a user request. Genuine errors (e.g. Redis::CommandError) still propagate.
1 parent 5a84938 commit cee89e1

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

‎lib/cloud_controller/metrics/statsd_updater.rb‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,32 @@ def initialize(socket)
133133
end
134134
@prefix = 'metrics'
135135
@request_outstanding_gauge_key = "#{@prefix}:cc.requests.outstanding.gauge"
136-
@redis.set(@request_outstanding_gauge_key, 0)
136+
tolerate_connection_errors { @redis.set(@request_outstanding_gauge_key, 0) }
137137
end
138138

139139
def increment_request_outstanding_gauge
140-
@redis.incr(@request_outstanding_gauge_key)
140+
tolerate_connection_errors { @redis.incr(@request_outstanding_gauge_key) }
141141
end
142142

143143
def decrement_request_outstanding_gauge
144-
@redis.decr(@request_outstanding_gauge_key)
144+
tolerate_connection_errors { @redis.decr(@request_outstanding_gauge_key) }
145+
end
146+
147+
private
148+
149+
# A metrics write must never fail a user request. When valkey is
150+
# unreachable (e.g. its socket is removed as it is stopped ahead of
151+
# cloud_controller_ng draining during an api-group update), swallow the
152+
# connection error and let the request complete.
153+
def tolerate_connection_errors
154+
yield
155+
rescue Redis::BaseConnectionError => e
156+
logger.warn("statsd_updater.redis_store.connection_error: #{e.class}: #{e.message}")
157+
nil
158+
end
159+
160+
def logger
161+
@logger ||= Steno.logger('cc.metrics.statsd_updater.redis_store')
145162
end
146163
end
147164
end

‎spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ module VCAP::CloudController::Metrics
378378
StatsdUpdater::RedisStore.new(redis_socket)
379379
end
380380
end
381+
382+
context 'when valkey is unreachable' do
383+
before do
384+
allow(redis).to receive(:set).and_raise(Redis::CannotConnectError)
385+
end
386+
387+
it 'does not raise' do
388+
expect { StatsdUpdater::RedisStore.new(redis_socket) }.not_to raise_error
389+
end
390+
end
381391
end
382392

383393
it 'increments the gauge in Redis' do
@@ -389,6 +399,40 @@ module VCAP::CloudController::Metrics
389399
allow(redis).to receive(:decr).with('metrics:cc.requests.outstanding.gauge').and_return(-1)
390400
expect(redis_store.decrement_request_outstanding_gauge).to eq(-1)
391401
end
402+
403+
context 'when valkey is unreachable' do
404+
before do
405+
allow(redis).to receive(:incr).and_raise(Redis::CannotConnectError.new('connect(2) for valkey.sock'))
406+
allow(redis).to receive(:decr).and_raise(Redis::TimeoutError)
407+
end
408+
409+
it 'does not raise when incrementing, returning nil' do
410+
expect(redis_store.increment_request_outstanding_gauge).to be_nil
411+
end
412+
413+
it 'does not raise when decrementing, returning nil' do
414+
expect(redis_store.decrement_request_outstanding_gauge).to be_nil
415+
end
416+
417+
it 'logs the connection error' do
418+
logger = instance_double(Steno::Logger, warn: nil)
419+
allow(Steno).to receive(:logger).and_return(logger)
420+
421+
redis_store.increment_request_outstanding_gauge
422+
423+
expect(logger).to have_received(:warn).with(/Redis::CannotConnectError/)
424+
end
425+
end
426+
427+
context 'when Redis raises a non-connection error' do
428+
before do
429+
allow(redis).to receive(:decr).and_raise(Redis::CommandError)
430+
end
431+
432+
it 'propagates the error' do
433+
expect { redis_store.decrement_request_outstanding_gauge }.to raise_error(Redis::CommandError)
434+
end
435+
end
392436
end
393437
end
394438
end

0 commit comments

Comments
 (0)