region HA: unreachable cached holder re-reads the lock (N-HA15)
Dev drill P1b-3a Step 4b: a box with a stale cached holder (the holder had since released clean) refused for the whole 10 s dead-holder window, so the survivor never served. After a forward to a cached holder fails as unreachable, the backoff now re-reads the lock once per host per window (shared by concurrent requests). Released: placement with the failed box excluded (N-HA12). Held: refuse as before (N-HA9). Budget: +1 GET per host per suspect window. Astra built it, Opus review READY, census approved on #280, CI green. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from region-ha-placement-survivor into main.
11 files changed,
+443
−46
docs/superpowers/notes/census-proposal-region-ha-placement-survivor.md+11 −0internal/activator/hacache.go+39 −21internal/activator/haforward.go+12 −4internal/activator/haplacement_test.go+1 −0internal/activator/hasuspect_test.go+141 −14internal/edge/forwardha_export_test.go+29 −0internal/edge/forwardha_wiring_test.go+7 −6internal/edge/haplacement_wiring_test.go+192 −0internal/host/haplacement_budget_test.go+7 −1internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-region-ha-placement-survivor.tsv+3 −0
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-region-ha-placement-survivor.md b/docs/superpowers/notes/census-proposal-region-ha-placement-survivor.md| new file mode 100644 |
| index 000000000..bf9518b56 |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-region-ha-placement-survivor.md |
| @@ -0,0 +1,11 @@ |
| +# Census approval: region-ha-placement-survivor |
| + |
| +Ruling `census-ruling-region-ha-placement-survivor`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +Envelopes re-pinned: `internal/activator`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Seal: `09ad1d7267b9598c64e89ec0faa7b18839c785c5c63905b398afe681cf9777c6` → `09ad1d7267b9598c64e89ec0faa7b18839c785c5c63905b398afe681cf9777c6`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-region-ha-placement-survivor.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/activator/hacache.go b/internal/activator/hacache.go| index 23426cb13..64144017f 100644 |
| --- a/internal/activator/hacache.go |
| +++ b/internal/activator/hacache.go |
| @@ -15,10 +15,10 @@ import ( |
| // entry names the holder; a negative one says "not held elsewhere, serve |
| // here" and exists only to cap the lock reads of a host that keeps |
| // missing. A suspect one names a holder that did not answer a forward: the |
| -// host's requests are refused until it expires, and the request after that |
| -// reads the lock again. Every entry carries a generation, handed out with |
| -// each forward and renewed by each change, so an outcome older than the |
| -// cache's news is ignored. (How many dials a dead holder costs is bounded |
| +// next request shares one bounded re-read. A released or absent lock can |
| +// use placement; other answers keep refusing until the window expires. |
| +// Every entry carries a generation, handed out with each forward and |
| +// renewed by each change, so outcomes older than the cache are ignored. (How many dials a dead holder costs is bounded |
| // where the dials are made, in the edge's forward transport: |
| // forwardDialGate.) The cache is |
| // advice, never a lease: every way it can be wrong ends in a drop |
| @@ -34,9 +34,9 @@ const ( |
| // remembered: one lock read per host per 10 s per box at most. |
| holderNegativeTTL = 10 * time.Second |
| // holderSuspectTTL is how long a holder that did not answer a forward |
| - // is suspect: its host's requests are refused at the front door, with |
| - // no lock read and no dial, so a dead holder whose lease has not run |
| - // out costs one lock read and one failed dial per host per 10 s per box. |
| + // is suspect. N-HA15 allows one shared lock re-read in this window, |
| + // in addition to discovery. A still-held answer costs no further |
| + // reads or dials until expiry. |
| holderSuspectTTL = 10 * time.Second |
| ) |
| @@ -52,10 +52,11 @@ type holderCache struct { |
| } |
| type holderEntry struct { |
| - host string |
| - ref string // "" for a negative entry |
| - suspect bool // ref did not answer a forward |
| - expires time.Time |
| + host string |
| + ref string // "" for a negative entry |
| + suspect bool // ref did not answer a forward |
| + rechecked bool // the suspect window has spent its N-HA15 read |
| + expires time.Time |
| // gen is the entry's generation, new with every put and every answer |
| // from ref: a forward's failure counts only under it. |
| gen uint64 |
| @@ -67,14 +68,14 @@ type holderEntry struct { |
| // holderFlight is one lock read in flight for a host. stale is set when |
| // the host's entry is set or dropped while the read runs: the read began |
| // before that news, so its answer is not cached and its callers look |
| -// again. suspect is the ref of the expired suspect entry the read |
| -// replaces: if the read fails, the box stays suspect and every caller is |
| -// refused (refuse). cached is whether the read's answer was cached. |
| +// again. suspect is the ref of the suspect entry the read replaces: if |
| +// the read fails, the box stays suspect and every caller is refused. cached is whether the read's answer was cached. |
| type holderFlight struct { |
| done chan struct{} |
| ref string |
| view *hafresh.View |
| suspect string |
| + until time.Time // nonzero for a re-read within the original suspect window |
| refuse bool |
| cached bool |
| stale bool |
| @@ -197,6 +198,7 @@ func (c *holderCache) putLocked(host, ref string, suspect bool, ttl time.Duratio |
| e := el.Value.(*holderEntry) |
| e.ref, e.suspect, e.expires, e.gen = ref, suspect, expires, c.seq |
| e.view, e.attempt = nil, "" |
| + e.rechecked = false |
| e.kind = edge.Holder |
| c.order.MoveToBack(el) |
| return |
| @@ -214,8 +216,8 @@ func (c *holderCache) removeLocked(el *list.Element) { |
| // resolve answers a request for host: the holder to forward to and the |
| // generation to report its outcome under, "" to serve here, or refuse |
| -// while the holder is suspect. A live entry answers at once. A miss is one |
| -// read at a time: the first caller runs read and every caller meanwhile |
| +// while the holder is suspect. N-HA15 revalidates a live suspect once; |
| +// other live entries answer at once. A miss is one read at a time: the first caller runs read and every caller meanwhile |
| // waits for its answer (or its own ctx, answering ""). read names the |
| // holder ("" for none) and ok false when it could not tell, which caches |
| // nothing. A read that replaces an expired suspect entry and fails keeps |
| @@ -262,15 +264,24 @@ func (c *holderCache) resolve(ctx context.Context, host string, read func() hold |
| } |
| c.mu.Lock() |
| suspect := "" |
| + var until time.Time |
| if el := c.holders[host]; el != nil { |
| e := el.Value.(*holderEntry) |
| if c.clock().Before(e.expires) { |
| - r := holderResolution{Ref: e.ref, Refuse: e.suspect, Gen: e.gen, View: e.view} |
| - c.mu.Unlock() |
| - if r.Refuse { |
| + if !e.suspect || e.rechecked { |
| + r := holderResolution{Ref: e.ref, Refuse: e.suspect, Gen: e.gen, View: e.view} |
| + c.mu.Unlock() |
| + if r.Refuse { |
| + return holderResolution{Refuse: true} |
| + } |
| + return r |
| + } |
| + if c.flights[host] == nil && len(c.flights) >= holderCacheMax { |
| + // Keep the suspect entry when no read can be admitted. |
| + c.mu.Unlock() |
| return holderResolution{Refuse: true} |
| } |
| - return r |
| + until = e.expires |
| } |
| if e.suspect { |
| suspect = e.ref |
| @@ -284,7 +295,7 @@ func (c *holderCache) resolve(ctx context.Context, host string, read func() hold |
| c.mu.Unlock() |
| return holderResolution{} |
| } |
| - f = &holderFlight{done: make(chan struct{}), suspect: suspect} |
| + f = &holderFlight{done: make(chan struct{}), suspect: suspect, until: until} |
| if c.flights == nil { |
| c.flights = map[string]*holderFlight{} |
| } |
| @@ -314,6 +325,12 @@ func (c *holderCache) runRead(host string, f *holderFlight, read func() holderRe |
| delete(c.flights, host) |
| switch { |
| case f.stale: |
| + case !f.until.IsZero() && !(r.Err == nil && r.View != nil && (r.View.Absent || r.View.Lock.Release != nil)): |
| + // N-HA15: only a release or absence ends backoff early. Keep the |
| + // original deadline, including on read errors and live owners. |
| + c.putLocked(host, f.suspect, true, f.until.Sub(c.clock())) |
| + c.holders[host].Value.(*holderEntry).rechecked = true |
| + f.refuse = true |
| case r.Err == nil && r.View != nil: |
| ttl := holderNegativeTTL |
| if r.Ref != "" { |
| @@ -324,6 +341,7 @@ func (c *holderCache) runRead(host string, f *holderFlight, read func() holderRe |
| f.cached = true |
| case f.suspect != "": |
| c.putLocked(host, f.suspect, true, holderSuspectTTL) |
| + c.holders[host].Value.(*holderEntry).rechecked = true |
| f.refuse = true |
| } |
| f.ref, f.view = r.Ref, r.View |
diff --git a/internal/activator/haforward.go b/internal/activator/haforward.go| index 56d244027..d877a9757 100644 |
| --- a/internal/activator/haforward.go |
| +++ b/internal/activator/haforward.go |
| @@ -28,9 +28,9 @@ import ( |
| // that is out of view, answers "none", or answers 503 without naming |
| // anyone is dropped at once, and the request is served here or retried |
| // against a fresh read; one that is unreachable is suspect for 10 s, and |
| -// the request after that reads afresh; a box that stops holding the host |
| -// without any of those answers it itself, as a verified hop it can wake |
| -// for. The hour's lifetime bounds the rest. |
| +// one shared re-read can discover a release within that window. A box |
| +// that stops holding the host without any of those answers it itself, as |
| +// a verified hop it can wake for. The hour's lifetime bounds the rest. |
| var _ edge.HAForwarder = (*Service)(nil) |
| @@ -241,7 +241,15 @@ func otherHolder(v hafresh.View, self hafresh.Self, now time.Time) string { |
| // entry: that box is the holder. |
| func (a *Service) NoteForward(host string, res edge.ForwardResult) { |
| self, _ := boxSelfRef(a.backend) |
| - note := func() { a.peers.note(res, a.holders.clock()) } |
| + note := func() { |
| + peerResult := res |
| + if res.Kind == edge.Holder && res.Outcome == edge.ForwardUnreachable { |
| + // N-HA15: a release found by the suspect re-read must not send |
| + // placement back to this failed box. Reuse N-HA12's classifier. |
| + peerResult.Kind = edge.Placement |
| + } |
| + a.peers.note(peerResult, a.holders.clock()) |
| + } |
| if res.Kind != edge.Holder { |
| // Local ownership supersedes routing advice, even after its cache |
| // entry was dropped. Keep this check and the note under the service |
diff --git a/internal/activator/haplacement_test.go b/internal/activator/haplacement_test.go| index 6e0f78544..c32fa8b35 100644 |
| --- a/internal/activator/haplacement_test.go |
| +++ b/internal/activator/haplacement_test.go |
| @@ -193,6 +193,7 @@ func TestPlacementSnapshotIsLazy(t *testing.T) { |
| case "positive": |
| s.a.holders.set(s.rt.Host, "b") |
| case "suspect", "failed revalidation": |
| + putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, clk.now().Add(time.Minute))) |
| s.a.holders.set(s.rt.Host, "b") |
| s.a.holders.mu.Lock() |
| gen := s.a.holders.holders[s.rt.Host].Value.(*holderEntry).gen |
diff --git a/internal/activator/hasuspect_test.go b/internal/activator/hasuspect_test.go| index eee37a0f5..efb596f70 100644 |
| --- a/internal/activator/hasuspect_test.go |
| +++ b/internal/activator/hasuspect_test.go |
| @@ -14,10 +14,9 @@ import ( |
| // Region HA P1b-2 follow-up: a holder that did not answer a forward is |
| // suspect for holderSuspectTTL. While it is, a visitor's request for the |
| -// host is refused at the front door with no lock read, no dial and no |
| -// local wake; with the edge's dial gate (forwardDialGate) a dead holder |
| -// whose lease has not run out costs each box one lock read and one failed |
| -// dial per window per host. |
| +// host shares one N-HA15 re-read, then refuses with no further read, dial |
| +// or local wake if still held. Discovery plus revalidation costs two lock |
| +// reads and one failed dial per window per host per box. |
| // frontDoor is one visitor request for s's host at the edge's front door |
| // (edge.frontForwardHA), over a forward that never connects. It asks |
| @@ -116,8 +115,8 @@ func TestDeadHolderCostsOneReadAndDialPerWindow(t *testing.T) { |
| } |
| clk.advance(190 * time.Millisecond) // 50 requests in 9.5 s |
| } |
| - if n := lockGets(s.b, s.rt); n != 1 || forwards != 1 { |
| - t.Fatalf("50 requests within the window: %d lock reads and %d forwards, want 1 and 1", n, forwards) |
| + if n := lockGets(s.b, s.rt); n != 2 || forwards != 1 { |
| + t.Fatalf("50 requests within the window: %d lock reads and %d forwards, want 2 and 1", n, forwards) |
| } |
| if s.wokeHere() { |
| t.Fatal("a refused request woke the host here") |
| @@ -129,8 +128,8 @@ func TestDeadHolderCostsOneReadAndDialPerWindow(t *testing.T) { |
| if got := s.frontDoor(t, &forwards); got != "unreachable" { |
| t.Fatalf("the request after the window: %s, want unreachable", got) |
| } |
| - if n := lockGets(s.b, s.rt); n != 2 || forwards != 2 { |
| - t.Fatalf("after the window: %d lock reads and %d forwards, want 2 and 2", n, forwards) |
| + if n := lockGets(s.b, s.rt); n != 3 || forwards != 2 { |
| + t.Fatalf("after the window: %d lock reads and %d forwards, want 3 and 2", n, forwards) |
| } |
| for i := range 10 { |
| clk.advance(500 * time.Millisecond) |
| @@ -138,8 +137,8 @@ func TestDeadHolderCostsOneReadAndDialPerWindow(t *testing.T) { |
| t.Fatalf("request %d of the second window: %s, want refused", i, got) |
| } |
| } |
| - if n := lockGets(s.b, s.rt); n != 2 || forwards != 2 { |
| - t.Fatalf("the second window: %d lock reads and %d forwards, want 2 and 2", n, forwards) |
| + if n := lockGets(s.b, s.rt); n != 4 || forwards != 2 { |
| + t.Fatalf("the second window: %d lock reads and %d forwards, want 4 and 2", n, forwards) |
| } |
| } |
| @@ -376,11 +375,13 @@ func TestStaleUnreachableIsIgnored(t *testing.T) { |
| func TestSuspectClearedByTakingTheLease(t *testing.T) { |
| s := haService(t, "shop.example.com") |
| s.rt.HA = true |
| + putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(time.Minute))) |
| s.a.holders.set(s.rt.Host, "b") |
| s.note(s.decide(), edge.ForwardResult{Outcome: edge.ForwardUnreachable}) |
| if d := s.decide(); !d.Refuse { |
| t.Fatalf("a suspect holder: %+v, want a refusal", d) |
| } |
| + putLockFor(t, s.b, s.rt, markerBody(t, "b", "i-b", haFixOf(s.b).lifetime, 5, true)) |
| e, err := s.wakeForTest(context.Background()) |
| if err != nil || e == nil { |
| t.Fatalf("wake: %v", err) |
| @@ -398,8 +399,8 @@ func TestSuspectClearedByAHolderHeader(t *testing.T) { |
| host := s.rt.Host |
| // Another box's answer names c: c is the holder, no read needed. |
| s.a.NoteForward(host, edge.ForwardResult{Outcome: edge.ForwardDone, Ref: "c", Status: 503, Holder: "c"}) |
| - if d := s.decide(); d.Ref != "c" || d.Refuse || lockGets(s.b, s.rt) != 1 { |
| - t.Fatalf("after X-Carlos-Holder: c: %+v with %d reads, want c and 1", d, lockGets(s.b, s.rt)) |
| + if d := s.decide(); d.Ref != "c" || d.Refuse || lockGets(s.b, s.rt) != 2 { |
| + t.Fatalf("after X-Carlos-Holder: c: %+v with %d reads, want c and 2", d, lockGets(s.b, s.rt)) |
| } |
| // Naming this box drops the entry: the next request reads the lock. |
| @@ -415,8 +416,8 @@ func TestSuspectClearedByASuccessfulForward(t *testing.T) { |
| s, _ := suspectB(t) |
| // A forward begun before b went suspect comes back answered. |
| s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardDone, Ref: ref, Status: http.StatusOK}) |
| - if d := s.decide(); d.Ref != ref || d.Refuse || lockGets(s.b, s.rt) != 1 { |
| - t.Fatalf("after %s answered: %+v with %d reads, want %s and 1", ref, d, lockGets(s.b, s.rt), ref) |
| + if d := s.decide(); d.Ref != ref || d.Refuse || lockGets(s.b, s.rt) != 2 { |
| + t.Fatalf("after %s answered: %+v with %d reads, want %s and 2", ref, d, lockGets(s.b, s.rt), ref) |
| } |
| } |
| // A forward that came back with no answer (Status 0) proves nothing. |
| @@ -487,3 +488,129 @@ func TestSuspectThenExpiredLeaseTakesOver(t *testing.T) { |
| t.Fatalf("awake here after the takeover: %+v, want served here", d) |
| } |
| } |
| + |
| +func TestSuspectWindowSharesRevalidation(t *testing.T) { |
| + body, err := hafresh.MarkerBody(145, hafresh.Marker{V: 1, Box: "b", IID: "i-b", Clean: true}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + released, err := hafresh.DecodeLock(body, "released") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, tc := range []struct { |
| + name string |
| + read holderRead |
| + free bool |
| + }{ |
| + {"released", holderRead{View: &released}, true}, |
| + {"absent", holderRead{View: &hafresh.View{Absent: true}}, true}, |
| + {"held", holderRead{Ref: "b", View: &hafresh.View{Lock: hafresh.Lock{ExpiresAt: time.Now().Add(time.Hour)}}}, false}, |
| + {"different holder", holderRead{Ref: "c", View: &hafresh.View{Lock: hafresh.Lock{ExpiresAt: time.Now().Add(time.Hour)}}}, false}, |
| + {"unclassified", holderRead{View: &hafresh.View{}}, false}, |
| + {"error", holderRead{Err: context.DeadlineExceeded}, false}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + clk := &fakeClock{t: time.Now()} |
| + c := holderCache{now: clk.now} |
| + c.set("host", "b") |
| + d, _ := c.peek("host") |
| + c.unreachable("host", d.Gen) |
| + clk.advance(9 * time.Second) |
| + var reads atomic.Int32 |
| + gate := make(chan struct{}) |
| + read := func() holderRead { |
| + reads.Add(1) |
| + <-gate |
| + return tc.read |
| + } |
| + out := make(chan holderResolution, 20) |
| + for range cap(out) { |
| + go func() { out <- c.resolve(t.Context(), "host", read) }() |
| + } |
| + waitFor(t, "shared suspect read", func() bool { return reads.Load() == 1 }) |
| + close(gate) |
| + for range cap(out) { |
| + select { |
| + case got := <-out: |
| + if got.Refuse == tc.free || got.Ref != "" { |
| + t.Fatalf("revalidation: %+v, free=%v", got, tc.free) |
| + } |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("revalidation waiter stuck") |
| + } |
| + } |
| + for range 20 { |
| + got := c.resolve(t.Context(), "host", read) |
| + if got.Refuse == tc.free || got.Ref != "" { |
| + t.Fatalf("cached revalidation: %+v, free=%v", got, tc.free) |
| + } |
| + } |
| + if n := reads.Load(); n != 1 { |
| + t.Fatalf("N-HA15 window read budget: %d, want 1", n) |
| + } |
| + if !tc.free { |
| + clk.advance(time.Second) |
| + if _, hit := c.peek("host"); hit { |
| + t.Fatal("revalidation extended the original suspect deadline") |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestSuspectRevalidationAtFlightLimit(t *testing.T) { |
| + c := holderCache{} |
| + c.set("host", "b") |
| + d, _ := c.peek("host") |
| + c.unreachable("host", d.Gen) |
| + c.flights = make(map[string]*holderFlight, holderCacheMax) |
| + for i := range holderCacheMax { |
| + c.flights[string(rune(i))] = &holderFlight{} |
| + } |
| + for range 2 { |
| + d := c.resolve(t.Context(), "host", func() holderRead { |
| + t.Error("read admitted beyond the flight limit") |
| + return holderRead{} |
| + }) |
| + if !d.Refuse { |
| + t.Fatalf("flight limit lost suspect refusal: %+v", d) |
| + } |
| + } |
| + if d, hit := c.peek("host"); !hit || !d.Refuse { |
| + t.Fatalf("flight limit lost suspect entry: %+v, hit=%v", d, hit) |
| + } |
| +} |
| + |
| +func TestSuspectWindowRevalidationTimeout(t *testing.T) { |
| + s := haHeldByB(t) |
| + s.note(s.decide(), edge.ForwardResult{Outcome: edge.ForwardUnreachable}) |
| + var reads atomic.Int32 |
| + old := readHolderLock |
| + readHolderLock = func(ctx context.Context, _, _, _ string) (hafresh.View, error) { |
| + reads.Add(1) |
| + <-ctx.Done() |
| + return hafresh.View{}, ctx.Err() |
| + } |
| + t.Cleanup(func() { readHolderLock = old }) |
| + ctx, cancel := context.WithCancel(t.Context()) |
| + defer cancel() |
| + first := make(chan edge.ForwardDecision, 1) |
| + start := time.Now() |
| + go func() { first <- s.a.ForwardTarget(ctx, s.rt, nil) }() |
| + waitFor(t, "suspect read", func() bool { return reads.Load() == 1 }) |
| + cancel() |
| + recv(t, first) |
| + got := s.concurrently(20) |
| + for range 20 { |
| + if d := recv(t, got); !d.Refuse { |
| + t.Fatalf("timed-out suspect read: %+v, want refusal", d) |
| + } |
| + } |
| + if elapsed := time.Since(start); elapsed < 1900*time.Millisecond || elapsed > 4*time.Second { |
| + t.Fatalf("suspect read bound: %v", elapsed) |
| + } |
| + if d := s.decide(); !d.Refuse || reads.Load() != 1 || s.wokeHere() { |
| + t.Fatalf("after timeout: %+v, reads=%d, local wake=%v", d, reads.Load(), s.wokeHere()) |
| + } |
| +} |
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/forwardha_wiring_test.go b/internal/edge/forwardha_wiring_test.go| index dd059620a..62ce9d6dc 100644 |
| --- a/internal/edge/forwardha_wiring_test.go |
| +++ b/internal/edge/forwardha_wiring_test.go |
| @@ -84,12 +84,13 @@ func deadHolderGuard(t *testing.T, blackholed bool) { |
| t.Fatalf("request %d: %d Retry-After %q %q %v, want 503, 10", i, a.status, a.retry, a.body, a.err) |
| } |
| } |
| - if n, m := x.Dials()-dials, lockGets()-reads; n != 1 || m != 0 { |
| - t.Fatalf("20 concurrent requests for a gone holder: %d dials and %d lock reads, want 1 and 0", n, m) |
| + if n, m := x.Dials()-dials, lockGets()-reads; n != 1 || m > 1 { |
| + t.Fatalf("20 concurrent requests for a gone holder: %d dials and %d lock reads, want 1 dial and at most 1 N-HA15 read", n, m) |
| } |
| - // Within the window: refused at the front door, nothing dialled or read. |
| - dials, reads = x.Dials(), lockGets() |
| + // N-HA15 spends one shared re-read, during the burst or afterward. |
| + // The rest of the window has no reads or dials. |
| + dials = x.Dials() |
| start := time.Now() |
| for i := range 10 { |
| status, retry, got, err := x.Get("/") |
| @@ -100,7 +101,7 @@ func deadHolderGuard(t *testing.T, blackholed bool) { |
| if took := time.Since(start); took >= 10*time.Second { |
| t.Fatalf("the sequential requests took %v, past the window", took) |
| } |
| - if n, m := x.Dials()-dials, lockGets()-reads; n != 0 || m != 0 { |
| - t.Fatalf("sequential requests within the window: %d dials and %d lock reads, want 0 and 0", n, m) |
| + if n, m := x.Dials()-dials, lockGets()-reads; n != 0 || m != 1 { |
| + t.Fatalf("sequential requests within the window: %d dials and %d lock reads, want 0 dials and 1 shared N-HA15 read", n, m) |
| } |
| } |
diff --git a/internal/edge/haplacement_wiring_test.go b/internal/edge/haplacement_wiring_test.go| new file mode 100644 |
| index 000000000..0f7e37d8a |
| --- /dev/null |
| +++ b/internal/edge/haplacement_wiring_test.go |
| @@ -0,0 +1,192 @@ |
| +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, false) |
| +} |
| + |
| +func TestFrontPlacementCachedHolderKeepsBackoff(t *testing.T) { |
| + testFrontPlacementPausedPeer(t, true, false) |
| +} |
| + |
| +func TestFrontPlacementCachedHolderStillHeld(t *testing.T) { |
| + testFrontPlacementPausedPeer(t, true, true) |
| +} |
| + |
| +func testFrontPlacementPausedPeer(t *testing.T, previousHolder, stillHeld 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) |
| + } |
| + if !stillHeld { |
| + 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 stillHeld { |
| + 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 stillHeld { |
| + 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) |
| + } |
| + wantReads := 1 + wantAcquires |
| + if previousHolder { |
| + wantReads++ // N-HA15: one shared re-read in the suspect window. |
| + } |
| + if lockGets() != wantReads { |
| + t.Fatalf("lock reads: %d, want %d including %d acquires", lockGets(), wantReads, wantAcquires) |
| + } |
| + if stillHeld { |
| + 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) |
| + } |
| +} |
diff --git a/internal/host/haplacement_budget_test.go b/internal/host/haplacement_budget_test.go| index 2a489effa..901ca34e1 100644 |
| --- a/internal/host/haplacement_budget_test.go |
| +++ b/internal/host/haplacement_budget_test.go |
| @@ -151,7 +151,13 @@ func testColdPlacementBudgetRows(t *testing.T) { |
| if !d.Refuse { |
| t.Fatal("true holder suspect did not refuse", d) |
| } |
| - coldBudget(t, a, 1, 0, 0) |
| + // N-HA15: an unreachable cached holder costs one bounded re-read per |
| + // suspect window; a still-held lock keeps refusing with no further read. |
| + coldBudget(t, a, 2, 0, 0) |
| + if d = a.call(coldCommand{Op: "forward", Snapshot: s}).Decision; !d.Refuse { |
| + t.Fatal("true holder suspect stopped refusing", d) |
| + } |
| + coldBudget(t, a, 2, 0, 0) |
| }) |
| t.Run("recovery-suspect-keeps-negative", func(t *testing.T) { |
| f := newColdPlacementProcesses(t) |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 01be941a5..09e41a0a4 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -68,3 +68,4 @@ plan14-root-reads-devbox-identity.tsv 66a5df05f18f70c3162ad05aa316c1e69c0f053d54 |
| plan14-root-reads-region-ha-home-placement.tsv b0304f75d2c5f1b9818d0f282b89c00c00b33e3f9bf974031687527c191fb161 |
| plan14-root-reads-storedoor-stream-get.tsv 7e30dee4fb52370555ad173512d7f03782f0fbed725a8a6bf4f7e31f52c23668 |
| plan14-root-reads-design-pdf-service.tsv 09ad1d7267b9598c64e89ec0faa7b18839c785c5c63905b398afe681cf9777c6 |
| +plan14-root-reads-region-ha-placement-survivor.tsv 09ad1d7267b9598c64e89ec0faa7b18839c785c5c63905b398afe681cf9777c6 |
diff --git a/internal/provision/testdata/plan14-root-reads-region-ha-placement-survivor.tsv b/internal/provision/testdata/plan14-root-reads-region-ha-placement-survivor.tsv| new file mode 100644 |
| index 000000000..1d113bab6 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-region-ha-placement-survivor.tsv |
| @@ -0,0 +1,3 @@ |
| +# Census overlay region-ha-placement-survivor, written by census-propose (#369). census-ruling-region-ha-placement-survivor, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-region-ha-placement-survivor.md. |
| +# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@coverage internal/activator a85259a61115e948d84a473397501831fab0d03434ea04f852dd6d6ff81384ec |