From d3e5ecb9105984be2ac574623ee07de7e475ca22 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 19:22:38 +0100 Subject: [PATCH] P2 held-step-said: every end of a held step's wait says wait-ended with its how (ran, released, failed, superseded); AC-P1b and AC-P2 whole steps The wait-ended of a build held on the revision view carried no how, a rerun of the held step, a cancel, a mover's leaving and a revision the catalog answers gone said none. Each of the four ends is said now; a held step whose revision the catalog answers gone ends failed naming "revision gone", the mover staying. Builds: design 47 H2 H-A-599, H-A-577 (2), H-A-577 (3), H-A-738 (2), H-A-583 (2), H-A-619; AC-P1b, AC-P2 --- .../held_integration_test.go | 148 ++++++++++++++++++ .../cmd/mesh-delivery-pipeline/main.go | 3 +- .../run_integration_test.go | 1 + .../internal/pipeline/endopen_test.go | 36 ++++- .../internal/pipeline/repin.go | 12 +- .../internal/pipeline/steps.go | 34 ++-- .../internal/pipeline/supersede.go | 73 +++++++-- .../internal/pipeline/verbs.go | 6 +- .../internal/pipeline/verbs_test.go | 28 ++++ .../internal/pipeline/waitsended.go | 63 ++++++++ 10 files changed, 380 insertions(+), 24 deletions(-) create mode 100644 modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/held_integration_test.go create mode 100644 modules/mesh-delivery-pipeline/internal/pipeline/waitsended.go diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/held_integration_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/held_integration_test.go new file mode 100644 index 000000000..8a573a05b --- /dev/null +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/held_integration_test.go @@ -0,0 +1,148 @@ +//go:build integration + +package main + +import ( + "context" + "encoding/json" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/nats-io/nats.go" + + builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" + catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + stdio "git.novox.be/novox/mesh-sdk/go" + + "mesh-delivery-pipeline/internal/pipeline" +) + +// testASupersededBuildIsPassedOverOrHeldAndItsWaitEnds is a row of TestTheRunnerPlacesAPipelineAsksItsBuildTakesItsEndAndAnswersAReview +// (design 47 H2 H-A-583 (2), H-A-619): a build ended superseded naming a revision older than the pipeline's is passed over, said +// as a step-started with rerun, and its mover stays (AC-P1b, H-A-577 (3)); one ended naming a newer revision the view does not hold +// holds the step with wait-held, its bound the stalled bound at one minute and its clock the time it was held, and the view +// taking the revision in ends the wait ran (AC-P2, H-A-599). +func testASupersededBuildIsPassedOverOrHeldAndItsWaitEnds(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + t.Setenv(stdio.SettingWord(pipeline.StalledBoundSetting), "1m") + reg := catalog.Registration{Module: "app", Branches: []string{"main"}, Origin: "o/app", Policy: catalog.Policy{Steps: catalog.StepStatuses{Review: "bypass", Validate: "bypass", Composition: "bypass", Approval: "bypass"}}} + var nine atomic.Bool + w := startWhole(t, ctx, []catalog.Registration{reg}, func(raw []byte) catalog.RevisionAnswer { + var in catalog.RevisionInput + _ = json.Unmarshal(raw, &in) + if nine.Load() && (in.Revision == 9 || in.Revision == 0) { + return catalog.RevisionAnswer{Revision: &catalog.CatalogRevision{Revision: 9, Kind: "trunk", Branch: "main", Source: 9}, + Entries: []catalog.CatalogEntry{{Module: "app", Commit: "c1", Fp: "src2:a9"}}} + } + return catalog.RevisionAnswer{Revision: &catalog.CatalogRevision{Revision: 1, Kind: "trunk", Branch: "main", Source: 3}, + Entries: []catalog.CatalogEntry{{Module: "app", Commit: "c1", Fp: "src2:aa"}}} + }) + var mu sync.Mutex + var started []seat.StepEvent + var waits []seat.Wait + var held, ended int + for subject, hear := range map[string]func(*nats.Msg){ + seat.SubjectStepStartedAll: func(m *nats.Msg) { + var ev seat.StepEvent + if json.Unmarshal(m.Data, &ev) == nil { + mu.Lock() + started = append(started, ev) + mu.Unlock() + } + }, + seat.SubjectWaitHeldAll: func(m *nats.Msg) { hearWait(&mu, &waits, &held, m) }, + seat.SubjectWaitEndedAll: func(m *nats.Msg) { hearWait(&mu, &waits, &ended, m) }, + } { + if _, err := w.nc.Subscribe(subject, hear); err != nil { + t.Fatal(err) + } + } + taken, _ := json.Marshal(catalog.TakeInTaken{Branch: "main", Source: 3, Call: "0123456789abcdef", + Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}) + const key = "main.3" + var p seat.Pipeline + waitFor(t, ctx, w.logs, func() bool { + _, err := w.js.Publish(ctx, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", taken) + return err == nil && readPipeline(ctx, w.pipelines, key, &p) + }) + rev, _ := json.Marshal(catalog.CatalogRevisionEvent{V: 1, Revision: 1, Kind: "trunk", Branch: "main", Source: 3}) + if _, err := w.js.Publish(ctx, catalog.SubjectCatalogRevision("main"), rev); err != nil { + t.Fatal(err) + } + waitFor(t, ctx, w.logs, func() bool { + return readPipeline(ctx, w.pipelines, key, &p) && stepOf(p, seat.StepResultStepBuild).Outcome == seat.StepResultOutcomeRunning + }) + end := func(ask string, revision int64) { + raw, _ := json.Marshal(builds.StepEnded{ID: ask, Pipeline: key, Step: "build", Node: "n1", Outcome: builds.StepEndedOutcomeSuperseded, Revision: revision, + Said: "app at src2:aa is superseded", + Entries: []builds.BuildEntry{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}) + if _, err := w.js.Publish(ctx, "mesh.seat.mesh-builds.event.step-ended.n1", raw); err != nil { + t.Fatal(err) + } + } + // AC-P1b: an end naming revision 2, older than the pipeline's own 3, is passed over: asked again, said as a step-started with + // rerun by the runner, and the mover stays. + first := stepOf(p, seat.StepResultStepBuild).Ask + end(first, 2) + waitFor(t, ctx, w.logs, func() bool { + return readPipeline(ctx, w.pipelines, key, &p) && stepOf(p, seat.StepResultStepBuild).Ask != first + }) + again := stepOf(p, seat.StepResultStepBuild) + mu.Lock() + last := started[len(started)-1] + mu.Unlock() + if again.Outcome != seat.StepResultOutcomeRunning || !again.Rerun || len(p.Superseded) != 0 || !last.Result.Rerun || last.Result.By != "mesh-delivery-pipeline" || last.Result.Ask != again.Ask { + t.Fatalf("a superseded end naming an older revision left %+v, superseded %v, last step-started %+v; want the build asked again as a rerun by the runner, the mover staying", again, p.Superseded, last) + } + // AC-P2: an end naming revision 9, which the view (at 1) does not hold, holds the step: wait-held with the stalled bound. + end(again.Ask, 9) + waitFor(t, ctx, w.logs, func() bool { + mu.Lock() + defer mu.Unlock() + return held == 1 + }) + mu.Lock() + h := waits[0] + mu.Unlock() + if h.On != seat.WaitOnRevisionView || h.Bound != "1m0s" || h.ClockFrom == nil || h.Module != "app" || h.How != "" { + t.Fatalf("wait-held said %+v, want app's wait on the revision view, bound 1m0s, a clock-from", h) + } + if !readPipeline(ctx, w.pipelines, key, &p) || stepOf(p, seat.StepResultStepBuild).Outcome != seat.StepResultOutcomeWaiting || p.Outcome != seat.PipelineOutcomeRunning { + t.Fatalf("the held build reads %+v in pipeline %s", stepOf(p, seat.StepResultStepBuild), p.Outcome) + } + // The view takes revision 9 in: the wait ends ran, its clock the hold's, and the mover leaves for the newer revision's pipeline. + nine.Store(true) + rev, _ = json.Marshal(catalog.CatalogRevisionEvent{V: 1, Revision: 9, Kind: "trunk", Branch: "main", Source: 9}) + if _, err := w.js.Publish(ctx, catalog.SubjectCatalogRevision("main"), rev); err != nil { + t.Fatal(err) + } + waitFor(t, ctx, w.logs, func() bool { + mu.Lock() + defer mu.Unlock() + return ended == 1 + }) + mu.Lock() + e := waits[1] + mu.Unlock() + if e.How != "ran" || e.On != seat.WaitOnRevisionView || e.ClockFrom == nil || !e.ClockFrom.Equal(*h.ClockFrom) { + t.Fatalf("wait-ended said %+v, want how ran on the revision view with the hold's clock %v", e, h.ClockFrom) + } + if !strings.Contains(w.logs.String(), "judged again") { + t.Errorf("the view's move did not judge the held step again:\n%s", w.logs.String()) + } +} + +func hearWait(mu *sync.Mutex, waits *[]seat.Wait, n *int, m *nats.Msg) { + var w seat.Wait + if json.Unmarshal(m.Data, &w) == nil { + mu.Lock() + *waits = append(*waits, w) + *n++ + mu.Unlock() + } +} 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 3b8407d5e..1d65873c2 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -179,7 +179,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro if err != nil { return err } - hearer.StalledBound = stalledBound // the held step's wait is timed by it (H-A-738 (2)) + hearer.KeptRevision = revisionHeld(calls.CallKept) // a held step's revision the catalog answers gone ends its wait failed (H-A-599) + hearer.StalledBound = stalledBound // the held step's wait is timed by it (H-A-738 (2)) // design 47 H2 H-A-709 (2), §781: the answer bound, read as the tools program reads it; a value outside its range stops the start. answerBound, err := pipeline.AnswerBound(stdio.Setting) if err != nil { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/run_integration_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/run_integration_test.go index e62a9c912..38e206451 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/run_integration_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/run_integration_test.go @@ -57,6 +57,7 @@ func TestTheRunnerPlacesAPipelineAsksItsBuildTakesItsEndAndAnswersAReview(t *tes t.Run("a takeins bind that failed is bound again on registration-changed", testATakeInsBindThatFailedIsBoundAgainOnRegistrationChanged) t.Run("step log read does not wait for a batch it cannot fill", testStepLogReadDoesNotWaitForABatchItCannotFill) t.Run("a bound passing reads the runner's own step-ended durable", testABoundPassingReadsTheRunnersOwnStepEndedDurable) + t.Run("a superseded build is passed over or held and its wait ends", testASupersededBuildIsPassedOverOrHeldAndItsWaitEnds) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() reg := catalog.Registration{Module: "app", Branches: []string{"main"}, Origin: "o/app", Policy: catalog.Policy{Steps: catalog.StepStatuses{Review: "optional", Validate: "bypass", Composition: "required", Approval: "bypass"}}} diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go index e0b5f7a97..818ae7cb3 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go @@ -202,6 +202,10 @@ func TestAPipelineWithNothingOpenEndsAtOnce(t *testing.T) { {"the same revision's entry, the builder's kept revision (H-A-563)", 117, 117, "left"}, {"a newer revision's entry the view does not hold yet", 120, 118, "held"}, {"a newer revision's entry the view holds", 120, 120, "left"}, + // H-A-599: the held step's wait ends failed when the catalog answers its revision gone, ended superseded when a newer + // pipeline takes its mover. + {"a newer revision's entry the catalog answers gone", 120, 118, "gone"}, + {"a newer revision's entry, its mover then taken by a newer pipeline", 120, 118, "taken"}, } { t.Run("H-A-561: a build superseded by "+r.name, func(t *testing.T) { store := memStore{"main.117": {p: seat.Pipeline{V: 1, Pipeline: "main.117", Branch: "main", Source: 117, Revision: 117, Outcome: seat.PipelineOutcomeRunning, @@ -209,7 +213,7 @@ func TestAPipelineWithNothingOpenEndsAtOnce(t *testing.T) { var put []Asked var journal []string var waitBodies []seat.Wait - var waits []string // the waits said, by event and what they wait on (H-A-563) + var waits, hows []string // the waits said, by event and what they wait on (H-A-563), and each one's how (H-A-599) var started []seat.StepEvent view := r.view say := func(subject string, body any) error { @@ -223,10 +227,17 @@ func TestAPipelineWithNothingOpenEndsAtOnce(t *testing.T) { } waitBodies = append(waitBodies, w) waits = append(waits, ev+" "+w.Step+" "+w.On+" "+w.Module) + hows = append(hows, w.How) } return nil } h := &Hearer{Store: store, Say: say, Now: func() time.Time { return t0 }, Name: "mesh-delivery-pipeline", StalledBound: time.Minute, + KeptRevision: func(_ string, rv int64) error { + if r.want == "gone" { + return fmt.Errorf("%w: the catalog keeps no revision %d", ErrRevisionGone, rv) + } + return nil + }, View: func(string) (Half, bool) { return Half{Revision: view, Source: view, Entries: map[string]catalog.CatalogEntry{"a": {Module: "a", Fp: "src2:a2"}}}, true }, @@ -270,6 +281,26 @@ func TestAPipelineWithNothingOpenEndsAtOnce(t *testing.T) { if !slices.ContainsFunc(journal, func(l string) bool { return strings.Contains(l, "passed over") }) { t.Errorf("the passed-over end is not said: %q", journal) } + case "gone", "taken": + if s := p.Steps[0]; s.Outcome != seat.StepResultOutcomeWaiting || s.On != seat.WaitOnRevisionView || len(waits) != 1 { + t.Fatalf("a's build %+v, waits %q; want it held, one wait-held said", s, waits) + } + if r.want == "taken" { + h.SupersedeRunning(seat.Pipeline{Pipeline: "main.120", Branch: "main", Revision: 120, Movers: []seat.Mover{{Module: "a", Commit: "c2", Fingerprint: "src2:a2"}}}) + } else if moved := h.RevisionHeld("main"); !slices.Equal(moved, []string{"main.117"}) { + t.Fatalf("at revision 118, with revision 120 gone, the view moved %v, want main.117", moved) + } + p, _, _ := store.Get("main.117") + if want := []string{seat.EventWaitHeld + " build revision view a", seat.EventWaitEnded + " build revision view a"}; !slices.Equal(waits, want) { + t.Fatalf("waits said %q, want %q", waits, want) + } + if r.want == "gone" { + if s := p.Steps[0]; hows[1] != "failed" || s.Outcome != seat.StepResultOutcomeFailed || !strings.Contains(s.Why, "revision 120 gone") || len(p.Superseded) != 0 || p.Outcome != seat.PipelineOutcomeRunning { + t.Errorf("how %q, a's build %+v, superseded %v, pipeline %s; want the wait ended failed, the build failed naming revision 120 gone, the mover staying", hows[1], s, p.Superseded, p.Outcome) + } + } else if hows[1] != "superseded" || !slices.Equal(p.Superseded, []string{"a"}) { + t.Errorf("how %q, superseded %v; want the wait ended superseded with its mover gone", hows[1], p.Superseded) + } case "held": if s := p.Steps[0]; len(p.Superseded) != 0 || s.Outcome != seat.StepResultOutcomeWaiting || s.On != seat.WaitOnRevisionView || s.Revision != 120 || len(put) != 0 || p.Outcome != seat.PipelineOutcomeRunning { @@ -296,6 +327,9 @@ func TestAPipelineWithNothingOpenEndsAtOnce(t *testing.T) { if want := []string{seat.EventWaitHeld + " build revision view a", seat.EventWaitEnded + " build revision view a"}; !slices.Equal(waits, want) { t.Errorf("waits said %q, want %q", waits, want) } + if !slices.Equal(hows, []string{"", "ran"}) { + t.Errorf("the waits' hows %q, want the held wait with none and its end ran (H-A-599)", hows) + } // H-A-599, H-A-738 (2): each carries the runner's stalled-bound and the time the step was held; none outlives the hold. for _, w := range waitBodies { if w.Bound != "1m0s" || w.ClockFrom == nil || !w.ClockFrom.Equal(t0) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/repin.go b/modules/mesh-delivery-pipeline/internal/pipeline/repin.go index 75f28495e..af8f9a0b6 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/repin.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/repin.go @@ -3,6 +3,7 @@ package pipeline import ( "errors" "fmt" + runnerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient" "slices" "strings" "time" @@ -70,7 +71,7 @@ func (s *Starter) keepIn(key string, half Half, movers []seat.Mover) []seat.Move revisions := map[string]int64{} olds := slices.Clone(p.Movers) // the superseded steps are said at the fingerprint they ran for var moved, unregistered []string - var ended []seat.StepResult + var ended, held []seat.StepResult var leave, drop []seat.StepResult var asks []Asked var notAsked []int @@ -93,6 +94,7 @@ func (s *Starter) keepIn(key string, half Half, movers []seat.Mover) []seat.Move continue } live := open(st) || st.Outcome == seat.StepResultOutcomeWaiting + held = append(held, heldOf(seat.Pipeline{Steps: []seat.StepResult{st}})...) // its wait ends with it (H-A-599) l, d := supersedeStep(&p, &st, why, now) if live { leave, drop = append(leave, l...), append(drop, d...) @@ -134,6 +136,14 @@ func (s *Starter) keepIn(key string, half Half, movers []seat.Mover) []seat.Move s.logf("%s keeps the movers revision %d of its own source %d re-pinned, %s: %d steps ended superseded, %d asked again (H-A-283, H-A-284)", key, half.Revision, half.Source, strings.Join(moved, ", "), len(ended), len(asks)) s.sayRePinned(p, kept, revisions, half.Revision) + for _, st := range held { + if s.Say == nil { + break + } + if err := s.Say(runnerclient.RevisionSubject(seat.EventWaitEnded, p.Branch, subjectRevision(p)), waitOf(p, &st, now, 0, waitHowSuperseded)); err != nil { + s.logf("wait-ended of %s's %s %s on the revision view is not said: %v", key, st.Module, st.Step, err) + } + } // The re-pin says pipeline-started again with every revision its movers are kept at, so the indexer pins them (design // 47 H2 H-A-496 and its addendum). if keeps { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go index 99e2a56e2..93921c095 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go @@ -164,6 +164,9 @@ type Hearer struct { Where func(ask string) (string, *oc.Warrant, error) Logf func(string, ...any) Now func() time.Time + // KeptRevision says whether the catalog still keeps a branch's revision, as Verbs.RevisionHeld does: nil when it does, an error + // wrapping ErrRevisionGone when it answers it gone (H-A-599); nil reads none and leaves a held step waiting. + KeptRevision func(branch string, revision int64) error // Failures says a failed head as the finding pipeline-failed (§33); nil says none. Failures *Failures // RegistrationWaits judges, at every pipeline's end, the finding registration-missing of the modules no running pipeline @@ -446,6 +449,7 @@ func (h *Hearer) OnStepEndedRevised(bs StepSeat, subject string, ev StepEnded, s // (H-A-508, H-A-223): its other open steps end superseded, it is kept in the pipeline's superseded, and the pipeline // is judged by its other movers alone; never a failure. var leave, drop []seat.StepResult + heldBefore := heldOf(p) left := s.Step == seat.StepResultStepBuild && s.Outcome == seat.StepResultOutcomeSuperseded if left { why := "superseded: its build was superseded and its mover left for the newer revision's pipeline (H-A-508): " + s.Why @@ -521,6 +525,7 @@ func (h *Hearer) OnStepEndedRevised(bs StepSeat, subject string, ev StepEnded, s ev.Pipeline, s.Module, len(readers), s.Why) h.leaveSteps(ev.Pipeline, leave, drop, "the mover %s left "+ev.Pipeline+" for the newer revision's pipeline", "superseded: its build was superseded (H-A-508)") } + h.sayHeldEnded(p, heldBefore, allHow(waitHowSuperseded)) // a mover that left takes its held step's wait with it (H-A-599) if err := h.say(seat.EventStepEnded, p, s); err != nil { return p, fmt.Errorf("pipeline %s's %s is written but not said: %w", ev.Pipeline, seat.EventStepEnded, err) } @@ -763,20 +768,27 @@ func (h *Hearer) say(event string, p seat.Pipeline, step *seat.StepResult) error case seat.EventPipelineEnded: return h.Say(runnerclient.RevisionSubject(event, p.Branch, subjectRevision(p)), pipelineEnded(p)) default: - w := seat.Wait{V: 1, Pipeline: p.Pipeline, Branch: p.Branch, Revision: p.Revision, Source: p.Source, Run: p.Run, - Step: seat.WaitStepApproval, On: seat.WaitOnPerson, Since: at} - if step != nil { - m, _ := MoverOf(p, step.Module) - w.Module, w.Commit, w.Fingerprint, w.Step, w.Ask = m.Module, m.Commit, m.Fingerprint, step.Step, step.Ask - if step.On == seat.WaitOnRevisionView { // a build waiting for the revision that superseded it (H-A-563) - w.On = step.On - w.Bound, w.ClockFrom = boundOf(h.StalledBound), step.HeldAt // timed from its hold by the stalled bound (H-A-599, H-A-736 (1)) - } - } - return h.Say(runnerclient.RevisionSubject(event, p.Branch, subjectRevision(p)), w) + return h.Say(runnerclient.RevisionSubject(event, p.Branch, subjectRevision(p)), waitOf(p, step, at, h.StalledBound, "")) } } +// waitOf is the wait a step of p holds or ends: an approval's on a person, or a build's on the revision view, timed from its +// hold by the stalled bound (H-A-599, H-A-736 (1)); how says its end (ran, released, failed, superseded; H-A-599), empty for a +// wait held. +func waitOf(p seat.Pipeline, step *seat.StepResult, at time.Time, stalled time.Duration, how string) seat.Wait { + w := seat.Wait{V: 1, Pipeline: p.Pipeline, Branch: p.Branch, Revision: p.Revision, Source: p.Source, Run: p.Run, + Step: seat.WaitStepApproval, On: seat.WaitOnPerson, How: how, Since: at} + if step != nil { + m, _ := MoverOf(p, step.Module) + w.Module, w.Commit, w.Fingerprint, w.Step, w.Ask = m.Module, m.Commit, m.Fingerprint, step.Step, step.Ask + if step.On == seat.WaitOnRevisionView { // a build waiting for the revision that superseded it (H-A-563) + w.On = step.On + w.Bound, w.ClockFrom = boundOf(stalled), step.HeldAt + } + } + return w +} + // stepEvent is a step's step-started or step-ended of p, on the runner's subject for its mover: its result as it stands, // with its shared when it reads a run (§729; A-468e, A-469q; H §746). func stepEvent(event string, p seat.Pipeline, step seat.StepResult, at time.Time) (string, seat.StepEvent) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go b/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go index de358ce92..5da0060c1 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go @@ -127,6 +127,7 @@ func (h *Hearer) supersedeRunningOne(key string, newer seat.Pipeline) bool { why := "superseded: the newer " + newer.Pipeline + " moved its fingerprint, and carries it (H-A-223)" var ended []int var leave, drop []seat.StepResult + before := heldOf(p) for i := range p.Steps { st := &p.Steps[i] if !slices.Contains(moved, st.Module) || (!open(*st) && st.Outcome != seat.StepResultOutcomeWaiting) { @@ -156,6 +157,7 @@ func (h *Hearer) supersedeRunningOne(key string, newer seat.Pipeline) bool { if len(again) > 0 { h.logf("%s: %s, not asked on a dependency that left, placed again (H-A-361)", key, strings.Join(again, ", ")) } + h.sayHeldEnded(p, before, allHow(waitHowSuperseded)) for _, i := range ended { if err := h.say(seat.EventStepEnded, p, &p.Steps[i]); err != nil { h.logf("step-ended of %s's %s %s, superseded, is not said: %v", key, p.Steps[i].Module, p.Steps[i].Step, err) @@ -376,8 +378,34 @@ func waitsOnRevision(st seat.StepResult, held int64) bool { return st.Outcome == seat.StepResultOutcomeWaiting && st.On == seat.WaitOnRevisionView && st.Revision > 0 && st.Revision <= held } +// goneHeld reads, of each revision p's steps are held on that the view does not hold yet, whether the catalog still keeps it +// (H-A-599): the revisions it answers gone, with its words. A read that fails otherwise is journalled and leaves the wait. +func (h *Hearer) goneHeld(p seat.Pipeline, held int64) map[int64]error { + if h.KeptRevision == nil || p.Outcome != seat.PipelineOutcomeRunning { + return nil + } + gone := map[int64]error{} + read := map[int64]bool{} + for _, st := range heldOf(p) { + if st.Revision <= held || read[st.Revision] { + continue + } + read[st.Revision] = true + if err := h.KeptRevision(p.Branch, st.Revision); errors.Is(err, ErrRevisionGone) { + gone[st.Revision] = err + } else if err != nil { + h.logf("%s: revision %d, which a build of it waits on, is not read from the catalog, so the wait stays: %v", p.Pipeline, st.Revision, err) + } + } + return gone +} + func (h *Hearer) revisionHeldOne(key string, held int64) bool { wr := h.widensReads() // made before the compare-and-set, kept for its retries (H-A-289) + var gone map[int64]error + if p, _, err := h.Store.Get(key); err == nil { + gone = h.goneHeld(p, held) // read before the compare-and-set, never inside its retries + } for { p, rev, err := h.Store.Get(key) if err != nil || p.Outcome != seat.PipelineOutcomeRunning { @@ -391,20 +419,38 @@ func (h *Hearer) revisionHeldOne(key string, held int64) bool { whys[st.Module] = st.Why } } - if len(moved) == 0 { + // A step held on a revision the catalog answers gone ends failed, naming it; its pipeline is judged as at a step's end, + // the mover staying (H-A-599). + var failed []int + for i, st := range p.Steps { + if _, ok := gone[st.Revision]; ok && st.Outcome == seat.StepResultOutcomeWaiting && st.On == seat.WaitOnRevisionView && + !slices.Contains(p.Superseded, st.Module) && !slices.Contains(moved, st.Module) { + failed = append(failed, i) + } + } + if len(moved) == 0 && len(failed) == 0 { return false } now := h.now() var ended []int - var leave, drop, waited []seat.StepResult + var leave, drop []seat.StepResult + before := heldOf(p) // each wait-ended is said as the step was held (H-A-563) + goneWhy := "" + for _, i := range failed { + st := &p.Steps[i] + why := fmt.Sprintf("revision %d gone", st.Revision) + if goneWhy == "" { + goneWhy = why + } + st.Outcome, st.Why, st.Ended, st.On, st.HeldAt = seat.StepResultOutcomeFailed, why+": "+gone[st.Revision].Error(), &now, "", nil + p.Unlogged = append(p.Unlogged, endEntry(p.Pipeline, *st, now)) + ended = append(ended, i) + } for i := range p.Steps { st := &p.Steps[i] if !slices.Contains(moved, st.Module) || (!open(*st) && st.Outcome != seat.StepResultOutcomeWaiting) { continue } - if waitsOnRevision(*st, held) { - waited = append(waited, *st) // its wait-ended is said as it was held (H-A-563) - } why := fmt.Sprintf("superseded: its build was superseded and its mover left for the newer revision's pipeline, revision %d now held (H-A-508, H-A-562): %s", held, whys[st.Module]) st.On, st.Revision, st.HeldAt = "", 0, nil l, d := supersedeStep(&p, st, why, now) @@ -414,6 +460,9 @@ func (h *Hearer) revisionHeldOne(key string, held int64) bool { p.Superseded = append(p.Superseded, moved...) again := takeOutNotAsked(&p) done := h.endPipeline(&p, now, wr) + if done && goneWhy != "" && p.Outcome == seat.PipelineOutcomeFailed { + p.Why = goneWhy + "; " + p.Why // the pipeline ends failed naming the revision gone (H-A-599) + } if _, err := h.Store.Update(key, p, rev); errors.Is(err, ErrChanged) { continue } else if err != nil { @@ -425,11 +474,17 @@ func (h *Hearer) revisionHeldOne(key string, held int64) bool { if len(again) > 0 { h.logf("%s: %s, not asked on a dependency that left, placed again (H-A-361)", key, strings.Join(again, ", ")) } - for i := range waited { - if err := h.say(seat.EventWaitEnded, p, &waited[i]); err != nil { - h.logf("wait-ended of %s's %s %s on the revision view is not said: %v", key, waited[i].Module, waited[i].Step, err) + // The wait of a step the view took in ends ran; one of the mover's that still waited for a newer revision ends with the + // mover's leaving (H-A-599). + h.sayHeldEnded(p, before, func(st seat.StepResult) string { + switch _, isGone := gone[st.Revision]; { + case isGone && !slices.Contains(moved, st.Module): + return waitHowFailed + case waitsOnRevision(st, held): + return waitHowRan } - } + return waitHowSuperseded + }) for _, i := range ended { if err := h.say(seat.EventStepEnded, p, &p.Steps[i]); err != nil { h.logf("step-ended of %s's %s %s, superseded, is not said: %v", key, p.Steps[i].Module, p.Steps[i].Step, err) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go b/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go index c64e89328..6424a75ab 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go @@ -236,6 +236,7 @@ func (v Verbs) Cancel(caller string, in seat.CancelInput) (seat.CancelAnswer, er now := h.now() // 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) + held := heldOf(p) var cancelled []int for i := range p.Steps { s := &p.Steps[i] @@ -253,6 +254,7 @@ func (v Verbs) Cancel(caller string, in seat.CancelInput) (seat.CancelAnswer, er return seat.CancelAnswer{}, fmt.Errorf("pipeline %s cannot be written: %w", in.Pipeline, err) } h.DropOpen(in.Pipeline, asked, "the pipeline "+in.Pipeline+" is cancelled: "+in.Why) + h.sayHeldEnded(p, held, allHow(waitHowSuperseded)) // a held step's wait ends with its pipeline (H-A-599) // 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 { @@ -377,10 +379,11 @@ func (v Verbs) rerun(caller string, in seat.RerunInput, reopens bool) (seat.Reru } var ask *Asked var replaced string + var held []seat.StepResult 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, replaced = nil, "" + ask, replaced, held = nil, "", heldOf(*p) 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) @@ -426,6 +429,7 @@ func (v Verbs) rerun(caller string, in seat.RerunInput, reopens bool) (seat.Reru return seat.RerunAnswer{}, err } v.sayReopened(p, reopens) + v.Hearer.sayHeldEnded(p, held, allHow(waitHowReleased)) // the rerun of a held step releases its wait (H-A-599) 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) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go index 89734ab92..290d893c9 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/verbs_test.go @@ -229,6 +229,34 @@ func TestAcceptRerunAndCancelWriteByCompareAndSetAndRecordTheirHandAct(t *testin t.Errorf("end-again said %q and recorded %v, want pipeline-ended and its hand act", last, *acts) } + // H-A-599: a build held on the revision view whose step is asked again by rerun ends its wait released, and one whose pipeline + // is cancelled ends it superseded. + var hows []string + say := v.Hearer.Say + v.Hearer.Say = func(subject string, body any) error { + if w, ok := body.(seat.Wait); ok && strings.Contains(subject, seat.EventWaitEnded) { + hows = append(hows, w.Module+" "+w.On+" "+w.How) + } + return say(subject, body) + } + heldAt := time.Unix(100, 0) + held := func(key string) { + store[key] = struct { + p seat.Pipeline + rev uint64 + }{seat.Pipeline{Pipeline: key, Branch: "main", Source: 21, Outcome: seat.PipelineOutcomeRunning, Movers: []seat.Mover{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}, + Steps: []seat.StepResult{{Module: "app", Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeWaiting, + On: seat.WaitOnRevisionView, Revision: 9, HeldAt: &heldAt}}}, 1} + } + held("main.21") + if _, err := v.Rerun("operator", seat.RerunInput{Pipeline: "main.21", Step: "build", Why: "again"}); err != nil || !slices.Equal(hows, []string{"app revision view released"}) { + t.Errorf("rerun of a held build = %v, waits ended %q, want its wait ended released", err, hows) + } + held("main.22") + if _, err := v.Cancel("operator", seat.CancelInput{Pipeline: "main.22", Why: "wrong branch"}); err != nil || !slices.Equal(hows, []string{"app revision view released", "app revision view superseded"}) { + t.Errorf("cancel of a pipeline holding a build = %v, waits ended %q, want its wait ended superseded", err, hows) + } + // H-A-101 (1): on a pipeline of two movers a verb names the mover's module; one naming none, or a module the pipeline // does not move, is refused, and the module named picks that mover's step. two, store2, _, _, _, _ := verbsFixture() diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/waitsended.go b/modules/mesh-delivery-pipeline/internal/pipeline/waitsended.go new file mode 100644 index 000000000..b069c9930 --- /dev/null +++ b/modules/mesh-delivery-pipeline/internal/pipeline/waitsended.go @@ -0,0 +1,63 @@ +package pipeline + +import ( + runnerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" +) + +// The ways a build step's wait on the revision view ends, the `how` of its wait-ended (design 47 H2 H-A-599): the view took the +// revision in (ran), the runner's rerun asked the step again (released), the catalog answered the revision gone (failed), or +// its pipeline ended or was cancelled, or its mover left (superseded). +const ( + waitHowRan = "ran" + waitHowReleased = "released" + waitHowFailed = "failed" + waitHowSuperseded = "superseded" +) + +// heldOf are the steps of p held on the revision view, as they stand: copies, kept before a write changes them so their +// wait-ended is said as they were held (H-A-563). +func heldOf(p seat.Pipeline) []seat.StepResult { + var out []seat.StepResult + for _, st := range p.Steps { + if st.Outcome == seat.StepResultOutcomeWaiting && st.On == seat.WaitOnRevisionView { + out = append(out, st) + } + } + return out +} + +// heldEnded are the steps among before that p, as written, no longer holds on the revision view. +func heldEnded(p seat.Pipeline, before []seat.StepResult) []seat.StepResult { + var out []seat.StepResult + for _, b := range before { + still := false + for _, st := range p.Steps { + if st.Module == b.Module && st.Step == b.Step && st.Artifact == b.Artifact && + st.Outcome == seat.StepResultOutcomeWaiting && st.On == seat.WaitOnRevisionView { + still = true + break + } + } + if !still { + out = append(out, b) + } + } + return out +} + +// sayHeldEnded says wait-ended, with its how, of each step among before that p no longer holds on the revision view (H-A-599): +// how names the end of one step. A wait not said is journalled, never a fault of the write that ended it. +func (h *Hearer) sayHeldEnded(p seat.Pipeline, before []seat.StepResult, how func(seat.StepResult) string) { + for _, st := range heldEnded(p, before) { + w := waitOf(p, &st, h.now(), h.StalledBound, how(st)) + if err := h.Say(runnerclient.RevisionSubject(seat.EventWaitEnded, p.Branch, subjectRevision(p)), w); err != nil { + h.logf("wait-ended of %s's %s %s on the revision view is not said: %v", p.Pipeline, st.Module, st.Step, err) + } + } +} + +// allHow answers how for every step. +func allHow(how string) func(seat.StepResult) string { + return func(seat.StepResult) string { return how } +}