Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/read.go
T
jschoubben 19f06be6c1 Runner's list: each row names its branch, every branch's when none is named
The pipelines page lists every watched branch's pipelines together, the
branch a column of each row, so the short row carries the pipeline's branch;
list already answered every branch when none was named, and its description
now says so. The seat mesh-pipeline-runner's form rises to 11 for the field.

Builds: design 47 H2 H-A-533 (hq 1b1e1984)
2026-11-23 11:09:42 +01:00

505 lines
17 KiB
Go

// Package pipeline is the pipeline runner's own logic, holder of the seat mesh-pipeline-runner (design 47 G §21). It names
// no module, branch or repository (§7): every name comes from what it reads.
package pipeline
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"slices"
"sort"
"strconv"
"strings"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// Kept is one of the runner's old state sections as it reads them: every key, and one key's value (nil when absent).
type Kept interface {
Keys() ([]string, error)
Value(key string) (json.RawMessage, error)
}
// Reads answers the runner's read verbs (§83, §306) from its own states pipelines and kept, never another part's.
type Reads struct {
Pipelines Kept
Kept Kept
// Fill is each of the runner's stores' fill now, pipelines, kept and step-log, read through the bundle's calls
// (the state's `mesh/state.info`, the object store's and the log's fill; design 47 G §366, §623, A-214); nil says none.
Fill func() ([]seat.Fill, error)
}
// fills is the stores' fill, or an error saying it could not be read: an answer never leaves it out unsaid.
func (r Reads) fills() ([]seat.Fill, error) {
if r.Fill == nil {
return nil, nil
}
f, err := r.Fill()
if err != nil {
return nil, fmt.Errorf("the stores' fill cannot be read: %w", err)
}
return f, nil
}
// MaxAnswer is the most a read answers (design 47 H2 §789, §811): 768 KB, the rest read from its cursor.
const MaxAnswer = 768 << 10
// Show is one pipeline, with its steps (§83), or, naming a mover, the
// pipeline's record with that mover's steps alone (H-A-187). Its filters mover, step and state apply while it reads, and it
// answers at most MaxAnswer with capped, the filters' total and the cursor of the next page (design 47 H2 §789, §779).
func (r Reads) Show(in seat.ShowInput) (seat.ShowAnswer, error) {
p, ok, err := r.pipeline(in.Pipeline)
if err != nil {
return seat.ShowAnswer{}, err
}
if !ok {
return seat.ShowAnswer{}, fmt.Errorf("no pipeline %q in %s", in.Pipeline, seat.StatePipelines)
}
fills, err := r.fills()
if err != nil {
return seat.ShowAnswer{}, err
}
from, err := pageFrom(in.Next)
if err != nil {
return seat.ShowAnswer{}, err
}
// Named, a mover is answered by its steps alone (H-A-187): one mover's of a pipeline of a hundred movers.
m := strings.TrimSpace(in.Mover)
if m != "" && !Moves(p, m) {
return seat.ShowAnswer{}, fmt.Errorf("the pipeline %s does not move %s", in.Pipeline, m)
}
if m == "" && in.Step == "" && in.State == "" {
// Naming neither a mover nor a filter, show answers each mover's row, never every step of every mover (design 47 H2
// H-A-376), in pages as the steps are.
rows := MoverRows(p)
p.Steps = []seat.StepResult{}
a := seat.ShowAnswer{Pipeline: p, Fill: fills, Total: int64(len(rows)), Movers: []seat.MoverRow{}}
size := sizeOf(a)
for i := from; i < len(rows); i++ {
n := sizeOf(rows[i]) + 1
if size+n > MaxAnswer && len(a.Movers) > 0 {
a.Capped, a.Next = true, pageCursor(i)
break
}
size += n
a.Movers = append(a.Movers, rows[i])
}
return a, nil
}
held := []seat.StepResult{}
for _, s := range p.Steps {
if (m == "" || s.Module == m) && (in.Step == "" || s.Step == in.Step) && (in.State == "" || s.Outcome == in.State) {
held = append(held, s)
}
}
p.Steps = []seat.StepResult{}
a := seat.ShowAnswer{Pipeline: p, Fill: fills, Total: int64(len(held))}
size := sizeOf(a)
for i := from; i < len(held); i++ {
n := sizeOf(held[i]) + 1
if size+n > MaxAnswer && len(a.Pipeline.Steps) > 0 {
a.Capped, a.Next = true, pageCursor(i)
break
}
size += n
a.Pipeline.Steps = append(a.Pipeline.Steps, held[i])
}
return a, nil
}
// pageCursor is the opaque cursor of a page starting at the i-th step the filters hold (§779): the caller hands it back.
func pageCursor(i int) string {
return base64.RawURLEncoding.EncodeToString([]byte("step:" + strconv.Itoa(i)))
}
// pageFrom is the step a cursor starts at, 0 for none; a cursor this reader did not give is refused.
func pageFrom(next string) (int, error) {
if next = strings.TrimSpace(next); next == "" {
return 0, nil
}
raw, err := base64.RawURLEncoding.DecodeString(next)
if err == nil {
if n, ok := strings.CutPrefix(string(raw), "step:"); ok {
if i, err := strconv.Atoi(n); err == nil && i >= 0 {
return i, nil
}
}
}
return 0, fmt.Errorf("the cursor %q is not one show answered", next)
}
// sizeOf is v's size as JSON.
func sizeOf(v any) int {
b, _ := json.Marshal(v)
return len(b)
}
// Movers is one word per step of each mover of a pipeline (design 47 H2 §789), in the record's movers' order: passed,
// failed, running, waiting or not-run.
func (r Reads) Movers(pipeline string) (MoversAnswer, error) {
p, ok, err := r.pipeline(pipeline)
if err != nil {
return MoversAnswer{}, err
}
if !ok {
return MoversAnswer{}, fmt.Errorf("no pipeline %q in %s", pipeline, seat.StatePipelines)
}
a := MoversAnswer{Pipeline: p.Pipeline, Outcome: p.Outcome, Movers: make([]MoverWords, 0, len(p.Movers))}
at := map[string]int{}
for _, m := range p.Movers {
at[m.Module] = len(a.Movers)
a.Movers = append(a.Movers, MoverWords{Module: m.Module, Steps: map[string]string{}})
}
for _, s := range p.Steps {
i, ok := at[s.Module]
if !ok {
continue
}
name := s.Step
if s.Artifact != "" && s.Step != seat.StepResultStepValidate && s.Step != seat.StepResultStepComposition {
name += " " + s.Artifact
}
a.Movers[i].Steps[name] = StepWord(s.Outcome)
}
return a, nil
}
// MoversAnswer is runner_movers' answer: a pipeline's movers, each with one word per step (design 47 H2 §789).
type MoversAnswer struct {
Pipeline string `json:"pipeline"`
Outcome string `json:"outcome"`
Movers []MoverWords `json:"movers"`
}
// MoverWords is one mover's steps, each by its kind (and artifact where it has one) with one word.
type MoverWords struct {
Module string `json:"module"`
Steps map[string]string `json:"steps"`
}
// StepWord is a step outcome's one word (design 47 H2 §789): passed for one that passed, was carried, accepted, bypassed
// or had nothing to run; failed for one that failed, could not run or did not build; running; waiting for one waiting, on
// a build node too; not-run for one not asked, declined, superseded or cancelled.
func StepWord(outcome string) string {
switch outcome {
case seat.StepResultOutcomePassed, seat.StepResultOutcomeCarried, seat.StepResultOutcomeNothingToRun,
seat.StepResultOutcomeAccepted, seat.StepResultOutcomeBypassed:
return "passed"
case seat.StepResultOutcomeFailed, seat.StepResultOutcomeCouldNotRun, seat.StepResultOutcomeDidNotBuild:
return "failed"
case seat.StepResultOutcomeRunning:
return "running"
case seat.StepResultOutcomeWaiting, seat.StepResultOutcomeWaitingForABuildNode:
return "waiting"
}
return "not-run"
}
// ListRows is how many rows list answers when it names no limit (design 47 H2 H-A-376): the newest 20.
const ListRows = 20
// failedKind says an outcome is a step's failure: failed, could not run or did not build.
func failedKind(outcome string) bool {
return outcome == seat.StepResultOutcomeFailed || outcome == seat.StepResultOutcomeCouldNotRun || outcome == seat.StepResultOutcomeDidNotBuild
}
// firstFailed is the first required step of steps that failed or could not run, with its module and why; nil for none
// (design 47 H2 H-A-376).
func firstFailed(steps []seat.StepResult) *seat.FailedStep {
for _, s := range steps {
if s.Status == seat.StepResultStatusRequired && failedKind(s.Outcome) {
return &seat.FailedStep{Module: s.Module, Step: s.Step, Artifact: s.Artifact, Outcome: s.Outcome, Why: s.Why}
}
}
return nil
}
// MoverRows is each mover of p in show's short form (design 47 H2 H-A-376), in the movers' order: its module, its outcome
// (superseded for one a newer pipeline took out; waiting for one with no step placed; failed once a required step did not
// pass; running while a required step is open; else passed) and its first failed step.
func MoverRows(p seat.Pipeline) []seat.MoverRow {
out := make([]seat.MoverRow, 0, len(p.Movers))
for _, m := range p.Movers {
steps := stepsOf(p, m.Module)
row := seat.MoverRow{Module: m.Module, Outcome: seat.MoverRowOutcomePassed, Failed: firstFailed(steps)}
running, failed := false, false
for _, s := range steps {
switch {
case s.Status != seat.StepResultStatusRequired:
case open(s), s.Outcome == seat.StepResultOutcomeWaiting:
running = true
case !stepPassed(s):
failed = true
}
}
switch {
case slices.Contains(p.Superseded, m.Module):
row.Outcome = seat.MoverRowOutcomeSuperseded
case len(steps) == 0:
row.Outcome = seat.MoverRowOutcomeWaiting
case failed:
row.Outcome = seat.MoverRowOutcomeFailed
case running:
row.Outcome = seat.MoverRowOutcomeRunning
}
out = append(out, row)
}
return out
}
// List is the pipelines and runs of a module, a branch, a commit or a run, newest started first (§83), of every branch when
// none is named, each row naming its branch (design 47 H2 H-A-533): by default one
// short row per pipeline, the newest ListRows or Limit (design 47 H2 H-A-376); with Full and a Limit each one's record with
// its movers and steps counted, its steps empty (H-A-191).
func (r Reads) List(in seat.ListInput) (seat.ListAnswer, error) {
if in.Full && in.Limit <= 0 {
return seat.ListAnswer{}, errors.New("list answers the whole pipelines only with a limit")
}
all, err := r.pipelines()
if err != nil {
return seat.ListAnswer{}, err
}
out := []seat.Pipeline{}
steps := map[string][]seat.StepResult{}
for _, p := range all {
if (in.Module == "" || Moves(p, in.Module)) && (in.Branch == "" || p.Branch == in.Branch) &&
(in.Commit == "" || movesCommit(p, in.Commit)) && (in.Run == "" || p.Run == in.Run) {
// Each pipeline's record with its movers and steps counted, never its steps: show gives those (H-A-191); a row
// names its first failed step (H-A-376).
steps[p.Pipeline] = p.Steps
p.MoverCount, p.StepCount = int64(len(p.Movers)), int64(len(p.Steps))
p.Steps, p.Unlogged = []seat.StepResult{}, nil
out = append(out, p)
}
}
sort.SliceStable(out, func(i, j int) bool { return startedOf(out[i]).After(startedOf(out[j])) })
limit := in.Limit
if limit <= 0 {
limit = ListRows
}
if int64(len(out)) > limit {
out = out[:limit]
}
if in.Full {
return seat.ListAnswer{Pipelines: out}, nil
}
rows := make([]seat.PipelineRow, 0, len(out))
for _, p := range out {
// A row carries its source, its revision and its steps counted per outcome (design 47 H2 H-A-467): a page's list is
// one read of the short form.
rows = append(rows, seat.PipelineRow{Pipeline: p.Pipeline, Branch: p.Branch, Call: p.Call, Source: p.Source, Revision: p.Revision,
Started: p.Started, Ended: p.Ended, Outcome: p.Outcome, MoverCount: p.MoverCount, Steps: stepCounts(steps[p.Pipeline]),
Failed: firstFailed(steps[p.Pipeline])})
}
return seat.ListAnswer{Rows: rows}, nil
}
// stepCounts is how many of steps hold each outcome (design 47 H2 H-A-467), nil for none.
func stepCounts(steps []seat.StepResult) map[string]int64 {
if len(steps) == 0 {
return nil
}
out := map[string]int64{}
for _, s := range steps {
out[s.Outcome]++
}
return out
}
// Stands is every step that has started and not ended, with what it waits on and since when (§75, §83): the watchman
// seeds its open waits from it.
func (r Reads) Stands() (seat.StandsAnswer, error) {
all, err := r.pipelines()
if err != nil {
return seat.StandsAnswer{}, err
}
out := []seat.Wait{}
for _, p := range all {
for _, s := range p.Steps {
if s.Ended != nil || s.Started == nil {
continue
}
if !open(s) && s.Outcome != seat.StepResultOutcomeWaiting {
continue
}
m, _ := MoverOf(p, s.Module)
out = append(out, seat.Wait{Pipeline: p.Pipeline, Module: m.Module, Branch: p.Branch, Commit: m.Commit,
Run: p.Run, Source: p.Source, Revision: p.Revision, Fingerprint: m.Fingerprint, Step: s.Step,
Artifact: s.Artifact, On: s.On, Since: *s.Started, V: 1, Ask: askOfWait(s), Shared: sharedOfWait(s)})
}
}
sort.SliceStable(out, func(i, j int) bool { return out[i].Since.Before(out[j].Since) })
return seat.StandsAnswer{Stands: out}, nil
}
// WhyCarried is where a step's result was carried from (§25, §83): the pipeline it names.
func (r Reads) WhyCarried(in seat.WhyCarriedInput) (seat.WhyCarriedAnswer, error) {
p, ok, err := r.pipeline(in.Pipeline)
if err != nil {
return seat.WhyCarriedAnswer{}, err
}
if !ok {
return seat.WhyCarriedAnswer{}, fmt.Errorf("no pipeline %q in %s", in.Pipeline, seat.StatePipelines)
}
for _, s := range p.Steps {
if s.Step != in.Step || (in.Artifact != "" && s.Artifact != in.Artifact) {
continue
}
if s.Outcome != seat.StepResultOutcomeCarried {
return seat.WhyCarriedAnswer{}, fmt.Errorf("step %s of %s %s was not carried: it ended %q", s.Step,
in.Pipeline, s.Artifact, s.Outcome)
}
return seat.WhyCarriedAnswer{CarriedFrom: s.CarriedFrom}, nil
}
return seat.WhyCarriedAnswer{}, fmt.Errorf("pipeline %q has no step %s %s", in.Pipeline, in.Step, in.Artifact)
}
// KeptResults is the kept passing results, by level, digest or package (§83).
func (r Reads) KeptResults(in seat.KeptInput) (seat.KeptAnswer, error) {
keys, err := r.Kept.Keys()
if err != nil {
return seat.KeptAnswer{}, fmt.Errorf("%s cannot be read: %w", seat.StateKept, err)
}
sort.Strings(keys)
out := []seat.KeptResult{}
for _, k := range keys {
level, digest, ok := seat.ParseKeptByLevelDigestKey(k)
if !ok || (in.Level != "" && level != in.Level) || (in.Digest != "" && digest != in.Digest) {
continue
}
raw, err := r.Kept.Value(k)
if err != nil {
return seat.KeptAnswer{}, fmt.Errorf("%s %s cannot be read: %w", seat.StateKept, k, err)
}
if raw == nil {
continue
}
v, _, err := seat.DecodeKeptByLevelDigestValue(raw)
if err != nil {
return seat.KeptAnswer{}, fmt.Errorf("%s %s: %w", seat.StateKept, k, err)
}
if in.Package != "" && v.Package != in.Package {
continue
}
out = append(out, v)
}
fills, err := r.fills()
if err != nil {
return seat.KeptAnswer{}, err
}
a := seat.KeptAnswer{Kept: out}
for _, f := range fills {
if f.Store == seat.StateKept {
f := f
a.Fill = &f
}
}
return a, nil
}
func (r Reads) pipeline(key string) (seat.Pipeline, bool, error) {
key = strings.TrimSpace(key)
if key == "" {
return seat.Pipeline{}, false, fmt.Errorf("name the pipeline's key, <branch>.<revision>.<module>")
}
all, err := r.Pipelines.Keys()
if err != nil {
return seat.Pipeline{}, false, fmt.Errorf("%s cannot be read: %w", seat.StatePipelines, err)
}
return r.pipelineOf(key, all)
}
// pipelineOf is pipeline key, its steps composed from the mover keys among all that it names (H-A-187); a record that
// still carries its steps inline is read whole.
func (r Reads) pipelineOf(key string, all []string) (seat.Pipeline, bool, error) {
raw, err := r.Pipelines.Value(key)
if err != nil {
return seat.Pipeline{}, false, fmt.Errorf("%s %s cannot be read: %w", seat.StatePipelines, key, err)
}
if raw == nil {
return seat.Pipeline{}, false, nil
}
p, _, err := seat.DecodePipelinesByBranchRevisionValue(raw)
if err != nil {
return seat.Pipeline{}, false, fmt.Errorf("%s %s: %w", seat.StatePipelines, key, err)
}
if p.Pipeline == "" {
p.Pipeline = key
}
if len(p.Steps) > 0 {
return p, true, nil
}
prefix := stepsPrefix(key)
vals := map[string][]byte{}
for _, k := range all {
if !strings.HasPrefix(k, prefix) || strings.Contains(k[len(prefix):], ".") {
continue
}
v, err := r.Pipelines.Value(k)
if err != nil {
return seat.Pipeline{}, false, fmt.Errorf("%s %s cannot be read: %w", seat.StatePipelines, k, err)
}
if v != nil {
vals[k] = v
}
}
return p, true, composeSteps(&p, key, vals)
}
func (r Reads) pipelines() ([]seat.Pipeline, error) {
keys, err := r.Pipelines.Keys()
if err != nil {
return nil, fmt.Errorf("%s cannot be read: %w", seat.StatePipelines, err)
}
sort.Strings(keys)
var out []seat.Pipeline
for _, k := range keys {
if IsStepsKey(k) {
continue
}
p, ok, err := r.pipelineOf(k, keys)
if err != nil {
return nil, err
}
if ok {
out = append(out, p)
}
}
return out, nil
}
func startedOf(p seat.Pipeline) time.Time {
if p.Started == nil {
return time.Time{}
}
return *p.Started
}
// askOfWait is a waiting step's own ask (design 47 G §731, §496): a step reading another's test run keeps none (A-469m).
func askOfWait(s seat.StepResult) string { return s.Ask }
// sharedOfWait is the run a waiting step waits on, where the runner knows it (§731; A-469r, A-469v): a test step's run key
// and ask, as one entry with no machine and no asker.
func sharedOfWait(s seat.StepResult) []seat.Shared {
if len(s.Shared) == 0 {
return nil
}
out := make([]seat.Shared, 0, len(s.Shared))
for _, x := range s.Shared {
out = append(out, seat.Shared{Run: x.Run, Ask: x.Ask, Machine: x.Machine})
}
return out
}
// movesCommit says whether a mover of p is at commit.
func movesCommit(p seat.Pipeline, commit string) bool {
for _, m := range p.Movers {
if m.Commit == commit {
return true
}
}
return false
}