@justforlxz on X runs magpie in Kubernetes and every update cut the
requests in flight. A SIGTERM ended the gateway like Ctrl+C: the
listener closed at once and Shutdown cut what was left after 2s, an
agent's stream included, with no unready phase for the Service to see.
Now a SIGTERM to `magpie serve`, `magpie web` or `serve --web`:
- GET / answers 503 {"draining":true} at once, so `magpie healthcheck`
(the image's HEALTHCHECK, and the readiness probe the docs give) fails
and the pod leaves its Service's endpoints;
- in a pod (KUBERNETES_SERVICE_HOST) it keeps accepting for 5s while
that reaches kube-proxy and the ingress, then stops accepting;
- the requests in flight, streams included, get up to MAGPIE_DRAIN to
finish: unset, 20s in a pod (k8s' default grace is 30s, with room to
save after) and 7s elsewhere (docker stop and Compose kill at 10s,
and a drain past that would lose the state saved at the end);
seconds or a Go duration; 0 ends after 2s as before; an unreadable
value logs and uses the default. magpie web's page port (which
serves /v1 too) shuts down within the same grace.
Ctrl+C and SIGHUP end as before. Nothing to configure.
A request still answering at the end of any drain was logged as "cut"
but kept running, its upstream call too, until the process exited:
Shutdown doesn't close active connections. The gateway now closes them
after a failed Shutdown, so a gateway restarted in-process (the GUI's
restart, a test) leaves nothing holding the provider catalog
(holdCatalog). TestDrainEndsAtItsGrace's cut request, still in flight
after it returned, made the next test, TestGrokGetsAgentMessageAsUser,
read migrations.json through that hold and fail in a full gateway run
on WSL; the test now waits for it to end, and without the Close that
wait fails at 30s.
PID 1: the image's ENTRYPOINT is exec form, so magpie is PID 1 and gets
the SIGTERM itself (it calls signal.Notify). As PID 1 it exits 2 rather
than 143 after the drain, the same on the old code.
/config shared by two pods (a RollingUpdate overlap) is not verified
safe: files are flock'd and written by rename, but the serving magpie's
sign-in renewal, plugin sign-in renewal and check-ins would run in both.
The docs say one pod per volume with strategy: Recreate (an RWO volume
on another node also stalls a RollingUpdate). Requests sent between the
old pod's end and the new one's start are refused; the drain covers what
was in flight.
Docs: a Kubernetes section (Deployment with an exec readiness probe,
terminationGracePeriodSeconds, Recreate, the RWO note) and a
MAGPIE_DRAIN row on docker.html in en, zh and ja; docs/reference.md's
Docker section; site index regenerated.
Run:
- TestDrainedGatewayFinishesItsStream, TestDrainEndsAtItsGrace,
TestHandedOverGatewayFinishesItsStream, TestReadOnlyKeyEveryRoute,
TestDrainGrace, TestDrainOnSignal and the cli Signal tests: ok (mica).
- Broken by hand: without Drain's grace in drainFor,
TestDrainedGatewayFinishesItsStream fails "the gateway ended with a
stream in flight".
- Real binary (before the 7s default, which applies outside a pod
only, and the Close), sandbox HOME, fake SSE upstream streaming 10s, SIGTERM
at +2s, KUBERNETES_SERVICE_HOST set, magpie as PID 1 in its own pid
namespace (unshare) on Linux (WSL magpie-desktop), serve and web:
new: healthcheck fails while draining, a new request 1s after SIGTERM
200, the stream completes at +10s, exit at +11s. origin/main: the new
request is refused, the stream is cut at +4s after 4 chunks. Also
serve on macOS (mica): same, exit status 143.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., go build: ok (mica), before the 7s default
and the Close.
- Full suite, go test -count=1 -timeout 45m ./..., on Linux (WSL
magpie-desktop; mote and mica were each running another session's
suite) at the first version of this change on 2051dfc23: every
package ok except root, internal/cli and internal/gui, which can't
build there (no gtk4/webkitgtk-6.0 dev packages), and
internal/gateway, red on TestGrokGetsAgentMessageAsUser (the hold
above, fixed here). The doneHold "close of closed channel" panic in
the log is #6339/#6505/#6690, not this change.
- After the fixes: internal/gateway and internal/provider whole on WSL
ok (1075s, 156s); root, internal/cli, internal/gui on mote ok.
- At this head, rebased on 1c9c359f9: internal/cli whole and the drain,
handover, Codex tool and Grok gateway tests ok (mote); go vet of
gateway/cli/gui, GOOS=windows the same, GOOS=linux -tags nogui of
gateway/cli ok (mote).
Not tried: the real Docker image build and a real Kubernetes cluster
(no Docker daemon on mote, mica or WSL; Docker Desktop on xiaomi-gpu is
not running).
91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
func TestDrainGrace(t *testing.T) {
|
|
t.Setenv("KUBERNETES_SERVICE_HOST", "10.0.0.1")
|
|
for v, want := range map[string]time.Duration{
|
|
"": defaultDrain,
|
|
"30": 30 * time.Second,
|
|
"90s": 90 * time.Second,
|
|
"2m": 2 * time.Minute,
|
|
"0": 0,
|
|
"soon": defaultDrain,
|
|
"-5": defaultDrain,
|
|
" 45 ": 45 * time.Second,
|
|
"1.5s": 1500 * time.Millisecond,
|
|
"-1m": defaultDrain,
|
|
"10000": 10000 * time.Second,
|
|
} {
|
|
t.Setenv("MAGPIE_DRAIN", v)
|
|
if got := drainGrace(); got != want {
|
|
t.Errorf("MAGPIE_DRAIN=%q: %s, want %s", v, got, want)
|
|
}
|
|
}
|
|
// outside Kubernetes the default ends inside docker stop's 10s
|
|
t.Setenv("KUBERNETES_SERVICE_HOST", "")
|
|
for _, v := range []string{"", "soon"} {
|
|
t.Setenv("MAGPIE_DRAIN", v)
|
|
if got := drainGrace(); got != plainDrain || got >= 10*time.Second {
|
|
t.Errorf("MAGPIE_DRAIN=%q outside Kubernetes: %s, want %s", v, got, plainDrain)
|
|
}
|
|
}
|
|
t.Setenv("MAGPIE_DRAIN", "30")
|
|
if got := drainGrace(); got != 30*time.Second {
|
|
t.Errorf("MAGPIE_DRAIN=30 outside Kubernetes: %s", got)
|
|
}
|
|
}
|
|
|
|
// Only a SIGTERM drains; in a Kubernetes pod the gateway goes on
|
|
// accepting for kubeLinger first, out of the grace.
|
|
func TestDrainOnSignal(t *testing.T) {
|
|
var slept time.Duration
|
|
old := lingerSleep
|
|
lingerSleep = func(d time.Duration) { slept += d }
|
|
t.Cleanup(func() { lingerSleep = old })
|
|
t.Setenv("MAGPIE_DRAIN", "")
|
|
|
|
drainOnSignal(nil, syscall.SIGTERM) // magpie web beside another magpie's gateway
|
|
|
|
t.Setenv("KUBERNETES_SERVICE_HOST", "")
|
|
for _, sig := range []os.Signal{os.Interrupt, syscall.SIGHUP} {
|
|
g := gateway.New()
|
|
drainOnSignal(g, sig)
|
|
if g.Draining() {
|
|
t.Errorf("%v drained the gateway", sig)
|
|
}
|
|
}
|
|
g := gateway.New()
|
|
drainOnSignal(g, syscall.SIGTERM)
|
|
if !g.Draining() || slept != 0 {
|
|
t.Fatalf("SIGTERM outside Kubernetes: draining %v, lingered %s", g.Draining(), slept)
|
|
}
|
|
if g.Grace() != plainDrain {
|
|
t.Errorf("SIGTERM outside Kubernetes: grace %s, want %s", g.Grace(), plainDrain)
|
|
}
|
|
|
|
t.Setenv("KUBERNETES_SERVICE_HOST", "10.96.0.1")
|
|
g = gateway.New()
|
|
drainOnSignal(g, syscall.SIGTERM)
|
|
if !g.Draining() || slept != kubeLinger {
|
|
t.Fatalf("SIGTERM in Kubernetes: draining %v, lingered %s, want %s", g.Draining(), slept, kubeLinger)
|
|
}
|
|
if g.Grace() != defaultDrain-kubeLinger {
|
|
t.Errorf("SIGTERM in Kubernetes: grace %s, want %s", g.Grace(), defaultDrain-kubeLinger)
|
|
}
|
|
|
|
t.Setenv("MAGPIE_DRAIN", "0")
|
|
g = gateway.New()
|
|
drainOnSignal(g, syscall.SIGTERM)
|
|
if g.Draining() {
|
|
t.Fatal("MAGPIE_DRAIN=0 drained")
|
|
}
|
|
}
|