Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/bounds.go
T
jschoubben d89a6bacc6 Merge branch 'feat/47-G-fix11-b1g' into feat/47-G-join11
# Conflicts:
#	modules/mesh-delivery-contracts/seats/mesh-pipeline-runner/seat.json
#	modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go
#	modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go
#	modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/verbs.go
#	modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/verbs_test.go
#	modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/views.go
#	modules/mesh-delivery-pipeline/internal/pipeline/start.go
#	modules/mesh-delivery-pipeline/manifest_test.go
#	modules/mesh-delivery-pipeline/module.json
2026-11-12 10:32:18 +01:00

133 lines
5.4 KiB
Go

package pipeline
import (
"errors"
"fmt"
"strings"
"time"
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"
nodebuildimage "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildimage"
nodebuildnode "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildnode"
)
// AskBound is how long an ask on a build seat's work queue may wait for its holder to take it (design 47 G §535, §647): five
// minutes, the bound of a key of the state `bounds` with its own time to live (A-77, A-104), never a timer.
const AskBound = 5 * time.Minute
// TakenSeat is a build seat whose holder says `ask-taken` when it takes an ask (design 47 G §602): where the runner hears it.
type TakenSeat struct {
Seat string
// Subject hears every node's ask-taken of the seat.
Subject string
}
// TakenSeats are the three build seats, from their contracts: the seat mesh-builds and the seat mesh-wants say no ask-taken,
// so no bound is armed for their asks.
var TakenSeats = []TakenSeat{
{Seat: nodebuildgo.Seat, Subject: nodebuildgo.SubjectAskTakenAll},
{Seat: nodebuildnode.Seat, Subject: nodebuildnode.SubjectAskTakenAll},
{Seat: nodebuildimage.Seat, Subject: nodebuildimage.SubjectAskTakenAll},
}
// BoundSeat says whether the holder of a seat says ask-taken, so an ask put on its work queue has a bound.
func BoundSeat(name string) bool {
for _, s := range TakenSeats {
if s.Seat == name {
return true
}
}
return false
}
// Arm puts a bound of one ask's wait for its holder: a key of the state `bounds` named for the ask, written with its own
// time to live. It is armed before the ask is put, so an ask-taken heard at once finds it.
type Arm func(ask string) error
// NotTaken ends the step asked under ask that no holder took within its bound, the bound's expiry heard (design 47 G §32,
// §535, §647, A-77): the step ends `could not run` naming the seat, so its pipeline ends failed once the others end, never
// a silent wait. A step that ended or was never asked of a seat takes nothing.
func (h *Hearer) NotTaken(ask string, bound time.Duration) error {
pkeys, err := h.Store.Keys("*.*.*")
if err != nil {
return fmt.Errorf("the pipelines are not listed: %w", err)
}
for _, key := range pkeys {
p, _, err := h.Store.Get(key)
if err != nil || p.Outcome != seat.PipelineOutcomeRunning || stepOfAsk(p, ask) < 0 {
continue
}
return h.endNotTaken(key, ask, bound)
}
return nil
}
func (h *Hearer) endNotTaken(key, ask string, bound time.Duration) error {
for {
p, rev, err := h.Store.Get(key)
if err != nil {
return fmt.Errorf("the pipeline %s is not read: %w", key, err)
}
i := stepOfAsk(p, ask)
if i < 0 || p.Steps[i].Outcome != seat.StepResultOutcomeRunning {
return nil
}
now := h.now()
s := &p.Steps[i]
s.Outcome, s.Ended = seat.StepResultOutcomeCouldNotRun, &now
s.Why = fmt.Sprintf("the seat %s has no holder that took ask %s within %s", s.Seat, ask, bound)
p.Unlogged = append(p.Unlogged, endEntry(key, *s, now))
ended := endPipeline(&p, now)
if _, err := h.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
continue
} else if err != nil {
return fmt.Errorf("pipeline %s cannot be written: %w", key, err)
}
h.Failures.StepEnded(h.Store, p, *s)
if err := h.say(seat.EventStepEnded, p, s); err != nil {
return fmt.Errorf("pipeline %s's %s is written but not said: %w", key, seat.EventStepEnded, err)
}
h.appendUnlogged(key)
h.Ager.Age()
h.Fills.Check()
return h.said(p, false, ended)
}
}
// IsAskKey says whether a key of the state `bounds` is an ask's bound.
func IsAskKey(key string) bool { return strings.HasPrefix(key, "step-") && !strings.Contains(key, ".") }
// DropBoundPrefix begins the key of a superseded step's drop in the state `bounds`, `drop.<build seat>.<ask>` (design 47 G
// A-222, A-77): its five-minute expiry says the drop unanswered.
const DropBoundPrefix = "drop."
// DropBoundKey is the key of the bound of buildSeat's drop of ask.
func DropBoundKey(buildSeat, ask string) string { return DropBoundPrefix + buildSeat + "." + ask }
// ParseDropBound reads a key of the state `bounds` as a drop's bound: the build seat and the ask it names.
func ParseDropBound(key string) (buildSeat, ask string, ok bool) {
rest, found := strings.CutPrefix(key, DropBoundPrefix)
if !found {
return "", "", false
}
buildSeat, ask, found = strings.Cut(rest, ".")
return buildSeat, ask, found && buildSeat != "" && ask != "" && !strings.Contains(ask, ".")
}
// BoundDrop is drop with a bound of five minutes (A-222, A-77): the bound key is armed, with its own time to live, before
// the drop is asked, and answered is told when the drop is answered, to delete the key, so an answer ends the wait with no
// finding; a drop never answered leaves its key to expire, and the expiry says it unanswered. A bound that cannot be armed
// is returned and the drop not asked.
func BoundDrop(drop Drop, arm func(key string) error, answered func(buildSeat, ask string) error) Drop {
return func(buildSeat, node, ask, why string) error {
if err := arm(DropBoundKey(buildSeat, ask)); err != nil {
return fmt.Errorf("the bound of the drop of ask %s is not armed: %w", ask, err)
}
if err := drop(buildSeat, node, ask, why); err != nil {
return err
}
return answered(buildSeat, ask)
}
}