dbydd: a group's routing still goes over its members in order however they have been failing, and with providers magpie can't read a quota from, that is close to a DDoS of the ones that are down. What was there: a failure rests the member (credit 30m, quota till its reset, rate by Retry-After, anything else 1m doubling to 10m), and one answer clears it. But restLast only moved resting candidates behind the ready ones. Once the ready ones failed, every request went on to ask each resting member in turn, so the rest kept nobody back: in an outage of the whole group each request went to all N members, and the last one again on its retries. Now (probeOne, fallback.go): of the candidates resting, a request asks only the one whose rest ends soonest (the first in order on a tie), at its place. Every ready candidate is still asked in the user's order. A lone candidate is always asked. A group that is down whole takes one upstream request per request, and the backoff grows with each real probe instead of each request. The trace still lists every member, the resting ones with their rest. The same is applied where the other paths go over candidates: count_tokens, embeddings/rerank, image accounts (drawOnAccounts) and a group's System One decision models. Codex autoReset still looks over the whole list (all), so a week used up on every account still spends a reset. Default on, no switch: it only reads rests the user's order already demotes, and only within a rest's cooldown; once a rest ends, the member is back at its place in the user's order. Each request still makes at least one real upstream attempt and returns the vendor's own error. Scoping: only rests are read, and a failure about the request's content or its model rests no member (failShape, prompt, overflow, refused, loop, auth, proxy...; a model refusal rests only restID), so neither leaves a member out. Borrowed from AxonHub's error-aware load balancing (looplj/axonhub, docs/zh/guides/load-balance.md, ErrorAware), which dbydd pointed to: a failed channel's score drops and recovers as its penalty decays, rather than it being sent every request. magpie's rest and backoff already are that decaying penalty; asking the one back soonest is how it recovers. Not taken: a success-rate memory that keeps a member demoted after it answers. One answer means it serves now, a flaky member already rests at least a minute per failure, and holding it down past that would override the user's order beyond a cooldown. Unchanged: the last candidate still isn't rested by its own failure, and the Patience rounds (#1418) and retries on the last one re-ask only candidates already in the trimmed list. Tests (resting_probe_test.go): TestGroupAsksOneRestingMemberNotEach (three resting members, minutes apart: only the soonest is asked; with a ready member, it then the soonest), TestRetrievalGroupAsksOneRestingMember (embeddings), TestLoneRestingMemberStillAsked, TestProbeOne. With probeOne returning its input (the old behaviour), the first two fail: "resting members asked map[a:1 b:1 c:1]". TestSystemOneDecisionGroupFailsOver held the old behaviour (all failing: kb, ka1, ka2 asked, ka2's 503 back); it now wants kb then ka1, the one rested first, and ka1's 429 back. Run: gofmt -l (clean); go vet (darwin, GOOS=linux -tags nogui, GOOS=windows); GOOS=linux -tags nogui and GOOS=windows go build; go test -tags nogui ./... -timeout 40m under a sandbox HOME at load average ~100: every package ok but gateway and agent, which hit the 40m timeout mid-run (TestZenUnsupportedModelLeavesTheListAlone and TestSnowNoProfiles running, a few seconds in), and plugin, where TestNPMInfoDeadline hung on its own failed 2s check (filed #1808). Rerun alone with -timeout 80m: gateway ok (1896s), agent ok (1811s), plugin ok (746s). Not run: a real upstream outage, the Windows box; count_tokens and image accounts have no test of their own.
187 lines
6.3 KiB
Go
187 lines
6.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// hits counts the requests each upstream took, and fails them all as out
|
|
// of credit: a failure that passes the request on, with no retry.
|
|
type hits struct {
|
|
sync.Mutex
|
|
n map[string]int
|
|
}
|
|
|
|
func (h *hits) on(id string) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
h.Lock()
|
|
h.n[id]++
|
|
h.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusPaymentRequired)
|
|
io.WriteString(w, `{"error":{"message":"insufficient balance"}}`)
|
|
})
|
|
}
|
|
|
|
func (h *hits) got() map[string]int {
|
|
h.Lock()
|
|
defer h.Unlock()
|
|
out := map[string]int{}
|
|
for k, v := range h.n {
|
|
out[k] = v
|
|
}
|
|
return out
|
|
}
|
|
|
|
// restFor rests each named member of group's candidates until the time
|
|
// given, as a failure before would have.
|
|
func restFor(t *testing.T, s *Server, group string, until map[string]time.Time) {
|
|
t.Helper()
|
|
g, ms, ok := provider.FindGroup("group/" + group)
|
|
if !ok {
|
|
t.Fatalf("no group %s", group)
|
|
}
|
|
cs, _ := s.planGroup(g.Live(), ms, provider.Chat)
|
|
restingUntil.Lock()
|
|
defer restingUntil.Unlock()
|
|
for _, c := range cs {
|
|
if u, ok := until[c.p.ID]; ok {
|
|
restingUntil.m[c.restKey()] = u
|
|
restingUntil.note[c.restKey()] = Rest{Why: failOther, Status: 500, Key: c.restKey()}
|
|
}
|
|
}
|
|
}
|
|
|
|
// #1799: a group whose members are resting after failures doesn't send
|
|
// each request to every one of them in turn. Of those resting, only the
|
|
// one whose rest ends soonest is asked; a ready member is asked as ever,
|
|
// first.
|
|
func TestGroupAsksOneRestingMemberNotEach(t *testing.T) {
|
|
fresh(t)
|
|
h := &hits{n: map[string]int{}}
|
|
for _, id := range []string{"a", "b", "c", "d"} {
|
|
serveOn(t, id, "k-"+id, []string{"m"}, h.on(id))
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Down", Members: []string{"a/m", "b/m", "c/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Half", Members: []string{"a/m", "b/m", "d/m", "c/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
now := time.Now()
|
|
// minutes apart, so which is back soonest doesn't hang on the clock
|
|
restFor(t, s, "down", map[string]time.Time{"a": now.Add(9 * time.Minute), "b": now.Add(3 * time.Minute), "c": now.Add(6 * time.Minute)})
|
|
|
|
code, body := postAs(t, s, "", `{"model":"group/down","messages":[{"role":"user","content":"hi"}]}`)
|
|
if code < 400 {
|
|
t.Fatalf("every member fails, answered %d %s", code, body)
|
|
}
|
|
if got := h.got(); got["a"] != 0 || got["c"] != 0 || got["b"] != 1 {
|
|
t.Fatalf("resting members asked %v, want b alone, once", got)
|
|
}
|
|
r := s.trace.routes[len(s.trace.routes)-1]
|
|
if len(r.Order) != 3 || len(r.Tries) != 1 || r.Tries[0].ID != "b" {
|
|
t.Fatalf("trace: order %d, tries %+v", len(r.Order), r.Tries)
|
|
}
|
|
for _, w := range r.Order {
|
|
if w.Rest == nil {
|
|
t.Fatalf("the trace shows %s not resting: %+v", w.Provider, r.Order)
|
|
}
|
|
}
|
|
|
|
// a member not resting is asked first, then the resting one back
|
|
// soonest: b rests longer now that it failed again
|
|
restFor(t, s, "half", map[string]time.Time{"a": now.Add(9 * time.Minute), "b": now.Add(8 * time.Minute), "c": now.Add(2 * time.Minute)})
|
|
h.n = map[string]int{}
|
|
if code, body := postAs(t, s, "", `{"model":"group/half","messages":[{"role":"user","content":"hi"}]}`); code < 400 {
|
|
t.Fatalf("every member fails, answered %d %s", code, body)
|
|
}
|
|
if got := h.got(); got["d"] != 1 || got["c"] != 1 || got["a"] != 0 || got["b"] != 0 {
|
|
t.Fatalf("asked %v, want d then c", got)
|
|
}
|
|
}
|
|
|
|
// A lone member resting is still asked: the request has nobody else, and
|
|
// gets the vendor's own answer.
|
|
func TestLoneRestingMemberStillAsked(t *testing.T) {
|
|
fresh(t)
|
|
h := &hits{n: map[string]int{}}
|
|
serveOn(t, "a", "k-a", []string{"m"}, h.on("a"))
|
|
if err := provider.SaveGroup(provider.Group{Name: "One", Members: []string{"a/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
restFor(t, s, "one", map[string]time.Time{"a": time.Now().Add(5 * time.Minute)})
|
|
code, body := postAs(t, s, "", `{"model":"group/one","messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != http.StatusPaymentRequired || !strings.Contains(body, "insufficient balance") {
|
|
t.Fatalf("lone member: %d %s", code, body)
|
|
}
|
|
if got := h.got(); got["a"] != 1 {
|
|
t.Fatalf("lone resting member asked %v", got)
|
|
}
|
|
}
|
|
|
|
// The embeddings path goes over a group as a chat does, and asks one
|
|
// resting member too.
|
|
func TestRetrievalGroupAsksOneRestingMember(t *testing.T) {
|
|
fresh(t)
|
|
h := &hits{n: map[string]int{}}
|
|
for _, id := range []string{"a", "b", "c"} {
|
|
serveOn(t, id, "k-"+id, []string{"e"}, h.on(id))
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Emb", Members: []string{"a/e", "b/e", "c/e"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
now := time.Now()
|
|
restFor(t, s, "emb", map[string]time.Time{"a": now.Add(9 * time.Minute), "b": now.Add(6 * time.Minute), "c": now.Add(3 * time.Minute)})
|
|
if code, raw := postRetrieval(t, s, "/v1/embeddings", `{"model":"group/emb","input":"hi"}`); code < 400 {
|
|
t.Fatalf("every member fails, answered %d %s", code, raw)
|
|
}
|
|
if got := h.got(); got["a"] != 0 || got["b"] != 0 || got["c"] != 1 {
|
|
t.Fatalf("resting members asked %v, want c alone", got)
|
|
}
|
|
}
|
|
|
|
// probeOne keeps every ready candidate, and of the resting ones only the
|
|
// one back soonest, at its place; with one resting or none, all.
|
|
func TestProbeOne(t *testing.T) {
|
|
fresh(t)
|
|
c := func(id string) candidate { return candidate{p: provider.Provider{ID: id}, model: "m", rest: id} }
|
|
cs := []candidate{c("a"), c("b"), c("c"), c("d"), c("e")}
|
|
ids := func(cs []candidate) string {
|
|
var out []string
|
|
for _, c := range cs {
|
|
out = append(out, c.p.ID)
|
|
}
|
|
return strings.Join(out, " ")
|
|
}
|
|
if got := ids(probeOne(cs)); got != "a b c d e" {
|
|
t.Fatalf("none resting: %s", got)
|
|
}
|
|
now := time.Now()
|
|
restingUntil.Lock()
|
|
restingUntil.m["b"] = now.Add(4 * time.Minute)
|
|
restingUntil.Unlock()
|
|
if got := ids(probeOne(cs)); got != "a b c d e" {
|
|
t.Fatalf("one resting: %s", got)
|
|
}
|
|
restingUntil.Lock()
|
|
restingUntil.m["d"] = now.Add(2 * time.Minute)
|
|
restingUntil.m["e"] = now.Add(2 * time.Minute)
|
|
restingUntil.Unlock()
|
|
if got := ids(probeOne(cs)); got != "a c d" {
|
|
t.Fatalf("three resting: %s", got)
|
|
}
|
|
if got := ids(probeOne(cs[1:2])); got != "b" {
|
|
t.Fatalf("lone: %s", got)
|
|
}
|
|
}
|