diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go index bea4010f4..94bdb3d73 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -188,8 +188,10 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro clock := askClock(ctx, bounds, askBound, rv.Findings, cred.Module, time.Now) starter.Asks, hearer.Asks = clock, clock starter.Supersede = func(newer seat.Pipeline) { hearer.SupersedeRunning(newer) } - starter.Left = hearer.LeftInPlace // a same-source re-pin's superseded steps leave their runs (H-A-284) - starter.Revision = widener.Revision // a kept mover is placed from its kept revision (H-A-286) + starter.Drop = hearer.DropOpen // a placing's end drops the asks still open (H-A-506) + hearer.Aside = func(do func()) { go do() } // each drop of an ended pipeline's asks beside its end (H-A-506) + starter.Left = hearer.LeftInPlace // a same-source re-pin's superseded steps leave their runs (H-A-284) + starter.Revision = widener.Revision // a kept mover is placed from its kept revision (H-A-286) // A mover's wait on a package is read first: a package published before the wait places it at once (H-A-326, H-A-328). // It is a kept call within ask-unanswered-bound (design 47 H2 §775). starter.AskPublished = publishedOf(func(key string, body any) (json.RawMessage, error) { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go index afe5b284d..02f416a70 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go @@ -166,14 +166,9 @@ func (t testRunsKV) Has(key string) (bool, error) { return err == nil, err } -// askValue is an ask key's value: the key of the run the ask is of (H-A-500 addendum (1)). -type askValue struct { - Run string `json:"run"` -} - -// MarkAsk creates the ask key of ask holding run; one written already is kept. -func (t testRunsKV) MarkAsk(ask, run string) error { - raw, err := json.Marshal(askValue{Run: run}) +// MarkAsk creates the ask key of ask holding mark (H-A-506 addendum); one written already is kept. +func (t testRunsKV) MarkAsk(ask string, mark pipeline.AskMark) error { + raw, err := json.Marshal(mark) if err != nil { return err } @@ -183,20 +178,20 @@ func (t testRunsKV) MarkAsk(ask, run string) error { return nil } -// RunOfAsk reads the ask key of ask: the run it holds. -func (t testRunsKV) RunOfAsk(ask string) (string, bool, error) { +// AskOf reads the ask key of ask: the mark it holds, a key written before the H-A-506 addendum naming its run alone. +func (t testRunsKV) AskOf(ask string) (pipeline.AskMark, bool, error) { e, err := t.kv.Get(t.ctx, pipeline.AskKey(ask)) if errors.Is(err, jetstream.ErrKeyNotFound) { - return "", false, nil + return pipeline.AskMark{}, false, nil } if err != nil { - return "", false, err + return pipeline.AskMark{}, false, err } - var v askValue - if err := json.Unmarshal(e.Value(), &v); err != nil || v.Run == "" { - return "", false, fmt.Errorf("the ask key of ask %s holds no run: %v", ask, err) + var m pipeline.AskMark + if err := json.Unmarshal(e.Value(), &m); err != nil || (m.Run == "" && m.Pipeline == "") { + return pipeline.AskMark{}, false, fmt.Errorf("the ask key of ask %s names no pipeline and no run: %v", ask, err) } - return v.Run, true, nil + return m, true, nil } func (t testRunsKV) Keys() ([]string, error) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go index bf8faf926..c855dadf7 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go @@ -53,10 +53,19 @@ func TestAnAsksBoundCountsFromThePullAndEndsItsStep(t *testing.T) { } p.Steps[stepIndex(p, "lint", "app:program")].Outcome = seat.StepResultOutcomeFailed store.Update(pkey, p, rev) + old := p.Steps[stepIndex(p, "lint", "app:program")].Ask + _ = v.Hearer.Clock.MarkAsk(old, AskMark{Pipeline: pkey, Module: "app", Step: "lint"}) r, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "retry"}) if err != nil { t.Fatal(err) } + // The step asked again keeps one ask key, its new ask's: the earlier one is deleted (H-A-506 addendum). + if _, had, _ := v.Hearer.Clock.AskOf(old); had { + t.Errorf("the ask key of the earlier ask %s outlives the rerun", old) + } + if m, ok, _ := v.Hearer.Clock.AskOf(r.Result.Ask); !ok || m.Pipeline != pkey || m.Step != "lint" { + t.Errorf("the rerun's ask key = %+v, %v; want it naming %s's lint step", m, ok, pkey) + } ask, key := r.Result.Ask, AskBoundKey(nodebuildgo.Seat, r.Result.Ask) if want := []string{key + " 10m0s"}; !slices.Equal(armed, want) || len(asks.ids) != 1 { t.Fatalf("armed %v asking %v, want %v before one ask", armed, asks.ids, want) @@ -110,15 +119,17 @@ func TestAnAsksBoundCountsFromThePullAndEndsItsStep(t *testing.T) { if !slices.Equal(raised, []string{nodebuildgo.Seat + " " + ask}) || !slices.Contains(*said, "pipeline-ended.main.5") { t.Errorf("raised %v, said %v", raised, *said) } - // A bound whose step ended asks nothing; the step's end deletes the bound; a late ask-taken clears the finding. + // A bound whose step ended asks nothing; the step's end deletes the bound; a late ask-taken after the pipeline's + // end clears nothing: the end deleted the ask's key, so the finding stands as what happened (H-A-506 second + // addendum; H-A-238 (2) holds while the pipeline runs). if err := v.AskUnanswered(nodebuildgo.Seat, ask); err != nil || len(asks.ids) != 1 || len(dropped) != 1 { t.Errorf("a bound of an ended step asked %v, dropped %v (%v)", asks.ids, dropped, err) } if err := c.Ended(nodebuildgo.Seat, ask); err != nil || !slices.Equal(unbound, []string{key}) { t.Errorf("the step's end deleted %v (%v)", unbound, err) } - if _, err := v.Hearer.Taken(ask, "n2", now); err != nil || !slices.Equal(cleared, []string{nodebuildgo.Seat + " " + ask}) { - t.Errorf("a late ask-taken cleared %v (%v)", cleared, err) + if _, err := v.Hearer.Taken(ask, "n2", now); err != nil || len(cleared) != 0 { + t.Errorf("a late ask-taken after the pipeline's end cleared %v (%v), want nothing", cleared, err) } }) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/declined.go b/modules/mesh-delivery-pipeline/internal/pipeline/declined.go index 1cdd6b43e..384305bdf 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/declined.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/declined.go @@ -96,10 +96,10 @@ func (h *Hearer) putDeclined(key, old string, s seat.StepResult, a Asked, ev Ste } else if err != nil { return fmt.Errorf("the test run %s is not written: %w", a.Run, err) } - markAsk(h.Clock, a.ID, a.Run, h.logf) // H-A-500 addendum (1) break } } + unmarkAsk(h.Clock, old, h.logf) // one key per ask, gone once the ask is no longer open (H-A-506 addendum, corrected) h.logf("%s: the %s step of %s was declined on %s (%s): asked again as %s, not on %s (H-A-445)", key, s.Step, strings.TrimSpace(s.Module+" "+s.Artifact), ev.Node, ev.Said, a.ID, strings.Join(s.NotOn, ", ")) return h.PutAgain(key, a) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/drops.go b/modules/mesh-delivery-pipeline/internal/pipeline/drops.go new file mode 100644 index 000000000..ed81f85f1 --- /dev/null +++ b/modules/mesh-delivery-pipeline/internal/pipeline/drops.go @@ -0,0 +1,117 @@ +package pipeline + +import ( + "slices" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" +) + +// dropSeat says whether the holder of a seat serves the seat's drop of an ask the runner put on it: every seat whose +// holder says ask-taken, the three build seats, mesh-builds and mesh-wants (AskTakenSeats). +func dropSeat(name string) bool { + for _, s := range AskTakenSeats { + if s.Seat == name { + return true + } + } + return false +} + +// openAtEnd are copies of the steps of steps that an end of their pipeline takes while still asked (design 47 H2 +// H-A-506): an open step with an ask of its own, and a step reading a test run, open or waiting, which leaves it. Read +// before the end is written, since the end rewrites the steps. +func openAtEnd(steps []seat.StepResult) []seat.StepResult { + var out []seat.StepResult + for _, s := range steps { + asked := open(s) && s.Ask != "" + reads := sharedRun(s) != "" && s.Seat != wants.Seat && (open(s) || s.Outcome == seat.StepResultOutcomeWaiting) + if asked || reads { + out = append(out, s) + } + } + return out +} + +// DropOpen takes the steps of pipeline key that its end took while asked (openAtEnd) off the seats (design 47 H2 +// H-A-506 as corrected): a step reading a test run leaves it (leaveRun), the run's ask dropped only by its last reader's +// leave; every other own ask still open, a mesh-wants step's included, is dropped through its seat's drop, so its holder +// refuses it unrun and no build capacity goes to an ended pipeline. Each drop and each refusal is said in one journal line +// per ask; a drop that is not answered fails nothing. The leaves are written before it answers; the drops are asked beside +// the path that ended the pipeline (Aside), since a seat with no holder answers a kept call only at its bound, so a cancel +// is answered at once whatever its seats. +func (h *Hearer) DropOpen(key string, steps []seat.StepResult, why string) { + done := map[string]bool{} + var drops []seat.StepResult + for _, st := range steps { + if sharedRun(st) != "" && st.Seat != wants.Seat { + // A shared test run's ask is dropped only by its last reader's leave, never while another pipeline reads it + // (H-A-506 corrected). + h.leaveRun(key, st, why) + continue + } + if !open(st) || st.Ask == "" || done[st.Ask] || !dropSeat(st.Seat) { + continue + } + done[st.Ask] = true + if h.DropRun == nil { + h.logf("%s: the ask %s of %s's %s on %s is not dropped: the runner has no drop (H-A-506)", key, st.Ask, st.Module, st.Step, st.Seat) + continue + } + drops = append(drops, st) + } + for _, st := range drops { + h.aside(func() { + if err := h.DropRun(st.Seat, st.Node, st.Ask, why); err != nil { + h.logf("%s: the drop of the ask %s of %s's %s is refused or not answered by %s: %v (H-A-506)", key, st.Ask, st.Module, st.Step, st.Seat, err) + return + } + h.logf("%s: the ask %s of %s's %s on %s is dropped (H-A-506)", key, st.Ask, st.Module, st.Step, st.Seat) + }) + } +} + +// aside runs do through Aside, in place when there is none. +func (h *Hearer) aside(do func()) { + if h.Aside == nil { + do() + return + } + h.Aside(do) +} + +// endedOpen are the steps of prior, a pipeline's steps read before a placing that ended it, which the end took while asked +// (openAtEnd): those no longer open in steps, the steps as written, the same index naming the same step since a placing only +// appends. An ask in unput, placed but never put, is no ask of a seat and is left out. +func endedOpen(prior, steps []seat.StepResult, unput []Asked) []seat.StepResult { + var out []seat.StepResult + for i, s := range prior { + if i >= len(steps) || open(steps[i]) || len(openAtEnd([]seat.StepResult{s})) == 0 { + continue + } + if s.Ask != "" && slices.ContainsFunc(unput, func(a Asked) bool { return a.ID == s.Ask }) { + continue + } + out = append(out, s) + } + return out +} + +// dropEnded drops, through Drop, the asks the placing end of pipeline p took while open (H-A-506); a pipeline not ended +// drops nothing. +func (s *Starter) dropEnded(p seat.Pipeline, prior []seat.StepResult, unput []Asked) { + if p.Ended == nil { + return + } + steps := endedOpen(prior, p.Steps, unput) + if s.Drop != nil && len(steps) > 0 { + s.Drop(p.Pipeline, steps, "the pipeline "+p.Pipeline+" ended "+p.Outcome+": "+p.Why) + } + // The end cleared these steps' asks, so their ask keys go here, not with the end's own (H-A-506 addendum); a shared + // test run's key goes with the run's end (H-A-506 corrected). + for _, st := range steps { + if sharedRun(st) == "" || st.Seat == wants.Seat { // only a test run's key waits for the run's end + unmarkAsk(s.Clock, st.Ask, s.logf) + } + } +} diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/place.go b/modules/mesh-delivery-pipeline/internal/pipeline/place.go index e74f96a53..d093ffa2f 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/place.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/place.go @@ -508,6 +508,17 @@ func askAll(asker StepAsker, key string, asks []Asked, recovery func(commit stri return first } +// armAsk is what askAll does before each ask of pipeline key is put: its ask key is written, naming the pipeline, the step +// and the run (H-A-506 addendum), then its bound armed (§778); an ask whose key or bound is not written is not put. +func (s *Starter) armAsk(key string) func(Asked) error { + return func(a Asked) error { + if err := markAsk(s.Clock, key, a); err != nil { + return err + } + return s.Asks.arm(a) + } +} + // askedStep is the mover and the step an ask names, read from its body; "-" where it names none (the validate ask names no // mover, H-A-51). func askedStep(a Asked) (mover, step string) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/place_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/place_test.go index d78cc017a..40f37d233 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/place_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/place_test.go @@ -686,6 +686,47 @@ func TestASeatWithNoHolderIsReadFromTheSeatHoldersView(t *testing.T) { } }) } + // Design 47 H2 H-A-506: a placing that ends the pipeline (a later mover's seat with no holder) drops, through each + // seat's drop, every ask an earlier placing put and still open, and never an ask it placed and did not put. + { + store, asks := memStore{}, &stepAsks{} + holders := []string{"mesh-build-go"} + var dropped []string + s := &Starter{Pipelines: memCreated{store}, Say: func(string, any) error { return nil }, Steps: asks, Name: "the-runner", + Registration: func(string) (catalog.Registration, bool) { return reg, true }, + Holders: func(string) ([]string, bool) { return holders, true }, + Ended: func(seat.Pipeline, []int) error { return nil }, + Drop: func(key string, steps []seat.StepResult, why string) { + for _, st := range steps { + dropped = append(dropped, st.Seat+" "+st.Ask) + } + }} + if ok, err := s.Start(Begin{Branch: "main", Source: 4, Entries: entries, Movers: []seat.Mover{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}); !ok || err != nil { + t.Fatalf("start = %v, %v", ok, err) + } + p, rev, _ := store.Get("main.4") + var open []string + for _, st := range p.Steps { + if st.Ask != "" && st.Outcome == seat.StepResultOutcomeRunning { + open = append(open, st.Seat+" "+st.Ask) + } + } + p.Movers = append(p.Movers, seat.Mover{Module: "lib", Commit: "c2", Fingerprint: "src2:bb"}) + _, _ = store.Update("main.4", p, rev) + withLib := map[string]catalog.CatalogEntry{"lib": {Module: "lib", Path: "modules/lib", Artifacts: []catalog.Artifact{{Ref: "lib:program", Language: "go"}}}} + for k, e := range entries { + withLib[k] = e + } + holders = nil + got, _, _, _, _, err := s.placeOne(store, "main.4", func(seat.Pipeline) bool { return true }, + func(string) (catalog.Registration, bool) { return reg, true }, func(string) (Half, bool) { return Half{Entries: withLib}, true }, time.Unix(2, 0)) + if err != nil || got.Outcome != seat.PipelineOutcomeFailed { + t.Fatalf("the placing of lib = %s (%s), %v; want the pipeline failed, node-build-go holderless", got.Outcome, got.Why, err) + } + if len(open) < 3 || !slices.Contains(open, "mesh-wants "+stepNamed(p, "", "composition").Ask) || fmt.Sprint(dropped) != fmt.Sprint(open) { + t.Errorf("the placing's end dropped %v, want every ask still open %v, mesh-wants' among them, and none of lib's", dropped, open) + } + } // Design 47 H2 H-A-445: the nodes that can take a declined step hold every seat named, as the view keys a node seat. view := map[string]json.RawMessage{"node-build-go.ace": json.RawMessage(`{"holders":[{"module":"mesh-build-go"}]}`), "node-build-go.g14": json.RawMessage(`{"holders":[{"module":"mesh-build-go"}]}`), "node-build-node.g14": json.RawMessage(`{"holders":[{"module":"mesh-build-node"}]}`), diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/repin.go b/modules/mesh-delivery-pipeline/internal/pipeline/repin.go index 0313ac033..2e7bc6a0f 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/repin.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/repin.go @@ -111,6 +111,7 @@ func (s *Starter) keepIn(key string, half Half, movers []seat.Mover) []seat.Move return left } var holderEnded []int + prior, unput := slices.Clone(p.Steps), asks if len(asks) > 0 { asks, holderEnded = s.holderless(&p, asks, now) } @@ -126,6 +127,7 @@ func (s *Starter) keepIn(key string, half Half, movers []seat.Mover) []seat.Move s.logf("%s is not written, so the movers revision %d re-pinned stay its own at their old fingerprints: %v", key, half.Revision, err) return left } + s.dropEnded(p, prior, unput) // a holderless end drops the asks still open, never one placed and not put (H-A-506) if !keeps { s.logf("%s keeps no kept-revisions, so the pin check of its re-pinned movers reads the pipeline's revision (H-A-285)", key) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/repin_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/repin_test.go index f64a63717..3ac122550 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/repin_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/repin_test.go @@ -157,6 +157,49 @@ func TestASameSourceRepinKeepsTheMoverInItsPipeline(t *testing.T) { if p, _, _ := store.Get(key); !slices.Contains(p.Superseded, "app") { t.Errorf("the new source's revision left app in %s: %v", key, p.Superseded) } + t.Run("a holderless end drops the open asks of the movers it keeps", testARepinsHolderlessEndDropsTheOpenAsks) +} + +// design 47 H2 H-A-506: a re-pin whose placing ends the pipeline holderless drops, through each seat's drop, every ask still +// open of the movers it did not re-pin, and none of the re-pinned mover's new asks, never put. +func testARepinsHolderlessEndDropsTheOpenAsks(t *testing.T) { + reg, entries, repinned, read := repinFixture() + lib := catalog.CatalogEntry{Module: "lib", Path: "modules/lib", Fp: "src2:l1", Artifacts: []catalog.Artifact{{Ref: "lib:program", Language: "go"}}} + entries["lib"], repinned["lib"] = lib, lib + store := &MoverStore{Raw: newMemRaw()} + holders := []string{"mesh-build-go"} + var dropped []string + s := &Starter{Pipelines: store, Store: store, Steps: &stepAsks{}, Name: "the-runner", Now: ticking(), Revision: read, + Registration: func(m string) (catalog.Registration, bool) { return reg, m == "app" || m == "lib" }, + Say: func(string, any) error { return nil }, + Holders: func(string) ([]string, bool) { return holders, true }, + Ended: func(seat.Pipeline, []int) error { return nil }, + Drop: func(_ string, steps []seat.StepResult, _ string) { + for _, st := range steps { + dropped = append(dropped, st.Seat+" "+st.Ask) + } + }} + key := Key("main", 10) + if ok, err := s.Start(Begin{Branch: "main", Source: 41, Revision: 10, Entries: entries, Movers: []seat.Mover{ + {Module: "app", Commit: "c1", Fingerprint: "src2:aa", Repository: "o/app"}, + {Module: "lib", Commit: "c1", Fingerprint: "src2:l1", Repository: "o/lib"}}}); !ok || err != nil { + t.Fatalf("start = %v, %v", ok, err) + } + before, _, _ := store.Get(key) + var want []string + for _, st := range before.Steps { + if st.Module == "lib" && st.Ask != "" && open(st) { + want = append(want, st.Seat+" "+st.Ask) + } + } + holders = nil + if _, err := s.Revised(revisionEvent(11), read); err != nil { + t.Fatal(err) + } + p, _, _ := store.Get(key) + if p.Outcome != seat.PipelineOutcomeFailed || len(want) == 0 || fmt.Sprint(dropped) != fmt.Sprint(want) { + t.Errorf("the re-pin's holderless end = %s (%s), dropped %v; want failed and lib's open asks %v dropped", p.Outcome, p.Why, dropped, want) + } } // H-A-286: a kept mover not placed at its keep (its registration arrived later) is placed from the revision it is kept at diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/runends.go b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go index 99e2267d7..7a68b1e83 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/runends.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go @@ -55,15 +55,17 @@ type RunClock interface { Has(key string) (bool, error) // Keys are test-runs' keys. Keys() ([]string, error) - // MarkAsk writes the ask key of ask (AskKey) holding run, create-only: one written already is kept (H-A-500 addendum (1)). - MarkAsk(ask, run string) error - // RunOfAsk reads the ask key of ask: the key of the run it holds; ok false when it is not written. - RunOfAsk(ask string) (run string, ok bool, err error) + // MarkAsk writes the ask key of ask (AskKey) holding mark, create-only: one written already is kept (H-A-500 addendum + // (1), H-A-506 addendum). + MarkAsk(ask string, mark AskMark) error + // AskOf reads the ask key of ask: the mark it holds; ok false when it is not written. + AskOf(ask string) (mark AskMark, ok bool, err error) } -// AskPrefix begins the ask key of a test run's ask in test-runs, `ask.`, holding the run's key: written with each ask -// a run is given and deleted with the run, so an ask-taken or step-progress finds its run in one read and never lists the -// pipelines (design 47 H2 H-A-500 addendum (1)). +// AskPrefix begins the ask key of an ask the runner put, `ask.` in test-runs, holding its AskMark: written at the put +// of every ask, a test run's and every other's, and deleted with its pipeline's end or its run's, so an ask-taken or a +// step-progress finds its pipeline, step and run in one read and never lists the pipelines (design 47 H2 H-A-500 addendum +// (1), H-A-506 addendum: one key per ask, the test run's ask key of H-A-500 and the ask key of every other ask in one form). const AskPrefix = "ask." // AskKey is the ask key of ask. @@ -72,13 +74,42 @@ func AskKey(ask string) string { return AskPrefix + ask } // IsRunAsk says whether a key of test-runs is an ask key rather than a run. func IsRunAsk(key string) bool { return strings.HasPrefix(key, AskPrefix) } -// markAsk writes the ask key of a run's ask; one not written is said, and the ask's events are then found by the pipelines. -func markAsk(clock RunClock, ask, run string, logf func(string, ...any)) { - if clock == nil || ask == "" || run == "" { +// AskMark is what an ask key holds (H-A-506 addendum): the pipeline that put the ask, the mover and the step it asks, and +// the test run it asks, empty for an ask of no test run. An ask key written before the addendum holds its run alone. +type AskMark struct { + Pipeline string `json:"pipeline,omitempty"` + Module string `json:"module,omitempty"` + Step string `json:"step,omitempty"` + Run string `json:"run,omitempty"` +} + +// markOf is the mark of ask a, put by the pipeline key. +func markOf(key string, a Asked) AskMark { + mover, step := askedStep(a) + if mover == "-" { + mover = "" + } + return AskMark{Pipeline: key, Module: mover, Step: step, Run: a.Run} +} + +// markAsk writes the ask key of ask a of pipeline key before it is put (H-A-506 addendum); a nil clock writes none. +func markAsk(clock RunClock, key string, a Asked) error { + if clock == nil || a.ID == "" { + return nil + } + if err := clock.MarkAsk(a.ID, markOf(key, a)); err != nil { + return fmt.Errorf("the ask key of ask %s of %s is not written: %w", a.ID, key, err) + } + return nil +} + +// unmarkAsk deletes the ask key of ask, at its pipeline's end or its run's (H-A-506 addendum); one not deleted is said. +func unmarkAsk(clock RunClock, ask string, logf func(string, ...any)) { + if clock == nil || ask == "" { return } - if err := clock.MarkAsk(ask, run); err != nil && logf != nil { - logf("the ask key of ask %s of the test run %s is not written, so its events are found by the pipelines: %v", ask, run, err) + if err := clock.Unbound(AskKey(ask)); err != nil { + logf("the ask key of ask %s is not deleted: %v", ask, err) } } @@ -98,7 +129,7 @@ func unmarkAsks(clock RunClock, run string, logf func(string, ...any)) { if !IsRunAsk(k) { continue } - if held, ok, err := clock.RunOfAsk(strings.TrimPrefix(k, AskPrefix)); err != nil || !ok || held != run { + if held, ok, err := clock.AskOf(strings.TrimPrefix(k, AskPrefix)); err != nil || !ok || held.Run != run { continue } if err := clock.Unbound(k); err != nil { @@ -110,24 +141,35 @@ func unmarkAsks(clock RunClock, run string, logf func(string, ...any)) { // LateEveryLine is how many late events of ended runs' asks one journal line says (H-A-500 (a)). const LateEveryLine = 100 -// LateRunAsk says whether ask is the ask of a test run that has ended, or whose run was asked again under another ask -// since, read from its ask key and its run, never from the pipelines (design 47 H2 H-A-500 (a) and its addenda): such an -// event is acknowledged at once and moves nothing, which replaces H-A-311 (2)'s placing and H-A-238 (2)'s clearing for -// it. One journal line says every LateEveryLine of them. An ask with no ask key, or whose run is gone, is not judged here. +// LateRunAsk says whether an event of ask is acknowledged at once, moving nothing (design 47 H2 H-A-500 (a) and its addenda, +// H-A-506 addendum): an ask with no ask key, put before the ask keys or of a pipeline or a run already ended, reads nothing +// else; an ask of a test run that has ended, or whose run was asked again under another ask since, is read from its ask +// key and its run. Both are said in one journal line every LateEveryLine of them. This replaces +// H-A-311 (2)'s placing and H-A-238 (2)'s clearing for it. With no clock nothing is judged here. func (h *Hearer) LateRunAsk(event, ask string) bool { - if h.TestRuns == nil || h.Clock == nil || ask == "" { + if h.Clock == nil || ask == "" { return false } - run, ok, err := h.Clock.RunOfAsk(ask) - if err != nil || !ok { + m, ok, err := h.Clock.AskOf(ask) + if err != nil { + h.logf("the %s of ask %s is acknowledged, moving nothing: its ask key is not read: %v (H-A-506)", event, ask, err) + return true + } + if !ok { + if n := atomic.AddInt64(&h.lateRuns, 1); n%LateEveryLine == 0 { + h.logf("%d late events acknowledged at once, moving nothing; the last the %s of ask %s, which has no ask key (H-A-500, H-A-506)", n, event, ask) + } + return true + } + if m.Run == "" || h.TestRuns == nil { return false } - r, _, err := h.TestRuns.Get(run) + r, _, err := h.TestRuns.Get(m.Run) if err != nil || (r.Ended == nil && r.Ask == ask) { return false } if n := atomic.AddInt64(&h.lateRuns, 1); n%LateEveryLine == 0 { - h.logf("%d late events of ended test runs' asks acknowledged at once, moving nothing; the last the %s of ask %s of the test run %s (H-A-500)", n, event, ask, run) + h.logf("%d late events of ended test runs' asks acknowledged at once, moving nothing; the last the %s of ask %s of the test run %s (H-A-500)", n, event, ask, m.Run) } return true } @@ -149,39 +191,38 @@ func IsRunBound(key string) bool { return ok } -// runOfAsk is the test run asked under ask: the run its asking step names in Shared, read from the state pipelines. +// markOfAsk is the mark of ask, read from its ask key alone, never from the pipelines (H-A-506 addendum); ok false when the +// ask has no key, or no clock is held. +func (h *Hearer) markOfAsk(ask string) (AskMark, bool) { + if h.Clock == nil || ask == "" { + return AskMark{}, false + } + m, ok, err := h.Clock.AskOf(ask) + if err != nil { + h.logf("the ask key of ask %s is not read, so its event moves nothing: %v", ask, err) + return AskMark{}, false + } + return m, ok +} + +// runOfAsk is the test run asked under ask, read from its ask key (H-A-500 addendum (1)). func (h *Hearer) runOfAsk(ask string) (string, bool) { run, _, ok := h.runAndPipelineOfAsk(ask) return run, ok } -// runAndPipelineOfAsk is runOfAsk with the key of the pipeline whose step asked it. +// runAndPipelineOfAsk is runOfAsk with the key of the pipeline that put the ask, both read from its ask key alone (H-A-506 +// addendum); an ask key written before the addendum names no pipeline, the run's readers then naming every pipeline that +// reads it, the asking one among them. func (h *Hearer) runAndPipelineOfAsk(ask string) (string, string, bool) { - if h.TestRuns == nil || ask == "" { + if h.TestRuns == nil { return "", "", false } - // The ask key names the run in one read (H-A-500 addendum (1)); the pipeline is then left unnamed, the run's readers - // naming every pipeline that reads it, the asking one among them. - if h.Clock != nil { - if run, ok, err := h.Clock.RunOfAsk(ask); err == nil && ok { - return run, "", true - } - } - pkeys, err := allKeys(h.Store) - if err != nil { + m, ok := h.markOfAsk(ask) + if !ok || m.Run == "" { return "", "", false } - for _, key := range pkeys { - p, _, err := h.Store.Get(key) - if err != nil { - continue - } - if i := stepOfAsk(p, ask); i >= 0 { - run := sharedRun(p.Steps[i]) - return run, key, run != "" - } - } - return "", "", false + return m.Run, m.Pipeline, true } // releaseLate places what the end of a step heard late unlocks (H-A-311 (2)): a step-progress or step-end that arrives @@ -420,6 +461,7 @@ func (h *Hearer) EndRun(run string, end seat.TestRun, skip *seat.TestRunReader) h.logf("the bound %s of the ended test run is not deleted: %v", k, err) } } + unmarkAsk(h.Clock, r.Ask, h.logf) // the ask key goes with its run's end (H-A-506 addendum) } var first error for _, reader := range r.Readers { @@ -498,21 +540,21 @@ const NoReaderLeft = "no reader is left" // leaveRun takes the step of pipeline key that read or asked a test run out of the run's readers (§726): a step superseded // or cancelled. When no reader is left the run ends `could not run`, naming that (A-469z), and its ask is dropped (§119); // while one is left the ask stays, so a superseded pipeline never takes away another's run. A step of no test run leaves -// nothing. -func (h *Hearer) leaveRun(key string, s seat.StepResult, why string) { +// nothing. It answers the ask whose drop it asked, empty when it asked none. +func (h *Hearer) leaveRun(key string, s seat.StepResult, why string) string { run := sharedRun(s) if run == "" || h.TestRuns == nil { - return + return "" } reader := seat.TestRunReader{Pipeline: key, Artifact: s.Artifact} for { r, rev, err := h.TestRuns.Get(run) if errors.Is(err, ErrNoRun) { - return + return "" } if err != nil { h.logf("the test run %s is not read, so the step %s of %s does not leave it: %v", run, s.Step, key, err) - return + return "" } left := r.Readers[:0:0] for _, o := range r.Readers { @@ -521,7 +563,7 @@ func (h *Hearer) leaveRun(key string, s seat.StepResult, why string) { } } if len(left) == len(r.Readers) { - return + return "" } r.Readers = left // The last to leave ends the run in the same write, so a step joining it after that read finds it ended and asks @@ -535,10 +577,10 @@ func (h *Hearer) leaveRun(key string, s seat.StepResult, why string) { continue } else if err != nil { h.logf("the step %s of %s does not leave the test run %s: %v", s.Step, key, run, err) - return + return "" } if !last { - return + return "" } if h.Clock != nil { for _, k := range []string{BoundPrefix + run, UntakenPrefix + run} { @@ -546,13 +588,21 @@ func (h *Hearer) leaveRun(key string, s seat.StepResult, why string) { h.logf("the bound %s of the ended test run is not deleted: %v", k, err) } } + unmarkAsk(h.Clock, r.Ask, h.logf) // the ask key goes with its run's end (H-A-506 addendum) } - if h.DropRun != nil { + if h.DropRun == nil { + h.logf("%s: the ask %s of the test run %s, which no reader is left of, is not dropped: the runner has no drop", key, r.Ask, run) + return "" + } + // Asked beside the leave, so a seat with no holder never holds it; one line says what happened (H-A-506). + h.aside(func() { if err := h.DropRun(r.Seat, r.Node, r.Ask, why); err != nil { - h.logf("the drop of ask %s of the test run %s, which no reader is left of, is not answered: %v", r.Ask, run, err) + h.logf("%s: the drop of the ask %s of the test run %s, which no reader is left of, is refused or not answered by %s: %v (H-A-506)", key, r.Ask, run, r.Seat, err) + return } - } - return + h.logf("%s: the ask %s of the test run %s, which no reader is left of, is dropped on %s (H-A-506)", key, r.Ask, run, r.Seat) + }) + return r.Ask } } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/start.go b/modules/mesh-delivery-pipeline/internal/pipeline/start.go index 00f55c1c0..1082dbc0d 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/start.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/start.go @@ -198,6 +198,9 @@ type Starter struct { // Ended says a pipeline that ended at its placing (holderless): the step-ended of each step of steps, then its end // (Hearer.EndedAtPlacing); nil says it in the journal alone. Ended func(p seat.Pipeline, steps []int) error + // Drop takes the steps of pipeline key a placing's end took while asked off their seats, each own ask dropped through + // its seat's drop and each test run read left (design 47 H2 H-A-506; Hearer.DropOpen); nil drops none. + Drop func(key string, steps []seat.StepResult, why string) // Supersede takes out of every older running pipeline of newer's branch each mover whose fingerprint newer moved // (H-A-223; Hearer.SupersedeRunning); nil leaves running pipelines as they are. Supersede func(newer seat.Pipeline) @@ -386,7 +389,7 @@ func (s *Starter) askSteps(key string, asks []Asked) error { var err error if len(asks) > 0 { s.carry(key, asks) - err = askAll(s.Steps, key, asks, func(commit string) bool { return s.recovered(key, commit) }, s.Asks.arm, s.logf) + err = askAll(s.Steps, key, asks, func(commit string) bool { return s.recovered(key, commit) }, s.armAsk(key), s.logf) } s.sayStarted(key, placed) return err @@ -678,7 +681,7 @@ func (s *Starter) PutAgain(key string, a Asked) error { return errors.New("the runner puts no ask") } s.carry(key, []Asked{a}) - return askAll(s.Steps, key, []Asked{a}, func(commit string) bool { return s.recovered(key, commit) }, s.Asks.arm, s.logf) + return askAll(s.Steps, key, []Asked{a}, func(commit string) bool { return s.recovered(key, commit) }, s.armAsk(key), s.logf) } // validateStep is the index of p's first validate step that names its ask, -1 when it has none. @@ -979,6 +982,7 @@ func (s *Starter) placeOne(store Updater, key string, due func(seat.Pipeline) bo return p, nil, nil, nil, 0, nil } before, marks := len(p.Steps), unplacedMarks(p) + prior := slices.Clone(p.Steps) asks, unregistered, ended, notAsked := s.placeMovers(&p, reg, half.Entries, now) // Until its registration arrives, the pipeline says it waits on it (A-327): a mover with no registration has no step, // and it holds the pipeline's end only while every placed mover's required steps passed (H-A-163, H-A-171). @@ -1004,6 +1008,7 @@ func (s *Starter) placeOne(store Updater, key string, due func(seat.Pipeline) bo } else if err != nil { return p, nil, nil, nil, 0, fmt.Errorf("the placing of %s is not written: %w", key, err) } + s.dropEnded(p, prior, nil) // an end at this placing drops the asks its earlier placings put (H-A-506) return p, asks, ended, notAsked, len(p.Steps) - before, nil } } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go index 23d4ecb90..0c26785f7 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go @@ -195,6 +195,9 @@ type Hearer struct { AskDeclined func(p seat.Pipeline, s seat.StepResult, now time.Time) (*Asked, error) PutAgain func(key string, a Asked) error + // Aside runs do beside the caller's path: the drops of an ended pipeline's open asks, each asked side by side, so an end is + // never held by a seat that answers a kept call only at its bound (H-A-506); nil runs it in place. + Aside func(do func()) // lateRuns counts the late events of ended runs' asks acknowledged at once (LateRunAsk, H-A-500 (a)). lateRuns int64 } @@ -517,6 +520,13 @@ func (h *Hearer) said(p seat.Pipeline, asked []int, ended bool) error { } h.Failures.Ended(p) h.Unfinished.Ended(p) + // Every ask key of its steps goes with the pipeline's end (H-A-506 addendum), but a shared test run's: another + // pipeline may still read the run, and its key goes with the run's end (H-A-500 addendum (1), H-A-506 corrected). + for _, st := range p.Steps { + if sharedRun(st) == "" || st.Seat == wants.Seat { // only a test run's key waits for the run's end + unmarkAsk(h.Clock, st.Ask, h.logf) + } + } if h.RegistrationWaits != nil { h.RegistrationWaits() } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/steps_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/steps_test.go index 61dd483c5..2925bd337 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/steps_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/steps_test.go @@ -213,6 +213,34 @@ func TestStepEndedEndsThePipeline(t *testing.T) { t.Errorf("after the second decline: the step %+v, %d puts; want could not run, %q", lint, len(put), want) } }) + // H-A-506 addendum, corrected: a declined test-run step asked again keeps one ask key, its new ask's; the earlier + // ask's key is deleted as it is asked again, not at the run's end. + t.Run("declined test run", func(t *testing.T) { + h, store, _, _, _ := fixture() + h.Asker = nil + clock := &clockMem{runs: testRunsMem{}, bound: map[string]time.Duration{}} + h.Clock = clock + e := store[pkey] + e.p.Steps[0].Shared = []seat.Shared{{Run: "main.5.lint.d", Ask: "a1"}} + store[pkey] = e + _ = clock.MarkAsk("a1", AskMark{Pipeline: pkey, Module: "app", Step: "lint", Run: "main.5.lint.d"}) + h.NeedNodes = func([]string) ([]string, bool) { return []string{"n1", "n2"}, true } + h.AskDeclined = func(_ seat.Pipeline, s seat.StepResult, _ time.Time) (*Asked, error) { + return &Asked{Seat: s.Seat, ID: s.Ask + "x", Ask: nodebuildgo.StepAsk{ID: s.Ask + "x"}}, nil + } + h.PutAgain = func(string, Asked) error { return nil } + ev := ended("a1", "lint", nodebuildgo.StepEndedOutcomeDeclined) + ev.Node, ev.Needs, ev.Lacks, ev.Said = "n1", []string{"node-build-node"}, []string{"node-build-node"}, "this node lacks node-build-node" + if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ev); err != nil { + t.Fatal(err) + } + if lint := store[pkey].p.Steps[0]; lint.Ask != "a1x" { + t.Fatalf("the declined test-run step = %+v, want it asked again as a1x", lint) + } + if _, ok, _ := clock.AskOf("a1"); ok { + t.Error("the earlier ask's key of a declined test-run step outlives its ask") + } + }) } // The approval step is asked last, only when every other required step passed, and ended by its warrant (§26, §37). diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go b/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go index 420ce2ec9..284b478c8 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go @@ -25,43 +25,44 @@ func (h *Hearer) Taken(ask, node string, at time.Time) (*time.Time, error) { if ask == "" || node == "" { return nil, nil } - pkeys, err := allKeys(h.Store) - if err != nil { - return nil, fmt.Errorf("the pipelines are not listed: %w", err) + // The ask key names the ask's pipeline: that one record is read, never the pipelines list (H-A-506 addendum). + m, ok := h.markOfAsk(ask) + if !ok || m.Pipeline == "" { + return nil, nil } - for _, key := range pkeys { - for { - p, rev, err := h.Store.Get(key) - if err != nil { - break - } - i := stepOfAsk(p, ask) - if i >= 0 && !open(p.Steps[i]) { - h.Asks.answeredLate(p.Steps[i], h.logf) - } - if i < 0 || !open(p.Steps[i]) { - break - } - s := &p.Steps[i] - if s.Node == node && s.TakenAt != nil { - return s.TakenAt, nil - } - s.Node = node - if s.TakenAt == nil { - if at.IsZero() { - at = h.now() - } - s.TakenAt = ptrTime(at.UTC()) - } - if _, err := h.Store.Update(key, p, rev); errors.Is(err, ErrChanged) { - continue - } else if err != nil { - return nil, fmt.Errorf("pipeline %s cannot be written: %w", key, err) - } + key := m.Pipeline + for { + p, rev, err := h.Store.Get(key) + if err != nil { + return nil, nil + } + i := stepOfAsk(p, ask) + // A late ask-taken clears the finding only while the pipeline runs; after its end the finding stands (H-A-506 second + // addendum). + if i >= 0 && !open(p.Steps[i]) && p.Outcome == seat.PipelineOutcomeRunning { + h.Asks.answeredLate(p.Steps[i], h.logf) + } + if i < 0 || !open(p.Steps[i]) { + return nil, nil + } + s := &p.Steps[i] + if s.Node == node && s.TakenAt != nil { return s.TakenAt, nil } + s.Node = node + if s.TakenAt == nil { + if at.IsZero() { + at = h.now() + } + s.TakenAt = ptrTime(at.UTC()) + } + if _, err := h.Store.Update(key, p, rev); errors.Is(err, ErrChanged) { + continue + } else if err != nil { + return nil, fmt.Errorf("pipeline %s cannot be written: %w", key, err) + } + return s.TakenAt, nil } - return nil, nil } func ptrTime(t time.Time) *time.Time { return &t } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go index d604054e3..50c93f174 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go @@ -3,6 +3,9 @@ package pipeline import ( "errors" "testing" + "time" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo" ) @@ -11,16 +14,46 @@ import ( // supersedes it no more (design 47 H §748). func TestATakenAskNamesItsNode(t *testing.T) { h, store, _, _, _ := fixture() + clock := &clockMem{runs: testRunsMem{}, bound: map[string]time.Duration{}} + h.Clock = clock + _ = clock.MarkAsk("a1", AskMark{Pipeline: pkey, Module: "app", Step: "lint"}) + // The ask key names the pipeline: the ask-taken reads that one record and never lists the pipelines; an ask with no + // key reads none (H-A-506 addendum). + counted := &countedStore{memStore: store} + h.Store = counted if _, err := h.Taken("a1", "n7", h.now()); err != nil { t.Fatal(err) } if p, _, _ := store.Get(pkey); p.Steps[0].Node != "n7" || p.Steps[1].Node != "" { t.Fatalf("after ask-taken of a1 on n7 the steps name %q and %q", p.Steps[0].Node, p.Steps[1].Node) } + if counted.gets != 1 || counted.lists != 0 { + t.Errorf("the ask-taken of a1 read %d pipelines and listed %d times, want one read and no list", counted.gets, counted.lists) + } + counted.gets = 0 + if at, err := h.Taken("a2", "n7", h.now()); err != nil || at != nil || counted.gets != 0 || counted.lists != 0 { + t.Errorf("the ask-taken of a2, which has no ask key, = %v, %v, read %d and listed %d; want nothing read", at, err, counted.gets, counted.lists) + } } // A-222, A-77: a drop is bounded by a key of its own armed before it is asked and deleted when it is answered; one never // answered leaves its key to expire. +// countedStore is a memStore that counts its reads and its lists. +type countedStore struct { + memStore + gets, lists int +} + +func (c *countedStore) Get(key string) (seat.Pipeline, uint64, error) { + c.gets++ + return c.memStore.Get(key) +} + +func (c *countedStore) Keys(filter string) ([]string, error) { + c.lists++ + return c.memStore.Keys(filter) +} + func TestBoundDrop(t *testing.T) { var armed, answered []string arm := func(key string) error { armed = append(armed, key); return nil } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go b/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go index 422badffb..a46e85e85 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go @@ -250,8 +250,6 @@ func (s *Starter) started(a Asked, write func() error) error { if err := write(); err != nil { return err } - // The run's ask gets its ask key, so its events find the run in one read (H-A-500 addendum (1)). - markAsk(s.Clock, a.ID, a.Run, s.logf) if s.Clock != nil { untaken := s.Runs.Untaken if untaken <= 0 { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go index 385e3624d..60a47cb9a 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go @@ -4,6 +4,7 @@ import ( "fmt" "slices" "strings" + "sync" "testing" "time" @@ -20,8 +21,8 @@ type clockMem struct { asks map[string]string revs map[string]uint64 rev uint64 - // askRuns are the ask keys, each holding its run (H-A-500 addendum (1)). - askRuns map[string]string + // askRuns are the ask keys, each holding its mark (H-A-500 addendum (1), H-A-506 addendum). + askRuns map[string]AskMark // before is called once at the next Bound, a write that comes between its caller's read and its write. before func() } @@ -54,19 +55,25 @@ func (c *clockMem) UnboundAt(key string, rev uint64) error { return nil } func (c *clockMem) Has(key string) (bool, error) { _, ok := c.bound[key]; return ok, nil } -func (c *clockMem) MarkAsk(ask, run string) error { +func (c *clockMem) MarkAsk(ask string, m AskMark) error { if _, ok := c.askRuns[AskKey(ask)]; ok { return nil } if c.askRuns == nil { - c.askRuns = map[string]string{} + c.askRuns = map[string]AskMark{} } - c.askRuns[AskKey(ask)] = run + c.askRuns[AskKey(ask)] = m return nil } +func (c *clockMem) AskOf(ask string) (AskMark, bool, error) { + m, ok := c.askRuns[AskKey(ask)] + return m, ok, nil +} + +// RunOfAsk is the run the ask key of ask names. func (c *clockMem) RunOfAsk(ask string) (string, bool, error) { - run, ok := c.askRuns[AskKey(ask)] - return run, ok, nil + m, ok := c.askRuns[AskKey(ask)] + return m.Run, ok, nil } func (c *clockMem) Keys() ([]string, error) { var out []string @@ -107,6 +114,8 @@ type runFixture struct { fill int64 s *Starter h *Hearer + // mu guards drops and logged, written also by the drops DropOpen asks side by side (H-A-506). + mu sync.Mutex } func newRunFixture() *runFixture { @@ -120,8 +129,14 @@ func newRunFixture() *runFixture { return nil } f.h = &Hearer{Store: f.store, Say: say, TestRuns: f.runs, Clock: f.clock, RunOutputs: f.objects, - Logf: func(format string, a ...any) { f.logged = append(f.logged, fmt.Sprintf(format, a...)) }, + Logf: func(format string, a ...any) { + f.mu.Lock() + defer f.mu.Unlock() + f.logged = append(f.logged, fmt.Sprintf(format, a...)) + }, DropRun: func(seatName, node, ask, why string) error { + f.mu.Lock() + defer f.mu.Unlock() f.drops = append(f.drops, seatName+" "+node+" "+ask) return nil }} @@ -280,9 +295,11 @@ func TestATestRunHasOneClockACapAndAnAge(t *testing.T) { if _, _, err := f.runs.Get(run); err != nil { t.Fatal("a run is aged before test-runs-kept passed") } - if held, ok, _ := f.clock.RunOfAsk(unit.Ask); !ok || held != run { - t.Fatalf("the ask key of %s holds %q before the run is aged, want %s", unit.Ask, held, run) + // The ask key goes with the run's end, not its aging (H-A-506 addendum). + if held, ok, _ := f.clock.RunOfAsk(unit.Ask); ok { + t.Fatalf("the ask key of %s holds %q after its run ended, want it deleted with the end", unit.Ask, held) } + _ = f.clock.MarkAsk(unit.Ask, AskMark{Run: run}) // a key written before the addendum, holding its run alone now = now.Add(2 * 24 * time.Hour) a.Age() if _, _, err := f.runs.Get(run); err != ErrNoRun || len(f.objects) != 0 { @@ -352,20 +369,39 @@ func TestATestRunsEndMovesEveryReaderAndIsCountedOnce(t *testing.T) { t.Errorf("%s's unit ends journaled %d times in %q, want once per artifact", key, n, f.logged) } } - // a, cancelled, leaves the lint run to b's readers; b, cancelled after, leaves none: the ask is dropped and the run ends. + // Two pipelines read one lint run: a, cancelled, leaves it to b's readers and its ask is not dropped, while a's own + // asks of no shared run are; b, cancelled after, is the last reader: the run ends, no reader left, and its ask is + // dropped once (H-A-506 corrected). lint := stepNamed(a, "app:program", "lint") v := Verbs{Hearer: f.h, Acts: &actLog{}} if _, err := v.Cancel("operator", seat.CancelInput{Pipeline: a.Pipeline, Why: "not needed"}); err != nil { t.Fatal(err) } - if r, _, _ := f.runs.Get(lint.Shared[0].Run); r.Ended != nil || len(r.Readers) != 2 || len(f.drops) != 0 { - t.Fatalf("after a left the lint run = %+v, drops %v; want it running for b's two readers", r, f.drops) + if r, _, _ := f.runs.Get(lint.Shared[0].Run); r.Ended != nil || len(r.Readers) != 2 || slices.Contains(f.drops, "node-build-go "+lint.Ask) || len(f.drops) == 0 { + t.Fatalf("after a left the lint run = %+v, drops %v; want it running for b's two readers, its ask kept, a's own asks dropped", r, f.drops) } + // a's end keeps the run's ask key, which b still reads: b's holder's progress re-arms the run's clock (H-A-500 + // addendum (1), H-A-506 corrected). + run := lint.Shared[0].Run + if m, ok, _ := f.clock.AskOf(lint.Ask); !ok || m.Run != run { + t.Fatalf("after a's end the lint run's ask key = %+v, %v; want it kept for b", m, ok) + } + if err := f.h.RunTaken(lint.Ask, "ace"); err != nil { + t.Fatal(err) + } + delete(f.clock.bound, BoundPrefix+run) + if err := f.h.RunProgress(lint.Ask); err != nil || f.clock.bound[BoundPrefix+run] == 0 { + t.Fatalf("b's progress after a's end re-armed %v (%v), want the run's clock armed", f.clock.bound, err) + } + n := len(f.drops) if _, err := v.Cancel("operator", seat.CancelInput{Pipeline: b.Pipeline, Why: "not needed"}); err != nil { t.Fatal(err) } - if r, _, _ := f.runs.Get(lint.Shared[0].Run); r.Outcome != seat.StepResultOutcomeCouldNotRun || r.Said != NoReaderLeft || len(f.drops) != 2 || f.drops[0] != "node-build-go "+lint.Ask { - t.Errorf("after the last reader left the lint run = %+v, drops %v; want it ended, no reader left, its ask and the integration run's dropped", r, f.drops) + if r, _, _ := f.runs.Get(lint.Shared[0].Run); r.Outcome != seat.StepResultOutcomeCouldNotRun || r.Said != NoReaderLeft { + t.Errorf("after the last reader left the lint run = %+v; want it ended, no reader left", r) + } + if got := countLines(f.drops[n:], lint.Ask); got != 1 { + t.Errorf("b's cancel dropped %v, want the lint run's ask %s once", f.drops[n:], lint.Ask) } // A composition step's end keeps the runs mesh-wants names, as they came (A-468d). c := newRunFixture() @@ -384,6 +420,15 @@ func TestATestRunsEndMovesEveryReaderAndIsCountedOnce(t *testing.T) { if s := stepNamed(c.pipeline(p), "", "composition"); len(s.Shared) != 1 || s.Shared[0] != (seat.Shared{Machine: "ace", Run: "main.9.ace", Ask: "r1"}) { t.Errorf("the composition step's shared = %+v, want mesh-wants' as it came", s.Shared) } + // Its own ask key goes with the pipeline's end: a mesh-wants step's shared runs are no test run whose end deletes it + // (H-A-506 addendum, corrected). + _ = c.clock.MarkAsk(comp.Ask, AskMark{Pipeline: p.Pipeline, Step: "composition"}) + if err := c.h.said(c.pipeline(p), nil, true); err != nil { + t.Fatal(err) + } + if _, ok, _ := c.clock.AskOf(comp.Ask); ok { + t.Errorf("the composition's ask key %s outlives its pipeline's end", comp.Ask) + } } // Asking a test run again moves every reader (design 47 G §727; A-469l, A-470d, A-470e): a rerun of one artifact's failed @@ -668,8 +713,9 @@ func TestNoWriteOfATestRunActsOnAReadBeforeItsLatestChange(t *testing.T) { p := f.start(t, 4, 9) unit := stepNamed(p, "app:program", "unit") run := unit.Shared[0].Run - if got, ok, _ := f.clock.RunOfAsk(unit.Ask); !ok || got != run { - t.Fatalf("the ask key of %s holds %q, %v; want the run %s", unit.Ask, got, ok, run) + // The ask key of the run's ask names its pipeline, its step and its run (H-A-506 addendum). + if got, ok, _ := f.clock.AskOf(unit.Ask); !ok || got != (AskMark{Pipeline: p.Pipeline, Module: "app", Step: "unit", Run: run}) { + t.Fatalf("the ask key of %s holds %+v, %v; want %s's unit step of the run %s", unit.Ask, got, ok, p.Pipeline, run) } if f.h.LateRunAsk("step-progress", unit.Ask) { t.Fatal("the ask of a running run is judged late") @@ -684,8 +730,8 @@ func TestNoWriteOfATestRunActsOnAReadBeforeItsLatestChange(t *testing.T) { t.Fatalf("event %d of the ended run's ask is not judged late", i) } } - if f.h.LateRunAsk("ask-taken", "anaskofnorun") { - t.Error("an ask with no ask key is judged late") + if !f.h.LateRunAsk("ask-taken", "anaskofnorun") { + t.Error("an ask with no ask key is not acknowledged at once (H-A-506 addendum)") } if len(f.logged) != 2 || !strings.Contains(f.logged[1], "200 late events") { t.Errorf("journal %q; want one line per hundred late events", f.logged) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go b/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go index be2870482..4eb168431 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go @@ -208,8 +208,9 @@ func (v Verbs) Accept(caller string, in seat.AcceptInput) (seat.AcceptAnswer, er return seat.AcceptAnswer{Result: p.Steps[i]}, nil } -// Cancel ends a running pipeline `cancelled`, each step not ended cancelled and each step of a test run leaving it, recorded -// in the hand-act log first (§394, §726); it says pipeline-ended. +// Cancel ends a running pipeline `cancelled`, each step not ended cancelled, each step of a test run leaving it and every +// ask still open dropped through its seat's drop, recorded in the hand-act log first (§394, §726, H-A-506); it says +// pipeline-ended. func (v Verbs) Cancel(caller string, in seat.CancelInput) (seat.CancelAnswer, error) { h := v.Hearer recorded := false @@ -228,16 +229,14 @@ func (v Verbs) Cancel(caller string, in seat.CancelInput) (seat.CancelAnswer, er recorded = true } now := h.now() - var leave []seat.StepResult + // Every ask still open is dropped and every test run read is left once the cancel is written (H-A-506). + asked := openAtEnd(p.Steps) var cancelled []int for i := range p.Steps { s := &p.Steps[i] if !open(*s) && s.Outcome != seat.StepResultOutcomeWaiting { continue } - if sharedRun(*s) != "" && s.Seat != wants.Seat { - leave = append(leave, *s) // it leaves its test run, dropped only with the last reader (§726) - } s.Outcome, s.By, s.Why, s.Ended = seat.StepResultOutcomeCancelled, caller, in.Why, &now cancelled = append(cancelled, i) } @@ -248,9 +247,7 @@ func (v Verbs) Cancel(caller string, in seat.CancelInput) (seat.CancelAnswer, er } else if err != nil { return seat.CancelAnswer{}, fmt.Errorf("pipeline %s cannot be written: %w", in.Pipeline, err) } - for _, s := range leave { - h.leaveRun(in.Pipeline, s, "the pipeline "+in.Pipeline+" is cancelled: "+in.Why) - } + h.DropOpen(in.Pipeline, asked, "the pipeline "+in.Pipeline+" is cancelled: "+in.Why) // Each step the cancel ended is said, with its line (design 47 H2 §784). for _, i := range cancelled { if err := h.say(seat.EventStepEnded, p, &p.Steps[i]); err != nil { @@ -369,10 +366,11 @@ func (v Verbs) rerun(caller string, in seat.RerunInput, reopens bool) (seat.Reru } } var ask *Asked + var replaced string p, _, err := v.change(in.Pipeline, seat.EventStepStarted, func() error { return v.record(seat.VerbRerun, fmt.Sprintf("asked the %s step of %s again", in.Step, in.Pipeline), in.Why) }, func(p *seat.Pipeline, now time.Time) (int, error) { - ask = nil + ask, replaced = nil, "" switch p.Outcome { case seat.PipelineOutcomeCancelled, seat.PipelineOutcomeSuperseded: return -1, fmt.Errorf("the pipeline %s is %s: a step of it is not asked again", p.Pipeline, p.Outcome) @@ -391,6 +389,7 @@ func (v Verbs) rerun(caller string, in seat.RerunInput, reopens bool) (seat.Reru } } oldAsk := s.Ask + replaced = oldAsk *s = seat.StepResult{Step: s.Step, Module: s.Module, Artifact: s.Artifact, Status: s.Status, Fingerprint: s.Fingerprint, Seat: s.Seat, Outcome: seat.StepResultOutcomeWaiting, By: caller, Rerun: true} if ask != nil { @@ -417,9 +416,12 @@ func (v Verbs) rerun(caller string, in seat.RerunInput, reopens bool) (seat.Reru return seat.RerunAnswer{}, err } v.sayReopened(p, reopens) + if ask == nil || replaced != ask.ID { + unmarkAsk(v.Hearer.Clock, replaced, v.Hearer.logf) // one key per ask, gone once the ask is no longer open (H-A-506 addendum) + } if ask != nil { v.Starter.carry(in.Pipeline, []Asked{*ask}) - if err := askAll(v.Starter.Steps, in.Pipeline, []Asked{*ask}, func(commit string) bool { return v.Starter.recovered(in.Pipeline, commit) }, v.Starter.Asks.arm, v.Starter.logf); err != nil { + if err := askAll(v.Starter.Steps, in.Pipeline, []Asked{*ask}, func(commit string) bool { return v.Starter.recovered(in.Pipeline, commit) }, v.Starter.armAsk(in.Pipeline), v.Starter.logf); err != nil { return seat.RerunAnswer{}, fmt.Errorf("the step is written running but not asked, it is asked again at the next start: %w", err) } } @@ -567,7 +569,7 @@ func (v Verbs) rerunTest(caller string, in seat.RerunInput, st seat.StepResult, break } v.Starter.carry(in.Pipeline, []Asked{*ask}) - if err := askAll(v.Starter.Steps, in.Pipeline, []Asked{*ask}, func(commit string) bool { return v.Starter.recovered(in.Pipeline, commit) }, v.Starter.Asks.arm, v.Starter.logf); err != nil { + if err := askAll(v.Starter.Steps, in.Pipeline, []Asked{*ask}, func(commit string) bool { return v.Starter.recovered(in.Pipeline, commit) }, v.Starter.armAsk(in.Pipeline), v.Starter.logf); err != nil { return seat.RerunAnswer{}, fmt.Errorf("the step is written running but not asked, it is asked again at the next start: %w", err) } } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go index 53954de59..2fb55c882 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go @@ -4,7 +4,9 @@ import ( "encoding/json" "errors" "fmt" + "slices" "strings" + "sync" "testing" "time" @@ -36,9 +38,22 @@ func verbsFixture() (Verbs, memStore, *[]string, *stepAsks, *actLog, *[]string) rev uint64 }{p, 1} asks, acts, dropped := &stepAsks{}, &actLog{}, &[]string{} + clock := &clockMem{runs: testRunsMem{}, bound: map[string]time.Duration{}} + h.Clock = clock s := &Starter{Steps: asks, Name: "the-runner", Registration: func(string) (catalog.Registration, bool) { return reg, true }, - Now: h.Now} - h.DropRun = func(_, _, ask, _ string) error { *dropped = append(*dropped, ask); return nil } + Now: h.Now, Clock: clock} + var mu sync.Mutex // the drops DropOpen asks side by side write here too (H-A-506) + h.DropRun = func(onSeat, _, ask, _ string) error { + mu.Lock() + defer mu.Unlock() + *dropped = append(*dropped, onSeat+" "+ask) + return nil + } + h.Logf = func(format string, args ...any) { + mu.Lock() + defer mu.Unlock() + *dropped = append(*dropped, "log: "+fmt.Sprintf(format, args...)) + } return Verbs{Hearer: h, Starter: s, View: func(string) (Half, bool) { return Half{Entries: entries}, true }, Acts: acts}, store, said, asks, acts, dropped } @@ -137,10 +152,41 @@ func TestAcceptRerunAndCancelWriteByCompareAndSetAndRecordTheirHandAct(t *testin if _, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "approval", Why: "again"}); err == nil { t.Error("an approval was asked again by rerun") } - // A cancel ends the pipeline cancelled and drops the bounds of the asks still held. + // A cancel ends the pipeline cancelled and drops, through each seat's drop, every ask still open: the build on + // mesh-builds, the build steps on node-build-go and the composition on mesh-wants; an ended step's ask is not dropped + // (H-A-506). + before, _, _ := store.Get(pkey) + var open []string + for _, s := range before.Steps { + if s.Ask != "" && (s.Outcome == seat.StepResultOutcomeRunning || s.Outcome == seat.StepResultOutcomeWaitingForABuildNode) { + open = append(open, s.Seat+" "+s.Ask) + } + } c, err := v.Cancel("operator", seat.CancelInput{Pipeline: pkey, Why: "wrong branch"}) - if err != nil || c.Pipeline.Outcome != "cancelled" || c.Pipeline.EndCount != 1 || len(*dropped) != 0 { - t.Fatalf("cancel = %+v, %v, dropped %v", c.Pipeline, err, *dropped) + if err != nil || c.Pipeline.Outcome != "cancelled" || c.Pipeline.EndCount != 1 { + t.Fatalf("cancel = %+v, %v", c.Pipeline, err) + } + seats := map[string]bool{} + for _, o := range open { + seats[strings.Fields(o)[0]] = true + } + var drops, lines []string + for _, d := range *dropped { + if l, ok := strings.CutPrefix(d, "log: "); ok { + lines = append(lines, l) + } else { + drops = append(drops, d) + } + } + slices.Sort(drops) + slices.Sort(open) + if len(open) < 4 || !seats["mesh-builds"] || !seats["mesh-wants"] || !seats["node-build-go"] || fmt.Sprint(drops) != fmt.Sprint(open) { + t.Fatalf("cancel dropped %v, want every open ask %v, on mesh-builds, node-build-go and mesh-wants", drops, open) + } + for _, o := range open { + if ask := strings.Fields(o)[1]; countLines(lines, "the ask "+ask+" of ") != 1 { + t.Errorf("the drop of %s is not said in one journal line: %q", ask, lines) + } } if s := stepNamed(c.Pipeline, "app:program", "lint"); s.Outcome != "cancelled" || s.By != "operator" { t.Errorf("the lint step = %+v", s)