Only a superseded end naming a revision older than the pipeline's is asked again; one naming the pipeline's own revision (the builder's kept-revision path) moves the mover out as H-A-508 has it, so no re-ask can loop. A build that starts waiting on the revision view says wait-held, on: revision view, and wait-ended when the view's move ends it, as the operator's wait does. Builds: design 47 H2 H-A-563
286 lines
17 KiB
Go
286 lines
17 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshgrants"
|
|
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"
|
|
)
|
|
|
|
// refusedOnce is a store whose first write of a pipeline is refused as changed, another write having come between (the
|
|
// pipeline's key moving while its end is judged, as main.117's did).
|
|
type refusedOnce struct {
|
|
memStore
|
|
refused map[string]bool
|
|
}
|
|
|
|
func (r *refusedOnce) Update(key string, p seat.Pipeline, rev uint64) (uint64, error) {
|
|
if !r.refused[key] {
|
|
r.refused[key] = true
|
|
e := r.memStore[key]
|
|
e.rev++
|
|
r.memStore[key] = e
|
|
return 0, ErrChanged
|
|
}
|
|
return r.memStore.Update(key, p, rev)
|
|
}
|
|
|
|
// H-A-288, H-A-289 (met with main.117 and main.76): a pipeline ends, and the next waiting is placed, the moment nothing of it is
|
|
// still open: when its last mover leaves, when its last open step ends by the silent bound, and at the runner's start pass;
|
|
// its outcome is said in the journal. The reads its end needs are made once, before the compare-and-set, shared by every mover
|
|
// and every retry of the write: a write refused once is written at the retry without asking grants-for again.
|
|
func TestAPipelineWithNothingOpenEndsAtOnce(t *testing.T) {
|
|
t0 := time.Date(2026, 11, 21, 17, 45, 19, 0, time.UTC)
|
|
movers := []seat.Mover{{Module: "a", Commit: "c1", Fingerprint: "src2:a1"}, {Module: "b", Commit: "c1", Fingerprint: "src2:b1"},
|
|
{Module: "c", Commit: "c1", Fingerprint: "src2:c1"}}
|
|
step := func(module, outcome, ask string) seat.StepResult {
|
|
return seat.StepResult{Module: module, Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Outcome: outcome, Ask: ask, Seat: builds.Seat}
|
|
}
|
|
rows := []struct {
|
|
name string
|
|
steps []seat.StepResult
|
|
end func(t *testing.T, h *Hearer)
|
|
outcome string
|
|
}{
|
|
{"its last mover leaves for a newer pipeline (H-A-223)",
|
|
[]seat.StepResult{step("a", seat.StepResultOutcomeRunning, "x1"), step("b", seat.StepResultOutcomePassed, "x2"), step("c", seat.StepResultOutcomePassed, "x3")},
|
|
func(t *testing.T, h *Hearer) {
|
|
h.SupersedeRunning(seat.Pipeline{Pipeline: "main.120", Branch: "main", Revision: 120, Movers: []seat.Mover{
|
|
{Module: "a", Commit: "c2", Fingerprint: "src2:a2"}, {Module: "b", Commit: "c2", Fingerprint: "src2:b2"}, {Module: "c", Commit: "c2", Fingerprint: "src2:c2"}}})
|
|
}, seat.PipelineOutcomeSuperseded},
|
|
// H-A-508: a build the builder ended superseded moves its mover out as a newer pipeline taking it does: its other open
|
|
// steps end superseded, it is kept in superseded, and the pipeline passes by its other movers, never failed.
|
|
{"its last open step, a build, ends superseded (H-A-508)",
|
|
[]seat.StepResult{step("a", seat.StepResultOutcomeRunning, "x1"), {Module: "a", Artifact: "program", Step: seat.StepResultStepUnit,
|
|
Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeWaiting, Seat: "node-build-go"},
|
|
{Module: "a", Artifact: "program", Step: seat.StepResultStepLint, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning,
|
|
Ask: "l1", Seat: "node-build-go"},
|
|
step("b", seat.StepResultOutcomePassed, "x2"), step("c", seat.StepResultOutcomePassed, "x3")},
|
|
func(t *testing.T, h *Hearer) {
|
|
// Another seat's end naming superseded is an outcome the runner does not know: refused, loud, nothing taken.
|
|
gs := stepSeatOf(t, "node-build-go")
|
|
lint := StepEnded{ID: "l1", Node: "n1", Pipeline: "main.117", Module: "a", Commit: "c1", Fingerprint: "src2:a1", Step: "lint",
|
|
Outcome: builds.StepEndedOutcomeSuperseded}
|
|
if _, err := h.OnStepEnded(gs, gs.Prefix+"n1", lint); !errors.Is(err, ErrRefused) || !strings.Contains(err.Error(), "not an outcome of step-ended") {
|
|
t.Errorf("a node-build-go end naming superseded: %v, want refused as no outcome of step-ended", err)
|
|
}
|
|
if p, _, _ := h.Store.Get("main.117"); p.Steps[2].Outcome != seat.StepResultOutcomeRunning {
|
|
t.Errorf("the refused end moved the lint step: %+v", p.Steps[2])
|
|
}
|
|
h.View = newerView // the runner's view holds revision 120, naming a at src2:a2 (H-A-555)
|
|
bs := stepSeatOf(t, builds.Seat)
|
|
end := StepEnded{ID: "x1", Node: "n1", Pipeline: "main.117", Module: "a", Commit: "c1", Fingerprint: "src2:a1", Step: "build",
|
|
Outcome: builds.StepEndedOutcomeSuperseded, Said: "a at src2:a1 is superseded by src2:a2, the newest entry heard on its first branch"}
|
|
if _, err := h.OnStepEnded(bs, bs.Prefix+"n1", end); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _, _ := h.Store.Get("main.117")
|
|
if !slices.Equal(p.Superseded, []string{"a"}) || p.Steps[0].Outcome != seat.StepResultOutcomeSuperseded || !strings.Contains(p.Steps[0].Why, "src2:a2") ||
|
|
p.Steps[1].Outcome != seat.StepResultOutcomeSuperseded || p.Steps[2].Outcome != seat.StepResultOutcomeSuperseded {
|
|
t.Errorf("after a's build ended superseded: superseded %v, steps %+v; want a left, its build and unit superseded naming src2:a2", p.Superseded, p.Steps[:3])
|
|
}
|
|
}, seat.PipelineOutcomePassed},
|
|
{"its last open step ends could not run on the silent bound (H-A-243)",
|
|
[]seat.StepResult{step("a", seat.StepResultOutcomeRunning, "mwql5pio3390aa"), step("b", seat.StepResultOutcomePassed, "x2"), step("c", seat.StepResultOutcomePassed, "x3")},
|
|
func(t *testing.T, h *Hearer) {
|
|
if err := h.endUnanswered("main.117", "mwql5pio3390aa", UntakenWhy("mesh-wants")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}, seat.PipelineOutcomeFailed},
|
|
{"the runner's start pass finds it with nothing open",
|
|
[]seat.StepResult{step("a", seat.StepResultOutcomePassed, "x1"), step("b", seat.StepResultOutcomePassed, "x2"), step("c", seat.StepResultOutcomePassed, "x3")},
|
|
func(t *testing.T, h *Hearer) {
|
|
if n, err := h.EndAtStart(); n != 1 || err != nil {
|
|
t.Fatalf("the start pass ended %d (%v), want 1", n, err)
|
|
}
|
|
}, seat.PipelineOutcomePassed},
|
|
}
|
|
for _, r := range rows {
|
|
t.Run(r.name, func(t *testing.T) {
|
|
store := &refusedOnce{memStore: memStore{}, refused: map[string]bool{}}
|
|
store.memStore["main.117"] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{seat.Pipeline{V: 1, Pipeline: "main.117", Branch: "main", Source: 117, Revision: 117, Outcome: seat.PipelineOutcomeRunning,
|
|
Movers: slices.Clone(movers), Steps: slices.Clone(r.steps)}, 1}
|
|
// Another running pipeline with a step open, and one waiting its turn: neither ends.
|
|
store.memStore["main.118"] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{seat.Pipeline{V: 1, Pipeline: "main.118", Branch: "other", Source: 118, Revision: 118, Outcome: seat.PipelineOutcomeRunning,
|
|
Movers: movers[:1], Steps: []seat.StepResult{step("a", seat.StepResultOutcomeRunning, "y1")}}, 1}
|
|
store.memStore["main.119"] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{seat.Pipeline{V: 1, Pipeline: "main.119", Branch: "other", Source: 119, Revision: 119, Outcome: seat.PipelineOutcomeWaiting, Movers: movers[:1]}, 1}
|
|
store.refused["main.118"], store.refused["main.119"] = true, true
|
|
grants, revisions := 0, 0
|
|
w := &Widener{
|
|
Revision: func(string, int64) (Half, error) { revisions++; return Half{}, nil },
|
|
Holder: func(s string) (string, error) { return s, nil },
|
|
GrantsFor: func(string, int64) (meshgrants.GrantsFor, error) {
|
|
grants++
|
|
return meshgrants.GrantsFor{}, fmt.Errorf("cut after 5s")
|
|
},
|
|
}
|
|
var turned, journal []string
|
|
h := &Hearer{Store: store, Say: func(string, any) error { return nil }, Now: func() time.Time { return t0 }, Widens: w.Reads,
|
|
Turn: func(key string) { turned = append(turned, key) },
|
|
Logf: func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) }}
|
|
r.end(t, h)
|
|
p, _, _ := store.Get("main.117")
|
|
if p.Outcome != r.outcome {
|
|
t.Fatalf("main.117 = %s (%s), want %s", p.Outcome, p.Why, r.outcome)
|
|
}
|
|
if !slices.Equal(turned, []string{"main.117"}) {
|
|
t.Errorf("the turns freed = %v, want main.117's alone, so the next waiting is placed", turned)
|
|
}
|
|
if !slices.ContainsFunc(journal, func(l string) bool { return strings.HasPrefix(l, "main.117 ended "+r.outcome) }) {
|
|
t.Errorf("the journal does not say main.117's end %s: %q", r.outcome, journal)
|
|
}
|
|
if grants != 1 || revisions != 2 {
|
|
t.Errorf("the end asked grants-for %d times and a revision %d times over 3 movers and a refused write, want 1 and 2", grants, revisions)
|
|
}
|
|
for _, m := range p.Movers {
|
|
if len(m.Widens) != 1 || m.Widens[0].Name != "mesh-grants" {
|
|
t.Errorf("%s's widens = %+v, want the cut grants-for said", m.Module, m.Widens)
|
|
}
|
|
}
|
|
for _, k := range []string{"main.118", "main.119"} {
|
|
if q, _, _ := store.Get(k); q.Outcome == seat.PipelineOutcomeSuperseded || q.Outcome == seat.PipelineOutcomePassed || q.Outcome == seat.PipelineOutcomeFailed {
|
|
t.Errorf("%s ended %s with a step open or waiting its turn", k, q.Outcome)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
// H-A-508, H-A-361: a dependent that stays, whose steps ended not asked on the mover whose build ended superseded, is
|
|
// taken out and placed again; the pipeline does not end on it, and the superseded build's end is said as its own step's.
|
|
t.Run("a dependent that stays is placed again when its dependency's build ends superseded", 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,
|
|
Movers: slices.Clone(movers[:2]), Steps: []seat.StepResult{step("b", seat.StepResultOutcomeNotAsked, ""), step("a", seat.StepResultOutcomeRunning, "x1")}}, rev: 1}}
|
|
e := store["main.117"]
|
|
e.p.Steps[0].Why = notBuiltWhy("a")
|
|
store["main.117"] = e
|
|
var released []string
|
|
h := &Hearer{Store: store, Say: func(string, any) error { return nil }, Now: func() time.Time { return t0 }, Release: func(key string) { released = append(released, key) },
|
|
View: newerView}
|
|
bs := stepSeatOf(t, builds.Seat)
|
|
got, err := h.OnStepEnded(bs, bs.Prefix+"n1", StepEnded{ID: "x1", Node: "n1", Pipeline: "main.117", Module: "a", Commit: "c1", Fingerprint: "src2:a1",
|
|
Step: "build", Outcome: builds.StepEndedOutcomeSuperseded, Said: "a at src2:a1 is superseded by src2:a2"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Outcome != seat.PipelineOutcomeRunning || !slices.Equal(got.Superseded, []string{"a"}) || len(got.Steps) != 1 || got.Steps[0].Module != "a" ||
|
|
got.Steps[0].Outcome != seat.StepResultOutcomeSuperseded || !slices.Equal(released, []string{"main.117"}) {
|
|
t.Errorf("pipeline %s superseded %v steps %+v released %v; want running, a left, b taken out to be placed again", got.Outcome, got.Superseded, got.Steps, released)
|
|
}
|
|
})
|
|
// H-A-555, H-A-561, H-A-562 (main.453): a build ended superseded is judged by the revision its end names of the entry that
|
|
// superseded it: none (a builder before H-A-561) moves the mover out as before; one not newer than the pipeline's own is
|
|
// passed over, said, and asked again at once; one newer the runner's view does not hold yet holds the step waiting on the
|
|
// revision view, kept in the pipeline's value, until the view's move judges it again and the mover moves out; one newer and
|
|
// held moves the mover out at once.
|
|
// The revision is read from the seat mesh-builds' end, and only from one ended superseded.
|
|
if got, passed := buildSuperseding([]byte(`{"outcome":"superseded","revision":120}`)), buildSuperseding([]byte(`{"outcome":"passed","revision":120}`)); got != 120 || passed != 0 {
|
|
t.Errorf("the superseding revision read = %d, of a passed end %d; want 120 and 0", got, passed)
|
|
}
|
|
for _, r := range []struct {
|
|
name string
|
|
superseding int64
|
|
view int64
|
|
want string // left, again or held
|
|
}{
|
|
{"an end naming no revision (H-A-508 as before)", 0, 116, "left"},
|
|
{"an older revision's entry", 116, 117, "again"},
|
|
{"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"},
|
|
} {
|
|
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,
|
|
Movers: slices.Clone(movers[:2]), Steps: []seat.StepResult{step("a", seat.StepResultOutcomeRunning, "x1"), step("b", seat.StepResultOutcomeRunning, "x2")}}, rev: 1}}
|
|
var put []Asked
|
|
var journal []string
|
|
var waits []string // the waits said, by event and what they wait on (H-A-563)
|
|
view := r.view
|
|
say := func(subject string, body any) error {
|
|
if w, ok := body.(seat.Wait); ok {
|
|
ev := seat.EventWaitHeld
|
|
if strings.Contains(subject, seat.EventWaitEnded) {
|
|
ev = seat.EventWaitEnded
|
|
}
|
|
waits = append(waits, ev+" "+w.Step+" "+w.On+" "+w.Module)
|
|
}
|
|
return nil
|
|
}
|
|
h := &Hearer{Store: store, Say: say, Now: func() time.Time { return t0 },
|
|
View: func(string) (Half, bool) {
|
|
return Half{Revision: view, Source: view, Entries: map[string]catalog.CatalogEntry{"a": {Module: "a", Fp: "src2:a2"}}}, true
|
|
},
|
|
AskDeclined: func(_ seat.Pipeline, s seat.StepResult, _ time.Time) (*Asked, error) {
|
|
return &Asked{Seat: s.Seat, ID: s.Ask + "again"}, nil
|
|
},
|
|
PutAgain: func(_ string, a Asked) error { put = append(put, a); return nil },
|
|
Logf: func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) }}
|
|
bs := stepSeatOf(t, builds.Seat)
|
|
if _, err := h.OnStepEndedRevised(bs, bs.Prefix+"n1", StepEnded{ID: "x1", Node: "n1", Pipeline: "main.117", Module: "a", Commit: "c1", Fingerprint: "src2:a1",
|
|
Step: "build", Outcome: builds.StepEndedOutcomeSuperseded, Said: "a at src2:a1 is superseded by src2:a2, the newest entry heard on its first branch"}, nil, nil, r.superseding); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _, _ := store.Get("main.117")
|
|
left := func(p seat.Pipeline) bool {
|
|
return slices.Equal(p.Superseded, []string{"a"}) && p.Steps[0].Outcome == seat.StepResultOutcomeSuperseded && len(put) == 0
|
|
}
|
|
switch r.want {
|
|
case "left":
|
|
if !left(p) {
|
|
t.Errorf("superseded %v, a's build %+v, put %+v; want a left, its build superseded, nothing asked again", p.Superseded, p.Steps[0], put)
|
|
}
|
|
case "again":
|
|
if len(p.Superseded) != 0 || p.Steps[0].Outcome != seat.StepResultOutcomeRunning || p.Steps[0].Ask != "x1again" || p.Steps[0].Ended != nil ||
|
|
len(put) != 1 || put[0].ID != "x1again" || p.Outcome != seat.PipelineOutcomeRunning {
|
|
t.Errorf("superseded %v, a's build %+v, put %+v, pipeline %s; want nothing left, the build asked again as x1again", p.Superseded, p.Steps[0], put, p.Outcome)
|
|
}
|
|
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 "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 {
|
|
t.Fatalf("superseded %v, a's build %+v, put %+v; want the build waiting on the revision view for revision 120, nothing asked", p.Superseded, s, put)
|
|
}
|
|
if want := []string{seat.EventWaitHeld + " build revision view a"}; !slices.Equal(waits, want) {
|
|
t.Errorf("waits said %q, want %q", waits, want)
|
|
}
|
|
// The view's move to 119 holds it still; at 120 it is judged again and the mover moves out (H-A-562).
|
|
view = 119
|
|
if moved := h.RevisionHeld("main"); len(moved) != 0 {
|
|
t.Errorf("at revision 119 the view moved %v, want none", moved)
|
|
}
|
|
view = 120
|
|
if moved := h.RevisionHeld("main"); !slices.Equal(moved, []string{"main.117"}) {
|
|
t.Errorf("at revision 120 the view moved %v, want main.117", moved)
|
|
}
|
|
if p, _, _ := store.Get("main.117"); !left(p) || p.Steps[0].On != "" || p.Outcome != seat.PipelineOutcomeRunning {
|
|
t.Errorf("after the view held 120: superseded %v, a's build %+v, pipeline %s; want a left, b still running", p.Superseded, p.Steps[0], p.Outcome)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// newerView is the runner's view of main at revision 120, naming a at src2:a2: newer than main.117 (H-A-555).
|
|
func newerView(string) (Half, bool) {
|
|
return Half{Revision: 120, Source: 120, Entries: map[string]catalog.CatalogEntry{"a": {Module: "a", Fp: "src2:a2"}}}, true
|
|
}
|