Test placement survival and cached-holder backoff
Distinguish cold placement from a prior holder observation through the real edge front door and activator placement wiring. A paused cold choice is excluded and the survivor takes an HA lease; a cached holder retains the existing ten-second refusal even after its remote lock is released. This is investigation coverage, not a production fix. The cold path passes on main. Immediate recovery from a stale positive holder requires a separate decision about the existing zero-read holder backoff policy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+211
−0
internal/edge/forwardha_export_test.go+29 −0internal/edge/haplacement_wiring_test.go+182 −0
CI passed — run details
diff --git a/internal/edge/forwardha_export_test.go b/internal/edge/forwardha_export_test.go| index 0b54ad493..a498e58cb 100644 |
| --- a/internal/edge/forwardha_export_test.go |
| +++ b/internal/edge/forwardha_export_test.go |
| @@ -2,11 +2,13 @@ package edge |
| import ( |
| "context" |
| + "fmt" |
| "io" |
| "net" |
| "net/http" |
| "sync/atomic" |
| "testing" |
| + "time" |
| ) |
| // The forward scene (forwardha_test.go) for forwardha_wiring_test.go, the |
| @@ -74,3 +76,30 @@ func (s *HAForwardScene) HolderDown(blackholed bool) { |
| return true |
| }) |
| } |
| + |
| +func (s *HAForwardScene) EnablePlacement(t *testing.T) { |
| + t.Helper() |
| + s.x.a.homes.privateDial = true |
| + for _, ref := range []string{"annie", "betty"} { |
| + putJSON(t, s.x.seed, "control/edges/"+ref+".json", fmt.Sprintf( |
| + `{"v":1,"box":%q,"eip":"127.0.0.1","region":"ie","generation":%q,"data_bucket":"bkt","secrets_key_fp":"fp","capabilities":["region-ha-1"],"hibernation_configured":true,"headroom":{"mem_free_mib":50,"mem_total_mib":100,"disk_free_mib":50,"disk_total_mib":100},"hosts":{%q:{"role":"home","account":"abc","app":"shop"}}}`, |
| + ref, time.Now().UTC().Format(time.RFC3339Nano), fwHost)) |
| + } |
| + s.x.converge(t) |
| +} |
| + |
| +func (s *HAForwardScene) PausePeer(t *testing.T) { |
| + t.Helper() |
| + l, err := net.Listen("tcp", "127.0.0.1:0") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { l.Close() }) |
| + old := forwardDial |
| + forwardDial = func(ctx context.Context, network, _ string) (net.Conn, error) { |
| + s.dials.Add(1) |
| + var d net.Dialer |
| + return d.DialContext(ctx, network, l.Addr().String()) |
| + } |
| + t.Cleanup(func() { forwardDial = old }) |
| +} |
diff --git a/internal/edge/haplacement_wiring_test.go b/internal/edge/haplacement_wiring_test.go| new file mode 100644 |
| index 000000000..588004364 |
| --- /dev/null |
| +++ b/internal/edge/haplacement_wiring_test.go |
| @@ -0,0 +1,182 @@ |
| +package edge_test |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "net/http" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/activator" |
| + "github.com/carlosframework/platform/internal/edge" |
| + "github.com/carlosframework/platform/internal/hafresh" |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/lease/locktest" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +type placementAcquire struct { |
| + *activator.Service |
| + mu sync.Mutex |
| + bucket, prefix string |
| + held *lease.Held |
| + acquires int |
| + notes []edge.ForwardResult |
| +} |
| + |
| +func (a *placementAcquire) NoteForward(host string, res edge.ForwardResult) { |
| + a.mu.Lock() |
| + a.notes = append(a.notes, res) |
| + a.mu.Unlock() |
| + a.Service.NoteForward(host, res) |
| +} |
| + |
| +// Keep process launch at the fixture's existing HTTP app. A local dispatch |
| +// must first take the real HA lease against the conditional-write fixture. |
| +func (a *placementAcquire) ServeHTTP(w http.ResponseWriter, r *http.Request, rt registry.Route, next http.Handler) bool { |
| + a.mu.Lock() |
| + defer a.mu.Unlock() |
| + if a.held == nil { |
| + a.acquires++ |
| + h, err := lease.AcquireHA(r.Context(), a.bucket, a.prefix, "eu-west-1", hafresh.Marker{}, hafresh.Proofs{}) |
| + if err != nil { |
| + http.Error(w, err.Error(), 503) |
| + return true |
| + } |
| + a.held = h |
| + } |
| + return false |
| +} |
| + |
| +func TestFrontPlacementPausedPeerUsesSurvivor(t *testing.T) { |
| + testFrontPlacementPausedPeer(t, false) |
| +} |
| + |
| +func TestFrontPlacementCachedHolderKeepsBackoff(t *testing.T) { |
| + testFrontPlacementPausedPeer(t, true) |
| +} |
| + |
| +func testFrontPlacementPausedPeer(t *testing.T, previousHolder bool) { |
| + fx := locktest.NewLockFixture(t) |
| + self := hafresh.Self{Ref: "annie", IID: "i-a", Account: "111122223333", Region: "eu-west-1"} |
| + previous := lease.CurrentSelf() |
| + lease.SetSelf(self) |
| + t.Cleanup(func() { lease.SetSelf(previous) }) |
| + if err := lease.SetObligationDir(t.TempDir()); err != nil { |
| + t.Fatal(err) |
| + } |
| + prefix := activator.HibernatePrefix("", edge.HAForwardHost) |
| + lockGets := func() int { |
| + n := 0 |
| + for _, request := range fx.Requests() { |
| + if request == "GET "+prefix+"/lock.json" { |
| + n++ |
| + } |
| + } |
| + return n |
| + } |
| + body, err := hafresh.MarkerBody(145, hafresh.Marker{V: 1, Box: "betty", IID: "i-b", Clean: true}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.PutLock(prefix, body) |
| + cfg := activator.Config{Bucket: fx.Bucket, Idle: time.Hour, Self: func() hafresh.Self { return self }} |
| + a := &placementAcquire{Service: activator.New(cfg, &activator.ExecBackend{Cfg: cfg}, nil), bucket: fx.Bucket, prefix: prefix} |
| + t.Cleanup(func() { |
| + a.Service.Shutdown(context.Background()) |
| + if a.held != nil { |
| + if err := a.held.Release(context.Background()); err != nil { |
| + t.Error(err) |
| + } |
| + } |
| + }) |
| + x := edge.NewHAForwardScene(t, a) |
| + x.EnablePlacement(t) |
| + if previousHolder { |
| + held, err := json.Marshal(hafresh.Lock{Generation: 145, ExpiresAt: time.Now().Add(time.Minute), Owner: hafresh.OwnerFor(hafresh.Self{Ref: "betty", IID: "i-b"}, 7, true)}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fx.PutLock(prefix, held) |
| + if status, _, _, err := x.Get("/"); err != nil || status != 200 { |
| + t.Fatalf("previous holder: %d %v", status, err) |
| + } |
| + fx.PutLock(prefix, body) |
| + x.HolderDown(false) |
| + a.notes = nil |
| + } |
| + x.PausePeer(t) |
| + type answer struct { |
| + status int |
| + retry, body string |
| + err error |
| + } |
| + answers := make([]answer, 4) |
| + var wg sync.WaitGroup |
| + start := time.Now() |
| + for i := range answers { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + answers[i].status, answers[i].retry, answers[i].body, answers[i].err = x.Get("/") |
| + }() |
| + } |
| + wg.Wait() |
| + for i, got := range answers { |
| + if got.err != nil || got.status != 503 || got.retry != "10" { |
| + t.Fatalf("initial request %d: %+v", i, got) |
| + } |
| + } |
| + if elapsed := time.Since(start); elapsed < 1900*time.Millisecond || elapsed > 5*time.Second { |
| + t.Fatalf("failure bound: %v", elapsed) |
| + } |
| + if a.acquires != 0 { |
| + t.Fatalf("first requests acquired locally: %d", a.acquires) |
| + } |
| + t.Logf("forward outcomes: %+v", a.notes) |
| + if len(a.notes) != len(answers) || lockGets() != 1 { |
| + t.Fatalf("initial burst: %d outcomes, %d lock reads", len(a.notes), lockGets()) |
| + } |
| + wantKind := edge.Placement |
| + if previousHolder { |
| + wantKind = edge.Holder |
| + } |
| + for _, note := range a.notes { |
| + if note.Kind != wantKind || note.Outcome != edge.ForwardUnreachable || note.Ref != edge.HAForwardHolder || note.PeerGen == 0 { |
| + t.Fatalf("forward result: %+v, want unreachable kind %d", note, wantKind) |
| + } |
| + } |
| + dials := x.Dials() |
| + failed := time.Now() |
| + for i := range 4 { |
| + status, retry, body, err := x.Get("/") |
| + if previousHolder { |
| + if err != nil || status != 503 || retry != "10" || body != unreachableBody { |
| + t.Fatalf("cached holder request %d: %d %q %q %v", i, status, retry, body, err) |
| + } |
| + continue |
| + } |
| + if err != nil || status != 200 || body != "annie|"+edge.HAForwardHost+"|/" { |
| + t.Fatalf("survivor request %d: %d Retry-After=%q body=%q err=%v; dials=%d (was %d), acquires=%d", i, status, retry, body, err, x.Dials(), dials, a.acquires) |
| + } |
| + } |
| + wantAcquires := 1 |
| + if previousHolder { |
| + wantAcquires = 0 |
| + } |
| + if time.Since(failed) >= 10*time.Second || x.Dials() != dials || a.acquires != wantAcquires { |
| + t.Fatalf("survivor took %v, dials %d -> %d, acquires %d", time.Since(failed), dials, x.Dials(), a.acquires) |
| + } |
| + if lockGets() != 1+wantAcquires { |
| + t.Fatalf("lock reads: %d, want one observation plus %d acquires", lockGets(), wantAcquires) |
| + } |
| + if previousHolder { |
| + return |
| + } |
| + view, err := lease.ReadLock(t.Context(), fx.Bucket, prefix, "eu-west-1") |
| + owner, ok := hafresh.ParseOwner(view.Lock.Owner) |
| + if err != nil || !ok || !owner.HA || owner.Ref != self.Ref || view.Lock.Generation != 146 { |
| + t.Fatalf("survivor lock: %+v %v", view, err) |
| + } |
| +} |