carlos / platform Public

edge: HA dispatch follows the selected path route (#186)

routeHAIngress gains routeName: the name whose route every HA decision is
judged under (spec 5.7). ServeHTTP passes sel.haRouteName(host): the
selected local target's route host (a platform-alias target's instance),
else the request host. The HA projection, name resolution, local claim,
catch-all fallback and blocked set use routeName; host stays the wire Host
for moveGate and frontForwardHAIngress. A selected target is claimed as
its own route and loses the platform alias's exemptions, so it is judged
as a request naming its route would be.

path_across_ha_test.go: region HA on the three-box path scene (T born-HA
in B and C's pool, H born-HA on A): from the front box, from a pool box
that does not serve the front, through a non-holder pool box's holder
503, through a handover, plus front-HA skips for remote, local and
platform-alias targets and the no-prefix control.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 3eb45b145b23dc517d6dd1530af0f42c56232230 parent ec13d59
5 files changed, +672 −17
  • internal/edge/edge.go +1 −1
  • internal/edge/haingress.go +27 −14
  • internal/edge/haingress_internal_test.go +2 −2
  • internal/edge/path_across_ha_test.go +629 −0
  • internal/edge/pathselect.go +13 −0
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 23203c5a4..59bd61717 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -2362,7 +2362,7 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if sel.Remote != nil && e.servePathRemote(w, r, host, sel.Remote) {
return
}
- if e.routeHAIngress(w, r, host, up, ok) {
+ if e.routeHAIngress(w, r, host, sel.haRouteName(host), up, ok) {
return
}
// Resolve local aliases and catch-alls against peer ownership before
diff --git a/internal/edge/haingress.go b/internal/edge/haingress.go
index 5f4c6bd95..95e97e52f 100644
--- a/internal/edge/haingress.go
+++ b/internal/edge/haingress.go
@@ -43,27 +43,40 @@ func (e *Edge) localPoolEligible(rt registry.Route) bool {
// routeHAIngress runs after region admission and before any miss or alias
// backhaul. Name authority selects the primary; metadata never becomes an upstream.
-func (e *Edge) routeHAIngress(w http.ResponseWriter, r *http.Request, host string, up upstream, local bool) bool {
- rt, known := e.ingressRoute(host)
+//
+// host is the request's own name: the wire Host every forward and handover
+// carries and signs (moveGate, frontForwardHAIngress). routeName is the name
+// whose route every HA decision here is judged under (path routes, spec
+// §5.7): the HA ingress projection, the name resolution and its local claim,
+// and the blocked set. It is host, except when a prefix on host selected a
+// local target (requestSelection.haRouteName), when it is the target's route
+// host: a platform-alias target's is its instance's. A selected target is
+// judged as a request naming routeName is: claimed as its own route, and
+// without the platform alias's exemptions, which belong to a request naming
+// the alias (its entry is how the target was reached, not its identity).
+func (e *Edge) routeHAIngress(w http.ResponseWriter, r *http.Request, host, routeName string, up upstream, local bool) bool {
+ named := routeName == host
+ viaPlatformAlias := up.platformAlias && named
+ rt, known := e.ingressRoute(routeName)
var claims []edgesfile.NameClaim
if local {
- name := host
- if !e.servesExactly(host) {
+ name := routeName
+ if !e.servesExactly(routeName) {
name = up.route.Host
}
class := edgesfile.NameRoute
target := ""
- if up.alias {
+ if up.alias && named {
class = edgesfile.NameAlias
target = up.route.Host
}
claims = append(claims, edgesfile.NameClaim{Name: name, Class: class, TargetHost: target, Account: registry.EffectiveAccount(up.route.Account), App: up.route.App, Incarnation: up.route.Incarnation, ServingSeq: up.route.ServingSeq, Stamp: int64(up.route.ClaimStamp), Local: true, Eligible: true})
}
- resolved := e.homes.Load().resolveName(host, claims, false)
- name := host
- if !known && resolved.Authority.Name != host {
- if i := strings.IndexByte(host, '.'); i > 0 {
- name = "*" + host[i:]
+ resolved := e.homes.Load().resolveName(routeName, claims, false)
+ name := routeName
+ if !known && resolved.Authority.Name != routeName {
+ if i := strings.IndexByte(routeName, '.'); i > 0 {
+ name = "*" + routeName[i:]
rt, known = e.ingressRoute(name)
}
}
@@ -75,7 +88,7 @@ func (e *Edge) routeHAIngress(w http.ResponseWriter, r *http.Request, host strin
class, target = edgesfile.NameAlias, rt.Host
}
claims = append(claims, edgesfile.NameClaim{Name: name, Class: class, TargetHost: target, Account: rt.Account, App: rt.App, Incarnation: rt.Incarnation, ServingSeq: rt.ServingSeq, Local: true, Eligible: true})
- resolved = e.homes.Load().resolveName(host, claims, false)
+ resolved = e.homes.Load().resolveName(routeName, claims, false)
}
if !known && resolved.State != edgesfile.NameAbsent {
primary := resolved.Authority.Name
@@ -84,14 +97,14 @@ func (e *Edge) routeHAIngress(w http.ResponseWriter, r *http.Request, host strin
}
rt, known = e.ingressRoute(primary)
}
- if local && (!up.hibernating || up.route.IsForward() || up.platformAlias || up.viaTunnel) && known && rt.Host == "" {
+ if local && (!up.hibernating || up.route.IsForward() || viaPlatformAlias || up.viaTunnel) && known && rt.Host == "" {
return false
}
if local && up.route.HA && e.localPoolEligible(up.route) && (!known || rt.Host == "" || !rt.HA) {
return false
}
if !known {
- if local && up.hibernating && !up.route.IsForward() && !up.platformAlias && !up.viaTunnel && !e.localPoolEligible(up.route) {
+ if local && up.hibernating && !up.route.IsForward() && !viaPlatformAlias && !up.viaTunnel && !e.localPoolEligible(up.route) {
HAUnavailableResponse(w, r)
return true
}
@@ -104,7 +117,7 @@ func (e *Edge) routeHAIngress(w http.ResponseWriter, r *http.Request, host strin
HAUnavailableResponse(w, r)
return true
}
- if blocked := e.haIngressBlocked.Load(); blocked != nil && ((*blocked)[host] || (*blocked)[rt.Host]) {
+ if blocked := e.haIngressBlocked.Load(); blocked != nil && ((*blocked)[routeName] || (*blocked)[rt.Host]) {
HAUnavailableResponse(w, r)
return true
}
diff --git a/internal/edge/haingress_internal_test.go b/internal/edge/haingress_internal_test.go
index 51e8647d8..89f98e528 100644
--- a/internal/edge/haingress_internal_test.go
+++ b/internal/edge/haingress_internal_test.go
@@ -326,7 +326,7 @@ func TestPoolGateCannotFallBackLocal(t *testing.T) {
up.platformAlias = true
}
w := httptest.NewRecorder()
- handled := x.a.edge.routeHAIngress(w, httptest.NewRequest(http.MethodGet, "https://"+fwHost+"/", nil), fwHost, up, true)
+ handled := x.a.edge.routeHAIngress(w, httptest.NewRequest(http.MethodGet, "https://"+fwHost+"/", nil), fwHost, fwHost, up, true)
if handled != (kind == "managed") {
t.Fatalf("handled=%v status=%d", handled, w.Code)
}
@@ -358,7 +358,7 @@ func TestHAIngressZeroProjectionPreservesUnmanagedUpstream(t *testing.T) {
up.platformAlias = true
}
w := httptest.NewRecorder()
- handled := x.a.edge.routeHAIngress(w, httptest.NewRequest(http.MethodGet, "https://"+fwHost+"/", nil), fwHost, up, true)
+ handled := x.a.edge.routeHAIngress(w, httptest.NewRequest(http.MethodGet, "https://"+fwHost+"/", nil), fwHost, fwHost, up, true)
if handled != (kind == "managed") {
t.Fatalf("handled=%v status=%d", handled, w.Code)
}
diff --git a/internal/edge/path_across_ha_test.go b/internal/edge/path_across_ha_test.go
new file mode 100644
index 000000000..db565a270
--- /dev/null
+++ b/internal/edge/path_across_ha_test.go
@@ -0,0 +1,629 @@
+package edge
+
+// Region HA on the selected path route (#186, spec §5.7). Once a prefix on
+// the front name H (hub.example) selects a target T (tenant.example), every
+// HA decision for the request is T's: the ingress routing, the blocked
+// state, the holder choice, the front forward and the handover. H stays the
+// wire Host and the name every hop is signed for.
+//
+// The scene is Task 8's (newPathAcrossScene) with region HA added:
+//
+// A (annie): serves H; with hub set, H is born-HA too (pool "hub")
+// B (betty): serves T, born-HA in pool "ha"; publishes T (stamp 5)
+// C (cindy): serves T in the same pool (its own backend); publishes T
+// (stamp 4, so B stays the name's published owner)
+//
+// Every box's activator is acrossHAAct: it names each route's lease holder
+// (holders), answers a hop for a route another box holds with the holder's
+// 503, and records the route host of every HA question it is asked. Every
+// box's HA ingress projection (SetHAIngressLookup) knows the born-HA routes
+// and records each name it is asked about. Front forwards (forwardDial) go
+// to the box whose address they dial, counted per address.
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "maps"
+ "net"
+ "net/http"
+ "net/http/httptest"
+ "slices"
+ "strings"
+ "sync"
+ "sync/atomic"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/boxchoice"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// acrossHAAct is one box's activator: an HAForwarder naming holders[route],
+// and a server that answers a request for a route another box holds as a
+// non-holder pool box does (503 naming the holder). It records the route
+// host of every question.
+type acrossHAAct struct {
+ box string
+ mu sync.Mutex
+ holders map[string]string // route host -> lease holder ref
+ asked []string // ForwardTarget's rt.Host, in order
+ served []string // ServeHTTP's rt.Host, in order
+ noted []string // NoteForward's host, in order
+}
+
+var _ HAForwarder = (*acrossHAAct)(nil)
+
+func (a *acrossHAAct) holder(host string) string {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ return a.holders[host]
+}
+
+func (a *acrossHAAct) ServeHTTP(w http.ResponseWriter, r *http.Request, rt registry.Route, _ http.Handler) bool {
+ a.mu.Lock()
+ a.served = append(a.served, rt.Host)
+ a.mu.Unlock()
+ if h := a.holder(rt.Host); h != "" && h != a.box {
+ w.Header().Set(holderHeader, h)
+ w.Header().Set("Retry-After", "1")
+ MarkAnswered(r)
+ http.Error(w, "held on "+h, http.StatusServiceUnavailable)
+ return true
+ }
+ return false
+}
+func (a *acrossHAAct) RetireExcept(map[string]bool) {}
+func (a *acrossHAAct) AdmitHeldLeg(context.Context, registry.Route) (HeldLeg, error) {
+ return nil, errors.New("asleep")
+}
+func (a *acrossHAAct) HeldAdmissible(string) bool { return false }
+func (a *acrossHAAct) SetHeldWakeHook(func(string)) {}
+
+func (a *acrossHAAct) ForwardTarget(_ context.Context, rt registry.Route, _ func() boxchoice.WakeSnapshot) ForwardDecision {
+ a.mu.Lock()
+ a.asked = append(a.asked, rt.Host)
+ a.mu.Unlock()
+ if h := a.holder(rt.Host); h != "" && h != a.box {
+ return ForwardDecision{Ref: h, Kind: Holder}
+ }
+ return ForwardDecision{}
+}
+
+func (a *acrossHAAct) NoteForward(host string, _ ForwardResult) {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ a.noted = append(a.noted, host)
+}
+
+// record is a copy of asked, served and noted.
+func (a *acrossHAAct) record() (asked, served, noted []string) {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ return slices.Clone(a.asked), slices.Clone(a.served), slices.Clone(a.noted)
+}
+
+// acrossHAMover is acrossHAAct moving one route: a replayable hop for it is
+// answered MoveTaken, and confirmed, naming to.
+type acrossHAMover struct {
+ *acrossHAAct
+ moving, to string
+ hosts []string // MoveState's rt.Host, in order
+}
+
+var _ HAMover = (*acrossHAMover)(nil)
+
+func (m *acrossHAMover) MoveState(rt registry.Route, ask MoveAsk) MoveDecision {
+ m.mu.Lock()
+ m.hosts = append(m.hosts, rt.Host)
+ m.mu.Unlock()
+ switch {
+ case rt.Host != m.moving:
+ return MoveDecision{}
+ case ask.Hop && ask.Replayable:
+ return MoveDecision{Phase: MoveTaken, Holder: m.to, Gen: 2}
+ }
+ return MoveDecision{Phase: MoveRefused}
+}
+
+func (m *acrossHAMover) ConfirmMoveHolder(context.Context, registry.Route, func() boxchoice.WakeSnapshot) MoveDecision {
+ return MoveDecision{Phase: MoveTaken, Holder: m.to, Gen: 2}
+}
+
+func (m *acrossHAMover) moveHosts() []string {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ return slices.Clone(m.hosts)
+}
+
+// acrossHALookup is a box's HA ingress projection, recording each name asked.
+type acrossHALookup struct {
+ routes map[string]registry.Route
+ mu sync.Mutex
+ asked []string
+}
+
+func (l *acrossHALookup) lookup(host string) (registry.Route, bool) {
+ l.mu.Lock()
+ defer l.mu.Unlock()
+ l.asked = append(l.asked, host)
+ rt, ok := l.routes[host]
+ return rt, ok
+}
+
+// set adds a route to the projection.
+func (l *acrossHALookup) set(host string, rt registry.Route) {
+ l.mu.Lock()
+ defer l.mu.Unlock()
+ l.routes[host] = rt
+}
+
+// names is every distinct name asked, sorted, and forgets them.
+func (l *acrossHALookup) names() []string {
+ l.mu.Lock()
+ defer l.mu.Unlock()
+ out := slices.Clone(l.asked)
+ l.asked = nil
+ slices.Sort(out)
+ return slices.Compact(out)
+}
+
+type acrossHAOpts struct {
+ // target: T born-HA on B and C, lease held by tHolder.
+ target bool
+ tHolder string
+ // hub: H born-HA on A (pool "hub"), lease held by hubHolder.
+ hub bool
+ hubHolder string
+}
+
+// pathAcrossHAScene is the three-edge scene with region HA.
+type pathAcrossHAScene struct {
+ *pathAcrossScene
+ tenantC *acrossApp // C's backend for T
+ acts map[*backhaulEdge]*acrossHAAct
+ lookups map[*backhaulEdge]*acrossHALookup
+ // forwards counts front-forward dials per destination IP.
+ forwards sync.Map
+}
+
+// acrossHARoute is a born-HA instance route of the across account.
+func acrossHARoute(host, app, pool string) registry.Route {
+ rt := acrossInstance(host, acrossAcct, app, "")
+ rt.HA, rt.Hibernate, rt.Pool, rt.Incarnation = true, true, pool, 1
+ return rt
+}
+
+func withAddr(rt registry.Route, addr string) registry.Route {
+ rt.Addr = addr
+ return rt
+}
+
+func newPathAcrossHAScene(t *testing.T, o acrossHAOpts) *pathAcrossHAScene {
+ t.Helper()
+ s := &pathAcrossHAScene{pathAcrossScene: newPathAcrossScene(t), acts: map[*backhaulEdge]*acrossHAAct{}, lookups: map[*backhaulEdge]*acrossHALookup{}}
+ ha := map[string]registry.Route{}
+ if o.target {
+ ha[acrossTenant] = acrossHARoute(acrossTenant, "tenant", "ha")
+ }
+ if o.hub {
+ ha[acrossHub] = acrossHARoute(acrossHub, "hub", "hub")
+ }
+ byIP := map[string]string{}
+ for _, be := range s.boxes() {
+ byIP[s.ips[be]] = be.tlsAddr
+ }
+ old := forwardDial
+ forwardDial = func(ctx context.Context, network, addr string) (net.Conn, error) {
+ ip, _, err := net.SplitHostPort(addr)
+ if err != nil {
+ return nil, err
+ }
+ n, _ := s.forwards.LoadOrStore(ip, new(atomic.Int64))
+ n.(*atomic.Int64).Add(1)
+ var d net.Dialer
+ return d.DialContext(ctx, network, byIP[ip])
+ }
+ t.Cleanup(func() { forwardDial = old })
+ for _, be := range s.boxes() {
+ act := &acrossHAAct{box: be.box, holders: map[string]string{acrossTenant: o.tHolder, acrossHub: o.hubHolder}}
+ s.setAct(t, be, act, act)
+ l := &acrossHALookup{routes: maps.Clone(ha)}
+ be.edge.SetHAIngressLookup(l.lookup)
+ s.lookups[be] = l
+ }
+ if o.hub {
+ s.haRoute(t, s.hub, withAddr(ha[acrossHub], s.hubApp.addr()))
+ }
+ if o.target {
+ s.tenantC = newAcrossApp(t, "tenant-c")
+ s.haRoute(t, s.tenant, withAddr(ha[acrossTenant], s.tenantApp.addr()))
+ s.haRoute(t, s.third, withAddr(ha[acrossTenant], s.tenantC.addr()))
+ s.publish(t, s.third, acrossTenant, acrossRouteEntry(acrossAcct, "tenant", 4))
+ }
+ for _, be := range s.boxes() {
+ s.lookups[be].names()
+ }
+ return s
+}
+
+// haRoute writes rt on be as a fresh row (an existing row keeps its
+// lifetime pair on Upsert) and reloads.
+func (s *pathAcrossHAScene) haRoute(t *testing.T, be *backhaulEdge, rt registry.Route) {
+ t.Helper()
+ if err := be.reg.Delete(rt.Host); err != nil {
+ t.Fatal(err)
+ }
+ s.route(t, be, rt)
+ if got, err := be.reg.Get(rt.Host); err != nil || got.Incarnation != rt.Incarnation || !got.HA {
+ t.Fatalf("fixture: %s on %s is %+v (%v), want incarnation %d, HA", rt.Host, be.box, got, err, rt.Incarnation)
+ }
+}
+
+// setAct makes act be's activator (rec is its recording half) and reloads.
+func (s *pathAcrossHAScene) setAct(t *testing.T, be *backhaulEdge, act Activator, rec *acrossHAAct) {
+ t.Helper()
+ be.act, be.edge.act = act, act
+ s.acts[be] = rec
+ s.reload(t, be)
+}
+
+// forwardsTo is how many front-forward dials went to ip.
+func (s *pathAcrossHAScene) forwardsTo(ip string) int64 {
+ n, ok := s.forwards.Load(ip)
+ if !ok {
+ return 0
+ }
+ return n.(*atomic.Int64).Load()
+}
+
+// forwardsAny is every front-forward dial.
+func (s *pathAcrossHAScene) forwardsAny() int64 {
+ var total int64
+ for _, be := range s.boxes() {
+ total += s.forwardsTo(s.ips[be])
+ }
+ return total
+}
+
+// block puts names in be's HA ingress blocked set (as Reload derives it for
+// a held, importing or refused route), on top of what its last reload
+// stored.
+func block(be *backhaulEdge, names ...string) {
+ blocked := map[string]bool{}
+ if cur := be.edge.haIngressBlocked.Load(); cur != nil {
+ for k, v := range *cur {
+ blocked[k] = v
+ }
+ }
+ for _, n := range names {
+ blocked[n] = true
+ }
+ be.edge.haIngressBlocked.Store(&blocked)
+}
+
+// heldHolder is the backhaul holder be remembers for host, "" when none.
+func heldHolder(be *backhaulEdge, host string) string {
+ c := &be.edge.backhaulHolders
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ if el := c.entries[host]; el != nil {
+ return el.Value.(backhaulHolderEntry).ref
+ }
+ return ""
+}
+
+func wantNames(t *testing.T, what string, got []string, want ...string) {
+ t.Helper()
+ if want == nil {
+ want = []string{}
+ }
+ if got == nil {
+ got = []string{}
+ }
+ if !slices.Equal(got, want) {
+ t.Fatalf("%s: %v, want %v", what, got, want)
+ }
+}
+
+// From the front's own box: the Remote hop goes to T's published owner B,
+// T's lease holder, which judges the arrived hop by T's HA identity (not
+// H's, though H is born-HA too) and serves it. A runs no front HA step.
+func TestPathAcrossHATargetFromFrontBox(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{target: true, tHolder: "betty", hub: true, hubHolder: "annie"})
+ s.get(t, s.hub, acrossHub, "/acme/x").want(t, http.StatusOK, "tenant-1|"+acrossHub+"|/acme/x")
+ s.tenantApp.wantSeen(t, acrossSeen{acrossHub, "/acme/x"})
+ s.tenantC.wantSeen(t)
+ s.hubApp.wantSeen(t)
+ wantNames(t, "names B's HA ingress asked", s.lookups[s.tenant].names(), acrossTenant)
+ _, served, _ := s.acts[s.tenant].record()
+ wantNames(t, "routes B's activator served", served, acrossTenant)
+ asked, _, _ := s.acts[s.hub].record()
+ wantNames(t, "routes A's forwarder was asked about", asked)
+ wantNames(t, "names A's HA ingress asked", s.lookups[s.hub].names())
+ if b, c := s.dialsFrom(s.hub, acrossIPTenant), s.dialsFrom(s.hub, acrossIPThird); b != 1 || c != 0 {
+ t.Fatalf("A dialled B %d and C %d times, want 1 and 0", b, c)
+ }
+ if n := s.forwardsAny(); n != 0 {
+ t.Fatalf("%d front forwards, want 0", n)
+ }
+}
+
+// From a pool box that does not serve the front (C, case 3): T is local but
+// B holds its lease, so C front-forwards to B under T's identity with the
+// wire Host H, and B serves. H's own HA dispatch (its holder is A) never
+// runs.
+func TestPathAcrossHATargetFromThirdBox(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{target: true, tHolder: "betty", hub: true, hubHolder: "annie"})
+ s.get(t, s.third, acrossHub, "/acme/x").want(t, http.StatusOK, "tenant-1|"+acrossHub+"|/acme/x")
+ s.tenantApp.wantSeen(t, acrossSeen{acrossHub, "/acme/x"})
+ s.tenantC.wantSeen(t)
+ s.hubApp.wantSeen(t)
+ asked, served, noted := s.acts[s.third].record()
+ wantNames(t, "routes C's forwarder was asked about", asked, acrossTenant)
+ wantNames(t, "routes C's activator served", served)
+ if len(noted) == 0 || slices.ContainsFunc(noted, func(h string) bool { return h != acrossTenant }) {
+ t.Fatalf("C noted forwards for %v, want %s only", noted, acrossTenant)
+ }
+ wantNames(t, "names C's HA ingress asked", s.lookups[s.third].names(), acrossTenant)
+ if b, a := s.forwardsTo(acrossIPTenant), s.forwardsTo(acrossIPHub); b != 1 || a != 0 {
+ t.Fatalf("front forwards to B %d and A %d, want 1 and 0", b, a)
+ }
+ if n := s.dialsFromAny(s.third); n != 0 {
+ t.Fatalf("C backhauled %d times, want 0", n)
+ }
+}
+
+// B, T's published owner, is not T's holder: its 503 names C. A follows to
+// C once, with the wire Host H; C serves; A remembers C as T's holder (not
+// H's), so the next request goes straight to C.
+func TestPathAcrossHATargetFromNonHolderPoolBox(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{target: true, tHolder: "cindy", hub: true, hubHolder: "annie"})
+ s.get(t, s.hub, acrossHub, "/acme/one").want(t, http.StatusOK, "tenant-c|"+acrossHub+"|/acme/one")
+ s.tenantApp.wantSeen(t)
+ s.tenantC.wantSeen(t, acrossSeen{acrossHub, "/acme/one"})
+ s.hubApp.wantSeen(t)
+ _, served, _ := s.acts[s.tenant].record()
+ wantNames(t, "routes B's activator judged", served, acrossTenant)
+ if b, c := s.dialsFrom(s.hub, acrossIPTenant), s.dialsFrom(s.hub, acrossIPThird); b != 1 || c != 1 {
+ t.Fatalf("A dialled B %d and C %d times, want 1 and 1", b, c)
+ }
+ if got := heldHolder(s.hub, acrossTenant); got != "cindy" {
+ t.Fatalf("A remembers %q as %s's holder, want cindy", got, acrossTenant)
+ }
+ if got := heldHolder(s.hub, acrossHub); got != "" {
+ t.Fatalf("A remembers %q as %s's holder, want none", got, acrossHub)
+ }
+ // Requests each box received (dials do not count a reused connection).
+ if b, c := s.tenant.calls.Load(), s.third.calls.Load(); b != 1 || c != 1 {
+ t.Fatalf("B received %d and C %d requests, want 1 and 1", b, c)
+ }
+ s.get(t, s.hub, acrossHub, "/acme/two").want(t, http.StatusOK, "tenant-c|"+acrossHub+"|/acme/two")
+ if b, c := s.tenant.calls.Load(), s.third.calls.Load(); b != 1 || c != 2 {
+ t.Fatalf("after the holder was learned, B received %d and C %d requests, want 1 and 2", b, c)
+ }
+ for _, be := range []*backhaulEdge{s.tenant, s.third} {
+ if n := s.dialsFromAny(be); n != 0 {
+ t.Fatalf("%s dialled %d times on arrived hops, want 0", be.box, n)
+ }
+ }
+ if n := s.forwardsAny(); n != 0 {
+ t.Fatalf("%d front forwards, want 0", n)
+ }
+}
+
+// B holds T and is handing it to C: A's marked first hop gets B's handover
+// answer, decided on T (the mover is asked about T only), and A follows it
+// to C, which C's claim on T authorises; C serves, and A remembers C under T.
+func TestPathAcrossHAHandoverFollowKeyedOnTarget(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{target: true, tHolder: "betty", hub: true, hubHolder: "annie"})
+ mv := &acrossHAMover{acrossHAAct: s.acts[s.tenant], moving: acrossTenant, to: "cindy"}
+ s.setAct(t, s.tenant, mv, mv.acrossHAAct)
+ s.acts[s.third].mu.Lock()
+ s.acts[s.third].holders[acrossTenant] = "cindy"
+ s.acts[s.third].mu.Unlock()
+
+ s.get(t, s.hub, acrossHub, "/acme/x").want(t, http.StatusOK, "tenant-c|"+acrossHub+"|/acme/x")
+ wantNames(t, "routes B's mover was asked about", mv.moveHosts(), acrossTenant)
+ s.tenantApp.wantSeen(t)
+ s.tenantC.wantSeen(t, acrossSeen{acrossHub, "/acme/x"})
+ s.hubApp.wantSeen(t)
+ if b, c := s.dialsFrom(s.hub, acrossIPTenant), s.dialsFrom(s.hub, acrossIPThird); b != 1 || c != 1 {
+ t.Fatalf("A dialled B %d and C %d times, want 1 and 1", b, c)
+ }
+ if got := heldHolder(s.hub, acrossTenant); got != "cindy" {
+ t.Fatalf("A remembers %q as %s's holder, want cindy", got, acrossTenant)
+ }
+ if got := heldHolder(s.hub, acrossHub); got != "" {
+ t.Fatalf("A remembers %q as %s's holder, want none", got, acrossHub)
+ }
+}
+
+// H is born-HA on A, its lease is elsewhere and its name is in A's blocked
+// set. A prefix selecting a remote T still reaches T: no front forward and
+// no HA refusal. The same name with no prefix is refused, as today.
+func TestPathAcrossHAFrontRemoteTargetSkipsFrontHA(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{hub: true, hubHolder: "cindy"})
+ block(s.hub, acrossHub)
+ s.get(t, s.hub, acrossHub, "/acme/x").want(t, http.StatusOK, "tenant-1|"+acrossHub+"|/acme/x")
+ s.tenantApp.wantSeen(t, acrossSeen{acrossHub, "/acme/x"})
+ asked, _, _ := s.acts[s.hub].record()
+ wantNames(t, "routes A's forwarder was asked about", asked)
+ if n := s.forwardsAny(); n != 0 {
+ t.Fatalf("%d front forwards, want 0", n)
+ }
+ // Control: the front's own dispatch is armed.
+ res := s.get(t, s.hub, acrossHub, "/other")
+ if res.code != http.StatusServiceUnavailable || res.header.Get(holderHeader) != "none" {
+ t.Fatalf("blocked front, no prefix: %d %v %q, want the HA refusal", res.code, res.header, res.body)
+ }
+ s.hubApp.wantSeen(t)
+}
+
+// The same with T on A itself (case 1): the prefix's target is served here,
+// whatever H's HA state, and with no prefix H's own dispatch runs.
+func TestPathAcrossHAFrontLocalTargetSkipsFrontHA(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ blocked bool
+ }{{"blocked", true}, {"leased elsewhere", false}} {
+ t.Run(tc.name, func(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{hub: true, hubHolder: "cindy"})
+ const near = "near.example"
+ nearApp := newAcrossApp(t, "near-1")
+ s.route(t, s.hub, acrossInstance(near, acrossAcct, "near", nearApp.addr()))
+ s.path(t, s.hub, acrossHub, "near", near, acrossAcct, "hub")
+ if tc.blocked {
+ block(s.hub, acrossHub)
+ }
+ s.get(t, s.hub, acrossHub, "/near/x").want(t, http.StatusOK, "near-1|"+acrossHub+"|/near/x")
+ nearApp.wantSeen(t, acrossSeen{acrossHub, "/near/x"})
+ asked, _, _ := s.acts[s.hub].record()
+ wantNames(t, "routes A's forwarder was asked about", asked)
+ wantNames(t, "names A's HA ingress asked", s.lookups[s.hub].names(), near)
+ if n := s.forwardsAny() + s.dialsFromAny(s.hub); n != 0 {
+ t.Fatalf("A sent %d requests elsewhere, want 0", n)
+ }
+ // Control: with no prefix, H's own dispatch refuses or forwards.
+ s.get(t, s.hub, acrossHub, "/other")
+ s.hubApp.wantSeen(t)
+ nearApp.wantSeen(t, acrossSeen{acrossHub, "/near/x"})
+ if !tc.blocked {
+ asked, _, _ = s.acts[s.hub].record()
+ wantNames(t, "routes A's forwarder was asked about", asked, acrossHub)
+ }
+ })
+ }
+}
+
+// With no prefix, H's own HA dispatch runs exactly as today: A, not H's
+// holder, front-forwards to C, which serves H from its own pool copy; and
+// once H is blocked on A, A refuses it.
+func TestPathAcrossHAFrontNoPrefixAsToday(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{hub: true, hubHolder: "cindy"})
+ hubC := newAcrossApp(t, "hub-c")
+ s.haRoute(t, s.third, withAddr(acrossHARoute(acrossHub, "hub", "hub"), hubC.addr()))
+ s.get(t, s.hub, acrossHub, "/other").want(t, http.StatusOK, "hub-c|"+acrossHub+"|/other")
+ hubC.wantSeen(t, acrossSeen{acrossHub, "/other"})
+ s.hubApp.wantSeen(t)
+ asked, _, _ := s.acts[s.hub].record()
+ wantNames(t, "routes A's forwarder was asked about", asked, acrossHub)
+ wantNames(t, "names A's HA ingress asked", s.lookups[s.hub].names(), acrossHub)
+ if n := s.forwardsTo(acrossIPThird); n != 1 {
+ t.Fatalf("front forwards to C: %d, want 1", n)
+ }
+ block(s.hub, acrossHub)
+ res := s.get(t, s.hub, acrossHub, "/again")
+ if res.code != http.StatusServiceUnavailable || res.header.Get(holderHeader) != "none" {
+ t.Fatalf("blocked front: %d %v %q, want the HA refusal", res.code, res.header, res.body)
+ }
+ hubC.wantSeen(t, acrossSeen{acrossHub, "/other"})
+}
+
+// R3 P1-1: an HA front H (blocked, its lease elsewhere) whose prefix selects
+// a same-box platform alias P of a born-HA instance I. The HA identity is
+// I's (the instance's route, which the alias's entry carries), so H's state
+// does not refuse it, and I serves.
+func TestPathAcrossHAFrontPlatformAliasTarget(t *testing.T) {
+ s := newPathAcrossHAScene(t, acrossHAOpts{hub: true, hubHolder: "cindy"})
+ const inst, alias = "i.bae.oncarlos.example", "shop.bae.oncarlos.example"
+ backend := rgSocketBackend(t, shortSockDir(t), "inst-1")
+ irt := acrossHARoute(inst, "shop", "shop")
+ irt.Socket = backend.sock
+ s.lookups[s.hub].set(inst, irt)
+ s.acts[s.hub].mu.Lock()
+ s.acts[s.hub].holders[inst] = "annie"
+ s.acts[s.hub].mu.Unlock()
+ s.haRoute(t, s.hub, irt)
+ prt := acrossInstance(alias, acrossAcct, "shop", "")
+ prt.Socket, prt.Platform = backend.sock, true
+ s.route(t, s.hub, prt)
+ s.path(t, s.hub, acrossHub, "shop", alias, acrossAcct, "hub")
+ if up := s.hub.edge.current()[alias]; !up.platformAlias || up.route.Host != inst {
+ t.Fatalf("fixture: %s is not a platform alias of %s: %+v", alias, inst, up)
+ }
+ block(s.hub, acrossHub)
+ s.lookups[s.hub].names()
+
+ s.get(t, s.hub, acrossHub, "/shop/x").want(t, http.StatusOK, "inst-1")
+ if n := backend.n.Load(); n != 1 {
+ t.Fatalf("instance backend saw %d requests, want 1", n)
+ }
+ wantNames(t, "names A's HA ingress asked", s.lookups[s.hub].names(), inst)
+ asked, served, _ := s.acts[s.hub].record()
+ wantNames(t, "routes A's forwarder was asked about", asked, inst)
+ wantNames(t, "routes A's activator served", served, inst)
+ s.hubApp.wantSeen(t)
+ if n := s.forwardsAny() + s.dialsFromAny(s.hub); n != 0 {
+ t.Fatalf("A sent %d requests elsewhere, want 0", n)
+ }
+
+ // The selected request is judged exactly as a request whose Host is I:
+ // I's own route claim (never the alias its entry was reached through)
+ // against what peers publish for I. B publishes I in a newer lifetime
+ // (both refuse); then in I's own lifetime from a box that is not
+ // admitted (both serve: this box's own claim on I is what serves it).
+ plain := func(c context.Context) context.Context { return c }
+ for _, row := range []struct {
+ name string
+ inc int
+ admitted bool
+ want int
+ }{
+ {"newer lifetime on B", 2, true, http.StatusServiceUnavailable},
+ {"held on B", 1, false, http.StatusOK},
+ } {
+ t.Run(row.name, func(t *testing.T) {
+ s.files["betty"][inst] = fmt.Sprintf(`{"role":"home","account":%q,"app":"shop","stamp":7,"incarnation":%d}`, acrossAcct, row.inc)
+ defer delete(s.files["betty"], inst)
+ body := strings.Replace(s.fileBody(s.tenant), `"admitted":true`, fmt.Sprintf(`"admitted":%t`, row.admitted), 1)
+ putJSON(t, s.store, "control/edges/betty.json", body)
+ s.refresh(t)
+ before := backend.n.Load()
+ direct := s.serve(t, s.hub, inst, "/x", plain)
+ mid := backend.n.Load()
+ selected := s.serve(t, s.hub, acrossHub, "/shop/x", plain)
+ after := backend.n.Load()
+ if direct.code != selected.code || mid-before != after-mid {
+ t.Fatalf("Host %s answered %d (backend +%d); %s/shop/x answered %d %q (backend +%d)",
+ inst, direct.code, mid-before, acrossHub, selected.code, selected.body, after-mid)
+ }
+ if selected.code != row.want {
+ t.Fatalf("%s/shop/x answered %d %q, want %d", acrossHub, selected.code, selected.body, row.want)
+ }
+ })
+ }
+}
+
+// A selected platform-alias target is judged as its instance I, as a request
+// naming I is: an unavailable (zero) projection for I refuses it, and so
+// does no projection on a box outside I's pool. The platform alias's own
+// exemptions (TestHAIngressZeroProjectionPreservesUnmanagedUpstream,
+// TestPoolGateCannotFallBackLocal) belong to a request naming the alias.
+func TestPathAcrossHASelectedAliasZeroProjection(t *testing.T) {
+ for _, known := range []bool{true, false} {
+ t.Run(fmt.Sprintf("projection known=%v", known), func(t *testing.T) {
+ selectedAliasJudgedAsInstance(t, known)
+ })
+ }
+}
+
+func selectedAliasJudgedAsInstance(t *testing.T, known bool) {
+ const front, alias = "front.forward.test", "shop.forward.test"
+ x := newFwScene(t, fwOpts{aAct: &ingressForwarder{&fwAct{}}})
+ x.a.edge.SetHAIngressLookup(func(string) (registry.Route, bool) { return registry.Route{}, known })
+ up := upstream{route: registry.Route{Host: fwHost, Kind: "instance", Hibernate: true, Provisioned: true}, hibernating: true, alias: true, platformAlias: true}
+
+ w := httptest.NewRecorder()
+ if x.a.edge.routeHAIngress(w, httptest.NewRequest(http.MethodGet, "https://"+alias+"/", nil), alias, alias, up, true) {
+ t.Fatalf("a request naming the platform alias was answered %d", w.Code)
+ }
+ w = httptest.NewRecorder()
+ if !x.a.edge.routeHAIngress(w, httptest.NewRequest(http.MethodGet, "https://"+front+"/shop/", nil), front, fwHost, up, true) || w.Code != http.StatusServiceUnavailable {
+ t.Fatalf("the alias selected for %s, judged as %s: answered %d, want the HA refusal", front, fwHost, w.Code)
+ }
+ w = httptest.NewRecorder()
+ up.alias, up.platformAlias = false, false
+ if !x.a.edge.routeHAIngress(w, httptest.NewRequest(http.MethodGet, "https://"+fwHost+"/", nil), fwHost, fwHost, up, true) || w.Code != http.StatusServiceUnavailable {
+ t.Fatalf("control: a request naming %s answered %d, want the HA refusal", fwHost, w.Code)
+ }
+}
diff --git a/internal/edge/pathselect.go b/internal/edge/pathselect.go
index 477211638..675b17675 100644
--- a/internal/edge/pathselect.go
+++ b/internal/edge/pathselect.go
@@ -81,6 +81,19 @@ func (s requestSelection) sameBox() (upstream, bool) {
return s.Up, s.OK
}
+// haRouteName is the name whose route every region HA decision for this
+// request is judged under (spec §5.7; routeHAIngress): the selected target's
+// route host when a prefix selected a local target (case 1 or case 3; for a
+// platform-alias target, its instance's, which is what its entry holds),
+// else host, the request's own name. A Remote selection never reaches HA
+// dispatch here (servePathRemote answers it first).
+func (s requestSelection) haRouteName(host string) string {
+ if s.LocalTarget {
+ return s.Up.route.Host
+ }
+ return host
+}
+
// selectUpstream is THE selection: pinned branch first and unchanged; then
// pathMatch on the local map; then selectAcross for cases 2 and 3. The
// arrived-hop rule is settled here: on a verified hop a remote answer never