From 963c677e1c1c562640eea314a96ac123a5513944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Thu, 26 Nov 2026 00:00:11 +0100 Subject: [PATCH] fix: roll the API out without cutting off checkers Checkers long-poll the API for up to 25 seconds, longer than a shutdown waits for requests, so a rollout cut their polls off (GOAWAY, timeouts). A draining API now ends long polls within about a second, empty, and the checker's next poll reaches another instance. The chart rolls the API and status page servers one pod at a time with maxUnavailable 0, and a new pod serves for 5 seconds before an old one goes, so the ingress has picked it up. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- cmd/api/main.go | 1 + deploy/helm/uptimesebi/templates/api.yaml | 9 +++++ .../helm/uptimesebi/templates/statuspage.yaml | 9 +++++ internal/api/agent.go | 27 +++++++++++++-- internal/api/drain_test.go | 34 +++++++++++++++++++ internal/api/server.go | 6 ++++ internal/platform/health.go | 14 ++++++-- 7 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 internal/api/drain_test.go diff --git a/cmd/api/main.go b/cmd/api/main.go index f7cc2ed..a34fdfa 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -89,6 +89,7 @@ func run(log *slog.Logger, level *slog.LevelVar, args []string) error { health.Register(r) r.StaticFS("/static", web.Static()) srv.Mount(r) + srv.EndLongPollsOn(health.Draining()) go srv.RunScheduler(ctx) go srv.RunNotifier(ctx) go srv.RunHousekeeping(ctx) diff --git a/deploy/helm/uptimesebi/templates/api.yaml b/deploy/helm/uptimesebi/templates/api.yaml index 886fa58..90543f6 100644 --- a/deploy/helm/uptimesebi/templates/api.yaml +++ b/deploy/helm/uptimesebi/templates/api.yaml @@ -8,6 +8,15 @@ metadata: app.kubernetes.io/component: api spec: replicas: {{ .Values.api.replicas }} + # Roll one pod at a time without ever having fewer ready than wanted; + # a new pod serves a few seconds before an old one goes, so the ingress + # has picked it up. + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + minReadySeconds: 5 selector: matchLabels: {{- include "uptimesebi.selector" (list . "api") | nindent 6 }} diff --git a/deploy/helm/uptimesebi/templates/statuspage.yaml b/deploy/helm/uptimesebi/templates/statuspage.yaml index 2a05530..033f99d 100644 --- a/deploy/helm/uptimesebi/templates/statuspage.yaml +++ b/deploy/helm/uptimesebi/templates/statuspage.yaml @@ -9,6 +9,15 @@ metadata: app.kubernetes.io/component: statuspage spec: replicas: {{ .Values.statuspage.replicas }} + # Roll one pod at a time without ever having fewer ready than wanted; + # a new pod serves a few seconds before an old one goes, so the ingress + # has picked it up. + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + minReadySeconds: 5 selector: matchLabels: {{- include "uptimesebi.selector" (list . "statuspage") | nindent 6 }} diff --git a/internal/api/agent.go b/internal/api/agent.go index 66bcb24..7abb40e 100644 --- a/internal/api/agent.go +++ b/internal/api/agent.go @@ -32,7 +32,9 @@ const ( ctxAgent = "api.agent" ctxInstance = "api.instance" maxJobWait = 25 * time.Second - maxJobBatch = 200 + // drainingPollHold is how long a draining instance still holds a poll. + drainingPollHold = time.Second + maxJobBatch = 200 ) func (s *Server) mountAgentAPI(r gin.IRouter) { @@ -197,8 +199,27 @@ func (s *Server) agentJobs(c *gin.Context) { return } wait := min(time.Duration(req.WaitMS)*time.Millisecond, maxJobWait) - entries, err := s.sched.Take(ctx, instance, min(req.Max, maxJobBatch), wait) - if err != nil && ctx.Err() == nil { + // A draining instance ends long polls within a second or so, empty, so + // the shutdown is not held up and the checker's next poll reaches + // another instance. It still holds them that second, so checkers that + // poll again at once do not spin against it. + pollCtx, stop := context.WithCancel(ctx) + defer stop() + if s.draining != nil { + go func() { + select { + case <-s.draining: + select { + case <-time.After(drainingPollHold): + case <-pollCtx.Done(): + } + stop() + case <-pollCtx.Done(): + } + }() + } + entries, err := s.sched.Take(pollCtx, instance, min(req.Max, maxJobBatch), wait) + if err != nil && pollCtx.Err() == nil { internalError(c, err) return } diff --git a/internal/api/drain_test.go b/internal/api/drain_test.go new file mode 100644 index 0000000..8b728b7 --- /dev/null +++ b/internal/api/drain_test.go @@ -0,0 +1,34 @@ +package api_test + +import ( + "context" + "testing" + "time" + + "github.com/google/uuid" + + "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" +) + +func TestLongPollsEndWhenDraining(t *testing.T) { + e := newEnv(t) + tok, _ := e.platformChecker(t, uniq("eu-")) + drain := make(chan struct{}) + e.api.EndLongPollsOn(drain) + client := agentapi.NewClient(e.srv.URL, tok, uuid.New(), "test").WithHTTPClient(e.srv.Client()) + + go func() { time.Sleep(300 * time.Millisecond); close(drain) }() + start := time.Now() + jobs, err := client.Jobs(context.Background(), 10, 20*time.Second) + if err != nil || len(jobs) != 0 { + t.Fatalf("Jobs() while draining = %v, %v; want no jobs and no error", jobs, err) + } + if took := time.Since(start); took > 3*time.Second { + t.Errorf("a long poll ran %v after draining began, want it to end within about a second", took) + } + // Once draining, polls answer at once. + start = time.Now() + if _, err := client.Jobs(context.Background(), 10, 20*time.Second); err != nil || time.Since(start) > 2*time.Second { + t.Errorf("a poll on a draining instance took %v (%v)", time.Since(start), err) + } +} diff --git a/internal/api/server.go b/internal/api/server.go index c9ccbca..9fc4f78 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -55,6 +55,8 @@ type Server struct { limits *versioned[Limits] // support caches how the platform runs support tickets. support *versioned[supportSettings] + // draining, once closed, ends the long polls of checkers. + draining <-chan struct{} } // Deps are the external resources a Server needs. @@ -94,6 +96,10 @@ func New(cfg config.API, d Deps) *Server { } } +// EndLongPollsOn makes the checkers' long polls return as soon as ch is +// closed, as when the instance starts draining for a shutdown. +func (s *Server) EndLongPollsOn(ch <-chan struct{}) { s.draining = ch } + // Mount installs session loading, locale resolution and CSRF protection and // registers the JSON API and the console pages below them. func (s *Server) Mount(r gin.IRouter) { diff --git a/internal/platform/health.go b/internal/platform/health.go index a9645ef..06ca1b6 100644 --- a/internal/platform/health.go +++ b/internal/platform/health.go @@ -3,6 +3,7 @@ package platform import ( "context" "net/http" + "sync" "sync/atomic" "time" @@ -17,15 +18,24 @@ type Probe func(context.Context) error type Health struct { probes map[string]Probe draining atomic.Bool + drained chan struct{} + once sync.Once } // NewHealth returns a Health that is ready while all probes pass. func NewHealth(probes map[string]Probe) *Health { - return &Health{probes: probes} + return &Health{probes: probes, drained: make(chan struct{})} } // Drain makes /readyz fail permanently. Call it before shutdown. -func (h *Health) Drain() { h.draining.Store(true) } +func (h *Health) Drain() { + h.draining.Store(true) + h.once.Do(func() { close(h.drained) }) +} + +// Draining is closed once Drain is called, so long-running requests can +// end early instead of holding up the shutdown. +func (h *Health) Draining() <-chan struct{} { return h.drained } // Register mounts /healthz (liveness) and /readyz (readiness). func (h *Health) Register(r gin.IRoutes) {