region HA P1a Task 9: born-HA record flag, the lifetime index, and a sticky HA mark on the row
Record.HA (json ha, omitempty). provision.LifetimeIndex (Replace after a complete scan, Merge after a partial one, MarkHA and record HA sticky), filled by Reconcile through ReconcileConfig.Lifetimes; LifetimeEntryOf is the one place a record becomes an entry. registry.Route.HA (column ha): minted by RouteFor, converged one way by SetHA, kept by Upsert whatever the incoming row says.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+395
−6
internal/provision/lifetimeindex.go+114 −0internal/provision/lifetimeindex_test.go+147 −0internal/provision/provision.go+7 −0internal/provision/reconcile.go+28 −0internal/registry/ha_test.go+45 −0internal/registry/registry.go+51 −6internal/registry/registry_test.go+1 −0internal/registry/remote_dispatch.go+2 −0
diff --git a/internal/provision/lifetimeindex.go b/internal/provision/lifetimeindex.go| new file mode 100644 |
| index 000000000..e47593a07 |
| --- /dev/null |
| +++ b/internal/provision/lifetimeindex.go |
| @@ -0,0 +1,114 @@ |
| +package provision |
| + |
| +import ( |
| + "sync" |
| + "time" |
| +) |
| + |
| +// LifetimeEntry is what a box knows of a host's current record for region |
| +// HA (P1a): the record's lifetime, its creation time, and whether the host |
| +// is HA. ID is Incarnation(account, app, host, CreatedAt), the lifetime a |
| +// release marker names and the keep decision compares. |
| +type LifetimeEntry struct { |
| + ID string |
| + Created time.Time |
| + HA bool |
| +} |
| + |
| +// LifetimeEntryOf is rec's entry: the one place a record becomes one, for |
| +// the reconcile pass's index and for a box's record read on an index miss. |
| +func LifetimeEntryOf(rec Record) (LifetimeEntry, error) { |
| + id, err := Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| + if err != nil { |
| + return LifetimeEntry{}, err |
| + } |
| + return LifetimeEntry{ID: id, Created: rec.CreatedAt, HA: rec.HA}, nil |
| +} |
| + |
| +// LifetimeIndex is the box's in-memory index of its in-scope records' |
| +// lifetime entries, filled by the reconcile pass (ReconcileConfig. |
| +// Lifetimes) and read by the activator before every wake of a provisioned |
| +// instance. The zero value is ready to use; its methods are safe from any |
| +// goroutine. |
| +// |
| +// HA is sticky per host: once a record said HA, or MarkHA was called (the |
| +// lock was HA-format), Lookup answers HA for that host from then on, |
| +// whatever a later Replace or Merge carries. A record that cannot become |
| +// an entry (its identity is malformed) is left out: a miss, which the |
| +// activator answers with a record read of its own. |
| +type LifetimeIndex struct { |
| + mu sync.Mutex |
| + m map[string]LifetimeEntry |
| + ha map[string]bool |
| +} |
| + |
| +// Replace makes recs, by host, the whole index: called after a complete |
| +// scan, so a host the scan no longer holds is forgotten. Sticky HA marks |
| +// survive it. |
| +func (x *LifetimeIndex) Replace(recs map[string]Record) { |
| + m := entriesOf(recs) |
| + x.mu.Lock() |
| + defer x.mu.Unlock() |
| + x.m = m |
| + x.stickLocked(m) |
| +} |
| + |
| +// Merge adds or updates recs, by host, keeping every other entry: called |
| +// after a partial scan, which proves nothing about a host it did not see. |
| +func (x *LifetimeIndex) Merge(recs map[string]Record) { |
| + m := entriesOf(recs) |
| + x.mu.Lock() |
| + defer x.mu.Unlock() |
| + if x.m == nil { |
| + x.m = map[string]LifetimeEntry{} |
| + } |
| + for h, e := range m { |
| + x.m[h] = e |
| + } |
| + x.stickLocked(m) |
| +} |
| + |
| +// Lookup is host's entry. ok is false when the index holds no entry for |
| +// the host; a host only marked HA answers ok with an empty ID, so the |
| +// caller still reads the record for its lifetime. |
| +func (x *LifetimeIndex) Lookup(host string) (LifetimeEntry, bool) { |
| + x.mu.Lock() |
| + defer x.mu.Unlock() |
| + e, ok := x.m[host] |
| + if x.ha[host] { |
| + e.HA = true |
| + ok = true |
| + } |
| + return e, ok |
| +} |
| + |
| +// MarkHA marks host HA for good: a later Replace or Merge never clears it. |
| +func (x *LifetimeIndex) MarkHA(host string) { |
| + x.mu.Lock() |
| + defer x.mu.Unlock() |
| + if x.ha == nil { |
| + x.ha = map[string]bool{} |
| + } |
| + x.ha[host] = true |
| +} |
| + |
| +func (x *LifetimeIndex) stickLocked(m map[string]LifetimeEntry) { |
| + for h, e := range m { |
| + if e.HA { |
| + if x.ha == nil { |
| + x.ha = map[string]bool{} |
| + } |
| + x.ha[h] = true |
| + } |
| + } |
| +} |
| + |
| +func entriesOf(recs map[string]Record) map[string]LifetimeEntry { |
| + m := make(map[string]LifetimeEntry, len(recs)) |
| + for h, rec := range recs { |
| + if e, err := LifetimeEntryOf(rec); err == nil { |
| + m[h] = e |
| + } |
| + } |
| + return m |
| +} |
diff --git a/internal/provision/lifetimeindex_test.go b/internal/provision/lifetimeindex_test.go| new file mode 100644 |
| index 000000000..c7b9fe1f1 |
| --- /dev/null |
| +++ b/internal/provision/lifetimeindex_test.go |
| @@ -0,0 +1,147 @@ |
| +package provision |
| + |
| +import ( |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func lifetimeRecord(host string, ha bool) Record { |
| + return Record{Schema: 1, Host: host, App: "tito", Account: domains.Sqid(acctID), Channel: "stable", |
| + CreatedAt: time.Date(2026, 10, 26, 9, 0, 0, 0, time.UTC), HA: ha} |
| +} |
| + |
| +func TestLifetimeIndexReplaceMergeLookup(t *testing.T) { |
| + var x LifetimeIndex |
| + if _, ok := x.Lookup("a.example.com"); ok { |
| + t.Fatal("the zero index answered a lookup") |
| + } |
| + a, b := lifetimeRecord("a.example.com", false), lifetimeRecord("b.example.com", true) |
| + x.Replace(map[string]Record{a.Host: a, b.Host: b}) |
| + want, err := LifetimeEntryOf(a) |
| + if err != nil || want.ID == "" { |
| + t.Fatalf("entry of a: %+v, %v", want, err) |
| + } |
| + if e, ok := x.Lookup(a.Host); !ok || e != want || e.HA { |
| + t.Fatalf("a: %+v %v, want %+v", e, ok, want) |
| + } |
| + if e, ok := x.Lookup(b.Host); !ok || !e.HA { |
| + t.Fatalf("b: %+v %v", e, ok) |
| + } |
| + // The ID is the record's incarnation identity. |
| + if id, _ := Incarnation(a.Account, a.App, a.Host, a.CreatedAt); want.ID != id { |
| + t.Fatalf("ID %q, want Incarnation %q", want.ID, id) |
| + } |
| + |
| + // A partial scan keeps what it did not see. |
| + c := lifetimeRecord("c.example.com", false) |
| + x.Merge(map[string]Record{c.Host: c}) |
| + for _, h := range []string{a.Host, b.Host, c.Host} { |
| + if _, ok := x.Lookup(h); !ok { |
| + t.Fatalf("Merge lost %s", h) |
| + } |
| + } |
| + // A complete scan forgets what it no longer holds. |
| + x.Replace(map[string]Record{c.Host: c}) |
| + if _, ok := x.Lookup(a.Host); ok { |
| + t.Fatal("Replace kept a host the complete scan no longer holds") |
| + } |
| +} |
| + |
| +// HA is never cleared (region HA P1a): neither a record's HA nor a MarkHA |
| +// is undone by a later Replace or Merge, whatever they carry. |
| +func TestLifetimeIndexMarkHASticky(t *testing.T) { |
| + var x LifetimeIndex |
| + legacy := lifetimeRecord("shop.example.com", false) |
| + x.Replace(map[string]Record{legacy.Host: legacy}) |
| + x.MarkHA(legacy.Host) |
| + x.Replace(map[string]Record{legacy.Host: legacy}) |
| + if e, ok := x.Lookup(legacy.Host); !ok || !e.HA || e.ID == "" { |
| + t.Fatalf("Replace cleared MarkHA: %+v %v", e, ok) |
| + } |
| + x.Merge(map[string]Record{legacy.Host: legacy}) |
| + if e, _ := x.Lookup(legacy.Host); !e.HA { |
| + t.Fatal("Merge cleared MarkHA") |
| + } |
| + // A host only marked, never indexed, answers HA with no lifetime. |
| + x.Replace(nil) |
| + if e, ok := x.Lookup(legacy.Host); !ok || !e.HA || e.ID != "" { |
| + t.Fatalf("marked host after its record left the scan: %+v %v", e, ok) |
| + } |
| + |
| + // A record's own HA sticks the same way. |
| + var y LifetimeIndex |
| + born := lifetimeRecord("born.example.com", true) |
| + y.Merge(map[string]Record{born.Host: born}) |
| + born.HA = false |
| + y.Replace(map[string]Record{born.Host: born}) |
| + if e, _ := y.Lookup(born.Host); !e.HA { |
| + t.Fatal("a later record without HA cleared the host's HA") |
| + } |
| +} |
| + |
| +// The reconcile pass fills the index (Replace on a complete scan, Merge on |
| +// a partial one) and carries the record's HA onto the row, at mint and |
| +// after. |
| +func TestReconcileFillsLifetimesAndHA(t *testing.T) { |
| + s, sqid := setup(t) |
| + born := acmeRecord(sqid) |
| + born.HA = true |
| + if err := CreateRecord(ctx, s, born); err != nil { |
| + t.Fatal(err) |
| + } |
| + other := acmeRecord(sqid) |
| + other.Host, other.Slug = "other.go.tito.io", "other" |
| + if err := CreateRecord(ctx, s, other); err != nil { |
| + t.Fatal(err) |
| + } |
| + // A row minted before the field existed for the HA host: converged. |
| + reg := regWith(t) |
| + idx := &LifetimeIndex{} |
| + idx.Merge(map[string]Record{"gone.go.tito.io": lifetimeRecord("gone.go.tito.io", false)}) |
| + cfg := ReconcileConfig{Region: "ie", Pools: []string{"tito"}, DataDir: "/data", Lifetimes: idx} |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rt := mustGet(t, reg, born.Host); !rt.HA { |
| + t.Fatalf("the born-HA record minted a row without HA: %+v", rt) |
| + } |
| + if rt := mustGet(t, reg, other.Host); rt.HA { |
| + t.Fatalf("a plain record minted an HA row: %+v", rt) |
| + } |
| + if e, ok := idx.Lookup(born.Host); !ok || !e.HA || e.ID == "" { |
| + t.Fatalf("index for the HA host: %+v %v", e, ok) |
| + } |
| + if e, ok := idx.Lookup(other.Host); !ok || e.HA { |
| + t.Fatalf("index for the plain host: %+v %v", e, ok) |
| + } |
| + if _, ok := idx.Lookup("gone.go.tito.io"); ok { |
| + t.Fatal("a complete scan kept a host it no longer holds") |
| + } |
| + |
| + // An existing row without the mark is converged to it. Upsert keeps a |
| + // stored mark, so the row starts minted without it. |
| + reg3 := regWith(t, func() registry.Route { r := RouteFor(born, "/data", `["seed"]`, false, nil); r.HA = false; return r }()) |
| + if rt := mustGet(t, reg3, born.Host); rt.HA { |
| + t.Fatal("fixture: the row should start unmarked") |
| + } |
| + if err := Reconcile(ctx, s, reg3, ReconcileConfig{Region: "ie", Pools: []string{"tito"}, DataDir: "/data"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rt := mustGet(t, reg3, born.Host); !rt.HA { |
| + t.Fatal("the owned converge did not mark an existing row HA") |
| + } |
| + |
| + // A partial scan (an account whose manifest will not read) merges. |
| + writeAccountManifest(t, s, acctID+1, "Broken") |
| + putReplace(t, s, "accounts/"+domains.Sqid(acctID+1)+"/account.json", []byte("{not json")) |
| + idx.Merge(map[string]Record{"kept.go.tito.io": lifetimeRecord("kept.go.tito.io", false)}) |
| + if err := Reconcile(ctx, s, reg, cfg); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, ok := idx.Lookup("kept.go.tito.io"); !ok { |
| + t.Fatal("a partial scan replaced the index") |
| + } |
| +} |
diff --git a/internal/provision/provision.go b/internal/provision/provision.go| index 836a3b7ee..d9a3b8e82 100644 |
| --- a/internal/provision/provision.go |
| +++ b/internal/provision/provision.go |
| @@ -56,6 +56,13 @@ type Record struct { |
| CreatedAt time.Time `json:"created_at"` |
| CreatedBy string `json:"created_by,omitempty"` |
| + // HA marks a host born high-availability (region HA P1a): the console's |
| + // create door stamps it when the record's region is one of |
| + // CONSOLE_HA_REGIONS. Every box honours it, whatever its own |
| + // CARLOS_REGION_HA, and nothing ever clears it. Additive and |
| + // omitempty: every record written before it reads as not HA. |
| + HA bool `json:"ha,omitempty"` |
| + |
| // Move is an in-flight residence move (issue #331, instance-move spec): |
| // present only between move-prepare and the cut's terminal flip, absent |
| // on every record at rest. Additive + omitempty — pre-move records parse |
diff --git a/internal/provision/reconcile.go b/internal/provision/reconcile.go| index 848fad361..c47f75c7a 100644 |
| --- a/internal/provision/reconcile.go |
| +++ b/internal/provision/reconcile.go |
| @@ -188,6 +188,13 @@ type ReconcileConfig struct { |
| // this pass read completely and applied (edge asset cache design §4.2, |
| // ReconcileReport). Nil: nobody asks, and the pass is exactly as before. |
| Report *ReconcileReport |
| + // Lifetimes is the box's lifetime index (region HA P1a), which the |
| + // activator reads before every wake of a provisioned instance. Each |
| + // pass hands it the in-scope records it scanned: Replace after a |
| + // complete scan, Merge after a partial one (a failed read or a partial |
| + // listing proves nothing about a host it did not see). A dry run |
| + // leaves it alone. Nil: no index is kept. |
| + Lifetimes *LifetimeIndex |
| // beforeHoldWrite is the tests' pause under the host's hold lock, just |
| // before holdExisting's conditional write (TestReconcileHoldWrite...). |
| @@ -526,6 +533,8 @@ func RouteFor(rec Record, dataDir, seed string, nested bool, policy *UpdatePolic |
| // instead (mintHeld, ReconcileConfig.PairOf). |
| Incarnation: rec.Incarnation, |
| ServingSeq: rec.MoveSeq, |
| + // Born HA (region HA P1a): sticky on the row (registry.Route.HA). |
| + HA: rec.HA, |
| } |
| if rec.DerivesPaths() { |
| rt.Socket = DefaultSocket(rec.Host) |
| @@ -901,6 +910,18 @@ func convergeOwned(reg *registry.Registry, existing registry.Route, rec Record, |
| log.Printf("provision: converged %s replica mark %v -> %v", rec.Host, existing.Replica, want) |
| } |
| } |
| + // The born-HA mark (region HA P1a) converges one way only: a record |
| + // that says HA marks the row, and nothing clears a marked one. |
| + if rec.HA && !existing.HA { |
| + if dryRun { |
| + log.Printf("provision: dry-run: would mark %s high availability", rec.Host) |
| + } else if err := reg.SetHA(rec.Host); err != nil { |
| + log.Printf("provision: converge high availability on %s: %v", rec.Host, err) |
| + errs = append(errs, fmt.Errorf("converge high availability on %s: %w", rec.Host, err)) |
| + } else { |
| + log.Printf("provision: marked %s high availability", rec.Host) |
| + } |
| + } |
| // Update settings (settings spec §4): the record over the app policy, |
| // both directions, including back to "". Left alone when the policy |
| // could not be read this pass. |
| @@ -1189,6 +1210,13 @@ func Reconcile(ctx context.Context, s release.Store, reg *registry.Registry, cfg |
| return err |
| } |
| recs, protect, readFailed := sc.recs, sc.protect, sc.readFailed |
| + if cfg.Lifetimes != nil && !cfg.DryRun { |
| + if readFailed || sc.partial { |
| + cfg.Lifetimes.Merge(recs) |
| + } else { |
| + cfg.Lifetimes.Replace(recs) |
| + } |
| + } |
| // What the report will say (ReconcileReport): every app starts out |
| // unvouched-for, and a host counts only once its record is positively |
| // applied below. A dry run applies nothing. |
diff --git a/internal/registry/ha_test.go b/internal/registry/ha_test.go| new file mode 100644 |
| index 000000000..9a8f53bd4 |
| --- /dev/null |
| +++ b/internal/registry/ha_test.go |
| @@ -0,0 +1,45 @@ |
| +package registry |
| + |
| +import ( |
| + "path/filepath" |
| + "testing" |
| +) |
| + |
| +// The born-HA mark is sticky (region HA P1a): a full-row Upsert that knows |
| +// nothing of it never clears it, SetHA sets it on an existing row only, and |
| +// a fresh row takes the incoming value. |
| +func TestHAMarkIsSticky(t *testing.T) { |
| + reg, err := Open(filepath.Join(t.TempDir(), "registry.db")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + base := Route{Host: "shop.example.com", Kind: "instance", Status: "ready", App: "shop", Channel: "stable"} |
| + if err := reg.Upsert(base); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, _ := reg.Get(base.Host); got == nil || got.HA { |
| + t.Fatalf("fresh row: %+v", got) |
| + } |
| + if err := reg.SetHA(base.Host); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, _ := reg.Get(base.Host); got == nil || !got.HA { |
| + t.Fatalf("SetHA did not mark the row: %+v", got) |
| + } |
| + if err := reg.Upsert(base); err != nil { // HA false on the incoming row |
| + t.Fatal(err) |
| + } |
| + if got, _ := reg.Get(base.Host); got == nil || !got.HA { |
| + t.Fatalf("a full-row Upsert cleared the HA mark: %+v", got) |
| + } |
| + born := Route{Host: "born.example.com", Kind: "instance", Status: "ready", App: "shop", Channel: "stable", HA: true} |
| + if err := reg.Upsert(born); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, _ := reg.Get(born.Host); got == nil || !got.HA { |
| + t.Fatalf("a row minted HA: %+v", got) |
| + } |
| + if err := reg.SetHA("nope.example.com"); err == nil { |
| + t.Fatal("SetHA on an unknown host: want an error") |
| + } |
| +} |
diff --git a/internal/registry/registry.go b/internal/registry/registry.go| index 1d506f162..3c359dc3b 100644 |
| --- a/internal/registry/registry.go |
| +++ b/internal/registry/registry.go |
| @@ -205,6 +205,16 @@ type Route struct { |
| Caps string |
| Hibernate bool |
| + // HA marks a born high-availability host (region HA P1a): the |
| + // instance record's ha flag, converged by the provision pass (RouteFor |
| + // at mint, SetHA after). It is sticky: once true, nothing on the box |
| + // clears it. Upsert keeps a stored true whatever the incoming row |
| + // says, and SetHA only sets it. The activator's classification |
| + // (activator.ExecBackend.IsHA) reads it beside the record facts; it is |
| + // never the whole answer, since a row minted before the field, or a |
| + // lock that is HA-format, says nothing here. Not in Fingerprint: it |
| + // changes how a wake runs, not what the edge serves. |
| + HA bool |
| // Runtime is how the row's process is launched: "" is a binary (the |
| // existing launch), RuntimeRootfsV1 is a rootfs image under a mount |
| // namespace. Materialized from the record at mint, read by the |
| @@ -699,6 +709,8 @@ CREATE TABLE IF NOT EXISTS routes ( |
| `ALTER TABLE routes ADD COLUMN egress_allow TEXT NOT NULL DEFAULT ''`, |
| `ALTER TABLE routes ADD COLUMN size TEXT NOT NULL DEFAULT ''`, |
| `ALTER TABLE routes ADD COLUMN policy_expires_at TEXT NOT NULL DEFAULT ''`, |
| + // Region HA P1a: the born-HA mark (Route.HA), sticky. |
| + `ALTER TABLE routes ADD COLUMN ha INTEGER NOT NULL DEFAULT 0`, |
| } { |
| if _, err := r.sql.Exec(stmt); err != nil && !strings.Contains(err.Error(), "duplicate column") { |
| return err |
| @@ -988,10 +1000,10 @@ provisioned, seed, unit, fleet_box, addrs, health, steering, gate, created_at, |
| args, idle_seconds, max_run_seconds, replicas, replica, claim_incarnation, claim_stamp, claim_owner, claim_row_id, hold_op, |
| good_version, good_digest, good_demand, serving_version, update_settings, build_state, |
| seed_source, import_gate, runtime, rate_limit, assets_off, asset_gen, incarnation, serving_seq, |
| -egress_allow, size, policy_expires_at` |
| +egress_allow, size, policy_expires_at, ha` |
| // routeColCount is the number of names in routeCols. |
| -const routeColCount = 64 |
| +const routeColCount = 65 |
| // placeholders is n comma-joined bind parameters. |
| func placeholders(n int) string { |
| @@ -1000,7 +1012,7 @@ func placeholders(n int) string { |
| func scanRoute(scan func(...any) error) (Route, error) { |
| var t Route |
| - var hib, can, sn, plat, prov, rep, assetsOff int |
| + var hib, can, sn, plat, prov, rep, assetsOff, ha int |
| err := scan(&t.Host, &t.Slug, &t.Kind, &t.Socket, &t.Addr, &t.Status, |
| &t.LastError, &t.DBPath, &t.Version, &t.PrevVersion, &t.App, &t.Pool, |
| &t.Region, &t.Channel, &hib, &can, &t.Replication, &t.NextDue, &t.Backing, |
| @@ -1010,7 +1022,8 @@ func scanRoute(scan func(...any) error) (Route, error) { |
| &t.Args, &t.IdleSeconds, &t.MaxRunSeconds, &t.Replicas, &rep, &t.ClaimIncarnation, &t.ClaimStamp, &t.ClaimOwner, &t.ClaimRowID, &t.HoldOp, |
| &t.GoodVersion, &t.GoodDigest, &t.GoodDemand, &t.ServingVersion, &t.UpdateSettings, &t.BuildState, |
| &t.SeedSource, &t.ImportGate, &t.Runtime, &t.RateLimit, &assetsOff, &t.AssetGen, &t.Incarnation, &t.ServingSeq, |
| - &t.EgressAllow, &t.Size, &t.PolicyExpiresAt) |
| + &t.EgressAllow, &t.Size, &t.PolicyExpiresAt, &ha) |
| + t.HA = ha != 0 |
| t.AssetsOff = assetsOff != 0 |
| t.Hibernate = hib != 0 |
| t.Canary = can != 0 |
| @@ -1248,6 +1261,8 @@ func (r *Registry) Delete(host string) error { |
| // after that only SetHoldIfSeq, ReleaseHoldSeq and InstallLifetimeIf move |
| // it, each with the status it belongs with, so a stale full-row write can |
| // never put a row back into an older lifetime. |
| +// `ha` (region HA P1a) is sticky: the stored mark or the incoming one, |
| +// whichever is set, so no full-row write ever clears a born-HA row. |
| // opsAccount is the ops account's sqid: account #1, pinned by migrate. It is |
| // release.OpsAccount (release cannot be imported here; a test in release |
| // pins the two together through EffectiveAccount). |
| @@ -1336,7 +1351,8 @@ ON CONFLICT(host) DO UPDATE SET |
| size=CASE WHEN `+tenantChangedSQL+` THEN excluded.size |
| WHEN excluded.size = '' THEN routes.size ELSE excluded.size END, |
| policy_expires_at=CASE WHEN `+tenantChangedSQL+` THEN excluded.policy_expires_at |
| - WHEN excluded.policy_expires_at = '' THEN routes.policy_expires_at ELSE excluded.policy_expires_at END |
| + WHEN excluded.policy_expires_at = '' THEN routes.policy_expires_at ELSE excluded.policy_expires_at END, |
| + ha=MAX(routes.ha, excluded.ha) |
| WHERE routes.claim_incarnation=excluded.claim_incarnation AND routes.claim_stamp=excluded.claim_stamp |
| AND routes.claim_owner=excluded.claim_owner AND routes.claim_row_id=excluded.claim_row_id`, |
| routeArgs(normalizeRoute(t))...) |
| @@ -1398,7 +1414,7 @@ func routeArgs(t Route) []any { |
| t.Replicas, b2iDB(t.Replica), t.ClaimIncarnation, t.ClaimStamp, t.ClaimOwner, t.ClaimRowID, t.HoldOp, |
| t.GoodVersion, t.GoodDigest, t.GoodDemand, t.ServingVersion, t.UpdateSettings, t.BuildState, |
| t.SeedSource, t.ImportGate, t.Runtime, t.RateLimit, b2iDB(t.AssetsOff), t.AssetGen, t.Incarnation, t.ServingSeq, |
| - t.EgressAllow, t.Size, t.PolicyExpiresAt, |
| + t.EgressAllow, t.Size, t.PolicyExpiresAt, b2iDB(t.HA), |
| } |
| } |
| @@ -2341,6 +2357,28 @@ func (r *Registry) SetReplica(host string, replica bool) error { |
| return nil |
| } |
| +// SetHA marks a route born HA (Route.HA), converged by the provision pass |
| +// from the instance record's ha flag. It only ever sets the mark: nothing |
| +// clears it (region HA P1a). |
| +func (r *Registry) SetHA(host string) error { |
| + if r.remote != nil { |
| + _, err := r.remote.call(nil, remoteRequest{Method: "SetHA", Host: host}, nil) |
| + return err |
| + } |
| + res, err := r.sql.Exec(`UPDATE routes SET ha = 1 WHERE host = ?`, host) |
| + if err != nil { |
| + return err |
| + } |
| + n, err := res.RowsAffected() |
| + if err != nil { |
| + return err |
| + } |
| + if n == 0 { |
| + return fmt.Errorf("no route for host %q", host) |
| + } |
| + return nil |
| +} |
| + |
| // b2iDB is Upsert's b2i for the narrow setters, which sit outside that |
| // function's closure. |
| func b2iDB(b bool) int { |
| @@ -3723,6 +3761,13 @@ func (r *Registry) SnapshotFingerprint() (string, error) { |
| } |
| continue |
| } |
| + // ha too (region HA P1a): only a born-HA row hashes it. |
| + if cols[i] == "ha" { |
| + if v.Valid && v.String != "" && v.String != "0" { |
| + fmt.Fprintf(h, "ha=%s\x1f", v.String) |
| + } |
| + continue |
| + } |
| if cols[i] == "asset_gen" { |
| if v.Valid && v.String != "" { |
| fmt.Fprintf(h, "asset_gen=%q\x1f", v.String) |
diff --git a/internal/registry/registry_test.go b/internal/registry/registry_test.go| index 87fce828f..049c2ea34 100644 |
| --- a/internal/registry/registry_test.go |
| +++ b/internal/registry/registry_test.go |
| @@ -95,6 +95,7 @@ func TestUpsertRoundTripsAllColumns(t *testing.T) { |
| EgressAllow: "10.0.5.20 fd00:5::20", |
| Size: "large", |
| PolicyExpiresAt: "1900000000", |
| + HA: true, |
| } |
| if err := reg.Upsert(want); err != nil { |
| t.Fatal(err) |
diff --git a/internal/registry/remote_dispatch.go b/internal/registry/remote_dispatch.go| index b38b72537..1d6cf08f7 100644 |
| --- a/internal/registry/remote_dispatch.go |
| +++ b/internal/registry/remote_dispatch.go |
| @@ -269,6 +269,8 @@ func (r *Registry) dispatchRemote(ctx context.Context, q remoteRequest, callback |
| err = r.SetReplicas(q.Host, q.Replicas) |
| case "SetReplica": |
| err = r.SetReplica(q.Host, q.Replica) |
| + case "SetHA": |
| + err = r.SetHA(q.Host) |
| case "SetGate": |
| err = r.SetGate(q.Host, q.Hash) |
| case "SetRateLimit": |