fix(shared-state): count discovery failures per instance

With redis.sharedState on, every route instance added each failed
discovery refresh to one fleet count. One failed round across the
fleet could then open the discovery breaker.

Each instance now counts its own consecutive failures. Only the
instance whose own count reaches breaker.failureThreshold shares a
trip, and the fleet breaker opens on the first trip.
This commit is contained in:
2026-11-26 06:21:10 +00:00
parent 00bb36d29d
commit ebe38e7527
5 changed files with 36 additions and 7 deletions
+3 -3
View File
@@ -738,9 +738,9 @@ Traefik calls `New()` once per route and runs several pods. Without `redis.share
How it works:
- A breaker counts consecutive failures across all replicas. A success on any replica resets the count and closes an open breaker. A replica whose flush leaves the count at or above the threshold opens the breaker for all replicas, unless its last refresh already shows the breaker open. Two replicas can then open it at nearly the same time. A success also resets the doubling of the open time. Open times and thresholds are the local ones: 3 failures and 30 seconds, doubling to 5 minutes, for the request breaker; the `breaker` block for the discovery breaker; 3 failures and 30 seconds for the decision breaker.
- A breaker counts consecutive failures across all replicas. A success on any replica resets the count and closes an open breaker. A replica whose flush leaves the count at or above the threshold opens the breaker for all replicas, unless its last refresh already shows the breaker open. Two replicas can then open it at nearly the same time. A success also resets the doubling of the open time. Open times and thresholds are the local ones: 3 failures and 30 seconds, doubling to 5 minutes, for the request breaker; the `breaker` block for the discovery breaker, counted on each instance (see below); 3 failures and 30 seconds for the decision breaker.
- A breaker reads as open when its local breaker or the shared breaker is open.
- With `redis.sharedState` on, the discovery `breaker.failureThreshold` counts failed refreshes across every route instance of every replica, not consecutive failures on one instance. Each instance runs its own discovery loop and adds each failed refresh to the shared count. Any success on any instance resets the count. The count also expires after twice `breaker.maxOpenDuration`. With the default threshold of `3` and several instances, a single round of failed refreshes can open the shared breaker. Raise `failureThreshold` for a large fleet. The local breaker of each instance still counts consecutive failures on that instance.
- With `redis.sharedState` on, the discovery `breaker.failureThreshold` still counts consecutive failed refreshes on one route instance. Each instance runs its own discovery loop. When one instance opens its own breaker, the shared breaker opens for every instance of every replica. Failures spread over several instances do not add up, so one failed round across the fleet does not open the shared breaker.
- In-flight load is the larger of the local count and the shared count. The read window is the longest request timeout in 5 minute epochs, plus one epoch. A request stops counting when its start epoch leaves the window. A request timeout is progress-based: a stream that keeps sending data can run longer than the timeout. Such a stream drops out of the shared count when the window passes, so the shared count can be too low for long streams. It is never too high because of them.
- The request path never calls Redis. It reads a local copy. A background loop sends the counts and refreshes the copy every second, so a change on one replica reaches the others in about 1 second. The token-size estimate refreshes every 60 seconds with the router learning loop, and it applies after 50 shared samples.
- A Redis outage never fails or slows a request, and `redis.failOpen` does not apply to this state. After 3 failed refreshes in a row the gateway drops the shared values and uses its own local state. A failed token-size refresh keeps the last ratios.
@@ -768,7 +768,7 @@ How it works:
| Field | Type | Default | Semantics |
|---|---|---|---|
| `failureThreshold` | `int` | `3` | Consecutive failed discovery refreshes that open the breaker — see [Provider health](#provider-health-discovery-circuit-breaker). With `redis.sharedState` on, it counts failed refreshes across all route instances and replicas instead — see [Shared state across replicas](#shared-state-across-replicas). `0` uses the default. Must be between `1` and `100`, or construction fails. |
| `failureThreshold` | `int` | `3` | Consecutive failed discovery refreshes that open the breaker — see [Provider health](#provider-health-discovery-circuit-breaker). With `redis.sharedState` on, the first instance that reaches it opens the breaker for the whole fleet — see [Shared state across replicas](#shared-state-across-replicas). `0` uses the default. Must be between `1` and `100`, or construction fails. |
| `openDuration` | `string` (Go duration) | `1m` | Base backoff a newly opened breaker waits before its first half-open probe; doubles on every further failed probe, capped at `maxOpenDuration`. Invalid or non-positive duration string is a construction error. |
| `maxOpenDuration` | `string` (Go duration) | `6h` | Ceiling on the backoff `openDuration` doubles into. Must be `>= openDuration` and no more than `24h`, or construction fails. Deliberately longer than the default `discoveryInterval` (1h) — see [Provider health](#provider-health-discovery-circuit-breaker) for why a value at or below `discoveryInterval` suppresses nothing. |
+4 -1
View File
@@ -512,9 +512,12 @@ func (st *providerState) recordHealthLocked(now time.Time, err error) {
if st.breakerThreshold <= 0 {
return
}
st.shareOutcomeLocked(true)
st.consecutiveFailures++
if st.consecutiveFailures >= st.breakerThreshold {
// Only this instance's own trip reaches the fleet: one failed round on every instance must not trip it.
if st.consecutiveFailures == st.breakerThreshold {
st.shareOutcomeLocked(true)
}
st.openBreakerLocked(now, false)
}
}
+2
View File
@@ -28,6 +28,8 @@ const (
sharedInflightPrefix = "llmgw:inf:"
sharedBreakerOpensTTLFactor = 4
// sharedDiscoveryFleetThreshold is the fleet count of instance trips that opens the discovery breaker.
sharedDiscoveryFleetThreshold = 1
sharedBreakerMaxDoublings = 30
// sharedBreakerKeysPerID counts the keys keys() lists per id: open, fails, opens.
sharedBreakerKeysPerID = 3
+25
View File
@@ -79,3 +79,28 @@ func TestSharedStateOffIdenticalDiscoveryBreaker(t *testing.T) {
t.Fatalf("store touched with sharedState off: %d reads, %d writes", len(rec.reads), len(rec.writes))
}
}
// One failed round on every instance must not trip the fleet breaker: only an instance whose own consecutive
// failures reach the threshold trips it (formal SharedState local_gate_no_trip_before_t, local_gate_trips_at_t).
func TestSharedDiscoveryBreakerCountsPerInstance(t *testing.T) {
a, b, _, _, _ := sharedGatewayPair(t, nil)
boom := errors.New("discovery down")
now := time.Now()
for i := 0; i < defaultBreakerFailureThreshold-1; i++ {
a.registry.finishRefresh(a.registry.states["alpha"], now, nil, boom)
b.registry.finishRefresh(b.registry.states["alpha"], now, nil, boom)
}
a.sharedState.refresh()
b.sharedState.refresh()
a.sharedState.refresh()
b.sharedState.refresh()
if !a.registry.discoveryHealthy("alpha") || !b.registry.discoveryHealthy("alpha") {
t.Fatalf("fleet breaker open after %d failures per instance, below the threshold of %d on each", defaultBreakerFailureThreshold-1, defaultBreakerFailureThreshold)
}
a.registry.finishRefresh(a.registry.states["alpha"], now, nil, boom)
a.sharedState.refresh()
b.sharedState.refresh()
if b.registry.discoveryHealthy("alpha") {
t.Fatal("B healthy after A reached the threshold on its own")
}
}
+2 -3
View File
@@ -20,10 +20,9 @@ func (g *Gateway) wireSharedState(cfg *Config) {
if err != nil {
brk = breakerConfig{failureThreshold: defaultBreakerFailureThreshold, openBase: defaultBreakerOpenDuration, openMax: defaultBreakerMaxOpenDuration}
}
// With a shared breaker, brk.failureThreshold counts failed refreshes across all route instances and replicas
// inside 2*openMax, not consecutive failures on one instance.
// Each instance counts its own consecutive failures and shares only its trip, so the fleet opens on the first one.
g.registry.shareBreaker(newSharedBreaker(g.sharedState, sharedKindDiscovery, ids,
brk.failureThreshold, brk.openBase, brk.openMax, 2*brk.openMax))
sharedDiscoveryFleetThreshold, brk.openBase, brk.openMax, 2*brk.openMax))
}
g.shareDecisionBreakers(ids)
if g.tokenCalib != nil && g.limiter != nil {