mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(router): share the decision breaker through Redis
The decision-model breaker now also reads and writes the fleet breaker when redis.sharedState is on. Routers that use the same decision provider share one key. A client cancel is still not a failure. With the switch off, the decider runs its local code only and makes no store call.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package traefikllmgateway
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func sharedDecisionCfg(c *Config) {
|
||||
pc := c.Providers["alpha"]
|
||||
pc.PathBaseURLs = map[string]string{"/v1/decide": pc.BaseURL}
|
||||
rc := c.SmartRouters[routerName]
|
||||
rc.Classifier = smartClassifierV2
|
||||
rc.Decision = &SmartRouterDecisionConfig{Provider: "alpha"}
|
||||
other := *rc
|
||||
c.SmartRouters["other"] = &other
|
||||
}
|
||||
|
||||
func decisionOf(g *Gateway, router string) *smartDecider {
|
||||
return g.registry.routers[router].decision.dec
|
||||
}
|
||||
|
||||
func TestSharedDecisionBreaker(t *testing.T) {
|
||||
a, b, _, clk, _ := sharedGatewayPair(t, sharedDecisionCfg)
|
||||
for i := 0; i < decisionBreakerFails; i++ {
|
||||
decisionOf(a, routerName).record(false)
|
||||
}
|
||||
if decisionOf(b, routerName).breakerOpen() {
|
||||
t.Fatal("B open before any refresh, breakerOpen must not read the store")
|
||||
}
|
||||
a.sharedState.refresh()
|
||||
b.sharedState.refresh()
|
||||
if !decisionOf(b, routerName).breakerOpen() {
|
||||
t.Fatal("B closed after A tripped the decision breaker and one refresh")
|
||||
}
|
||||
if !decisionOf(b, "other").breakerOpen() {
|
||||
t.Fatal("a second router on the same decision provider did not share the breaker")
|
||||
}
|
||||
|
||||
decisionOf(b, routerName).record(true)
|
||||
b.sharedState.refresh()
|
||||
if decisionOf(b, routerName).breakerOpen() || decisionOf(b, "other").breakerOpen() {
|
||||
t.Fatal("B still open after a successful decision call")
|
||||
}
|
||||
|
||||
for i := 0; i < decisionBreakerFails; i++ {
|
||||
decisionOf(a, routerName).record(false)
|
||||
}
|
||||
a.sharedState.refresh()
|
||||
b.sharedState.refresh()
|
||||
clk.advance(decisionBreakerOpen + time.Second)
|
||||
if decisionOf(b, routerName).breakerOpen() {
|
||||
t.Fatal("B open after the open window elapsed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedStateOffIdenticalDecisionBreaker(t *testing.T) {
|
||||
gw, _ := sharedOffGateway(t)
|
||||
rec, _ := sharedTestEnv()
|
||||
gw.limiter.store = rec
|
||||
now := time.Unix(1_800_000_000, 0)
|
||||
nowFn := func() time.Time { return now }
|
||||
got, want := newSmartDecider(nowFn), newSmartDecider(nowFn)
|
||||
steps := []struct {
|
||||
ok bool
|
||||
advance time.Duration
|
||||
}{{false, 0}, {false, 0}, {false, 0}, {true, 0}, {false, 0}, {false, 31 * time.Second}, {false, 0}, {false, 0}}
|
||||
for i, s := range steps {
|
||||
now = now.Add(s.advance)
|
||||
got.record(s.ok)
|
||||
want.record(s.ok)
|
||||
if got.breakerOpen() != want.breakerOpen() {
|
||||
t.Fatalf("step %d: breakerOpen = %v, want %v", i, got.breakerOpen(), want.breakerOpen())
|
||||
}
|
||||
}
|
||||
if len(rec.reads) != 0 || len(rec.writes) != 0 {
|
||||
t.Fatalf("store touched with sharedState off: %d reads, %d writes", len(rec.reads), len(rec.writes))
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ func (g *Gateway) wireSharedState(cfg *Config) {
|
||||
g.registry.shareBreaker(newSharedBreaker(g.sharedState, sharedKindDiscovery, ids,
|
||||
brk.failureThreshold, brk.openBase, brk.openMax, 2*brk.openMax))
|
||||
}
|
||||
g.shareDecisionBreakers(ids)
|
||||
}
|
||||
|
||||
// maxProviderTimeout is the longest request timeout any provider can run, which bounds how long an in-flight count stays live.
|
||||
@@ -42,3 +43,21 @@ func maxProviderTimeout(cfg *Config) time.Duration {
|
||||
}
|
||||
return longest
|
||||
}
|
||||
|
||||
// shareDecisionBreakers gives every router's decider one fleet breaker. Routers on the same decision provider share a key on purpose.
|
||||
func (g *Gateway) shareDecisionBreakers(ids []string) {
|
||||
if g.registry == nil {
|
||||
return
|
||||
}
|
||||
var brk *sharedBreaker
|
||||
for _, rt := range g.registry.routers {
|
||||
if rt == nil || rt.decision == nil || rt.decision.dec == nil {
|
||||
continue
|
||||
}
|
||||
if brk == nil {
|
||||
brk = newSharedBreaker(g.sharedState, sharedKindDecision, ids,
|
||||
decisionBreakerFails, decisionBreakerOpen, decisionBreakerOpen, 2*decisionBreakerOpen)
|
||||
}
|
||||
rt.decision.dec.share(brk, rt.decision.provider)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,8 +168,16 @@ type smartDecider struct {
|
||||
items map[uint64]*list.Element
|
||||
order *list.List
|
||||
openUntil time.Time
|
||||
mu sync.Mutex
|
||||
fails int
|
||||
// shared layers the fleet breaker over the local one, keyed by the decision provider; nil means local only.
|
||||
shared *sharedBreaker
|
||||
key string
|
||||
mu sync.Mutex
|
||||
fails int
|
||||
}
|
||||
|
||||
// share attaches the fleet breaker. Call it before the decider serves requests.
|
||||
func (d *smartDecider) share(b *sharedBreaker, key string) {
|
||||
d.shared, d.key = b, key
|
||||
}
|
||||
|
||||
func newSmartDecider(nowFn func() time.Time) *smartDecider {
|
||||
@@ -221,7 +229,10 @@ func (d *smartDecider) cachePut(key uint64, rank, prob int) {
|
||||
func (d *smartDecider) breakerOpen() bool {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
return d.now().Before(d.openUntil)
|
||||
if d.now().Before(d.openUntil) {
|
||||
return true
|
||||
}
|
||||
return d.shared != nil && d.shared.sharedOpen(d.key)
|
||||
}
|
||||
|
||||
// record counts one call result; a failure at the threshold opens the breaker.
|
||||
@@ -230,8 +241,14 @@ func (d *smartDecider) record(success bool) {
|
||||
defer d.mu.Unlock()
|
||||
if success {
|
||||
d.fails = 0
|
||||
if d.shared != nil {
|
||||
d.shared.recordSuccess(d.key)
|
||||
}
|
||||
return
|
||||
}
|
||||
if d.shared != nil {
|
||||
d.shared.recordFailure(d.key)
|
||||
}
|
||||
d.fails++
|
||||
if d.fails >= decisionBreakerFails {
|
||||
d.openUntil = d.now().Add(decisionBreakerOpen)
|
||||
|
||||
Reference in New Issue
Block a user