| 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) |
| + } |
| +} |