console-perf-hot-routes: review fixes: bounded prefetch bytes, generation-checked memo reuse, streaming fallback for bootstrap
Astra round 1 (NOT READY), all three findings: - P1: the prefetch held every selected box report before status refused the oversized ones, so a customer fleet's 200 reports of 32 MiB could be held at once. Bodies are now fetched ahead in the dry pass's order through a window of readConcurrency slots, each held until the real pass takes it, and no box report is read past status's cap and one byte (boxReportCap, held equal to status's by TestBoxReportCapIsStatusCap): an oversized body is cut while streaming, and status still refuses it and reports the read incomplete. TestBoxLoadHoldsBoundedBytes: 30 reports of 2 MiB hold at most ten bodies of the cap and one byte. - P2: pageBoxReports held a request's memoized box read even when an invalidation came after that read began. The memo now carries the cache generation from before its read, and the cache holds it only at the same generation; otherwise it loads afresh. TestPageBoxReportsTakesNoMemoReadFromBeforeAnInvalidation. - P2: a failed conditional read of bootstrap/carlos (errors.ErrUnsupported from a prefix store, a refusal) returned unreadable for good. It now forgets the kept sum and streams the object, unless the object is gone or the request has ended; the kept sum is never the answer to an error. TestBootstrapFallsBackToStreamingWhenTheConditionalReadFails. The 9B8 re-pins (handleAPIInstanceList, pageRecords) do not move.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+413
−55
internal/console/apisystemupdate.go+20 −14internal/console/hotreads.go+199 −35internal/console/hotreads_test.go+125 −0internal/console/perfhotroutes_test.go+57 −0internal/console/remoteinstances.go+4 −2internal/console/reqmemo.go+7 −3internal/console/server.go+1 −1
CI passed — run details
diff --git a/internal/console/apisystemupdate.go b/internal/console/apisystemupdate.go| index f6e1cb209..a32807071 100644 |
| --- a/internal/console/apisystemupdate.go |
| +++ b/internal/console/apisystemupdate.go |
| @@ -741,8 +741,8 @@ type bootstrapHash struct { |
| // object comes back whole (in memory, once per change) and is hashed |
| // afresh. So every status read still asks the bucket, and answers for the |
| // object as it is at that moment; only the download of an unchanged binary |
| -// is gone. A store with no conditional read streams it every time, as |
| -// before. |
| +// is gone. A store with no conditional read, or a conditional read that |
| +// fails while the request is still live, streams it, as before. |
| func (s *Server) bootstrapSHA256(ctx context.Context) (string, error) { |
| h := &s.bootstrapSum |
| h.mu.Lock() |
| @@ -753,21 +753,27 @@ func (s *Server) bootstrapSHA256(ctx context.Context) (string, error) { |
| err = ctx.Err() |
| } |
| switch { |
| - case err != nil: |
| - if errors.Is(err, os.ErrNotExist) { |
| - h.etag, h.sum = "", "" |
| + case err == nil && !changed: |
| + return h.sum, nil |
| + case err == nil: |
| + sum := sha256.Sum256(body) |
| + got := hex.EncodeToString(sum[:]) |
| + h.etag, h.sum = etag, got |
| + if etag == "" { |
| + h.sum = "" |
| } |
| + return got, nil |
| + case errors.Is(err, os.ErrNotExist): |
| + h.etag, h.sum = "", "" |
| + return "", err |
| + case ctx.Err() != nil: |
| return "", err |
| - case !changed: |
| - return h.sum, nil |
| - } |
| - sum := sha256.Sum256(body) |
| - got := hex.EncodeToString(sum[:]) |
| - h.etag, h.sum = etag, got |
| - if etag == "" { |
| - h.sum = "" |
| } |
| - return got, nil |
| + // The conditional read failed some other way (a store that cannot |
| + // make one answers errors.ErrUnsupported): forget the kept sum and |
| + // stream the object, as a first read does. The kept sum is never |
| + // the answer after an error. |
| + h.etag, h.sum = "", "" |
| } |
| rc, etag, err := release.OpenObject(ctx, s.cfg.Releases, bootstrapKey) |
| if err != nil { |
diff --git a/internal/console/hotreads.go b/internal/console/hotreads.go| index 7d743a51a..61aee5c42 100644 |
| --- a/internal/console/hotreads.go |
| +++ b/internal/console/hotreads.go |
| @@ -3,7 +3,9 @@ package console |
| import ( |
| "context" |
| "errors" |
| + "io" |
| "slices" |
| + "strings" |
| "sync" |
| "sync/atomic" |
| @@ -79,22 +81,43 @@ var errNoListMod = errors.New("console: the store cannot list modification times |
| // read concurrently without being rewritten. readConcurrently runs the |
| // reader twice over it. The first pass is dry: its listings go to the |
| // store and are kept, and each Get is noted and answered with dryBody, |
| -// which the reader accepts without a word. The noted keys are then fetched |
| -// concurrently. The second pass is the real one: it gets the kept listings |
| -// and each prefetched read once, so it decides on exactly the bytes it |
| -// would have read itself. The reader keeps every rule of its own (which |
| -// keys it reads, its caps, how it judges a body), so nothing is copied out |
| -// of it. |
| +// which the reader accepts without a word. The second pass is the real |
| +// one: it gets the kept listings, and the noted keys are fetched ahead of |
| +// it, in the order the dry pass asked for them, while it runs. So it |
| +// decides on exactly the bytes it would have read itself. The reader keeps |
| +// every rule of its own (which keys it reads, its caps, how it judges a |
| +// body), so nothing is copied out of it. |
| +// |
| +// The bytes held are bounded, not only the requests: a fetched body holds |
| +// one of readConcurrency window slots until the real pass takes it, so at |
| +// most readConcurrency bodies are in flight or waiting at once. And no body |
| +// is read past limit(key)+1 bytes, the same cap the reader applies (a body |
| +// over it is cut there, still over the cap, and the reader refuses it as |
| +// it always did), so a box that writes huge reports costs the console at |
| +// most readConcurrency times the cap, however many it writes. |
| type prefetchStore struct { |
| release.Store |
| dryBody []byte |
| + // limit is the most bytes the reader accepts for key, 0 for no limit. |
| + limit func(key string) int64 |
| - mu sync.Mutex |
| - dry bool |
| - lists map[string]listed |
| - mods map[string]listedMod |
| - asked []string |
| - held map[string]fetched |
| + mu sync.Mutex |
| + dry bool |
| + lists map[string]listed |
| + mods map[string]listedMod |
| + asked []string |
| + pending map[string]*pendingGet |
| + window chan struct{} |
| + // held is the bytes fetched and not yet taken; stats its most, and the |
| + // largest body, for the tests. |
| + held int64 |
| + stats prefetchStats |
| +} |
| + |
| +// prefetchStats is what one concurrent read held: the most fetched bytes |
| +// at once, and the largest single body. |
| +type prefetchStats struct { |
| + Peak, Largest int64 |
| } |
| type listed struct { |
| @@ -113,6 +136,15 @@ type fetched struct { |
| err error |
| } |
| +// pendingGet is one key the dry pass named: whether its fetch has started, |
| +// whether the real pass has asked for it, and the fetch's result once done |
| +// is closed. |
| +type pendingGet struct { |
| + started, taken bool |
| + done chan struct{} |
| + f fetched |
| +} |
| + |
| func (p *prefetchStore) List(ctx context.Context, prefix string) ([]string, error) { |
| p.mu.Lock() |
| l, ok := p.lists[prefix] |
| @@ -152,39 +184,171 @@ func (p *prefetchStore) Get(ctx context.Context, key string) ([]byte, string, er |
| p.mu.Unlock() |
| return slices.Clone(p.dryBody), "", nil |
| } |
| - f, ok := p.held[key] |
| - delete(p.held, key) |
| + pg, ok := p.pending[key] |
| + if !ok || pg.taken { |
| + p.mu.Unlock() |
| + f := p.readOne(ctx, key) |
| + return f.body, f.etag, f.err |
| + } |
| + pg.taken = true |
| + started := pg.started |
| p.mu.Unlock() |
| - if ok { |
| + if !started { |
| + // Asked before its turn: read it here (the fetcher skips it). |
| + f := p.readOne(ctx, key) |
| return f.body, f.etag, f.err |
| } |
| - return p.Store.Get(ctx, key) |
| + <-pg.done |
| + p.mu.Lock() |
| + p.held -= int64(len(pg.f.body)) |
| + f := pg.f |
| + pg.f = fetched{} |
| + p.mu.Unlock() |
| + <-p.window |
| + return f.body, f.etag, f.err |
| +} |
| + |
| +// readOne reads key from the store, no further than its limit and one |
| +// byte: a body over the limit comes back cut at limit+1 bytes, which the |
| +// reader refuses as over its cap without the rest ever being held. |
| +func (p *prefetchStore) readOne(ctx context.Context, key string) fetched { |
| + rc, etag, err := release.OpenObject(ctx, p.Store, key) |
| + if errors.Is(err, errors.ErrUnsupported) { |
| + body, etag, err := p.Store.Get(ctx, key) |
| + return fetched{body: body, etag: etag, err: err} |
| + } |
| + if err != nil { |
| + return fetched{err: err} |
| + } |
| + defer rc.Close() |
| + var r io.Reader = rc |
| + if n := p.limitOf(key); n > 0 { |
| + r = io.LimitReader(rc, n+1) |
| + } |
| + body, err := io.ReadAll(r) |
| + if err != nil { |
| + return fetched{err: err} |
| + } |
| + return fetched{body: body, etag: etag} |
| +} |
| + |
| +func (p *prefetchStore) limitOf(key string) int64 { |
| + if p.limit == nil { |
| + return 0 |
| + } |
| + return p.limit(key) |
| +} |
| + |
| +// fetchAhead fetches keys in order, each into its pendingGet, holding a |
| +// window slot from the start of a fetch until the real pass takes the |
| +// body. It returns when stop is closed or every key has been started, and |
| +// done is closed once every fetch it started has finished. |
| +func (p *prefetchStore) fetchAhead(ctx context.Context, keys []string, stop <-chan struct{}, done chan<- struct{}, panicked *atomic.Pointer[any]) { |
| + var wg sync.WaitGroup |
| + defer func() { |
| + wg.Wait() |
| + close(done) |
| + }() |
| + for _, k := range keys { |
| + select { |
| + case p.window <- struct{}{}: |
| + case <-stop: |
| + return |
| + } |
| + p.mu.Lock() |
| + pg := p.pending[k] |
| + if pg.taken { |
| + p.mu.Unlock() |
| + <-p.window |
| + continue |
| + } |
| + pg.started = true |
| + p.mu.Unlock() |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + defer close(pg.done) |
| + defer func() { |
| + if v := recover(); v != nil { |
| + panicked.CompareAndSwap(nil, &v) |
| + pg.f = fetched{err: errMemoLoadPanicked} |
| + } |
| + }() |
| + f := p.readOne(ctx, k) |
| + p.mu.Lock() |
| + pg.f = f |
| + p.held += int64(len(f.body)) |
| + p.stats.Peak = max(p.stats.Peak, p.held) |
| + p.stats.Largest = max(p.stats.Largest, int64(len(f.body))) |
| + p.mu.Unlock() |
| + }() |
| + } |
| } |
| // readConcurrently is read(st), with the object reads read makes after its |
| -// listings made concurrently (prefetchStore). read must decide which keys |
| -// to Get from its listings alone, never from a body it read, and must take |
| -// dryBody as a body it accepts silently; a key it asks for in the real pass |
| -// that the dry pass did not name is read from st when asked. |
| -func readConcurrently[T any](ctx context.Context, st release.Store, dryBody []byte, read func(release.Store) T) T { |
| - p := &prefetchStore{Store: st, dryBody: dryBody, dry: true, |
| - lists: map[string]listed{}, mods: map[string]listedMod{}, held: map[string]fetched{}} |
| +// listings made concurrently (prefetchStore), no body read past limit(key) |
| +// and one byte (limit nil, or 0 for a key, is no limit). read must decide |
| +// which keys to Get from its listings alone, never from a body it read, |
| +// and must take dryBody as a body it accepts silently; a key it asks for |
| +// in the real pass that the dry pass did not name is read when asked. |
| +func readConcurrently[T any](ctx context.Context, st release.Store, dryBody []byte, limit func(key string) int64, read func(release.Store) T) T { |
| + out, _ := readConcurrentlyStats(ctx, st, dryBody, limit, read) |
| + return out |
| +} |
| + |
| +// readConcurrentlyStats is readConcurrently, and what it held. |
| +func readConcurrentlyStats[T any](ctx context.Context, st release.Store, dryBody []byte, limit func(key string) int64, read func(release.Store) T) (T, prefetchStats) { |
| + p := &prefetchStore{Store: st, dryBody: dryBody, limit: limit, dry: true, |
| + lists: map[string]listed{}, mods: map[string]listedMod{}, pending: map[string]*pendingGet{}, |
| + window: make(chan struct{}, readConcurrency)} |
| read(p) |
| p.mu.Lock() |
| p.dry = false |
| - keys := slices.Compact(slices.Sorted(slices.Values(p.asked))) |
| + var keys []string |
| + for _, k := range p.asked { |
| + if _, seen := p.pending[k]; !seen { |
| + p.pending[k] = &pendingGet{done: make(chan struct{})} |
| + keys = append(keys, k) |
| + } |
| + } |
| p.mu.Unlock() |
| - got := make([]fetched, len(keys)) |
| - forEachLimit(len(keys), readConcurrency, func(i int) { |
| - body, etag, err := st.Get(ctx, keys[i]) |
| - got[i] = fetched{body: body, etag: etag, err: err} |
| - }) |
| - p.mu.Lock() |
| - for i, k := range keys { |
| - p.held[k] = got[i] |
| + stop, fetchDone := make(chan struct{}), make(chan struct{}) |
| + var panicked atomic.Pointer[any] |
| + go p.fetchAhead(ctx, keys, stop, fetchDone, &panicked) |
| + out := func() T { |
| + defer func() { |
| + // Start nothing more, and wait for every fetch started, so |
| + // nothing this read began outlives it. |
| + close(stop) |
| + <-fetchDone |
| + }() |
| + return read(p) |
| + }() |
| + if v := panicked.Load(); v != nil { |
| + panic(*v) |
| } |
| + p.mu.Lock() |
| + stats := p.stats |
| p.mu.Unlock() |
| - return read(p) |
| + return out, stats |
| +} |
| + |
| +// boxReportCap is the largest box report status.LoadBoxesComplete accepts |
| +// (status's maxFleetBoxBodyBytes): a customer fleet's boxes write their own |
| +// reports, so a prefetch reads none of them past it and one byte. |
| +// TestBoxReportCapIsStatusCap holds the two equal. |
| +const boxReportCap = 1 << 20 |
| + |
| +// boxReportLimit limits every key a box-report load reads to boxReportCap. |
| +func boxReportLimit(string) int64 { return boxReportCap } |
| + |
| +// boxReportKeyLimit limits only the box reports among a reader's keys, for |
| +// a reader that also reads other objects (the edge-address fold). |
| +func boxReportKeyLimit(key string) int64 { |
| + if strings.HasPrefix(key, "status/boxes/") { |
| + return boxReportCap |
| + } |
| + return 0 |
| } |
| // loadBoxesConcurrently is status.LoadBoxesComplete on st with the box |
| @@ -197,7 +361,7 @@ func loadBoxesConcurrently(ctx context.Context, st release.Store) ([]status.Box, |
| complete bool |
| err error |
| } |
| - o := readConcurrently(ctx, st, []byte("{}"), func(s release.Store) out { |
| + o := readConcurrently(ctx, st, []byte("{}"), boxReportLimit, func(s release.Store) out { |
| b, c, err := status.LoadBoxesComplete(ctx, s) |
| return out{b, c, err} |
| }) |
| @@ -213,7 +377,7 @@ func readEdgesSnapshotConcurrently(ctx context.Context, st release.Store) (edges |
| snap edgesSnapshot |
| err error |
| } |
| - o := readConcurrently(ctx, st, []byte(`{"v":1,"box":"-"}`), func(s release.Store) out { |
| + o := readConcurrently(ctx, st, []byte(`{"v":1,"box":"-"}`), nil, func(s release.Store) out { |
| snap, err := readEdgesSnapshot(ctx, s) |
| return out{snap, err} |
| }) |
diff --git a/internal/console/hotreads_test.go b/internal/console/hotreads_test.go| index 590121081..b5edfc621 100644 |
| --- a/internal/console/hotreads_test.go |
| +++ b/internal/console/hotreads_test.go |
| @@ -234,3 +234,128 @@ func TestForEachLimitRaisesAPanicOnTheCaller(t *testing.T) { |
| t.Errorf("%d tasks ran, want task 7 and those before it at least", ran.Load()) |
| } |
| } |
| + |
| +// boxReportOfSize is a box report body of exactly n bytes that decodes. |
| +func boxReportOfSize(t *testing.T, label string, n int) []byte { |
| + t.Helper() |
| + head, tail := `{"label":"`+label+`","name":"`, `"}` |
| + pad := n - len(head) - len(tail) |
| + if pad < 0 { |
| + t.Fatalf("a report of %d bytes is too small to build", n) |
| + } |
| + return []byte(head + strings.Repeat("x", pad) + tail) |
| +} |
| + |
| +// TestBoxReportCapIsStatusCap: the prefetch's cut (boxReportCap) is the |
| +// size status.LoadBoxesComplete refuses past, so cutting a body there and |
| +// one byte over never changes what the reader decides. |
| +func TestBoxReportCapIsStatusCap(t *testing.T) { |
| + perfCaptureLogs(t) |
| + fx, _ := newPerfFixture(t) |
| + ctx := context.Background() |
| + for _, c := range []struct { |
| + size int |
| + complete bool |
| + }{{boxReportCap, true}, {boxReportCap + 1, false}} { |
| + prefix := fmt.Sprintf("fleets/cap-%d/", c.size) |
| + putBoxReport(t, fx, prefix+"status/boxes/b.json", boxReportOfSize(t, "b", c.size)) |
| + st := release.NewPrefixStore(fx.srv.cfg.Releases, prefix) |
| + boxes, complete, err := status.LoadBoxesComplete(ctx, st) |
| + if err != nil || complete != c.complete || (len(boxes) == 1) != c.complete { |
| + t.Errorf("a %d-byte report: %d boxes, complete %v, err %v; want accepted %v (status's cap moved: move boxReportCap with it)", |
| + c.size, len(boxes), complete, err, c.complete) |
| + } |
| + got, gotComplete, _ := loadBoxesConcurrently(ctx, st) |
| + if !reflect.DeepEqual(got, boxes) || gotComplete != complete { |
| + t.Errorf("a %d-byte report: the concurrent load answered %d boxes, complete %v; status %d, %v", c.size, len(got), gotComplete, len(boxes), complete) |
| + } |
| + } |
| +} |
| + |
| +// TestBoxLoadHoldsBoundedBytes: a fleet whose boxes write many oversized |
| +// reports (a customer fleet writes its own prefix) costs the concurrent |
| +// load at most readConcurrency bodies cut at the cap and one byte, never |
| +// the reports themselves; the answer is status's, incomplete. |
| +func TestBoxLoadHoldsBoundedBytes(t *testing.T) { |
| + perfCaptureLogs(t) |
| + fx, lat := newPerfFixture(t) |
| + ctx := context.Background() |
| + const prefix, huge, many = "fleets/flood/", 2 * boxReportCap, 3 * readConcurrency |
| + for i := range many { |
| + putBoxReport(t, fx, fmt.Sprintf("%sstatus/boxes/huge-%02d.json", prefix, i), boxReportOfSize(t, "huge", huge)) |
| + } |
| + putBoxReport(t, fx, prefix+"status/boxes/ok.json", boxReportOfSize(t, "ok", 200)) |
| + st := release.NewPrefixStore(fx.srv.cfg.Releases, prefix) |
| + s0, _ := lat.Calls() |
| + want, wantComplete, wantErr := status.LoadBoxesComplete(ctx, st) |
| + s1, _ := lat.Calls() |
| + type out struct { |
| + boxes []status.Box |
| + complete bool |
| + err error |
| + } |
| + got, held := readConcurrentlyStats(ctx, st, []byte("{}"), boxReportLimit, func(s release.Store) out { |
| + b, c, err := status.LoadBoxesComplete(ctx, s) |
| + return out{b, c, err} |
| + }) |
| + s2, _ := lat.Calls() |
| + if wantErr != nil || got.err != nil || wantComplete || got.complete || len(want) != 1 || !reflect.DeepEqual(got.boxes, want) { |
| + t.Errorf("concurrent = %d boxes, complete %v, err %v; status = %d boxes, complete %v, err %v; want the one small report, incomplete", |
| + len(got.boxes), got.complete, got.err, len(want), wantComplete, wantErr) |
| + } |
| + if bound := int64(readConcurrency) * (boxReportCap + 1); held.Peak > bound { |
| + t.Errorf("the load held %d bytes at once, over %d (%d bodies cut at the cap and one byte)", held.Peak, bound, readConcurrency) |
| + } |
| + if held.Largest > boxReportCap+1 { |
| + t.Errorf("the load held a %d-byte body: an oversized report past the cap and one byte", held.Largest) |
| + } |
| + if s2-s1 != s1-s0 { |
| + t.Errorf("concurrent load made %d S3 requests, status %d", s2-s1, s1-s0) |
| + } |
| +} |
| + |
| +// TestPageBoxReportsTakesNoMemoReadFromBeforeAnInvalidation: a request |
| +// whose own read of the box reports finished before an invalidation (a |
| +// record deletion) does not hand that read to the page views' cache: the |
| +// cache loads afresh and sees what changed. Without an invalidation the |
| +// request's read is held and nothing is read again. |
| +func TestPageBoxReportsTakesNoMemoReadFromBeforeAnInvalidation(t *testing.T) { |
| + perfCaptureLogs(t) |
| + fx, lat := newPerfFixture(t) |
| + s := fx.srv |
| + labels := func(boxes []status.Box) []string { |
| + var out []string |
| + for _, b := range boxes { |
| + out = append(out, b.Label) |
| + } |
| + return out |
| + } |
| + |
| + ctx := withReqMemo(context.Background()) |
| + s.boxReports.invalidate() |
| + if _, _, err := s.loadBoxes(ctx, s.cfg.Releases, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + s0, _ := lat.Calls() |
| + s.pageBoxReports(ctx) |
| + if s1, _ := lat.Calls(); s1 != s0 { |
| + t.Errorf("no invalidation: the page views' cache read %d more requests, want the request's own read held", s1-s0) |
| + } |
| + |
| + ctx = withReqMemo(context.Background()) |
| + s.boxReports.invalidate() |
| + if _, _, err := s.loadBoxes(ctx, s.cfg.Releases, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := status.WriteBox(context.Background(), fx.rel, status.Box{GeneratedAt: time.Now(), Label: "box-new", Region: "eu-west-1"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + s.boxReports.invalidate() |
| + boxes, _ := s.pageBoxReports(ctx) |
| + if !slices.Contains(labels(boxes), "box-new") { |
| + t.Errorf("after an invalidation the page views' cache holds %v, want a fresh read with box-new", labels(boxes)) |
| + } |
| + if again, _ := s.pageBoxReports(context.Background()); !slices.Contains(labels(again), "box-new") { |
| + t.Errorf("the cache then holds %v, want box-new", labels(again)) |
| + } |
| +} |
diff --git a/internal/console/perfhotroutes_test.go b/internal/console/perfhotroutes_test.go| index 1691d9a16..733e78136 100644 |
| --- a/internal/console/perfhotroutes_test.go |
| +++ b/internal/console/perfhotroutes_test.go |
| @@ -10,6 +10,7 @@ import ( |
| "net/http" |
| "slices" |
| "strings" |
| + "sync/atomic" |
| "testing" |
| "time" |
| @@ -265,3 +266,59 @@ func TestPerfHotRoutesReadEachObjectOnce(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// TestBootstrapFallsBackToStreamingWhenTheConditionalReadFails: once the |
| +// bootstrap binary's sum is kept, a conditional read that fails (here the |
| +// bucket refuses it) is followed by a plain streaming read, and the status |
| +// still answers for the binary; the kept sum is never the answer to a |
| +// failed read. |
| +func TestBootstrapFallsBackToStreamingWhenTheConditionalReadFails(t *testing.T) { |
| + perfCaptureLogs(t) |
| + fx, lat := newPerfFixture(t) |
| + token := cliToken(t, fx, "paul@keymail.dev", "code-bootstrap-fallback") |
| + refuse := atomic.Bool{} |
| + rec := recordS3(lat, func(q s3Req) bool { return refuse.Load() && q.Key == bootstrapKey && q.IfNoneMatch != "" }) |
| + state := func() any { |
| + t.Helper() |
| + st, out := getJSON(t, fx.ts.URL+"/api/cli/system/status", token) |
| + if st != http.StatusOK { |
| + t.Fatalf("status = %d %v", st, out) |
| + } |
| + row, _ := out["bootstrap"].(map[string]any) |
| + return row["state"] |
| + } |
| + bootstrapReads := func() (plain, conditional int) { |
| + for _, q := range rec.all() { |
| + if q.Key == bootstrapKey && q.Method == http.MethodGet { |
| + if q.IfNoneMatch != "" { |
| + conditional++ |
| + } else { |
| + plain++ |
| + } |
| + } |
| + } |
| + return plain, conditional |
| + } |
| + if got := state(); got != "current" { |
| + t.Fatalf("first read: bootstrap %v, want current", got) |
| + } |
| + if p, c := bootstrapReads(); p != 1 || c != 0 { |
| + t.Fatalf("first read: %d plain and %d conditional reads of the binary, want one plain", p, c) |
| + } |
| + rec.reset() |
| + refuse.Store(true) |
| + if got := state(); got != "current" { |
| + t.Errorf("conditional read refused: bootstrap %v, want current from a streaming read", got) |
| + } |
| + if p, c := bootstrapReads(); p != 1 || c != 1 { |
| + t.Errorf("conditional read refused: %d plain and %d conditional reads, want the refused conditional one and one plain", p, c) |
| + } |
| + rec.reset() |
| + refuse.Store(false) |
| + if got := state(); got != "current" { |
| + t.Errorf("after the fallback: bootstrap %v, want current", got) |
| + } |
| + if p, c := bootstrapReads(); p != 0 || c != 1 { |
| + t.Errorf("after the fallback: %d plain and %d conditional reads, want one conditional", p, c) |
| + } |
| +} |
diff --git a/internal/console/remoteinstances.go b/internal/console/remoteinstances.go| index 957515dde..75b548883 100644 |
| --- a/internal/console/remoteinstances.go |
| +++ b/internal/console/remoteinstances.go |
| @@ -193,14 +193,16 @@ func (s *Server) pageBoxReports(ctx context.Context) ([]status.Box, bool) { |
| // A read of the platform's boxes this request has already finished |
| // (the instance list's rollout join, loadBoxes) is the read this |
| // load would make, so it is held instead, timed from when it |
| - // finished. Otherwise the load is the cache's own. |
| + // finished, but only when it began at this generation: a read that |
| + // began before an invalidation is what the invalidation discards. |
| + // Otherwise the load is the cache's own. |
| var ( |
| boxes []status.Box |
| complete bool |
| err error |
| at time.Time |
| ) |
| - if l, ok := memoFinished[loadedBoxes](ctx, boxesMemoKey("")); ok { |
| + if l, ok := memoFinished[loadedBoxes](ctx, boxesMemoKey("")); ok && l.gen == gen { |
| boxes, complete, at = l.boxes[:len(l.boxes):len(l.boxes)], l.complete, c.clock().Add(-time.Since(l.at)) |
| } else { |
| loadCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), instanceStoreTimeout) |
diff --git a/internal/console/reqmemo.go b/internal/console/reqmemo.go| index 8cbf9236a..842e9f4f3 100644 |
| --- a/internal/console/reqmemo.go |
| +++ b/internal/console/reqmemo.go |
| @@ -112,12 +112,15 @@ func withReqMemoOnViews(h http.HandlerFunc) http.HandlerFunc { |
| } |
| } |
| -// loadedBoxes is one memoized read of a prefix's box reports, and when it |
| -// finished. |
| +// loadedBoxes is one memoized read of a prefix's box reports, when it |
| +// finished, and the page views' box-report cache generation (boxReports) |
| +// when it began: pageBoxReports holds the read only if no invalidation |
| +// came between. |
| type loadedBoxes struct { |
| boxes []status.Box |
| complete bool |
| at time.Time |
| + gen uint64 |
| } |
| // boxesMemoKey is loadBoxes' memo key for prefix. |
| @@ -136,8 +139,9 @@ func (s *Server) loadBoxes(ctx context.Context, st release.Store, prefix string) |
| if prefix != "" { |
| store = release.NewPrefixStore(st, prefix) |
| } |
| + gen := s.boxReports.generation() |
| b, c, err := loadBoxesConcurrently(ctx, store) |
| - return loadedBoxes{b, c, time.Now()}, err |
| + return loadedBoxes{b, c, time.Now(), gen}, err |
| }) |
| return l.boxes[:len(l.boxes):len(l.boxes)], l.complete, err |
| } |
diff --git a/internal/console/server.go b/internal/console/server.go| index 2a95f8ac3..1e287daba 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1542,7 +1542,7 @@ func (s *Server) realFoldEdgeIPs(ctx context.Context) []string { |
| // accepts for either without logging. |
| now := time.Now() |
| dry := []byte(`{"v":1,"box":"-","label":"-","generated_at":"` + now.UTC().Format(time.RFC3339) + `"}`) |
| - ips := readConcurrently(ctx, s.cfg.Releases, dry, func(st release.Store) []string { |
| + ips := readConcurrently(ctx, s.cfg.Releases, dry, boxReportKeyLimit, func(st release.Store) []string { |
| return edgesfile.EdgeIPs(ctx, st, nil, now) |
| }) |
| slices.Sort(ips) |