host: publish box_mem_total_mib in the edges file
The box's MemTotal (capacityMiB's memory half, read once at loop arm) is a top-level edges-file field, box_mem_total_mib, published with region HA on or off and inside the change gate: a changed total writes once, an unchanged one never does. PlacementView reads it as BoxMemTotalMiB (0 = missing or unreadable), independent of Headroom's nested mem_total_mib (ruling C3). TestSizesAddNoBoxStoreRequests pins the box side of the §3a budget: a day of passes makes the same requests with the total as without, and a changed total adds one GET and one PUT.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+198
−1
internal/edgesfile/snapshot.go+6 −1internal/edgesfile/snapshot_test.go+46 −0internal/host/edgesfile.go+11 −0internal/host/edgesfile_test.go+135 −0
diff --git a/internal/edgesfile/snapshot.go b/internal/edgesfile/snapshot.go| index bbfd5c6b5..9af4b4908 100644 |
| --- a/internal/edgesfile/snapshot.go |
| +++ b/internal/edgesfile/snapshot.go |
| @@ -67,7 +67,12 @@ type PlacementView struct { |
| DiskTotalMiB int64 `json:"disk_total_mib"` |
| Awake int `json:"awake"` |
| } `json:"headroom"` |
| - Hosts map[string]json.RawMessage `json:"hosts"` |
| + // BoxMemTotalMiB is the box's memory total, published at the top level |
| + // with region HA on or off (instance sizes plan 3, #329). 0 is missing |
| + // or unreadable. Not Headroom.MemTotalMiB, which only a region HA box |
| + // publishes (ruling C3); a reader falls back to that one at 0. |
| + BoxMemTotalMiB int64 `json:"box_mem_total_mib"` |
| + Hosts map[string]json.RawMessage `json:"hosts"` |
| // Generation is when the box last wrote the file (RFC 3339), which |
| // region HA spec 2's placement policy reads as headroom's age. |
diff --git a/internal/edgesfile/snapshot_test.go b/internal/edgesfile/snapshot_test.go| index 4f142a5f5..0caba451d 100644 |
| --- a/internal/edgesfile/snapshot_test.go |
| +++ b/internal/edgesfile/snapshot_test.go |
| @@ -3,6 +3,7 @@ package edgesfile |
| import ( |
| "bytes" |
| "context" |
| + "encoding/json" |
| "errors" |
| "log" |
| "os" |
| @@ -134,3 +135,48 @@ func (f failingStatusList) List(ctx context.Context, prefix string) ([]string, e |
| } |
| return f.Store.List(ctx, prefix) |
| } |
| + |
| +// Instance sizes plan 3, Task 8: the top-level box_mem_total_mib is read |
| +// as BoxMemTotalMiB; absent is 0 (a writer that predates it, or a box whose |
| +// read failed). |
| +func TestPlacementViewReadsBoxMemTotal(t *testing.T) { |
| + for body, want := range map[string]int64{ |
| + `{"v":1,"box":"box-a","box_mem_total_mib":3884,"hosts":{}}`: 3884, |
| + `{"v":1,"box":"box-a","hosts":{}}`: 0, |
| + } { |
| + var f PlacementView |
| + if err := json.Unmarshal([]byte(body), &f); err != nil { |
| + t.Fatal(err) |
| + } |
| + if f.BoxMemTotalMiB != want { |
| + t.Errorf("%s: BoxMemTotalMiB = %d, want %d", body, f.BoxMemTotalMiB, want) |
| + } |
| + } |
| +} |
| + |
| +// Ruling C3: the nested headroom mem_total_mib (region HA only) and the |
| +// top-level box_mem_total_mib are two fields, each decoded on its own. |
| +func TestPlacementViewDecodesBothMemTotalsIndependently(t *testing.T) { |
| + var f PlacementView |
| + body := `{"v":1,"box":"box-a","box_mem_total_mib":3884,"headroom":{"mem_free_mib":512,"mem_total_mib":4096},"hosts":{}}` |
| + if err := json.Unmarshal([]byte(body), &f); err != nil { |
| + t.Fatal(err) |
| + } |
| + if f.BoxMemTotalMiB != 3884 || f.Headroom == nil || f.Headroom.MemTotalMiB != 4096 { |
| + t.Fatalf("top-level %d, headroom %+v; want 3884 and 4096", f.BoxMemTotalMiB, f.Headroom) |
| + } |
| + var top PlacementView |
| + if err := json.Unmarshal([]byte(`{"v":1,"box":"box-a","box_mem_total_mib":3884,"hosts":{}}`), &top); err != nil { |
| + t.Fatal(err) |
| + } |
| + if top.BoxMemTotalMiB != 3884 || top.Headroom != nil { |
| + t.Fatalf("top-level only: %d, headroom %+v", top.BoxMemTotalMiB, top.Headroom) |
| + } |
| + var nested PlacementView |
| + if err := json.Unmarshal([]byte(`{"v":1,"box":"box-a","headroom":{"mem_total_mib":4096},"hosts":{}}`), &nested); err != nil { |
| + t.Fatal(err) |
| + } |
| + if nested.BoxMemTotalMiB != 0 || nested.Headroom == nil || nested.Headroom.MemTotalMiB != 4096 { |
| + t.Fatalf("headroom only: %d, headroom %+v", nested.BoxMemTotalMiB, nested.Headroom) |
| + } |
| +} |
diff --git a/internal/host/edgesfile.go b/internal/host/edgesfile.go| index 6db2376fe..5af30612b 100644 |
| --- a/internal/host/edgesfile.go |
| +++ b/internal/host/edgesfile.go |
| @@ -393,6 +393,14 @@ type edgesFile struct { |
| // read failed, which the console treats as lacking, like a file |
| // written before the field existed. |
| Addrs []string `json:"addrs"` |
| + // BoxMemTotalMiB is this box's MemTotal in MiB, read once at loop arm |
| + // (capacityMiB's memory half), for the console's size door and its |
| + // committed figure (instance sizes spec §5.3, #329). Published with |
| + // region HA on or off; Headroom's nested mem_total_mib is a different |
| + // field that rides headroom only (ruling C3). Inside the change gate, |
| + // so a changed total writes once and an unchanged one never does (spec |
| + // §3a). Omitempty: 0 is a failed read, which a reader takes as missing. |
| + BoxMemTotalMiB int64 `json:"box_mem_total_mib,omitempty"` |
| Generation string `json:"generation"` |
| Hosts map[string]edgeHost `json:"hosts"` |
| @@ -1026,6 +1034,9 @@ func edgesFileLoop(reg *registry.Registry, lifetimes bool, sb servingBuild) { |
| BoxID: box, |
| Capabilities: edgesFileCapabilities(os.Getenv), |
| } |
| + // The box's memory does not change under a running agent, so the |
| + // total is read once here; the disk half is not wanted. |
| + ident.BoxMemTotalMiB, _ = capacityMiB(readMemBytes, func() (uint64, uint64, error) { return 0, 0, errors.ErrUnsupported }) |
| // The flag-derived capabilities; offline-move-1 joins them on each pass |
| // once the moves worker is armed and capable, and db-read-1 joins them |
| // on each pass while the armed db-reads loop's last readiness probe |
diff --git a/internal/host/edgesfile_test.go b/internal/host/edgesfile_test.go| index 4b3b8ed57..c79e6950f 100644 |
| --- a/internal/host/edgesfile_test.go |
| +++ b/internal/host/edgesfile_test.go |
| @@ -2232,3 +2232,138 @@ func inspectFunc(t *testing.T, file, fn string, visit func(ast.Node)) { |
| } |
| t.Fatalf("%s: no func %s", file, fn) |
| } |
| + |
| +// ---- the box's memory total (instance sizes plan 3, Task 8) ---------------- |
| + |
| +// box_mem_total_mib is published at the top level with region HA off (no |
| +// headroom), so the console's size door can judge a size against the box |
| +// without waiting on region HA (ruling C3: the nested headroom total is a |
| +// different field, published only with headroom). |
| +func TestEdgesFilePublishesBoxMemTotal(t *testing.T) { |
| + t.Parallel() |
| + ctx := context.Background() |
| + st := release.NewDirStore(t.TempDir()) |
| + reg := edgesTestRegistry(t) |
| + seedRoute(t, reg, "app.example.com") |
| + var last []byte |
| + if err := edgesFileWriteClaims(ctx, st, reg, edgesFile{Box: "annie", BoxMemTotalMiB: 3884}, "1.2.3.4", &last, false, nil, false, nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + body, _, err := st.Get(ctx, edgesFileKey("annie")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var top map[string]json.RawMessage |
| + if err := json.Unmarshal(body, &top); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := string(top["box_mem_total_mib"]); got != "3884" { |
| + t.Errorf("top-level box_mem_total_mib = %q, want 3884 in %s", got, body) |
| + } |
| + if _, ok := top["headroom"]; ok { |
| + t.Errorf("region HA off published headroom: %s", body) |
| + } |
| + |
| + // An unreadable total (0) publishes no key at all. |
| + var last0 []byte |
| + if err := edgesFileWriteClaims(ctx, st, reg, edgesFile{Box: "betty"}, "1.2.3.4", &last0, false, nil, false, nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + body, _, err = st.Get(ctx, edgesFileKey("betty")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if bytes.Contains(body, []byte("box_mem_total_mib")) { |
| + t.Errorf("a 0 total was published: %s", body) |
| + } |
| +} |
| + |
| +// The total is inside the change gate: a changed total writes again, an |
| +// unchanged one writes nothing, and a restart's seeded gate carries it. |
| +func TestEdgesFileBoxMemTotalIsInTheGate(t *testing.T) { |
| + t.Parallel() |
| + ctx := context.Background() |
| + st := &putCountingStore{Store: release.NewDirStore(t.TempDir())} |
| + reg := edgesTestRegistry(t) |
| + seedRoute(t, reg, "app.example.com") |
| + var last []byte |
| + pass := func(total int64) { |
| + t.Helper() |
| + if err := edgesFileWriteClaims(ctx, st, reg, edgesFile{Box: "annie", BoxMemTotalMiB: total}, "1.2.3.4", &last, false, nil, false, nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + pass(3884) |
| + pass(3884) |
| + if st.puts != 1 { |
| + t.Fatalf("puts = %d, want 1: an unchanged total is not a reason to write", st.puts) |
| + } |
| + pass(7820) |
| + if st.puts != 2 { |
| + t.Fatalf("puts = %d, want 2: a changed total must be published", st.puts) |
| + } |
| + if f := readEdgesFile(t, st, "annie"); f.BoxMemTotalMiB != 7820 { |
| + t.Fatalf("published total = %d, want 7820", f.BoxMemTotalMiB) |
| + } |
| + pass(7820) |
| + if st.puts != 2 { |
| + t.Fatalf("puts = %d, want 2: an unchanged total is not a reason to write", st.puts) |
| + } |
| + last = seedEdgesFileGate(ctx, st, "annie") |
| + pass(7820) |
| + if st.puts != 2 { |
| + t.Fatalf("puts = %d, want 2: a restart rewrote an unchanged total", st.puts) |
| + } |
| +} |
| + |
| +// The box-side request budget for sizes (spec §3a, ruling 11), pinned: a |
| +// day of the loop's passes (288, one per five minutes) with the routes and |
| +// the total unchanged makes exactly the requests the same passes make with |
| +// no total published, with or without headroom. A changed total adds one |
| +// write: putReplacing's read of the ETag and its Put. (The activator's |
| +// live-size loop takes no store at all: Task 6's retrySizeLocked reads |
| +// a.Routes, the local registry.) |
| +func TestSizesAddNoBoxStoreRequests(t *testing.T) { |
| + t.Parallel() |
| + ctx := context.Background() |
| + reg := edgesTestRegistry(t) |
| + seedRoute(t, reg, "app.example.com") |
| + day := func(ident edgesFile) []string { |
| + st := &egressCountingStore{Store: release.NewDirStore(t.TempDir())} |
| + var last []byte |
| + for i := 0; i < 288; i++ { |
| + if err := edgesFileWriteClaims(ctx, st, reg, ident, "1.2.3.4", &last, i%2 == 0, nil, false, nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return slices.Clone(st.seen) |
| + } |
| + for _, without := range []edgesFile{ |
| + {Box: "annie"}, |
| + {Box: "annie", Capabilities: []string{capRegionHA1}, Headroom: &edgeHeadroom{MemFreeMiB: 100, MemTotalMiB: 1000}}, |
| + } { |
| + with := without |
| + with.BoxMemTotalMiB = 3884 |
| + w, wo := day(with), day(without) |
| + if !slices.Equal(w, wo) { |
| + t.Errorf("headroom %v: with the total %d requests %v, without %d %v", without.Headroom != nil, len(w), w, len(wo), wo) |
| + } |
| + } |
| + |
| + st := &egressCountingStore{Store: release.NewDirStore(t.TempDir())} |
| + var last []byte |
| + pass := func(total int64) { |
| + t.Helper() |
| + if err := edgesFileWriteClaims(ctx, st, reg, edgesFile{Box: "annie", BoxMemTotalMiB: total}, "1.2.3.4", &last, false, nil, false, nil, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + pass(3884) |
| + before := st.total() |
| + pass(3884) |
| + pass(7820) |
| + pass(7820) |
| + if got, want := st.seen[before:], []string{"GET " + edgesFileKey("annie"), "PUT " + edgesFileKey("annie")}; !slices.Equal(got, want) { |
| + t.Errorf("a changed total made %v, want %v", got, want) |
| + } |
| +} |