diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go index 16dc05327..648d88000 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "fmt" + "os" + "path/filepath" "slices" "strings" "sync" @@ -407,6 +409,109 @@ func TestAPassWritesWantsWhileAKeepingIsBlocked(t *testing.T) { t.Errorf("assigner_keep_now: %v, asked %v", err, asked) } }) + // Design 47 H2 H-A-635: a pass still running after pass-stall-seconds says in the journal which phase it is in, and again at each + // further bound until it ends; one that ends within the bound says none, and its end line stays as it was. The bound is the setting + // read from its file (the unit a millisecond), so a row that blocks a phase waits only for its bounds. + t.Run("a pass past pass-stall-seconds says its phase", func(t *testing.T) { + was := passStallUnit + passStallUnit = time.Millisecond + t.Cleanup(func() { passStallUnit = was }) + file := filepath.Join(t.TempDir(), "pass-stall-seconds") + if err := os.WriteFile(file, []byte("20"), 0o600); err != nil { + t.Fatal(err) + } + stall, err := passStallSetting(func(string) string { return file }) + if none, noneErr := passStallSetting(func(string) string { return "" }); err != nil || stall != 20*time.Millisecond || noneErr != nil || none != 0 || + (&passer{}).passStallOf() != time.Minute || (&passer{passStall: stall}).passStallOf() != stall { + t.Fatalf("the setting reads %v (%v), none set %v (%v), the default %v; want 20 units, none set the default of a minute", stall, err, none, noneErr, (&passer{}).passStallOf()) + } + for _, row := range []struct { + name, phase string + block func(p *passer, gate chan struct{}) // blocks the phase until the gate closes + }{ + {"the kept graph", phaseKeptGraph, func(p *passer, gate chan struct{}) { + p.graph = &graphKeeper{objects: &blockedObjects{memObjects: memObjects{}}, background: func(f func()) { f() }, wake: func() {}, + halves: func() map[string]mcc.Half { <-gate; return nil }} + }}, + {"the node wants", phaseNodeWants, func(p *passer, gate chan struct{}) { + p.kvs[buckets.Wants()] = &gatedKV{memKV: &memKV{values: map[string][]byte{}}, gate: gate} + }}, + } { + t.Run(row.name, func(t *testing.T) { + var mu sync.Mutex + var journal []string + stalled := make(chan string, 64) + p := newPasser(kvSet{buckets.Wants(): &memKV{values: map[string][]byte{}}, buckets.Asks(): &memKV{values: map[string][]byte{}}}, time.Now()) + p.passStall = stall + p.logf = func(f string, a ...any) { + line := fmt.Sprintf(f, a...) + mu.Lock() + journal = append(journal, line) + mu.Unlock() + if strings.Contains(line, "the pass started at ") { + stalled <- line + } + } + placeOn(t, p.w, "ace", "web") + gate := make(chan struct{}) + row.block(p, gate) + done := make(chan struct{}) + go func() { p.pass(context.Background()); close(done) }() + var earlier time.Duration + for range 2 { // the first bound and the next + select { + case line := <-stalled: + _, after, _ := strings.Cut(line, " is in "+row.phase+" for ") + spent, err := time.ParseDuration(after) + if after == "" || err != nil || spent < earlier { + t.Fatalf("the journal says %q, want the pass in %q for a time that grows", line, row.phase) + } + earlier = spent + case <-time.After(bound): + t.Fatalf("a pass blocked in %s says nothing within %v", row.phase, bound) + } + } + select { + case <-done: + t.Fatal("the pass ended while its phase was blocked") + default: + } + close(gate) + select { + case <-done: + case <-time.After(bound): + t.Fatal("the pass does not end when its phase is opened") + } + mu.Lock() + defer mu.Unlock() + if last := journal[len(journal)-1]; !strings.HasPrefix(last, "the pass took ") { + t.Errorf("the journal ends %q, want the end line of the pass as before", last) + } + }) + } + // A pass that ends within the bound says none: the bound is armed once, disarmed at the end, and a bound that still fires + // after the end (as a late timer) says nothing. + var armed []func() + var stopped int + var journal []string + p := newPasser(kvSet{buckets.Wants(): &memKV{values: map[string][]byte{}}, buckets.Asks(): &memKV{values: map[string][]byte{}}}, time.Now()) + p.logf = func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) } + p.stallAfter = func(d time.Duration, f func()) func() { + if d != time.Minute { + t.Errorf("the bound armed is %v, want the default of a minute", d) + } + armed = append(armed, f) + return func() { stopped++ } + } + p.pass(context.Background()) + if len(armed) != 1 || stopped != 1 { + t.Fatalf("the bound was armed %d times and disarmed %d times, want once each", len(armed), stopped) + } + armed[0]() + if len(armed) != 1 || len(journal) != 1 || !strings.HasPrefix(journal[0], "the pass took ") { + t.Errorf("a pass that ended within the bound: journal %q, armed %d; want only its end line", journal, len(armed)) + } + }) } // failingObjects refuses the next fail puts, then is objects. @@ -422,3 +527,14 @@ func (f *failingObjects) Put(ctx context.Context, name string, data []byte) erro } return f.blockedObjects.Put(ctx, name, data) } + +// gatedKV is a bucket whose put waits until the gate closes. +type gatedKV struct { + *memKV + gate chan struct{} +} + +func (g *gatedKV) Put(ctx context.Context, key string, value []byte) (uint64, error) { + <-g.gate + return g.memKV.Put(ctx, key, value) +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go index a99dfb49f..afcd59c68 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go @@ -599,6 +599,10 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, return err } p.tierBound, p.tierWake = tierBound, wake + // The setting pass-stall-seconds: a pass still running after it says which phase it is in (design 47 H2 H-A-635). + if p.passStall, err = passStallSetting(getenv); err != nil { + return err + } // The wait on an outside cause (outside.go, design 47 G §706): the keeper's three events say when to judge again, and the // seat mesh-wants' wait-held and wait-ended are said; registered before the hub starts. if bus != nil && kvs[buckets.Waits()] != nil { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go index 2e12cbb6d..d32129583 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -121,6 +121,11 @@ type passer struct { tierBound time.Duration tierWake func() tierTimer *time.Timer + // passStall is the setting pass-stall-seconds (passstall.go; zero: its default): a pass still running after it says which phase + // it is in (design 47 H2 H-A-635). + passStall time.Duration + // stallAfter arms a bound of the pass's watch in the place of a timer, returning what disarms it; nil: a timer (a test's seam). + stallAfter func(time.Duration, func()) func() } // wantsSay says one event of the seat mesh-wants; with no bus to say it on, nothing is said. @@ -151,16 +156,28 @@ func (p *passer) clock() time.Time { func (p *passer) pass(ctx context.Context) { start := time.Now() - defer func() { p.logf("the pass took %s", time.Since(start).Round(time.Millisecond)) }() // design 47 H-A-259 + watch := p.watchPass(start) // a pass past pass-stall-seconds says which phase it is in (design 47 H2 H-A-635) + defer func() { + watch.end() + p.logf("the pass took %s", time.Since(start).Round(time.Millisecond)) // design 47 H-A-259 + }() + watch.in(phaseAssignmentsSeat) p.assignmentsSeat(ctx) + watch.in(phaseKeptGraph) if held := p.keepGraph(ctx); held == "" { + watch.in(phaseWants) p.wants(ctx) + watch.in(phasePassSaid) p.sayPass(ctx, "judged") } else { + watch.in(phasePassSaid) p.sayPass(ctx, held) // a full store keeps the last revision (design 47 G §181); none kept yet writes no want (H-A-261) } + watch.in(phasePuts) p.puts(ctx) + watch.in(phaseNodeWants) p.nodeWants(ctx) + watch.in(phaseBackups) p.backups(ctx, p.reverts(ctx)) } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/passstall.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/passstall.go new file mode 100644 index 000000000..1bc5d3a3d --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/passstall.go @@ -0,0 +1,98 @@ +package main + +import ( + "sync" + "time" +) + +// The phases of a pass, each named as it starts so a pass that stands still can say where (design 47 H2 H-A-635). +const ( + phaseAssignmentsSeat = "the assignments seat" + phaseKeptGraph = "the kept graph" + phaseWants = "the wants" + phasePassSaid = "the pass said" + phasePuts = "the puts" + phaseNodeWants = "the node wants" + phaseBackups = "the backups and reverts" +) + +// defaultPassStall is the setting pass-stall-seconds' default, 60 seconds (design 47 H2 H-A-635). +const defaultPassStall = 60 * time.Second + +// passStallUnit is the unit of the setting pass-stall-seconds, a second; the whole test sets a shorter one, so a pass passes its +// bound without a test waiting seconds (as tierBoundUnit). +var passStallUnit = time.Second + +// passStallSetting is the setting pass-stall-seconds, read at the start from the file the manifest declares with the content +// ${config:pass-stall-seconds}; zero, its default, for a file missing or empty (design 47 H2 H-A-635, principle 11). +func passStallSetting(getenv func(string) string) (time.Duration, error) { + seconds, err := settingFile(getenv("MESH_ASSIGNER_PASS_STALL_FILE")) + return time.Duration(seconds) * passStallUnit, err +} + +// passStallOf is the setting pass-stall-seconds; zero or less is its default. +func (p *passer) passStallOf() time.Duration { + if p.passStall > 0 { + return p.passStall + } + return defaultPassStall +} + +// passWatch is what one running pass keeps of where it stands, in the passer's own memory: its start, the phase it is in and +// since when. A pass still running at the bound says in the journal which phase it is in, and again at each further bound until +// it ends; nothing is said for a pass that ends within the bound, and the end line stays as it was (design 47 H2 H-A-635 on +// H-A-259; principle 3, a part that waits says where; principle 7, a journal line, no new key or event). +type passWatch struct { + logf func(string, ...any) + after func(time.Duration, func()) func() + bound time.Duration + start time.Time + + mu sync.Mutex + phase string + since time.Time + stop func() + ended bool +} + +// watchPass starts the watch of the pass that began at start; stallAfter, when set, arms the bound in the place of a timer. +func (p *passer) watchPass(start time.Time) *passWatch { + w := &passWatch{logf: p.logf, after: p.stallAfter, bound: p.passStallOf(), start: start, since: start} + if w.after == nil { + w.after = func(d time.Duration, f func()) func() { + t := time.AfterFunc(d, f) + return func() { t.Stop() } + } + } + w.mu.Lock() + defer w.mu.Unlock() + w.stop = w.after(w.bound, w.say) + return w +} + +// in says the pass is in phase now. +func (w *passWatch) in(phase string) { + w.mu.Lock() + defer w.mu.Unlock() + w.phase, w.since = phase, time.Now() +} + +// say is one bound passed with the pass still running: the journal line, and the next bound armed. The line is said under the +// watch's lock, so none is said after the pass's end has returned. +func (w *passWatch) say() { + w.mu.Lock() + defer w.mu.Unlock() + if w.ended { + return + } + w.stop = w.after(w.bound, w.say) + w.logf("the pass started at %s is in %s for %s", w.start.Format(time.RFC3339), w.phase, time.Since(w.since).Round(time.Millisecond)) +} + +// end is the pass's end: the bound is disarmed and no further line is said. +func (w *passWatch) end() { + w.mu.Lock() + defer w.mu.Unlock() + w.ended = true + w.stop() +} diff --git a/modules/mesh-delivery-assigner/manifest_test.go b/modules/mesh-delivery-assigner/manifest_test.go index 4d6b406f2..27672e376 100644 --- a/modules/mesh-delivery-assigner/manifest_test.go +++ b/modules/mesh-delivery-assigner/manifest_test.go @@ -185,11 +185,13 @@ func TestManifest(t *testing.T) { } file, _ := os.ReadFile("module.json") if err := json.Unmarshal(file, &raw); err != nil || raw.Config["outside-wait-minutes"].Default != float64(30) || raw.Config["outside-settle-seconds"].Default != float64(90) || - raw.Config["ask-operator-for"].Default != "take-off,remove-node,stops" || len(raw.Config) != 16 || raw.Config["tier-bound"].Default != float64(30) || raw.Config["entry-asks"].Default != float64(5) || + raw.Config["ask-operator-for"].Default != "take-off,remove-node,stops" || len(raw.Config) != 17 || raw.Config["tier-bound"].Default != float64(30) || raw.Config["entry-asks"].Default != float64(5) || raw.Config["composition-runs-cap-mib"].Default != float64(16) || raw.Config["composition-runs-kept"].Default != "24h" || raw.Config["composition-step-bound"].Default != "30m" || raw.Config["run-untaken-bound"].Default != "24h" || raw.Config["blame-tries"].Default != float64(20) || // design 47 H2 H-A-628 (3): the bound of a run's wait for a package's publish, the builder's default raw.Config["package-wait-minutes"].Default != float64(10) || + // design 47 H2 H-A-635: the bound a pass runs before it says which phase it is in + raw.Config["pass-stall-seconds"].Default != float64(60) || // design 47 H2 §775, H-A-381 (2): the taken key's bound and the kept-call pace, runtime settings raw.Config["ask-taken-bound"].Default != "10m" || raw.Config["kept-call-pace-first"].Default != "1s" || raw.Config["kept-call-pace-max"].Default != "30s" || raw.Config["compose-ask-bound"].Default != "10s" || raw.Config["keeping-retry"].Default != "30s" { t.Errorf("config = %+v (%v), want outside-wait-minutes of 30, outside-settle-seconds of 90 and ask-operator-for of take-off,remove-node,stops (A-425a)", raw.Config, err) @@ -205,6 +207,7 @@ func TestManifest(t *testing.T) { {"MESH_ASSIGNER_RUN_UNTAKEN_BOUND_FILE", "${dir:state}/run-untaken-bound", "run-untaken-bound"}, {"MESH_ASSIGNER_BLAME_TRIES_FILE", "${dir:state}/blame-tries", "blame-tries"}, {"MESH_ASSIGNER_PACKAGE_WAIT_FILE", "${dir:state}/package-wait-minutes", "package-wait-minutes"}, // design 47 H2 H-A-628 (3) + {"MESH_ASSIGNER_PASS_STALL_FILE", "${dir:state}/pass-stall-seconds", "pass-stall-seconds"}, // design 47 H2 H-A-635 {"MESH_ASSIGNER_TIER_BOUND_FILE", "${dir:state}/tier-bound", "tier-bound"}, // design 47 H §751 {"MESH_ASSIGNER_ENTRY_ASKS_FILE", "${dir:state}/entry-asks", "entry-asks"}, // design 47 H H-A-60 } { diff --git a/modules/mesh-delivery-assigner/module.json b/modules/mesh-delivery-assigner/module.json index d2266001d..ec9a7447d 100644 --- a/modules/mesh-delivery-assigner/module.json +++ b/modules/mesh-delivery-assigner/module.json @@ -239,6 +239,14 @@ "trusted": false, "default": 10 }, + "pass-stall-seconds": { + "type": "number", + "description": "seconds a pass of the assigner may run before it says in the journal which phase it is in, and again at each further bound until it ends (design 47 H2 H-A-635).", + "minimum": 10, + "maximum": 600, + "trusted": false, + "default": 60 + }, "blame-tries": { "type": "number", "description": "the most compositions a machine's composer makes to find the modules it blames, carried in the run's request as blame_tries (design 47 G section 721, A-466r).", @@ -386,6 +394,14 @@ "mode": "0600", "content": "${config:package-wait-minutes}" }, + { + "id": "pass-stall-seconds", + "type": "file", + "path": "${dir:state}/pass-stall-seconds", + "owner": "mesh-delivery-assigner", + "mode": "0600", + "content": "${config:pass-stall-seconds}" + }, { "id": "blame-tries", "type": "file", @@ -414,7 +430,8 @@ "MESH_ASSIGNER_COMPOSITION_STEP_BOUND_FILE": "${dir:state}/composition-step-bound", "MESH_ASSIGNER_RUN_UNTAKEN_BOUND_FILE": "${dir:state}/run-untaken-bound", "MESH_ASSIGNER_BLAME_TRIES_FILE": "${dir:state}/blame-tries", - "MESH_ASSIGNER_PACKAGE_WAIT_FILE": "${dir:state}/package-wait-minutes" + "MESH_ASSIGNER_PACKAGE_WAIT_FILE": "${dir:state}/package-wait-minutes", + "MESH_ASSIGNER_PASS_STALL_FILE": "${dir:state}/pass-stall-seconds" }, "user": "mesh-delivery-assigner", "health": {