console: per-app frame generations; the walk measures a cold console
Review round 1 on the app-tab frame cache: - The generation lives on each (account, app) slot instead of one counter for the cache, so a clear of another app or account no longer stops an in-flight load from storing. Slots exist only while they hold a frame or have a load in flight, and only get makes one, so a clear of a name nobody viewed allocates nothing. - perfColdReset empties the frame cache and the account-manifest memo, so the walk's cold request meets a cold console. Measured that way, account-review and activity (7 S3 calls, about 185 ms), the account home (6 calls, about 160 ms) and Analytics (11 calls, about 290 ms) miss 150 ms cold, and get honest over records again. Warm, they read 0, 0, 0 and 4. - The blocking tests wait at most 30 s and release their held loads in a defer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+165
−68
internal/console/appframe.go+67 −46internal/console/appframe_test.go+88 −19internal/console/perfwalk_test.go+6 −2internal/console/testdata/perf-budgets.txt+4 −1
CI failed — run details
diff --git a/internal/console/appframe.go b/internal/console/appframe.go| index 2c0fa9baf..22ee8821b 100644 |
| --- a/internal/console/appframe.go |
| +++ b/internal/console/appframe.go |
| @@ -141,28 +141,33 @@ func loadAppFrame(ctx context.Context, st release.Store, pub ed25519.PublicKey, |
| type frameKey struct{ acct, app string } |
| -type frameEntry struct { |
| - frame appFrame |
| - at time.Time |
| +// frameSlot is one (account, app)'s place in the cache: the stored frame, |
| +// if any, and the generation every clear of it bumps. A slot exists only |
| +// while it holds a frame or a load for it is in flight. |
| +type frameSlot struct { |
| + frame appFrame |
| + at time.Time |
| + has bool |
| + gen uint64 |
| + loading int |
| } |
| // appFrames is the cache. The zero value is ready (ttl 0 is appFrameTTL, nil |
| // now is time.Now), like keyedLocks, so &Server{} literals stay safe. It |
| -// holds one entry per app viewed; nothing refreshes in the background. |
| +// holds one slot per app viewed; nothing refreshes in the background. |
| // |
| // The generation is what stops a view that began before a write from |
| -// putting the old frame back after the write's clear: get notes it before |
| -// loading and stores only if no clear has happened since. It is one counter |
| -// for the whole cache, not one per app: the clears run on every non-GET |
| -// request before any gate, so a map keyed by the names in those requests |
| -// could be grown by anyone. A clear anywhere costs an in-flight load |
| -// elsewhere its store, which is one extra load on that app's next view. |
| +// putting the old frame back after the write's clear: get notes its slot's |
| +// generation before loading and stores only if no clear of that slot has |
| +// happened since. Generations live on the slots, so a clear only touches |
| +// slots that already exist: the clears run on every non-GET request before |
| +// any gate, and a name nobody has viewed allocates nothing. Slots are made |
| +// only by get, which page views reach after their gate. |
| type appFrames struct { |
| - mu sync.Mutex |
| - ttl time.Duration |
| - now func() time.Time |
| - m map[frameKey]frameEntry |
| - gen uint64 |
| + mu sync.Mutex |
| + ttl time.Duration |
| + now func() time.Time |
| + slots map[frameKey]*frameSlot |
| } |
| func (c *appFrames) clock() time.Time { |
| @@ -180,60 +185,76 @@ func (c *appFrames) lifetime() time.Duration { |
| } |
| // get answers acct/app's frame from the cache while it is fresh, else from |
| -// load, storing the result only when it has no error and no clear happened |
| -// while it loaded. Either way the caller gets its own copy. |
| +// load, storing the result only when it has no error and no clear of this |
| +// app happened while it loaded. Either way the caller gets its own copy. |
| func (c *appFrames) get(ctx context.Context, acct, app string, load func(context.Context) appFrame) appFrame { |
| k := frameKey{acct, app} |
| c.mu.Lock() |
| now := c.clock() |
| - if e, ok := c.m[k]; ok && now.Before(e.at.Add(c.lifetime())) { |
| - f := e.frame.clone() |
| + sl := c.slots[k] |
| + if sl != nil && sl.has && now.Before(sl.at.Add(c.lifetime())) { |
| + f := sl.frame.clone() |
| c.mu.Unlock() |
| return f |
| } |
| - gen := c.gen |
| + if sl == nil { |
| + if c.slots == nil { |
| + c.slots = make(map[frameKey]*frameSlot) |
| + } |
| + sl = &frameSlot{} |
| + c.slots[k] = sl |
| + } |
| + sl.loading++ |
| + gen := sl.gen |
| c.mu.Unlock() |
| f := load(ctx) |
| - if f.failed() { |
| - return f |
| - } |
| + |
| c.mu.Lock() |
| - if c.gen == gen { |
| - if c.m == nil { |
| - c.m = make(map[frameKey]frameEntry) |
| - } |
| + defer c.mu.Unlock() |
| + sl.loading-- |
| + if sl.gen == gen && !f.failed() { |
| // Stamped with the time the load began: an entry never outlives |
| // the TTL measured from its oldest read. |
| - c.m[k] = frameEntry{frame: f.clone(), at: now} |
| + sl.frame, sl.at, sl.has = f.clone(), now, true |
| } |
| - c.mu.Unlock() |
| + c.dropIfIdle(k, sl) |
| return f |
| } |
| -// clearApp drops app's entry in every account (app names are per account, |
| -// and a CLI door's account segment is not checked here). |
| -func (c *appFrames) clearApp(app string) { |
| - c.mu.Lock() |
| - c.gen++ |
| - for k := range c.m { |
| - if k.app == app { |
| - delete(c.m, k) |
| - } |
| +// dropIfIdle removes a slot that holds no frame and has no load in flight. |
| +// The slot is only removed from the map while no get holds it, so a clear |
| +// always reaches the slot an in-flight load will check. |
| +func (c *appFrames) dropIfIdle(k frameKey, sl *frameSlot) { |
| + if !sl.has && sl.loading == 0 && c.slots[k] == sl { |
| + delete(c.slots, k) |
| } |
| - c.mu.Unlock() |
| } |
| -// clearAccount drops every entry of the account. |
| -func (c *appFrames) clearAccount(sqid string) { |
| +// clearWhere drops the frame of every slot match picks and bumps its |
| +// generation, so a load in flight for it will not store. |
| +func (c *appFrames) clearWhere(match func(frameKey) bool) { |
| c.mu.Lock() |
| - c.gen++ |
| - for k := range c.m { |
| - if k.acct == sqid { |
| - delete(c.m, k) |
| + defer c.mu.Unlock() |
| + for k, sl := range c.slots { |
| + if !match(k) { |
| + continue |
| } |
| + sl.gen++ |
| + sl.frame, sl.has = appFrame{}, false |
| + c.dropIfIdle(k, sl) |
| } |
| - c.mu.Unlock() |
| +} |
| + |
| +// clearApp drops app's frame in every account (app names are per account, |
| +// and a CLI door's account segment is not checked here). |
| +func (c *appFrames) clearApp(app string) { |
| + c.clearWhere(func(k frameKey) bool { return k.app == app }) |
| +} |
| + |
| +// clearAccount drops every frame of the account. |
| +func (c *appFrames) clearAccount(sqid string) { |
| + c.clearWhere(func(k frameKey) bool { return k.acct == sqid }) |
| } |
| // frameBypassKey marks a non-GET request's context: appFrame loads fresh |
diff --git a/internal/console/appframe_test.go b/internal/console/appframe_test.go| index 7ff21f56c..35bd2ba43 100644 |
| --- a/internal/console/appframe_test.go |
| +++ b/internal/console/appframe_test.go |
| @@ -3,6 +3,7 @@ package console |
| import ( |
| "context" |
| "errors" |
| + "fmt" |
| "io" |
| "io/fs" |
| "net/http" |
| @@ -44,21 +45,50 @@ func countingLoad(version string, n *int) func(context.Context) appFrame { |
| } |
| } |
| +// frameWait fails the test unless ch is ready within testWait. |
| +func frameWait[T any](t *testing.T, ch <-chan T, what string) T { |
| + t.Helper() |
| + select { |
| + case v := <-ch: |
| + return v |
| + case <-time.After(testWait): |
| + t.Fatalf("timed out after %v waiting for %s", testWait, what) |
| + var zero T |
| + return zero |
| + } |
| +} |
| + |
| +const testWait = 30 * time.Second |
| + |
| +// releaseOnce returns a channel and an idempotent close for it, so a test |
| +// can release a blocked load early and still defer the release. |
| +func releaseOnce() (chan struct{}, func()) { |
| + ch := make(chan struct{}) |
| + var once sync.Once |
| + return ch, func() { once.Do(func() { close(ch) }) } |
| +} |
| + |
| // TestAppFrameLoadDuringWriteNotStored: a load that started before a clear |
| -// and finishes after it must not put its (pre-write) frame into the cache. |
| +// of its app (or account) and finishes after it must not put its |
| +// (pre-write) frame into the cache. A clear of another app leaves it |
| +// eligible. |
| func TestAppFrameLoadDuringWriteNotStored(t *testing.T) { |
| for _, tc := range []struct { |
| - name string |
| - clear func(c *appFrames) |
| + name string |
| + clear func(c *appFrames) |
| + stored bool |
| }{ |
| - {"clearApp", func(c *appFrames) { c.clearApp("kass") }}, |
| - {"clearAccount", func(c *appFrames) { c.clearAccount("bab") }}, |
| - {"clearApp of another app", func(c *appFrames) { c.clearApp("other") }}, |
| + {"clearApp", func(c *appFrames) { c.clearApp("kass") }, false}, |
| + {"clearAccount", func(c *appFrames) { c.clearAccount("bab") }, false}, |
| + {"clearApp of another app", func(c *appFrames) { c.clearApp("other") }, true}, |
| + {"clearAccount of another account", func(c *appFrames) { c.clearAccount("other") }, true}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| var c appFrames |
| - started, unblock := make(chan struct{}), make(chan struct{}) |
| - done := make(chan appFrame) |
| + started := make(chan struct{}) |
| + unblock, release := releaseOnce() |
| + defer release() |
| + done := make(chan appFrame, 1) |
| go func() { |
| done <- c.get(context.Background(), "bab", "kass", func(context.Context) appFrame { |
| close(started) |
| @@ -66,14 +96,20 @@ func TestAppFrameLoadDuringWriteNotStored(t *testing.T) { |
| return testFrame("old") |
| }) |
| }() |
| - <-started |
| + frameWait(t, started, "the load to start") |
| tc.clear(&c) |
| - close(unblock) |
| - if f := <-done; f.Channels[0].Pointer.Version != "old" { |
| + release() |
| + if f := frameWait(t, done, "the in-flight get"); f.Channels[0].Pointer.Version != "old" { |
| t.Fatalf("the in-flight get answered %q, want its own load's value", f.Channels[0].Pointer.Version) |
| } |
| n := 0 |
| f := c.get(context.Background(), "bab", "kass", countingLoad("new", &n)) |
| + if tc.stored { |
| + if n != 0 || f.Channels[0].Pointer.Version != "old" { |
| + t.Fatalf("after an unrelated clear during a load: load calls %d, version %q; want 0 and \"old\" (stored)", n, f.Channels[0].Pointer.Version) |
| + } |
| + return |
| + } |
| if n != 1 || f.Channels[0].Pointer.Version != "new" { |
| t.Fatalf("after a clear during a load: load calls %d, version %q; want 1 and \"new\"", n, f.Channels[0].Pointer.Version) |
| } |
| @@ -81,6 +117,30 @@ func TestAppFrameLoadDuringWriteNotStored(t *testing.T) { |
| } |
| } |
| +// TestAppFrameClearOfUnknownNameAllocatesNothing: the hook clears whatever |
| +// a request's path names, before any gate; a name with no slot adds none. |
| +func TestAppFrameClearOfUnknownNameAllocatesNothing(t *testing.T) { |
| + var c appFrames |
| + c.get(context.Background(), "bab", "kass", func(context.Context) appFrame { return testFrame("v1") }) |
| + for i := range 1000 { |
| + c.clearApp(fmt.Sprintf("nobody-%d", i)) |
| + c.clearAccount(fmt.Sprintf("nobody-%d", i)) |
| + } |
| + c.mu.Lock() |
| + n := len(c.slots) |
| + c.mu.Unlock() |
| + if n != 1 { |
| + t.Fatalf("slots = %d after clears of unknown names, want 1", n) |
| + } |
| + c.clearApp("kass") |
| + c.mu.Lock() |
| + n = len(c.slots) |
| + c.mu.Unlock() |
| + if n != 0 { |
| + t.Fatalf("slots = %d after clearing the only app, want 0", n) |
| + } |
| +} |
| + |
| // TestAppFrameConcurrentGetsAndClears is the race detector's case: views and |
| // writes on the same app at once. After the last clear, the next get loads. |
| func TestAppFrameConcurrentGetsAndClears(t *testing.T) { |
| @@ -590,7 +650,11 @@ func TestAppFrameGetDuringWrite(t *testing.T) { |
| stableKey := release.ChannelsPrefix(fx.sqid, "kass") + "stable.json" |
| var mu sync.Mutex |
| armed := true |
| - reached, hold := make(chan struct{}), make(chan struct{}) |
| + reached := make(chan struct{}) |
| + hold, release := releaseOnce() |
| + // Released however the test ends, so neither the held view nor the |
| + // fake's handler outlives it. |
| + defer release() |
| lat.Fake().OnRequest(func(r *http.Request) (int, <-chan struct{}) { |
| mu.Lock() |
| defer mu.Unlock() |
| @@ -601,7 +665,7 @@ func TestAppFrameGetDuringWrite(t *testing.T) { |
| } |
| return 0, nil |
| }) |
| - viewed := make(chan string) |
| + viewed := make(chan string, 1) |
| go func() { |
| res, err := c.Get(fx.u("/apps/kass/releases")) |
| if err != nil { |
| @@ -612,15 +676,15 @@ func TestAppFrameGetDuringWrite(t *testing.T) { |
| b, _ := io.ReadAll(res.Body) |
| viewed <- string(b) |
| }() |
| - <-reached |
| + frameWait(t, reached, "the view's read of the stable pointer") |
| status, loc, body := postForm(t, c, fx.u("/apps/kass/promote"), |
| url.Values{"csrf": {csrf}, "channel": {"stable"}, "version": {"v2"}, "confirmed": {"1"}}) |
| if status != http.StatusSeeOther { |
| t.Fatalf("promote = %d -> %q:\n%s", status, loc, body) |
| } |
| wantChannel(t, fx, "stable", "v2") |
| - close(hold) |
| - if got := channelsTableVersion(<-viewed, "stable"); got != "v1" { |
| + release() |
| + if got := channelsTableVersion(frameWait(t, viewed, "the held view"), "stable"); got != "v1" { |
| t.Fatalf("the view that began before the promote shows stable at %q, want v1 (its own reads)", got) |
| } |
| _, _, body = get(t, c, fx.u("/apps/kass/releases")) |
| @@ -629,12 +693,17 @@ func TestAppFrameGetDuringWrite(t *testing.T) { |
| } |
| } |
| -// cached reports whether acct/app has an entry, fresh or not. |
| +// cached reports whether acct/app has a stored frame, fresh or not. |
| func (c *appFrames) cached(acct, app string) bool { |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| - _, ok := c.m[frameKey{acct, app}] |
| - return ok |
| + sl := c.slots[frameKey{acct, app}] |
| + return sl != nil && sl.has |
| +} |
| + |
| +// forgetAll empties the cache: perfColdReset's cold console. |
| +func (c *appFrames) forgetAll() { |
| + c.clearWhere(func(frameKey) bool { return true }) |
| } |
| // TestAppFrameWriteHookClears: registerRoute's hook clears the app (or the |
diff --git a/internal/console/perfwalk_test.go b/internal/console/perfwalk_test.go| index 165877b64..7efec4070 100644 |
| --- a/internal/console/perfwalk_test.go |
| +++ b/internal/console/perfwalk_test.go |
| @@ -66,8 +66,12 @@ type perfScreenTarget struct { |
| var perfQuiesce = func(fx *consoleFixture) {} |
| // perfColdReset runs before a screen's cold request, so that it meets a |
| -// console that has cached nothing for it. Plan 2 fills it in. |
| -var perfColdReset = func(fx *consoleFixture) {} |
| +// console that has cached nothing for it: no app-tab frames (appframe.go) |
| +// and no remembered account manifests. |
| +var perfColdReset = func(fx *consoleFixture) { |
| + fx.srv.frames.forgetAll() |
| + fx.srv.manifestSeen.Clear() |
| +} |
| // perfEnforce is whether timing fails the walk: only in CI, where the |
| // machine is quiet enough to hold to a number. Call counts and fixture |
diff --git a/internal/console/testdata/perf-budgets.txt b/internal/console/testdata/perf-budgets.txt| index 1b168876b..b6ad20440 100644 |
| --- a/internal/console/testdata/perf-budgets.txt |
| +++ b/internal/console/testdata/perf-budgets.txt |
| @@ -39,7 +39,9 @@ GET /a/{sqid}/apps/{app}/logs/data deferred: deferred part (the Logs tab's data |
| # is read once per process and then remembered. |
| /fleet/architecture over: reads economics/active.json, infra/inventory.json and status/fleet.json, and lists and reads every box report (8 S3 calls warm) |
| GET /a/{sqid}/apps/{app} over: Overview is the release listing and every manifest (3 S3 calls warm, median about 80 ms); cold, also the app-tab frame and every box's metrics hours for two days (about 110) |
| -GET /a/{sqid}/apps/{app}/analytics calls 4: both telemetry settings, the analytics day listing and one day read |
| +GET /a/{sqid}/apps/{app}/account-review over: a warm view reads nothing (the app-tab frame is cached); a cold view reads the frame and the account manifest (7 S3 calls, about 185 ms) |
| +GET /a/{sqid}/apps/{app}/activity over: a warm view reads nothing (the app-tab frame is cached); a cold view reads the frame and the account manifest (7 S3 calls, about 185 ms) |
| +GET /a/{sqid}/apps/{app}/analytics over: both telemetry settings, the analytics day listing and one day read (4 S3 calls warm, median about 105 ms); cold, also the app-tab frame and the account manifest (11 S3 calls, about 290 ms) |
| GET /a/{sqid}/apps/{app}/availability over: both telemetry settings and three listings and a read each for the log and error chunks (10 S3 calls warm) |
| GET /a/{sqid}/apps/{app}/errors over: both telemetry settings and three listings and a read each for the log and error chunks (10 S3 calls warm) |
| GET /a/{sqid}/apps/{app}/instances over: instances.json, and every edges file and box report listed and read twice (17 S3 calls warm) |
| @@ -52,6 +54,7 @@ GET /a/{sqid}/fleet over: reads status/fleet.json and lists and reads each custo |
| GET /a/{sqid}/fleets/{fleet} over: lists and reads the fleet's box reports (2 S3 calls warm) |
| GET /a/{sqid}/plan/keep over: the lapse inventory lists every app's instance records and reads each one, lists every app's sidecar records, and lists the domain claims (8 S3 calls warm for two apps and three instances, median 209 ms; it grows with the account) |
| GET /a/{sqid}/settings over: reads the account and app telemetry settings and the storage entitlement (3 S3 calls warm) |
| +GET /a/{sqid}/{$} over: a warm view reads nothing (each app's frame is cached); a cold view reads every app's frame (6 S3 calls per app, about 160 ms for one) |
| GET /a/{sqid}/trash over: reads each trashed app's deleted.json marker (1 S3 call warm per app) |
| GET /fleet over: lists and reads every box report, reads status/fleet.json, and lists and reads the storage entitlements (8 S3 calls warm) |
| GET /fleet/economics over: the cold view sweeps usage for every day of the period (about 160 S3 calls, over 4 s); warm views come from the 60 s cache |