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..14820be9f 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -188,6 +188,7 @@ 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.Drop = hearer.DropOpen // a placing's end drops the asks still open (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). 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..709e03f4c --- /dev/null +++ b/modules/mesh-delivery-pipeline/internal/pipeline/drops.go @@ -0,0 +1,92 @@ +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): a step reading a test run leaves it (leaveRun, whose last reader's leave drops the run's ask), and every own +// ask still open, a shared test run's and a mesh-wants step's alike, 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. +func (h *Hearer) DropOpen(key string, steps []seat.StepResult, why string) { + done := map[string]bool{} + for _, st := range steps { + if sharedRun(st) != "" && st.Seat != wants.Seat { + if dropped := h.leaveRun(key, st, why); dropped != "" { + done[dropped] = true + h.logf("%s: the ask %s of %s's %s on %s is dropped with its test run, no reader left (H-A-506)", key, dropped, st.Module, st.Step, st.Seat) + } + } + 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 + } + 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) + continue + } + 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) + } +} + +// 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 s.Drop == nil || p.Ended == nil { + return + } + if steps := endedOpen(prior, p.Steps, unput); len(steps) > 0 { + s.Drop(p.Pipeline, steps, "the pipeline "+p.Pipeline+" ended "+p.Outcome+": "+p.Why) + } +} 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/runends.go b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go index 99e2267d7..c2d3f8399 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/runends.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go @@ -498,21 +498,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 it dropped, empty when it dropped 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 +521,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 +535,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} { @@ -551,8 +551,9 @@ func (h *Hearer) leaveRun(key string, s seat.StepResult, why string) { 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) } + return r.Ask } - return + return "" } } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/start.go b/modules/mesh-delivery-pipeline/internal/pipeline/start.go index 00f55c1c0..c8a05b6a2 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) @@ -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/testruns_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go index 385e3624d..fdd0d8cc9 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go @@ -352,20 +352,28 @@ 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. + // a, cancelled, leaves the lint run to b's readers, and drops every ask of its own still open, the lint run's it asked + // among them (H-A-506); b, cancelled after, leaves none: the run ends, no reader left, its ask not dropped twice. 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) { + t.Fatalf("after a left the lint run = %+v, drops %v; want it running for b's two readers, a's lint ask dropped", r, f.drops) } + 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 || len(f.drops) <= n { + t.Errorf("after the last reader left the lint run = %+v, drops %v; want it ended, no reader left", r, f.drops) + } + seen := map[string]int{} + for _, d := range f.drops[n:] { + if seen[d]++; seen[d] > 1 { + t.Errorf("b's cancel dropped %s twice: %v", d, f.drops[n:]) + } } // A composition step's end keeps the runs mesh-wants names, as they came (A-468d). c := newRunFixture() diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go b/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go index be2870482..47608faa7 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 { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go index 53954de59..6aeb4b8aa 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go @@ -38,7 +38,8 @@ func verbsFixture() (Verbs, memStore, *[]string, *stepAsks, *actLog, *[]string) asks, acts, dropped := &stepAsks{}, &actLog{}, &[]string{} 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 } + h.DropRun = func(onSeat, _, ask, _ string) error { *dropped = append(*dropped, onSeat+" "+ask); return nil } + h.Logf = func(format string, args ...any) { *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 +138,39 @@ 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) + } + } + 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)