mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(concurrency): share in-flight counts through Redis
In-flight counts and the learned limit now merge the fleet view when redis.sharedState is on. The load a pod reports to the router and the saturation downgrade is the larger of its own count and the fleet count. A 429 halves the fleet count, not only the local one. With the switch off, the learner runs its local code only and makes no store call.
This commit is contained in:
+37
-3
@@ -39,8 +39,15 @@ type providerConcurrency struct {
|
||||
// memory stays bounded.
|
||||
type concurrencyLearner struct {
|
||||
byProvider map[string]*providerConcurrency
|
||||
mu sync.Mutex
|
||||
explicit int
|
||||
// shared merges fleet-wide in-flight counts and the learned limit; nil means local only.
|
||||
shared *sharedInflight
|
||||
mu sync.Mutex
|
||||
explicit int
|
||||
}
|
||||
|
||||
// share attaches the fleet tracker. Call it before the learner serves requests.
|
||||
func (c *concurrencyLearner) share(s *sharedInflight) {
|
||||
c.shared = s
|
||||
}
|
||||
|
||||
func newConcurrencyLearner(explicit int) *concurrencyLearner {
|
||||
@@ -61,12 +68,19 @@ func (c *concurrencyLearner) begin(provider string) func() {
|
||||
c.mu.Lock()
|
||||
c.state(provider).inFlight++
|
||||
c.mu.Unlock()
|
||||
var endShared func()
|
||||
if c.shared != nil {
|
||||
endShared = c.shared.begin(provider)
|
||||
}
|
||||
var once sync.Once
|
||||
return func() {
|
||||
once.Do(func() {
|
||||
c.mu.Lock()
|
||||
c.state(provider).inFlight--
|
||||
c.mu.Unlock()
|
||||
if endShared != nil {
|
||||
endShared()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -91,7 +105,11 @@ func (c *concurrencyLearner) observe(provider string, resp *http.Response, err e
|
||||
s := c.state(provider)
|
||||
if overload {
|
||||
s.successes = 0
|
||||
next := s.inFlight / 2
|
||||
inFlight := s.inFlight
|
||||
if c.shared != nil {
|
||||
inFlight = c.shared.merged(provider, inFlight)
|
||||
}
|
||||
next := inFlight / 2
|
||||
if next < 1 {
|
||||
next = 1
|
||||
}
|
||||
@@ -100,6 +118,7 @@ func (c *concurrencyLearner) observe(provider string, resp *http.Response, err e
|
||||
}
|
||||
if s.learned == 0 || next < s.learned {
|
||||
s.learned = next
|
||||
c.shareLearnedLocked(provider, s.learned)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -115,6 +134,14 @@ func (c *concurrencyLearner) observe(provider string, resp *http.Response, err e
|
||||
if s.learned >= learnedCeiling {
|
||||
s.learned = 0
|
||||
}
|
||||
c.shareLearnedLocked(provider, s.learned)
|
||||
}
|
||||
|
||||
// shareLearnedLocked publishes a changed learned limit. A reset to 0 (unknown) is not published; the shared value ages out.
|
||||
func (c *concurrencyLearner) shareLearnedLocked(provider string, learned int) {
|
||||
if c.shared != nil {
|
||||
c.shared.setLearnedLimit(provider, learned)
|
||||
}
|
||||
}
|
||||
|
||||
// limit returns the effective advertised limit for provider: the smaller of
|
||||
@@ -126,6 +153,9 @@ func (c *concurrencyLearner) limit(provider string) int {
|
||||
learned = s.learned
|
||||
}
|
||||
c.mu.Unlock()
|
||||
if c.shared != nil {
|
||||
learned = c.shared.learnedLimit(provider, learned)
|
||||
}
|
||||
switch {
|
||||
case c.explicit > 0 && (learned == 0 || c.explicit < learned):
|
||||
return c.explicit
|
||||
@@ -142,6 +172,10 @@ func (c *concurrencyLearner) load(provider string) (int, int) {
|
||||
inFlight, learned = s.inFlight, s.learned
|
||||
}
|
||||
c.mu.Unlock()
|
||||
if c.shared != nil {
|
||||
inFlight = c.shared.merged(provider, inFlight)
|
||||
learned = c.shared.learnedLimit(provider, learned)
|
||||
}
|
||||
if c.explicit > 0 && (learned == 0 || c.explicit < learned) {
|
||||
return inFlight, c.explicit
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package traefikllmgateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func sharedConcurrencyCfg(c *Config) {
|
||||
c.ConcurrencyAutodetect = true
|
||||
c.MaxConcurrency = 1
|
||||
rc := c.SmartRouters[routerName]
|
||||
rc.KeywordRules = nil
|
||||
rc.SaturationDowngrade = true
|
||||
rc.Tiers = []SmartRouterTier{
|
||||
{Name: "t0", MinScore: 0, Models: []string{"a1"}},
|
||||
{Name: "t1", MinScore: 100, Models: []string{"b1"}},
|
||||
{Name: "t2", MinScore: 400, Models: []string{"c1"}},
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedInflightAcrossGateways(t *testing.T) {
|
||||
a, b, _, _, _ := sharedGatewayPair(t, sharedConcurrencyCfg)
|
||||
rt := b.registry.routers[routerName]
|
||||
|
||||
if got := planOf(t, b, rt, userReq(downgradeAsk)); got.downgradedFrom != "" {
|
||||
t.Fatalf("B downgraded with nothing in flight: %q", got.downgradedFrom)
|
||||
}
|
||||
end := a.concurrency.begin("beta")
|
||||
a.sharedState.refresh()
|
||||
b.sharedState.refresh()
|
||||
if in, lim := b.concurrency.load("beta"); in != 1 || lim != 1 {
|
||||
t.Fatalf("B load = (%d, %d), want (1, 1)", in, lim)
|
||||
}
|
||||
plan := planOf(t, b, rt, userReq(downgradeAsk))
|
||||
if plan.downgradedFrom != "t1" || plan.startTier != "t0" {
|
||||
t.Fatalf("B plan = start %q from %q, want t0 downgraded from t1", plan.startTier, plan.downgradedFrom)
|
||||
}
|
||||
end()
|
||||
end()
|
||||
a.sharedState.refresh()
|
||||
b.sharedState.refresh()
|
||||
if in, _ := b.concurrency.load("beta"); in != 0 {
|
||||
t.Fatalf("B in-flight = %d after A ended, want 0", in)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedLearnedLimitAcrossGateways(t *testing.T) {
|
||||
a, b, _, _, _ := sharedGatewayPair(t, func(c *Config) { c.ConcurrencyAutodetect = true })
|
||||
var ends []func()
|
||||
for i := 0; i < 4; i++ {
|
||||
ends = append(ends, a.concurrency.begin("alpha"))
|
||||
}
|
||||
a.sharedState.refresh()
|
||||
a.concurrency.observe("alpha", &http.Response{StatusCode: http.StatusTooManyRequests}, nil)
|
||||
b.sharedState.refresh()
|
||||
if got := b.concurrency.limit("alpha"); got != 2 {
|
||||
t.Fatalf("B limit = %d, want 2 (half of the fleet in-flight)", got)
|
||||
}
|
||||
for _, e := range ends {
|
||||
e()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedStateOffIdenticalConcurrency(t *testing.T) {
|
||||
gw, _ := sharedOffGateway(t)
|
||||
gw.concurrency = newConcurrencyLearner(0)
|
||||
gw.wireSharedState(&Config{})
|
||||
rec, _ := sharedTestEnv()
|
||||
gw.limiter.store = rec
|
||||
ref := newConcurrencyLearner(0)
|
||||
var gwEnds, refEnds []func()
|
||||
for i := 0; i < 5; i++ {
|
||||
gwEnds = append(gwEnds, gw.concurrency.begin("alpha"))
|
||||
refEnds = append(refEnds, ref.begin("alpha"))
|
||||
}
|
||||
resp := &http.Response{StatusCode: http.StatusTooManyRequests}
|
||||
ok := &http.Response{StatusCode: http.StatusOK}
|
||||
for _, r := range []*http.Response{resp, ok, ok, ok, ok, ok} {
|
||||
gw.concurrency.observe("alpha", r, nil)
|
||||
ref.observe("alpha", r, nil)
|
||||
gi, gl := gw.concurrency.load("alpha")
|
||||
ri, rl := ref.load("alpha")
|
||||
if gi != ri || gl != rl || gw.concurrency.limit("alpha") != ref.limit("alpha") {
|
||||
t.Fatalf("diverged: gw (%d,%d) ref (%d,%d)", gi, gl, ri, rl)
|
||||
}
|
||||
}
|
||||
for i := range gwEnds {
|
||||
gwEnds[i]()
|
||||
refEnds[i]()
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxProviderTimeout(t *testing.T) {
|
||||
cases := []struct {
|
||||
cfg *Config
|
||||
name string
|
||||
want time.Duration
|
||||
}{
|
||||
{&Config{}, "default", defaultRequestTimeout},
|
||||
{&Config{RequestTimeout: "10s"}, "global", 10 * time.Second},
|
||||
{&Config{RequestTimeout: "10s", Providers: map[string]*ProviderConfig{"a": {RequestTimeout: "20m"}, "b": nil}}, "provider longer", 20 * time.Minute},
|
||||
{&Config{RequestTimeout: "10m", Providers: map[string]*ProviderConfig{"a": {RequestTimeout: "1m"}}}, "provider shorter", 10 * time.Minute},
|
||||
{&Config{RequestTimeout: "x"}, "bad global falls back", defaultRequestTimeout},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := maxProviderTimeout(tc.cfg); got != tc.want {
|
||||
t.Fatalf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package traefikllmgateway
|
||||
|
||||
import "time"
|
||||
|
||||
// wireSharedState layers fleet-wide state over the local trackers. It is a no-op without a shared-state loop.
|
||||
func (g *Gateway) wireSharedState(cfg *Config) {
|
||||
if g.sharedState == nil {
|
||||
@@ -10,4 +12,25 @@ func (g *Gateway) wireSharedState(cfg *Config) {
|
||||
g.failoverHealth.share(newSharedBreaker(g.sharedState, sharedKindRequest, ids,
|
||||
requestBreakerFailureThreshold, requestBreakerOpenDuration, requestBreakerMaxOpenDuration, 2*requestBreakerMaxOpenDuration))
|
||||
}
|
||||
if g.concurrency != nil {
|
||||
g.concurrency.share(newSharedInflight(g.sharedState, ids, maxProviderTimeout(cfg)))
|
||||
}
|
||||
}
|
||||
|
||||
// maxProviderTimeout is the longest request timeout any provider can run, which bounds how long an in-flight count stays live.
|
||||
func maxProviderTimeout(cfg *Config) time.Duration {
|
||||
global, err := resolveRequestTimeout(cfg.RequestTimeout, "requestTimeout")
|
||||
if err != nil {
|
||||
global = defaultRequestTimeout
|
||||
}
|
||||
longest := global
|
||||
for name, pc := range cfg.Providers {
|
||||
if pc == nil {
|
||||
continue
|
||||
}
|
||||
if d, perr := resolveProviderTimeout(name, pc.RequestTimeout, global); perr == nil && d > longest {
|
||||
longest = d
|
||||
}
|
||||
}
|
||||
return longest
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user