Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/start.go
T
jschoubben 0548dc8f80 R13: a mover whose dependency was superseded out says so in its build's why
A waiting pipeline keeps in superseded (C-R5) the modules a newer waiting pipeline of its branch
took out of it, written with its movers by compare-and-set; at its placing each mover building on
one of them (an edge of a kind its build waits on) has its build step's why say that dependency was
taken out and that it builds against the newest published package.

Builds: design 47 H §748; lead answers H-A-53, H-A-107

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:56:31 +01:00

868 lines
34 KiB
Go

package pipeline
import (
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
"sort"
"strconv"
"strings"
"time"
runnerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient"
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"
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
"git.novox.be/novox/mesh-sdk/go/keys"
)
// ErrExists is a Create of a pipeline key that is written already: the second start, which writes nothing (§54).
var ErrExists = errors.New("the pipeline's key is written already")
// Created is the runner's state pipelines as a start writes it: a key written only when it is absent (compare-and-set on
// its absence), ErrExists when it is not.
type Created interface {
Create(key string, value []byte) error
}
// Begin is what a start names: the pipeline's key (watched branch and source revision, design 47 H §746), the take-in's
// call and catalog graph revision, its movers, and the catalog half of that branch.
type Begin struct {
Branch string
Source int64
Call string
Revision int64
// Movers are the take-in's entries, each with its tier (§746).
Movers []seat.Mover
// Entries are the catalog half's entries of the pipeline's branch: the steps' artifacts and build seats are read from them.
Entries map[string]catalog.CatalogEntry
// Published is the package's publish that started the pipeline, nil for any other start (§747).
Published *seat.Published
// Key is the pipeline's key when it is not <branch>.<source> (a publish's, §747); empty for a take-in's.
Key string
// unheld says the branch's catalog half is not held yet: the pipeline starts with no steps, placed once it is (A-336).
unheld bool
}
// Key is a take-in's pipeline key in the state pipelines, `<branch>.<source>`, the branch escaped as a key's name is
// (design 47 H §746).
func Key(branch string, source int64) string {
return seat.PipelinesByBranchSourceKey(keys.EscapeName(branch), strconv.FormatInt(source, 10))
}
// PublishedKey is the key of a pipeline a package's publish starts, `<branch>.<source>.published.<package>.<version>`,
// each part escaped as a key's name is (design 47 H §747).
func PublishedKey(branch string, source int64, pkg, version string) string {
return seat.PipelinesByBranchSourcePublishedPackageVersionKey(keys.EscapeName(branch), strconv.FormatInt(source, 10),
keys.EscapeName(pkg), keys.EscapeName(version))
}
// keyForms are the filters of every key form of the state pipelines: a take-in's, a run's and a publish's.
var keyForms = []string{"*.*", "*." + runToken + ".*", "*.*.published.*.*"}
// AllKeys are every pipeline's key, of each key form (§746, §747; H-A-83).
func AllKeys(store interface {
Keys(string) ([]string, error)
}) ([]string, error) {
return allKeys(store)
}
func allKeys(store interface {
Keys(string) ([]string, error)
}) ([]string, error) {
var out []string
for _, f := range keyForms {
k, err := store.Keys(f)
if err != nil {
return nil, err
}
out = append(out, k...)
}
return out, nil
}
// branchKeys are the keys of every pipeline of one watched branch, of each key form.
func branchKeys(store interface {
Keys(string) ([]string, error)
}, branch string) ([]string, error) {
var out []string
for _, f := range keyForms {
_, rest, _ := strings.Cut(f, ".")
k, err := store.Keys(keys.EscapeName(branch) + "." + rest)
if err != nil {
return nil, err
}
out = append(out, k...)
}
return out, nil
}
// MoverOf is the mover of p whose module is module.
func MoverOf(p seat.Pipeline, module string) (seat.Mover, bool) {
for _, m := range p.Movers {
if m.Module == module {
return m, true
}
}
return seat.Mover{}, false
}
// Moves says whether p moves module.
func Moves(p seat.Pipeline, module string) bool {
_, ok := MoverOf(p, module)
return ok
}
// placed says whether mover m of p has its steps placed: every placed mover has its build step at least.
func placed(p seat.Pipeline, module string) bool {
for _, s := range p.Steps {
if s.Module == module {
return true
}
}
return false
}
// Starter starts pipelines (design 47 G §54, §103): it writes a pipeline's key by compare-and-set on its absence, so one
// source revision and module has one pipeline whichever event starts it first, and says pipeline-started for the one it
// wrote.
type Starter struct {
Pipelines Created
// Say says one of the seat's events on its subject.
Say func(subject string, body any) error
Now func() time.Time
Logf func(string, ...any)
// Registration is a module's registration, whose policy names the steps of its pipeline (§21, §23); with none, a
// pipeline starts with no step and says so.
Registration func(module string) (catalog.Registration, bool)
// Steps asks each build seat for its step, as the pipeline's asking half (§22, §24); Name is the runner's own name as an
// asker.
Steps StepAsker
Name string
// Kept reads the kept results of each named package, by the package name an artifact's catalog entry carries (§25, §416): the
// results whose package is that name or one beneath it. A step ask carries them, so a build seat runs only the packages
// with no result for their digest.
Kept func(packages []string) (map[string][]nodebuildgo.KeptResult, error)
// Toolchain is the toolchain of a language as the runner heard it agreed (§416, A-104): its language and version; nil when
// none was heard, which asks the step with none.
Toolchain func(language string) (*nodebuildgo.ToolchainUsed, error)
// Holders are the modules the seat mesh-module-assigner's view seat-holders names for a seat, and whether that view's
// first view is read (§535, §647 as A-466y, A-466z amend them); nil, or a view not read yet, judges no seat holderless.
Holders func(seat string) ([]string, bool)
// 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
// Fills refuses a start while a store of the runner is full (§366); nil refuses none.
Fills *Fills
// Recovered says whether a commit is named a recovery (§81, §86): its steps are asked on the build seats' stream delivery.
Recovered func(commit string) (bool, error)
// Store is the state pipelines read and written by compare-and-set, where a run's pipelines are placed (PlaceRun).
Store Updater
// TestRuns is the state test-runs, where a test step's run is shared by every step of its candidate, level and digest
// (§724, §725); nil shares none, each step's ask put. Clock writes a run's untaken bound at its start, Runs holds the
// settings, RunsFill reads test-runs' fill, judged against Runs' cap before a run is written (A-469d), and EndReaders ends
// the steps of a run read ended or never asked.
TestRuns TestRuns
Clock RunClock
Runs RunSettings
RunsFill func() (seat.Fill, error)
EndReaders func(run string, readers []seat.TestRunReader, r seat.TestRun) error
// Queue bounds the pipelines asked at once to pipelines-at-once, the others waiting in order (design 47 H §748); nil
// places every start at once.
Queue *Queue
// RunTurn places a run on request whose turn in the queue came, from its candidate's newest revision (A-222); nil places
// it at the runner's next start.
RunTurn func(UnplacedRun)
}
// holderless ends at once, as it is placed, a pipeline with a build step whose seat the seat mesh-module-assigner's view
// seat-holders names no holder for (design 47 G §32, §535, §647 as A-466y and A-466z amend them; A-470g, A-470h): judged only
// once that view's first view is read, never from the picture before it. Each such step ends `could not run` naming the seat,
// every other step not ended ends `not asked`, each with its step log entry, and the pipeline ends `failed` naming the seat;
// none of its steps is asked. It answers the asks still to put and the steps it ended, none when no seat is holderless.
func (s *Starter) holderless(p *seat.Pipeline, asks []Asked, now time.Time) ([]Asked, []int) {
if s.Holders == nil {
return asks, nil
}
var none []string
for _, st := range p.Steps {
if !open(st) || !BoundSeat(st.Seat) || slices.Contains(none, st.Seat) {
continue
}
if holders, read := s.Holders(st.Seat); read && len(holders) == 0 {
none = append(none, st.Seat)
}
}
if len(none) == 0 {
return asks, nil
}
sort.Strings(none)
whys := make([]string, 0, len(none))
for _, n := range none {
whys = append(whys, fmt.Sprintf("the seat %s has no holder: the seat-holders view names none", n))
}
var ended []int
for i := range p.Steps {
st := &p.Steps[i]
if !open(*st) && st.Outcome != seat.StepResultOutcomeWaiting {
continue
}
st.Ended, st.Ask, st.Shared = &now, "", nil
if slices.Contains(none, st.Seat) {
st.Outcome, st.Why = seat.StepResultOutcomeCouldNotRun, fmt.Sprintf("the seat %s has no holder: the seat-holders view names none", st.Seat)
} else {
st.Outcome, st.Why = seat.StepResultOutcomeNotAsked, "the pipeline failed at once: "+strings.Join(whys, "; ")
}
p.Unlogged = append(p.Unlogged, endEntry(p.Pipeline, *st, now))
ended = append(ended, i)
}
p.Outcome, p.Why, p.Ended = seat.PipelineOutcomeFailed, strings.Join(whys, "; "), &now
p.EndCount++
return nil, ended
}
// endedAtPlacing says a pipeline holderless ended at placing (A-470h): each step it ended in its step-ended, then the
// pipeline's end, through Ended; with none given it is said in the journal alone.
func (s *Starter) endedAtPlacing(p seat.Pipeline, steps []int) error {
s.logf("%s ended failed at once: %s (A-470g)", p.Pipeline, p.Why)
if s.Ended == nil {
return nil
}
return s.Ended(p, steps)
}
// askSteps puts a placed pipeline's asks (§22, §24): a test run another step asked is read, not asked again (share), then
// the kept results and toolchain are carried and each ask is put.
func (s *Starter) askSteps(key string, asks []Asked) error {
placed := asks
asks = s.share(key, asks)
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.sayStarted(key, placed)
return err
}
// sayStarted says step-started for each step of pipeline key that asks were placed for (design 47 G §726, §729; A-469a):
// the step asked, and each step reading the test run it asks, naming the run's key and ask in shared, so the watchman times
// it from that ask (§496, §732). A composition step's, said before any run is chosen, names none (§729).
func (s *Starter) sayStarted(key string, asks []Asked) {
if s.Store == nil || s.Say == nil || len(asks) == 0 {
return
}
ids, runs := map[string]bool{}, map[string]bool{}
for _, a := range asks {
ids[a.ID] = true
if a.Run != "" {
runs[a.Run] = true
}
}
p, _, err := s.Store.Get(key)
if err != nil {
s.logf("the pipeline %s is not read, so its steps' step-started is not said: %v", key, err)
return
}
at := s.now()
for _, st := range p.Steps {
if !open(st) || !(ids[st.Ask] || (st.Ask == "" && runs[sharedRun(st)])) {
continue
}
subject, ev := stepEvent(seat.EventStepStarted, p, st, at)
if err := s.Say(subject, ev); err != nil {
s.logf("step-started of %s's %s %s is not said: %v", key, st.Artifact, st.Step, err)
}
}
}
func newPipeline(b Begin, key string, now time.Time) seat.Pipeline {
movers := append([]seat.Mover(nil), b.Movers...)
return seat.Pipeline{V: 1, Pipeline: key, Branch: b.Branch, Source: b.Source, Call: b.Call, Revision: b.Revision, Movers: movers,
Outcome: seat.PipelineOutcomeRunning, Started: &now, Steps: []seat.StepResult{}, Published: b.Published}
}
func (s *Starter) create(key string, p seat.Pipeline) error {
raw, err := json.Marshal(p)
if err != nil {
return err
}
return s.Pipelines.Create(key, raw)
}
// started is a pipeline's pipeline-started (§746): its key, branch, source, call, revision and movers.
func started(p seat.Pipeline, at time.Time) seat.PipelineStarted {
return seat.PipelineStarted{V: 1, At: &at, Pipeline: p.Pipeline, Branch: p.Branch, Source: p.Source, Call: p.Call,
Revision: p.Revision, Movers: p.Movers, Published: p.Published, Run: p.Run}
}
// sayStartedPipeline says p's pipeline-started on <branch>.<source> (§746).
func (s *Starter) sayStartedPipeline(p seat.Pipeline, at time.Time) {
if s.Say == nil {
return
}
if err := s.Say(runnerclient.SourceSubject(seat.EventPipelineStarted, p.Branch, p.Source), started(p, at)); err != nil {
s.logf("pipeline-started of %s is not said: %v", p.Pipeline, err)
}
}
// placeMovers places the steps of each mover of p that has none and whose registration reg names (§746): each mover's from
// its own registration and entry. It answers the asks to put, the movers still without a registration, the steps the
// placing ended at once (holderless), and the steps it placed not asked because a dependency did not build (H-A-101 (2)),
// each said by sayNotAsked once the pipeline is written.
func (s *Starter) placeMovers(p *seat.Pipeline, reg func(module string) (catalog.Registration, bool), entries map[string]catalog.CatalogEntry, now time.Time) ([]Asked, []string, []int, []int) {
var asks []Asked
var unregistered []string
var notAsked []int
for _, m := range p.Movers {
if placed(*p, m.Module) {
continue
}
r, ok := reg(m.Module)
if !ok {
unregistered = append(unregistered, m.Module)
continue
}
// A mover's steps wait for the publish of each mover it builds on (§746, H-A-83 (2)): placed once each is heard,
// ended not asked when one's build ended without passing.
due, waiting, failed := ready(*p, m.Module, entries)
if !due && failed == "" {
s.logf("%s: %s waits for %s before its build", p.Pipeline, m.Module, strings.Join(waiting, ", "))
continue
}
steps, a := Place(*p, m, r, entries, now, s.Name)
supersededWhy(*p, m.Module, entries, steps)
if failed != "" {
why := "its dependency " + failed + " did not build"
for k := range steps {
st := &steps[k]
if st.Outcome == seat.StepResultOutcomeBypassed {
continue
}
st.Outcome, st.Why, st.Ask, st.Shared, st.Ended = seat.StepResultOutcomeNotAsked, why, "", nil, &now
p.Unlogged = append(p.Unlogged, endEntry(p.Pipeline, *st, now))
notAsked = append(notAsked, len(p.Steps)+k)
}
p.Steps = append(p.Steps, steps...)
continue
}
// The pipeline's validate is one run (§745, H-A-51): a mover placed after it was asked reads it, its result too
// when it ended.
if j := validateStep(*p); j >= 0 {
a = slices.DeleteFunc(a, func(x Asked) bool { return x.ID == p.Steps[j].Ask })
for k := range steps {
if steps[k].Step == seat.StepResultStepValidate && steps[k].Ask == p.Steps[j].Ask && !open(p.Steps[j]) {
v := p.Steps[j]
steps[k].Outcome, steps[k].Why, steps[k].Node, steps[k].Ended = v.Outcome, v.Why, v.Node, v.Ended
}
}
}
p.Steps = append(p.Steps, steps...)
for _, x := range a {
if !slices.ContainsFunc(asks, func(y Asked) bool { return y.ID == x.ID }) {
asks = append(asks, x)
}
}
}
// A mover placed after the validate ended has its composition asked, or not asked, at once (§745).
more, _ := releaseCompositions(p, now)
asks = append(asks, more...)
asks, ended := s.holderless(p, asks, now)
return asks, unregistered, ended, notAsked
}
// sayNotAsked says the step-ended of each step of p placed not asked because its mover's dependency did not build, naming
// the dependency in its why, so no step ends silently (H-A-101 (2)).
func (s *Starter) sayNotAsked(p seat.Pipeline, steps []int) {
if s.Say == nil {
return
}
at := s.now()
for _, i := range steps {
subject, ev := stepEvent(seat.EventStepEnded, p, p.Steps[i], at)
if err := s.Say(subject, ev); err != nil {
s.logf("step-ended of %s's %s %s, not asked, is not said: %v", p.Pipeline, p.Steps[i].Module, p.Steps[i].Step, err)
}
}
}
// AskSteps puts the asks of pipeline key and says their step-started (askSteps), for the hearer's asks of a step another's
// end made due: a mover's composition once validate passed (§745).
func (s *Starter) AskSteps(key string, asks []Asked) error {
if s.Steps == nil || len(asks) == 0 {
return nil
}
return s.askSteps(key, asks)
}
// validateStep is the index of p's first validate step that names its ask, -1 when it has none.
func validateStep(p seat.Pipeline) int {
for i, st := range p.Steps {
if st.Step == seat.StepResultStepValidate && st.Ask != "" {
return i
}
}
return -1
}
// Start writes b's pipeline, running, and says pipeline-started; false, nil when its key was written already (the second
// start writes nothing, §746).
func (s *Starter) Start(b Begin) (bool, error) {
if b.Branch == "" || len(b.Movers) == 0 || b.Source <= 0 {
return false, fmt.Errorf("a pipeline names its watched branch, source revision and one or more movers: %+v", b)
}
now := s.now()
key := b.Key
if key == "" {
key = Key(b.Branch, b.Source)
}
if err := s.Fills.refuseStart(key); err != nil {
return false, err
}
p := newPipeline(b, key, now)
var asks []Asked
var ended, notAsked []int
place, wkey, err := s.queueStart(&p)
if err != nil {
return false, fmt.Errorf("the pipeline %s is not judged against pipelines-at-once: %w", key, err)
}
if !place {
s.logf("%s waits its turn under pipelines-at-once, sequence %d", key, p.Seq)
} else if b.unheld {
s.logf("%s starts with no step: the catalog half of %s is not held yet, and its steps are placed once it is (A-336)", key, b.Branch)
} else if s.Registration != nil {
var unregistered []string
asks, unregistered, ended, notAsked = s.placeMovers(&p, s.Registration, b.Entries, now)
for _, m := range unregistered {
s.logf("%s: %s has no registration yet: its steps wait on its registration, which places them (A-327)", key, m)
}
}
if err := s.create(key, p); err != nil {
if wkey != "" {
_ = s.dropWaiting(wkey)
}
if errors.Is(err, ErrExists) {
s.logf("%s is started already: this start writes nothing", key)
return false, nil
}
return false, fmt.Errorf("the pipeline %s is not written: %w", key, err)
}
// A waiting pipeline says pipeline-started at its write, so its revision is pinned while it waits (H-A-52), and its
// wait on its turn.
s.sayStartedPipeline(p, now)
if !place {
s.sayQueueWait(seat.EventWaitHeld, p, now, "")
if _, err := s.supersedeWaiting(p); err != nil {
s.logf("the older waiting pipelines of %s are not superseded by %s: %v", p.Branch, key, err)
}
s.Queue.overCap(key, s.logf)
return true, nil
}
s.ran(key)
s.sayNotAsked(p, notAsked)
if len(ended) > 0 {
return true, s.endedAtPlacing(p, ended)
}
if s.Steps != nil && len(asks) > 0 {
if err := s.askSteps(key, asks); err != nil {
return true, err
}
}
return true, nil
}
// Resume asks again, of the build seats and the assigner, each step of branch's running pipelines that is placed and still
// running (design 47 G §88's restart pickup): a pipeline written whose asks never reached their queues (the runner stopped
// between its write and its asks, or a publish failed) is asked at the next start. Each ask carries the id its step was
// placed under, so one the seat holds already is kept once. half is the branch's catalog half, read for the steps' artifacts
// and seats as Place reads it. It says how many steps it asked.
func (s *Starter) Resume(store Updater, branch string, half Half) (int, error) {
if s.Steps == nil || s.Registration == nil {
return 0, nil
}
pkeys, err := allKeys(store)
if err != nil {
return 0, fmt.Errorf("the pipelines are not listed: %w", err)
}
now := s.now()
asked := 0
var first error
for _, key := range pkeys {
p, _, err := store.Get(key)
if err != nil {
if first == nil {
first = fmt.Errorf("the pipeline %s is not read: %w", key, err)
}
continue
}
// A run on request's steps are placed from its candidate, not from the branch's half (PlaceRun).
if p.Branch != branch || p.Outcome != seat.PipelineOutcomeRunning || p.Run != "" {
continue
}
// A step that names its test run is asked as the run's asker (share); one that names none was asked before the test
// runs and keeps its own ask, ending by the path it was asked under (§730, A-468k).
running, own := map[string]bool{}, map[string]bool{}
for _, st := range p.Steps {
if open(st) && st.Ask != "" {
running[st.Ask], own[st.Ask] = true, len(st.Shared) == 0
}
}
var due []Asked
for _, m := range p.Movers {
reg, ok := s.Registration(m.Module)
if !ok {
continue
}
_, shared := Place(p, m, reg, half.Entries, now, s.Name)
_, unshared := place(p, m, reg, half.Entries, now, s.Name, false)
for _, a := range shared {
if running[a.ID] && !own[a.ID] {
due = append(due, a)
}
}
for _, a := range unshared {
if running[a.ID] && own[a.ID] {
a.Run = ""
due = append(due, a)
}
}
}
if len(due) == 0 {
continue
}
if err := s.askSteps(key, due); err != nil && first == nil {
first = err
}
asked += len(due)
}
return asked, first
}
// Registered places, on a registration-changed of reg's module, the steps of that module's mover in every running pipeline
// that moves it with none placed, from the registration (design 47 G §21, §23, A-327): a pipeline started before its module
// had a registration. Each is written by compare-and-set, so it is placed once, its steps asked, and said; a run on request
// is left to its candidate, and one whose branch has no catalog half yet keeps waiting, said, until that half is held
// (Waiting). It says how many pipelines it placed in.
func (s *Starter) Registered(store Updater, reg catalog.Registration, view func(branch string) (Half, bool)) (int, error) {
if reg.Module == "" {
return 0, nil
}
return s.placeWaiting(store, func(p seat.Pipeline) bool { return Moves(p, reg.Module) && !placed(p, reg.Module) },
func(module string) (catalog.Registration, bool) { return reg, module == reg.Module },
view, "the registration of "+reg.Module+", which arrived after the pipeline started")
}
// Waiting places, from the registrations held now, the steps of every mover with none of every running pipeline of branch
// (A-327): a registration heard before the runner could place from it, or before the branch's catalog half was held, is not
// lost, since the half becoming held and the registrations' first view being held each place what waited on them. An empty
// branch is every branch. It says how many pipelines it placed in.
func (s *Starter) Waiting(store Updater, branch string, view func(branch string) (Half, bool)) (int, error) {
if s.Registration == nil {
return 0, nil
}
return s.placeWaiting(store, func(p seat.Pipeline) bool {
if branch != "" && p.Branch != branch {
return false
}
for _, m := range p.Movers {
if !placed(p, m.Module) {
return true
}
}
return false
}, s.Registration, view, "the registration held when its branch's half or the registrations became held")
}
// placeWaiting places the steps of each mover with none of each running pipeline due names that is not a run on request,
// from the registration reg names and its branch's catalog half (Registered, Waiting).
func (s *Starter) placeWaiting(store Updater, due func(seat.Pipeline) bool, reg func(module string) (catalog.Registration, bool), view func(branch string) (Half, bool), from string) (int, error) {
pkeys, err := allKeys(store)
if err != nil {
return 0, fmt.Errorf("the pipelines are not listed: %w", err)
}
now := s.now()
count := 0
var first error
note := func(err error) {
if first == nil {
first = err
}
}
for _, key := range pkeys {
p, rev, err := store.Get(key)
if err != nil {
note(fmt.Errorf("the pipeline %s is not read: %w", key, err))
continue
}
if p.Outcome != seat.PipelineOutcomeRunning || p.Run != "" || !due(p) {
continue
}
half, ok := view(p.Branch)
if !ok {
s.logf("the catalog half of %s is not held yet: %s waits on it for its steps, placed once it is held", p.Branch, key)
continue
}
if p.Revision > 0 && half.Revision < p.Revision {
s.logf("the catalog half of %s holds revision %d: %s waits for revision %d, which its take-in made, to place its steps (§200)", p.Branch, half.Revision, key, p.Revision)
continue
}
before := len(p.Steps)
asks, _, ended, notAsked := s.placeMovers(&p, reg, half.Entries, now)
if len(p.Steps) == before {
continue
}
if _, err := store.Update(key, p, rev); err != nil {
s.logf("%s is written meanwhile, so nothing is placed in it now: %v", key, err)
continue
}
count++
s.logf("%s: %d steps placed from %s (A-327)", key, len(p.Steps)-before, from)
// A pipeline whose steps were never asked is started by this placing (A-446a); the pipeline-started of its start,
// said already, is said again harmlessly.
s.sayStartedPipeline(p, now)
s.sayNotAsked(p, notAsked)
if len(ended) > 0 {
if err := s.endedAtPlacing(p, ended); err != nil {
note(err)
}
continue
}
if s.Steps != nil && len(asks) > 0 {
if err := s.askSteps(key, asks); err != nil {
note(err)
}
}
}
return count, first
}
// recovered says whether the commit of pipeline key is named a recovery; a read that fails asks on the stream check, said, since
// a step is asked either way (a recovery's only goes ahead of the first round).
func (s *Starter) recovered(key, commit string) bool {
if s.Recovered == nil || commit == "" {
return false
}
named, err := s.Recovered(commit)
if err != nil {
s.logf("whether %s of %s is a recovery is not read, so its steps are asked on check: %v", commit, key, err)
}
return named
}
// carry puts the kept results of each ask's package and the toolchain of its language on its step ask (§25, §416, A-104). A
// read that fails asks the steps with none, so every package runs: slower, never wrong; it is said.
func (s *Starter) carry(key string, asks []Asked) {
var pkgs []string
seen := map[string]bool{}
for _, a := range asks {
if a.Package != "" && !seen[a.Package] {
seen[a.Package] = true
pkgs = append(pkgs, a.Package)
}
}
var kept map[string][]nodebuildgo.KeptResult
if s.Kept != nil && len(pkgs) > 0 {
var err error
if kept, err = s.Kept(pkgs); err != nil {
s.logf("the kept results for the steps of %s are not read, so each package runs: %v", key, err)
}
}
chains := map[string]*nodebuildgo.ToolchainUsed{}
for _, a := range asks {
ask, ok := a.Ask.(nodebuildgo.StepAsk)
if !ok || ask.Step == nil {
continue
}
if a.Package != "" {
ask.Step.Kept = kept[a.Package]
}
if s.Toolchain == nil || a.Toolchain == "" {
continue
}
t, done := chains[a.Toolchain]
if !done {
var err error
if t, err = s.Toolchain(a.Toolchain); err != nil {
s.logf("the toolchain of %s for the steps of %s is not read: %v", a.Toolchain, key, err)
}
chains[a.Toolchain] = t
}
ask.Step.Toolchain = t
}
}
func (s *Starter) logf(format string, args ...any) {
if s.Logf != nil {
s.Logf(format, args...)
}
}
// TakenInSubject is where any module's take-in.taken is said, `mesh.mod.<module>.event.take-in.taken`: the runner names no
// module (§7), so the bus's grant from the manifest's `consumes` says which one it hears (§54).
const TakenInSubject = "mesh.mod.*.event.take-in.taken"
// DecodeTakenIn decodes a take-in.taken strictly by its contract (design 47 H §765, H-A-58, H-A-98): an event of the old
// form, one missing a field its new form requires (branch, source, call), is refused, naming what it lacks.
func DecodeTakenIn(raw []byte) (catalog.TakeInTaken, error) {
var fields map[string]json.RawMessage
if err := json.Unmarshal(raw, &fields); err != nil {
return catalog.TakeInTaken{}, err
}
var missing []string
for _, f := range []string{"branch", "source", "call"} {
if v, ok := fields[f]; !ok || string(v) == "null" || string(v) == `""` {
missing = append(missing, f)
}
}
if len(missing) > 0 {
return catalog.TakeInTaken{}, fmt.Errorf("a take-in.taken of the old form: it names no %s", strings.Join(missing, ", "))
}
var ev catalog.TakeInTaken
err := json.Unmarshal(raw, &ev)
return ev, err
}
// TakenIn starts, for a take-in.taken with entries, exactly one pipeline keyed `<branch>.<source>` by compare-and-set on its
// absence, whose movers are the event's entries, each with its tier (design 47 H §746). A take-in with no entries, or a
// skip, starts nothing; a second hearing writes nothing. view is the catalog half of the event's branch, read for the
// entries the steps and the tiers are made from.
func (s *Starter) TakenIn(ev catalog.TakeInTaken, view func(branch string) (Half, bool)) (int, error) {
if len(ev.Entries) == 0 {
s.logf("the take-in of %s source %d names no entries: it starts no pipeline", ev.Branch, ev.Source)
return 0, nil
}
if ev.Branch == "" || ev.Source <= 0 {
return 0, fmt.Errorf("a take-in.taken names its watched branch and source revision: branch %q source %d", ev.Branch, ev.Source)
}
// A take-in heard before its branch's half is held starts its pipeline with no steps, never placed from empty entries
// (§200, A-336); Waiting places them once the half is held. It starts at the revision the take-in made (A-448): while the
// view holds an older one it starts with no step, which Waiting places once the view reaches it.
half, held := view(ev.Branch)
if held && half.Revision < ev.Revision {
s.logf("the take-in of %s source %d made revision %d and the runner's view holds %d: its pipeline starts with no step, placed once the view reaches it (§200)", ev.Branch, ev.Source, ev.Revision, half.Revision)
held = false
}
movers := make([]seat.Mover, 0, len(ev.Entries))
for _, e := range ev.Entries {
movers = append(movers, seat.Mover{Module: e.Module, Commit: e.Commit, Fingerprint: e.Fingerprint, Repository: e.Repository})
}
movers = Tiers(movers, half.Entries, s.logf)
ok, err := s.Start(Begin{Branch: ev.Branch, Source: ev.Source, Call: ev.Call, Revision: ev.Revision, Movers: movers,
Entries: half.Entries, unheld: !held})
started := 0
if ok {
started = 1
}
// The view was read before the start wrote: when the pipeline was held back for it and it has reached the revision since,
// it is placed now, not left for an advance that already came (A-448, §200).
if !held && ok && ev.Revision > 0 && s.Store != nil {
if again, held := view(ev.Branch); held && again.Revision >= ev.Revision {
if _, werr := s.Waiting(s.Store, ev.Branch, view); werr != nil && err == nil {
err = werr
}
}
}
return started, err
}
// Release places the movers of pipeline key whose awaited publishes are all heard now (design 47 H §746, H-A-84). It says
// how many pipelines it placed in.
func (s *Starter) Release(store Updater, key string, view func(branch string) (Half, bool)) (int, error) {
if s.Registration == nil {
return 0, nil
}
return s.placeWaiting(store, func(p seat.Pipeline) bool {
if p.Pipeline != key {
return false
}
for _, m := range p.Movers {
if !placed(p, m.Module) {
return true
}
}
return false
}, s.Registration, view, "the publishes its movers waited for")
}
// heardPublished writes a package-published into the publishing mover's published, in the running pipeline of the source
// revision that published it, and places the movers that waited for it (design 47 H §746, H-A-84). It answers that
// source's pipeline, read, and whether one is held.
func (s *Starter) heardPublished(ev builds.PackagePublished, view func(branch string) (Half, bool)) (seat.Pipeline, bool, error) {
if s.Store == nil {
return seat.Pipeline{}, false, nil
}
key := Key(ev.Branch, ev.Source)
for {
p, rev, err := s.Store.Get(key)
if err != nil {
return seat.Pipeline{}, false, nil
}
if p.Outcome != seat.PipelineOutcomeRunning || !Moves(p, ev.Module) {
return p, true, nil
}
i := slices.IndexFunc(p.Movers, func(m seat.Mover) bool { return m.Module == ev.Module })
if slices.Contains(p.Movers[i].Published, ev.Artifact) {
return p, true, nil
}
p.Movers[i].Published = append(p.Movers[i].Published, ev.Artifact)
if _, err := s.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
continue
} else if err != nil {
return p, true, fmt.Errorf("the pipeline %s cannot be written: %w", key, err)
}
_, err = s.Release(s.Store, key, view)
return p, true, err
}
}
// publishedKind is the edge a dependent builds on a published package by: B published before A builds, a package A
// imports (§53).
const publishedKind = 1
// Published hears a package-published (design 47 H §747, H-A-52): in the running pipeline of the source revision that
// published it, a mover that builds on it waits for it there (§746) and nothing starts; every module the catalog half
// holds a kind-1 edge from the package into that this pipeline does not move (a publish again of an older build, a
// dependent it does not move) is a mover of one pipeline keyed `<branch>.<source>.published.<package>.<version>`, at the
// publishing source's catalog revision. The dependents come from the edges the runner holds, never from a comparison.
func (s *Starter) Published(ev builds.PackagePublished, view func(branch string) (Half, bool)) (int, error) {
if ev.Artifact == "" || ev.Branch == "" || ev.Source <= 0 {
return 0, fmt.Errorf("a package-published names its artifact, watched branch and source revision: %+v", ev)
}
publishing, held, err := s.heardPublished(ev, view)
if err != nil {
return 0, err
}
half, ok := view(ev.Branch)
if !ok {
return 0, fmt.Errorf("the catalog half of %s is not held yet: the publish of %s %s is heard again once it is", ev.Branch, ev.Artifact, ev.Version)
}
var movers []seat.Mover
for _, module := range slices.Sorted(maps.Keys(half.Entries)) {
e := half.Entries[module]
if module == ev.Module || Moves(publishing, module) || !slices.ContainsFunc(e.Edges, func(x catalog.Edge) bool {
return x.Kind == publishedKind && x.From == ev.Artifact
}) {
continue
}
movers = append(movers, seat.Mover{Module: module, Commit: e.Commit, Fingerprint: e.Fp, Repository: e.Repository})
}
if len(movers) == 0 {
return 0, nil
}
revision := half.Revision
if held && publishing.Revision > 0 {
revision = publishing.Revision
}
movers = Tiers(movers, half.Entries, s.logf)
started, err := s.Start(Begin{Branch: ev.Branch, Source: ev.Source, Call: publishing.Call, Revision: revision, Movers: movers,
Entries: half.Entries, Published: &seat.Published{Artifact: ev.Artifact, Version: ev.Version},
Key: PublishedKey(ev.Branch, ev.Source, ev.Artifact, ev.Version)})
if started {
return 1, err
}
return 0, err
}