edge: end the dial hold of a retiring copy and refuse (#511 R28)
Review round 1, Astra (e): a request that selected a replica row before Reload dropped it, and dials after RetireExcept stopped the app, waited out the 13 s hold and failed, though a survivor existed. holdEnded and refuseUnreachedApp now also end the hold, within one step, and refuse with REFUSED_STREAM when the copy is retiring on this box. Reload hands the edge's retiring check to the replicated row's app socket proxy. Only replicated rows (ManyCopies); the no-app-write proof (appNotReachedError) is unchanged, and single-copy rows keep the whole hold. Witness: TestReplicaRetiringDialHoldRefused, through the real genRoundTripper. A POST and a GET are held after the route was selected; the copy retires and its app stops; then they dial and box-01 serves both, within 2 s. Without the retiring arm they wait out the hold. The scale-down witness now waits on request counts, not sleeps.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/edge/edge.go+1 −1internal/edge/genproxy.go+38 −13internal/edge/replica_drain_e2e_test.go+13 −0internal/edge/replica_retire_e2e_test.go+70 −3
Intent
<task-notification> <task-id>a882c6cc125e29e60</task-id> <tool-use-id>toolu_01EyCVS3pH4prm9xyBNiXHqk</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/a882c6cc125e29e60.output</output-file> <status>completed</status> <summary>Agent "Implement R28 scale-down fix" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE_WITH_CONCERNS - **Cause:** when copies dropped to 1, box-2's provision pass deleted its row (`reconcile.go:2646`). Its edge reloaded with no entry for the host, so the requests the hub still sent it became an unknown-host 404 (`edge.go:2504→2541→3742`). The old hub kept sending them until two probes failed, about 1.3 s. - **Fix:** a box now refuses requests (REFUSED_STREAM) for a two-copy host it served and just dropped, for `retiredCopyGrace`. The hub retries them on the survivor (R26). The grace is 60 s, two `hubSetReloadBound` of 30 s. After it, and for any other host, the box answers 404 as before. - **Commits:** a1eeb726 (the fix and its tests), d7adb5fa (census overlay, seal 87c16b80, card clean; `Edge.Reload` re-pinned in `TestGroupNameClassCensus`). Not pushed. - **Tests:** the scale-down test passes 2→1 under GET+POST load through the real hub and box wiring, including the hub's rewrite to one copy, with zero failures. Box-00's late requests are all served by box-01 and its app sees none. The two negatives pass: a host the box never served still gets 404, and 404 returns after the grace. With the refusal turned off, the test fails with drill 4's 404s. The new tests are clean at `-count=10 -cpu=1,2,8` and under `-race`. The edge regression run, grouproll/grouprollaws, TestRegionHA (12), the census tests and all three cross-compiles pass. - **Concerns:** - Deleting a whole instance also refuses briefly, giving a hub 503 instead of a 404. - A new box behind an old hub gives POSTs 502 instead of 404. - A request that picked the route just before the switch and reaches the app after it stops still waits up to 13 s. Not seen in drill 4; I can extend the fix if wanted. - 30 s is my estimate from one drill; the hub has no named reload constant. - The Astra census review is still owed on #280, and drill 5 with the new hub is the live check. - **Report:** /home/paulca/amadan.net/carlos/platform/.claude/worktrees/stateless-replicas-4/.superpowers/sdd/2026-11-01-stateless-replicas-4-deploy-and-drill/task-r28-report.md</result> <usage><subagent_tokens>266645</subagent_tokens><tool_uses>88</tool_uses><duration_ms>1835051</duration_ms></usage> </task-notification>
<task-notification> <task-id>b94c8jgbl</task-id> <tool-use-id>toolu_01YPkof1Bbo7J3QFKBxTbvuf</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/b94c8jgbl.output</output-file> <status>completed</status> <summary>Background command "Astra census review of R28" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>buq7a4nco</task-id> <tool-use-id>toolu_01SR7UQk12zUKEsRhBaxpa87</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/buq7a4nco.output</output-file> <status>completed</status> <summary>Background command "Astra review of R28" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>bnhl2culs</task-id> <tool-use-id>toolu_018inAw6zSra4SsjvBh3gVBS</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bnhl2culs.output</output-file> <status>completed</status> <summary>Background command "Fable review of R28" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>bjpoou29f</task-id> <tool-use-id>toolu_0186pEqPz43gvFbJkbTo1Vf1</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bjpoou29f.output</output-file> <status>completed</status> <summary>Background command "Wait for CI on R28 tip" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 7f30d079c..07cef8306 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -1227,7 +1227,7 @@ func (e *Edge) Reload() error { |
| // (hib); viaTunnel is false in this arm by construction. |
| var p *httputil.ReverseProxy |
| if e.gens != nil && t.ActivatorOwned() { |
| - p, genManaged = appSocketProxyFor(t.Host, e.gens, e.genPools, ManyCopies(t)), true |
| + p, genManaged = appSocketProxyFor(t.Host, e.gens, e.genPools, ManyCopies(t), e.retired.retiring), true |
| } else { |
| p = unixProxy(t.Socket) |
| } |
diff --git a/internal/edge/genproxy.go b/internal/edge/genproxy.go| index 5d71cb27a..795a409ed 100644 |
| --- a/internal/edge/genproxy.go |
| +++ b/internal/edge/genproxy.go |
| @@ -256,6 +256,10 @@ type genRoundTripper struct { |
| // replicated: the route has more than one copy (ManyCopies), so an |
| // outside stop of this box's app may end the dial hold (holdEnded). |
| replicated bool |
| + // retiring, for a replicated route, reports whether host's copy is |
| + // retiring on this box (#511 R28, retiredCopies): its row left the |
| + // map and the activator's RetireExcept stops its app. nil: never. |
| + retiring func(string) bool |
| } |
| // manyCopiesKey marks a request genRoundTripper sends for a route with |
| @@ -263,6 +267,10 @@ type genRoundTripper struct { |
| // every route of the host's generation (holdEnded). |
| type manyCopiesKey struct{} |
| +// retiringKey carries a replicated request's genRoundTripper.retiring to |
| +// the dial hold (holdEnded), for the same reason as manyCopiesKey. |
| +type retiringKey struct{} |
| + |
| // genRetries bounds how often one request is sent again after its |
| // generation moved under it. |
| const genRetries = 2 |
| @@ -283,7 +291,11 @@ func (t *genRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| // on the current generation's pool, a streaming POST with no GetBody |
| // included. |
| if t.replicated { |
| - req = req.WithContext(context.WithValue(req.Context(), manyCopiesKey{}, true)) |
| + ctx := context.WithValue(req.Context(), manyCopiesKey{}, true) |
| + if t.retiring != nil { |
| + ctx = context.WithValue(ctx, retiringKey{}, t.retiring) |
| + } |
| + req = req.WithContext(ctx) |
| } |
| var hb *heldBody |
| if req.Body != nil && req.Body != http.NoBody { |
| @@ -396,7 +408,11 @@ func waitServingGen(ctx context.Context, src GenSource, host string) (GenInfo, b |
| // StoppedOutside), only for a route with more than one copy |
| // (manyCopiesKey), where another copy can serve; within one step. A |
| // single-copy row keeps the whole hold, so an app the activator |
| -// restarts inside it still serves the request. |
| +// restarts inside it still serves the request; or |
| +// - host's copy is retiring on this box (#511 R28, review round 1), |
| +// only for a route with more than one copy, within one step: a |
| +// request that chose the route before Reload dropped it dials after |
| +// RetireExcept stopped its app, and the app will not come back. |
| // |
| // The request then fails with nothing written to the app, and |
| // refuseUnreachedApp refuses it unprocessed: drill 3's box exited about |
| @@ -412,6 +428,9 @@ func holdEnded(ctx context.Context, src GenSource, host string) bool { |
| if many, _ := ctx.Value(manyCopiesKey{}).(bool); !many { |
| return false |
| } |
| + if retiring, _ := ctx.Value(retiringKey{}).(func(string) bool); retiring != nil && retiring(host) { |
| + return true |
| + } |
| so, ok := src.(interface{ StoppedOutside(string) bool }) |
| return ok && so.StoppedOutside(host) |
| } |
| @@ -458,13 +477,17 @@ func (b *heldBody) release() { _ = b.rc.Close() } |
| // generations on: the same Rewrite, Host preservation, upgrade pass-through |
| // and error handler, over genRoundTripper instead of one fixed socket. |
| func appSocketProxy(host string, src GenSource, pools *GenPools) *httputil.ReverseProxy { |
| - return appSocketProxyFor(host, src, pools, false) |
| + return appSocketProxyFor(host, src, pools, false, nil) |
| } |
| // appSocketProxyFor is appSocketProxy for a route with more than one copy |
| -// when replicated (ManyCopies): an outside stop of this box's app may then |
| -// end the dial hold and refuse the request (#511 R27, review round 3). |
| -func appSocketProxyFor(host string, src GenSource, pools *GenPools, replicated bool) *httputil.ReverseProxy { |
| +// when replicated (ManyCopies): an outside stop of this box's app (#511 |
| +// R27, review round 3), or the copy retiring on this box (retiring, R28 |
| +// review round 1), may then end the dial hold and refuse the request. |
| +func appSocketProxyFor(host string, src GenSource, pools *GenPools, replicated bool, retiring func(string) bool) *httputil.ReverseProxy { |
| + if !replicated { |
| + retiring = nil |
| + } |
| return &httputil.ReverseProxy{ |
| Rewrite: func(pr *httputil.ProxyRequest) { |
| setForwarded(pr) |
| @@ -473,9 +496,9 @@ func appSocketProxyFor(host string, src GenSource, pools *GenPools, replicated b |
| pr.Out.Host = pr.In.Host // backend sees the real hostname |
| scrubInboundPurgeHeader(pr) |
| }, |
| - Transport: &genRoundTripper{host: host, src: src, pools: pools, replicated: replicated}, |
| + Transport: &genRoundTripper{host: host, src: src, pools: pools, replicated: replicated, retiring: retiring}, |
| ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { |
| - if refuseUnreachedApp(r, host, src, replicated, err) { |
| + if refuseUnreachedApp(r, host, src, replicated, retiring, err) { |
| return |
| } |
| defaultProxyError(proxyCause)(w, r, err) |
| @@ -503,16 +526,18 @@ func (e *appNotReachedError) Unwrap() error { return e.error } |
| // (appNotReachedError) and either the box's shutdown has begun (its |
| // tunnel drain), or, for a route with more than one copy (replicated), |
| // the host's app last ended without the activator asking it to (src's |
| -// StoppedOutside). The hub then sends the request to another copy, or, |
| -// for a single-copy row, answers its 503. Anything else is answered as |
| -// before. |
| -func refuseUnreachedApp(r *http.Request, host string, src GenSource, replicated bool, err error) bool { |
| +// StoppedOutside) or its copy is retiring on this box (retiring, #511 R28 |
| +// review round 1: RetireExcept stopped the app). The hub then sends the |
| +// request to another copy, or, for a single-copy row, answers its 503. |
| +// Anything else is answered as before. |
| +func refuseUnreachedApp(r *http.Request, host string, src GenSource, replicated bool, retiring func(string) bool, err error) bool { |
| var nr *appNotReachedError |
| if !errors.As(err, &nr) { |
| return false |
| } |
| so, _ := src.(interface{ StoppedOutside(string) bool }) |
| - return RefuseBeforeApp(r, replicated && so != nil && so.StoppedOutside(host)) |
| + gone := so != nil && so.StoppedOutside(host) || retiring != nil && retiring(host) |
| + return RefuseBeforeApp(r, replicated && gone) |
| } |
| // AppSocketProxy is appSocketProxy for callers outside this package: the |
diff --git a/internal/edge/replica_drain_e2e_test.go b/internal/edge/replica_drain_e2e_test.go| index bbf024ab4..a7456950e 100644 |
| --- a/internal/edge/replica_drain_e2e_test.go |
| +++ b/internal/edge/replica_drain_e2e_test.go |
| @@ -751,6 +751,11 @@ type replicaGenActivator struct { |
| handler http.Handler |
| singleCopy bool // box-00's row is a single-copy row, not a two-copy set |
| outside atomic.Bool |
| + // gate, when set, holds each request between the activator taking it |
| + // (the route already selected) and its proxy (the dial), until closed |
| + // (R28 review round 1); gated counts the requests it holds. |
| + gate atomic.Pointer[chan struct{}] |
| + gated atomic.Int32 |
| } |
| // restartApp is the activator starting the app again on its socket. |
| @@ -764,6 +769,14 @@ func newReplicaGenActivator() *replicaGenActivator { |
| } |
| func (a *replicaGenActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt registry.Route, proxy http.Handler) bool { |
| + if g := a.gate.Load(); g != nil { |
| + a.gated.Add(1) |
| + select { |
| + case <-*g: |
| + case <-r.Context().Done(): |
| + return true |
| + } |
| + } |
| proxy.ServeHTTP(w, r) |
| return true |
| } |
diff --git a/internal/edge/replica_retire_e2e_test.go b/internal/edge/replica_retire_e2e_test.go| index 4d0434dfd..17a700d86 100644 |
| --- a/internal/edge/replica_retire_e2e_test.go |
| +++ b/internal/edge/replica_retire_e2e_test.go |
| @@ -70,6 +70,20 @@ func startRetireLoad(srvURL string, workers int) *retireLoad { |
| return l |
| } |
| +// count is how many requests of the load have finished. |
| +func (l *retireLoad) count() int { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + return len(l.res) |
| +} |
| + |
| +// more waits for n more of the load's requests to finish. |
| +func (l *retireLoad) more(t *testing.T, what string, n int) { |
| + t.Helper() |
| + base := l.count() |
| + replicaAwait(t, what, func() bool { return l.count() >= base+n }) |
| +} |
| + |
| func (l *retireLoad) end() []drainResult { |
| close(l.stop) |
| l.wg.Wait() |
| @@ -198,7 +212,7 @@ func TestReplicaRetiredCopyServedBySurvivor(t *testing.T) { |
| if !b0.edge.retired.retiring("shop.example.com") { |
| t.Fatal("box-00 did not record its copy as retiring") |
| } |
| - time.Sleep(300 * time.Millisecond) |
| + load.more(t, "load across the retirement", 40) |
| across := load.end() |
| // Requests box-00 took before its row went may finish there. |
| checkServed(t, "load across the retirement", across, "box-00", "box-01") |
| @@ -252,7 +266,7 @@ func TestReplicaRetiredCopyServedBySurvivor(t *testing.T) { |
| // The hub's own reload: its provision pass rewrites the set to box-01 |
| // alone, under load. |
| load = startRetireLoad(r.url, 4) |
| - time.Sleep(100 * time.Millisecond) |
| + load.more(t, "load before the hub's rewrite", 20) |
| one := registry.Route{Host: "shop.example.com", Kind: registry.KindForward, Region: "fleet/pi", Account: "bab", App: "shop", Stateless: true, |
| FleetBoxes: `["box-01"]`, Copies: 1, ReplicaRevision: 2, ReplicaIncarnation: "life"} |
| if err := r.edge.reg.PutForward(one); err != nil { |
| @@ -261,7 +275,7 @@ func TestReplicaRetiredCopyServedBySurvivor(t *testing.T) { |
| if err := r.edge.Reload(); err != nil { |
| t.Fatal(err) |
| } |
| - time.Sleep(300 * time.Millisecond) |
| + load.more(t, "load across the hub's rewrite", 40) |
| posts, gets := checkServed(t, "load across the hub's rewrite", load.end(), "box-01") |
| if posts == 0 || gets == 0 { |
| t.Fatalf("the load across the hub's rewrite sent %d POSTs and %d GETs", posts, gets) |
| @@ -345,3 +359,56 @@ func TestRetiredCopyNeverServedHost404(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// TestReplicaRetiringDialHoldRefused is #511 R28 review round 1's witness |
| +// (Astra (e)) through the real appSocketProxy, genRoundTripper and dial |
| +// hold on box-00, in production's order: a POST and a GET select box-00's |
| +// route and are taken by its activator (held there, before the dial); |
| +// box-00's copy retires (its row dropped, its edge reloaded); RetireExcept |
| +// stops the app; then they dial. The dial hold ends within a step, both |
| +// are refused unprocessed (refuseUnreachedApp, nothing written to the |
| +// app), and box-01 serves both, well inside the 13 s hold. |
| +// |
| +// Without the retirement arm of holdEnded and refuseUnreachedApp they |
| +// wait out the hold and fail. |
| +func TestReplicaRetiringDialHoldRefused(t *testing.T) { |
| + genAct := newReplicaGenActivator() |
| + r := newReplicaDrainRig(t, drainRigOpts{drains: true, genAct: genAct}) |
| + b0, b1 := r.boxes[0], r.boxes[1] |
| + retire := retireBox00(t, b0) |
| + appBefore, b1Before := b0.backend.Load(), b1.backend.Load() |
| + |
| + gate := make(chan struct{}) |
| + genAct.gate.Store(&gate) |
| + t.Cleanup(func() { |
| + select { |
| + case <-gate: |
| + default: |
| + close(gate) |
| + } |
| + }) |
| + held := []<-chan drainResult{ |
| + parkOn(t, r.url, "POST", "/", "selected", func() bool { return genAct.gated.Load() >= 1 }), |
| + parkOn(t, r.url, "GET", "/", "", func() bool { return genAct.gated.Load() >= 2 }), |
| + } |
| + retire() |
| + genAct.app.stop() // RetireExcept: the activator stops the retired copy's app |
| + start := time.Now() |
| + close(gate) |
| + for _, ch := range held { |
| + res := <-ch |
| + // TestReplicaRetiringDialHoldRefused |
| + if res.code != 200 || res.got != "box-01 "+res.method+" "+res.body { |
| + t.Errorf("a request selected before the retirement: %s %d %q, want box-01's answer", res.method, res.code, res.got) |
| + } |
| + } |
| + if took := time.Since(start); took > 2*time.Second { |
| + t.Fatalf("the dial hold ended %s after the gate opened, want within a step (the hold is %s)", took, localDialHold) |
| + } |
| + if b0.backend.Load() != appBefore { |
| + t.Fatal("box-00's app saw a request selected before the retirement") |
| + } |
| + if n := b1.backend.Load() - b1Before; n < 2 { |
| + t.Fatalf("box-01 served %d of the held requests", n) |
| + } |
| +} |