diff --git a/shared_state.go b/shared_state.go index a4d9de2..729d627 100644 --- a/shared_state.go +++ b/shared_state.go @@ -356,19 +356,12 @@ func sharedStateIDs(cfg *Config) []string { return ids } -// breakerLocal is one id's local breaker state, mirroring requestHealthState. -type breakerLocal struct { - openUntil time.Time - backoff time.Duration - consecutiveFailures int - open bool -} - -// sharedBreaker is a per-id circuit breaker whose trips are shared fleet-wide through the store. With a nil loop it is purely local. +// sharedBreaker is the fleet arm of a per-id circuit breaker. Callers keep their own local breaker and layer sharedOpen on top. +// With a nil loop it never touches the store and never reports open. type sharedBreaker struct { loop *sharedStateLoop nowFn func() time.Time - local map[string]*breakerLocal + fails map[string]int openUntil map[string]int64 opensSeen map[string]int64 failsSeen map[string]int64 @@ -384,12 +377,12 @@ type sharedBreaker struct { mu sync.Mutex } -// newSharedBreaker returns a breaker over ids; loop may be nil for a local-only breaker. +// newSharedBreaker returns a breaker over ids; loop may be nil for a no-op breaker. func newSharedBreaker(loop *sharedStateLoop, kind string, ids []string, threshold int, openBase, openMax, failWindow time.Duration) *sharedBreaker { b := &sharedBreaker{ loop: loop, nowFn: time.Now, kind: kind, ids: ids, 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), failAccAt: make(map[string]time.Time), + fails: make(map[string]int), 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), } b.keyList = make([]string, 0, sharedBreakerKeysPerID*len(ids)) for _, id := range ids { @@ -424,29 +417,7 @@ func (b *sharedBreaker) apply(vals []int64) { } } -func (b *sharedBreaker) localFor(id string) *breakerLocal { - st, ok := b.local[id] - if !ok { - st = &breakerLocal{} - b.local[id] = st - } - return st -} - -// isOpen reports whether the local or the last-refreshed shared breaker is open. It never calls the store. -func (b *sharedBreaker) isOpen(id string) bool { - b.loop.touch() - b.mu.Lock() - defer b.mu.Unlock() - now := b.nowFn() - if st, ok := b.local[id]; ok && st.open && now.Before(st.openUntil) { - return true - } - return b.openUntil[id] > now.UnixMilli() -} - -// sharedOpen reports whether the last-refreshed fleet state says id is open, ignoring this breaker's local arm. -// Callers with their own local breaker use it to layer the fleet view on top. It never calls the store. +// sharedOpen reports whether the last-refreshed fleet state says id is open. It never calls the store. func (b *sharedBreaker) sharedOpen(id string) bool { b.loop.touch() b.mu.Lock() @@ -461,10 +432,10 @@ func (b *sharedBreaker) viewOpen(id string, now time.Time) bool { return b.openUntil[id] > now.UnixMilli() } -// recordFailure updates local state and queues the failure for the loop to flush. +// recordFailure counts the failure since the last success and queues it for the loop to flush. func (b *sharedBreaker) recordFailure(id string) { b.mu.Lock() - b.recordFailureLocal(b.localFor(id), b.nowFn()) + b.fails[id]++ if b.loop != nil { if b.failAcc[id] == 0 { b.failAccAt[id] = b.nowFn() @@ -475,31 +446,6 @@ func (b *sharedBreaker) recordFailure(id string) { b.loop.touch() } -// recordFailureLocal is requestHealthTracker.record's failure arm with this breaker's tunables. -func (b *sharedBreaker) recordFailureLocal(st *breakerLocal, now time.Time) { - if st.open && now.Before(st.openUntil) { - return - } - if st.open { - if st.backoff <= 0 { - st.backoff = b.openBase - } else { - st.backoff *= 2 - } - if st.backoff > b.openMax { - st.backoff = b.openMax - } - st.openUntil = now.Add(st.backoff) - return - } - st.consecutiveFailures++ - if st.consecutiveFailures >= b.threshold { - st.open = true - st.backoff = b.openBase - st.openUntil = now.Add(st.backoff) - } -} - func (b *sharedBreaker) pending() bool { b.mu.Lock() defer b.mu.Unlock() @@ -595,20 +541,16 @@ func (b *sharedBreaker) openDuration(opens int64) time.Duration { return d } -// recordSuccess resets local state and clears the fleet failure count, open mark and open count when any may exist. +// recordSuccess resets the failure count since the last success and clears the fleet failure count, open mark and open count when any may exist. func (b *sharedBreaker) recordSuccess(id string) { b.mu.Lock() - st := b.localFor(id) - dirty := st.consecutiveFailures > 0 || b.failsSeen[id] > 0 || b.opensSeen[id] > 0 + dirty := b.fails[id] > 0 || b.failsSeen[id] > 0 || b.opensSeen[id] > 0 // A success closes the fleet breaker too, as it closes the local one. wasOpen := b.openUntil[id] > b.nowFn().UnixMilli() opensSeen := b.opensSeen[id] > 0 delete(b.failAcc, id) delete(b.failAccAt, id) - st.open = false - st.consecutiveFailures = 0 - st.backoff = 0 - st.openUntil = time.Time{} + delete(b.fails, id) b.mu.Unlock() if b.loop == nil || (!dirty && !wasOpen) { return diff --git a/shared_state_failover_test.go b/shared_state_failover_test.go index 73ca6c9..d736625 100644 --- a/shared_state_failover_test.go +++ b/shared_state_failover_test.go @@ -1,6 +1,7 @@ package traefikllmgateway import ( + "errors" "testing" "time" ) @@ -65,6 +66,18 @@ func TestSharedRequestBreakerAcrossGateways(t *testing.T) { } } +func TestSharedRequestBreakerLocalWhileStoreDown(t *testing.T) { + a, _, store, _, _ := sharedGatewayPair(t, nil) + store.setFail(errors.New("redis down")) + for i := 0; i < requestBreakerFailureThreshold; i++ { + a.failoverHealth.record("alpha", false) + } + a.sharedState.refresh() + if a.failoverHealth.healthy("alpha") { + t.Fatal("A healthy after its own threshold failures with the store down, want the local breaker open") + } +} + func TestSharedRequestBreakerClosesAfterWindow(t *testing.T) { a, b, _, clk, _ := sharedGatewayPair(t, nil) for i := 0; i < requestBreakerFailureThreshold; i++ { diff --git a/shared_state_test.go b/shared_state_test.go index eaa5c3e..7ea69d1 100644 --- a/shared_state_test.go +++ b/shared_state_test.go @@ -134,20 +134,20 @@ func TestSharedBreakerTripsFleetWide(t *testing.T) { a.recordFailure("p") la.refresh() lc.refresh() - if c.isOpen("p") { + if c.sharedOpen("p") { t.Fatal("C open after 2 fleet failures, want closed") } b.recordFailure("p") lb.refresh() - if c.isOpen("p") { - t.Fatal("C open before refresh, isOpen must not read the store") + if c.sharedOpen("p") { + t.Fatal("C open before refresh, sharedOpen must not read the store") } lc.refresh() - if !c.isOpen("p") { + if !c.sharedOpen("p") { t.Fatal("C closed after threshold failures and a refresh, want open") } clk.advance(testBrkOpenBase + time.Second) - if c.isOpen("p") { + if c.sharedOpen("p") { t.Fatal("C still open after the open window") } } @@ -190,13 +190,13 @@ func TestSharedBreakerSuccessResets(t *testing.T) { a2.recordFailure("p") a2.loop.refresh() lc.refresh() - if c.isOpen("p") { + if c.sharedOpen("p") { t.Fatal("C open after success reset then 2 failures, want closed") } a2.recordFailure("p") a2.loop.refresh() lc.refresh() - if !c.isOpen("p") { + if !c.sharedOpen("p") { t.Fatal("C closed after 3 failures since the reset, want open") } } @@ -254,33 +254,6 @@ func TestSharedBreakerOpenDurationTable(t *testing.T) { } } -func TestSharedBreakerLocalOnlyMatchesRequestHealth(t *testing.T) { - clk := &sharedClock{t: time.Unix(1_800_000_000, 0)} - b := newSharedBreaker(nil, "req", []string{"p"}, requestBreakerFailureThreshold, requestBreakerOpenDuration, requestBreakerMaxOpenDuration, time.Minute) - b.nowFn = clk.now - ref := newRequestHealthTracker() - ref.nowFn = clk.now - steps := []struct { - success bool - advance time.Duration - }{ - {false, 0}, {false, 0}, {false, 0}, {false, time.Second}, {false, 31 * time.Second}, - {false, 0}, {false, 61 * time.Second}, {true, 0}, {false, 0}, {false, 0}, {false, 0}, - } - for i, s := range steps { - clk.advance(s.advance) - if s.success { - b.recordSuccess("p") - } else { - b.recordFailure("p") - } - ref.record("p", s.success) - if got, want := !b.isOpen("p"), ref.healthy("p"); got != want { - t.Fatalf("step %d: healthy = %v, want %v", i, got, want) - } - } -} - func inflightEnv(maxReq time.Duration) (*sharedRecStore, *sharedClock, func() (*sharedStateLoop, *sharedInflight)) { store, clk := sharedTestEnv() return store, clk, func() (*sharedStateLoop, *sharedInflight) { @@ -400,7 +373,7 @@ func TestSharedStateStoreErrorFallsBackLocal(t *testing.T) { } la.refresh() lc.refresh() - if !c.isOpen("p") { + if !c.sharedOpen("p") { t.Fatal("setup: C should see the shared open") } @@ -410,22 +383,14 @@ func TestSharedStateStoreErrorFallsBackLocal(t *testing.T) { if !lc.stale() { t.Error("loop not marked stale after failed refreshes") } - if !c.isOpen("p") { + if !c.sharedOpen("p") { t.Error("last-known shared open dropped before 3 failed refreshes") } lc.refresh() - if c.isOpen("p") { + if c.sharedOpen("p") { t.Error("shared open still used after 3 failed refreshes, want absent") } - // Local breaker keeps working while the store is down. - for i := 0; i < 3; i++ { - c.recordFailure("p") - } - if !c.isOpen("p") { - t.Error("local breaker did not open while store is down") - } - store.setFail(nil) lc.refresh() if lc.stale() { @@ -458,7 +423,7 @@ func TestSharedStateOffNoKeys(t *testing.T) { for i := 0; i < 4; i++ { b.recordFailure("p") b.recordSuccess("p") - b.isOpen("p") + b.sharedOpen("p") } in.begin("p")() in.shared("p") @@ -557,7 +522,7 @@ func TestSharedStateLoopLifecycle(t *testing.T) { l.mu.Unlock() // A new use restarts it; close stops it for good and blocks restarts. - b.isOpen("p") + b.sharedOpen("p") l.mu.Lock() stop, done := l.stop, l.done l.mu.Unlock() @@ -572,7 +537,7 @@ func TestSharedStateLoopLifecycle(t *testing.T) { case <-time.After(2 * time.Second): t.Fatal("loop did not stop on close") } - b.isOpen("p") + b.sharedOpen("p") l.mu.Lock() if l.running { t.Error("closed loop restarted") diff --git a/tools/yaegi-check/shared_state.go b/tools/yaegi-check/shared_state.go index ea34d1f..fac2e76 100644 --- a/tools/yaegi-check/shared_state.go +++ b/tools/yaegi-check/shared_state.go @@ -37,15 +37,15 @@ const ssProbeSource = `func sharedStateProbe() string { } la.refresh() lb.refresh() - if !b.isOpen("p") { + if !b.sharedOpen("p") { return "breaker: second instance closed after the first tripped it and one refresh" } - if b.isOpen("other") { + if b.sharedOpen("other") { return "breaker: a trip on one id opened another" } b.recordSuccess("p") lb.refresh() - if b.isOpen("p") { + if b.sharedOpen("p") { return "breaker: second instance still open after its own success" }