diff --git a/internal/usage/cold_split_test.go b/internal/usage/cold_split_test.go index 495dc23cc..965fe3581 100644 --- a/internal/usage/cold_split_test.go +++ b/internal/usage/cold_split_test.go @@ -106,6 +106,55 @@ func TestFirstBuildPairsSpans(t *testing.T) { } } +// A first start pairs the history once for the warm-up's page and +// Codex's effort, whichever is asked first, and tells what each pairing +// of its own does (#2278). +func TestFirstBuildPairsOnceForPageAndEffort(t *testing.T) { + yesterday, _ := advanceHistory(t) + writeLongCodexThread(t, yesterday.AddDate(0, 0, -20), yesterday.AddDate(0, 0, -1)) + coldSpan = 4 + restart := func() { + requestCache.Lock() + requestCache.parts, requestCache.pageOld, requestCache.heatParts, requestCache.heatOld, requestCache.heats, requestCache.pairs, requestCache.pages = nil, nil, nil, nil, nil, nil, map[pageKey]RequestPage{} + requestCache.Unlock() + codexEffort.Lock() + codexEffort.periods = map[Period]*effortEntry{} + codexEffort.Unlock() + codexEffortBuilding.Lock() + effortOlds = map[Period]*effortOld{} + codexEffortBuilding.Unlock() + for _, path := range []string{pageDiskPath(), heatDiskPath(), effortDiskPath()} { + os.Remove(path) + } + } + restart() + t.Cleanup(restart) + spans := 0 + coldSpanning = func(int) { spans++ } + t.Cleanup(func() { coldSpanning = nil }) + page := QueryPage(All, Filter{}, 0, 50) + restart() + effort := CodexModelEffort(All) + restart() + spans = 0 + if got := QueryPage(All, Filter{}, 0, 50); !reflect.DeepEqual(got, page) { + t.Fatalf("page first: %+v, alone %+v", got, page) + } + first := spans + if got := CodexModelEffort(All); !reflect.DeepEqual(got, effort) || spans != first || first == 0 { + t.Fatalf("page first: effort %+v after %d spans of %d, alone %+v", got, spans, first, effort) + } + restart() + spans = 0 + if got := CodexModelEffort(All); !reflect.DeepEqual(got, effort) { + t.Fatalf("effort first: %+v, alone %+v", got, effort) + } + first = spans + if got := QueryPage(All, Filter{}, 0, 50); !reflect.DeepEqual(got, page) || spans != first || first == 0 { + t.Fatalf("effort first: page %+v after %d spans of %d, alone %+v", got, spans, first, page) + } +} + // writeLongCodexThread writes one Codex rollout with a call at each of // ats, and the gateway's row of each. func writeLongCodexThread(t *testing.T, ats ...time.Time) { diff --git a/internal/usage/effort_split.go b/internal/usage/effort_split.go index 8564c0103..c32bc17e7 100644 --- a/internal/usage/effort_split.go +++ b/internal/usage/effort_split.go @@ -144,7 +144,8 @@ func effortSplit(sums effortSums, p Period, now time.Time, gateways, chunks, oth if !named { return false } - key = fmt.Sprintf("%x", sha256.Sum256([]byte("effort-v1\x00"+key))) + settledKey := key + key = effortKeyOf(Filter{}, settledKey) switch { case same(old) && old.key == key: fresh := *old @@ -155,7 +156,7 @@ func effortSplit(sums effortSums, p Period, now time.Time, gateways, chunks, oth old, kept = &effortOld{root: disk.root, cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, sums: s}, true } else if s, found := effortAdvance(disk, cut, since, until, identities, sets, key); found { old, kept = &effortOld{root: disk.root, cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, sums: s}, true - } else if s, found := effortCold(cut, since, identities, sets, key); found { + } else if s, found := effortCold(cut, since, until, identities, sets, key, newCellPass(p, Filter{}, now, true), settledKey); found { old, kept = &effortOld{root: disk.root, cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, sums: s}, true } } @@ -191,16 +192,31 @@ func effortSplit(sums effortSums, p Period, now time.Time, gateways, chunks, oth } // effortCold is the effort from since to cut, of a pairing a few days -// at a time (coldSpans), written under key; or false. -func effortCold(cut, since time.Time, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key string) (map[string][]sessions.ModelEffort, bool) { +// at a time (coldSpans), written under key; or false. The cells before +// cut of the period's unfiltered page, read through pass, are counted of +// the same pairing and kept too, when the file doesn't have them: the +// warm-up's page of All paired the whole history again after (pageCold). +func effortCold(cut, since, until time.Time, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key string, pass *cellPass, settledKey string) (map[string][]sessions.ModelEffort, bool) { part := effortSums{} + pageKey := pageKeyOf(until, pass, settledKey) + var cells *chunkPart + if _, _, found := pageDiskRead(pageKey); !found { + cells = &chunkPart{} + } if !coldSpans(since, cut, sets, func(from, to time.Time, span [3][]*rowChunk) { - pairRowsIn(from, to, span[0], span[1], span[2], identities).visitActors(part.add) + ps := pairRowsIn(from, to, span[0], span[1], span[2], identities) + ps.visitActors(part.add) + if cells != nil { + cells = addCells(cells, ps.cellsBefore(pass, to)) + } }) { return nil, false } sums := part.values() _ = effortDiskWrite(key, cut, since, sums) + if cells != nil { + _ = pageDiskWrite(pageKey, cut, since, cells) + } return sums, true } diff --git a/internal/usage/page_split.go b/internal/usage/page_split.go index bf3810587..770b653e0 100644 --- a/internal/usage/page_split.go +++ b/internal/usage/page_split.go @@ -84,7 +84,7 @@ func pageSplit(p Period, f Filter, now time.Time, take int, pass *cellPass, gate old, kept = &pageOld{cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, cells: c}, true } else if c, found := pageAdvance(cache.disk, cut, since, until, f, pass, identities, sets, key); found { old, kept = &pageOld{cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, cells: c}, true - } else if c, found := pageCold(cut, since, pass, identities, sets, key); found { + } else if c, found := pageCold(cut, since, pass, identities, sets, key, effortKeyOf(f, k)); found { old, kept = &pageOld{cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, cells: c}, true } } @@ -175,18 +175,46 @@ func pageAdvance(d heatDiskRef, cut, since, until time.Time, f Filter, pass *cel } // pageCold is a page's cells before cut from since, of a pairing a few -// days at a time (coldSpans), written under key; or false. -func pageCold(cut, since time.Time, pass *cellPass, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key string) (*chunkPart, bool) { +// days at a time (coldSpans), written under key; or false. With +// effortKey, Codex's effort before cut is counted of the same pairing and +// kept under it, when the file doesn't have it: a first start paired the +// whole history for the warm-up's page and again for the effort of the +// same period, ~65 and ~105 MB of its live heap one after the other, and +// seconds of CPU each (#2278). +func pageCold(cut, since time.Time, pass *cellPass, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key, effortKey string) (*chunkPart, bool) { cells := &chunkPart{} + var effort effortSums + if effortKey != "" { + if _, found := effortDiskRead(effortKey); !found { + effort = effortSums{} + } + } if !coldSpans(since, cut, sets, func(from, to time.Time, span [3][]*rowChunk) { - cells = addCells(cells, pairRowsIn(from, to, span[0], span[1], span[2], identities).cellsBefore(pass, to)) + ps := pairRowsIn(from, to, span[0], span[1], span[2], identities) + cells = addCells(cells, ps.cellsBefore(pass, to)) + if effort != nil { + ps.visitActors(effort.add) + } }) { return nil, false } _ = pageDiskWrite(key, cut, since, cells) + if effort != nil { + _ = effortDiskWrite(effortKey, cut, since, effort.values()) + } return cells, true } +// effortKeyOf is the key Codex's effort before the cut is kept under, of +// the settled chunks heatSettledKey names k for f; "" when f filters +// anything, as the effort is of every row. +func effortKeyOf(f Filter, k string) string { + if f != (Filter{}) { + return "" + } + return fmt.Sprintf("%x", sha256.Sum256([]byte("effort-v1\x00"+k))) +} + // addCells is the cells of a and of b, the later, as one part: a cell of // the same names, bits and bucket added up, in the order of their last. func addCells(a, b *chunkPart) *chunkPart {