Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/asks.go
T
jschoubben e9673046cb Runner: read the bus, not a copy; arm a bound only while its count shrinks (issue 1984)
Review round 1. The store kept a running pipeline's mover-key values in
memory while its record stayed at the revision read, but the module's
own runner_unlogged_append writes through a store of its own: a read
between that store's record write and its mover-key writes kept old
values at the new revision, and the runner served `running` while the
bus held `passed`. The copy goes; each key is still read by its name,
none listed, with the trunk's check of the revisions read.

A bound passing while step ends wait now arms the bound again only while
their count (not yet delivered plus unacknowledged) shrank since the
bound before, kept as `waited` on the ask's mark beside `unread`: an end
the handler naks forever, which no delivery limit stops, ends the step
saying the count did not shrink. Every such why says only what is true
("its ask X is not known to be answered: ..."), and none raises
call-unanswered. The ask clock the start makes now reads the durable
itself, so the integration row fails when the start wires none; the
nudge's mutex is the hearer's own.

Builds: issue 1984 note 6 (1984/AC1, 1984/AC2), notes 7 (b), (c), 9 (d), 10 (e)
2026-11-23 18:07:56 +01:00

411 lines
17 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"
}
// notKnownWhy is the why of a step its bound ended while whether its end waits in the runner's own step-ended durable is not
// known, what saying why (issue 1984 notes 7 (b), 10 (e)): its holder is never said silent.
func notKnownWhy(ask, what string) string {
return "its ask " + ask + " is not known to be answered: " + what
}
// unreadWhat is why a step's end is not known when the runner could not read its own step-ended durable of onSeat.
func unreadWhat(onSeat string) string {
return "the runner could not read its own step-ended durable of " + onSeat
}
// stuckWhat is why a step's end is not known when n step ends of onSeat wait in the runner's own step-ended durable, no
// fewer than the before at the bound before (issue 1984 notes 9 (d), 10 (e)).
func stuckWhat(onSeat string, n, before uint64) string {
return fmt.Sprintf("%d step ends of %s wait in the runner's own step-ended durable and did not shrink since the bound before (%d then)", n, onSeat, before)
}
// unkeptWhat is why a step's end is not known when n step ends of onSeat wait in the runner's own step-ended durable and
// the runner cannot keep their count to judge the bound after, so it arms no bound it could not end.
func unkeptWhat(onSeat string, n uint64) string {
return fmt.Sprintf("%d step ends of %s wait in the runner's own step-ended durable, and the runner could not keep their count to judge the bound after", n, onSeat)
}
// 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
// Waiting reads, once, the runner's own durable on onSeat's step-ended stream (issue 1984 notes 6, 7): the step ends it
// holds not yet delivered and those delivered and not yet acknowledged, none for a durable or stream the bus does not
// hold; nil reads none, and a bound then ends its step as it passes.
Waiting func(onSeat string) (pending, unacknowledged uint64, err 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"
}
// Its end may wait in the runner's own step-ended durable, not yet taken: the runner's queue is never counted against
// the holder (issue 1984 notes 6, 7).
read, unknown := "", ""
if c.Waiting != nil && c.Rearm != nil {
var again bool
if read, again, unknown = v.endsWaiting(c, key, onSeat, s); again {
return nil
}
}
if unknown != "" {
why = notKnownWhy(s.Ask, unknown)
h.logf("the ask %s of the %s step of %s on %s is not known to be answered within %s: %s; the step ends could not run and its ask is dropped (§778)",
s.Ask, s.Step, key, onSeat, c.bound(), unknown)
} else {
h.logf("the ask %s of the %s step of %s on %s %s within %s%s: the step ends could not run and its ask is dropped (§778)",
s.Ask, s.Step, key, onSeat, said, c.bound(), read)
}
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)
}
}
// The finding call-unanswered says its holder said nothing, which is not known of an ask whose end may wait in the
// runner's own durable: its step's why and the pipeline's failure say it (issue 1984 note 10 (e)).
if c.Raise != nil && unknown == "" {
return c.Raise(onSeat, s.Ask, c.bound())
}
return nil
}
// endsWaiting reads the runner's own step-ended durable of onSeat once, as the bound of step s's ask passes (issue 1984
// notes 6, 7, 9, 10): the step ends waiting there, not yet delivered and unacknowledged. While some wait, and fewer than at
// the bound before, or at the first read that fails, the bound is armed again whole and said: again true. A read that
// fails after one that failed, or a count no smaller than the one before, answers in unknown what is true of the ask, and
// the step ends saying it. With none waiting it answers what the journal adds to the step's end, the count read. What a
// bound read is kept on the ask's mark, unread and waited, for the bound after (notes 7 (c), 10 (e)); a mark not kept
// ends the step, so no bound is armed again endlessly.
func (v Verbs) endsWaiting(c *AskClock, key, onSeat string, s seat.StepResult) (read string, again bool, unknown string) {
h := v.Hearer
boundKey := AskBoundKey(onSeat, s.Ask)
pending, unacked, err := c.Waiting(onSeat)
if h.Clock == nil {
if err != nil {
h.logf("the bound of ask %s of the %s step of %s on %s passed and the runner's own step-ended durable of %s is not read (%v); the runner keeps no ask marks to arm it once more",
s.Ask, s.Step, key, onSeat, onSeat, err)
return "", false, unreadWhat(onSeat)
}
if n := pending + unacked; n > 0 {
h.logf("the bound of ask %s of the %s step of %s on %s passed while %d step ends wait in the runner's own step-ended durable of %s; the runner keeps no ask marks to judge the next bound",
s.Ask, s.Step, key, onSeat, n, onSeat)
return "", false, unkeptWhat(onSeat, n)
}
return fmt.Sprintf(", and no step end waits in the runner's own step-ended durable of %s (0 not yet delivered, 0 unacknowledged)", onSeat), false, ""
}
mark, marked, merr := h.Clock.AskOf(s.Ask)
if merr != nil {
h.logf("the mark of ask %s is not read, so the bound passing reads it as unmarked: %v", s.Ask, merr)
}
if !marked {
mark = AskMark{Pipeline: key, Module: s.Module, Step: s.Step}
}
keep := func(what string) bool {
if werr := h.Clock.MarkAskAgain(s.Ask, mark); werr != nil {
h.logf("the bound of ask %s of the %s step of %s on %s passed and %s, and the ask's mark keeping it is not written: %v",
s.Ask, s.Step, key, onSeat, what, werr)
return false
}
return true
}
arm := func(what string) {
if rerr := c.Rearm(boundKey, c.bound()); rerr != nil {
h.logf("the bound of ask %s on %s is not armed again after %s: %v", s.Ask, onSeat, what, rerr)
}
}
if err != nil {
if mark.Unread != nil {
h.logf("the bound of ask %s of the %s step of %s on %s passed and the runner's own step-ended durable of %s is not read again (%v), as at the bound before (%s)",
s.Ask, s.Step, key, onSeat, onSeat, err, mark.Unread.Format(time.RFC3339))
return "", false, unreadWhat(onSeat)
}
now := h.now()
mark.Unread = &now
what := fmt.Sprintf("the runner's own step-ended durable of %s is not read (%v)", onSeat, err)
if !keep(what) {
return "", false, unreadWhat(onSeat)
}
arm("its durable was not read")
h.logf("the bound of ask %s of the %s step of %s on %s passed and %s: the step is not ended and its bound is armed again with %s; a second failed read at that bound ends it",
s.Ask, s.Step, key, onSeat, what, c.bound())
return "", true, ""
}
n, before, wasUnread := pending+unacked, mark.Waited, mark.Unread != nil
mark.Unread = nil
switch {
case n == 0:
if wasUnread || before != nil {
mark.Waited = nil
keep("no step end waits")
}
return fmt.Sprintf(", and no step end waits in the runner's own step-ended durable of %s (0 not yet delivered, 0 unacknowledged)", onSeat), false, ""
case before != nil && n >= *before:
h.logf("the bound of ask %s of the %s step of %s on %s passed while %d step ends wait in the runner's own step-ended durable of %s (%d not yet delivered, %d delivered and not acknowledged), no fewer than the %d at the bound before: the runner takes none of them",
s.Ask, s.Step, key, onSeat, n, onSeat, pending, unacked, *before)
return "", false, stuckWhat(onSeat, n, *before)
}
mark.Waited = &n
what := fmt.Sprintf("%d step ends wait in the runner's own step-ended durable of %s", n, onSeat)
if !keep(what) {
return "", false, unkeptWhat(onSeat, n)
}
arm("step ends wait")
h.logf("the bound of ask %s of the %s step of %s on %s passed while %s (%d not yet delivered, %d delivered and not acknowledged), fewer than at any bound before: the step is not ended and its bound is armed again with %s",
s.Ask, s.Step, key, onSeat, what, pending, unacked, c.bound())
return "", true, ""
}
// 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)
}
}