Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/start.go
T
jschoubben 9afc8a160d H-A-506 runner: every ask put has an ask key; its events read that key, never the pipelines
An ask-taken or step-progress of an ask with no test-run key listed and read
every pipeline (about 3,850), 1 to 2 s per event on the runner's one consumer.
Now every ask the runner puts gets its ask key at the put, naming its
pipeline, mover, step and run: the test run's ask key of H-A-500 and the key
of every other ask are merged into this one form, `ask.<ask>` in test-runs
(a key written before holds its run alone and still reads). Taken, RunTaken
and RunProgress read that key and the one pipeline it names; an event whose
ask has no key is acknowledged at once, counted in H-A-500's one journal line
per hundred. The key goes with the pipeline's end or its run's end, so a late
ask-taken after the end clears no finding (H-A-506 second addendum).

The drops of an ended pipeline's asks are asked beside the end (the Hearer's
Aside): a seat with no holder answers a kept call only at its bound, which
held the cancel verb's answer past its caller's wait in the queue
integration test.

Builds: design 47 H2 H-A-506, H-A-506 addendum, H-A-506 second addendum
2026-11-22 23:57:08 +01:00

1331 lines
57 KiB
Go

package pipeline
import (
"encoding/json"
"errors"
"fmt"
"slices"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"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 catalog revision, design 47 H §746, H-A-213), 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
// Key is the pipeline's key when it is not <branch>.<revision>; empty for every start the runner makes.
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 pipeline's key in the state pipelines, `<branch>.<revision>`, the catalog revision's number the indexer counts,
// the branch escaped as a key's name is (design 47 H §746, H-A-213 (4)); a take-in's number is unchanged where it equalled
// its source. A pipeline kept under its source before (`<branch>.<source>`) has the same form and is read as it is.
func Key(branch string, revision int64) string {
return seat.PipelinesByBranchRevisionKey(keys.EscapeName(branch), strconv.FormatInt(revision, 10))
}
// subjectRevision is the number p's pipeline-started, pipeline-ended and waits are said under, <branch>.<revision>: its
// key's number (H-A-221).
func subjectRevision(p seat.Pipeline) int64 {
return keyRevision(Begin{Revision: p.Revision, Source: p.Source})
}
// keyRevision is the number b's pipeline is keyed by (H-A-213 (4)): its revision, or its source where the start names no
// revision (a take-in.taken of an indexer whose revision numbers were its sources, H-A-211).
func keyRevision(b Begin) int64 {
if b.Revision > 0 {
return b.Revision
}
return b.Source
}
// keyForms are the filters of every key form of the state pipelines: a revision's, a run's, a publish's kept from before
// a release revision started one pipeline (H-A-213 (2)), an unfinished mover's pipeline of its own (H-A-398), and a pipeline
// started again at the head (H-A-479).
var keyForms = []string{"*.*", "*." + runToken + ".*", "*.*.published.*.*", "*.*." + unfinishedToken + ".*", "*.*." + againToken + ".*"}
// againToken is the third part of the key of a pipeline started again at the head by start with modules,
// `<branch>.<revision>.again.<n>` (design 47 H2 H-A-479 addendum (1)).
const againToken = "again"
// AgainPipelineKey is the key of the n-th pipeline started again at revision of branch, `<branch>.<revision>.again.<n>`
// (H-A-479 addendum (1)).
func AgainPipelineKey(branch string, revision int64, n int) string {
return seat.PipelinesByBranchRevisionAgainNKey(keys.EscapeName(branch), strconv.FormatInt(revision, 10), strconv.Itoa(n))
}
// 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
}
// notBuiltWhy is the why of a step placed not asked because dependency failed did not build (H-A-101 (2)).
func notBuiltWhy(failed string) string { return "its dependency " + failed + " did not build" }
// notBuiltDependency answers the dependency a step placed not asked names as not built (notBuiltWhy), if it is one.
func notBuiltDependency(st seat.StepResult) (string, bool) {
if st.Outcome != seat.StepResultOutcomeNotAsked {
return "", false
}
d, ok := strings.CutPrefix(st.Why, "its dependency ")
if !ok {
return "", false
}
d, ok = strings.CutSuffix(d, " did not build")
return d, ok && d != ""
}
// 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)
// RegistrationsRead says whether the registrations view's first read is in (design 47 H2 §788): before it no
// registration-missing is judged and "registrations not yet read" is said once; nil reads as read.
RegistrationsRead func() bool
// RootTree reads a commit's root tree hash through the seat git's trees: a source-only check's fingerprint (design 47
// H2 §815, H-A-408).
RootTree func(repository, commit string) (string, error)
unreadSaid atomic.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
// 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)
// Left takes the steps a same-source re-pin superseded in a pipeline that kept their mover out of the test runs they
// read and drops their own asks (H-A-283, H-A-284 (1); Hearer.LeftInPlace); nil leaves them.
Left func(key string, leave, drop []seat.StepResult, why string)
// Revision reads a catalog revision of a branch by its number through the catalog seat's revision verb: a mover a
// same-source re-pin kept is placed from the entries of the revision it is kept at (H-A-286); nil leaves such a mover
// waiting.
Revision func(branch string, revision int64) (Half, 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
// Asks bounds every ask but a test run's until its holder's first word (H-A-235, H-A-238); nil bounds none.
Asks *AskClock
// 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)
// Findings is the runner's finding writer, through which a mover waiting on its registration raises
// registration-missing (H-A-262); Observer is the runner's own module name. nil raises none.
Findings Findings
Observer string
// AskPublished asks the seat mesh-builds' verb published whether a package built at a fingerprint is published (H-A-326
// (1), H-A-328): read when a mover's wait on it is written, so a publish made before the wait places the mover without
// any event; nil reads nothing, the publish event alone placing it.
AskPublished func(in builds.PublishedInput) (builds.PublishedAnswer, error)
// Unfinished puts the movers an earlier pipeline left unfinished in the pipeline of the branch's next catalog revision
// (design 47 H2 §786); nil puts none.
Unfinished *Unfinished
publishedMu sync.Mutex
publishedRead map[string]builds.PublishedAnswer
}
// readPublished asks, once per pipeline, package and fingerprint, the seat mesh-builds whether each package mover waits on
// in p is published already (H-A-326 (1), H-A-328): a wait is a read first and an event second. Each package answered
// published goes into its From mover's published in p, written with the placing, and the journal says the wait resolved
// from the build seat's published, apart from one a package-published places (H-A-326 (3)). The answer is kept for the
// placing's compare-and-set retries, so no retry asks again. It says whether it added any.
func (s *Starter) readPublished(p *seat.Pipeline, mover string, waiting []string) bool {
if s.AskPublished == nil {
return false
}
added := false
for _, ref := range waiting {
i := slices.IndexFunc(p.Movers, func(m seat.Mover) bool { return m.Module == moduleOf(ref) })
if i < 0 || slices.Contains(p.Movers[i].Published, ref) {
continue
}
in := builds.PublishedInput{Module: p.Movers[i].Module, Artifact: ref, Fingerprint: p.Movers[i].Fingerprint}
key := p.Pipeline + "\x00" + ref + "\x00" + in.Fingerprint
s.publishedMu.Lock()
a, asked := s.publishedRead[key]
s.publishedMu.Unlock()
if !asked {
var err error
if a, err = s.AskPublished(in); err != nil {
s.logf("%s: whether %s at %s is published is not read, so %s waits for its publish event: %v", p.Pipeline, ref, in.Fingerprint, mover, err)
continue
}
s.publishedMu.Lock()
if s.publishedRead == nil {
s.publishedRead = map[string]builds.PublishedAnswer{}
}
s.publishedRead[key] = a
s.publishedMu.Unlock()
}
if !a.Published {
continue
}
p.Movers[i].Published = append(p.Movers[i].Published, ref)
added = true
s.logf("%s: %s's wait on %s is resolved from the build seat's published (at %s), not from an event: placed from the published record (H-A-326)", p.Pipeline, mover, ref, a.Version)
}
return added
}
// forgetPublished drops the published answers readPublished kept for pipeline key, at its end (H-A-328).
func (s *Starter) forgetPublished(key string) {
s.publishedMu.Lock()
defer s.publishedMu.Unlock()
for k := range s.publishedRead {
if strings.HasPrefix(k, key+"\x00") {
delete(s.publishedRead, k)
}
}
}
// 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) && !pinHeld(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
}
// keptGone ends p failed at its placing, "kept revision <r> gone", when the catalog no longer holds revision r a mover of
// it is kept at (design 47 H2 H-A-496): each step not ended ends not asked, so its movers, whose steps never ran, are left
// unfinished and rejoin the next pipeline (§786), and its end frees its turn under pipelines-at-once. It answers the steps
// it ended, none when it had none; the pipeline's Ended says it ended.
func keptGone(p *seat.Pipeline, r int64, err error, now time.Time) []int {
why := fmt.Sprintf("kept revision %d gone", r)
ended := []int{}
for i := range p.Steps {
st := &p.Steps[i]
if !open(*st) && st.Outcome != seat.StepResultOutcomeWaiting {
continue
}
st.Outcome, st.Why, st.Ended, st.Ask, st.Shared = seat.StepResultOutcomeNotAsked, "the pipeline failed: "+why, &now, "", nil
p.Unlogged = append(p.Unlogged, endEntry(p.Pipeline, *st, now))
ended = append(ended, i)
}
p.Outcome, p.Why, p.Ended = seat.PipelineOutcomeFailed, fmt.Sprintf("%s (%v)", why, err), &now
p.EndCount++
return 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.armAsk(key), s.logf)
}
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{}}
}
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, and kept, every
// catalog revision its movers are kept at (design 47 H2 H-A-496), so the indexer pins each while the pipeline runs.
func started(p seat.Pipeline, at time.Time, kept []int64) 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, Kept: kept}
}
// keptList is every distinct revision of kept-revisions, in order (H-A-496).
func keptList(kept map[string]int64) []int64 {
var out []int64
for _, r := range kept {
if r > 0 && !slices.Contains(out, r) {
out = append(out, r)
}
}
slices.Sort(out)
return out
}
// sayStartedPipeline says p's pipeline-started on <branch>.<revision> (§746), with the revisions its movers are kept at as
// its record's kept-revisions holds them (H-A-496): a re-pin says it again with the full list (H-A-496 addendum).
func (s *Starter) sayStartedPipeline(p seat.Pipeline, at time.Time) {
if s.Say == nil {
return
}
kept, err := keptOfStore(s.Store, p.Pipeline)
if err != nil {
s.logf("the kept-revisions of %s are not read, so its pipeline-started names no kept revision: %v", p.Pipeline, err)
}
if err := s.Say(runnerclient.RevisionSubject(seat.EventPipelineStarted, p.Branch, subjectRevision(p)), started(p, at, keptList(kept))); 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
var waits []seat.Mover
kept, err := keptOfStore(s.Store, p.Pipeline)
if err != nil {
s.logf("the kept-revisions of %s are not read, so its movers are placed as movers not kept: %v", p.Pipeline, err)
}
var gone int64
var goneWhy error
all := entries
for i, m := range p.Movers {
// A mover that left for a newer pipeline is placed there, never here (H-A-223, H-A-361).
if placed(*p, m.Module) || slices.Contains(p.Superseded, m.Module) {
continue
}
r, ok := reg(m.Module)
if !ok {
unregistered = append(unregistered, m.Module)
continue
}
// A mover a same-source re-pin kept is placed from the revision it is kept at, its ids its own (H-A-286).
entries := all
if rev := kept[m.Module]; rev > 0 {
var err error
if entries, err = s.keptEntries(p.Branch, rev); errors.Is(err, ErrRevisionGone) {
// A kept revision the catalog no longer holds is never read: the pipeline ends failed (H-A-496).
if gone == 0 {
gone, goneWhy = rev, err
}
continue
} else if err != nil {
// It waits for its kept revision to be read, said once and carried on the mover (design 47 H2 §787).
if m.Unplaced != seat.MoverUnplacedKeptRevisionUnread || m.Revision != rev {
s.logf("%s: %s waits for its kept revision %d to be read (%v)", p.Pipeline, m.Module, rev, err)
p.Movers[i].Unplaced, p.Movers[i].Revision = seat.MoverUnplacedKeptRevisionUnread, rev
}
continue
}
}
p.Movers[i].Unplaced, p.Movers[i].Revision = "", 0
// 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.readPublished(p, m.Module, waiting) {
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, ", "))
waits = append(waits, m)
continue
}
steps, a := place(*p, m, r, entries, now, s.Name, true, kept[m.Module])
supersededWhy(*p, m.Module, entries, steps)
for _, st := range steps {
if pinHeld(st) {
s.logf("%s: %s's %s of %s is not asked: it %s, as its build does (H-A-206)", p.Pipeline, m.Module, st.Step, st.Artifact, st.Why)
}
}
if failed != "" {
why := notBuiltWhy(failed)
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)
}
}
}
if gone > 0 {
return nil, unregistered, keptGone(p, gone, goneWhy, now), notAsked
}
if steps, ended := cycled(p, waits, unregistered, reg, all, now, s.Name); ended {
return nil, unregistered, steps, notAsked
} else {
notAsked = append(notAsked, steps...)
}
// A mover placed after the validate ended has its composition asked, or not asked, at once (§745).
more, _ := releaseCompositions(p, now, kept)
asks = append(asks, more...)
asks, ended := s.holderless(p, asks, now)
return asks, unregistered, ended, notAsked
}
// cycled ends not asked, at p's placing, the steps of every mover still without steps when each waits only on movers
// without steps (H-A-175, H-A-176; G §270 and A-326 break a build module's own inputs before this): a cycle no build can
// start, and the movers that wait only on it. Their steps are placed ended not asked (as H-A-101 (2) does), the why
// `cycle` naming each artifact and edge kind whose two movers share a strongly connected group; no step already asked is
// touched. The pipeline then ends by the end judgement of H-A-163 (endPipeline), failed once no required step is open,
// its running steps running to their own end. It answers the steps it ended not asked, or, when the pipeline ended, every
// step it ended, and whether the pipeline ended.
func cycled(p *seat.Pipeline, waits []seat.Mover, unregistered []string, reg func(module string) (catalog.Registration, bool), entries map[string]catalog.CatalogEntry, now time.Time, name string) ([]int, bool) {
if len(waits) == 0 || len(unregistered) > 0 {
return nil, false
}
after := map[string][]string{}
edges := map[string][]catalog.Edge{}
for _, m := range waits {
for _, x := range awaited(*p, m.Module, entries) {
from := moduleOf(x.From)
if placed(*p, from) {
return nil, false
}
after[m.Module] = append(after[m.Module], from)
edges[m.Module] = append(edges[m.Module], x)
}
}
comp := components(waits, after)
var on []string
for _, m := range waits {
for _, x := range edges[m.Module] {
if comp[moduleOf(x.From)] == comp[m.Module] {
on = append(on, fmt.Sprintf("%s before %s (kind %d)", x.From, x.To, x.Kind))
}
}
}
sort.Strings(on)
why := "cycle: " + strings.Join(on, "; ")
var ended []int
for _, m := range waits {
r, _ := reg(m.Module)
steps, _ := Place(*p, m, r, entries, now, name)
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))
ended = append(ended, len(p.Steps)+k)
}
p.Steps = append(p.Steps, steps...)
}
var approvals []int
for i, st := range p.Steps {
if st.Step == seat.StepResultStepApproval && st.Outcome == seat.StepResultOutcomeWaiting {
approvals = append(approvals, i)
}
}
if !endPipeline(p, now, nil) {
return ended, false
}
for _, i := range approvals {
if p.Steps[i].Outcome == seat.StepResultOutcomeNotAsked {
ended = append(ended, i)
}
}
return ended, true
}
// 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 {
if err := sayStepEnded(s.logf, s.Say, p, p.Steps[i], at); 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)
}
// PutAgain puts one step's ask again under its new id, with its kept results and toolchain and its bound armed (a declined
// step's, design 47 H2 H-A-445): never joined to a test run, whose ask it is already.
func (s *Starter) PutAgain(key string, a Asked) error {
if s.Steps == nil {
return errors.New("the runner puts no ask")
}
s.carry(key, []Asked{a})
return askAll(s.Steps, key, []Asked{a}, func(commit string) bool { return s.recovered(key, commit) }, s.armAsk(key), s.logf)
}
// 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, keyRevision(b))
}
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)
s.registrationMissing(m, key)
}
}
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)
// A mover whose fingerprint this revision moved leaves every older running pipeline of the branch (H-A-223).
if s.Supersede != nil {
s.Supersede(p)
}
// ... and every older waiting one, whether this one waits or is placed at once (H-A-244 (1)).
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)
}
if !place {
s.sayQueueWait(seat.EventWaitHeld, p, now, "")
s.Queue.overCap(key, s.logf)
return true, nil
}
s.ran(key)
s.sayNotAsked(p, notAsked)
if len(ended) > 0 || p.Ended != nil {
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, still
// running and not taken by a holder (design 47 G §88's restart pickup; H2 §777, H-A-397): 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 {
// A taken ask's put reached its holder, which takes an id once: only an ask no holder took is put again
// (design 47 H2 §777, H-A-397).
if open(st) && st.Ask != "" && st.TakenAt == nil {
running[st.Ask], own[st.Ask] = true, len(st.Shared) == 0
}
}
var due []Asked
kept, err := keptOfStore(store, key)
if err != nil {
s.logf("the kept-revisions of %s are not read, so its kept movers' steps are not asked again: %v", key, err)
}
for _, m := range p.Movers {
reg, ok := s.Registration(m.Module)
if !ok {
continue
}
// A kept mover's steps are found by the ids its kept revision gives, from that revision's entries (H-A-286).
entries, rev := half.Entries, kept[m.Module]
if rev > 0 {
var err error
if entries, err = s.keptEntries(p.Branch, rev); err != nil {
s.logf("%s: revision %d of %s, which %s is kept at, is not read (%v): its steps are asked again once it is", key, rev, p.Branch, m.Module, err)
continue
}
}
_, shared := place(p, m, reg, entries, now, s.Name, true, rev)
_, unshared := place(p, m, reg, entries, now, s.Name, false, rev)
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
}
// Unplaced says why mover module of p has no step placed (H-A-171): "<module> has no registration" when the registrations
// do not name it (A-327), else "<module> waits on the publish of <package>, …", the packages it awaits not yet published
// as the catalog half of p's branch holds its edges (§746).
func (s *Starter) Unplaced(p seat.Pipeline, module string, view func(branch string) (Half, bool)) string {
// A kept mover waiting for its kept revision says so (design 47 H2 §787).
if m, ok := MoverOf(p, module); ok && m.Unplaced == seat.MoverUnplacedKeptRevisionUnread {
return fmt.Sprintf("%s waits for its kept revision %d to be read", module, m.Revision)
}
if s.Registration == nil {
return module + " has no registration"
}
if _, ok := s.Registration(module); !ok {
return module + " has no registration"
}
var waiting []string
if view != nil {
if half, ok := view(p.Branch); ok {
_, waiting, _ = ready(p, module, half.Entries)
}
}
if len(waiting) == 0 {
return module + " waits on the publish of the movers it builds on"
}
return module + " waits on the publish of " + strings.Join(waiting, ", ")
}
// 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
}
defer s.registrationArrived(reg.Module, "the registration of "+reg.Module+" arrived") // the finding of a mover that waited on it goes (H-A-262)
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
}
defer s.JudgeRegistrationMissing(store) // the finding of a mover registered now, or no longer waited on, goes (H-A-262, H-A-265 (4))
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, asks, ended, notAsked, n, err := s.placeOne(store, key, due, reg, view, now)
if err != nil {
note(err)
}
// A pipeline its placing ended (a kept revision gone, H-A-496) is said ended though it placed no step.
if n == 0 && ended == nil {
continue
}
count++
if n > 0 {
s.logf("%s: %d steps placed from %s (A-327)", key, n, 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 ended != nil {
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
}
// placeOne places, in pipeline key, the steps of each mover with none, when the pipeline is running, not a run on request
// and due names it (placeWaiting). Its write is by compare-and-set: a write refused because the pipeline was written
// meanwhile is done again at once from the record as read again, until it lands or the pipeline no longer waits on a
// placing (it ended, or every mover due is placed), so a concurrent write never cuts a placing for good (H-A-311 (1)). It
// answers the pipeline as written, its asks, the steps ended or not asked at the placing, and how many steps it placed.
func (s *Starter) placeOne(store Updater, key string, due func(seat.Pipeline) bool, reg func(module string) (catalog.Registration, bool), view func(branch string) (Half, bool), now time.Time) (seat.Pipeline, []Asked, []int, []int, int, error) {
for {
p, rev, err := store.Get(key)
if err != nil {
return p, nil, nil, nil, 0, fmt.Errorf("the pipeline %s is not read: %w", key, err)
}
if p.Outcome != seat.PipelineOutcomeRunning || p.Run != "" || !due(p) {
return p, nil, nil, nil, 0, nil
}
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)
return p, nil, nil, nil, 0, nil
}
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)
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).
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)
s.registrationMissing(m, key)
}
if len(p.Steps) == before && p.Ended == nil {
// A mover that now waits for its kept revision, or no longer does, is written so (design 47 H2 §787).
if unplacedMarks(p) == marks {
return p, nil, nil, nil, 0, nil
}
if _, err := store.Update(key, p, rev); errors.Is(err, ErrChanged) {
continue
} else if err != nil {
return p, nil, nil, nil, 0, fmt.Errorf("the movers of %s are not written: %w", key, err)
}
return p, nil, nil, nil, 0, nil
}
if _, err := store.Update(key, p, rev); errors.Is(err, ErrChanged) {
s.logf("%s is written meanwhile, so its placing is done again from the record read again (H-A-311): %v", key, err)
continue
} 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
}
}
// unplacedMarks is what p's movers carry of why they have no step placed (design 47 H2 §787), one line.
func unplacedMarks(p seat.Pipeline) string {
var b strings.Builder
for _, m := range p.Movers {
if m.Unplaced != "" {
fmt.Fprintf(&b, "%s %s %d;", m.Module, m.Unplaced, m.Revision)
}
}
return b.String()
}
// 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 ev.Branch == "" || ev.Source <= 0 {
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
}
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})
}
// The revision the take-in kept carries the movers an earlier pipeline left unfinished (design 47 H2 §786), judged
// against that revision's own entries.
if !ev.Skipped && ev.Revision > 0 {
movers = s.carryUnfinished(ev.Branch, ev.Revision, half, held, movers)
}
if len(movers) == 0 {
s.logf("the take-in of %s source %d names no entries: it starts no pipeline", ev.Branch, ev.Source)
return 0, nil
}
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
}
// carryUnfinished adds to the movers of revision of branch the unfinished movers whose fingerprint is still their entry's
// there (§786): half when it is that revision, else the revision read by its number; an unread revision carries none, said.
func (s *Starter) carryUnfinished(branch string, revision int64, half Half, held bool, movers []seat.Mover) []seat.Mover {
if s.Unfinished == nil {
return movers
}
if !held || half.Revision != revision {
if s.Revision == nil {
s.logf("revision %d of %s is not read, so it carries no unfinished mover: the runner holds no revision reader", revision, branch)
return movers
}
read, err := s.Revision(branch, revision)
if err != nil {
s.logf("revision %d of %s is not read, so it carries no unfinished mover: %v", revision, branch, err)
return movers
}
half = read
}
return s.Unfinished.Carry(branch, revision, half.Entries, movers)
}
// 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) && !slices.Contains(p.Superseded, 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 catalog
// revision that published it, found by the branch and revision the publish names (H-A-218), and places the movers that
// waited for it (design 47 H §746, H-A-84). It answers that 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, ok := Key(ev.Branch, ev.Revision), true
if ev.Revision <= 0 {
// A publish of an older builder names no revision: its source's lowest-numbered revision, the take-in's (H-A-218).
key, ok = s.sourcePipeline(ev.Branch, ev.Source)
}
if !ok {
return seat.Pipeline{}, false, nil
}
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
}
}
// sourcePipeline is the key of branch's pipeline of the lowest-numbered revision of source, the take-in's (H-A-213 (4),
// H-A-218): a pipeline is keyed by its revision, so one of a source is found by the source it names, never by its key.
func (s *Starter) sourcePipeline(branch string, source int64) (string, bool) {
pkeys, err := s.Store.Keys(keys.EscapeName(branch) + ".*")
if err != nil {
s.logf("the pipelines of %s are not listed, so source %d's is not found: %v", branch, source, err)
return "", false
}
found, low := "", int64(0)
for _, k := range pkeys {
p, _, err := s.Store.Get(k)
if err != nil || p.Branch != branch || p.Source != source || p.Run != "" {
continue
}
if r := keyRevision(Begin{Revision: p.Revision, Source: p.Source}); found == "" || r < low {
found, low = k, r
}
}
return found, found != ""
}
// Published hears a package-published (design 47 H §746, H-A-52): in the running pipeline of the revision that published
// it, a mover that builds on it waits for it there and is placed once it is heard. It starts no pipeline: a re-pin on a
// module's release is a catalog revision of its own, whose pipeline Revised starts (H-A-212, H-A-213 (2)).
func (s *Starter) Published(ev builds.PackagePublished, view func(branch string) (Half, bool)) error {
if ev.Artifact == "" || ev.Branch == "" || ev.Source <= 0 {
return fmt.Errorf("a package-published names its artifact, watched branch and source revision: %+v", ev)
}
_, _, err := s.heardPublished(ev, view)
return err
}
// revisedKinds are the call kinds of a catalog revision whose pipeline Revised starts: every kind but a take-in's, whose
// pipeline its take-in.taken starts (H-A-213 (2), H-A-217).
var revisedKinds = []string{catalog.CatalogRevisionEventCallKindRelease, catalog.CatalogRevisionEventCallKindTakeInAgain,
catalog.CatalogRevisionEventCallKindRecompute}
// Revised starts, on a catalog-revision of the trunk kind whose call kind is release, take-in again or recompute, ONE
// pipeline keyed <branch>.<revision> by compare-and-set on its absence (design 47 H §746, H-A-212, H-A-213 (2) and (4),
// H-A-217): the revision is read by its number through the catalog seat's revision verb, never the newest half, and its
// movers are the revision's movers, its steps placed from the revision's entries. A revision of another kind, or one with
// no mover, starts nothing; a second hearing writes nothing.
func (s *Starter) Revised(ev catalog.CatalogRevisionEvent, read func(branch string, revision int64) (Half, error)) (int, error) {
if ev.Kind != "trunk" || !slices.Contains(revisedKinds, ev.CallKind) {
return 0, nil
}
branch, err := keys.UnescapeName(ev.Branch)
if err != nil || branch == "" || ev.Revision <= 0 {
return 0, fmt.Errorf("a catalog-revision names its watched branch and revision: branch %q revision %d", ev.Branch, ev.Revision)
}
if read == nil {
return 0, fmt.Errorf("the revision %d of %s cannot be read: the runner holds no revision reader", ev.Revision, branch)
}
half, err := read(branch, ev.Revision)
if err != nil {
return 0, fmt.Errorf("the revision %d of %s is not read, so its pipeline is not started: %w", ev.Revision, branch, err)
}
if half.Revision != ev.Revision {
return 0, fmt.Errorf("the revision verb answered revision %d of %s for %d", half.Revision, branch, ev.Revision)
}
movers := make([]seat.Mover, 0, len(half.Movers))
for _, e := range half.Movers {
movers = append(movers, seat.Mover{Module: e.Module, Commit: e.Commit, Fingerprint: e.Fingerprint, Repository: e.Repository})
}
// A revision of the same source moves no mover out of the open pipeline of that source holding it: that pipeline keeps
// it, and this revision starts no pipeline of its own for it (H-A-283); only a revision of a new source moves movers.
if len(movers) > 0 {
if movers = s.keepSameSource(branch, half, movers); len(movers) == 0 {
s.logf("the revision %d of %s (%s) moves only movers the open pipelines of its source %d keep (H-A-283)", ev.Revision, branch, ev.Call, half.Source)
}
}
// It carries the movers an earlier pipeline left unfinished, or starts a pipeline for them alone (design 47 H2 §786).
movers = s.carryUnfinished(branch, half.Revision, half, true, movers)
if len(movers) == 0 {
s.logf("the revision %d of %s (%s) moves no mover: it starts no pipeline", ev.Revision, branch, ev.Call)
return 0, nil
}
movers = Tiers(movers, half.Entries, s.logf)
call := half.Call
if call == "" {
call = ev.Call
}
ok, err := s.Start(Begin{Branch: branch, Source: half.Source, Call: call, Revision: half.Revision, Movers: movers, Entries: half.Entries})
if ok {
return 1, err
}
return 0, err
}