| index dace05bff..f3af50a7b 100644 |
| --- a/internal/edge/replica_drain_e2e_test.go |
| +++ b/internal/edge/replica_drain_e2e_test.go |
| @@ -46,10 +46,20 @@ type replicaDrainBox struct { |
| inflight chan string // /slow ids that reached the app |
| } |
| |
| -func newReplicaDrainBox(t *testing.T, hubURL, label, token string, drains bool, act *replicaGapActivator) *replicaDrainBox { |
| +func newReplicaDrainBox(t *testing.T, hubURL, label, token string, drains bool, act *replicaGapActivator, genAct *replicaGenActivator) *replicaDrainBox { |
| t.Helper() |
| b := &replicaDrainBox{label: label, admission: &replicaDrainAdmission{}, done: make(chan struct{}), finish: make(chan struct{}), inflight: make(chan string, 8)} |
| appHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + if r.URL.Path == "/written" && label == "box-00" { |
| + // The app reads the request, then drops the connection |
| + // without an answer (TestReplicaDialWrittenIsNotRefused). |
| + io.ReadAll(r.Body) |
| + b.backend.Add(1) |
| + if conn, _, err := w.(http.Hijacker).Hijack(); err == nil { |
| + conn.Close() |
| + } |
| + return |
| + } |
| b.backend.Add(1) |
| body, _ := io.ReadAll(r.Body) |
| if r.URL.Path == "/slow" && label == "box-00" { |
| @@ -68,7 +78,17 @@ func newReplicaDrainBox(t *testing.T, hubURL, label, token string, drains bool, |
| reg := openReg(t) |
| var e *Edge |
| var err error |
| - if act == nil { |
| + if genAct != nil { |
| + // An instance with generations on: the edge reaches it through |
| + // appSocketProxy, the real genRoundTripper and dial hold. |
| + sock := filepath.Join(genSockDir(t), "g1.sock") |
| + genAct.app = startGenApp(t, sock, appHandler) |
| + genAct.switchTo("shop.example.com", GenInfo{Socket: sock, N: 1, Build: "v1"}) |
| + if err := reg.Upsert(registry.Route{Host: "shop.example.com", Kind: "instance", Status: "ready", Account: "bab", App: "shop", Stateless: true, Socket: sock}); err != nil { |
| + t.Fatal(err) |
| + } |
| + e, err = New(reg, genAct) |
| + } else if act == nil { |
| app := httptest.NewServer(appHandler) |
| t.Cleanup(app.Close) |
| if err := reg.Upsert(registry.Route{Host: "shop.example.com", Kind: "service", Status: "ready", Addr: strings.TrimPrefix(app.URL, "http://")}); err != nil { |
| @@ -142,6 +162,7 @@ func replicaDrainRig(t *testing.T, drains bool) (srvURL string, hub *TunnelHub, |
| type drainRigOpts struct { |
| drains, holdGoAway bool |
| act *replicaGapActivator |
| + genAct *replicaGenActivator |
| } |
| |
| type drainRig struct { |
| @@ -214,10 +235,11 @@ func newReplicaDrainRig(t *testing.T, opts drainRigOpts) *drainRig { |
| } |
| for i, label := range []string{"box-00", "box-01"} { |
| var act *replicaGapActivator |
| + var genAct *replicaGenActivator |
| if i == 0 { |
| - act = opts.act |
| + act, genAct = opts.act, opts.genAct |
| } |
| - rig.boxes[i] = newReplicaDrainBox(t, srv.URL, label, "tok-"+label, opts.drains, act) |
| + rig.boxes[i] = newReplicaDrainBox(t, srv.URL, label, "tok-"+label, opts.drains, act, genAct) |
| waitForTunnelState(t, hub, "pi/"+label, "connected", 10*time.Second) |
| } |
| // The jobs started before the boxes connected; let each find its |
| @@ -548,8 +570,26 @@ type drainResult struct { |
| func drainAsync(t *testing.T, srvURL, method, path, body string) <-chan drainResult { |
| ch := make(chan drainResult, 1) |
| go func() { |
| - code, got := drainDo(t, srvURL, method, path, body) |
| - ch <- drainResult{method, body, code, got} |
| + // No t.Fatal off the test's goroutine: a failed request is a |
| + // result with code 0 and the error as its text. |
| + var rd io.Reader |
| + if body != "" { |
| + rd = strings.NewReader(body) |
| + } |
| + req, err := http.NewRequest(method, srvURL+path, rd) |
| + if err != nil { |
| + ch <- drainResult{method, body, 0, err.Error()} |
| + return |
| + } |
| + req.Host = "shop.example.com" |
| + resp, err := (&http.Client{Timeout: 10 * time.Second}).Do(req) |
| + if err != nil { |
| + ch <- drainResult{method, body, 0, err.Error()} |
| + return |
| + } |
| + b, _ := io.ReadAll(resp.Body) |
| + resp.Body.Close() |
| + ch <- drainResult{method, body, resp.StatusCode, string(b)} |
| }() |
| return ch |
| } |
| @@ -685,3 +725,113 @@ func TestReplicaGapRequestsServedBySurvivor(t *testing.T) { |
| t.Fatalf("box-00 refused %d, answered %d, its app saw %d gap requests; want 3, 0, 0", act.refused.Load(), act.answered.Load(), b0.backend.Load()-appBefore) |
| } |
| } |
| + |
| +// replicaGenActivator stands in for a box's activator while it still |
| +// proxies (#511 R27, review round 2): it passes every request to the |
| +// edge's proxy, which for an instance with generations on is the real |
| +// appSocketProxy, genRoundTripper and dial hold, over the fake generation |
| +// source it embeds; and it answers StoppedOutside as the test says. |
| +type replicaGenActivator struct { |
| + *fakeGens |
| + app *genApp |
| + outside atomic.Bool |
| +} |
| + |
| +func newReplicaGenActivator() *replicaGenActivator { |
| + return &replicaGenActivator{fakeGens: newFakeGens(true)} |
| +} |
| + |
| +func (a *replicaGenActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt registry.Route, proxy http.Handler) bool { |
| + proxy.ServeHTTP(w, r) |
| + return true |
| +} |
| +func (a *replicaGenActivator) RetireExcept(map[string]bool) {} |
| +func (a *replicaGenActivator) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) { |
| + return nil, io.EOF |
| +} |
| +func (a *replicaGenActivator) HeldAdmissible(string) bool { return false } |
| +func (a *replicaGenActivator) SetHeldWakeHook(func(string)) {} |
| +func (a *replicaGenActivator) StoppedOutside(string) bool { return a.outside.Load() } |
| + |
| +// TestReplicaDialHoldEndsAndRefuses is R27's app-proxy witness (review |
| +// round 2) through the real genRoundTripper and dial path on box-00: |
| +// systemd stops box-00's app, while the activator still has its |
| +// generation current, so a gap POST and a gap GET wait in dialGenUntil's |
| +// 13 s hold. The hold ends at once when the box's drain closes (or, in the |
| +// second case, as soon as the app is known stopped from outside), the |
| +// requests are refused with REFUSED_STREAM (refuseUnreachedApp: no app |
| +// byte written), and the hub serves both from box-01: zero failures, well |
| +// inside the hold. |
| +func TestReplicaDialHoldEndsAndRefuses(t *testing.T) { |
| + for _, trigger := range []string{"drain", "stopped outside"} { |
| + t.Run(trigger, func(t *testing.T) { |
| + genAct := newReplicaGenActivator() |
| + r := newReplicaDrainRig(t, drainRigOpts{drains: true, genAct: genAct}) |
| + b0 := r.boxes[0] |
| + served := map[string]int{} |
| + for i := 0; i < 4; i++ { |
| + code, body := drainDo(t, r.url, "GET", "/", "") |
| + if code != 200 { |
| + t.Fatalf("before the stop: %d %q", code, body) |
| + } |
| + served[strings.Fields(body)[0]]++ |
| + } |
| + if served["box-00"] == 0 || served["box-01"] == 0 { |
| + t.Fatalf("both copies did not serve: %v", served) |
| + } |
| + appBefore := b0.backend.Load() |
| + genAct.app.stop() |
| + base := b0.reached.Load() |
| + gap := []<-chan drainResult{ |
| + parkOn(t, r.url, "POST", "/", "order", func() bool { return b0.reached.Load() >= base+1 }), |
| + parkOn(t, r.url, "GET", "/", "", func() bool { return b0.reached.Load() >= base+2 }), |
| + } |
| + // Both are in box-00's dial hold now. |
| + time.Sleep(300 * time.Millisecond) |
| + start := time.Now() |
| + switch trigger { |
| + case "drain": |
| + b0.edge.beginShutdown() |
| + case "stopped outside": |
| + genAct.outside.Store(true) |
| + } |
| + for _, ch := range gap { |
| + res := <-ch |
| + // TestReplicaDialHoldEndsAndRefuses |
| + if res.code != 200 || res.got != "box-01 "+res.method+" "+res.body { |
| + t.Errorf("%s: a gap request: %s %d %q, want box-01's answer", trigger, res.method, res.code, res.got) |
| + } |
| + } |
| + if took := time.Since(start); took > 2*time.Second { |
| + t.Fatalf("%s: the hold ended %s after the trigger, want at once (the hold is %s)", trigger, took, localDialHold) |
| + } |
| + if b0.backend.Load() != appBefore { |
| + t.Fatal("box-00's app saw a gap request") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// TestReplicaDialWrittenIsNotRefused is the negative: once the request was |
| +// written to the app (here the app reads it and drops the connection |
| +// without an answer), box-00 answers its 502 and nothing is refused or |
| +// repeated, even with its app stopped from outside. |
| +func TestReplicaDialWrittenIsNotRefused(t *testing.T) { |
| + genAct := newReplicaGenActivator() |
| + r := newReplicaDrainRig(t, drainRigOpts{drains: true, genAct: genAct}) |
| + b0, b1 := r.boxes[0], r.boxes[1] |
| + genAct.outside.Store(true) |
| + for i := 0; i < 10; i++ { |
| + b1Before := b1.backend.Load() |
| + code, got := drainDo(t, r.url, "POST", "/written", "x") |
| + if strings.HasPrefix(got, "box-01 ") { |
| + continue |
| + } |
| + // TestReplicaDialWrittenIsNotRefused |
| + if code != http.StatusBadGateway || b1.backend.Load() != b1Before || b0.backend.Load() == 0 { |
| + t.Fatalf("a POST written to box-00's app: %d %q, box-01 saw %d, box-00's app saw %d", code, got, b1.backend.Load()-b1Before, b0.backend.Load()) |
| + } |
| + return |
| + } |
| + t.Fatal("no POST reached box-00") |
| +} |