diff --git a/internal/provider/rename.go b/internal/provider/rename.go index f7ed28a5c..d3451caf9 100644 --- a/internal/provider/rename.go +++ b/internal/provider/rename.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "os" + "reflect" "slices" "strings" "sync/atomic" @@ -269,6 +270,16 @@ type Renames struct { merged map[string][]string } +// Same is whether o is r itself, the one read UsageRenames hands back +// while providers.json is unchanged: the same maps, not equal ones. A +// reader that keeps what it made from r asks it to know r is unchanged. +func (r Renames) Same(o Renames) bool { + at := func(m any) uintptr { return reflect.ValueOf(m).Pointer() } + return at(r.now) == at(o.now) && at(r.gave) == at(o.gave) && at(r.hosts) == at(o.hosts) && + at(r.sites) == at(o.sites) && at(r.gone) == at(o.gone) && at(r.into) == at(o.into) && + at(r.merged) == at(o.merged) +} + type gaveTo struct { Until time.Time `json:"until"` To string `json:"to"` diff --git a/internal/usage/probes.go b/internal/usage/probes.go index 230abde11..9dd30c08f 100644 --- a/internal/usage/probes.go +++ b/internal/usage/probes.go @@ -1,6 +1,8 @@ package usage import ( + "maps" + "slices" "strings" "sync" "time" @@ -169,6 +171,39 @@ func clipProbeError(s string) string { // way: CyberCaius again, Leee's model list asked at leee.baby before it // was saved on another host of that site. func probeChunk(probes []*rowChunk, since time.Time, renames provider.Renames, hosts map[string]string) *rowChunk { + // the page polls every few seconds and the probes rarely change: the + // chunk was made again each time, ~45 MB of two and a half minutes' + // allocations with the page open (#2278). A frozen block is never + // written again, so the same blocks, since, renames and hosts make + // the same chunk, which no reader writes. + probeMemo.Lock() + m := probeMemo.last + probeMemo.Unlock() + if m != nil && m.since.Equal(since) && slices.Equal(m.probes, probes) && m.renames.Same(renames) && maps.Equal(m.hosts, hosts) { + return m.c + } + c := makeProbeChunk(probes, since, renames, hosts) + probeMemo.Lock() + probeMemo.last = &probeMade{probes: slices.Clone(probes), since: since, renames: renames, hosts: maps.Clone(hosts), c: c} + probeMemo.Unlock() + return c +} + +// probeMemo is the last chunk probeChunk made, and what it was made from. +var probeMemo struct { + sync.Mutex + last *probeMade +} + +type probeMade struct { + probes []*rowChunk + since time.Time + renames provider.Renames + hosts map[string]string + c *rowChunk +} + +func makeProbeChunk(probes []*rowChunk, since time.Time, renames provider.Renames, hosts map[string]string) *rowChunk { var c *rowChunk sites := sync.OnceValue(func() map[string]string { return sitesOf(hosts) }) eachProbe(probes, since, func(r Record, off int64) { diff --git a/internal/usage/probes_test.go b/internal/usage/probes_test.go index 68112d02e..0b21158b4 100644 --- a/internal/usage/probes_test.go +++ b/internal/usage/probes_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/yetone/magpie/internal/probelog" + "github.com/yetone/magpie/internal/provider" ) // ledgerSpy is an Observer that only counts the rows Append hands it. @@ -131,3 +132,34 @@ func TestAnsweredModelListIsNoRow(t *testing.T) { t.Fatalf("failed filter: total %d", p.Total) } } + +// The request page asks for the probes on every poll; while none was +// made and no host or rename changed, the chunk made last time is the +// answer, and any of those changing makes it again (#2278). +func TestProbeChunkMadeOncePerChange(t *testing.T) { + at := time.Date(2026, 8, 1, 9, 0, 0, 0, time.UTC) + rows := probeRows{} + for i := range 3 { + rows.add(Record{Time: at.Add(time.Duration(i) * time.Minute), Agent: "magpie", Provider: "p", Host: "api.example.com", Status: 500, Source: SourceProbe}, int64(i)) + } + blocks := rows.done() + hosts := map[string]string{"api.example.com": "p"} + first := probeChunk(blocks, at, provider.Renames{}, hosts) + if first == nil || first.Count != 3 { + t.Fatalf("first: %+v", first) + } + if again := probeChunk(slices.Clone(blocks), at, provider.Renames{}, map[string]string{"api.example.com": "p"}); again != first { + t.Fatal("the same probes, since, renames and hosts made the chunk again") + } + if c := probeChunk(blocks, at.Add(time.Minute), provider.Renames{}, hosts); c == first || c.Count != 2 { + t.Fatalf("a later since kept the old chunk: %+v", c) + } + if c := probeChunk(blocks, at, provider.Renames{}, map[string]string{"api.example.com": "q"}); c == first { + t.Fatal("a changed host kept the old chunk") + } + more := probeRows{blocks: slices.Clone(blocks)} + more.add(Record{Time: at.Add(5 * time.Minute), Agent: "magpie", Provider: "p", Host: "api.example.com", Status: 500, Source: SourceProbe}, 3) + if c := probeChunk(more.done(), at, provider.Renames{}, hosts); c == first || c.Count != 4 { + t.Fatalf("a new probe kept the old chunk: %+v", c) + } +}