mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(discovery): keep context lengths when one upstream fails metadata
A failing modelBaseUrls upstream dropped its models from the metadata map. The registry replaced the discovered contexts, so those models lost their context length. The failure was also not visible. The adapter now keeps the last good metadata per upstream and reports metadata failures through upstreamErrors.
This commit is contained in:
@@ -2409,8 +2409,9 @@ providers:
|
||||
timed-out `modelBaseUrls` upstream does not
|
||||
degrade the provider: its explicit `models` stay served, the other upstreams'
|
||||
ids still merge, and the admin overview lists it under `upstreamErrors` (also
|
||||
logged, at most once per 10 minutes). Metadata keeps the ids of the upstreams
|
||||
that answered. `modelBaseUrls` ids stay declared models.
|
||||
logged, at most once per 10 minutes). Metadata of an upstream that
|
||||
failed keeps its last known values, and the failure is listed under
|
||||
`upstreamErrors`. `modelBaseUrls` ids stay declared models.
|
||||
- **Admin catalog**: each `pathBaseUrls` key appears as a pseudo-model row
|
||||
(`gx10/v1/decide`). It is display only and never routable as a model.
|
||||
`kinds` labels a model id or a path key. The Models page then groups the
|
||||
|
||||
+24
-5
@@ -39,6 +39,9 @@ type openaiAdapter struct {
|
||||
metadataPath string
|
||||
// upstreamErrs is the modelBaseUrls failures of the latest listModels call; guarded by upstreamMu.
|
||||
upstreamErrs []upstreamFailure
|
||||
// metaLast is the last good metadata per upstream base; metaErrs the metadata failures of the latest fetchModelMetadata. Guarded by upstreamMu.
|
||||
metaLast map[string]map[string]int
|
||||
metaErrs []upstreamFailure
|
||||
// timeout is this adapter's resolved per-request timeout
|
||||
// (resolveProviderTimeout, timeout.go) — UNLIKE retry above, it is NOT
|
||||
// left at its zero value by a bare newOpenAIAdapter call: a
|
||||
@@ -457,11 +460,14 @@ type upstreamErrorReporter interface {
|
||||
upstreamErrors() []upstreamFailure
|
||||
}
|
||||
|
||||
// upstreamErrors returns the modelBaseUrls failures of the latest listModels call.
|
||||
// upstreamErrors returns the modelBaseUrls failures of the latest listModels
|
||||
// call, followed by those of the latest metadata fetch.
|
||||
func (a *openaiAdapter) upstreamErrors() []upstreamFailure {
|
||||
a.upstreamMu.Lock()
|
||||
defer a.upstreamMu.Unlock()
|
||||
return append([]upstreamFailure(nil), a.upstreamErrs...)
|
||||
out := make([]upstreamFailure, 0, len(a.upstreamErrs)+len(a.metaErrs))
|
||||
out = append(out, a.upstreamErrs...)
|
||||
return append(out, a.metaErrs...)
|
||||
}
|
||||
|
||||
func (a *openaiAdapter) setUpstreamErrors(errs []upstreamFailure) {
|
||||
@@ -645,21 +651,34 @@ func (a *openaiAdapter) fetchModelMetadata(ctx context.Context) (map[string]int,
|
||||
})
|
||||
out := make(map[string]int)
|
||||
var firstErr error
|
||||
var failures []upstreamFailure
|
||||
ok := false
|
||||
a.upstreamMu.Lock()
|
||||
if a.metaLast == nil {
|
||||
a.metaLast = make(map[string]map[string]int)
|
||||
}
|
||||
for i, base := range bases {
|
||||
src := got[i]
|
||||
if errs[i] != nil {
|
||||
if firstErr == nil {
|
||||
firstErr = errs[i]
|
||||
}
|
||||
continue
|
||||
failures = append(failures, upstreamFailure{Upstream: base, Err: "metadata: " + errs[i].Error()})
|
||||
src = a.metaLast[base]
|
||||
} else {
|
||||
ok = true
|
||||
a.metaLast[base] = got[i]
|
||||
}
|
||||
ok = true
|
||||
for id, n := range got[i] {
|
||||
for id, n := range src {
|
||||
if a.baseForModel(id) == base {
|
||||
out[id] = n
|
||||
}
|
||||
}
|
||||
}
|
||||
if ok && ctx.Err() == nil {
|
||||
a.metaErrs = failures
|
||||
}
|
||||
a.upstreamMu.Unlock()
|
||||
if !ok {
|
||||
return nil, firstErr
|
||||
}
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package traefikllmgateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// flakyMetaServer serves ids with the given max_model_len on /v1/models,
|
||||
// or a 500 while down is set.
|
||||
func flakyMetaServer(t *testing.T, id string, n *atomic.Int32, down *atomic.Bool) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
if down.Load() {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = fmt.Fprintf(w, `{"data":[{"id":%q,"max_model_len":%d}]}`, id, n.Load())
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
type metaFixture struct {
|
||||
ad *openaiAdapter
|
||||
mainN, otherN *atomic.Int32
|
||||
mainDown, otherDown *atomic.Bool
|
||||
otherURL string
|
||||
}
|
||||
|
||||
func newMetaFixture(t *testing.T) *metaFixture {
|
||||
t.Helper()
|
||||
f := &metaFixture{
|
||||
mainN: new(atomic.Int32), otherN: new(atomic.Int32),
|
||||
mainDown: new(atomic.Bool), otherDown: new(atomic.Bool),
|
||||
}
|
||||
f.mainN.Store(1000)
|
||||
f.otherN.Store(2000)
|
||||
mainSrv := flakyMetaServer(t, "main-model", f.mainN, f.mainDown)
|
||||
otherSrv := flakyMetaServer(t, "glm", f.otherN, f.otherDown)
|
||||
f.otherURL = otherSrv.URL
|
||||
f.ad = newOpenAIAdapter("d1", mainSrv.URL, "")
|
||||
f.ad.modelBases = map[string]string{"glm": otherSrv.URL}
|
||||
f.ad.metadataPath = "/v1/models"
|
||||
return f
|
||||
}
|
||||
|
||||
func TestFetchModelMetadata_PartialFailureKeepsLastKnown(t *testing.T) {
|
||||
f := newMetaFixture(t)
|
||||
got, err := f.ad.fetchModelMetadata(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, map[string]int{"main-model": 1000, "glm": 2000}, got)
|
||||
|
||||
f.mainN.Store(1500)
|
||||
f.otherDown.Store(true)
|
||||
got, err = f.ad.fetchModelMetadata(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, map[string]int{"main-model": 1500, "glm": 2000}, got)
|
||||
}
|
||||
|
||||
func TestFetchModelMetadata_PartialFailureReportedInUpstreamErrors(t *testing.T) {
|
||||
f := newMetaFixture(t)
|
||||
_, err := f.ad.fetchModelMetadata(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, f.ad.upstreamErrors())
|
||||
|
||||
f.otherDown.Store(true)
|
||||
_, err = f.ad.fetchModelMetadata(context.Background())
|
||||
require.NoError(t, err)
|
||||
errs := f.ad.upstreamErrors()
|
||||
require.Len(t, errs, 1)
|
||||
assert.Equal(t, f.otherURL, errs[0].Upstream)
|
||||
assert.Contains(t, errs[0].Err, "metadata")
|
||||
}
|
||||
|
||||
func TestFetchModelMetadata_RecoveryClearsMetadataError(t *testing.T) {
|
||||
f := newMetaFixture(t)
|
||||
_, _ = f.ad.fetchModelMetadata(context.Background())
|
||||
f.otherDown.Store(true)
|
||||
_, _ = f.ad.fetchModelMetadata(context.Background())
|
||||
require.NotEmpty(t, f.ad.upstreamErrors())
|
||||
|
||||
f.otherDown.Store(false)
|
||||
f.otherN.Store(3000)
|
||||
got, err := f.ad.fetchModelMetadata(context.Background())
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, f.ad.upstreamErrors())
|
||||
assert.Equal(t, 3000, got["glm"])
|
||||
}
|
||||
|
||||
func TestFetchModelMetadata_AllFailReturnsError(t *testing.T) {
|
||||
f := newMetaFixture(t)
|
||||
_, _ = f.ad.fetchModelMetadata(context.Background())
|
||||
f.mainDown.Store(true)
|
||||
f.otherDown.Store(true)
|
||||
got, err := f.ad.fetchModelMetadata(context.Background())
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, got)
|
||||
}
|
||||
|
||||
func TestRegistry_PartialMetadataFailureKeepsContextFor(t *testing.T) {
|
||||
f := newMetaFixture(t)
|
||||
cfg := &Config{Providers: map[string]*ProviderConfig{"d1": {Discovery: true, MetadataPath: "/v1/models"}}}
|
||||
reg, err := newModelRegistry(map[string]providerAdapter{"d1": f.ad}, cfg, func(string, ...any) {})
|
||||
require.NoError(t, err)
|
||||
st := reg.states["d1"]
|
||||
|
||||
reg.captureModelMetadata(context.Background(), "d1", f.ad, st)
|
||||
f.otherDown.Store(true)
|
||||
reg.captureModelMetadata(context.Background(), "d1", f.ad, st)
|
||||
|
||||
n, ok := st.contextFor("glm")
|
||||
assert.True(t, ok, "context of the failed upstream's model must survive")
|
||||
assert.Equal(t, 2000, n)
|
||||
}
|
||||
|
||||
func TestAdminOverview_UpstreamErrorsCarryMetadataFailure(t *testing.T) {
|
||||
mainSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = fmt.Fprint(w, `{"data":[{"id":"main-model","max_model_len":1000}]}`)
|
||||
}))
|
||||
t.Cleanup(mainSrv.Close)
|
||||
otherSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/meta" {
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = fmt.Fprint(w, `{"data":[{"id":"glm"}]}`)
|
||||
}))
|
||||
t.Cleanup(otherSrv.Close)
|
||||
|
||||
cfg := newAdminTestConfig()
|
||||
cfg.Providers = map[string]*ProviderConfig{
|
||||
"uni": {
|
||||
Type: "openai", BaseURL: mainSrv.URL, APIKey: "sk", Discovery: true,
|
||||
MetadataPath: "/meta",
|
||||
Models: []string{"glm"},
|
||||
ModelBaseURLs: map[string]string{"glm": otherSrv.URL},
|
||||
},
|
||||
}
|
||||
h, _ := newAdminGatewayHandle(t, cfg)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminOverviewPath, "sk-admin1"))
|
||||
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
||||
var got adminOverviewResponse
|
||||
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
|
||||
require.Len(t, got.Providers, 1)
|
||||
require.Len(t, got.Providers[0].UpstreamErrors, 1)
|
||||
e := got.Providers[0].UpstreamErrors[0]
|
||||
assert.Contains(t, e.Error, "metadata")
|
||||
assert.Contains(t, e.Error, "502")
|
||||
assert.True(t, strings.HasPrefix(e.Upstream, "http://"), e.Upstream)
|
||||
}
|
||||
Reference in New Issue
Block a user