usage: a log snapshot read inside its stamp's tick is checked once, when it settles (#4368)
Each request the gateway writes wakes several readers at once: the menu bar, the pages, the Sessions list. They all read inside the change stamp's tick (100ms on a fine clock, 1s on a whole-second one). The read that built the snapshot hashed the whole log after its parse, each later reader in the tick hashed it again, and the first read once it settled hashed it once more. That was four times the log for one request with three readers, the recordHash 3.08s in KaranocaVe's profile. Now a snapshot read inside the tick is not hashed after its parse, and a reader still inside the tick takes it as it is. The first read once the tick settles hashes it and makes it settled, as before, so nothing settled is made of an unchecked read. A snapshot continued from one that never settled still hashes its prefix first, since trusted needs the old one settled. The disk copy is checked against the file when it is loaded, as before. What changes: a same-size rewrite by another program inside the tick reaches readers when the tick settles, up to 100ms or 1s later, not within it. A read a moment earlier would have had the old bytes anyway. TestUsageSameTickRewriteIsNotCached's case with no append and no settle now settles before it checks; its other seven cases are as they were. Run: - TestUsageReadersOfOneAppendHashTheLogOnce (new): an append, three readers in the tick, one after it settles. 1.00x the log hashed per append, 4.00x on the old code (red there). - TestUsageAppendReadHashesNoPreReadScan: 0.80x per append + read, 1.80x before. - go test ./internal/usage: ok on mote (macOS 27.2, 44s) and in WSL magpie-desktop (Ubuntu 24.04, 32s). - GOOS=windows and GOOS=linux go vet ./internal/usage: ok. gofmt: clean. Not tried: the reporter's log size, and a filesystem with whole-second stamps (where the tick is 1s, the same path).
This commit is contained in:
@@ -95,6 +95,14 @@ func TestUsageSameTickRewriteIsNotCached(t *testing.T) {
|
||||
if appendAfter {
|
||||
Append(Record{Time: clock, Agent: "codex", Provider: "relay", Model: "m", Input: 19})
|
||||
}
|
||||
if !afterSettle && !appendAfter {
|
||||
// Inside the tick the read before the rewrite may
|
||||
// stand, as a read a moment earlier would have had
|
||||
// it; the first read once the tick settles checks
|
||||
// it (#4368).
|
||||
Summarize(All)
|
||||
afterSettle = true
|
||||
}
|
||||
if afterSettle {
|
||||
logIndexNow = func() time.Time { return tick.Add(time.Hour) }
|
||||
}
|
||||
|
||||
@@ -170,3 +170,53 @@ func mustStat(t *testing.T) os.FileInfo {
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
// Each append wakes several readers at once (the menu bar, the pages), all
|
||||
// inside the change stamp's tick. Each of them hashed the whole log again,
|
||||
// and so did the read that built the snapshot and the first read once it
|
||||
// settled: four times the log for one request (#4368). The read once it
|
||||
// settles is the one that checks it.
|
||||
func TestUsageReadersOfOneAppendHashTheLogOnce(t *testing.T) {
|
||||
pageHome(t)
|
||||
holdClock(t, time.Now())
|
||||
historyLog(t, 20000)
|
||||
now := logIndexNow
|
||||
t.Cleanup(func() { logIndexNow = now })
|
||||
later := func() time.Time { return time.Now().Add(2 * logStampSettle) }
|
||||
logIndexNow = later
|
||||
Summarize(Today)
|
||||
hash := logRecordHash
|
||||
var hashed int64
|
||||
logRecordHash = func(path string, n int64) string {
|
||||
hashed += n
|
||||
return hash(path, n)
|
||||
}
|
||||
t.Cleanup(func() { logRecordHash = hash })
|
||||
const cycles = 5
|
||||
for range cycles {
|
||||
Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 1, Status: 200})
|
||||
info, err := os.Stat(Path())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// the readers come within the tick, whatever the machine's speed
|
||||
logIndexNow = func() time.Time { return logChangeTime(info) }
|
||||
for range 3 {
|
||||
Summarize(Today)
|
||||
}
|
||||
logIndexNow = later
|
||||
Summarize(Today)
|
||||
}
|
||||
info, err := os.Stat(Path())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
perCycle := hashed / cycles
|
||||
t.Logf("log %d bytes: %d bytes hashed per append and four reads (%.2f× the log)", info.Size(), perCycle, float64(perCycle)/float64(info.Size()))
|
||||
if perCycle > info.Size()+info.Size()/10 {
|
||||
t.Fatalf("an append and its readers hashed %d bytes over a %d-byte log, more than the one check once it settles", perCycle, info.Size())
|
||||
}
|
||||
if s := Summarize(Today); s.Calls != 1+cycles {
|
||||
t.Fatalf("calls today: %d", s.Calls)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +111,13 @@ func readLogSnapshot() *logSnapshot {
|
||||
old := logIndex.snapshot
|
||||
unchanged := old != nil && old.path == path && (err != nil && old.info == nil || err == nil && sameLogInfo(old.info, info))
|
||||
if unchanged && info != nil {
|
||||
if logChangeStamp(info) == "" || !old.settled {
|
||||
if !old.settled && !settled && old.hash != "" && logChangeStamp(info) != "" {
|
||||
// Still in the stamp's tick: hashing the log again proves nothing
|
||||
// a read a moment earlier wouldn't have, and every reader an
|
||||
// append wakes (the menu bar, the pages) hashed the whole log
|
||||
// once more (#4368). The first read once it settles verifies it,
|
||||
// before anything is built on it, as below.
|
||||
} else if logChangeStamp(info) == "" || !old.settled {
|
||||
unchanged = old.hash != "" && logRecordHash(path, old.off) == old.hash
|
||||
if unchanged && settled {
|
||||
promoted := *old
|
||||
@@ -227,8 +233,13 @@ func readLogSnapshot() *logSnapshot {
|
||||
next.blocks = append(next.blocks, tail.freeze())
|
||||
}
|
||||
next.hash, next.digest = fmt.Sprintf("%x", sum.Sum(nil)), marshalLogHash(sum)
|
||||
// A rewrite during the read must not give old blocks the new content's hash.
|
||||
if (!trusted || !settled) && next.hash != logRecordHash(path, next.off) {
|
||||
// A rewrite during the read must not give old blocks the new content's
|
||||
// hash. A read inside the stamp's tick is checked when it settles
|
||||
// instead (no settled snapshot is made of it unchecked, and one
|
||||
// continued from it hashes its prefix first): checked now, it hashed
|
||||
// the whole log twice for each append, once now and again on
|
||||
// settling (#4368).
|
||||
if settled && !trusted && next.hash != logRecordHash(path, next.off) {
|
||||
next.hash, next.digest = "", nil
|
||||
next.settled = false
|
||||
next.full = logFull{}
|
||||
|
||||
Reference in New Issue
Block a user