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)
This commit is contained in:
2026-11-23 18:07:56 +01:00
parent 1a8b7c8591
commit e9673046cb
12 changed files with 190 additions and 235 deletions
@@ -42,9 +42,11 @@ func durationSetting(getenv func(string) string, env string, def time.Duration)
// askClock is the bound of every ask but a test run's over the state bounds (design 47 H2 §778, H-A-395): a key armed with // askClock is the bound of every ask but a test run's over the state bounds (design 47 H2 §778, H-A-395): a key armed with
// its own time to live before the ask is put, armed again at the holder's ask-taken and at each step-progress of the ask, // its own time to live before the ask is put, armed again at the holder's ask-taken and at each step-progress of the ask,
// its expiry heard by watchBounds; the finding call-unanswered, scope call, id the ask, raised when the bound ends the // its expiry heard by watchBounds; the finding call-unanswered, scope call, id the ask, raised when the bound ends the
// step and cleared when the ask is answered after all (H-A-238 (2)). // step and cleared when the ask is answered after all (H-A-238 (2)). Before a bound ends its step, the runner's own durable
func askClock(ctx context.Context, bounds jetstream.KeyValue, bound time.Duration, w callFindings, module string, now func() time.Time) *pipeline.AskClock { // named durable on the seat's step-ended stream is read (issue 1984): an end waiting there is not the holder's silence.
return &pipeline.AskClock{Bound: bound, func askClock(ctx context.Context, bounds jetstream.KeyValue, bound time.Duration, w callFindings, module string, now func() time.Time,
js jetstream.JetStream, durable string) *pipeline.AskClock {
return &pipeline.AskClock{Bound: bound, Waiting: stepEndsWaiting(ctx, js, durable),
Arm: func(key string, ttl time.Duration) error { Arm: func(key string, ttl time.Duration) error {
_, err := bounds.Create(ctx, key, []byte(`{"ask":"`+key+`"}`), jetstream.KeyTTL(ttl)) _, err := bounds.Create(ctx, key, []byte(`{"ask":"`+key+`"}`), jetstream.KeyTTL(ttl))
if errors.Is(err, jetstream.ErrKeyExists) { if errors.Is(err, jetstream.ErrKeyExists) {
@@ -186,9 +186,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
hearer.AskSteps = starter.AskSteps hearer.AskSteps = starter.AskSteps
hearer.RegistrationWaits = func() { starter.JudgeRegistrationMissing(store) } // at every end (H-A-265 (4)) hearer.RegistrationWaits = func() { starter.JudgeRegistrationMissing(store) } // at every end (H-A-265 (4))
logger.Printf("asks: an ask's bound %s, counted from its put until its holder's ask-taken, then from the take and each step-progress; past it the step ends could not run and its ask is dropped", askBound) logger.Printf("asks: an ask's bound %s, counted from its put until its holder's ask-taken, then from the take and each step-progress; past it the step ends could not run and its ask is dropped", askBound)
clock := askClock(ctx, bounds, askBound, rv.Findings, cred.Module, time.Now) clock := askClock(ctx, bounds, askBound, rv.Findings, cred.Module, time.Now, js, cred.EventConsumerName())
// A bound passing reads the runner's own step-ended durable first: an end waiting there is not the holder's silence (issue 1984).
clock.Waiting = stepEndsWaiting(ctx, js, cred.EventConsumerName())
starter.Asks, hearer.Asks = clock, clock starter.Asks, hearer.Asks = clock, clock
starter.Supersede = func(newer seat.Pipeline) { hearer.SupersedeRunning(newer) } starter.Supersede = func(newer seat.Pipeline) { hearer.SupersedeRunning(newer) }
starter.Drop = hearer.DropOpen // a placing's end drops the asks still open (H-A-506) starter.Drop = hearer.DropOpen // a placing's end drops the asks still open (H-A-506)
@@ -18,7 +18,8 @@ import (
// testABoundPassingReadsTheRunnersOwnStepEndedDurable is a row of TestTheRunnerPlacesAPipelineAsksItsBuildTakesItsEndAndAnswersAReview // testABoundPassingReadsTheRunnersOwnStepEndedDurable is a row of TestTheRunnerPlacesAPipelineAsksItsBuildTakesItsEndAndAnswersAReview
// (design 47 H2 §819: the module's tests are folded under its budget). 1984/AC2: as an ask's bound passes the runner reads its own // (design 47 H2 §819: the module's tests are folded under its budget). 1984/AC2: as an ask's bound passes the runner reads its own
// durable on the seat's step-ended stream once, against the bus: the ends not yet delivered and those delivered and not // durable on the seat's step-ended stream once, against the bus: the ends not yet delivered and those delivered and not
// acknowledged; a durable or stream the bus does not hold holds none; a read the bus does not answer is an error. // acknowledged; a durable or stream the bus does not hold holds none; a read the bus does not answer is an error. The clock
// is made as the runner's start makes it (askClock), so a start that reads no durable fails here.
func testABoundPassingReadsTheRunnersOwnStepEndedDurable(t *testing.T) { func testABoundPassingReadsTheRunnersOwnStepEndedDurable(t *testing.T) {
nc, err := nats.Connect(testbus.URL(t)) nc, err := nats.Connect(testbus.URL(t))
if err != nil { if err != nil {
@@ -29,7 +30,11 @@ func testABoundPassingReadsTheRunnersOwnStepEndedDurable(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
const durable = "n1_mesh-delivery-pipeline" const durable = "n1_mesh-delivery-pipeline"
waiting := stepEndsWaiting(ctx, js, durable) // The ask clock the runner's start makes reads the durable it is given: without it a bound ends its step unread.
waiting := askClock(ctx, nil, time.Minute, nil, "mesh-delivery-pipeline", time.Now, js, durable).Waiting
if waiting == nil {
t.Fatal("the runner's ask clock reads no step-ended durable as a bound passes")
}
read := func(onSeat string) (uint64, uint64) { read := func(onSeat string) (uint64, uint64) {
t.Helper() t.Helper()
p, u, err := waiting(onSeat) p, u, err := waiting(onSeat)
@@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"sync"
"time" "time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
@@ -367,13 +366,10 @@ func (h *Hearer) AgeOnStepEnds(ctx context.Context) {
} }
} }
// ageWakeMade guards the first making of a hearer's ageWake, whichever of its step ends and AgeOnStepEnds comes first.
var ageWakeMade sync.Mutex
// ageNudge is the nudge AgeOnStepEnds waits on, holding one nudge at most. // ageNudge is the nudge AgeOnStepEnds waits on, holding one nudge at most.
func (h *Hearer) ageNudge() chan struct{} { func (h *Hearer) ageNudge() chan struct{} {
ageWakeMade.Lock() h.ageWakeMade.Lock()
defer ageWakeMade.Unlock() defer h.ageWakeMade.Unlock()
if h.ageWake == nil { if h.ageWake == nil {
h.ageWake = make(chan struct{}, 1) h.ageWake = make(chan struct{}, 1)
} }
@@ -41,11 +41,27 @@ func UnansweredWhy(ask string) string {
return "its ask " + ask + " was not answered: its holder said nothing of it within ask-unanswered-bound" return "its ask " + ask + " was not answered: its holder said nothing of it within ask-unanswered-bound"
} }
// unreadWhy is the why of a step whose bound passed twice in a row while the runner could not read its own step-ended // 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
// durable of onSeat (issue 1984 note 7 (b)): whether its end waits there is not known, so its holder is not said silent. // known, what saying why (issue 1984 notes 7 (b), 10 (e)): its holder is never said silent.
func unreadWhy(onSeat, ask string) string { func notKnownWhy(ask, what string) string {
return "its ask " + ask + " is not known to be answered: the runner could not read its own step-ended durable of " + onSeat + return "its ask " + ask + " is not known to be answered: " + what
" at two bounds in a row" }
// 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). // UntakenWhy is the why of a step whose ask no holder of onSeat took within the bound after it was asked (§778).
@@ -152,17 +168,17 @@ func (v Verbs) AskUnanswered(onSeat, ask string) error {
} }
// Its end may wait in the runner's own step-ended durable, not yet taken: the runner's queue is never counted against // 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). // the holder (issue 1984 notes 6, 7).
read, unread := "", false read, unknown := "", ""
if c.Waiting != nil && c.Rearm != nil { if c.Waiting != nil && c.Rearm != nil {
var again bool var again bool
if read, again, unread = v.endsWaiting(c, key, onSeat, s); again { if read, again, unknown = v.endsWaiting(c, key, onSeat, s); again {
return nil return nil
} }
} }
if unread { if unknown != "" {
why = unreadWhy(onSeat, s.Ask) 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: the runner could not read its own step-ended durable of %s at this bound and the one before; the step ends could not run and its ask is dropped (§778)", 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(), onSeat) s.Ask, s.Step, key, onSeat, c.bound(), unknown)
} else { } 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)", 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) s.Ask, s.Step, key, onSeat, said, c.bound(), read)
@@ -175,73 +191,98 @@ func (v Verbs) AskUnanswered(onSeat, ask string) error {
h.logf("the ask %s on %s, ended by its bound, is not dropped: %v", s.Ask, onSeat, err) 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 ends were not read: its // The finding call-unanswered says its holder said nothing, which is not known of an ask whose end may wait in the
// step's why and the pipeline's failure say it. // runner's own durable: its step's why and the pipeline's failure say it (issue 1984 note 10 (e)).
if c.Raise != nil && !unread { if c.Raise != nil && unknown == "" {
return c.Raise(onSeat, s.Ask, c.bound()) return c.Raise(onSeat, s.Ask, c.bound())
} }
return nil 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 // 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): while step ends wait there, or at the first read that fails, the bound is armed again whole and said, again // notes 6, 7, 9, 10): the step ends waiting there, not yet delivered and unacknowledged. While some wait, and fewer than at
// true; a read that fails at the bound after one that failed answers unread true; else it answers what the journal adds to // the bound before, or at the first read that fails, the bound is armed again whole and said: again true. A read that
// the step's end, the count read, and the ask's mark loses its unread. // fails after one that failed, or a count no smaller than the one before, answers in unknown what is true of the ask, and
func (v Verbs) endsWaiting(c *AskClock, key, onSeat string, s seat.StepResult) (read string, again, unread bool) { // 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 h := v.Hearer
boundKey := AskBoundKey(onSeat, s.Ask) boundKey := AskBoundKey(onSeat, s.Ask)
pending, unacked, err := c.Waiting(onSeat) pending, unacked, err := c.Waiting(onSeat)
var mark AskMark if h.Clock == nil {
marked := false if err != nil {
if h.Clock != 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",
var merr error s.Ask, s.Step, key, onSeat, onSeat, err)
if mark, marked, merr = h.Clock.AskOf(s.Ask); merr != nil { return "", false, unreadWhat(onSeat)
h.logf("the mark of ask %s is not read, so the bound passing reads it as unmarked: %v", s.Ask, merr)
} }
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 { if !marked {
mark = AskMark{Pipeline: key, Module: s.Module, Step: s.Step} 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 err != nil {
if mark.Unread != 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)", 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)) s.Ask, s.Step, key, onSeat, onSeat, err, mark.Unread.Format(time.RFC3339))
return "", false, true return "", false, unreadWhat(onSeat)
}
if h.Clock == 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, true
} }
now := h.now() now := h.now()
mark.Unread = &now mark.Unread = &now
if werr := h.Clock.MarkAskAgain(s.Ask, mark); werr != nil { what := fmt.Sprintf("the runner's own step-ended durable of %s is not read (%v)", onSeat, err)
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), and the ask's mark keeping that is not written (%v)", if !keep(what) {
s.Ask, s.Step, key, onSeat, onSeat, err, werr) return "", false, unreadWhat(onSeat)
return "", false, true
} }
if rerr := c.Rearm(boundKey, c.bound()); rerr != nil { arm("its durable was not read")
h.logf("the bound of ask %s on %s is not armed again after its durable was not read: %v", s.Ask, onSeat, rerr) 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())
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 step is not ended and its bound is armed again with %s; a second failed read at that bound ends it", return "", true, ""
s.Ask, s.Step, key, onSeat, onSeat, err, c.bound())
return "", true, false
} }
if mark.Unread != nil && h.Clock != nil { n, before, wasUnread := pending+unacked, mark.Waited, mark.Unread != nil
mark.Unread = nil mark.Unread = nil
if werr := h.Clock.MarkAskAgain(s.Ask, mark); werr != nil { switch {
h.logf("the mark of ask %s keeps its unread, its durable read now: %v", s.Ask, werr) 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)
} }
if n := pending + unacked; n > 0 { mark.Waited = &n
if rerr := c.Rearm(boundKey, c.bound()); rerr != nil { what := fmt.Sprintf("%d step ends wait in the runner's own step-ended durable of %s", n, onSeat)
h.logf("the bound of ask %s on %s is not armed again while step ends wait: %v", s.Ask, onSeat, rerr) if !keep(what) {
} return "", false, unkeptWhat(onSeat, n)
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): the step is not ended and its bound is armed again with %s",
s.Ask, s.Step, key, onSeat, n, onSeat, pending, unacked, c.bound())
return "", true, false
} }
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, false 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. // OpenAsk is an ask a running step waits on, and when its holder last spoke of it: its taken-at, else its asked-at.
@@ -142,11 +142,12 @@ func TestAnAsksBoundCountsFromThePullAndEndsItsStep(t *testing.T) {
} }
} }
// 1984/AC2 (issue 1984 notes 6 and 7): before a bound ends its step, the runner reads its own step-ended durable of the // 1984/AC2 (issue 1984 notes 6, 7, 9 and 10): before a bound ends its step, the runner reads its own step-ended durable of the
// ask's seat once. While step ends wait there the step is not ended: its bound is armed again and the journal says how many // ask's seat once. While step ends wait there, fewer than at the bound before, the step is not ended: its bound is armed
// wait. With none waiting the step ends as before, its why true, the journal naming the count read. A read that fails arms // again and the journal says how many wait; a count no smaller ends the step naming it. With none waiting the step ends as
// the bound once more, kept on the ask's mark; a second failed read at that bound ends the step saying the durable was not // before, its why true, the journal naming the count read. A read that fails arms the bound once more, kept on the ask's
// read, never that its holder said nothing, and raises no call-unanswered. A read that succeeds between clears the mark. // mark; a second failed read at that bound ends the step saying the durable was not read. Neither says its holder said
// nothing, and neither raises call-unanswered. A read that succeeds between clears the mark.
func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) { func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) {
type read struct { type read struct {
pending, unacked uint64 pending, unacked uint64
@@ -161,6 +162,10 @@ func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) {
{"step ends wait: armed again, then ended once none waits", []read{{3, 1, nil}, {0, 0, nil}}, []string{"armed", "unanswered"}}, {"step ends wait: armed again, then ended once none waits", []read{{3, 1, nil}, {0, 0, nil}}, []string{"armed", "unanswered"}},
{"a failed read arms once more; a second ends saying the durable was not read", []read{{0, 0, errors.New("timeout")}, {0, 0, errors.New("timeout")}}, []string{"armed", "unread"}}, {"a failed read arms once more; a second ends saying the durable was not read", []read{{0, 0, errors.New("timeout")}, {0, 0, errors.New("timeout")}}, []string{"armed", "unread"}},
{"a read between two failed ones clears the first", []read{{0, 0, errors.New("timeout")}, {2, 0, nil}, {0, 0, errors.New("timeout")}, {0, 0, errors.New("timeout")}}, []string{"armed", "armed", "armed", "unread"}}, {"a read between two failed ones clears the first", []read{{0, 0, errors.New("timeout")}, {2, 0, nil}, {0, 0, errors.New("timeout")}, {0, 0, errors.New("timeout")}}, []string{"armed", "armed", "armed", "unread"}},
// 1984/AC2, notes 9 (d) and 10 (e): an end the runner takes again and again (the durables keep no delivery limit)
// never shrinks the count, so the bound is armed again only while the count shrinks, and a count no smaller ends the
// step saying it.
{"armed again only while the count shrinks; a count no smaller ends the step naming it", []read{{5, 0, nil}, {3, 1, nil}, {4, 0, nil}}, []string{"armed", "armed", "stuck"}},
} { } {
t.Run(c.name, func(t *testing.T) { t.Run(c.name, func(t *testing.T) {
v, store, _, _, _, journal := verbsFixture() v, store, _, _, _, journal := verbsFixture()
@@ -222,6 +227,9 @@ func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) {
if (m.Unread != nil) != (c.reads[i].err != nil) { if (m.Unread != nil) != (c.reads[i].err != nil) {
t.Errorf("bound %d: the ask's mark keeps unread %v after a read that failed %v", i+1, m.Unread, c.reads[i].err != nil) t.Errorf("bound %d: the ask's mark keeps unread %v after a read that failed %v", i+1, m.Unread, c.reads[i].err != nil)
} }
if n := c.reads[i].pending + c.reads[i].unacked; c.reads[i].err == nil && (m.Waited == nil || *m.Waited != n) {
t.Errorf("bound %d: the ask's mark keeps waited %v; want %d, the count read", i+1, m.Waited, n)
}
case "unanswered": case "unanswered":
if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != UnansweredWhy(ask) || len(raised) != 1 { if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != UnansweredWhy(ask) || len(raised) != 1 {
t.Fatalf("bound %d: the step is %s (%s), raised %v; want could not run: %s, and its finding", i+1, s.Outcome, s.Why, raised, UnansweredWhy(ask)) t.Fatalf("bound %d: the step is %s (%s), raised %v; want could not run: %s, and its finding", i+1, s.Outcome, s.Why, raised, UnansweredWhy(ask))
@@ -229,9 +237,13 @@ func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) {
if countLines(*journal, "0 not yet delivered, 0 unacknowledged") != 1 { if countLines(*journal, "0 not yet delivered, 0 unacknowledged") != 1 {
t.Errorf("bound %d: the journal %q does not name the count read", i+1, *journal) t.Errorf("bound %d: the journal %q does not name the count read", i+1, *journal)
} }
case "unread": case "unread", "stuck":
if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != unreadWhy(nodebuildgo.Seat, ask) || strings.Contains(s.Why, "said nothing") { what := unreadWhat(nodebuildgo.Seat)
t.Fatalf("bound %d: the step is %s (%s); want could not run: %s", i+1, s.Outcome, s.Why, unreadWhy(nodebuildgo.Seat, ask)) if want == "stuck" {
what = stuckWhat(nodebuildgo.Seat, 4, 4)
}
if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != notKnownWhy(ask, what) || strings.Contains(s.Why, "said nothing") {
t.Fatalf("bound %d: the step is %s (%s); want could not run: %s", i+1, s.Outcome, s.Why, notKnownWhy(ask, what))
} }
if len(raised) != 0 || len(dropped) != 1 { if len(raised) != 0 || len(dropped) != 1 {
t.Errorf("bound %d: raised %v, dropped %v; want no call-unanswered, which says its holder said nothing, and the ask dropped", i+1, raised, dropped) t.Errorf("bound %d: raised %v, dropped %v; want no call-unanswered, which says its holder said nothing, and the ask dropped", i+1, raised, dropped)
@@ -68,60 +68,18 @@ type MoverStore struct {
Raw RawState Raw RawState
Logf func(string, ...any) Logf func(string, ...any)
// mu guards the three maps and ended, which every pipeline's reads and writes share across goroutines (H §774): the // mu guards the three maps, which every pipeline's reads and writes share across goroutines (H §774): the lock table,
// lock table, the last reads and the records said inline. A pipeline's own lock is held across its read or write. // the last reads and the records said inline. A pipeline's own lock is held across its read or write.
mu sync.Mutex mu sync.Mutex
locks map[string]*sync.Mutex locks map[string]*sync.Mutex
read map[string]readAt read map[string]readAt
inline map[string]bool inline map[string]bool
// ended is the pipeline whose end this store wrote last: its mover keys' values are kept beside the running ones' (keep).
ended string
} }
// readAt is what the last read or write of a pipeline found: its record's revision and each mover key's revision, and for // readAt is what the last read of a pipeline found: its record's revision and each mover key's revision.
// a running pipeline whose record carries no steps inline each mover key's value (issue 1984): every write of a mover key
// writes the record too, so while the record is still at rev a read takes the mover keys from here and reads the record
// alone, and a step's end reads and writes only its record and its own mover key.
type readAt struct { type readAt struct {
rev uint64 rev uint64
movers map[string]uint64 movers map[string]uint64
vals map[string][]byte
}
// keep keeps r as the last read or write of pipeline key, whose record is p, under mu: the mover keys' values only of a
// running pipeline whose record carries no steps inline, and of the one pipeline whose end this store wrote last, so the
// writes that follow an end (its unlogged entries cleared) read no mover key either; what is kept is bounded by the
// pipelines running and one.
func (s *MoverStore) keep(key string, p seat.Pipeline, r readAt) {
s.mu.Lock()
defer s.mu.Unlock()
had := s.read[key].vals != nil
switch {
case len(p.Steps) > 0 || r.vals == nil:
r.vals = nil
case p.Outcome == seat.PipelineOutcomeRunning:
case had || s.ended == key:
if s.ended != key {
if e, ok := s.read[s.ended]; ok {
e.vals = nil
s.read[s.ended] = e
}
s.ended = key
}
default:
r.vals = nil
}
s.read[key] = r
}
// held is the mover keys' values and revisions kept from the last read or write of pipeline key, while its record is still
// at rev.
func (s *MoverStore) held(key string, rev uint64) (map[string][]byte, map[string]uint64, bool) {
r, ok := s.seen(key)
if !ok || r.rev != rev || r.vals == nil {
return nil, nil, false
}
return r.vals, r.movers, true
} }
func (s *MoverStore) lock(key string) func() { func (s *MoverStore) lock(key string) func() {
@@ -147,6 +105,13 @@ func (s *MoverStore) seen(key string) (readAt, bool) {
return r, ok return r, ok
} }
// setSeen keeps the last read of pipeline key, under mu.
func (s *MoverStore) setSeen(key string, r readAt) {
s.mu.Lock()
defer s.mu.Unlock()
s.read[key] = r
}
// markInline says whether pipeline key is said inline for the first time, marking it, under mu. // markInline says whether pipeline key is said inline for the first time, marking it, under mu.
func (s *MoverStore) markInline(key string) bool { func (s *MoverStore) markInline(key string) bool {
s.mu.Lock() s.mu.Lock()
@@ -332,14 +297,11 @@ func (s *MoverStore) Get(key string) (seat.Pipeline, uint64, error) {
if err := json.Unmarshal(raw, &p); err != nil { if err := json.Unmarshal(raw, &p); err != nil {
return seat.Pipeline{}, 0, fmt.Errorf("the pipeline %s is no pipeline: %w", key, err) return seat.Pipeline{}, 0, fmt.Errorf("the pipeline %s is no pipeline: %w", key, err)
} }
vals, _, ok := s.held(key, rev) vals, revs, err := s.readKeys(named(key, p))
if !ok { if err != nil {
var revs map[string]uint64 return seat.Pipeline{}, 0, err
if vals, revs, err = s.readKeys(named(key, p)); err != nil {
return seat.Pipeline{}, 0, err
}
s.keep(key, p, readAt{rev: rev, movers: revs, vals: vals})
} }
s.setSeen(key, readAt{rev: rev, movers: revs})
if len(p.Steps) > 0 { if len(p.Steps) > 0 {
if s.markInline(key) { if s.markInline(key) {
s.logf("the pipeline %s carries its %d steps inline in its record: they are read whole and split into one key per mover at its next write (H-A-187)", key, len(p.Steps)) s.logf("the pipeline %s carries its %d steps inline in its record: they are read whole and split into one key per mover at its next write (H-A-187)", key, len(p.Steps))
@@ -446,67 +408,58 @@ func (s *MoverStore) update(key string, p seat.Pipeline, revision uint64, kept m
} }
var was seat.Pipeline var was seat.Pipeline
inline := json.Unmarshal(raw, &was) == nil && len(was.Steps) > 0 inline := json.Unmarshal(raw, &was) == nil && len(was.Steps) > 0
// The mover keys as the last read or write left them while the record is at the revision read: every write of a mover // Each mover key the record and p name, and each the read found, is read by its name, never listed (issue 1984).
// key writes the record too (issue 1984). Else each key the record and p name is read by its name, never listed. seen, read := s.seen(key)
vals, revs, ok := s.held(key, revision) read = read && seen.rev == revision
if !ok { names := named(key, was)
names := named(key, was) for k := range want {
for k := range want { if !slices.Contains(names, k) {
names = append(names, k)
}
}
if read {
for k := range seen.movers {
if !slices.Contains(names, k) { if !slices.Contains(names, k) {
names = append(names, k) names = append(names, k)
} }
} }
seen, read := s.seen(key) }
read = read && seen.rev == revision vals, revs, err := s.readKeys(names)
if read { if err != nil {
for k := range seen.movers { return 0, err
if !slices.Contains(names, k) { }
names = append(names, k) // A mover key written since the read is another write between: the read named its revision.
} if read {
for k, r := range revs {
if seen.movers[k] != r {
return 0, ErrChanged
} }
} }
if vals, revs, err = s.readKeys(names); err != nil { for k := range seen.movers {
return 0, err if _, ok := revs[k]; !ok {
} return 0, ErrChanged
// A mover key written since the read is another write between: the read named its revision.
if read {
for k, r := range revs {
if r != seen.movers[k] {
return 0, ErrChanged
}
}
for k := range seen.movers {
if _, ok := revs[k]; !ok {
return 0, ErrChanged
}
} }
} }
} }
var after map[string]uint64 var after map[string]uint64
if inline { if inline {
if after, err = s.writeMovers(key, want, vals, revs); err != nil { if after, err = s.writeMovers(key, want, vals, revs); err != nil {
s.forget(key, true, false)
return 0, err return 0, err
} }
} }
rev, err := s.Raw.Update(key, record, revision) rev, err := s.Raw.Update(key, record, revision)
if err != nil { if err != nil {
if inline {
s.forget(key, true, false) // the mover keys are written and the record is not: nothing kept is read again
}
return 0, err return 0, err
} }
if !inline { if !inline {
if after, err = s.writeMovers(key, want, vals, revs); err != nil { if after, err = s.writeMovers(key, want, vals, revs); err != nil {
s.forget(key, true, false)
return 0, err return 0, err
} }
} else { } else {
s.logf("the pipeline %s's steps are split into %d mover keys and its record no longer carries them (H-A-187)", key, len(want)) s.logf("the pipeline %s's steps are split into %d mover keys and its record no longer carries them (H-A-187)", key, len(want))
s.forget(key, false, true) s.forget(key, false, true)
} }
p.Steps = nil // the record written carries no steps inline s.setSeen(key, readAt{rev: rev, movers: after})
s.keep(key, p, readAt{rev: rev, movers: after, vals: want})
return rev, nil return rev, nil
} }
@@ -570,8 +523,7 @@ func (s *MoverStore) Create(key string, value []byte) error {
if err != nil { if err != nil {
return err return err
} }
p.Steps = nil // the record written carries no steps inline s.setSeen(key, readAt{rev: rev, movers: after})
s.keep(key, p, readAt{rev: rev, movers: after, vals: want})
return nil return nil
} }
@@ -122,24 +122,15 @@ func TestMoverKeys(t *testing.T) {
q, _, _ := s.Get(key) q, _, _ := s.Get(key)
return fmt.Sprintf("beta kept %t: %s %v", m.revs["steps.main.7.beta"] == beta, steps(q), err) return fmt.Sprintf("beta kept %t: %s %v", m.revs["steps.main.7.beta"] == beta, steps(q), err)
}, "beta kept true: beta build running; beta approval not asked; alpha build passed <nil>"}, }, "beta kept true: beta build running; beta approval not asked; alpha build passed <nil>"},
{"a mover key written since the read of a pipeline not running refuses the update, nothing written", func(t *testing.T) string { {"a mover key written since the read refuses the update, nothing written", func(t *testing.T) string {
m := newMemRaw() m, s, _ := created(t)
s := &MoverStore{Raw: m}
ended := two()
ended.Outcome = seat.PipelineOutcomeFailed
raw, _ := json.Marshal(ended)
if err := s.Create(key, raw); err != nil {
return err.Error()
}
p, rev, _ := s.Get(key) p, rev, _ := s.Get(key)
m.put("steps.main.7.alpha", m.vals["steps.main.7.alpha"]) m.put("steps.main.7.alpha", m.vals["steps.main.7.alpha"])
p.Why = "read again" p.Outcome = seat.PipelineOutcomePassed
_, err := s.Update(key, p, rev) _, err := s.Update(key, p, rev)
return fmt.Sprintf("%t, record at %t", errors.Is(err, ErrChanged), m.revs[key] == rev) return fmt.Sprintf("%t, record at %t", errors.Is(err, ErrChanged), m.revs[key] == rev)
}, "true, record at true"}, }, "true, record at true"},
// 1984/AC1: a running pipeline's mover keys are kept from the last read or write while its record is at the revision {"a mover key another store wrote since the read refuses the update, and the next read reads it", func(t *testing.T) string {
// read; every write of a mover key writes the record too, so another store's write is never missed.
{"a mover key another store wrote since the read refuses the update, and the next read reads it (1984/AC1)", func(t *testing.T) string {
m, s, _ := created(t) m, s, _ := created(t)
p, rev, _ := s.Get(key) p, rev, _ := s.Get(key)
other := &MoverStore{Raw: m} other := &MoverStore{Raw: m}
@@ -153,44 +144,6 @@ func TestMoverKeys(t *testing.T) {
again, _, _ := s.Get(key) again, _, _ := s.Get(key)
return fmt.Sprintf("%t: %s", errors.Is(err, ErrChanged), steps(again)) return fmt.Sprintf("%t: %s", errors.Is(err, ErrChanged), steps(again))
}, "true: beta build running; beta approval not asked; alpha build passed"}, }, "true: beta build running; beta approval not asked; alpha build passed"},
{"each key is read by its name, none listed; while the record is at the revision kept a read reads the record alone (1984/AC1)", func(t *testing.T) string {
m, _, _ := created(t)
c := &countedRaw{memRaw: m}
fresh := &MoverStore{Raw: c} // a store after a restart keeps nothing yet
p, rev, _ := fresh.Get(key)
first := fmt.Sprintf("%d reads, %d listings", len(c.reads), c.listings)
c.reset()
_, _, _ = fresh.Get(key)
second := fmt.Sprintf("%v, %d listings", c.reads, c.listings)
c.reset()
p.Steps[0].Outcome = seat.StepResultOutcomePassed
if _, err := fresh.Update(key, p, rev); err != nil {
return err.Error()
}
return fmt.Sprintf("%s; %s; update read %v wrote %v, %d listings", first, second, c.reads, c.writes, c.listings)
}, "3 reads, 0 listings; [main.7], 0 listings; update read [main.7] wrote [main.7 steps.main.7.beta], 0 listings"},
{"an ended pipeline's mover keys are kept only until another pipeline's end is written (1984/AC1)", func(t *testing.T) string {
m := newMemRaw()
c := &countedRaw{memRaw: m}
s := &MoverStore{Raw: c}
end := func(k string) {
p := two()
p.Pipeline = k
raw, _ := json.Marshal(p)
_ = s.Create(k, raw)
q, rev, _ := s.Get(k)
q.Outcome = seat.PipelineOutcomePassed
_, _ = s.Update(k, q, rev)
}
end("main.7")
c.reset()
_, _, _ = s.Get("main.7")
kept := len(c.reads)
end("main.8")
c.reset()
_, _, _ = s.Get("main.7")
return fmt.Sprintf("its end's read %d, after another's end %d", kept, len(c.reads))
}, "its end's read 1, after another's end 3"},
{"a record written since the read refuses the update", func(t *testing.T) string { {"a record written since the read refuses the update", func(t *testing.T) string {
_, s, _ := created(t) _, s, _ := created(t)
p, rev, _ := s.Get(key) p, rev, _ := s.Get(key)
@@ -97,11 +97,12 @@ func TestAMoversBuildWaitsForItsDependenciesInThePipeline(t *testing.T) {
before := mem["main.7"] before := mem["main.7"]
// H-A-311 (2): the build's end written while its placing was cut (heard by a hearer that places nothing): the same end // H-A-311 (2): the build's end written while its placing was cut (heard by a hearer that places nothing): the same end
// heard again, late, moves nothing of the build and still places tool, which that end unlocked. // heard again, late, moves nothing of the build and still places tool, which that end unlocked.
cut := *h release := h.Release
cut.Release = nil h.Release = nil
if _, err := cut.OnStepEnded(bs, bs.Prefix+"n1", end("passed")); err != nil { if _, err := h.OnStepEnded(bs, bs.Prefix+"n1", end("passed")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
h.Release = release
if p, _, _ = mem.Get("main.7"); placed(p, "tool") { if p, _, _ = mem.Get("main.7"); placed(p, "tool") {
t.Fatalf("the cut placing placed tool: %+v", stepsOf(p, "tool")) t.Fatalf("the cut placing placed tool: %+v", stepsOf(p, "tool"))
} }
@@ -79,13 +79,16 @@ func IsRunAsk(key string) bool { return strings.HasPrefix(key, AskPrefix) }
// AskMark is what an ask key holds (H-A-506 addendum): the pipeline that put the ask, the mover and the step it asks, and // AskMark is what an ask key holds (H-A-506 addendum): the pipeline that put the ask, the mover and the step it asks, and
// the test run it asks, empty for an ask of no test run. An ask key written before the addendum holds its run alone. Unread // the test run it asks, empty for an ask of no test run. An ask key written before the addendum holds its run alone. Unread
// is when the runner last could not read its own step-ended durable of the ask's seat as the ask's bound passed, cleared // is when the runner last could not read its own step-ended durable of the ask's seat as the ask's bound passed, cleared
// by a read that succeeds (issue 1984 note 7 (c)): a second failed read at the bound after ends the step. // by a read that succeeds (issue 1984 note 7 (c)): a second failed read at the bound after ends the step. Waited is how
// many step ends waited in that durable at the last bound that armed the bound again (notes 9 (d), 10 (e)): a count no
// smaller at the bound after ends the step.
type AskMark struct { type AskMark struct {
Pipeline string `json:"pipeline,omitempty"` Pipeline string `json:"pipeline,omitempty"`
Module string `json:"module,omitempty"` Module string `json:"module,omitempty"`
Step string `json:"step,omitempty"` Step string `json:"step,omitempty"`
Run string `json:"run,omitempty"` Run string `json:"run,omitempty"`
Unread *time.Time `json:"unread,omitempty"` Unread *time.Time `json:"unread,omitempty"`
Waited *uint64 `json:"waited,omitempty"`
} }
// markOf is the mark of ask a, put by the pipeline key. // markOf is the mark of ask a, put by the pipeline key.
@@ -11,16 +11,11 @@ import (
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo" nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
) )
// countedRaw is memRaw counting what each call reads and writes: the keys read, the keys written and the listings. // countedRaw is memRaw counting what each call writes, and the listings.
type countedRaw struct { type countedRaw struct {
*memRaw *memRaw
reads, writes []string writes []string
listings int listings int
}
func (c *countedRaw) Get(key string) ([]byte, uint64, error) {
c.reads = append(c.reads, key)
return c.memRaw.Get(key)
} }
func (c *countedRaw) Create(key string, value []byte) (uint64, error) { func (c *countedRaw) Create(key string, value []byte) (uint64, error) {
@@ -38,10 +33,10 @@ func (c *countedRaw) Keys(filter string) ([]string, error) {
return c.memRaw.Keys(filter) return c.memRaw.Keys(filter)
} }
func (c *countedRaw) reset() { c.reads, c.writes, c.listings = nil, nil, 0 } func (c *countedRaw) reset() { c.writes, c.listings = nil, 0 }
// 1984/AC1 (issue 1984 note 6; ADR 0682 §11): a step's end does the step's end alone. Its handler writes the record and its // 1984/AC1 (issue 1984 note 6; ADR 0682 §11): a step's end does the step's end alone. Its handler writes the record and its
// own mover key, lists no key and opens no consumer; the ageing and the stores' fill check run as work of their own, woken // own mover key, reads each key by its name, lists none and so opens no consumer; the ageing and the stores' fill check run as work of their own, woken
// by the step ends: the ends heard before a pass merge into one pass, and never more than one pass runs. // by the step ends: the ends heard before a pass merge into one pass, and never more than one pass runs.
func TestAStepEndDoesOnlyTheStepsEnd(t *testing.T) { func TestAStepEndDoesOnlyTheStepsEnd(t *testing.T) {
raw := &countedRaw{memRaw: newMemRaw()} raw := &countedRaw{memRaw: newMemRaw()}
@@ -88,11 +83,6 @@ func TestAStepEndDoesOnlyTheStepsEnd(t *testing.T) {
t.Errorf("the end of %s wrote %s; want only the record and %s", ask, k, mine) t.Errorf("the end of %s wrote %s; want only the record and %s", ask, k, mine)
} }
} }
for _, k := range raw.reads {
if k != pkey && k != mine {
t.Errorf("the end of %s read %s; want only the record and %s", ask, k, mine)
}
}
if raw.listings != 0 { if raw.listings != 0 {
t.Errorf("the end of %s listed the keys %d times, a consumer each; want none", ask, raw.listings) t.Errorf("the end of %s listed the keys %d times, a consumer each; want none", ask, raw.listings)
} }
@@ -7,6 +7,7 @@ import (
"slices" "slices"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
@@ -223,9 +224,10 @@ type Hearer struct {
Aside func(do func()) Aside func(do func())
// lateRuns counts the late events of ended runs' asks acknowledged at once (LateRunAsk, H-A-500 (a)). // lateRuns counts the late events of ended runs' asks acknowledged at once (LateRunAsk, H-A-500 (a)).
lateRuns int64 lateRuns int64
// ageWake is the nudge of AgeOnStepEnds, made at its first use under ageWakeMade: one nudge waits at most, the rest merge // ageWake is the nudge of AgeOnStepEnds, made at its first use under ageWakeMade, whichever of a step's end and
// into it (issue 1984). // AgeOnStepEnds comes first: one nudge waits at most, the rest merge into it (issue 1984).
ageWake chan struct{} ageWakeMade sync.Mutex
ageWake chan struct{}
} }
// EndReaders ends each of readers' steps of run with r's result (§725, §726): a step that read an ended run, or the steps // EndReaders ends each of readers' steps of run with r's result (§725, §726): a step that read an ended run, or the steps