agent: the window's drift poll remembers which values are magpie's per stamp
/api/drift runs every agent's Drift every 15s while the window is up. Each Drift asked every field's whole option list whether a value is one of magpie's, and the model lists under them are built again once a minute. With real accounts on mote (acgui6, w74), that was 27 MB of the 50 MB magpie allocated in three minutes with the window open. DriftShown, which only /api/drift calls, keeps magpieValueIn's answer per agent, field, value and settings while provider.StampNow stays the same, 5 minutes at most. Drift is unchanged for Reapply, rewiring, a launch and /api/state, which act on the answer. A provider resting or a model retired by its date is something the stamp can't see. The window's drift row now shows that within 5 minutes, not 1. Run: TestDriftShownAsksAValueOncePerStamp and the Drift tests on mica. Broken by hand (the memo never hit): red, "five polls built the options 5 times, want 1". Suite on mica at6c88d61f0+ this commit: gofmt clean; go vet internal/agent for darwin, linux and windows, internal/gui on darwin; go test ./internal/agent ok (194.9s); ./internal/gui red only in TestCoreScriptBudget (1269382 > 1268961). That test is red on origin/main6c88d61f0as well, and #6518 is taken. This change adds no script.
This commit is contained in:
@@ -173,6 +173,22 @@ var started = time.Now()
|
||||
// A field moved from one magpie model to another (the agent's own picker)
|
||||
// isn't drift; one moved off magpie is.
|
||||
func (a *Agent) Drift() *Drift {
|
||||
return a.drift(nil)
|
||||
}
|
||||
|
||||
// DriftShown is Drift for the window's poll, which only shows it: whether
|
||||
// a value is one of magpie's models is remembered per value while
|
||||
// provider.StampNow stays the same, shownValuesFor at most. Each poll
|
||||
// asked every field's whole option list for it, built again once a
|
||||
// minute: 27 MB of the 50 MB a magpie with real accounts allocated in
|
||||
// three minutes with the window up. What the stamp can't see (a provider
|
||||
// resting, a model retired by its date) shows within shownValuesFor;
|
||||
// Reapply, rewiring and a launch ask Drift, which reads them now.
|
||||
func (a *Agent) DriftShown() *Drift {
|
||||
return a.drift(&shownValues)
|
||||
}
|
||||
|
||||
func (a *Agent) drift(memo *valueMemo) *Drift {
|
||||
if a.Native != nil {
|
||||
s := a.Native.Read()
|
||||
if s.Provider == "invalid" {
|
||||
@@ -185,11 +201,17 @@ func (a *Agent) Drift() *Drift {
|
||||
}
|
||||
vals := a.Values()
|
||||
opts := fieldOptions{}
|
||||
valueIn := func(a *Agent, f Field, v string, vals map[string]string, opts fieldOptions) bool {
|
||||
if memo == nil {
|
||||
return magpieValueIn(a, f, v, vals, opts)
|
||||
}
|
||||
return memo.in(a, f, v, vals, opts)
|
||||
}
|
||||
// the field on one of magpie's models: where drift shows, and what
|
||||
// setting it again sets
|
||||
on, onMagpie := a.Fields[0], false
|
||||
for _, f := range a.Fields {
|
||||
if magpieValueIn(a, f, vals[f.Key], vals, opts) {
|
||||
if valueIn(a, f, vals[f.Key], vals, opts) {
|
||||
on, onMagpie = f, true
|
||||
break
|
||||
}
|
||||
@@ -223,7 +245,7 @@ func (a *Agent) Drift() *Drift {
|
||||
if now == "" && f.Follows != "" {
|
||||
now = vals[f.Follows]
|
||||
}
|
||||
if !ok || picked || now == want || !magpieValueIn(a, f, want, vals, opts) || magpieValueIn(a, f, now, vals, opts) || sameGroup(want, now) {
|
||||
if !ok || picked || now == want || !valueIn(a, f, want, vals, opts) || valueIn(a, f, now, vals, opts) || sameGroup(want, now) {
|
||||
continue
|
||||
}
|
||||
return &Drift{Kind: "replaced", Field: f.Key, Now: vals[f.Key], Want: want,
|
||||
@@ -327,6 +349,59 @@ func (o fieldOptions) of(f Field, vals map[string]string) []Option {
|
||||
return opts
|
||||
}
|
||||
|
||||
// valueMemo remembers magpieValueIn's answers (DriftShown).
|
||||
type valueMemo struct {
|
||||
sync.Mutex
|
||||
stamp provider.Stamp
|
||||
at time.Time
|
||||
m map[string]bool
|
||||
}
|
||||
|
||||
var shownValues valueMemo
|
||||
|
||||
// shownValuesFor is how long DriftShown keeps an answer while
|
||||
// provider.StampNow says nothing changed.
|
||||
const shownValuesFor = 5 * time.Minute
|
||||
|
||||
func (m *valueMemo) in(a *Agent, f Field, v string, vals map[string]string, opts fieldOptions) bool {
|
||||
if v == "" {
|
||||
return false
|
||||
}
|
||||
var k strings.Builder
|
||||
k.WriteString(a.ID + "\x00" + f.Key + "\x00" + v)
|
||||
keys := make([]string, 0, len(vals))
|
||||
for kk := range vals {
|
||||
keys = append(keys, kk)
|
||||
}
|
||||
slices.Sort(keys)
|
||||
for _, kk := range keys {
|
||||
k.WriteString("\x00" + kk + "=" + vals[kk])
|
||||
}
|
||||
key := k.String()
|
||||
stamp := provider.StampNow()
|
||||
m.Lock()
|
||||
if m.stamp != stamp || time.Since(m.at) >= shownValuesFor || len(m.m) > 512 {
|
||||
m.stamp, m.at, m.m = stamp, time.Now(), nil
|
||||
}
|
||||
yes, ok := m.m[key]
|
||||
m.Unlock()
|
||||
if ok {
|
||||
return yes
|
||||
}
|
||||
// the stamp is read before the answer: a change while it is moves it
|
||||
// past the one kept, and the next call asks again
|
||||
yes = magpieValueIn(a, f, v, vals, opts)
|
||||
m.Lock()
|
||||
if m.stamp == stamp {
|
||||
if m.m == nil {
|
||||
m.m = map[string]bool{}
|
||||
}
|
||||
m.m[key] = yes
|
||||
}
|
||||
m.Unlock()
|
||||
return yes
|
||||
}
|
||||
|
||||
// magpieValueIn is magpieValue reading a field's options from opts, made
|
||||
// for the same vals; nil reads them anew.
|
||||
func magpieValueIn(a *Agent, f Field, v string, vals map[string]string, opts fieldOptions) bool {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// The window's drift poll asks whether a value is one of magpie's models
|
||||
// once while provider.StampNow stays the same, not on every poll: each ask
|
||||
// built the field's whole option list, the agents' model lists under it
|
||||
// built again once a minute. A stamp that moved asks again, and so does
|
||||
// Drift itself, which Reapply and rewiring act on.
|
||||
func TestDriftShownAsksAValueOncePerStamp(t *testing.T) {
|
||||
asked := 0
|
||||
f := Field{Key: "model", Options: func(map[string]string) []Option {
|
||||
asked++
|
||||
return []Option{{Value: "own-model", Ref: "fake/m1"}}
|
||||
}}
|
||||
a := &Agent{ID: "drift-shown-test"}
|
||||
vals := map[string]string{"model": "own-model"}
|
||||
var memo valueMemo
|
||||
for range 5 {
|
||||
if !memo.in(a, f, "own-model", vals, fieldOptions{}) {
|
||||
t.Fatal("own-model is magpie's")
|
||||
}
|
||||
}
|
||||
if asked != 1 {
|
||||
t.Fatalf("five polls built the options %d times, want 1", asked)
|
||||
}
|
||||
// another value of the agent's settings is another question
|
||||
if !memo.in(a, f, "own-model", map[string]string{"model": "own-model", "effort": "high"}, fieldOptions{}) || asked != 2 {
|
||||
t.Fatalf("other settings: asked %d times, want 2", asked)
|
||||
}
|
||||
provider.Changed()
|
||||
if !memo.in(a, f, "own-model", vals, fieldOptions{}) || asked != 3 {
|
||||
t.Fatalf("after a change: asked %d times, want 3", asked)
|
||||
}
|
||||
for range 3 {
|
||||
magpieValueIn(a, f, "own-model", vals, fieldOptions{})
|
||||
}
|
||||
if asked != 6 {
|
||||
t.Fatalf("Drift's own asks: %d, want every one (6)", asked)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1363,7 +1363,7 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
|
||||
mux.HandleFunc("GET /api/drift", func(rw http.ResponseWriter, r *http.Request) {
|
||||
out := map[string]*agent.Drift{}
|
||||
for _, a := range agent.DetectedIn(heldAgents()) {
|
||||
if d := a.Drift(); d != nil {
|
||||
if d := a.DriftShown(); d != nil {
|
||||
out[a.ID] = d
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user