@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).
110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/filelock"
|
|
)
|
|
|
|
// A Kubernetes rolling update runs the new pod beside the old one on one
|
|
// volume, each gateway on a port of its own. The new one answers requests
|
|
// at once, but the work only the serving magpie does (renewing sign-ins,
|
|
// check-ins, WhileServing) waits until the old one's gateway ends: two
|
|
// never do it at once. Without the serving lock both did.
|
|
func TestServingWorkWaitsForTheOtherMagpie(t *testing.T) {
|
|
fresh(t)
|
|
oldRetry := servingRetry
|
|
servingRetry = 10 * time.Millisecond
|
|
t.Cleanup(func() { servingRetry = oldRetry })
|
|
var working atomic.Int32
|
|
var most atomic.Int32
|
|
var began atomic.Int32
|
|
oldWork := WhileServing
|
|
WhileServing = append([]func(context.Context){func(ctx context.Context) {
|
|
began.Add(1)
|
|
n := working.Add(1)
|
|
for m := most.Load(); n > m && !most.CompareAndSwap(m, n); m = most.Load() {
|
|
}
|
|
<-ctx.Done()
|
|
working.Add(-1)
|
|
}}, oldWork...)
|
|
t.Cleanup(func() { WhileServing = oldWork })
|
|
|
|
t.Setenv("MAGPIE_ADDR", "127.0.0.1:0")
|
|
old := New()
|
|
stopOld, oldDone := listening(t, old)
|
|
defer stopOld()
|
|
waitFor(t, "the first gateway's work to begin", func() bool { return began.Load() == 1 })
|
|
|
|
t.Setenv("MAGPIE_ADDR", "127.0.0.1:0")
|
|
next := New()
|
|
stopNext, nextDone := listening(t, next)
|
|
defer stopNext()
|
|
next.lnMu.Lock()
|
|
addr := next.ln.Addr().String()
|
|
next.lnMu.Unlock()
|
|
// the new one answers while the old one serves
|
|
resp, err := http.Get("http://" + addr + "/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
io.Copy(io.Discard, resp.Body)
|
|
resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("the second gateway answered %d while the first served", resp.StatusCode)
|
|
}
|
|
// many retries' worth: its work hasn't begun
|
|
time.Sleep(30 * servingRetry)
|
|
if n := began.Load(); n != 1 {
|
|
t.Fatalf("the serving work began %d times with the first gateway still serving", n)
|
|
}
|
|
|
|
stopOld()
|
|
if err := <-oldDone; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitFor(t, "the second gateway's work to begin once the first ended", func() bool { return began.Load() == 2 })
|
|
if m := most.Load(); m != 1 {
|
|
t.Fatalf("the serving work ran in %d gateways at once", m)
|
|
}
|
|
stopNext()
|
|
if err := <-nextDone; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// A gateway that ends while it waits for the lock never does the work,
|
|
// and lets go of nothing it doesn't hold.
|
|
func TestServingWaitEndsWithItsGateway(t *testing.T) {
|
|
fresh(t)
|
|
oldRetry := servingRetry
|
|
servingRetry = 10 * time.Millisecond
|
|
t.Cleanup(func() { servingRetry = oldRetry })
|
|
unlock, held := filelock.Lock(servingLockPath(), 0)
|
|
if !held {
|
|
t.Skip("no file locks here")
|
|
}
|
|
var began atomic.Int32
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
release := holdServing(ctx, servingLockPath(), func() { began.Add(1) })
|
|
time.Sleep(10 * servingRetry)
|
|
cancel()
|
|
release()
|
|
unlock()
|
|
time.Sleep(10 * servingRetry)
|
|
if n := began.Load(); n != 0 {
|
|
t.Fatalf("the work began %d times in a gateway that ended waiting", n)
|
|
}
|
|
// and the lock is free for the next
|
|
again := holdServing(context.Background(), servingLockPath(), func() { began.Add(1) })
|
|
defer again()
|
|
if n := began.Load(); n != 1 {
|
|
t.Fatalf("a free lock: the work began %d times, want at once", n)
|
|
}
|
|
}
|