fix(shared-state): drop breaker deltas older than the fail window

During a Redis outage the queued failure deltas kept a replaced
gateway's loop alive for ever. A queued failure older than the fail
window cannot change the fleet count, so the breaker now drops it.
This commit is contained in:
2026-11-26 04:10:29 +00:00
parent 5cb01e2c4b
commit a9468f5d51
2 changed files with 52 additions and 2 deletions
+22 -2
View File
@@ -334,6 +334,7 @@ type sharedBreaker struct {
opensSeen map[string]int64 opensSeen map[string]int64
failsSeen map[string]int64 failsSeen map[string]int64
failAcc map[string]int64 failAcc map[string]int64
failAccAt map[string]time.Time
kind string kind string
ids []string ids []string
openBase time.Duration openBase time.Duration
@@ -348,7 +349,7 @@ func newSharedBreaker(loop *sharedStateLoop, kind string, ids []string, threshol
b := &sharedBreaker{ b := &sharedBreaker{
loop: loop, nowFn: time.Now, kind: kind, ids: ids, loop: loop, nowFn: time.Now, kind: kind, ids: ids,
threshold: threshold, openBase: openBase, openMax: openMax, failWindow: failWindow, threshold: threshold, openBase: openBase, openMax: openMax, failWindow: failWindow,
local: make(map[string]*breakerLocal), openUntil: make(map[string]int64), opensSeen: make(map[string]int64), failsSeen: make(map[string]int64), failAcc: make(map[string]int64), local: make(map[string]*breakerLocal), openUntil: make(map[string]int64), opensSeen: make(map[string]int64), failsSeen: make(map[string]int64), failAcc: make(map[string]int64), failAccAt: make(map[string]time.Time),
} }
if loop != nil { if loop != nil {
b.nowFn = loop.nowFn b.nowFn = loop.nowFn
@@ -425,6 +426,9 @@ func (b *sharedBreaker) recordFailure(id string) {
b.mu.Lock() b.mu.Lock()
b.recordFailureLocal(b.localFor(id), b.nowFn()) b.recordFailureLocal(b.localFor(id), b.nowFn())
if b.loop != nil { if b.loop != nil {
if b.failAcc[id] == 0 {
b.failAccAt[id] = b.nowFn()
}
b.failAcc[id]++ b.failAcc[id]++
} }
b.mu.Unlock() b.mu.Unlock()
@@ -459,13 +463,26 @@ func (b *sharedBreaker) recordFailureLocal(st *breakerLocal, now time.Time) {
func (b *sharedBreaker) pending() bool { func (b *sharedBreaker) pending() bool {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
b.pruneStaleLocked(b.nowFn())
return len(b.failAcc) > 0 return len(b.failAcc) > 0
} }
// pruneStaleLocked drops queued failures older than the fail window: the fleet count they would join has expired,
// and keeping them would stop an idle loop during a Redis outage.
func (b *sharedBreaker) pruneStaleLocked(now time.Time) {
for id, at := range b.failAccAt {
if now.Sub(at) > b.failWindow {
delete(b.failAcc, id)
delete(b.failAccAt, id)
}
}
}
// flush adds the queued failures to the fleet counts. An instance whose flush leaves the count at or above the // flush adds the queued failures to the fleet counts. An instance whose flush leaves the count at or above the
// threshold opens the breaker unless its last refresh already shows it open. // threshold opens the breaker unless its last refresh already shows it open.
func (b *sharedBreaker) flush(l *sharedStateLoop, _ time.Time) error { func (b *sharedBreaker) flush(l *sharedStateLoop, now time.Time) error {
b.mu.Lock() b.mu.Lock()
b.pruneStaleLocked(now)
ids := make([]string, 0, len(b.failAcc)) ids := make([]string, 0, len(b.failAcc))
deltas := make([]int64, 0, len(b.failAcc)) deltas := make([]int64, 0, len(b.failAcc))
for id, n := range b.failAcc { for id, n := range b.failAcc {
@@ -491,8 +508,10 @@ func (b *sharedBreaker) flush(l *sharedStateLoop, _ time.Time) error {
for i, id := range ids { for i, id := range ids {
if left := b.failAcc[id] - deltas[i]; left > 0 { if left := b.failAcc[id] - deltas[i]; left > 0 {
b.failAcc[id] = left b.failAcc[id] = left
b.failAccAt[id] = now
} else { } else {
delete(b.failAcc, id) delete(b.failAcc, id)
delete(b.failAccAt, id)
} }
} }
b.mu.Unlock() b.mu.Unlock()
@@ -545,6 +564,7 @@ func (b *sharedBreaker) recordSuccess(id string) {
wasOpen := b.openUntil[id] > b.nowFn().UnixMilli() wasOpen := b.openUntil[id] > b.nowFn().UnixMilli()
opensSeen := b.opensSeen[id] > 0 opensSeen := b.opensSeen[id] > 0
delete(b.failAcc, id) delete(b.failAcc, id)
delete(b.failAccAt, id)
st.open = false st.open = false
st.consecutiveFailures = 0 st.consecutiveFailures = 0
st.backoff = 0 st.backoff = 0
+30
View File
@@ -294,3 +294,33 @@ func TestSharedInflightEndRestartsIdleStoppedLoop(t *testing.T) {
} }
l.close() l.close()
} }
func TestSharedBreakerDropsDeltasPastFailWindow(t *testing.T) {
store, clk := sharedTestEnv()
l := sharedTestLoop(store, clk)
b := testBreaker(l)
store.setFailIncr(errors.New("redis down"))
b.recordFailure("p")
l.refresh()
if !b.pending() {
t.Fatal("delta dropped inside the fail window")
}
clk.advance(testBrkWindow + time.Second)
if b.pending() {
t.Error("failure delta older than the fail window still pending")
}
}
func TestSharedStateIdleStopsDuringOutageAfterFailWindow(t *testing.T) {
store, clk := sharedTestEnv()
l := newSharedStateLoop(store, nil, nil)
l.nowFn = clk.now
l.every = 5 * time.Millisecond
b := testBreaker(l)
store.setFailIncr(errors.New("redis down"))
b.recordFailure("p")
waitLoop(t, l, true)
clk.advance(sharedStateIdleStop + time.Second)
waitLoop(t, l, false)
l.close()
}