filememo: a parse unread for five minutes is let go
filememo kept every parse for the life of the process. What start up reads once, RenameLegacy's look at every agent's config (a 1 MB YAML look, a 600 KB JSON), stayed in a tray-only magpie's live heap: ~2 MB of its 12.8 MB with real accounts. A read sweeps when idleFor (5 minutes) has gone by since the last sweep, dropping the parses not read since. What a poll reads (the tray's every 3 minutes, the window's every 15 seconds) stays; a parse goes 5 to 10 minutes after its last read, and the next read parses it again. Swept on a read, not a timer, since a timer armed inside a test's synctest bubble is the bubble's. Run: go test ./internal/filememo on mote, ok. Broken by hand: never deleting turns TestUnreadParseLetGo red.
This commit is contained in:
@@ -30,6 +30,8 @@ type entry struct {
|
||||
// (Hold), and in which of Forget's generations
|
||||
checked time.Time
|
||||
gen uint64
|
||||
// touched says it was read since sweepIdle last looked
|
||||
touched bool
|
||||
}
|
||||
|
||||
// memoKey is a parse's place in seen: kind and path apart, not joined
|
||||
@@ -62,8 +64,37 @@ var (
|
||||
missing = map[string]missingFile{}
|
||||
// stats counts the looks at the disk, for tests
|
||||
stats int
|
||||
// sweptAt is when sweepIdle last ran
|
||||
sweptAt time.Time
|
||||
)
|
||||
|
||||
// idleFor is how long a parse is kept unread, at least, before it is let
|
||||
// go: a read idleFor after the last sweep drops what wasn't read since,
|
||||
// so a parse goes between idleFor and twice that after its last read
|
||||
// (later, while nothing reads at all). What is read once, at start up
|
||||
// (RenameLegacy's look at every agent's config, ~2 MB of a tray-only
|
||||
// magpie's 12.8 MB live heap), was kept for the life of the process;
|
||||
// what a poll reads stays. Swept on a read, not a timer: a timer armed
|
||||
// inside a test's synctest bubble is the bubble's (see endWaits).
|
||||
const idleFor = 5 * time.Minute
|
||||
|
||||
// sweepIdle lets go of the parses not read since it last ran, when that
|
||||
// was idleFor ago; mu is held.
|
||||
func sweepIdle(now time.Time) {
|
||||
if now.Sub(sweptAt) < idleFor {
|
||||
return
|
||||
}
|
||||
sweptAt = now
|
||||
for k, e := range seen {
|
||||
if !e.touched {
|
||||
delete(seen, k)
|
||||
continue
|
||||
}
|
||||
e.touched = false
|
||||
seen[k] = e
|
||||
}
|
||||
}
|
||||
|
||||
type missingFile struct {
|
||||
err error
|
||||
checked time.Time
|
||||
@@ -184,7 +215,12 @@ func read[T any](kind, path string, parse func([]byte) (T, error), held bool) (T
|
||||
var zero T
|
||||
key := memoKey{kind, path}
|
||||
mu.Lock()
|
||||
sweepIdle(time.Now())
|
||||
e, ok := seen[key]
|
||||
if ok && !e.touched {
|
||||
e.touched = true
|
||||
seen[key] = e
|
||||
}
|
||||
if ok && held && holds > 0 && e.raw == nil && e.gen == gen && time.Since(e.checked) < heldFor {
|
||||
mu.Unlock()
|
||||
return e.v.(T), nil
|
||||
@@ -277,7 +313,7 @@ func parsed[T any](key memoKey, fi os.FileInfo, b []byte, parse func([]byte) (T,
|
||||
return zero, err
|
||||
}
|
||||
mu.Lock()
|
||||
e := entry{mod: fi.ModTime(), size: fi.Size(), v: v}
|
||||
e := entry{mod: fi.ModTime(), size: fi.Size(), v: v, touched: true}
|
||||
if time.Since(e.mod) <= settled {
|
||||
e.raw = b
|
||||
} else if holds > 0 && g == gen {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package filememo
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A parse not read for a while is let go, so what start up read once
|
||||
// (every agent's config) isn't kept for the life of the process; one a
|
||||
// poll keeps reading stays.
|
||||
func TestUnreadParseLetGo(t *testing.T) {
|
||||
mu.Lock()
|
||||
was := sweptAt
|
||||
mu.Unlock()
|
||||
t.Cleanup(func() { mu.Lock(); sweptAt = was; mu.Unlock() })
|
||||
dir := t.TempDir()
|
||||
once, polled := filepath.Join(dir, "once.json"), filepath.Join(dir, "polled.json")
|
||||
for _, p := range []string{once, polled} {
|
||||
if err := os.WriteFile(p, []byte("{}"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Hour)
|
||||
if err := os.Chtimes(p, old, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
parses := map[string]int{}
|
||||
read := func(p string) {
|
||||
t.Helper()
|
||||
if _, err := Read("idle", p, func(b []byte) (int, error) { parses[p]++; return len(b), nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
kept := func(p string) bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
_, ok := seen[memoKey{"idle", p}]
|
||||
return ok
|
||||
}
|
||||
sweep := func(at time.Time) {
|
||||
mu.Lock()
|
||||
sweepIdle(at)
|
||||
mu.Unlock()
|
||||
}
|
||||
read(once)
|
||||
read(polled)
|
||||
now := time.Now()
|
||||
sweep(now.Add(idleFor)) // both were read since the last sweep
|
||||
if !kept(once) || !kept(polled) {
|
||||
t.Fatal("let go of a parse read since the last sweep")
|
||||
}
|
||||
read(polled)
|
||||
sweep(now.Add(time.Minute)) // sooner than idleFor after the last: nothing
|
||||
sweep(now.Add(2 * idleFor))
|
||||
if kept(once) {
|
||||
t.Fatal("a parse unread for idleFor is still kept")
|
||||
}
|
||||
if !kept(polled) {
|
||||
t.Fatal("let go of the parse a poll keeps reading")
|
||||
}
|
||||
read(once)
|
||||
if parses[once] != 2 || parses[polled] != 1 {
|
||||
t.Fatalf("parses %v, want once's again and polled's not", parses)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user