Files
magpie/internal/provider/group_find_shared_test.go
T
yetone 8f72f95304 provider: a group's lookup copies the group found, not every group
FindGroup, FindGroupAlso and GroupFinder built every routing group with
groupsIn, which copies each group and its rules, to return one. The
window's 15-second /api/drift poll asks GroupFinder for each Pi model on
a group (piModelJSON), and the gateway asks once per request on a group.
They now read the groups held (sharedGroupsIn, heldFrom while Hold is
active) and copy only the group found and each group a member comes
through (cloneGroup), so what a caller gets is still its own to change.

Measured on mote with the real-account copy, GUI page open in Playwright
Chromium, network blocked, 2 minutes (acgui.sh):
- piModelJSON allocations: 10.6 MB -> 1.5 MB (v0.1.3420 vs this change)
- process CPU 0.39 s vs 0.43 s, footprint 187 vs 197 MB: within the run
  noise of the once-a-minute catalog rebuilds; the drop is in the profile.

Test: TestGroupFoundCopiesItselfAlone changes a found group, its member's
Via and EffortMap, and checks FindGroup, GroupFinder and Groups still see
the saved ones; then 50 looks must allocate less than 50 copies of every
group (100 groups with rules).
Broken by hand, each red:
- A: sharedGroupsIn -> groupsIn: looks 5384832 B >= copies 4796000 B
- B: no cloneGroup of the found group: index out of range (shared slice)
- C: no clone of a member's sub-group: "a change to a found group
  reached the next look"

Run (mote): go test ./internal/provider ./internal/agent ok;
go test ./internal/gateway -timeout 45m ok; go vet native, GOOS=linux,
GOOS=windows ok; gofmt -l clean.
Not tried: a gateway request through a group with a real account
(the path is the same FindGroup the test covers).
2026-11-09 23:08:13 +08:00

89 lines
2.9 KiB
Go

package provider
import (
"fmt"
"runtime"
"testing"
)
// totalAlloc is the bytes allocated by f, run n times.
func totalAlloc(n int, f func()) uint64 {
var a, b runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&a)
for range n {
f()
}
runtime.ReadMemStats(&b)
return b.TotalAlloc - a.TotalAlloc
}
// A routing group's lookup copied every group and its rules for the one it
// wanted: 4 MB of the window's 15-second drift poll (each Pi model on a
// group asked GroupFinder), and once per request on a group. While a
// request holds the catalog it reads the groups held and copies the one
// found, and the groups its members come through, alone: what a caller
// is given is still its own to change.
func TestGroupFoundCopiesItselfAlone(t *testing.T) {
groupHeldFixture(t)
if err := SaveGroup(Group{ID: "outer", Name: "Outer", Members: []string{"group/g4", "p5/m2"}, Routing: Ordered,
EffortMaps: map[string]map[string]string{"p5/m2": {"high": "low"}}}); err != nil {
t.Fatal(err)
}
// a user's many groups, each with rules: what every look copied
for i := range 100 {
if err := SaveGroup(Group{ID: fmt.Sprintf("x%d", i), Name: fmt.Sprintf("X%d", i), Routing: Ordered,
Members: []string{"p1/m3", "p2/m3", "p3/m3", "p4/m3"},
Rules: []Rule{{Use: "p2/m3", Tokens: 10, Agents: []string{"codex"}}}}); err != nil {
t.Fatal(err)
}
}
Changed()
release := Hold()
defer release()
g, ms, ok := FindGroup("group/outer")
if !ok || len(ms) < 2 || len(ms[0].Via) == 0 {
t.Fatalf("%v %d %+v", ok, len(ms), ms)
}
// every part a caller may change is its own
g.Members[0] = "changed"
ms[0].Via[0].Members[0] = "changed"
for _, m := range ms {
for k := range m.EffortMap {
m.EffortMap[k] = "changed"
}
}
find := GroupFinder()
for _, look := range []func(string) (Group, []Member, bool){FindGroup, find} {
g2, ms2, _ := look("group/outer")
if g2.Members[0] != "group/g4" || ms2[0].Via[0].Members[0] != "p0/m1" {
t.Fatalf("a change to a found group reached the next look: %v %v", g2.Members, ms2[0].Via[0].Members)
}
for _, m := range ms2 {
if m.EffortMap != nil && m.EffortMap["high"] != "low" {
t.Fatalf("a change to a member's effort map reached the next look: %v", m.EffortMap)
}
}
}
for _, x := range Groups() {
if x.ID == "g4" && x.Members[0] != "p0/m1" || x.ID == "outer" && x.Members[0] != "group/g4" {
t.Fatalf("a change to a found group reached Groups: %s %v", x.ID, x.Members)
}
}
// and a look costs less than one copy of every group
all := sharedGroupsIn(providerEntries())
if len(all) < 100 {
t.Fatalf("the fixture has %d groups; the test needs the found ones too", len(all))
}
const n = 50
copies := totalAlloc(n, func() { _ = cloneGroups(all) })
looks := totalAlloc(n, func() {
if _, _, ok := GroupFinder()("group/g4"); !ok {
t.Fatal("no group g4")
}
})
if looks >= copies {
t.Fatalf("%d looks allocated %d bytes, as much as %d copies of every group (%d)", n, looks, n, copies)
}
}