Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/run.go
T
jschoubben bb930de377 R12: the queue's approval wait, stall and cap
A pipeline whose every step not ended is an approval asked of the operator leaves the bound: its
running key goes and the next waiting is placed; answered and still running, it is keyed running
at once, over the bound. After every placing, pipelines waiting with none running raise the finding
pipelines-stalled naming the first waiting, cleared once one runs. A start beyond
pipelines-waiting-cap is an ordinary waiting pipeline and raises queue-full naming it, cleared when
an end brings the waiting back to the cap. Both findings are the scope delivery's, the kind as id,
warnings with no owner.

Builds: design 47 H §748; lead answers H-A-14, H-A-36, H-A-52, H-A-106

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

278 lines
11 KiB
Go

package pipeline
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"maps"
"slices"
"strings"
"time"
runnerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient"
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"
"git.novox.be/novox/mesh-sdk/go/keys"
)
// RunGroupPrefix is a run on request's candidate group before its id, `run-<id>` (design 47 G A-160).
const RunGroupPrefix = "run-"
// runToken is the key part that names a run on request's pipeline, `<branch>.run.<id>` (design 47 H H-A-83).
const runToken = "run"
// RunKey is the key of a run on request's one pipeline, `<branch>.run.<id>`, the branch escaped as a key's name is (design
// 47 H H-A-83).
func RunKey(branch, run string) string {
return seat.PipelinesByBranchRunRunKey(keys.EscapeName(branch), run)
}
// NewRunID is a run's id, a random 12-hex id, so two runs of one commit are two pipelines (A-180).
func NewRunID() (string, error) {
b := make([]byte, 6)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
// Run starts a run on request (design 47 G §64, §420; A-180; H H-A-83, H-A-85): it writes the run's one pipeline at once,
// running and with no steps, its movers the members, its `run` the run's new id and its source the newest source revision
// of its branch as view holds it, and says run-started, from which the indexer builds the run's candidate; the candidate's
// revision places the steps (PlaceRun). A member naming no module, commit or branch, or members naming two branches, is
// refused, the run then writing nothing. caller is the one the bus proved. newID makes the run's id (NewRunID).
func (s *Starter) Run(caller string, in seat.RunInput, newID func() (string, error), view func(branch string) (Half, bool)) (seat.RunAnswer, error) {
if len(in.Members) == 0 {
return seat.RunAnswer{}, errors.New("a run names one or more members, each a module at a commit on a branch")
}
branch := in.Members[0].Branch
var movers []seat.Mover
for _, m := range in.Members {
if m.Module == "" || m.Commit == "" || m.Branch == "" {
return seat.RunAnswer{}, fmt.Errorf("a run's member names its module, commit and branch: %+v", m)
}
if m.Branch != branch {
return seat.RunAnswer{}, fmt.Errorf("a run is one pipeline of one branch: its members name %s and %s", branch, m.Branch)
}
if Moves(seat.Pipeline{Movers: movers}, m.Module) {
return seat.RunAnswer{}, fmt.Errorf("a run names %s once", m.Module)
}
movers = append(movers, seat.Mover{Module: m.Module, Commit: m.Commit})
}
// Its source is the branch's newest source revision, what the run is checked against (H-A-85): a branch with none yet
// is refused, naming it.
var source int64
if view != nil {
if h, ok := view(branch); ok {
source = h.Source
}
}
if source <= 0 {
return seat.RunAnswer{}, fmt.Errorf("the branch %s has no source revision yet, which a run is checked against: nothing was started", branch)
}
id, err := newID()
if err != nil {
return seat.RunAnswer{}, fmt.Errorf("the run's id is not made: %w", err)
}
now := s.now()
key := RunKey(branch, id)
p := seat.Pipeline{V: 1, Pipeline: key, Branch: branch, Source: source, Movers: movers, Run: id,
Outcome: seat.PipelineOutcomeRunning, Started: &now, Steps: []seat.StepResult{}}
// A run waits in the same queue as every pipeline (§748, H-A-52): over pipelines-at-once it is written waiting, and its
// steps are placed from its candidate's newest revision at its turn (RunTurn).
place, wkey, err := s.queueStart(&p)
if err != nil {
return seat.RunAnswer{}, fmt.Errorf("the run's pipeline %s is not judged against pipelines-at-once: %w", key, err)
}
if err := s.create(key, p); err != nil {
if wkey != "" {
_ = s.dropWaiting(wkey)
}
return seat.RunAnswer{}, fmt.Errorf("the pipeline %s of the run is not written: %w", key, err)
}
if place {
s.ran(key)
} else {
s.logf("the run %s waits its turn under pipelines-at-once, sequence %d", id, p.Seq)
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", branch, key, err)
}
s.Queue.overCap(key, s.logf)
}
first := in.Members[0]
last := first.Commit
if len(in.Members) > 1 {
last = RunGroupPrefix + id
}
ev := seat.RunStarted{V: 1, At: &now, By: caller, Members: in.Members, Run: id}
if err := s.Say(runnerclient.Subject(seat.EventRunStarted, first.Module, first.Branch, last), ev); err != nil {
s.failRun([]string{key}, "run-started was not said, so no candidate is built for it")
return seat.RunAnswer{}, fmt.Errorf("the run %s is not started: run-started is not said: %w", id, err)
}
return seat.RunAnswer{Run: id, Pipelines: []string{key}}, nil
}
// failRun ends the run's pipelines written before its start failed, failed and saying why, so none stands running.
func (s *Starter) failRun(written []string, why string) {
if s.Store == nil {
return
}
for _, key := range written {
for {
p, rev, err := s.Store.Get(key)
if err != nil || p.Outcome != seat.PipelineOutcomeRunning {
break
}
now := s.now()
p.Outcome, p.Why, p.Ended = seat.PipelineOutcomeFailed, why, &now
p.EndCount++
if _, err := s.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
continue
} else if err != nil {
s.logf("the pipeline %s of a run not started stays running: %v", key, err)
}
break
}
}
}
// PlaceRun places a run's pipeline from its candidate's revision (design 47 G §64, §373, A-180; H H-A-83): ev is the
// catalog-revision of the candidate `run-<id>`, half its entries read whole through the seat's revision verb. The run's
// pipeline, not yet placed, takes each member's fingerprint and repository, the candidate's revision, a mover for each
// further entry whose fingerprint the members change (another than in base, the branch's catalog half, or absent there),
// the movers' tiers and their steps, by compare-and-set, and says pipeline-started. Another candidate's revision places
// nothing; a revision heard again places nothing twice. It says how many pipelines it placed.
func (s *Starter) PlaceRun(ev catalog.CatalogRevisionEvent, half Half, base func(branch string) (Half, bool)) (int, error) {
id, ok := strings.CutPrefix(ev.Group, RunGroupPrefix)
if !ok || id == "" {
return 0, nil
}
if s.Store == nil {
return 0, errors.New("the runner holds no pipelines to place a run's in")
}
pkeys, err := s.Store.Keys("*." + runToken + "." + id)
if err != nil {
return 0, fmt.Errorf("the pipeline of the run %s is not listed: %w", id, err)
}
if len(pkeys) == 0 {
s.logf("the candidate %s's revision %d names a run with no pipeline here: nothing is placed", ev.Group, ev.Revision)
return 0, nil
}
placed := 0
var first error
for _, key := range pkeys {
ok, err := s.placeRunPipeline(key, id, ev, half, base)
if err != nil && first == nil {
first = err
}
if ok {
placed++
}
}
return placed, first
}
// UnplacedRun is a run with a pipeline running and no steps placed yet, on the branch the run names.
type UnplacedRun struct{ Run, Branch string }
// UnplacedRuns are the runs with a pipeline running and no steps placed yet, by branch and id (design 47 G A-222): a placing
// lost while the runner was down is taken up from these at its start.
func (s *Starter) UnplacedRuns() ([]UnplacedRun, error) {
if s.Store == nil {
return nil, errors.New("the runner holds no pipelines to read a run's from")
}
pkeys, err := s.Store.Keys("*." + runToken + ".*")
if err != nil {
return nil, fmt.Errorf("the pipelines are not listed: %w", err)
}
var out []UnplacedRun
for _, key := range pkeys {
if p, _, err := s.Store.Get(key); err == nil && p.Run != "" && p.Outcome == seat.PipelineOutcomeRunning && p.Revision == 0 {
if u := (UnplacedRun{Run: p.Run, Branch: p.Branch}); !slices.Contains(out, u) {
out = append(out, u)
}
}
}
slices.SortFunc(out, func(a, b UnplacedRun) int { return strings.Compare(a.Branch+"\n"+a.Run, b.Branch+"\n"+b.Run) })
return out, nil
}
// placeRunPipeline places the run's pipeline, written and not yet placed (no revision), from the candidate's half.
func (s *Starter) placeRunPipeline(key, id string, ev catalog.CatalogRevisionEvent, half Half, base func(branch string) (Half, bool)) (bool, error) {
for {
p, rev, err := s.Store.Get(key)
if err != nil {
return false, fmt.Errorf("the pipeline %s is not read: %w", key, err)
}
if p.Run != id || p.Outcome != seat.PipelineOutcomeRunning || p.Revision != 0 {
return false, nil
}
now := s.now()
movers := make([]seat.Mover, 0, len(p.Movers))
for _, m := range p.Movers {
e, ok := half.Entries[m.Module]
if !ok {
s.logf("the candidate of the run %s holds no entry of %s: its mover is placed with no fingerprint", id, m.Module)
}
m.Fingerprint, m.Repository = e.Fp, e.Repository
movers = append(movers, m)
}
// The dependents (§373): the candidate's entries the members change, beside the members.
trunk, held := base(p.Branch)
if b, err := keys.UnescapeName(ev.Branch); err == nil && b != "" && b != p.Branch {
if h, ok := base(b); ok {
trunk, held = h, true
}
}
if !held {
s.logf("the run %s's dependents are not known: the catalog half of %s is not held, so its members run alone", id, p.Branch)
} else {
for _, module := range slices.Sorted(maps.Keys(half.Entries)) {
e := half.Entries[module]
if Moves(seat.Pipeline{Movers: movers}, module) {
continue
}
if was, ok := trunk.Entries[module]; ok && was.Fp == e.Fp {
continue
}
movers = append(movers, seat.Mover{Module: module, Commit: e.Commit, Fingerprint: e.Fp, Repository: e.Repository})
}
}
p.Movers, p.Revision = Tiers(movers, half.Entries, s.logf), ev.Revision
var asks []Asked
var ended, notAsked []int
if s.Registration != nil {
var unregistered []string
asks, unregistered, ended, notAsked = s.placeMovers(&p, s.Registration, half.Entries, now)
for _, m := range unregistered {
s.logf("%s: %s has no registration yet: its mover is placed with no step", key, m)
}
}
if _, err := s.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
continue
} else if err != nil {
return false, fmt.Errorf("the pipeline %s cannot be written: %w", key, err)
}
s.sayStartedPipeline(p, now)
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
}
}
func (s *Starter) now() time.Time {
if s.Now != nil {
return s.Now().UTC()
}
return time.Now().UTC()
}