diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/askbounds.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/askbounds.go index 01ebd2af8..96c7bae84 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/askbounds.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/askbounds.go @@ -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 // 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 -// step and cleared when the ask is answered after all (H-A-238 (2)). -func askClock(ctx context.Context, bounds jetstream.KeyValue, bound time.Duration, w callFindings, module string, now func() time.Time) *pipeline.AskClock { - return &pipeline.AskClock{Bound: bound, +// 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 +// named durable on the seat's step-ended stream is read (issue 1984): an end waiting there is not the holder's silence. +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 { _, err := bounds.Create(ctx, key, []byte(`{"ask":"`+key+`"}`), jetstream.KeyTTL(ttl)) if errors.Is(err, jetstream.ErrKeyExists) { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go index 40d98124d..e8418b4e8 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -186,9 +186,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro hearer.AskSteps = starter.AskSteps 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) - clock := askClock(ctx, bounds, askBound, rv.Findings, cred.Module, time.Now) - // 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()) + clock := askClock(ctx, bounds, askBound, rv.Findings, cred.Module, time.Now, js, cred.EventConsumerName()) starter.Asks, hearer.Asks = clock, clock 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) diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/stependswaiting_integration_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/stependswaiting_integration_test.go index bd5a18a01..0d5c622a4 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/stependswaiting_integration_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/stependswaiting_integration_test.go @@ -18,7 +18,8 @@ import ( // 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 // 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) { nc, err := nats.Connect(testbus.URL(t)) if err != nil { @@ -29,7 +30,11 @@ func testABoundPassingReadsTheRunnersOwnStepEndedDurable(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() 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) { t.Helper() p, u, err := waiting(onSeat) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/aged.go b/modules/mesh-delivery-pipeline/internal/pipeline/aged.go index 35cdcb07c..fe49a2185 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/aged.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/aged.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "sync" "time" 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. func (h *Hearer) ageNudge() chan struct{} { - ageWakeMade.Lock() - defer ageWakeMade.Unlock() + h.ageWakeMade.Lock() + defer h.ageWakeMade.Unlock() if h.ageWake == nil { h.ageWake = make(chan struct{}, 1) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/asks.go b/modules/mesh-delivery-pipeline/internal/pipeline/asks.go index 0727ef8a4..d86c568d1 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/asks.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/asks.go @@ -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" } -// unreadWhy is the why of a step whose bound passed twice in a row while the runner could not read its own step-ended -// durable of onSeat (issue 1984 note 7 (b)): whether its end waits there is not known, so its holder is not said silent. -func unreadWhy(onSeat, ask string) string { - return "its ask " + ask + " is not known to be answered: the runner could not read its own step-ended durable of " + onSeat + - " at two bounds in a row" +// 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). @@ -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 // the holder (issue 1984 notes 6, 7). - read, unread := "", false + read, unknown := "", "" if c.Waiting != nil && c.Rearm != nil { 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 } } - if unread { - why = unreadWhy(onSeat, s.Ask) - 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)", - s.Ask, s.Step, key, onSeat, c.bound(), onSeat) + 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) @@ -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) } } - // The finding call-unanswered says its holder said nothing, which is not known of an ask whose ends were not read: its - // step's why and the pipeline's failure say it. - if c.Raise != nil && !unread { + // 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): while step ends wait there, or at the first read that fails, the bound is armed again whole and said, again -// true; a read that fails at the bound after one that failed answers unread true; else it answers what the journal adds to -// the step's end, the count read, and the ask's mark loses its unread. -func (v Verbs) endsWaiting(c *AskClock, key, onSeat string, s seat.StepResult) (read string, again, unread bool) { +// 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) - var mark AskMark - marked := false - if h.Clock != nil { - var merr error - if mark, marked, merr = h.Clock.AskOf(s.Ask); 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 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, true - } - 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 + return "", false, unreadWhat(onSeat) } now := h.now() mark.Unread = &now - 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 the runner's own step-ended durable of %s is not read (%v), and the ask's mark keeping that is not written (%v)", - s.Ask, s.Step, key, onSeat, onSeat, err, werr) - return "", false, true + 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) } - if rerr := c.Rearm(boundKey, c.bound()); rerr != nil { - 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 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", - s.Ask, s.Step, key, onSeat, onSeat, err, c.bound()) - return "", true, false + 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, "" } - if mark.Unread != nil && h.Clock != nil { - mark.Unread = nil - if werr := h.Clock.MarkAskAgain(s.Ask, mark); werr != nil { - h.logf("the mark of ask %s keeps its unread, its durable read now: %v", s.Ask, werr) + 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) } - if n := pending + unacked; n > 0 { - if rerr := c.Rearm(boundKey, c.bound()); rerr != nil { - h.logf("the bound of ask %s on %s is not armed again while step ends wait: %v", s.Ask, onSeat, rerr) - } - 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 + 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) } - 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. diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go index 874d03ac1..a9f8dd57c 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go @@ -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 -// 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 -// wait. With none waiting the step ends as before, its why true, the journal naming the count read. A read that fails arms -// 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 -// read, never that its holder said nothing, and raises no call-unanswered. A read that succeeds between clears the mark. +// 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, fewer than at the bound before, the step is not ended: its bound is armed +// again and the journal says how many wait; a count no smaller ends the step naming it. With none waiting the step ends as +// before, its why true, the journal naming the count read. A read that fails arms 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 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) { type read struct { 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"}}, {"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"}}, + // 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) { v, store, _, _, _, journal := verbsFixture() @@ -222,6 +227,9 @@ func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) { 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) } + 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": 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)) @@ -229,9 +237,13 @@ func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) { 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) } - case "unread": - if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != unreadWhy(nodebuildgo.Seat, ask) || 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, unreadWhy(nodebuildgo.Seat, ask)) + case "unread", "stuck": + what := unreadWhat(nodebuildgo.Seat) + 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 { 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) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys.go b/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys.go index a96d5ab51..b6598bd08 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys.go @@ -68,60 +68,18 @@ type MoverStore struct { Raw RawState Logf func(string, ...any) - // mu guards the three maps and ended, which every pipeline's reads and writes share across goroutines (H §774): the - // lock table, the last reads and the records said inline. A pipeline's own lock is held across its read or write. + // mu guards the three maps, which every pipeline's reads and writes share across goroutines (H §774): the lock table, + // the last reads and the records said inline. A pipeline's own lock is held across its read or write. mu sync.Mutex locks map[string]*sync.Mutex read map[string]readAt 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 -// 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. +// readAt is what the last read of a pipeline found: its record's revision and each mover key's revision. type readAt struct { rev 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() { @@ -147,6 +105,13 @@ func (s *MoverStore) seen(key string) (readAt, bool) { 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. func (s *MoverStore) markInline(key string) bool { 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 { return seat.Pipeline{}, 0, fmt.Errorf("the pipeline %s is no pipeline: %w", key, err) } - vals, _, ok := s.held(key, rev) - if !ok { - var revs map[string]uint64 - 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}) + vals, revs, err := s.readKeys(named(key, p)) + if err != nil { + return seat.Pipeline{}, 0, err } + s.setSeen(key, readAt{rev: rev, movers: revs}) if len(p.Steps) > 0 { 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)) @@ -446,67 +408,58 @@ func (s *MoverStore) update(key string, p seat.Pipeline, revision uint64, kept m } var was seat.Pipeline 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 - // key writes the record too (issue 1984). Else each key the record and p name is read by its name, never listed. - vals, revs, ok := s.held(key, revision) - if !ok { - names := named(key, was) - for k := range want { + // Each mover key the record and p name, and each the read found, is read by its name, never listed (issue 1984). + seen, read := s.seen(key) + read = read && seen.rev == revision + names := named(key, was) + 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) { names = append(names, k) } } - seen, read := s.seen(key) - read = read && seen.rev == revision - if read { - for k := range seen.movers { - if !slices.Contains(names, k) { - names = append(names, k) - } + } + vals, revs, err := s.readKeys(names) + if err != nil { + return 0, err + } + // 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 { - return 0, err - } - // 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 - } + for k := range seen.movers { + if _, ok := revs[k]; !ok { + return 0, ErrChanged } } } var after map[string]uint64 if inline { if after, err = s.writeMovers(key, want, vals, revs); err != nil { - s.forget(key, true, false) return 0, err } } rev, err := s.Raw.Update(key, record, revision) 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 } if !inline { if after, err = s.writeMovers(key, want, vals, revs); err != nil { - s.forget(key, true, false) return 0, err } } 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.forget(key, false, true) } - p.Steps = nil // the record written carries no steps inline - s.keep(key, p, readAt{rev: rev, movers: after, vals: want}) + s.setSeen(key, readAt{rev: rev, movers: after}) return rev, nil } @@ -570,8 +523,7 @@ func (s *MoverStore) Create(key string, value []byte) error { if err != nil { return err } - p.Steps = nil // the record written carries no steps inline - s.keep(key, p, readAt{rev: rev, movers: after, vals: want}) + s.setSeen(key, readAt{rev: rev, movers: after}) return nil } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys_test.go index 29a1c7619..434ff3ec8 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/moverkeys_test.go @@ -122,24 +122,15 @@ func TestMoverKeys(t *testing.T) { q, _, _ := s.Get(key) 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 "}, - {"a mover key written since the read of a pipeline not running refuses the update, nothing written", func(t *testing.T) string { - m := newMemRaw() - 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() - } + {"a mover key written since the read refuses the update, nothing written", func(t *testing.T) string { + m, s, _ := created(t) p, rev, _ := s.Get(key) 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) return fmt.Sprintf("%t, record at %t", errors.Is(err, ErrChanged), m.revs[key] == rev) }, "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 - // 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 { + {"a mover key another store wrote since the read refuses the update, and the next read reads it", func(t *testing.T) string { m, s, _ := created(t) p, rev, _ := s.Get(key) other := &MoverStore{Raw: m} @@ -153,44 +144,6 @@ func TestMoverKeys(t *testing.T) { again, _, _ := s.Get(key) return fmt.Sprintf("%t: %s", errors.Is(err, ErrChanged), steps(again)) }, "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 { _, s, _ := created(t) p, rev, _ := s.Get(key) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/order_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/order_test.go index ea457f0ec..91399bfc6 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/order_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/order_test.go @@ -97,11 +97,12 @@ func TestAMoversBuildWaitsForItsDependenciesInThePipeline(t *testing.T) { 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 // heard again, late, moves nothing of the build and still places tool, which that end unlocked. - cut := *h - cut.Release = nil - if _, err := cut.OnStepEnded(bs, bs.Prefix+"n1", end("passed")); err != nil { + release := h.Release + h.Release = nil + if _, err := h.OnStepEnded(bs, bs.Prefix+"n1", end("passed")); err != nil { t.Fatal(err) } + h.Release = release if p, _, _ = mem.Get("main.7"); placed(p, "tool") { t.Fatalf("the cut placing placed tool: %+v", stepsOf(p, "tool")) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/runends.go b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go index ebadac686..f5745d664 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/runends.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go @@ -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 // 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 -// 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 { Pipeline string `json:"pipeline,omitempty"` Module string `json:"module,omitempty"` Step string `json:"step,omitempty"` Run string `json:"run,omitempty"` Unread *time.Time `json:"unread,omitempty"` + Waited *uint64 `json:"waited,omitempty"` } // markOf is the mark of ask a, put by the pipeline key. diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/stepend_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/stepend_test.go index 7f1081bf4..b3eae80eb 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/stepend_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/stepend_test.go @@ -11,16 +11,11 @@ import ( 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 { *memRaw - reads, writes []string - listings int -} - -func (c *countedRaw) Get(key string) ([]byte, uint64, error) { - c.reads = append(c.reads, key) - return c.memRaw.Get(key) + writes []string + listings int } 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) } -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 -// 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. func TestAStepEndDoesOnlyTheStepsEnd(t *testing.T) { 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) } } - 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 { t.Errorf("the end of %s listed the keys %d times, a consumer each; want none", ask, raw.listings) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go index 6b253e20b..ec3d1201d 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go @@ -7,6 +7,7 @@ import ( "slices" "sort" "strings" + "sync" "time" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" @@ -223,9 +224,10 @@ type Hearer struct { Aside func(do func()) // lateRuns counts the late events of ended runs' asks acknowledged at once (LateRunAsk, H-A-500 (a)). lateRuns int64 - // ageWake is the nudge of AgeOnStepEnds, made at its first use under ageWakeMade: one nudge waits at most, the rest merge - // into it (issue 1984). - ageWake chan struct{} + // ageWake is the nudge of AgeOnStepEnds, made at its first use under ageWakeMade, whichever of a step's end and + // AgeOnStepEnds comes first: one nudge waits at most, the rest merge into it (issue 1984). + 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