Region HA dead-holder backoff (P1b-2 follow-up). A forward for a born-HA host that cannot connect to its holder now marks that holder suspect for 10 s (holderSuspectTTL) in the activator's holder cache instead of dropping the entry. While a host is suspect, ForwardTarget answers a refusal (edge.ForwardDecision{Refuse: true}, which replaces the bare ref string) and the edge's frontForwardHA answers it at the front door with the existing unreachable answer, 503, Retry-After: 10, "instance is waking elsewhere; retry shortly", with no lock read, no dial, no note, no local wake and no per-request step (N-HA10 kept). After the window the next request reads lock.json once; if it still names the same box, only the caller that read forwards (one dial), queued callers are refused, and the box is suspect again until that forward's outcome says otherwise. So a dead holder whose lease has not run out costs each box one lock GET and one failed dial per 10 s per host instead of one of each per request. ForwardResult gains Ref (the box forwarded to). A suspect entry is cleared when this box takes the lease, when a forward to any box comes back with an answer (that box becomes the holder), and when an X-Carlos-Holder names a box (or drops on none or this box); an old unreachable forward never overwrites a negative entry or one naming another box. Once the lease expires and OwnerTerminated says the holder is terminated, the first request after the window reads no live holder and P1b-1's takeover serves it here. Legacy hosts, scheduled ticks, regional names and hops are unchanged; no new copy, no new S3 polling. Tests: TestDeadHolderCostsOneReadAndDialPerWindow, TestSuspectWindowEndForwardsOnce, TestSuspectClearedByTakingTheLease, TestSuspectClearedByAHolderHeader, TestSuspectClearedByASuccessfulForward, TestUnreachableKeepsNewerNews, TestSuspectThenExpiredLeaseTakesOver, TestFrontForwardSuspectRefusalRunsNoLocalStep. Census region-ha-dead-holder-backoff proposed (Edge.frontForwardHA body, Service.ForwardTarget sites; activator and edge envelopes re-pinned), awaiting the reviewer. Not reviewed, not released.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+593
−119
docs/superpowers/notes/census-proposal-region-ha-dead-holder-backoff.md+21 −0docs/superpowers/notes/census-region-ha-dead-holder-backoff-claims.md+4 −0internal/activator/hacache.go+107 −32internal/activator/haforward.go+28 −25internal/activator/haforward_test.go+27 −27internal/activator/hasuspect_test.go+272 −0internal/edge/forwardha.go+47 −22internal/edge/forwardha_test.go+76 −11internal/host/haforward_wiring_test.go+2 −2internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv+8 −0
Intent
Attributed to this commit: 13.0 g CO₂e (1,431,494 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-dead-holder-backoff, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/docs/superpowers/notes/census-proposal-region-ha-dead-holder-backoff.md b/docs/superpowers/notes/census-proposal-region-ha-dead-holder-backoff.md| new file mode 100644 |
| index 000000000..1b8e8d667 |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-region-ha-dead-holder-backoff.md |
| @@ -0,0 +1,21 @@ |
| +# Census approval: region-ha-dead-holder-backoff |
| + |
| +Ruling `census-ruling-region-ha-dead-holder-backoff`. 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). |
| + |
| +| unit | change | read sites | what changed | |
| +|---|---|---|---| |
| +| `internal/edge/forwardha.go` `Edge.frontForwardHA` | body | unchanged (5) | ForwardTarget now returns a ForwardDecision (Ref, Refuse); a Refuse is answered at the front door with the existing unreachable answer (503, Retry-After: 10, same body, via answerHolderUnreachable), with no forward, no dial, no NoteForward and no per-request step; the Ref path is unchanged. No new read; the same 5 read sites; root never calls it. | |
| +| `internal/activator/haforward.go` `Service.ForwardTarget` | sites | -registry:Host | Returns edge.ForwardDecision instead of a ref string; the separate holders.get lookup before resolve is folded into holderCache.resolve (one fewer rt.Host site), which now also answers Refuse while the in-memory entry is suspect (a holder that did not answer a forward, for 10 s) with no lock read; after the window the same single bounded (2 s) lock.json read through readHolderView runs as before. No new read and no new S3 request; root never calls it. | |
| + |
| +Envelopes re-pinned: `internal/activator`, `internal/edge`. |
| + |
| +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. |
| + |
| +Recorded dispositions, which each change must still satisfy: |
| + |
| +- `internal/edge/forwardha.go` `Edge.frontForwardHA`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's edge decides at the front door whether to forward a visitor request: reads the route's HA flag and host from the box's own registry row and asks the activator; forwards only to a box in view over the existing signed hop; root never calls it |
| +- `internal/activator/haforward.go` `Service.ForwardTarget`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's activator answers the edge with a holder box ref for an HA route not awake here, from its in-memory holder cache or one bounded (2 s) lock.json read through readHolderView; reads the route's HA flag and host from its own registry row; root never calls it |
| + |
| +Seal: `5b559ba7a42084908cb0fec08083bb2b9b3c9d960036fd10418f81e56304bb64` → `7e4cd4bc7a5633d701ac57057199d3a614556f1f3a9b794b38c7b8ad2f0920df`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/docs/superpowers/notes/census-region-ha-dead-holder-backoff-claims.md b/docs/superpowers/notes/census-region-ha-dead-holder-backoff-claims.md| new file mode 100644 |
| index 000000000..24e3f67ce |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-region-ha-dead-holder-backoff-claims.md |
| @@ -0,0 +1,4 @@ |
| +# CLAIMS for the root-read census, branch region-ha-dead-holder-backoff (region HA P1b-2 follow-up: dead-holder backoff). |
| +# Format (AGENTS.md "The census"): <file>:<unit><TAB><what changed>. Lines starting with # are ignored. |
| +internal/edge/forwardha.go:Edge.frontForwardHA ForwardTarget now returns a ForwardDecision (Ref, Refuse); a Refuse is answered at the front door with the existing unreachable answer (503, Retry-After: 10, same body, via answerHolderUnreachable), with no forward, no dial, no NoteForward and no per-request step; the Ref path is unchanged. No new read; the same 5 read sites; root never calls it. |
| +internal/activator/haforward.go:Service.ForwardTarget Returns edge.ForwardDecision instead of a ref string; the separate holders.get lookup before resolve is folded into holderCache.resolve (one fewer rt.Host site), which now also answers Refuse while the in-memory entry is suspect (a holder that did not answer a forward, for 10 s) with no lock read; after the window the same single bounded (2 s) lock.json read through readHolderView runs as before. No new read and no new S3 request; root never calls it. |
diff --git a/internal/activator/hacache.go b/internal/activator/hacache.go| index c1b73ded7..07c070443 100644 |
| --- a/internal/activator/hacache.go |
| +++ b/internal/activator/hacache.go |
| @@ -12,9 +12,11 @@ import ( |
| // visitor's request there without an S3 request on every one. A positive |
| // 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. The cache is advice, never a lease: every way it can be wrong |
| -// ends in a drop (haforward.go's NoteForward, the acquire in doWake) or, |
| -// at the latest, its lifetime. |
| +// 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. The cache is advice, never a lease: every way it |
| +// can be wrong ends in a drop (haforward.go's NoteForward, the acquire in |
| +// doWake) or, at the latest, its lifetime. |
| const ( |
| // holderCacheMax bounds the cache; the oldest entry set goes first. |
| holderCacheMax = 50_000 |
| @@ -24,6 +26,11 @@ const ( |
| // holderNegativeTTL is how long a miss that found no other holder is |
| // 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. |
| + holderSuspectTTL = 10 * time.Second |
| ) |
| // holderCache is host to holder box reference. The zero value is ready; |
| @@ -39,16 +46,22 @@ 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 |
| } |
| // 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 goes to its waiters but is not cached. |
| +// suspect is the ref of the expired suspect entry the read replaces; a |
| +// read naming it again refuses its waiters (refuse), and only the caller |
| +// that read forwards. |
| type holderFlight struct { |
| - done chan struct{} |
| - ref string |
| - stale bool |
| + done chan struct{} |
| + ref string |
| + suspect string |
| + refuse bool |
| + stale bool |
| } |
| func (c *holderCache) clock() time.Time { |
| @@ -59,8 +72,8 @@ func (c *holderCache) clock() time.Time { |
| } |
| // get is host's live entry: its ref and positive true for a holder, |
| -// positive false for a negative entry, found false for none (an expired |
| -// entry is removed). |
| +// positive false for a negative entry or a suspect one (which keeps its |
| +// ref), found false for none (an expired entry is removed). |
| func (c *holderCache) get(host string) (ref string, positive, found bool) { |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| @@ -77,7 +90,7 @@ func (c *holderCache) getLocked(host string) (string, bool, bool) { |
| c.removeLocked(el) |
| return "", false, false |
| } |
| - return e.ref, e.ref != "", true |
| + return e.ref, e.ref != "" && !e.suspect, true |
| } |
| // set names ref as host's holder for holderPositiveTTL. An empty ref sets |
| @@ -88,7 +101,7 @@ func (c *holderCache) set(host, ref string) { |
| } |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| - c.putLocked(host, ref, holderPositiveTTL) |
| + c.putLocked(host, ref, false, holderPositiveTTL) |
| } |
| // setNegative remembers for holderNegativeTTL that host is not held |
| @@ -96,7 +109,43 @@ func (c *holderCache) set(host, ref string) { |
| func (c *holderCache) setNegative(host string) { |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| - c.putLocked(host, "", holderNegativeTTL) |
| + c.putLocked(host, "", false, holderNegativeTTL) |
| +} |
| + |
| +// suspect marks ref, which did not answer a forward of host's, suspect for |
| +// holderSuspectTTL, unless host's entry is newer news: a negative entry, or |
| +// one naming another box. An empty ref drops the entry. |
| +func (c *holderCache) suspect(host, ref string) { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + el := c.holders[host] |
| + if ref == "" { |
| + if el != nil { |
| + c.removeLocked(el) |
| + } |
| + if f := c.flights[host]; f != nil { |
| + f.stale = true |
| + } |
| + return |
| + } |
| + if el != nil && el.Value.(*holderEntry).ref != ref { |
| + return |
| + } |
| + c.putLocked(host, ref, true, holderSuspectTTL) |
| +} |
| + |
| +// answered follows a forward of host's to ref that came back with an |
| +// answer: a suspect entry is replaced by ref as the holder. Any other entry |
| +// stands. |
| +func (c *holderCache) answered(host, ref string) { |
| + if ref == "" { |
| + return |
| + } |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + if el := c.holders[host]; el != nil && el.Value.(*holderEntry).suspect { |
| + c.putLocked(host, ref, false, holderPositiveTTL) |
| + } |
| } |
| // drop forgets host's entry, and any read in flight for it. |
| @@ -111,7 +160,7 @@ func (c *holderCache) drop(host string) { |
| } |
| } |
| -func (c *holderCache) putLocked(host, ref string, ttl time.Duration) { |
| +func (c *holderCache) putLocked(host, ref string, suspect bool, ttl time.Duration) { |
| if f := c.flights[host]; f != nil { |
| f.stale = true |
| } |
| @@ -121,14 +170,14 @@ func (c *holderCache) putLocked(host, ref string, ttl time.Duration) { |
| expires := c.clock().Add(ttl) |
| if el := c.holders[host]; el != nil { |
| e := el.Value.(*holderEntry) |
| - e.ref, e.expires = ref, expires |
| + e.ref, e.suspect, e.expires = ref, suspect, expires |
| c.order.MoveToBack(el) |
| return |
| } |
| for len(c.holders) >= holderCacheMax { |
| c.removeLocked(c.order.Front()) |
| } |
| - c.holders[host] = c.order.PushBack(&holderEntry{host: host, ref: ref, expires: expires}) |
| + c.holders[host] = c.order.PushBack(&holderEntry{host: host, ref: ref, suspect: suspect, expires: expires}) |
| } |
| func (c *holderCache) removeLocked(el *list.Element) { |
| @@ -136,27 +185,49 @@ func (c *holderCache) removeLocked(el *list.Element) { |
| c.order.Remove(el) |
| } |
| -// resolve answers a miss for host with 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. An entry set while |
| -// a caller queued is answered without a read. |
| -func (c *holderCache) resolve(ctx context.Context, host string, read func() (ref string, ok bool)) string { |
| +// resolve answers a request for host: the holder to forward to, "" 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 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 suspect entry that has expired is a miss too, remembered by its |
| +// flight: when the read names the same box, which may only not have |
| +// renewed yet, the caller that read forwards to it once more, its waiters |
| +// are refused, and the box is suspect again until that forward's outcome |
| +// says otherwise. So a dead holder costs one read and one dial a window. |
| +func (c *holderCache) resolve(ctx context.Context, host string, read func() (ref string, ok bool)) (string, bool) { |
| c.mu.Lock() |
| - if ref, _, found := c.getLocked(host); found { |
| - c.mu.Unlock() |
| - return ref |
| + suspect := "" |
| + if el := c.holders[host]; el != nil { |
| + e := el.Value.(*holderEntry) |
| + if c.clock().Before(e.expires) { |
| + ref, refuse := e.ref, e.suspect |
| + c.mu.Unlock() |
| + if refuse { |
| + return "", true |
| + } |
| + return ref, false |
| + } |
| + if e.suspect { |
| + suspect = e.ref |
| + } |
| + c.removeLocked(el) |
| } |
| if f := c.flights[host]; f != nil { |
| c.mu.Unlock() |
| select { |
| case <-f.done: |
| - return f.ref |
| + if f.refuse { |
| + return "", true |
| + } |
| + return f.ref, false |
| case <-ctx.Done(): |
| - return "" |
| + return "", false |
| } |
| } |
| - f := &holderFlight{done: make(chan struct{})} |
| + f := &holderFlight{done: make(chan struct{}), suspect: suspect} |
| if c.flights == nil { |
| c.flights = map[string]*holderFlight{} |
| } |
| @@ -167,17 +238,21 @@ func (c *holderCache) resolve(ctx context.Context, host string, read func() (ref |
| defer func() { |
| c.mu.Lock() |
| delete(c.flights, host) |
| + again := ok && ref != "" && ref == f.suspect |
| if ok && !f.stale { |
| - if ref != "" { |
| - c.putLocked(host, ref, holderPositiveTTL) |
| - } else { |
| - c.putLocked(host, "", holderNegativeTTL) |
| + switch { |
| + case again: |
| + c.putLocked(host, ref, true, holderSuspectTTL) |
| + case ref != "": |
| + c.putLocked(host, ref, false, holderPositiveTTL) |
| + default: |
| + c.putLocked(host, "", false, holderNegativeTTL) |
| } |
| } |
| - f.ref = ref |
| + f.ref, f.refuse = ref, again |
| c.mu.Unlock() |
| close(f.done) |
| }() |
| ref, ok = read() |
| - return ref |
| + return ref, false |
| } |
diff --git a/internal/activator/haforward.go b/internal/activator/haforward.go| index 4daed63d1..7c458c508 100644 |
| --- a/internal/activator/haforward.go |
| +++ b/internal/activator/haforward.go |
| @@ -24,11 +24,12 @@ import ( |
| // (answerForwarded). |
| // |
| // Nothing here can keep a host from being served for long. A named box |
| -// that is out of view, unreachable, answers "none", or answers 503 without |
| -// naming anyone is dropped at once, and the request is served here or |
| -// retried against a fresh read; 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. |
| +// 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. |
| var _ edge.HAForwarder = (*Service)(nil) |
| @@ -97,33 +98,28 @@ const holderReadTimeout = 2 * time.Second |
| // a host whose read fails reads again. |
| var holderReadErrLogged atomic.Int64 |
| -// ForwardTarget names the box rt's request should be forwarded to, or "" |
| -// to serve it here: always "" for a host that is not born HA, or that is |
| -// awake or waking here (begin's own first look, a live entry). Otherwise |
| -// the cache answers; a miss reads the lock once, however many requests |
| -// miss together, and an unexpired HA lease of another box sets the entry, |
| +// ForwardTarget says what to do with rt's request: forward it to the box |
| +// that holds the host, refuse it while that box is suspect, or serve it |
| +// here. Always here for a host that is not born HA, or that is awake or |
| +// waking here (begin's own first look, a live entry). Otherwise the cache |
| +// answers; a miss reads the lock once, however many requests miss |
| +// together, and an unexpired HA lease of another box sets the entry, |
| // anything else the negative one. A read that fails caches nothing. |
| -func (a *Service) ForwardTarget(ctx context.Context, rt registry.Route) string { |
| +func (a *Service) ForwardTarget(ctx context.Context, rt registry.Route) edge.ForwardDecision { |
| if !rt.HA { |
| - return "" |
| + return edge.ForwardDecision{} |
| } |
| r := holderReaderFor(a.backend, rt) |
| if r == nil { |
| - return "" |
| + return edge.ForwardDecision{} |
| } |
| a.mu.Lock() |
| here := a.entries[rt.Host] != nil |
| a.mu.Unlock() |
| if here { |
| - return "" |
| - } |
| - if ref, positive, found := a.holders.get(rt.Host); found { |
| - if positive { |
| - return ref |
| - } |
| - return "" |
| + return edge.ForwardDecision{} |
| } |
| - return a.holders.resolve(ctx, rt.Host, func() (string, bool) { |
| + ref, refuse := a.holders.resolve(ctx, rt.Host, func() (string, bool) { |
| // The read is every queued caller's, not only this one's: detached |
| // from this request's cancellation, so one visitor hanging up does |
| // not fail the rest, and bounded on its own. |
| @@ -140,6 +136,7 @@ func (a *Service) ForwardTarget(ctx context.Context, rt registry.Route) string { |
| self, _ := boxSelfRef(a.backend) |
| return otherHolder(v, self, a.holders.clock()), true |
| }) |
| + return edge.ForwardDecision{Ref: ref, Refuse: refuse} |
| } |
| // otherHolder is the box v names as holding the host, when that is an |
| @@ -158,13 +155,17 @@ func otherHolder(v hafresh.View, self string, now time.Time) string { |
| } |
| // NoteForward follows a forward's outcome for host: a named box out of |
| -// view or unreachable, an answer of "none", and a 503 that names nobody |
| -// drop the entry; an answer naming a box sets it, unless the box is this |
| -// one, which drops it (this box is never forwarded to). |
| +// view, an answer of "none", and a 503 that names nobody drop the entry; an |
| +// answer naming a box sets it, unless the box is this one, which drops it |
| +// (this box is never forwarded to). A box that could not be connected to |
| +// is suspect for holderSuspectTTL, and any other answer from a box clears |
| +// a suspect entry: that box is the holder. |
| func (a *Service) NoteForward(host string, res edge.ForwardResult) { |
| switch { |
| - case res.Outcome == edge.ForwardNotInView, res.Outcome == edge.ForwardUnreachable: |
| + case res.Outcome == edge.ForwardNotInView: |
| a.holders.drop(host) |
| + case res.Outcome == edge.ForwardUnreachable: |
| + a.holders.suspect(host, res.Ref) |
| case res.Holder == holderNone: |
| a.holders.drop(host) |
| case res.Holder != "": |
| @@ -175,6 +176,8 @@ func (a *Service) NoteForward(host string, res edge.ForwardResult) { |
| a.holders.set(host, res.Holder) |
| case res.Status == http.StatusServiceUnavailable: |
| a.holders.drop(host) |
| + case res.Status != 0: |
| + a.holders.answered(host, res.Ref) |
| } |
| } |
diff --git a/internal/activator/haforward_test.go b/internal/activator/haforward_test.go| index 226c5d6d9..2ee190b0c 100644 |
| --- a/internal/activator/haforward_test.go |
| +++ b/internal/activator/haforward_test.go |
| @@ -51,13 +51,13 @@ func haHeldByB(t *testing.T) *haSvc { |
| func TestForwardTargetNamesTheHolder(t *testing.T) { |
| s := haHeldByB(t) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "b" { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "b" { |
| t.Fatalf("ForwardTarget = %q, want b", got) |
| } |
| if n := lockGets(s.b, s.rt); n != 1 { |
| t.Fatalf("%d lock reads on a miss, want 1", n) |
| } |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "b" { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "b" { |
| t.Fatalf("ForwardTarget on a hit = %q, want b", got) |
| } |
| if n := lockGets(s.b, s.rt); n != 1 { |
| @@ -70,7 +70,7 @@ func TestForwardTargetServesHere(t *testing.T) { |
| s, _ := haServiceEntry(t, "shop.example.com") |
| s.rt.HA = true |
| before := lockGets(s.b, s.rt) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" { |
| t.Fatalf("ForwardTarget for a host awake here = %q", got) |
| } |
| if n := lockGets(s.b, s.rt) - before; n != 0 { |
| @@ -83,18 +83,18 @@ func TestForwardTargetServesHere(t *testing.T) { |
| clk := &fakeClock{t: time.Now()} |
| s.a.holders.now = clk.now |
| putLockFor(t, s.b, s.rt, markerBody(t, "b", "i-b", haFixOf(s.b).lifetime, 5, true)) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" { |
| t.Fatalf("ForwardTarget over a release marker = %q", got) |
| } |
| if _, positive, found := s.a.holders.get(s.rt.Host); !found || positive { |
| t.Fatalf("no negative entry: found %v, positive %v", found, positive) |
| } |
| clk.advance(9 * time.Second) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" || lockGets(s.b, s.rt) != 1 { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" || lockGets(s.b, s.rt) != 1 { |
| t.Fatalf("within 10 s: %q, %d reads, want \"\" and 1", got, lockGets(s.b, s.rt)) |
| } |
| clk.advance(2 * time.Second) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" || lockGets(s.b, s.rt) != 2 { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" || lockGets(s.b, s.rt) != 2 { |
| t.Fatalf("at 11 s: %q, %d reads, want \"\" and 2", got, lockGets(s.b, s.rt)) |
| } |
| }) |
| @@ -102,7 +102,7 @@ func TestForwardTargetServesHere(t *testing.T) { |
| s := haService(t, "shop.example.com") |
| s.rt.HA = true |
| putLockFor(t, s.b, s.rt, leaseBody(t, "box/v1;ref=a;iid=i-a;pid=99;ha=1", time.Now().Add(10*time.Minute))) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" { |
| t.Fatalf("ForwardTarget over this box's own lease = %q", got) |
| } |
| }) |
| @@ -110,21 +110,21 @@ func TestForwardTargetServesHere(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))) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" { |
| t.Fatalf("ForwardTarget over an expired lease = %q", got) |
| } |
| }) |
| t.Run("legacy route", func(t *testing.T) { |
| s := haHeldByB(t) |
| s.rt.HA = false |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" || lockGets(s.b, s.rt) != 0 { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" || lockGets(s.b, s.rt) != 0 { |
| t.Fatalf("legacy route: %q, %d reads", got, lockGets(s.b, s.rt)) |
| } |
| }) |
| t.Run("read error", func(t *testing.T) { |
| s := haHeldByB(t) |
| s.fx.FailNext("GET", http.StatusForbidden) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "" { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "" { |
| t.Fatalf("a failed read = %q", got) |
| } |
| if _, _, found := s.a.holders.get(s.rt.Host); found { |
| @@ -150,7 +150,7 @@ func TestForwardTargetSingleFlight(t *testing.T) { |
| wg.Add(1) |
| go func() { |
| defer wg.Done() |
| - got[i] = s.a.ForwardTarget(context.Background(), s.rt) |
| + got[i] = s.a.ForwardTarget(context.Background(), s.rt).Ref |
| }() |
| } |
| waitFor(t, "the first read", func() bool { return reads.Load() == 1 }) |
| @@ -170,16 +170,16 @@ func TestForwardTargetSingleFlight(t *testing.T) { |
| } |
| } |
| -func TestHolderCacheDroppedOnUnreachable(t *testing.T) { |
| - for _, outcome := range []edge.ForwardOutcome{edge.ForwardUnreachable, edge.ForwardNotInView} { |
| - s := haHeldByB(t) |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "b" { |
| - t.Fatalf("ForwardTarget = %q, want b", got) |
| - } |
| - s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: outcome}) |
| - if _, _, found := s.a.holders.get(s.rt.Host); found { |
| - t.Fatalf("outcome %d kept the entry", outcome) |
| - } |
| +// A box out of view drops the entry. (An unreachable one is suspect: |
| +// hasuspect_test.go.) |
| +func TestHolderCacheDroppedOutOfView(t *testing.T) { |
| + s := haHeldByB(t) |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "b" { |
| + t.Fatalf("ForwardTarget = %q, want b", got) |
| + } |
| + s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardNotInView, Ref: "b"}) |
| + if _, _, found := s.a.holders.get(s.rt.Host); found { |
| + t.Fatal("a box out of view kept the entry") |
| } |
| } |
| @@ -191,7 +191,7 @@ func TestHolderCacheFollowsHolderHeader(t *testing.T) { |
| if ref, positive, _ := s.a.holders.get(host); ref != "c" || !positive { |
| t.Fatalf("after X-Carlos-Holder: c the entry is %q", ref) |
| } |
| - if got := s.a.ForwardTarget(context.Background(), s.rt); got != "c" || lockGets(s.b, s.rt) != 0 { |
| + if got := s.a.ForwardTarget(context.Background(), s.rt).Ref; got != "c" || lockGets(s.b, s.rt) != 0 { |
| t.Fatalf("ForwardTarget = %q with %d reads, want c and none", got, lockGets(s.b, s.rt)) |
| } |
| s.a.NoteForward(host, edge.ForwardResult{Outcome: edge.ForwardDone, Status: 503, Holder: "none"}) |
| @@ -403,13 +403,13 @@ func TestScheduledTickNeverForwards(t *testing.T) { |
| func TestForwardTargetThroughSplitBackend(t *testing.T) { |
| s := haHeldByB(t) |
| a := New(Config{Bucket: s.b.Cfg.Bucket, Idle: time.Hour}, &SplitBackend{Exec: s.b, Unit: &UnitBackend{}}, s.b.Registry) |
| - if got := a.ForwardTarget(context.Background(), s.rt); got != "b" { |
| + if got := a.ForwardTarget(context.Background(), s.rt).Ref; got != "b" { |
| t.Fatalf("ForwardTarget through the split backend = %q, want b", got) |
| } |
| if n := lockGets(s.b, s.rt); n != 1 { |
| t.Fatalf("%d lock reads on a miss, want 1", n) |
| } |
| - if got := a.ForwardTarget(context.Background(), s.rt); got != "b" || lockGets(s.b, s.rt) != 1 { |
| + if got := a.ForwardTarget(context.Background(), s.rt).Ref; got != "b" || lockGets(s.b, s.rt) != 1 { |
| t.Fatalf("a hit: %q with %d reads, want b and 1", got, lockGets(s.b, s.rt)) |
| } |
| // This box's identity is the exec half's: an answer naming it drops. |
| @@ -419,7 +419,7 @@ func TestForwardTargetThroughSplitBackend(t *testing.T) { |
| } |
| unit := s.rt |
| unit.Host, unit.Backing = "unit.example.com", registry.BackingUnit |
| - if got := a.ForwardTarget(context.Background(), unit); got != "" { |
| + if got := a.ForwardTarget(context.Background(), unit).Ref; got != "" { |
| t.Fatalf("a unit-backed row = %q", got) |
| } |
| } |
| @@ -490,7 +490,7 @@ func TestForwardTargetStalledReadIsBounded(t *testing.T) { |
| start := time.Now() |
| got := make(chan string, 10) |
| for range cap(got) { |
| - go func() { got <- s.a.ForwardTarget(context.Background(), s.rt) }() |
| + go func() { got <- s.a.ForwardTarget(context.Background(), s.rt).Ref }() |
| } |
| bound := time.After(holderReadTimeout + 2*time.Second) |
| for range cap(got) { |
| @@ -513,7 +513,7 @@ func TestForwardTargetStalledReadIsBounded(t *testing.T) { |
| t.Fatalf("%d flights left after the stalled read", flights) |
| } |
| t.Setenv("CARLOS_S3_ENDPOINT", s.fx.URL) |
| - if ref := s.a.ForwardTarget(context.Background(), s.rt); ref != "b" || lockGets(s.b, s.rt) != 1 { |
| + if ref := s.a.ForwardTarget(context.Background(), s.rt).Ref; ref != "b" || lockGets(s.b, s.rt) != 1 { |
| t.Fatalf("the lookup after the stall: %q with %d reads, want b and 1", ref, lockGets(s.b, s.rt)) |
| } |
| } |
diff --git a/internal/activator/hasuspect_test.go b/internal/activator/hasuspect_test.go| new file mode 100644 |
| index 000000000..87d1070b9 |
| --- /dev/null |
| +++ b/internal/activator/hasuspect_test.go |
| @@ -0,0 +1,272 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "net/http" |
| + "net/http/httptest" |
| + "sync" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/edge" |
| + "github.com/carlosframework/platform/internal/hafresh" |
| +) |
| + |
| +// 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, so a dead holder whose lease has not run out costs each box |
| +// one lock read and one failed dial per window per host. |
| + |
| +// 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 |
| +// ForwardTarget and says what the edge does: "refused" (the front door's |
| +// 503, Retry-After: 10, TestFrontForwardSuspectRefusalRunsNoLocalStep), |
| +// "unreachable" (a forward to the named box that could not connect, counted |
| +// in forwards and noted as ForwardUnreachable: the same 503, |
| +// TestForwardHADialFailureIs503RetryTen), or "here" (served on this box). |
| +func (s *haSvc) frontDoor(t *testing.T, forwards *int) string { |
| + t.Helper() |
| + d := s.a.ForwardTarget(context.Background(), s.rt) |
| + switch { |
| + case d.Refuse: |
| + if d.Ref != "" { |
| + t.Fatalf("a refusal names %q", d.Ref) |
| + } |
| + return "refused" |
| + case d.Ref == "": |
| + return "here" |
| + } |
| + *forwards++ |
| + s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardUnreachable, Ref: d.Ref}) |
| + return "unreachable" |
| +} |
| + |
| +// wokeHere is whether the host has an entry on this box: awake or waking. |
| +func (s *haSvc) wokeHere() bool { |
| + s.a.mu.Lock() |
| + defer s.a.mu.Unlock() |
| + return s.a.entries[s.rt.Host] != nil |
| +} |
| + |
| +// suspectB is haHeldByB on a fake cache clock, after one request that |
| +// forwarded to b and could not connect: b is suspect. |
| +func suspectB(t *testing.T) (*haSvc, *fakeClock) { |
| + t.Helper() |
| + s := haHeldByB(t) |
| + clk := &fakeClock{t: time.Now()} |
| + s.a.holders.now = clk.now |
| + forwards := 0 |
| + if got := s.frontDoor(t, &forwards); got != "unreachable" || forwards != 1 || lockGets(s.b, s.rt) != 1 { |
| + t.Fatalf("the first request: %s, %d forwards, %d reads; want unreachable, 1 and 1", got, forwards, lockGets(s.b, s.rt)) |
| + } |
| + if got := s.frontDoor(t, &forwards); got != "refused" { |
| + t.Fatalf("the request after an unreachable forward: %s, want refused", got) |
| + } |
| + return s, clk |
| +} |
| + |
| +func TestDeadHolderCostsOneReadAndDialPerWindow(t *testing.T) { |
| + s := haHeldByB(t) |
| + clk := &fakeClock{t: time.Now()} |
| + s.a.holders.now = clk.now |
| + start := clk.now() |
| + forwards := 0 |
| + for i := range 50 { |
| + want := "refused" |
| + if i == 0 { |
| + want = "unreachable" |
| + } |
| + if got := s.frontDoor(t, &forwards); got != want { |
| + t.Fatalf("request %d at %v: %s, want %s", i, clk.now().Sub(start), got, want) |
| + } |
| + 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 s.wokeHere() { |
| + t.Fatal("a refused request woke the host here") |
| + } |
| + |
| + // The window is over: the next request reads the lock once, finds b's |
| + // lease still unexpired, forwards once more, and b is suspect again. |
| + clk.t = start.Add(holderSuspectTTL + time.Millisecond) |
| + 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) |
| + } |
| + for i := range 10 { |
| + clk.advance(500 * time.Millisecond) |
| + if got := s.frontDoor(t, &forwards); got != "refused" { |
| + 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) |
| + } |
| +} |
| + |
| +// Concurrent requests after the window: one lock read, and only the |
| +// caller that read forwards; the rest, queued on the read or arriving |
| +// after it, are refused. |
| +func TestSuspectWindowEndForwardsOnce(t *testing.T) { |
| + s, clk := suspectB(t) |
| + clk.advance(holderSuspectTTL + time.Millisecond) |
| + gate := make(chan struct{}) |
| + var reads atomic.Int32 |
| + old := readHolderLock |
| + readHolderLock = func(ctx context.Context, bucket, prefix, region string) (hafresh.View, error) { |
| + reads.Add(1) |
| + <-gate |
| + return old(ctx, bucket, prefix, region) |
| + } |
| + t.Cleanup(func() { readHolderLock = old }) |
| + var wg sync.WaitGroup |
| + got := make([]edge.ForwardDecision, 20) |
| + for i := range got { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + got[i] = s.a.ForwardTarget(context.Background(), s.rt) |
| + }() |
| + } |
| + waitFor(t, "the read", func() bool { return reads.Load() == 1 }) |
| + time.Sleep(50 * time.Millisecond) // the others queue on it |
| + close(gate) |
| + wg.Wait() |
| + forwards := 0 |
| + for i, d := range got { |
| + switch d { |
| + case edge.ForwardDecision{Ref: "b"}: |
| + forwards++ |
| + case edge.ForwardDecision{Refuse: true}: |
| + default: |
| + t.Fatalf("caller %d: %+v, want b or a refusal", i, d) |
| + } |
| + } |
| + if forwards != 1 || reads.Load() != 1 { |
| + t.Fatalf("%d forwards and %d reads for 20 requests after the window, want 1 and 1", forwards, reads.Load()) |
| + } |
| + if d := s.a.ForwardTarget(context.Background(), s.rt); !d.Refuse { |
| + t.Fatalf("while the one forward runs: %+v, want a refusal", d) |
| + } |
| +} |
| + |
| +func TestSuspectClearedByTakingTheLease(t *testing.T) { |
| + s := haService(t, "shop.example.com") |
| + s.rt.HA = true |
| + s.a.holders.set(s.rt.Host, "b") |
| + s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardUnreachable, Ref: "b"}) |
| + if d := s.a.ForwardTarget(context.Background(), s.rt); !d.Refuse { |
| + t.Fatalf("a suspect holder: %+v, want a refusal", d) |
| + } |
| + e, err := s.wakeForTest(context.Background()) |
| + if err != nil || e == nil { |
| + t.Fatalf("wake: %v", err) |
| + } |
| + if _, _, found := s.a.holders.get(s.rt.Host); found { |
| + t.Fatal("the suspect entry survived this box taking the lease") |
| + } |
| + if d := s.a.ForwardTarget(context.Background(), s.rt); d != (edge.ForwardDecision{}) { |
| + t.Fatalf("awake here: %+v, want served here", d) |
| + } |
| +} |
| + |
| +func TestSuspectClearedByAHolderHeader(t *testing.T) { |
| + s, _ := suspectB(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.a.ForwardTarget(context.Background(), s.rt); d != (edge.ForwardDecision{Ref: "c"}) || 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)) |
| + } |
| + |
| + // Naming this box drops the entry: the next request reads the lock. |
| + s, _ = suspectB(t) |
| + s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardDone, Ref: "c", Status: 503, Holder: "a"}) |
| + if _, _, found := s.a.holders.get(s.rt.Host); found { |
| + t.Fatal("X-Carlos-Holder naming this box kept the suspect entry") |
| + } |
| +} |
| + |
| +func TestSuspectClearedByASuccessfulForward(t *testing.T) { |
| + for _, ref := range []string{"b", "c"} { |
| + 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.a.ForwardTarget(context.Background(), s.rt); d != (edge.ForwardDecision{Ref: ref}) || 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) |
| + } |
| + } |
| + // A forward that came back with no answer (Status 0) proves nothing. |
| + s, _ := suspectB(t) |
| + s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardDone, Ref: "b"}) |
| + if d := s.a.ForwardTarget(context.Background(), s.rt); !d.Refuse { |
| + t.Fatalf("after a forward with no answer: %+v, want still refused", d) |
| + } |
| +} |
| + |
| +// An unreachable forward that began before newer news does not undo it: a |
| +// holder named since, or a lock read that found no holder, stands. |
| +func TestUnreachableKeepsNewerNews(t *testing.T) { |
| + s := haHeldByB(t) |
| + s.a.holders.set(s.rt.Host, "c") |
| + s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardUnreachable, Ref: "b"}) |
| + if d := s.a.ForwardTarget(context.Background(), s.rt); d != (edge.ForwardDecision{Ref: "c"}) { |
| + t.Fatalf("an old forward to b failed after c was named: %+v, want c", d) |
| + } |
| + s.a.holders.setNegative(s.rt.Host) |
| + s.a.NoteForward(s.rt.Host, edge.ForwardResult{Outcome: edge.ForwardUnreachable, Ref: "b"}) |
| + if d := s.a.ForwardTarget(context.Background(), s.rt); d != (edge.ForwardDecision{}) { |
| + t.Fatalf("an old forward to b failed after a read found no holder: %+v, want served here", d) |
| + } |
| +} |
| + |
| +// P1b-1's takeover is still reached: once b's lease has expired and EC2 |
| +// says b is terminated, the first request after the suspect window reads |
| +// the lock, finds no live holder, and is served here, taking the host over. |
| +func TestSuspectThenExpiredLeaseTakesOver(t *testing.T) { |
| + s, clk := suspectB(t) |
| + oracle := &terminatedOracle{yes: map[string]bool{"i-b": true}} |
| + s.b.OwnerTerminated = oracle.terminated |
| + // b is gone and renews nothing: its lease runs out. |
| + putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, time.Now().Add(-time.Minute))) |
| + forwards := 0 |
| + if got := s.frontDoor(t, &forwards); got != "refused" { |
| + t.Fatalf("within the window: %s, want refused", got) |
| + } |
| + if len(oracle.calls()) != 0 || s.wokeHere() { |
| + t.Fatal("a refused request tried a wake here") |
| + } |
| + |
| + clk.advance(holderSuspectTTL + time.Millisecond) |
| + if got := s.frontDoor(t, &forwards); got != "here" || forwards != 0 { |
| + t.Fatalf("after the window: %s with %d forwards, want here and none", got, forwards) |
| + } |
| + served := false |
| + rec := httptest.NewRecorder() |
| + s.a.ServeHTTP(rec, httptest.NewRequest("GET", "http://shop.example.com/", nil), s.rt, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| + served = true |
| + w.WriteHeader(http.StatusOK) |
| + })) |
| + if !served || rec.Code != http.StatusOK { |
| + t.Fatalf("the request after the window: %d, proxied %v; want served here", rec.Code, served) |
| + } |
| + if got := oracle.calls(); len(got) != 1 || got[0] != "i-b" { |
| + t.Fatalf("OwnerTerminated asked about %v, want [i-b]", got) |
| + } |
| + v, err := hafresh.DecodeLock(lockBodyOf(s.b, s.rt), "x") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if o, ok := hafresh.ParseOwner(v.Lock.Owner); !ok || !o.HA || o.Ref != "a" { |
| + t.Fatalf("the lock is %+v, want this box's HA lease", v.Lock) |
| + } |
| + if d := s.a.ForwardTarget(context.Background(), s.rt); d != (edge.ForwardDecision{}) { |
| + t.Fatalf("awake here after the takeover: %+v, want served here", d) |
| + } |
| +} |
diff --git a/internal/edge/forwardha.go b/internal/edge/forwardha.go| index fc8407ec3..56f3179a3 100644 |
| --- a/internal/edge/forwardha.go |
| +++ b/internal/edge/forwardha.go |
| @@ -31,15 +31,26 @@ import ( |
| // it with a type assertion on its Activator; an activator without it, or no |
| // activator, forwards nothing. |
| // |
| -// ForwardTarget names the box a request for rt should go to, or "" to serve |
| -// it here. NoteForward reports how the forward went, so the activator's |
| -// holder cache can follow it. host is rt.Host, the route's own host, never an |
| -// alias the request named. |
| +// ForwardTarget says what to do with a request for rt: forward it to a box, |
| +// refuse it at the front door, or serve it here. NoteForward reports how a |
| +// forward went, so the activator's holder cache can follow it. host is |
| +// rt.Host, the route's own host, never an alias the request named. |
| type HAForwarder interface { |
| - ForwardTarget(ctx context.Context, rt registry.Route) string |
| + ForwardTarget(ctx context.Context, rt registry.Route) ForwardDecision |
| NoteForward(host string, res ForwardResult) |
| } |
| +// ForwardDecision is ForwardTarget's answer. The zero value serves the request |
| +// here. |
| +type ForwardDecision struct { |
| + // Ref is the box to forward the request to. |
| + Ref string |
| + // Refuse answers the request at the front door as an unreachable forward |
| + // is answered (503, Retry-After: 10), without forwarding it or running |
| + // anything else: the host's holder did not answer a forward moments ago. |
| + Refuse bool |
| +} |
| + |
| // ForwardOutcome is how one forward ended. |
| type ForwardOutcome int |
| @@ -48,18 +59,21 @@ const ( |
| // key is mirrored. Nothing was written; the request is served here. |
| ForwardNotInView ForwardOutcome = iota |
| // ForwardUnreachable: the named box could not be connected to within |
| - // forwardDialTimeout. This box answered 503, Retry-After: 10. |
| + // forwardDialTimeout. This box answered 503, Retry-After: 10 |
| + // (answerHolderUnreachable). |
| ForwardUnreachable |
| // ForwardDone: the request went to the named box and its answer, whatever |
| // it was, was relayed, or this box refused the hop itself (Status 0). |
| ForwardDone |
| ) |
| -// ForwardResult is what NoteForward is told. Status is the upstream's status, |
| -// 0 when no answer came back from it (a hop this box refused, or a failure |
| -// after connecting). Holder is the upstream's X-Carlos-Holder, "" when absent. |
| +// ForwardResult is what NoteForward is told. Ref is the box the request was |
| +// sent to (ForwardDecision.Ref). Status is the upstream's status, 0 when no |
| +// answer came back from it (a hop this box refused, or a failure after |
| +// connecting). Holder is the upstream's X-Carlos-Holder, "" when absent. |
| type ForwardResult struct { |
| Outcome ForwardOutcome |
| + Ref string |
| Status int |
| Holder string |
| } |
| @@ -111,9 +125,10 @@ func (e *forwardConnectError) Unwrap() error { return e.err } |
| // route lookup. It reports whether it answered. False, serve here, unless the |
| // request has a local route (the caller's ok), is not a verified hop, a door |
| // call or a tunnel delivery, its route is born HA, activator-managed and not |
| -// reached through a regional name, and the activator names a box. Then the |
| -// request is forwarded, the activator is told how it went, and it is answered |
| -// unless the box was not in view. |
| +// reached through a regional name, and the activator names a box or refuses. |
| +// A refusal is answered here as an unreachable forward is, and nothing is |
| +// dialled or noted. Otherwise the request is forwarded, the activator is told |
| +// how it went, and it is answered unless the box was not in view. |
| func (e *Edge) frontForwardHA(w http.ResponseWriter, r *http.Request, host string, up upstream, hopped, isDoor bool) bool { |
| if hopped || isDoor || hopVerified(r.Context()) || fromTunnel(r.Context()) { |
| return false |
| @@ -125,11 +140,15 @@ func (e *Edge) frontForwardHA(w http.ResponseWriter, r *http.Request, host strin |
| if !ok { |
| return false |
| } |
| - ref := fw.ForwardTarget(r.Context(), up.route) |
| - if ref == "" { |
| + d := fw.ForwardTarget(r.Context(), up.route) |
| + if d.Refuse { |
| + answerHolderUnreachable(w, r) |
| + return true |
| + } |
| + if d.Ref == "" { |
| return false |
| } |
| - res := e.forwardHA(w, r, host, ref) |
| + res := e.forwardHA(w, r, host, d.Ref) |
| fw.NoteForward(up.route.Host, res) |
| return res.Outcome != ForwardNotInView |
| } |
| @@ -145,26 +164,24 @@ func (e *Edge) frontForwardHA(w http.ResponseWriter, r *http.Request, host strin |
| func (e *Edge) forwardHA(w http.ResponseWriter, r *http.Request, host, ref string) ForwardResult { |
| m := e.homes.Load() |
| if m == nil || hopVerified(r.Context()) { |
| - return ForwardResult{Outcome: ForwardNotInView} |
| + return ForwardResult{Outcome: ForwardNotInView, Ref: ref} |
| } |
| dial, ok := m.boxInView(ref, time.Now()) |
| if !ok { |
| - return ForwardResult{Outcome: ForwardNotInView} |
| + return ForwardResult{Outcome: ForwardNotInView, Ref: ref} |
| } |
| key := signingKey(m.meshKeys()) |
| if len(key) == 0 { |
| e.logThrottled("forward-no-mesh-key", fmt.Sprintf("%s is held on %s but no mesh key is mirrored; serving here", host, ref)) |
| - return ForwardResult{Outcome: ForwardNotInView} |
| + return ForwardResult{Outcome: ForwardNotInView, Ref: ref} |
| } |
| - res := ForwardResult{Outcome: ForwardDone} |
| + res := ForwardResult{Outcome: ForwardDone, Ref: ref} |
| onError := func(w http.ResponseWriter, r *http.Request, err error) { |
| var connect *forwardConnectError |
| if errors.As(err, &connect) && r.Context().Err() == nil { |
| e.logThrottled("forward-unreachable "+ref, fmt.Sprintf("forward %s → %s (%s): %v; answering 503 + Retry-After: 10", host, ref, dial, err)) |
| res.Outcome = ForwardUnreachable |
| - w.Header().Set("Retry-After", "10") |
| - MarkAnswered(r) |
| - http.Error(w, "instance is waking elsewhere; retry shortly", http.StatusServiceUnavailable) |
| + answerHolderUnreachable(w, r) |
| return |
| } |
| // After connecting, or the client left: backhaul's bad gateway. |
| @@ -179,6 +196,14 @@ func (e *Edge) forwardHA(w http.ResponseWriter, r *http.Request, host, ref strin |
| return res |
| } |
| +// answerHolderUnreachable is the answer to a request whose holder could not |
| +// be connected to, now or moments ago: 503, Retry-After: 10. |
| +func answerHolderUnreachable(w http.ResponseWriter, r *http.Request) { |
| + w.Header().Set("Retry-After", "10") |
| + MarkAnswered(r) |
| + http.Error(w, "instance is waking elsewhere; retry shortly", http.StatusServiceUnavailable) |
| +} |
| + |
| // forwardTransport is the cached transport to the holder at dial, as |
| // backhaulTransport is per EIP and for the same reasons, with one difference: |
| // connecting (TCP and TLS) has forwardDialTimeout in all, and a failure there |
diff --git a/internal/edge/forwardha_test.go b/internal/edge/forwardha_test.go| index 36526b817..a1a2d0c7d 100644 |
| --- a/internal/edge/forwardha_test.go |
| +++ b/internal/edge/forwardha_test.go |
| @@ -59,9 +59,11 @@ var fwBigBody = strings.Repeat("<p>forwarded and compressible</p>\n", 2048) |
| var fwHosts = []string{fwHost, fwGatedHost, fwAssetHost, fwRateHost, fwLegacyHost} |
| // fwAct is a box's activator: it wakes nothing (the route's TCP upstream |
| -// answers) and, as an HAForwarder, names target and keeps every note. |
| +// answers) and, as an HAForwarder, names target (or refuses, when refuse is |
| +// set) and keeps every note. |
| type fwAct struct { |
| target string |
| + refuse bool |
| asked atomic.Int64 |
| mu sync.Mutex |
| notes []fwNote |
| @@ -87,9 +89,9 @@ func (a *fwAct) ServeHTTP(w http.ResponseWriter, r *http.Request, _ registry.Rou |
| return false |
| } |
| func (a *fwAct) RetireExcept(map[string]bool) {} |
| -func (a *fwAct) ForwardTarget(_ context.Context, _ registry.Route) string { |
| +func (a *fwAct) ForwardTarget(_ context.Context, _ registry.Route) ForwardDecision { |
| a.asked.Add(1) |
| - return a.target |
| + return ForwardDecision{Ref: a.target, Refuse: a.refuse} |
| } |
| func (a *fwAct) NoteForward(host string, res ForwardResult) { |
| a.mu.Lock() |
| @@ -469,7 +471,7 @@ func TestFrontForwardReachesTheHolder(t *testing.T) { |
| if got := x.bAct.asked.Load(); got != 0 { |
| t.Fatalf("betty asked its activator %d times for a hop, want 0", got) |
| } |
| - x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Status: http.StatusOK}}) |
| + x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Ref: "betty", Status: http.StatusOK}}) |
| } |
| func TestFrontForwardRunsNoLocalStep(t *testing.T) { |
| @@ -603,6 +605,69 @@ func TestFrontForwardRunsNoLocalStep(t *testing.T) { |
| }) |
| } |
| +// The activator's refusal (a suspect holder, which did not answer a forward |
| +// moments ago) is answered at the front door as an unreachable forward is: |
| +// 503, Retry-After: 10, the same body, and nothing else. No dial, no note, |
| +// no request to either box's app, and no per-request step on either box: |
| +// not the gate, the asset cache, the guards, the rate limit, usage, |
| +// analytics or request records. |
| +func TestFrontForwardSuspectRefusalRunsNoLocalStep(t *testing.T) { |
| + x := newFwScene(t, fwOpts{}) |
| + x.aAct.refuse = true |
| + var dials atomic.Int64 |
| + old := forwardDial |
| + forwardDial = func(ctx context.Context, network, addr string) (net.Conn, error) { |
| + dials.Add(1) |
| + return old(ctx, network, addr) |
| + } |
| + t.Cleanup(func() { forwardDial = old }) |
| + cookie := gate.CookieName + "=" + gate.Mint(fwGatedHost, x.hash, time.Now().Add(time.Hour)) |
| + calls := x.b.calls.Load() |
| + cases := []struct { |
| + name, host, path string |
| + hdr []string |
| + }{ |
| + {"plain", fwHost, "/", nil}, |
| + {"gated without the cookie", fwGatedHost, "/in", nil}, |
| + {"gated with the cookie", fwGatedHost, "/in", []string{"Cookie", cookie}}, |
| + {"asset", fwAssetHost, "/assets/app.js", nil}, |
| + {"watched path", fwHost, "/.env", nil}, |
| + {"rate 1", fwRateHost, "/", nil}, |
| + {"rate 2", fwRateHost, "/", nil}, |
| + } |
| + for _, c := range cases { |
| + resp, body := x.fetch(t, c.host, c.path, c.hdr...) |
| + if resp.StatusCode != http.StatusServiceUnavailable || resp.Header.Get("Retry-After") != "10" || |
| + body != "instance is waking elsewhere; retry shortly\n" { |
| + t.Fatalf("%s = %d Retry-After %q %q, want the front door's 503", c.name, resp.StatusCode, resp.Header.Get("Retry-After"), body) |
| + } |
| + if v := resp.Header.Values(holderHeader); len(v) != 0 { |
| + t.Fatalf("%s: %s = %q, want none", c.name, holderHeader, v) |
| + } |
| + annie, betty := x.take(t, c.host) |
| + wantNothing(t, c.name, annie) |
| + wantNothing(t, c.name, betty) |
| + } |
| + if n := dials.Load(); n != 0 { |
| + t.Fatalf("%d dials, want none", n) |
| + } |
| + if n := x.b.calls.Load() - calls; n != 0 { |
| + t.Fatalf("betty saw %d requests, want none", n) |
| + } |
| + if notes := x.aAct.noted(); len(notes) != 0 { |
| + t.Fatalf("notes = %+v, want none: nothing was forwarded", notes) |
| + } |
| + if got := x.aAct.asked.Load(); got != int64(len(cases)) { |
| + t.Fatalf("annie asked its activator %d times, want %d", got, len(cases)) |
| + } |
| + if n, m := x.aApp.n.Load(), x.bApp.n.Load(); n != 0 || m != 0 { |
| + t.Fatalf("the apps saw %d and %d requests, want none", n, m) |
| + } |
| + if s := x.aStore.Stats(); s.Entries != 0 || s.Reserved != 0 { |
| + t.Fatalf("annie's asset store holds %+v", s) |
| + } |
| +} |
| + |
| func TestForwardHAHolderLostHost(t *testing.T) { |
| x := newFwScene(t, fwOpts{bettyLacks: true}) |
| resp, body := x.fetch(t, fwHost, "/") |
| @@ -612,7 +677,7 @@ func TestForwardHAHolderLostHost(t *testing.T) { |
| if v := resp.Header.Values(holderHeader); len(v) != 0 { |
| t.Fatalf("%s = %q, want none", holderHeader, v) |
| } |
| - x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Status: http.StatusServiceUnavailable}}) |
| + x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Ref: "betty", Status: http.StatusServiceUnavailable}}) |
| if n := x.aApp.n.Load(); n != 0 { |
| t.Fatalf("annie's app saw %d requests", n) |
| } |
| @@ -751,7 +816,7 @@ func TestForwardHANotInView(t *testing.T) { |
| if n := x.b.calls.Load() - calls; n != 0 { |
| t.Fatalf("betty saw %d requests", n) |
| } |
| - x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardNotInView}}) |
| + x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardNotInView, Ref: "carol"}}) |
| // Served here, it is recorded here. |
| annie, _ := x.take(t, fwHost) |
| if annie.use.Requests != 1 || annie.use.HopRequests != 0 { |
| @@ -782,7 +847,7 @@ func TestForwardHADialFailureIs503RetryTen(t *testing.T) { |
| t.Fatalf("the unreachable answer took %v, want about %v", took, forwardDialTimeout) |
| } |
| notes := x.aAct.noted() |
| - if len(notes) == 0 || notes[len(notes)-1] != (fwNote{fwHost, ForwardResult{Outcome: ForwardUnreachable}}) { |
| + if len(notes) == 0 || notes[len(notes)-1] != (fwNote{fwHost, ForwardResult{Outcome: ForwardUnreachable, Ref: "betty"}}) { |
| t.Fatalf("notes = %+v, want ForwardUnreachable last", notes) |
| } |
| if n := x.bApp.n.Load(); n != 0 { |
| @@ -856,7 +921,7 @@ func TestForwardHAReadsHolderHeader(t *testing.T) { |
| if resp.StatusCode != http.StatusServiceUnavailable || resp.Header.Get(holderHeader) != ref { |
| t.Fatalf("platform refusal = %d %s %q, want betty's 503 naming %s", resp.StatusCode, holderHeader, resp.Header.Get(holderHeader), ref) |
| } |
| - note(t, ForwardResult{Outcome: ForwardDone, Status: http.StatusServiceUnavailable, Holder: ref}) |
| + note(t, ForwardResult{Outcome: ForwardDone, Ref: "betty", Status: http.StatusServiceUnavailable, Holder: ref}) |
| } |
| // An app's own X-Carlos-Holder never leaves the holder: not to the |
| // client, not into the note. |
| @@ -871,7 +936,7 @@ func TestForwardHAReadsHolderHeader(t *testing.T) { |
| if v := resp.Header.Values(holderHeader); len(v) != 0 { |
| t.Fatalf("app %s: the client received %s %q", tc.query, holderHeader, v) |
| } |
| - note(t, ForwardResult{Outcome: ForwardDone, Status: tc.status}) |
| + note(t, ForwardResult{Outcome: ForwardDone, Ref: "betty", Status: tc.status}) |
| } |
| } |
| @@ -899,7 +964,7 @@ func TestForwardHAAppHolderTrailerNeverReachesTheClient(t *testing.T) { |
| t.Fatalf("announced=%s: X-Kept trailer = %q, want 1 (trailers %v): trailers must still cross", announced, got, resp.Trailer) |
| } |
| notes := x.aAct.noted() |
| - if len(notes) != i+1 || notes[i] != (fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Status: http.StatusOK}}) { |
| + if len(notes) != i+1 || notes[i] != (fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Ref: "betty", Status: http.StatusOK}}) { |
| t.Fatalf("notes = %+v, want ForwardDone 200 with no holder", notes) |
| } |
| } |
| @@ -958,5 +1023,5 @@ func TestForwardHAUpgradeSwitchesProtocols(t *testing.T) { |
| } |
| c.Close() |
| laWaitFor(t, "the forward's note", func() bool { return len(x.aAct.noted()) == 1 }) |
| - x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Status: http.StatusSwitchingProtocols}}) |
| + x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Ref: "betty", Status: http.StatusSwitchingProtocols}}) |
| } |
diff --git a/internal/host/haforward_wiring_test.go b/internal/host/haforward_wiring_test.go| index c58a326cf..d190f4e01 100644 |
| --- a/internal/host/haforward_wiring_test.go |
| +++ b/internal/host/haforward_wiring_test.go |
| @@ -48,8 +48,8 @@ func TestAgentActivatorForwards(t *testing.T) { |
| if !ok { |
| t.Fatal("the activator the agent hands the edge is not an edge.HAForwarder") |
| } |
| - if got := fw.ForwardTarget(context.Background(), registry.Route{Host: "shop.example.com"}); got != "" { |
| - t.Fatalf("a host that is not born HA forwards to %q, want served here", got) |
| + if got := fw.ForwardTarget(context.Background(), registry.Route{Host: "shop.example.com"}); got != (edge.ForwardDecision{}) { |
| + t.Fatalf("a host that is not born HA forwards with %+v, want served here", got) |
| } |
| // No activator configured: act stays an interface with no value, and the |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index ef4e6d799..5ba38000b 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -38,3 +38,4 @@ plan14-root-reads-rootfs-check-volumes.tsv 042684bf5af6f61ce8b87f10b68a42b08d131 |
| plan14-root-reads-rootfs-fleet-followups.tsv 78b7e7e30ad3ada44388a4ee16f3950ef888ae03e552477dcbfbb07c51ffb83e |
| plan14-root-reads-rootfs-worker-exit.tsv 7ebc3871ee3c93ff168bfa62d23d6be5a970786c67a9b8aa813e8cc4e39b3d74 |
| plan14-root-reads-region-ha-p1b2.tsv 5b559ba7a42084908cb0fec08083bb2b9b3c9d960036fd10418f81e56304bb64 |
| +plan14-root-reads-region-ha-dead-holder-backoff.tsv 7e4cd4bc7a5633d701ac57057199d3a614556f1f3a9b794b38c7b8ad2f0920df |
diff --git a/internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv b/internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv| new file mode 100644 |
| index 000000000..092f034ee |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-region-ha-dead-holder-backoff.tsv |
| @@ -0,0 +1,8 @@ |
| +# Census overlay region-ha-dead-holder-backoff, written by census-propose (#369). census-ruling-region-ha-dead-holder-backoff, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-region-ha-dead-holder-backoff.md. |
| +# 1 body, 1 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@legacy c2dd6d1295b5358ceef8f50d52b4448ea7a419d1cac8e8c6991927bc0100af04 internal/edge/forwardha.go Edge.frontForwardHA legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's edge decides at the front door whether to forward a visitor request: reads the route's HA flag and host from the box's own registry row and asks the activator; forwards only to a box in view over the existing signed hop; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=ForwardTarget now returns a ForwardDecision (Ref, Refuse); a Refuse is answered at the front door with the existing unreachable answer (503, Retry-After: 10, same body, via answerHolderUnreachable), with no forward, no dial, no NoteForward and no per-request step; the Ref path is unchanged. No new read; the same 5 read sites; root never calls it. registry:HA@225,registry:Host@567,whole:Route@228,whole:Route@405,whole:Route@570 afc079f9a7dcb7369a1d3c88a42feb1304ecde697684c91bde0ecddd1ec276a1 |
| +@legacy feb21c2afaddb5a44c0a7c259b6e1465a7df92ec9f73d291e4233136ef2403db internal/activator/haforward.go Service.ForwardTarget legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's activator answers the edge with a holder box ref for an HA route not awake here, from its in-memory holder cache or one bounded (2 s) lock.json read through readHolderView; reads the route's HA flag and host from its own registry row; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=Returns edge.ForwardDecision instead of a ref string; the separate holders.get lookup before resolve is folded into holderCache.resolve (one fewer rt.Host site), which now also answers Refuse while the in-memory entry is suspect (a holder that did not answer a forward, for 10 s) with no lock read; after the window the same single bounded (2 s) lock.json read through readHolderView runs as before. No new read and no new S3 request; root never calls it. registry:HA@100,registry:Host@262,registry:Host@378,registry:Host@995,whole:Route@176,whole:Route@736 735734490e0a5509c067719fe31bd02c8a62abf855cdb44da72c285a6cb59f37 |
| +internal/edge/forwardha.go Edge.frontForwardHA legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's edge decides at the front door whether to forward a visitor request: reads the route's HA flag and host from the box's own registry row and asks the activator; forwards only to a box in view over the existing signed hop; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=ForwardTarget now returns a ForwardDecision (Ref, Refuse); a Refuse is answered at the front door with the existing unreachable answer (503, Retry-After: 10, same body, via answerHolderUnreachable), with no forward, no dial, no NoteForward and no per-request step; the Ref path is unchanged. No new read; the same 5 read sites; root never calls it. registry:HA@225,registry:Host@567,whole:Route@228,whole:Route@405,whole:Route@570 afc079f9a7dcb7369a1d3c88a42feb1304ecde697684c91bde0ecddd1ec276a1 |
| +internal/activator/haforward.go Service.ForwardTarget legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's activator answers the edge with a holder box ref for an HA route not awake here, from its in-memory holder cache or one bounded (2 s) lock.json read through readHolderView; reads the route's HA flag and host from its own registry row; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=Returns edge.ForwardDecision instead of a ref string; the separate holders.get lookup before resolve is folded into holderCache.resolve (one fewer rt.Host site), which now also answers Refuse while the in-memory entry is suspect (a holder that did not answer a forward, for 10 s) with no lock read; after the window the same single bounded (2 s) lock.json read through readHolderView runs as before. No new read and no new S3 request; root never calls it. registry:HA@100,registry:Host@262,registry:Host@378,registry:Host@995,whole:Route@176,whole:Route@736 735734490e0a5509c067719fe31bd02c8a62abf855cdb44da72c285a6cb59f37 |
| +@coverage internal/activator 50c264d416235d04877e4057962db4200fc1c83533df7687ccb30767d2547494 |
| +@coverage internal/edge 65da495d04961a34d9c83d72a96956c154072d147dc612094399a25554f769a4 |