@justforlxz on X asked for a RollingUpdate (maxSurge 1, maxUnavailable 0) with no request dropped. The docs said strategy: Recreate, because the work only the serving magpie may do (renewing sign-ins, check-ins, moves, plugin updates, WhileServing) was kept to one magpie only by the gateway's port: a second couldn't bind it. Two pods each bind their own network namespace, so in an overlap both did it, and two renewals of one refresh token can spend it (a single-use token rotates). Design: both pods answer requests at once (each refresh and file write is already locked across processes: filelock, host.js lockRenew); the serving work is held to whichever gateway holds gateway.serving.lock (flock on the config folder, serving.go). The new pod retries every second and takes the work over as the old pod's gateway ends (its drain lets go first). A file system without locks is read as no other magpie, as before. Pods share the volume but not a pid namespace (both run magpie as pid 1), so the locks that judged a holder by its pid now judge it by host: - host.js lockRenew records host (hostname + pid namespace); a lock of another host is stale by age only, never by "pid 1 is alive here". - lockMoves (migrate.go) the same. - gateway.json records Host; forgetListening removes only its own, so the old pod's end no longer removes the new pod's file (same pid, same Started second was enough before). - temp and run-mark names get a random part (writeDurable, Claude run marks, gateway.json's temp file). While draining (SIGTERM), every reply says Connection: close, on the gateway and on magpie web's page port, so an agent that kept its connection to the old pod alive dials the Service again for its next request. Without it, the real rollout below had one broken pipe per kept-alive connection, ~5.8s after each SIGTERM, when the drain's end closed them. Docs (en/zh/ja docker, reference.md): RollingUpdate maxSurge 1 / maxUnavailable 0, the readiness probe, a required podAffinity on kubernetes.io/hostname (a ReadWriteOnce volume mounts on one node), and one Recreate update from a version before this one, whose old pod doesn't take the lock. Run: - Real overlap, podman 4.9.3 in WSL magpie-desktop: three pods (pa -> pb -> pc) on one volume, each its own container (pid 1, own hostname and network), a Service proxy over a readiness-probed endpoint list (1s probe, endpoint removed 1s after SIGTERM as kube-proxy does), 12 agents streaming 3s SSE turns through it, half keeping connections alive, no retries. Before Connection: close: 226 ok, 10 failed (broken pipe, kept-alive). After: 209 ok, 0 failed; second run 207 ok, 0 failed. Each new pod logged "another magpie serves", then "let go" as the old pod's gateway ended; gateway.json named the last pod. - Broken by hand, each red then green: TestServingWorkWaitsForTheOther- Magpie, TestServingWaitEndsWithItsGateway, TestForgetListeningKeeps- AnotherPods, TestMoveLockOfAnotherPod, TestRenewLockOfAnotherPodIs- WaitedFor, TestDrainingRepliesCloseTheirConnection, TestPagePortClosesWhileDraining. - Full suite, go test -count=1 -timeout 45m ./..., at 85305a82d (this change on2926b7bef): macOS on mote, 102 packages ok; Linux (-tags nogui) in WSL magpie-desktop, 101 ok. Reds: - TestAuthLockWaitDoesNotFreezeTheHost, both: this change's writeDurable called randomUUID, which the test's sandbox of host.js doesn't give. It now names the temp file with Math.random; plugin then ok on mote and on Linux. - Not this change, filed: TestCappedIsSmall (Linux, #5975), TestRequestStateKeptAcrossACatalogFetchedAgain (Linux, #6572), TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (macOS, #7117). - Not a red, filed: the macOS log has a recovered "http: panic serving ...: close of closed channel" in the doneHold test fake (continuation_test.go:781); no test failed on it. It is #6505 (also filed as #6339 and #6690). - go vet ./..., GOOS=linux -tags nogui and GOOS=windows go vet ./... ok on mote; go vet -tags nogui ./... ok on Linux; gofmt -l clean. - At the pushed head (rebased on6e5882109), on mote: go vet (macOS, GOOS=linux -tags nogui, GOOS=windows) of gateway, gui, plugin and provider ok; go test -count=1 of gui, plugin, provider ok, gateway ok but #7117's TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes; site node --test 135 pass, 0 fail. - New tests on Windows (win-run): gateway, provider, plugin, gui ok. Not tried: a real Kubernetes cluster and kube-proxy (the podman harness stands in for both); a real OAuth plugin's renewal across two pods (the plugin test uses a fake vendor); an RWX/NFS volume's locks. A stream that began before SIGTERM and outlasts the drain's 20s still ends with the old pod. Pods exit 2 on SIGTERM as pid 1 (before this change too).
154 lines
4.6 KiB
Go
154 lines
4.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// An account whose sign-in the vendor refuses while the move reads the
|
|
// plugin's models (a models hook throwing signIn: expired, as Zed's does
|
|
// on a refused model token) moves along marked, as the built-in would
|
|
// have marked it on its own model read, and doesn't stop the move.
|
|
func TestMoveRefusedWhileListing(t *testing.T) {
|
|
bun := testenv.Bun(t)
|
|
claudeHome(t)
|
|
t.Setenv("MAGPIE_BUN", bun)
|
|
t.Cleanup(plugin.Settle)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
|
if _, err := plugin.Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := plugin.Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fakeMover(t)
|
|
movers["fakeco"].pkg = abs
|
|
loginsMu.Lock()
|
|
if err := writeLogins([]savedLogin{fakeLogin("a@fake", "r-a", true, true), fakeLogin("g@fake", "r-models-gone", false, true)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loginsMu.Unlock()
|
|
if err := Move(ctx, "fakeco"); err != nil {
|
|
t.Fatalf("a sign-in refused while listing stopped the move: %v", err)
|
|
}
|
|
for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(50 * time.Millisecond) {
|
|
lapsed := map[string]bool{}
|
|
for _, l := range pluginLogins(mustPlugin(t)) {
|
|
lapsed[l.User] = l.Lapsed != ""
|
|
}
|
|
if lapsed["g@fake"] && !lapsed["a@fake"] {
|
|
return
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("lapsed after the move: %v, want g@fake alone", lapsed)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An account whose plugin says only in words that its sign-in has expired
|
|
// (Grok's, past its time with no CLI to renew it) moves along untried and
|
|
// unmarked, as a built-in Grok account never was marked; it used to stop
|
|
// the whole move. A plugin failing an account for any other reason still
|
|
// does (TestMove's r-dead).
|
|
func TestMoveExpiredInWords(t *testing.T) {
|
|
bun := testenv.Bun(t)
|
|
claudeHome(t)
|
|
t.Setenv("MAGPIE_BUN", bun)
|
|
t.Cleanup(plugin.Settle)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
|
if _, err := plugin.Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := plugin.Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fakeMover(t)
|
|
movers["fakeco"].pkg = abs
|
|
loginsMu.Lock()
|
|
if err := writeLogins([]savedLogin{fakeLogin("a@fake", "r-a", true, true), fakeLogin("x@fake", "r-models-expired", false, true)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loginsMu.Unlock()
|
|
if err := Move(ctx, "fakeco"); err != nil {
|
|
t.Fatalf("an expired sign-in stopped the move: %v", err)
|
|
}
|
|
users := map[string]bool{}
|
|
for _, l := range pluginLogins(mustPlugin(t)) {
|
|
users[l.User] = true
|
|
if l.Lapsed != "" {
|
|
t.Fatalf("%s marked lapsed: %q", l.User, l.Lapsed)
|
|
}
|
|
}
|
|
if !users["a@fake"] || !users["x@fake"] {
|
|
t.Fatalf("moved %v", users)
|
|
}
|
|
}
|
|
|
|
// A move's lock left by a magpie killed mid-move (a hung move stopped by
|
|
// hand) holds nothing: the next move takes it, where it used to be
|
|
// refused as "another magpie is moving" for a quarter of an hour.
|
|
func TestMoveLockOfDeadMagpie(t *testing.T) {
|
|
claudeHome(t)
|
|
gone := exec.Command("true")
|
|
if err := gone.Run(); err != nil {
|
|
t.Skip(err)
|
|
}
|
|
p := migrationsPath() + ".lock"
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(strconv.Itoa(gone.Process.Pid)), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
unlock, err := lockMoves()
|
|
if err != nil {
|
|
t.Fatalf("a dead magpie's lock: %v", err)
|
|
}
|
|
// one held by a magpie still running stays held
|
|
if _, err := lockMoves(); err == nil {
|
|
t.Fatal("a live lock was taken")
|
|
}
|
|
unlock()
|
|
}
|
|
|
|
// A move's lock written by another pod's magpie on the same volume (a
|
|
// Kubernetes rolling update) names a pid of that pod's namespace, one no
|
|
// process here has: it is held while it is fresh, not taken as a dead
|
|
// magpie's, and a stale one (a quarter of an hour) is taken.
|
|
func TestMoveLockOfAnotherPod(t *testing.T) {
|
|
claudeHome(t)
|
|
gone := exec.Command("true")
|
|
if err := gone.Run(); err != nil {
|
|
t.Skip(err)
|
|
}
|
|
p := migrationsPath() + ".lock"
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(strconv.Itoa(gone.Process.Pid)+" magpie-7d9f-old"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := lockMoves(); err == nil {
|
|
t.Fatal("another pod's lock was taken as a dead magpie's")
|
|
}
|
|
old := time.Now().Add(-16 * time.Minute)
|
|
os.Chtimes(p, old, old)
|
|
unlock, err := lockMoves()
|
|
if err != nil {
|
|
t.Fatalf("a stale lock of another pod: %v", err)
|
|
}
|
|
unlock()
|
|
}
|