region HA P1a Task 9: host wiring for the HA wake
Config.Record reads the record from the deployment store on an index miss; Config.Self is lease.CurrentSelf; every reconcile pass and the backend share boxLifetimes; the backend gets the registry, the hibernate bucket as its files store and the startup sweep's proof; the obligation store opens at <data>/.ha-owed before the Service starts, and a failure is logged (every HA acquire then refuses with lease.ErrNoObligationStore).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+136
−0
internal/host/agent.go+57 −0internal/host/hawake_wiring_test.go+79 −0
CI failed — run details
diff --git a/internal/host/agent.go b/internal/host/agent.go| index b21e2b246..db2457896 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -415,6 +415,11 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| started := make(chan struct{}) |
| var startupDone <-chan struct{} |
| if cfg != nil { |
| + // Region HA P1a: the record facts the HA classification reads on a |
| + // lifetime index miss, and the box identity the keep decision |
| + // compares, set before the backend copies cfg. |
| + cfg.Record = haRecordFacts(openDeployStore) |
| + cfg.Self = lease.CurrentSelf |
| // Exec children and carlos-app@ unit tenants share one state machine; |
| // the split backend dispatches per route on Backing. Note the arming |
| // gate is still the hibernate bucket env even though unit tenants |
| @@ -454,6 +459,23 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| if claims.Writes.Enabled { |
| eb.Timers = schedule.ClaimTimers{TimerStore: reg, Writes: claims.Writes, Context: agentCtx} |
| } |
| + // Region HA P1a: the lifetime index the reconcile passes fill, the |
| + // registry and files store an HA restore reads, the startup sweep's |
| + // proof, and the obligation store, opened before the Service |
| + // starts. A store that does not open is logged here and refuses |
| + // every HA acquire on the box (lease.ErrNoObligationStore), which |
| + // each refused wake reports: an obligation is never dropped. |
| + eb.Lifetimes = boxLifetimes |
| + eb.Registry = reg |
| + eb.StartupSwept = seedSweptAtStart.Load |
| + if fs, err := newProductionFilesStore(agentCtx, cfg.Bucket, cfg.Region); err != nil { |
| + log.Printf("agent: region HA: files store: %v (every HA restore that needs it fails)", err) |
| + } else { |
| + eb.FilesStore = fs |
| + } |
| + if err := lease.SetObligationDir(filepath.Join(dd, ".ha-owed")); err != nil { |
| + log.Printf("agent: region HA: obligation store: %v (every HA wake on this box refuses until it opens)", err) |
| + } |
| if logSink != nil { // a typed-nil in the interface would NOT be nil |
| eb.Logs = logSink |
| } |
| @@ -3508,9 +3530,44 @@ func boxProvisionConfig(judge *provision.LifetimeJudge, configure []func(*provis |
| // a new size applies live on the next sweep (#329); nil-safe before |
| // the activator is armed. |
| cfg.PolicyChanged = noteSizeChanged |
| + // The process's one lifetime index, which the activator reads before |
| + // every wake of a provisioned instance (region HA P1a). |
| + cfg.Lifetimes = boxLifetimes |
| return cfg |
| } |
| +// boxLifetimes is the box's lifetime index (provision.LifetimeIndex): every |
| +// reconcile pass fills it, the activator's HA classification reads it. |
| +var boxLifetimes = &provision.LifetimeIndex{} |
| + |
| +// haRecordFacts is activator.Config.Record on this box: the host's record |
| +// read from the deployment store (provision.GetRecord, under the route's |
| +// effective account) and made a lifetime entry. The store is opened on |
| +// first use and kept; a store that does not open is the read's error, and |
| +// the wake fails closed. |
| +func haRecordFacts(open func(context.Context) (release.Store, error)) func(context.Context, registry.Route) (provision.LifetimeEntry, error) { |
| + var mu sync.Mutex |
| + var store release.Store |
| + return func(ctx context.Context, rt registry.Route) (provision.LifetimeEntry, error) { |
| + mu.Lock() |
| + s := store |
| + if s == nil { |
| + var err error |
| + if s, err = open(ctx); err != nil { |
| + mu.Unlock() |
| + return provision.LifetimeEntry{}, fmt.Errorf("deployment store: %w", err) |
| + } |
| + store = s |
| + } |
| + mu.Unlock() |
| + rec, err := provision.GetRecord(ctx, s, release.RouteAccount(rt), rt.App, rt.Host) |
| + if err != nil { |
| + return provision.LifetimeEntry{}, err |
| + } |
| + return provision.LifetimeEntryOf(*rec) |
| + } |
| +} |
| + |
| // resolveHost is the production DNS seam: the default resolver with a hard |
| // 3s timeout, so one dead nameserver can't stall a whole reconcile pass. |
| func resolveHost(host string) ([]string, error) { |
diff --git a/internal/host/hawake_wiring_test.go b/internal/host/hawake_wiring_test.go| new file mode 100644 |
| index 000000000..21d5ea957 |
| --- /dev/null |
| +++ b/internal/host/hawake_wiring_test.go |
| @@ -0,0 +1,79 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "os" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// Every reconcile pass fills the one lifetime index the activator reads. |
| +func TestBoxProvisionConfigCarriesTheLifetimeIndex(t *testing.T) { |
| + if a, b := boxProvisionConfig(nil, nil), boxProvisionConfig(nil, nil); a.Lifetimes == nil || a.Lifetimes != boxLifetimes || b.Lifetimes != boxLifetimes { |
| + t.Fatalf("pass configs carry %p and %p, want the box's %p", a.Lifetimes, b.Lifetimes, boxLifetimes) |
| + } |
| +} |
| + |
| +// Config.Record on this box: the record from the deployment store, under |
| +// the route's effective account, as a lifetime entry; the store opened |
| +// once; a missing record or a store that will not open is an error. |
| +func TestHARecordFactsReadsTheRecord(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + acct := domains.Sqid(2) |
| + rec := provision.Record{Schema: 1, Host: "shop.example.com", App: "shop", Account: acct, Channel: "stable", |
| + CreatedAt: time.Date(2026, 10, 26, 9, 0, 0, 0, time.UTC), HA: true} |
| + if err := provision.CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + opens := 0 |
| + read := haRecordFacts(func(context.Context) (release.Store, error) { opens++; return s, nil }) |
| + got, err := read(ctx, registry.Route{Host: rec.Host, App: rec.App, Account: acct}) |
| + want, _ := provision.LifetimeEntryOf(rec) |
| + if err != nil || got != want || !got.HA { |
| + t.Fatalf("got %+v, %v; want %+v", got, err, want) |
| + } |
| + if _, err := read(ctx, registry.Route{Host: "gone.example.com", App: rec.App, Account: acct}); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("a missing record: %v", err) |
| + } |
| + if opens != 1 { |
| + t.Fatalf("the store was opened %d times", opens) |
| + } |
| + down := haRecordFacts(func(context.Context) (release.Store, error) { return nil, errors.New("no bucket") }) |
| + if _, err := down(ctx, registry.Route{Host: rec.Host, App: rec.App}); err == nil || !strings.Contains(err.Error(), "no bucket") { |
| + t.Fatalf("a store that will not open: %v", err) |
| + } |
| +} |
| + |
| +// RunAgent wires the HA wake before the Service starts: the record facts, |
| +// the identity, the index, the registry, the startup sweep's proof and the |
| +// obligation store (syntactic, like the policy cache's wiring test). |
| +func TestAgentWiresTheHAWake(t *testing.T) { |
| + src, err := os.ReadFile("agent.go") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + body := string(src) |
| + start := strings.Index(body, "Start: svc.Start,") |
| + for _, want := range []string{ |
| + "cfg.Record = haRecordFacts(openDeployStore)", |
| + "cfg.Self = lease.CurrentSelf", |
| + "eb.Lifetimes = boxLifetimes", |
| + "eb.Registry = reg", |
| + "eb.StartupSwept = seedSweptAtStart.Load", |
| + "eb.FilesStore = fs", |
| + `lease.SetObligationDir(filepath.Join(dd, ".ha-owed"))`, |
| + } { |
| + i := strings.Index(body, want) |
| + if i < 0 || start < 0 || i > start { |
| + t.Errorf("%q is not wired before the Service starts", want) |
| + } |
| + } |
| +} |