diff --git a/shared_state.go b/shared_state.go index 65d72cb..b2e8843 100644 --- a/shared_state.go +++ b/shared_state.go @@ -293,6 +293,25 @@ func (l *sharedStateLoop) refreshFailed(prims []sharedPrimitive, err error) { } } +// chunkedGet reads keys straight from the store in chunks. Unlike the limiter's reads it never latches the chat route's limiter +// on an error. ok is false when any chunk fails. +func (l *sharedStateLoop) chunkedGet(keys []string) ([]int64, bool) { + out := make([]int64, 0, len(keys)) + for off := 0; off < len(keys); off += sharedStateChunk { + end := off + sharedStateChunk + if end > len(keys) { + end = len(keys) + } + got, err := l.store.getMulti(keys[off:end]) + if err != nil || len(got) != end-off { + l.logErr("shared state read of %d keys failed: %v", end-off, err) + return nil, false + } + out = append(out, got...) + } + return out, true +} + func (l *sharedStateLoop) incrAndGet(e counterIncr) (int64, bool) { got, _, err := l.store.incrAndGetMulti([]counterIncr{e}, nil) if err != nil || len(got) != 1 { diff --git a/shared_state_calibrator_test.go b/shared_state_calibrator_test.go index 2026bc9..75bf4cf 100644 --- a/shared_state_calibrator_test.go +++ b/shared_state_calibrator_test.go @@ -1,6 +1,7 @@ package traefikllmgateway import ( + "errors" "testing" ) @@ -92,3 +93,23 @@ func TestSharedStateOffIdenticalCalibrator(t *testing.T) { t.Fatalf("store touched with sharedState off: %d reads, %d writes", len(rec.reads), len(rec.writes)) } } + +func TestSharedCalibrationReadErrorDoesNotLatchLimiter(t *testing.T) { + a, _, store, _, _ := sharedGatewayPair(t, nil) + shareLimiterStore(a, store) + for i := 0; i < smartCalibMinSamples; i++ { + observeShared(a, "alpha", "a1", 1000, 400) + } + a.refreshRouterLearning(a.limiter.now()) + if got := a.tokenCalib.promptTokens("alpha", "a1", 1000); got != 400 { + t.Fatalf("setup: shared estimate = %d, want 400", got) + } + store.setFail(errors.New("redis down")) + a.refreshRouterLearning(a.limiter.now()) + if a.limiter.storeLatched() { + t.Error("a failed shared-state read latched the chat route's limiter store") + } + if got := a.tokenCalib.promptTokens("alpha", "a1", 1000); got != 400 { + t.Errorf("estimate after a failed read = %d, want the previous 400", got) + } +} diff --git a/smart_router_learn.go b/smart_router_learn.go index be789aa..6143650 100644 --- a/smart_router_learn.go +++ b/smart_router_learn.go @@ -446,7 +446,7 @@ func (g *Gateway) calibrationTargets() []smartCalibKey { // refreshSharedCalibration reads the fleet counters of every calibrating router's targets and keeps one bytes-per-token ratio per model. // A failed read keeps the previous ratios. func (g *Gateway) refreshSharedCalibration(now time.Time) { - if !g.tokenCalib.sharing() || g.limiter == nil { + if !g.tokenCalib.sharing() || g.sharedState == nil { return } targets := g.calibrationTargets() @@ -463,7 +463,7 @@ func (g *Gateway) refreshSharedCalibration(now time.Time) { names = append(names, calibFleetKey(k, calibMetricBytes, bucket), calibFleetKey(k, calibMetricToken, bucket), calibFleetKey(k, calibMetricCount, bucket)) } } - vals, ok := g.limiter.chunkedGetMulti(names, usageModelsChunkKeys) + vals, ok := g.sharedState.chunkedGet(names) if !ok { return }