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 {