From f6a96b2f0b132fd1870624c0d9b2ca9b65ac1f8a Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Oct 2026 20:58:16 +0800 Subject: [PATCH] usage: a change time with a fraction settles in 100ms, so a read after an append hashes no log (#3394) readLogSnapshot checks its snapshot against a SHA-256 of the log's whole prefix whenever the log's change time is under a second old. Under steady traffic every read came within a second of the gateway's last append, so each one hashed the whole usage.jsonl again: 1.8 times the log for each append and read. A stamp with a nonzero fraction of a second comes from a clock that ticks in milliseconds (APFS in ns, Linux's coarse clock 1-10 ms, NTFS ~15.6 ms), so it settles in logFineSettle = 100ms. A stamp in whole seconds (HFS+, FAT, a share) keeps the one-second settle. Run: - go test -count=1 -timeout 45m ./internal/usage/ ./internal/budget/: ok. - TestUsageReadAfterAppendHashesNothingOnAFineClock (20000-row log, 5 appends, each followed by two reads): fine stamp 0 bytes hashed; whole-second stamp 50.7 MB. Broken by hand (logFineSettle = time.Second): 54 MB hashed, red. - Real account, sandbox HOME, a deepseek-only providers.json and a 98 MB usage.jsonl (the owner's own log, repeated 50 times). magpie serve took 30 real /v1/responses calls with a Codex user agent to deepseek/deepseek-flash, all answered, while /v1/magpie/usage?period=today (the Usage page's read) was polled every second. Process CPU time, two runs each: before 3.11 s and 3.05 s, after 1.95 s and 2.25 s. The same calls with /v1/magpie/quotas polled cost 0.2 s on both binaries, since that path never reads the log. - gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage/ clean. Not tried: an HFS+ or FAT volume (the test skips when the file system gives whole-second stamps, and covers that case by truncating the stamp); the full suite at this head runs after the push. --- internal/usage/log_hash_cost_test.go | 59 ++++++++++++++++++++++++++++ internal/usage/log_index.go | 18 ++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/internal/usage/log_hash_cost_test.go b/internal/usage/log_hash_cost_test.go index a85e4db5b..7327d0a9e 100644 --- a/internal/usage/log_hash_cost_test.go +++ b/internal/usage/log_hash_cost_test.go @@ -98,3 +98,62 @@ func TestUsagePartialLineThenCompletion(t *testing.T) { }) } } + +// Under steady traffic the gateway appends a call less than a second before +// most reads. A change time with a fraction of a second settles in +// logFineSettle, so a read that long after an append continues the +// snapshot from the appends it noted, hashing nothing again; one in whole +// seconds still waits out logStampSettle (#3394). +func TestUsageReadAfterAppendHashesNothingOnAFineClock(t *testing.T) { + for _, tc := range []struct { + name string + fine bool + hashes bool + }{{"fine stamp", true, false}, {"whole-second stamp", false, true}} { + t.Run(tc.name, func(t *testing.T) { + pageHome(t) + holdClock(t, time.Now()) + historyLog(t, 20000) + changeTime, now := logChangeTime, logIndexNow + t.Cleanup(func() { logChangeTime, logIndexNow = changeTime, now }) + if !tc.fine { + logChangeTime = func(info os.FileInfo) time.Time { return changeTime(info).Truncate(time.Second) } + } + // each read comes 150 ms after the append before it + logIndexNow = func() time.Time { return time.Now().Add(150 * time.Millisecond) } + 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}) + if c := logChangeTime(mustStat(t)); tc.fine && c.Nanosecond() == 0 { + t.Skip("this file system keeps change times in whole seconds") + } + Summarize(Today) + Summarize(Today) + } + if s := Summarize(Today); s.Calls != 1+cycles { + t.Fatalf("calls today: %d", s.Calls) + } + t.Logf("%d bytes hashed over %d appends", hashed, cycles) + if tc.hashes != (hashed > 0) { + t.Fatalf("hashed %d bytes over %d appends and reads 150 ms after each", hashed, cycles) + } + }) + } +} + +func mustStat(t *testing.T) os.FileInfo { + t.Helper() + info, err := statLogFile(Path()) + if err != nil { + t.Fatal(err) + } + return info +} diff --git a/internal/usage/log_index.go b/internal/usage/log_index.go index 5782c3689..e6b5022e0 100644 --- a/internal/usage/log_index.go +++ b/internal/usage/log_index.go @@ -21,6 +21,10 @@ import ( const ( logBlockRows = 512 logStampSettle = time.Second + // logFineSettle is logStampSettle for a change time with a fraction of + // a second, from a clock that ticks in milliseconds: APFS's in + // nanoseconds, Linux's coarse clock in 1 to 10 ms, NTFS's in ~15.6 ms. + logFineSettle = 100 * time.Millisecond ) // The blocks are always kept, however long the history: they are the index @@ -292,9 +296,21 @@ func oldInfo(s *logSnapshot) os.FileInfo { // A read in the change stamp's clock tick cannot prove that a later equal // stamp still names the same bytes. Decide before reading, not at reuse time. +// +// A stamp in whole seconds (HFS+, FAT, a share that keeps no fraction) may +// share its second with a write after the read. One with a fraction comes +// from a clock that ticks in milliseconds, past which a write has a later +// stamp, so it settles in logFineSettle. Under steady traffic each read +// came within a second of the gateway's last append, so each one hashed +// the whole log again: 1.8 times the log for each append and read, so a +// 100 MB history cost ~180 MB of SHA-256 for every request (#3394). func settledLogStamp(info os.FileInfo, start time.Time) bool { c := logChangeTime(info) - return !c.IsZero() && c.Before(start.Add(-logStampSettle)) + settle := logStampSettle + if c.Nanosecond() != 0 { + settle = logFineSettle + } + return !c.IsZero() && c.Before(start.Add(-settle)) } func sameLogInfo(a, b os.FileInfo) bool {