carlos / platform Public

Task 7 round 1 fixes: a breach latches invalidation until written; door-level witnesses

Astra Task 7 r1 (findings committed earlier):
- P1: invalidateHosts pages whether or not its write lands; a failed
  write latches invalidated in this process (data-kind creates stay
  refused, a stale active read from the store is overridden) and is
  written again every 30 s until it lands or the store leaves active.
- P2: S4 witnessed through the real doors, lifetimes off and on (an
  explicit region, a sidecar, a closed fleet), with nothing written; a
  new app's automatic region stays home before activation and picks only
  an examined region after.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 9c56fdbdcbef48931e3dffb4f31734ed068ba940 parent e656f5f
2 files changed, +173 −6
  • internal/console/hostsused.go +70 −6
  • internal/console/hostsused_active_test.go +103 −0

CI cancelled — superseded by a newer push — run details

diff --git a/internal/console/hostsused.go b/internal/console/hostsused.go
index 171cdd727..1d0a1001f 100644
--- a/internal/console/hostsused.go
+++ b/internal/console/hostsused.go
@@ -73,6 +73,10 @@ type hostsHistory struct {
// admitted is the data-kind creates, moves and placements admitted and
// still inside their handlers: the window's in-process drain count.
admitted int
+ // latched is a breach this process found and could not yet write: the
+ // reason, held as invalidated whatever the store says, until a write
+ // of the history's mode lands (invalidateHosts).
+ latched string
// background is the seed copies and import uploads running detached
// (goSeedWorker): counted until the worker returns, after its last
// write, whatever its record says.
@@ -103,6 +107,11 @@ func (s *Server) loadHostsLocked(ctx context.Context) error {
if err != nil {
return err
}
+ if s.hosts.latched != "" && st.Mode == provision.HostsModeActive {
+ // A breach this process found and has not yet written: the store's
+ // active is stale (Astra Task 7 r1).
+ st = provision.HostsState{Mode: provision.HostsModeInvalidated, Reason: s.hosts.latched, Closed: st.Closed}
+ }
s.hosts.state, s.hosts.etag = st, etag
return nil
}
@@ -187,10 +196,38 @@ func (s *Server) hostsTargetRefusal(ctx context.Context, region string) *doorRef
return nil
}
+// hostsInvalidateRetry is how long a breach's unwritten invalidation waits
+// before it is written again.
+var hostsInvalidateRetry = 30 * time.Second
+
// invalidateHosts moves an active history to invalidated (spec §4), in the
-// store and the process, and pages with subject. Every data-kind create is
-// refused (S5) until a fresh inventory completes.
+// store and the process, and pages with reason whether or not the write
+// lands. Every data-kind create is refused (S5) until a fresh inventory
+// completes. A write that fails latches the breach in this process (creates
+// stay refused) and is retried until it lands (Astra Task 7 r1).
func (s *Server) invalidateHosts(ctx context.Context, reason string) error {
+ err := s.writeInvalidated(ctx, reason)
+ if err != nil && err.Error() == msgHostsNotActive {
+ return err
+ }
+ log.Printf("console: %s", reason)
+ if perr := alerting.Page(ctx, alerting.LevelIncident, reason, reason); perr != nil {
+ log.Printf("console: hosts used: page the invalidation: %v", perr)
+ }
+ if err != nil {
+ s.hosts.mu.Lock()
+ s.hosts.latched = reason
+ s.hosts.state = provision.HostsState{Mode: provision.HostsModeInvalidated, Reason: reason}
+ s.hosts.etag = ""
+ s.hosts.mu.Unlock()
+ log.Printf("console: hosts used: write the invalidation: %v (new instances stay refused here; writing it again every %s)", err, hostsInvalidateRetry)
+ go s.persistInvalidation(context.WithoutCancel(ctx), reason)
+ }
+ return nil
+}
+
+// writeInvalidated writes invalidated over the store's active state.
+func (s *Server) writeInvalidated(ctx context.Context, reason string) error {
_, ref, err := s.hostsTransition(ctx, func(cur provision.HostsState) (provision.HostsState, *doorRefusal) {
if cur.Mode != provision.HostsModeActive {
return cur, &doorRefusal{http.StatusConflict, msgHostsNotActive}
@@ -203,13 +240,39 @@ func (s *Server) invalidateHosts(ctx context.Context, reason string) error {
if ref != nil {
return errors.New(ref.Msg)
}
- log.Printf("console: %s", reason)
- if err := alerting.Page(ctx, alerting.LevelIncident, reason, reason); err != nil {
- log.Printf("console: hosts used: page the invalidation: %v", err)
- }
return nil
}
+// persistInvalidation writes a latched breach until the store holds it, or
+// holds anything but active (an operator's re-inventory has begun).
+func (s *Server) persistInvalidation(ctx context.Context, reason string) {
+ for {
+ time.Sleep(hostsInvalidateRetry)
+ s.hosts.mu.Lock()
+ if s.hosts.latched == "" {
+ s.hosts.mu.Unlock()
+ return
+ }
+ st, etag, err := provision.ReadHostsState(ctx, s.cfg.Releases)
+ if err == nil && st.Mode != provision.HostsModeActive {
+ s.hosts.latched = ""
+ s.hosts.state, s.hosts.etag = provision.HostsState{}, "" // read again
+ s.hosts.mu.Unlock()
+ return
+ }
+ if err == nil {
+ next := provision.HostsState{Mode: provision.HostsModeInvalidated, Reason: reason, Closed: st.Closed, At: time.Now().UTC()}
+ if tag, werr := provision.WriteHostsState(ctx, s.cfg.Releases, next, etag); werr == nil {
+ s.hosts.latched = ""
+ s.hosts.state, s.hosts.etag = next, tag
+ s.hosts.mu.Unlock()
+ return
+ }
+ }
+ s.hosts.mu.Unlock()
+ }
+}
+
// recoveryMark is lifetime recovery's mark step (spec §2), before it
// publishes an allocation: once the history is active (or invalidated) the
// hostname must already be marked, and an absent mark invalidates the
@@ -271,6 +334,7 @@ func (s *Server) hostsTransition(ctx context.Context, next func(cur provision.Ho
return provision.HostsState{}, nil, err
}
s.hosts.state, s.hosts.etag = st, etag
+ s.hosts.latched = "" // a mode write landed: the store is the truth again
return st, nil, nil
}
diff --git a/internal/console/hostsused_active_test.go b/internal/console/hostsused_active_test.go
index 64db1ebf5..fadd057d5 100644
--- a/internal/console/hostsused_active_test.go
+++ b/internal/console/hostsused_active_test.go
@@ -9,6 +9,7 @@ package console
import (
"context"
"net/http"
+ "strings"
"sync"
"sync/atomic"
"testing"
@@ -274,3 +275,105 @@ func historyActive(srv *Server) {
srv.hosts.state = provision.HostsState{Mode: provision.HostsModeActive,
Examined: []string{"region:ie", "region:uk", "region:au", "region:us", "region:jp", "region:de", "region:ca"}}
}
+
+// TestFailedInvalidationStillRefuses (Astra Task 7 r1, P1): a breach found
+// while the invalidated mode cannot be written still refuses data-kind
+// creates in this process, and the mode is written once the store answers.
+func TestFailedInvalidationStillRefuses(t *testing.T) {
+ old := hostsInvalidateRetry
+ hostsInvalidateRetry = 10 * time.Millisecond
+ t.Cleanup(func() { hostsInvalidateRetry = old })
+ l := newLtOn(t)
+ l.pendingCreate(t, ltOne, "record")
+ dropMark(t, l.fx.rel, ltOne)
+ forceHostsState(t, l.fx.srv, provision.HostsState{Mode: provision.HostsModeActive})
+ l.bucket.setHook(failNth("put", provision.HostsModeKey, 1, true))
+ recoverOn(t, l.fx.srv)
+ if code, out := l.create(t, "after.shop.example.com"); code != http.StatusServiceUnavailable || ltErr(out) != msgHostsInvalidated {
+ t.Fatalf("create after a breach whose write failed = %d %s, want 503 S5", code, out)
+ }
+ if st, _, _ := provision.ReadHostsState(context.Background(), l.fx.rel); st.Mode != provision.HostsModeActive {
+ t.Fatalf("fixture: the store already says %s", st.Mode)
+ }
+ l.bucket.setHook(nil)
+ deadline := time.Now().Add(5 * time.Second)
+ for {
+ st, _, err := provision.ReadHostsState(context.Background(), l.fx.rel)
+ if err == nil && st.Mode == provision.HostsModeInvalidated {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatalf("the invalidation was never written: %+v %v", st, err)
+ }
+ time.Sleep(20 * time.Millisecond)
+ }
+ if code, out := l.create(t, "later.shop.example.com"); code != http.StatusServiceUnavailable || ltErr(out) != msgHostsInvalidated {
+ t.Fatalf("create once written = %d %s, want 503 S5", code, out)
+ }
+}
+
+// TestUnexaminedTargetRefusedAtEveryDoor (Astra Task 7 r1, P2): S4 through
+// the real doors, lifetimes off and on, with nothing published.
+func TestUnexaminedTargetRefusedAtEveryDoor(t *testing.T) {
+ for _, d := range markDoors {
+ if d.name != "region" && d.name != "sidecar" {
+ // The others create at home, which is always examined. A stored
+ // app region that is unexamined fails the app-region
+ // eligibility first (an ineligible candidate), as the brief says.
+ continue
+ }
+ t.Run(d.name, func(t *testing.T) {
+ lifetimesFor(t, func(t *testing.T, on func(*testing.T, markCase)) {
+ c := d.build(t)
+ on(t, c)
+ forceHostsState(t, c.fx.srv, provision.HostsState{Mode: provision.HostsModeActive})
+ code, out := c.create(t)
+ if code != http.StatusConflict || !strings.HasPrefix(ltErr(out), "New instances can't be created on ") {
+ t.Fatalf("create on an unexamined region = %d %s, want 409 S4", code, out)
+ }
+ if recordPresent(t, c, c.recordKey) || markOf(t, c) != nil {
+ t.Fatal("the refused create wrote something")
+ }
+ })
+ })
+ }
+ t.Run("closed fleet", func(t *testing.T) {
+ lifetimesFor(t, func(t *testing.T, on func(*testing.T, markCase)) {
+ fx, token := placeFixture(t, "code-s4-fleet")
+ on(t, markCase{fx: fx})
+ m := titoManifest()
+ m.Pool, m.Regions = "", nil
+ enableInstances(t, fx, "titogo", m)
+ if status, out := postJSON(t, fx.ts.URL+"/api/cli/apps/"+fx.sqid+"/titogo/place", map[string]string{"target": "fleet/pi"}, token); status != http.StatusOK {
+ t.Fatalf("place = %d %v", status, out)
+ }
+ forceHostsState(t, fx.srv, provision.HostsState{Mode: provision.HostsModeActive, Examined: []string{"fleet:pi"}, Closed: []string{"fleet:pi"}})
+ code, _, out := rawReq(t, http.MethodPost, instancesURL(fx, fx.sqid, "titogo"), token, `{"host":"fl.go.tito.io"}`)
+ if code != http.StatusConflict || ltErr(out) != msgHostsTargetUnexamined("fleet pi") {
+ t.Fatalf("create on a closed fleet = %d %s, want 409 S4", code, out)
+ }
+ })
+ })
+}
+
+// TestAutomaticRegionNeedsTheHistory (Astra Task 7 r1, P2): a new app's
+// automatic region stays home until the history is active, and then picks
+// only an examined region.
+func TestAutomaticRegionNeedsTheHistory(t *testing.T) {
+ fx, token := regionFixture(t)
+ for _, c := range []struct {
+ name string
+ st provision.HostsState
+ want string
+ }{
+ {"before", provision.HostsState{Mode: provision.HostsModeIncomplete}, "ie"},
+ {"after", provision.HostsState{Mode: provision.HostsModeActive, Examined: []string{"region:ie", "region:uk"}}, "uk"},
+ {"unexamined", provision.HostsState{Mode: provision.HostsModeActive, Examined: []string{"region:ie"}}, "ie"},
+ } {
+ forceHostsState(t, fx.srv, c.st)
+ status, res := postJSON(t, newAppURL(fx), map[string]string{"name": "app-" + c.name, "timezone": "Europe/London"}, token)
+ if status != http.StatusOK || res["region"] != c.want {
+ t.Fatalf("%s: create = %d %v, want region %s", c.name, status, res, c.want)
+ }
+ }
+}