The bound key ask.<seat>.<ask> is armed at the put, again at the holder's ask-taken (heard now for mesh-builds and mesh-wants too) with what is left since taken-at, again from now at each step-progress, and at a start for every open ask whose key is gone; it is deleted at the step's end. Past it the step ends could-not-run, naming an untaken ask's seat holder as taking none, and its ask is dropped through the seat's drop. The silent seats' bound, the second put under AgainID, the ask2. keys and the put again at a holder's running go; a put refused by a full work queue is said and put again at the kept-call pace. Builds: design 47 H2 §777, §778 (H-A-349 DQ-4, H-A-395)
281 lines
10 KiB
Go
281 lines
10 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
|
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
|
)
|
|
|
|
// DefaultAskUnansweredBound is the setting ask-unanswered-bound's default (design 47 H H-A-235, H-A-238 (1)): ten minutes
|
|
// from an ask until its holder's first word.
|
|
const DefaultAskUnansweredBound = 10 * time.Minute
|
|
|
|
// The key of the state bounds that times an ask the runner put (design 47 H2 §778, H-A-395): `ask.<seat>.<ask>`, armed
|
|
// before the ask is put with ask-unanswered-bound as its own time to live, armed again at the holder's ask-taken with the
|
|
// bound less the time since taken-at, and from now at each step-progress the holder says for the ask, so a holder that
|
|
// says nothing for the whole bound ends its step. Its expiry is heard by the runner's watch of the state.
|
|
const AskBoundPrefix = "ask."
|
|
|
|
// AskBoundKey is the key of the bound of the ask on onSeat.
|
|
func AskBoundKey(onSeat, ask string) string { return AskBoundPrefix + onSeat + "." + ask }
|
|
|
|
// ParseAskBound reads a key of the state bounds as an ask's bound: the seat and the ask.
|
|
func ParseAskBound(key string) (onSeat, ask string, ok bool) {
|
|
rest, found := strings.CutPrefix(key, AskBoundPrefix)
|
|
if !found {
|
|
return "", "", false
|
|
}
|
|
onSeat, ask, found = strings.Cut(rest, ".")
|
|
return onSeat, ask, found && onSeat != "" && ask != "" && !strings.Contains(ask, ".")
|
|
}
|
|
|
|
// UnansweredWhy is the why of a step whose holder took its ask and then said nothing of it for the whole bound (§778,
|
|
// H-A-395).
|
|
func UnansweredWhy(ask string) string {
|
|
return "its ask " + ask + " was not answered: its holder said nothing of it within ask-unanswered-bound"
|
|
}
|
|
|
|
// UntakenWhy is the why of a step whose ask no holder of onSeat took within the bound after it was asked (§778).
|
|
func UntakenWhy(onSeat string) string {
|
|
return "its ask was not taken: " + onSeat + "'s holder takes none"
|
|
}
|
|
|
|
// AskClock bounds every ask the runner puts on a seat but a test run's, which keeps §725's bounds (§778, H-A-238 (4)):
|
|
// Arm writes a key of the state bounds with its own time to live, keeping one that is there; Rearm writes it again with a
|
|
// new time to live; Unbound deletes it at the step's end; Raise raises the finding call-unanswered of an ask that ended its step, Clear clears it once the
|
|
// ask is answered after all (H-A-238 (2)).
|
|
type AskClock struct {
|
|
// Bound is ask-unanswered-bound.
|
|
Bound time.Duration
|
|
Arm func(key string, ttl time.Duration) error
|
|
Rearm func(key string, ttl time.Duration) error
|
|
Unbound func(key string) error
|
|
Raise func(onSeat, ask string, bound time.Duration) error
|
|
Clear func(onSeat, ask string) error
|
|
}
|
|
|
|
// arm arms the bound of a, before it is put (§778); a test run's ask, and a nil clock, arm none.
|
|
func (c *AskClock) arm(a Asked) error {
|
|
if c == nil || c.Arm == nil || a.Run != "" {
|
|
return nil
|
|
}
|
|
if err := c.Arm(AskBoundKey(a.Seat, a.ID), c.bound()); err != nil {
|
|
return fmt.Errorf("the bound of ask %s is not armed: %w", a.ID, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *AskClock) bound() time.Duration {
|
|
if c.Bound > 0 {
|
|
return c.Bound
|
|
}
|
|
return DefaultAskUnansweredBound
|
|
}
|
|
|
|
// Left is what is left of the bound of an ask whose holder last spoke at since, at now: at least a second, so an ask
|
|
// whose time is spent ends at once by its key's expiry.
|
|
func (c *AskClock) Left(since, now time.Time) time.Duration {
|
|
return max(c.bound()-now.Sub(since), time.Second)
|
|
}
|
|
|
|
// Taken arms the bound of the ask on onSeat again at its holder's ask-taken, with the bound less the time since takenAt
|
|
// (§778).
|
|
func (c *AskClock) Taken(onSeat, ask string, takenAt, now time.Time) error {
|
|
if c == nil || c.Rearm == nil || ask == "" {
|
|
return nil
|
|
}
|
|
return c.Rearm(AskBoundKey(onSeat, ask), c.Left(takenAt, now))
|
|
}
|
|
|
|
// Progress arms the bound of the ask on onSeat again from now, at a step-progress its holder says for it (H-A-395).
|
|
func (c *AskClock) Progress(onSeat, ask string) error {
|
|
if c == nil || c.Rearm == nil || ask == "" {
|
|
return nil
|
|
}
|
|
return c.Rearm(AskBoundKey(onSeat, ask), c.bound())
|
|
}
|
|
|
|
// Ended deletes the bound of the ask on onSeat at its step's end (H-A-395).
|
|
func (c *AskClock) Ended(onSeat, ask string) error {
|
|
if c == nil || c.Unbound == nil || ask == "" {
|
|
return nil
|
|
}
|
|
return c.Unbound(AskBoundKey(onSeat, ask))
|
|
}
|
|
|
|
// answeredLate clears the finding of the ask of step s, which ended by its bound, its holder heard after all
|
|
// (H-A-238 (2)).
|
|
func (c *AskClock) answeredLate(s seat.StepResult, logf func(string, ...any)) {
|
|
if c == nil || c.Clear == nil || s.Ask == "" || (s.Why != UnansweredWhy(s.Ask) && s.Why != UntakenWhy(s.Seat)) {
|
|
return
|
|
}
|
|
if err := c.Clear(s.Seat, s.Ask); err != nil {
|
|
logf("the finding of ask %s, answered after its step ended, is not cleared: %v", s.Ask, err)
|
|
}
|
|
}
|
|
|
|
// AskUnanswered takes the expiry of the bound of the ask on onSeat (§778, H-A-395): the step ends could not run, "its ask
|
|
// <id> was not answered" when its holder took it and then said nothing for the bound, "its ask was not taken: <seat>'s
|
|
// holder takes none" when no holder took it; its ask is dropped through the seat's drop, so no holder works on it after;
|
|
// the finding call-unanswered is raised, so the pipeline ends and its turn frees. A bound whose ask no running step names
|
|
// any longer asks nothing.
|
|
func (v Verbs) AskUnanswered(onSeat, ask string) error {
|
|
h, c := v.Hearer, v.Starter.Asks
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
key, p, i, err := v.runningAsk(onSeat, ask)
|
|
if err != nil || key == "" {
|
|
return err
|
|
}
|
|
s := p.Steps[i]
|
|
why, said := UnansweredWhy(s.Ask), "was taken and its holder said nothing of it"
|
|
if s.TakenAt == nil {
|
|
why, said = UntakenWhy(onSeat), "was taken by no holder"
|
|
}
|
|
h.logf("the ask %s of the %s step of %s on %s %s within %s: the step ends could not run and its ask is dropped (§778)",
|
|
s.Ask, s.Step, key, onSeat, said, c.bound())
|
|
if err := h.endUnanswered(key, s.Ask, why); err != nil {
|
|
return err
|
|
}
|
|
if h.DropRun != nil {
|
|
if err := h.DropRun(onSeat, s.Node, s.Ask, why); err != nil {
|
|
h.logf("the ask %s on %s, ended by its bound, is not dropped: %v", s.Ask, onSeat, err)
|
|
}
|
|
}
|
|
if c.Raise != nil {
|
|
return c.Raise(onSeat, s.Ask, c.bound())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// OpenAsk is an ask a running step waits on, and when its holder last spoke of it: its taken-at, else its asked-at.
|
|
type OpenAsk struct {
|
|
Seat, Ask string
|
|
Since time.Time
|
|
}
|
|
|
|
// OpenAsks are the asks of every running pipeline's open steps but a test run's (§778): at a start each whose bound key
|
|
// is gone is armed again with what is left of its bound, so a restart never grants a fresh one.
|
|
func (v Verbs) OpenAsks() ([]OpenAsk, error) {
|
|
pkeys, err := allKeys(v.Hearer.Store)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the pipelines are not listed: %w", err)
|
|
}
|
|
var out []OpenAsk
|
|
for _, key := range pkeys {
|
|
p, _, err := v.Hearer.Store.Get(key)
|
|
if err != nil || p.Outcome != seat.PipelineOutcomeRunning {
|
|
continue
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, s := range p.Steps {
|
|
if !open(s) || s.Ask == "" || sharedRun(s) != "" || seen[s.Ask] {
|
|
continue
|
|
}
|
|
seen[s.Ask] = true
|
|
since := s.TakenAt
|
|
if since == nil {
|
|
since = s.AskedAt
|
|
}
|
|
if since == nil {
|
|
since = s.Started
|
|
}
|
|
if since == nil {
|
|
continue
|
|
}
|
|
out = append(out, OpenAsk{Seat: s.Seat, Ask: s.Ask, Since: *since})
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// runningAsk is the pipeline and the index of its running step asked under ask, or put again under it, of onSeat; an empty
|
|
// key when none is.
|
|
func (v Verbs) runningAsk(onSeat, ask string) (string, seat.Pipeline, int, error) {
|
|
pkeys, err := allKeys(v.Hearer.Store)
|
|
if err != nil {
|
|
return "", seat.Pipeline{}, -1, fmt.Errorf("the pipelines are not listed: %w", err)
|
|
}
|
|
for _, key := range pkeys {
|
|
p, _, err := v.Hearer.Store.Get(key)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if i := stepOfAsk(p, ask); i >= 0 && open(p.Steps[i]) && p.Steps[i].Seat == onSeat {
|
|
return key, p, i, nil
|
|
}
|
|
}
|
|
return "", seat.Pipeline{}, -1, nil
|
|
}
|
|
|
|
// markRerun marks a's body a rerun by caller.
|
|
func markRerun(a *Asked, caller string) {
|
|
switch b := a.Ask.(type) {
|
|
case nodebuildgo.StepAsk:
|
|
if b.Step != nil {
|
|
step := *b.Step
|
|
step.Rerun, step.By = true, caller
|
|
b.Step = &step
|
|
}
|
|
a.Ask = b
|
|
case builds.StepRun:
|
|
b.Rerun, b.By = true, caller
|
|
a.Ask = b
|
|
case wants.StepRun:
|
|
b.Rerun, b.By = true, caller
|
|
a.Ask = b
|
|
}
|
|
}
|
|
|
|
// endUnanswered ends the running step of pipeline key asked under ask could not run, saying why (H-A-235, H-A-243): every
|
|
// other mover's validate reading the ask with it, a composition its validate held ended not asked, the pipeline ended when
|
|
// nothing else is open, said and logged as any step's end.
|
|
func (h *Hearer) endUnanswered(key, ask, why string) error {
|
|
wr := h.widensReads() // made before the compare-and-set, kept for its retries (H-A-289)
|
|
for {
|
|
p, rev, err := h.Store.Get(key)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
i := stepOfAsk(p, ask)
|
|
if i < 0 || !open(p.Steps[i]) {
|
|
return nil
|
|
}
|
|
now := h.now()
|
|
s := &p.Steps[i]
|
|
s.Outcome, s.Why, s.Ended = seat.StepResultOutcomeCouldNotRun, why, &now
|
|
p.Unlogged = append(p.Unlogged, endEntry(key, *s, now))
|
|
var readers []int
|
|
if s.Step == seat.StepResultStepValidate {
|
|
readers = sameValidate(&p, i, now)
|
|
_, notAsked := releaseCompositions(&p, now, h.kept(key))
|
|
readers = append(readers, notAsked...)
|
|
}
|
|
asked, err := askApproval(key, &p, now, h.Asker)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ended := h.endPipeline(&p, now, wr)
|
|
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)
|
|
for _, j := range append([]int{i}, readers...) {
|
|
if err := h.say(seat.EventStepEnded, p, &p.Steps[j]); err != nil {
|
|
return fmt.Errorf("pipeline %s's %s is written but not said: %w", key, seat.EventStepEnded, err)
|
|
}
|
|
}
|
|
h.appendUnlogged(key)
|
|
return h.said(p, asked, ended)
|
|
}
|
|
}
|