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 a6accd917..3d42b1674 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/askbounds.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/askbounds.go @@ -9,7 +9,6 @@ import ( "github.com/nats-io/nats.go/jetstream" - nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" @@ -21,20 +20,12 @@ import ( // (design 47 H H-A-238 (1)). const envAskUnansweredBound = "MESH_RUNNER_ASK_UNANSWERED_BOUND_FILE" -// envAskSilentBound names the file of the setting ask-silent-bound (H-A-243 (2)). -const envAskSilentBound = "MESH_RUNNER_ASK_SILENT_BOUND_FILE" - // askUnansweredBound reads the setting ask-unanswered-bound at the start, a Go duration (H-A-235, H-A-238 (1)): its default // ten minutes when the file is missing or empty, an error naming the file for a content that is no positive duration. func askUnansweredBound(getenv func(string) string) (time.Duration, error) { return durationSetting(getenv, envAskUnansweredBound, pipeline.DefaultAskUnansweredBound) } -// askSilentBound reads the setting ask-silent-bound at the start, a Go duration, forty-five minutes by default (H-A-243 (2)). -func askSilentBound(getenv func(string) string) (time.Duration, error) { - return durationSetting(getenv, envAskSilentBound, pipeline.DefaultAskSilentBound) -} - func durationSetting(getenv func(string) string, env string, def time.Duration) (time.Duration, error) { v, err := settingText(getenv(env)) if err != nil || v == "" { @@ -47,11 +38,12 @@ func durationSetting(getenv func(string) string, env string, def time.Duration) return d, nil } -// askClock is the bound of every ask but a test run's over the state bounds (H-A-238 (5)): a key armed with its own time to -// live before the ask is put, deleted at its holder's first word, its expiry heard by watchBounds; the finding call-unanswered, -// scope call, id the ask, raised at the second bound and cleared when the ask is answered after all (H-A-238 (2)). -func askClock(ctx context.Context, bounds jetstream.KeyValue, bound, silent time.Duration, w callFindings, module string, now func() time.Time) *pipeline.AskClock { - return &pipeline.AskClock{Bound: bound, Silent: silent, +// 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, 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) { @@ -59,14 +51,15 @@ func askClock(ctx context.Context, bounds jetstream.KeyValue, bound, silent time } return err }, - // A first word is heard for every ask, most of them unbounded or answered already: a key not there is deleted of - // nothing and writes no marker. - Unbound: func(key string) error { - if _, err := bounds.Get(ctx, key); errors.Is(err, jetstream.ErrKeyNotFound) { - return nil - } else if err != nil { + // A key armed again is deleted and created with its new time to live: a delete is never taken as an expiry. + Rearm: func(key string, ttl time.Duration) error { + if err := bounds.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) { return err } + _, err := bounds.Create(ctx, key, []byte(`{"ask":"`+key+`"}`), jetstream.KeyTTL(ttl)) + return err + }, + Unbound: func(key string) error { if err := bounds.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) { return err } @@ -79,7 +72,7 @@ func askClock(ctx context.Context, bounds jetstream.KeyValue, bound, silent time return w.Write(ctx, condseat.Finding{Scope: mcc.ScopeCall, ID: ask, Kind: mcc.KindCallUnanswered, Severity: condseat.FindingSeverityWarning, Summary: fmt.Sprintf("the ask %s of the seat %s is unanswered", ask, onSeat), Headline: "A step's ask is not answered", - Explanation: fmt.Sprintf("The runner put the ask %s on the seat %s and heard no word of its holder about it within %s, so its step ended could not run and its pipeline went on (H-A-235, H-A-243).", ask, onSeat, within), + Explanation: fmt.Sprintf("The runner put the ask %s on the seat %s and heard no word of its holder about it within %s, so its step ended could not run, its ask was dropped and its pipeline went on (design 47 H2 §778).", ask, onSeat, within), Source: module + "." + mcc.KindCallUnanswered, Observer: module, At: now().UTC(), Subject: condseat.Subject{Scope: mcc.ScopeCall, ID: ask}}) }, Clear: func(_, ask string) error { @@ -90,15 +83,16 @@ func askClock(ctx context.Context, bounds jetstream.KeyValue, bound, silent time }} } -// answered deletes the bounds of the ask on onSeat at its holder's first word, said when it fails: the bound then passes and -// its expiry finds the step answered or asks it once more. -func answered(c *pipeline.AskClock, onSeat, ask string, logf func(string, ...any)) { - if err := c.Answered(onSeat, ask); err != nil { - logf("the bounds of ask %s on %s are not deleted at its holder's word: %v", ask, onSeat, err) +// progressed arms the bound of the ask on onSeat again from now, at a step-progress its holder says for it (H-A-395), +// said when it fails: the bound then ends the step at its time. +func progressed(c *pipeline.AskClock, onSeat, ask string, logf func(string, ...any)) { + if err := c.Progress(onSeat, ask); err != nil { + logf("the bound of ask %s on %s is not armed again at its holder's step-progress: %v", ask, onSeat, err) } } -// hearWantsProgress takes a step progress of the seat mesh-wants as its holder's first word on the ask it names (H-A-238 (3)). +// hearWantsProgress takes a step progress of the seat mesh-wants as its holder's word on the ask it names: the ask's bound +// is armed again from now (H-A-395). func hearWantsProgress(hub handlers, c *pipeline.AskClock, logf func(string, ...any)) error { return hub.Handle(wants.SubjectStepProgressAll, func(m jetstream.Msg) { var ev wants.StepProgressEvent @@ -107,47 +101,19 @@ func hearWantsProgress(hub handlers, c *pipeline.AskClock, logf func(string, ... _ = m.Term() return } - answered(c, wants.Seat, ev.Ask, logf) + progressed(c, wants.Seat, ev.Ask, logf) _ = m.Ack() }) } -// holdersNotReadDelay is how long a running heard before the seat-holders view is read waits before it is heard again. -const holdersNotReadDelay = 2 * time.Second - -// hearHolderStarted puts again the open asks of each silent seat whose holder the node-engine says running (H-A-243 (1)). -func hearHolderStarted(hub handlers, started func(module string) (int, error), logf func(string, ...any)) error { - return hub.Handle(nodeengine.SubjectRunningAll, func(m jetstream.Msg) { - var ev nodeengine.Running - if err := json.Unmarshal(m.Data(), &ev); err != nil || ev.Module == "" { - logf("%s is no running: %v", m.Subject(), err) - _ = m.Term() - return - } - if ev.Stopped { - _ = m.Ack() - return - } - n, err := started(ev.Module) - switch { - case errors.Is(err, pipeline.ErrHoldersNotRead): - _ = m.NakWithDelay(holdersNotReadDelay) // heard again once the view is read, without spinning until then - return - case err != nil: - logf("the asks of the seats %s holds, put again as it runs on %s: %d, and one is not: %v", ev.Module, ev.Node, n, err) - } - _ = m.Ack() - }) -} - -// askBoundExpired takes the expiry of an ask's bound (H-A-235): the first asks once more, the second ends its step. -func askBoundExpired(unanswered func(onSeat, ask string, again bool) error, logf func(string, ...any)) func(key string) bool { +// askBoundExpired takes the expiry of an ask's bound (§778): it ends its step. +func askBoundExpired(unanswered func(onSeat, ask string) error, logf func(string, ...any)) func(key string) bool { return func(key string) bool { - s, ask, again, ok := pipeline.ParseAskBound(key) + s, ask, ok := pipeline.ParseAskBound(key) if !ok { return false } - if err := unanswered(s, ask, again); err != nil { + if err := unanswered(s, ask); err != nil { logf("the bound of ask %s on %s passed and is not taken: %v", ask, s, err) } return true diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks.go index d82af610f..e28594d3e 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks.go @@ -22,7 +22,8 @@ import ( // duplicate id is the ask's own, so an ask put twice is kept once (design 47 G §22, §24). The put is a kept call of the // paced form (design 47 H2 §775): a put that met a transport fault is put again under the same id at the kept-call pace, // until the runner's ask-unanswered-bound (bound); past it the error names every put's time and words. A refusal of the -// put returns at once. +// put returns at once, but for one refused by a full stream: that is said in the journal and put again at the same pace +// (design 47 H2 §777). type busStepAsker struct { ctx context.Context publish func(context.Context, *nats.Msg, ...jetstream.PublishOpt) (*jetstream.PubAck, error) @@ -38,6 +39,7 @@ func (b busStepAsker) AskStep(accept, id string, body any) error { } seatName, kind := acceptedOf(accept) k := busretry.Kept{Seat: seatName, Verb: "accept." + kind, Ask: id, Bound: b.bound, Logf: b.logf} + said := false _, err = busretry.DoPaced(k, func() (*jetstream.PubAck, error) { msg := nats.NewMsg(accept) msg.Header.Set("x-event-id", id) @@ -47,11 +49,28 @@ func (b busStepAsker) AskStep(accept, id string, body any) error { msg.Header.Set("content-type", "application/json") msg.Header.Set(nats.MsgIdHdr, id) msg.Data = data - return b.publish(b.ctx, msg) + ack, err := b.publish(b.ctx, msg) + if fullStream(err) { + if !said && b.logf != nil { + b.logf("the ask %s is refused by %s's full work queue (%v): put again at the kept-call pace", id, seatName, err) + said = true + } + return ack, &busretry.Away{Part: "the work queue of " + seatName, Words: err.Error()} + } + return ack, err }) return err } +// fullStream says err is a work queue's refusal of a put for being full: its messages or bytes at their cap. +func fullStream(err error) bool { + if err == nil { + return false + } + words := strings.ToLower(err.Error()) + return strings.Contains(words, "maximum messages") || strings.Contains(words, "maximum bytes") +} + // acceptedOf is the seat and the accepted kind an accept subject names (mesh.seat..accept.); a subject of // another form names itself as the seat. func acceptedOf(accept string) (seatName, kind string) { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks_test.go index 109a060df..5eccc6f8c 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/asks_test.go @@ -3,6 +3,9 @@ package main import ( "context" "errors" + "fmt" + "slices" + "strings" "testing" "time" @@ -28,6 +31,8 @@ func TestAStepsAskIsAKeptPut(t *testing.T) { }{ {name: "put again through no responders until it lands", faults: []error{nats.ErrNoResponders, nats.ErrTimeout}, bound: time.Minute, puts: 3, want: func(err error) bool { return err == nil }}, + {name: "a put refused by a full stream is said and put again", faults: []error{errors.New("nats: maximum messages exceeded")}, + bound: time.Minute, puts: 2, want: func(err error) bool { return err == nil }}, {name: "a refusal returns at once", always: refused, bound: time.Minute, puts: 1, want: func(err error) bool { return errors.Is(err, refused) }}, {name: "past the bound every put is named", always: nats.ErrNoResponders, bound: 20 * time.Millisecond, puts: -1, @@ -48,8 +53,9 @@ func TestAStepsAskIsAKeptPut(t *testing.T) { } return &jetstream.PubAck{}, nil } + var said []string a := busStepAsker{ctx: context.Background(), publish: publish, cred: process.Account{Module: "mesh-delivery-pipeline"}, - bound: tc.bound, logf: func(string, ...any) {}} + bound: tc.bound, logf: func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }} err := a.AskStep("mesh.seat.mesh-wants.accept.step", "ask-1", map[string]string{"step": "composition"}) if !tc.want(err) { t.Fatalf("AskStep = %v", err) @@ -57,6 +63,9 @@ func TestAStepsAskIsAKeptPut(t *testing.T) { if tc.puts > 0 && len(ids) != tc.puts { t.Fatalf("put %d times, want %d", len(ids), tc.puts) } + if full := strings.Contains(tc.name, "full"); full != slices.ContainsFunc(said, func(l string) bool { return strings.Contains(l, "full work queue") }) { + t.Errorf("the journal %q, want a line of the full stream: %v", said, full) + } for _, id := range ids { if id != "ask-1" { t.Fatalf("a put carried the id %q, want the ask's own", id) diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go index d37e6f7e5..7191b9bc6 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go @@ -54,24 +54,33 @@ func TestADropBoundsExpirySaysCallUnanswered(t *testing.T) { } } -// H-A-238 (5), review: at a start, a first bound's expiry is taken only when the first read holds no entry for its second -// bound (put again and taken: a delete marker; put again: a live put; both passed: an expiry, the second taken alone); a -// second bound's expiry is taken; a delete marker or a drop's expiry is none. +// Design 47 H2 §778: at a start, an ask's bound that expired while the runner was down (the purge marker) is taken; a delete +// marker (armed again or ended) or a drop's expiry is none. Every open ask whose key holds nothing is armed again with what +// is left of its bound since its taken-at: one taken 9 minutes before, with a 10-minute bound, gets one minute. func TestMissedBoundsAtTheStart(t *testing.T) { const s = "node-build-go" key := func(a string) string { return pipeline.AskBoundKey(s, a) } - again := func(a string) string { return pipeline.AskAgainBoundKey(s, pipeline.AgainID(a)) } last := map[string]jetstream.KeyValueOp{ - key("lostaaaa000001"): jetstream.KeyValuePurge, // passed while down, never asked again: taken - key("takenaaa000002"): jetstream.KeyValuePurge, again("takenaaa000002"): jetstream.KeyValueDelete, // asked again and taken - key("askedaaa000003"): jetstream.KeyValuePurge, again("askedaaa000003"): jetstream.KeyValuePut, // asked again, bound live - key("bothaaaa000004"): jetstream.KeyValuePurge, again("bothaaaa000004"): jetstream.KeyValuePurge, // both passed - key("wordaaaa000005"): jetstream.KeyValueDelete, // a first word + key("lostaaaa000001"): jetstream.KeyValuePurge, // passed while down: taken + key("wordaaaa000005"): jetstream.KeyValueDelete, // armed again or ended + key("liveaaaa000007"): jetstream.KeyValuePut, // live pipeline.DropBoundKey(s, "dropaaaa000006"): jetstream.KeyValuePurge, } - want := []string{key("lostaaaa000001"), again("bothaaaa000004")} - slices.Sort(want) - if got := missedBounds(last); !slices.Equal(got, want) { + if got, want := missedBounds(last), []string{key("lostaaaa000001")}; !slices.Equal(got, want) { t.Errorf("missedBounds = %v, want %v", got, want) } + now := time.Date(2026, 11, 22, 2, 0, 0, 0, time.UTC) + var rearmed []string + c := &pipeline.AskClock{Bound: 10 * time.Minute, Rearm: func(k string, ttl time.Duration) error { + rearmed = append(rearmed, k+" "+ttl.String()) + return nil + }} + open := func() ([]pipeline.OpenAsk, error) { + return []pipeline.OpenAsk{{Seat: s, Ask: "liveaaaa000007", Since: now}, {Seat: s, Ask: "wordaaaa000005", Since: now.Add(-9 * time.Minute)}, + {Seat: s, Ask: "goneaaaa000008", Since: now.Add(-20 * time.Minute)}}, nil + } + rearmOpen(open, c, func() time.Time { return now }, func(string, ...any) {})(map[string]bool{key("liveaaaa000007"): true}) + if want := []string{key("wordaaaa000005") + " 1m0s", key("goneaaaa000008") + " 1s"}; !slices.Equal(rearmed, want) { + t.Errorf("armed again at the start %v, want %v", rearmed, want) + } } 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 ce4f748a2..0456d9075 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -169,12 +169,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro Findings: rv.Findings, Observer: cred.Module} // a mover waiting on its registration is registration-missing (H-A-262) hearer.AskSteps = starter.AskSteps hearer.RegistrationWaits = func() { starter.JudgeRegistrationMissing(store) } // at every end (H-A-265 (4)) - silentBound, err := askSilentBound(getenv) - if err != nil { - return err - } - logger.Printf("asks: an ask unanswered bound %s, asked once more, then its step ends could not run; an ask of a seat that says nothing before its end bound %s", askBound, silentBound) - clock := askClock(ctx, bounds, askBound, silentBound, rv.Findings, cred.Module, time.Now) + 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) starter.Asks, hearer.Asks = clock, clock starter.Supersede = func(newer seat.Pipeline) { hearer.SupersedeRunning(newer) } starter.Left = hearer.LeftInPlace // a same-source re-pin's superseded steps leave their runs (H-A-284) @@ -309,7 +305,10 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro if err := watchRunBounds(ctx, runsKV, hearer.RunExpired, logger.Printf); err != nil { return err } - if err := hearStepProgress(heard, hearer.RunProgress, logger.Printf); err != nil { + if err := hearStepProgress(heard, func(onSeat, ask string) error { + progressed(clock, onSeat, ask, logger.Printf) // the ask's bound is armed again from now (H-A-395) + return hearer.RunProgress(ask) + }, logger.Printf); err != nil { return err } if err := hearStepEnded(heard, hearer, logger.Printf); err != nil { @@ -318,13 +317,14 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro if err := hearToolchain(ctx, heard, toolchains, logger.Printf); err != nil { return err } - taken := func(ask, node string) error { - if err := hearer.Taken(ask, node); err != nil { - return err + taken := func(ask, node string, at time.Time) (*time.Time, error) { + takenAt, err := hearer.Taken(ask, node, at) + if err != nil { + return nil, err } - return hearer.RunTaken(ask, node) + return takenAt, hearer.RunTaken(ask, node) } - if err := hearAskTaken(heard, taken, clock, logger.Printf); err != nil { + if err := hearAskTaken(heard, taken, clock, time.Now, logger.Printf); err != nil { return err } if err := hearWantsProgress(heard, clock, logger.Printf); err != nil { @@ -334,9 +334,6 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // since an ask's expired bound asks it once more through them (H-A-235). verbs := writeVerbs(hearer, starter, func(branch string) (pipeline.Half, bool) { return rv.Catalog.View(branch) }, calls) verbs.Runs = pipeline.RunsRead{Runs: runsKept{ctx: ctx, kv: runsKV}, Object: runObjs.Get, Fill: starter.TestRunsFill} - if err := hearHolderStarted(heard, verbs.HolderStarted, logger.Printf); err != nil { - return err - } dropExpired := boundExpired(dropUnanswered(ctx, rv.Findings, cred.Module, time.Now), logger.Printf) askExpired := askBoundExpired(verbs.AskUnanswered, logger.Printf) expired := func(key string) { @@ -344,7 +341,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro dropExpired(key) } } - if err := watchBounds(ctx, bounds, expired, logger.Printf); err != nil { + if err := watchBounds(ctx, bounds, expired, rearmOpen(verbs.OpenAsks, clock, time.Now, logger.Printf), logger.Printf); err != nil { return err } if err := hearDecided(heard, hearer, cred.Module, logger.Printf); err != nil { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/queue_integration_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/queue_integration_test.go index e9c797a17..23dc7a86c 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/queue_integration_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/queue_integration_test.go @@ -107,17 +107,6 @@ func TestTheQueueRunsPipelinesInTurn(t *testing.T) { const router = "operator-router" answer("mesh.seat."+seatlist.HolderSeat+".call."+seatlist.HolderVerb+".*", map[string]any{"seats": []any{ map[string]any{"seat": oc.Seat, "holders": []any{map[string]any{"module": router}}}}}) - // Each mesh-builds step ask as its holder hears it, on its plain subscription. - var buildAsksMu sync.Mutex - buildAsks := map[string]int{} - if _, err := nc.Subscribe(builds.AcceptSubjectStep, func(m *nats.Msg) { - buildAsksMu.Lock() - buildAsks[m.Header.Get(nats.MsgIdHdr)]++ - buildAsksMu.Unlock() - }); err != nil { - t.Fatal(err) - } - buildAsked := func(id string) int { buildAsksMu.Lock(); defer buildAsksMu.Unlock(); return buildAsks[id] } records, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: router + "_" + oc.StateAsks, History: 1}) if err != nil { t.Fatal(err) @@ -165,7 +154,6 @@ func TestTheQueueRunsPipelinesInTurn(t *testing.T) { t.Fatal(err) } askBound := filepath.Join(dir, "ask-unanswered-bound") - silentBound := filepath.Join(dir, "ask-silent-bound") logs := &syncBuffer{} start := func() func() { runCtx, stop := context.WithCancel(ctx) @@ -179,8 +167,6 @@ func TestTheQueueRunsPipelinesInTurn(t *testing.T) { return waitingCap case envAskUnansweredBound: return askBound - case envAskSilentBound: - return silentBound } return "" }, logs) @@ -376,16 +362,12 @@ func TestTheQueueRunsPipelinesInTurn(t *testing.T) { } // At the runner's next start the waiting main.10 is placed from its own state, saying wait-ended, which clears - // pipelines-stalled; the start begins no pipeline of its own. That start's ask-unanswered-bound is one second, its - // ask-silent-bound four. + // pipelines-stalled; the start begins no pipeline of its own. That start's ask-unanswered-bound is one second. stopRunner() setMaxMsgSize(t, ctx, js, "KV_"+module+"_"+seat.StateQueue, -1) if err := os.WriteFile(askBound, []byte("1s\n"), 0o600); err != nil { t.Fatal(err) } - if err := os.WriteFile(silentBound, []byte("4s\n"), 0o600); err != nil { - t.Fatal(err) - } before := pipelineKeys(t, ctx, pipelines) stopRunner = start() outcome("main.10", seat.PipelineOutcomeRunning) @@ -395,10 +377,9 @@ func TestTheQueueRunsPipelinesInTurn(t *testing.T) { t.Fatalf("the start wrote pipelines %v, want %v", after, before) } - // H-A-243: main.10's build ask on mesh-builds, whose holder says nothing before its end, is never asked again for - // ask-unanswered-bound (a build running four of those bounds is not touched); its one bound, - // ask-silent-bound, ends its step could not run "its ask had no end", the pipeline ending failed, its turn freed and - // the finding call-unanswered naming the ask. + // Design 47 H2 §778: main.10's build ask on mesh-builds, which no holder takes, ends its step could not run "its ask was + // not taken: mesh-builds's holder takes none" at ask-unanswered-bound, never asked again, the pipeline ending failed, + // its turn freed and the finding call-unanswered naming the ask. build := stepFor(get("main.10"), "app", seat.StepResultStepBuild) if build.Ask == "" || build.Started == nil { t.Fatalf("main.10's build = %+v, want it asked", build) @@ -428,11 +409,11 @@ func TestTheQueueRunsPipelinesInTurn(t *testing.T) { t.Fatal("the start did not say the bound it took") } s := stepFor(get("main.10"), "app", seat.StepResultStepBuild) - if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != "its ask "+build.Ask+" had no end" || s.Ended == nil || s.Ended.Sub(*build.Started) < 4*time.Second { - t.Fatalf("main.10's build = %+v, want could not run, its ask with no end, at its silent bound of 4s", s) + if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != pipeline.UntakenWhy(builds.Seat) || s.Ended == nil || s.Ended.Sub(*build.Started) < time.Second { + t.Fatalf("main.10's build = %+v, want could not run, its ask not taken, at its bound of 1s", s) } - if strings.Contains(logs.String(), ": asked once more as ") || buildAsked(pipeline.AgainID(build.Ask)) != 0 { - t.Fatalf("the silent build's ask was asked again for ask-unanswered-bound (put %d times, again %d)", buildAsked(build.Ask), buildAsked(pipeline.AgainID(build.Ask))) + if strings.Contains(logs.String(), "asked once more") { + t.Fatal("the untaken build's ask was asked once more for its bound") } waitFor(t, ctx, logs, func() bool { e, err := findings.Get(ctx, "call."+build.Ask+".call-unanswered") diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go index 6ae613ffe..4576a2d51 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go @@ -7,6 +7,7 @@ import ( "fmt" "sort" "strings" + "time" "github.com/nats-io/nats.go/jetstream" @@ -142,8 +143,10 @@ func hearStepEnded(hub handlers, h *pipeline.Hearer, logf func(string, ...any)) logf("the step-ended of ask %s is not taken yet, heard again: %v", ev.ID, err) _ = m.Nak() default: - // The step's end is its holder's word on the ask, the first for mesh-builds (H-A-238 (3)). - answered(h.Asks, bs.Seat, ev.ID, logf) + // The step's end ends its ask's bound (design 47 H2 §778, H-A-395). + if err := h.Asks.Ended(bs.Seat, ev.ID); err != nil { + logf("the bound of ask %s on %s is not deleted at its step's end: %v", ev.ID, bs.Seat, err) + } _ = m.Ack() } }) @@ -154,25 +157,36 @@ func hearStepEnded(hub handlers, h *pipeline.Hearer, logf func(string, ...any)) return nil } -// hearAskTaken keeps on its step the node whose build seat holder took an ask (§602, §647; A-182), so a drop of it is asked of -// that node's holder, and begins the clock of a test run's ask (§725). -func hearAskTaken(hub handlers, taken func(ask, node string) error, c *pipeline.AskClock, logf func(string, ...any)) error { - for _, ts := range pipeline.TakenSeats { +// hearAskTaken keeps on its step the node whose seat holder took an ask (§602, §647; A-182), so a drop of it is asked of +// that node's holder, and its taken-at, from which the ask's bound counts (design 47 H2 §778): the bound is armed again +// with what is left of it. It hears the ask-taken of every seat whose holder says one (pipeline.AskTakenSeats) and +// begins the clock of a test run's ask (§725). +func hearAskTaken(hub handlers, taken func(ask, node string, at time.Time) (*time.Time, error), c *pipeline.AskClock, + now func() time.Time, logf func(string, ...any)) error { + for _, ts := range pipeline.AskTakenSeats { ts := ts err := hub.Handle(ts.Subject, func(m jetstream.Msg) { - var ev nodebuildgo.AskTaken + var ev struct { + Ask string `json:"ask"` + Node string `json:"node"` + At time.Time `json:"at"` + } if err := json.Unmarshal(m.Data(), &ev); err != nil || ev.Ask == "" { logf("%s is no ask-taken: %v", m.Subject(), err) _ = m.Term() return } - if err := taken(ev.Ask, ev.Node); err != nil { + takenAt, err := taken(ev.Ask, ev.Node, ev.At) + if err != nil { logf("the node %s that took ask %s is not kept on its step, heard again: %v", ev.Node, ev.Ask, err) _ = m.Nak() return } - // The seat's ask-taken is its holder's first word on the ask (H-A-238 (3)). - answered(c, ts.Seat, ev.Ask, logf) + if takenAt != nil { + if err := c.Taken(ts.Seat, ev.Ask, *takenAt, now()); err != nil { + logf("the bound of ask %s on %s is not armed again at its ask-taken: %v", ev.Ask, ts.Seat, err) + } + } _ = m.Ack() }) if err != nil { @@ -182,11 +196,13 @@ func hearAskTaken(hub handlers, taken func(ask, node string) error, c *pipeline. return nil } -// watchBounds hears the end of each drop's and each ask's bound, the bus's marker of the key's expiry (A-222, A-77; H-A-235, -// H-A-238 (5)): no timer in the process. An ask's bound that expired while the runner was down left its marker before the +// watchBounds hears the end of each drop's and each ask's bound, the bus's marker of the key's expiry (A-222, A-77; design 47 +// H2 §778): no timer in the process. An ask's bound that expired while the runner was down left its marker before the // watch began: the watch's first entries, each key's last, are kept to their end, and the ask bounds missedBounds names are -// taken as expired then. Then every expiry is heard as it comes. -func watchBounds(ctx context.Context, bounds jetstream.KeyValue, expired func(key string), logf func(string, ...any)) error { +// taken as expired then; every open ask whose bound key holds nothing is armed with what is left of its bound (rearm), so +// a restart never grants a fresh one. Then every expiry is heard as it comes. +func watchBounds(ctx context.Context, bounds jetstream.KeyValue, expired func(key string), rearm func(live map[string]bool), + logf func(string, ...any)) error { w, err := bounds.WatchAll(ctx) if err != nil { return err @@ -203,10 +219,21 @@ func watchBounds(ctx context.Context, bounds jetstream.KeyValue, expired func(ke return } if e == nil { - for _, key := range missedBounds(last) { + missed := missedBounds(last) + for _, key := range missed { logf("the bound %s expired while the runner was away: taken now", key) expired(key) } + if rearm != nil { + live := map[string]bool{} + for key, op := range last { + live[key] = op == jetstream.KeyValuePut + } + for _, key := range missed { + live[key] = true // ended above: not armed again + } + rearm(live) + } last = nil // the first read is over: what follows is new continue } @@ -223,21 +250,15 @@ func watchBounds(ctx context.Context, bounds jetstream.KeyValue, expired func(ke return nil } -// missedBounds are the ask bounds the first read found expired that are still to be taken (H-A-238 (5)): a second bound's -// expiry always; a first bound's only when the read holds no entry at all for its second bound's key, since a live second -// bound, its expiry or its delete (the holder's ask-taken) says the ask was asked again already. A delete marker is a first -// word, never an expiry; a drop's bound is not taken here. +// missedBounds are the ask bounds the first read found expired (§778): a key whose last entry is the bus's purge marker. A +// delete marker is a bound armed again or ended, never an expiry; a drop's bound is not taken here. func missedBounds(last map[string]jetstream.KeyValueOp) []string { var out []string for key, op := range last { if op != jetstream.KeyValuePurge { continue } - onSeat, ask, again, ok := pipeline.ParseAskBound(key) - if !ok { - continue - } - if _, asked := last[pipeline.AskAgainBoundKey(onSeat, pipeline.AgainID(ask))]; again || !asked { + if _, _, ok := pipeline.ParseAskBound(key); ok { out = append(out, key) } } @@ -245,6 +266,30 @@ func missedBounds(last map[string]jetstream.KeyValueOp) []string { return out } +// rearmOpen arms, at a start, the bound of every open ask whose key live does not name as held (§778): with what is left of +// it since its taken-at, else its asked-at; an ask whose time is spent gets a second, so it ends at once. +func rearmOpen(open func() ([]pipeline.OpenAsk, error), c *pipeline.AskClock, now func() time.Time, logf func(string, ...any)) func(map[string]bool) { + return func(live map[string]bool) { + asks, err := open() + if err != nil { + logf("the open asks are not read at the start, so their bounds are not armed again: %v", err) + return + } + for _, a := range asks { + key := pipeline.AskBoundKey(a.Seat, a.Ask) + if live[key] || c == nil || c.Rearm == nil { + continue + } + left := c.Left(a.Since, now()) + if err := c.Rearm(key, left); err != nil { + logf("the bound of the open ask %s on %s is not armed again at the start: %v", a.Ask, a.Seat, err) + continue + } + logf("the bound of the open ask %s on %s is armed again at the start with %s left", a.Ask, a.Seat, left.Round(time.Second)) + } + } +} + // keptReader reads the kept results of the packages named (§25, §416): the entries of the state kept whose package is a // name or lies beneath it, in one pass over the bucket. func keptReader(ctx context.Context, kept jetstream.KeyValue) func([]string) (map[string][]nodebuildgo.KeptResult, error) { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go index 22a724d10..c59cb63d6 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go @@ -338,7 +338,7 @@ func settingText(path string) (string, error) { var progressSubjects = []string{nodebuildgo.SubjectStepProgressAll, nodebuildnode.SubjectStepProgressAll, nodebuildimage.SubjectStepProgressAll} // hearStepProgress begins again the clock of the test run whose ask a build seat's holder says progress of (§725, A-466p). -func hearStepProgress(hub handlers, progress func(ask string) error, logf func(string, ...any)) error { +func hearStepProgress(hub handlers, progress func(onSeat, ask string) error, logf func(string, ...any)) error { for _, subject := range progressSubjects { err := hub.Handle(subject, func(m jetstream.Msg) { var ev nodebuildgo.StepProgress @@ -347,7 +347,7 @@ func hearStepProgress(hub handlers, progress func(ask string) error, logf func(s _ = m.Term() return } - if err := progress(ev.Ask); err != nil { + if err := progress(seatOfEvent(m.Subject()), ev.Ask); err != nil { logf("the clock of the test run of ask %s is not begun again, heard again: %v", ev.Ask, err) _ = m.Nak() return @@ -361,6 +361,13 @@ func hearStepProgress(hub handlers, progress func(ask string) error, logf func(s return nil } +// seatOfEvent is the seat a seat event's subject names, mesh.seat..event.... +func seatOfEvent(subject string) string { + rest, _ := strings.CutPrefix(subject, "mesh.seat.") + seatName, _, _ := strings.Cut(rest, ".") + return seatName +} + // watchRunBounds hears the expiry of each test run's bound key, the bus's marker of the key's end (§725): no timer in the // process. func watchRunBounds(ctx context.Context, kv jetstream.KeyValue, expired func(key string) error, logf func(string, ...any)) error { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns_integration_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns_integration_test.go index 1ad69414f..d0c242096 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns_integration_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns_integration_test.go @@ -16,7 +16,6 @@ import ( "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" - nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" @@ -345,17 +344,6 @@ func TestTestRunsAreAskedOncePerLevelAndMoveEveryReader(t *testing.T) { const p3 = "main.1" takeIn("app", "c1", "src2:aa", 3, 1) placed(p3) // started by the take-in alone: the runner starts no pipeline by sweep (design 47 H §747) - // H-A-243 (1): the node-engine's running of the builder puts its open build ask again, under its own id: an ask put while - // it was down is lost; a running of a module holding no silent seat puts nothing again. - firstBuild := step(read(p3), "", seat.StepResultStepBuild).Ask - publish(nodeengine.SubjectRunning("n1", "mesh-build-go"), nodeengine.Running{Module: "mesh-build-go", Node: "n1", At: time.Now().UTC()}) - publish(nodeengine.SubjectRunning("n1", "mesh-delivery-builder"), nodeengine.Running{Module: "mesh-delivery-builder", Node: "n1", At: time.Now().UTC()}) - waitFor(t, ctx, logs, func() bool { - buildAsksMu.Lock() - n := buildAsks[firstBuild] - buildAsksMu.Unlock() - return n == 2 && strings.Count(logs.String(), "the ask "+firstBuild+" of the build step of "+p3+" is put again on mesh-builds") == 1 - }) passBuild(p3, "app", "c1", "src2:aa") unitRun, unit := runOfStep(p3, "app:program", "unit") integRun, integ := runOfStep(p3, "app:program", "integration") @@ -385,7 +373,7 @@ func TestTestRunsAreAskedOncePerLevelAndMoveEveryReader(t *testing.T) { t.Fatal(err) } else { for _, k := range ks { - _, ask, _, isAsk := pipeline.ParseAskBound(k) + _, ask, isAsk := pipeline.ParseAskBound(k) if (!isAsk && !strings.HasPrefix(k, pipeline.DropBoundPrefix)) || ask == unit.Ask || ask == integ.Ask { t.Errorf("the state bounds holds %s after the asks were put; want no bound of a test run's ask", k) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/asks.go b/modules/mesh-delivery-pipeline/internal/pipeline/asks.go index 06a1ccb1d..60ad7e29c 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/asks.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/asks.go @@ -1,8 +1,6 @@ package pipeline import ( - "crypto/sha256" - "encoding/hex" "errors" "fmt" "strings" @@ -18,89 +16,61 @@ import ( // from an ask until its holder's first word. const DefaultAskUnansweredBound = 10 * time.Minute -// DefaultAskSilentBound is the setting ask-silent-bound's default (H-A-243 (2)): forty-five minutes from an ask on a seat whose -// holder says no word before the step's end (mesh-builds, mesh-wants) until that end. -const DefaultAskSilentBound = 45 * time.Minute +// The key of the state bounds that times an ask the runner put (design 47 H2 §778, H-A-395): `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." -// Silent says whether the holder of a seat the runner asks says nothing of an ask before its step's end: every seat but the -// build seats, which say ask-taken (H-A-241, H-A-243). -func Silent(onSeat string) bool { return !BoundSeat(onSeat) } +// AskBoundKey is the key of the bound of the ask on onSeat. +func AskBoundKey(onSeat, ask string) string { return AskBoundPrefix + onSeat + "." + ask } -// AgainID is the id an ask is put again under (H-A-243 (3)): the first id's time part and six hex digits of the sha256 of the -// first id and ".2", the form the holders accept, so no duplicate window is relied on. -func AgainID(id string) string { - if len(id) <= 6 { - return id - } - sum := sha256.Sum256([]byte(id + ".2")) - return id[:len(id)-6] + hex.EncodeToString(sum[:3]) -} - -// The keys of the state bounds that time an ask the runner put (H-A-238 (5)): `ask..` armed before the ask is put, -// `ask2..` armed when it is asked once more, each with the setting as its own time to live. -const ( - AskBoundPrefix = "ask." - AskAgainBoundPrefix = "ask2." -) - -// AskBoundKey is the key of the first bound of the ask on onSeat; AskAgainBoundKey that of its second. -func AskBoundKey(onSeat, ask string) string { return AskBoundPrefix + onSeat + "." + ask } -func AskAgainBoundKey(onSeat, ask string) string { return AskAgainBoundPrefix + onSeat + "." + ask } - -// ParseAskBound reads a key of the state bounds as an ask's bound: the seat, the ask and whether it is the second bound. -func ParseAskBound(key string) (onSeat, ask string, again, ok bool) { - rest, found := strings.CutPrefix(key, AskAgainBoundPrefix) - again = found +// 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 { - if rest, found = strings.CutPrefix(key, AskBoundPrefix); !found { - return "", "", false, false - } + return "", "", false } onSeat, ask, found = strings.Cut(rest, ".") - return onSeat, ask, again, found && onSeat != "" && ask != "" && !strings.Contains(ask, ".") + return onSeat, ask, found && onSeat != "" && ask != "" && !strings.Contains(ask, ".") } -// UnansweredWhy is the why of a step whose ask was not answered within its two bounds (H-A-235). -func UnansweredWhy(ask string) string { return "its ask " + ask + " was not answered" } +// 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" +} -// SilentWhy is the why of a step on a silent seat whose ask had no end within ask-silent-bound (H-A-243 (2)). -func SilentWhy(ask string) string { return "its ask " + ask + " had no end" } +// 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 (H-A-235, H-A-238 (4)): -// Arm writes a key of the state bounds with Bound as its own time to live, Unbound deletes one; 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)). +// 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, for a seat that says ask-taken; Silent is ask-silent-bound, for a silent seat. + // Bound is ask-unanswered-bound. Bound time.Duration - Silent time.Duration Arm func(key string, ttl time.Duration) error + Rearm func(key string, ttl time.Duration) error Unbound func(key string) error Raise func(onSeat, ask string, bound time.Duration) error Clear func(onSeat, ask string) error } -// arm arms the first bound of a, before it is put (H-A-238 (5)); a test run's ask, and a nil clock, arm none. +// 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.boundOf(a.Seat)); err != 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 } -// boundOf is the bound of an ask on onSeat: ask-silent-bound for a silent seat (H-A-243 (2)), else ask-unanswered-bound. -func (c *AskClock) boundOf(onSeat string) time.Duration { - if !Silent(onSeat) { - return c.bound() - } - if c.Silent > 0 { - return c.Silent - } - return DefaultAskSilentBound -} - func (c *AskClock) bound() time.Duration { if c.Bound > 0 { return c.Bound @@ -108,21 +78,41 @@ func (c *AskClock) bound() time.Duration { return DefaultAskUnansweredBound } -// Answered deletes the bounds of the ask heard on onSeat, its holder's first word: an ask-taken, a step progress or, for a -// silent seat, the step's end (H-A-238 (3), H-A-241). Heard under a first id, the second bound of the ask put again under -// AgainID goes too. -func (c *AskClock) Answered(onSeat, ask string) error { +// 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 errors.Join(c.Unbound(AskBoundKey(onSeat, ask)), c.Unbound(AskAgainBoundKey(onSeat, ask)), - c.Unbound(AskAgainBoundKey(onSeat, AgainID(ask)))) + return c.Unbound(AskBoundKey(onSeat, ask)) } -// answeredLate clears the finding of the ask of step s, which ended unanswered or with no end, its holder heard after all +// 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 != SilentWhy(s.Ask)) { + 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 { @@ -130,13 +120,12 @@ func (c *AskClock) answeredLate(s seat.StepResult, logf func(string, ...any)) { } } -// AskUnanswered takes the expiry of a bound of the ask on onSeat (H-A-235, H-A-243). On a silent seat its one bound, -// ask-silent-bound, ends the step could not run "its ask had no end", never asking again (that would double a running -// build). On a seat that says ask-taken the first bound puts the ask once more under AgainID, said in the journal, its -// second bound armed under that id; the second ends the step could not run "its ask was not answered". An ended step -// raises the finding call-unanswered, 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, again bool) error { +// AskUnanswered takes the expiry of the bound of the ask on onSeat (§778, H-A-395): the step ends could not run, "its ask +// was not answered" when its holder took it and then said nothing for the bound, "its ask was not taken: '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 @@ -146,112 +135,65 @@ func (v Verbs) AskUnanswered(onSeat, ask string, again bool) error { return err } s := p.Steps[i] - if Silent(onSeat) || again { - why, within := UnansweredWhy(s.Ask), 2*c.bound() - if Silent(onSeat) { - why, within = SilentWhy(s.Ask), c.boundOf(onSeat) - } - h.logf("the ask %s of the %s step of %s on %s had no word from its holder within %s: the step ends could not run (H-A-235, H-A-243)", - s.Ask, s.Step, key, onSeat, within) - if err := h.endUnanswered(key, s.Ask, why); err != nil { - return err - } - if c.Raise != nil { - return c.Raise(onSeat, s.Ask, within) - } - return nil + 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" } - a, err := v.askAgain(p, s, AgainID(s.Ask), h.now()) - if err != nil { - h.logf("the ask %s of the %s step of %s on %s had no word from its holder within %s and cannot be put again: %v", s.Ask, s.Step, key, onSeat, c.bound(), err) - } else { - h.logf("the ask %s of the %s step of %s on %s had no word from its holder within %s: asked once more as %s (H-A-235, H-A-243)", s.Ask, s.Step, key, onSeat, c.bound(), a.ID) + h.logf("the ask %s of the %s step of %s on %s %s within %s: the step ends could not run and its ask is dropped (§778)", + s.Ask, s.Step, key, onSeat, said, c.bound()) + if err := h.endUnanswered(key, s.Ask, why); err != nil { + return err } - if c.Arm != nil { - if aerr := c.Arm(AskAgainBoundKey(onSeat, AgainID(s.Ask)), c.bound()); aerr != nil { - return fmt.Errorf("the second bound of ask %s is not armed, so it is not asked again: %w", s.Ask, aerr) + if h.DropRun != nil { + if err := h.DropRun(onSeat, s.Node, s.Ask, why); err != nil { + h.logf("the ask %s on %s, ended by its bound, is not dropped: %v", s.Ask, onSeat, err) } } - if err != nil { - return nil // its second bound ends the step + if c.Raise != nil { + return c.Raise(onSeat, s.Ask, c.bound()) } - v.Starter.carry(key, []Asked{*a}) - return askAll(v.Starter.Steps, key, []Asked{*a}, func(commit string) bool { return v.Starter.recovered(key, commit) }, nil, nil) + return nil } -// HolderStarted puts again every open ask of each silent seat module holds (H-A-243 (1)): its holder takes its asks on a plain -// subscription, so an ask put while it was down is lost; heard on the node-engine's running of module. Each ask is put under -// its own id, a rerun's marks kept, said in the journal; its bound stands as armed. -func (v Verbs) HolderStarted(module string) (int, error) { - st := v.Starter - if st == nil || st.Holders == nil { - return 0, nil - } - var silent []string - for onSeat := range acceptStep { - if !Silent(onSeat) { - continue - } - holders, read := st.Holders(onSeat) - if !read { - return 0, ErrHoldersNotRead - } - if containsString(holders, module) { - silent = append(silent, onSeat) - } - } - if len(silent) == 0 { - return 0, nil - } +// OpenAsk is an ask a running step waits on, and when its holder last spoke of it: its taken-at, else its asked-at. +type OpenAsk struct { + Seat, Ask string + Since time.Time +} + +// OpenAsks are the asks of every running pipeline's open steps but a test run's (§778): at a start each whose bound key +// is gone is armed again with what is left of its bound, so a restart never grants a fresh one. +func (v Verbs) OpenAsks() ([]OpenAsk, error) { pkeys, err := allKeys(v.Hearer.Store) if err != nil { - return 0, fmt.Errorf("the pipelines are not listed: %w", err) + return nil, fmt.Errorf("the pipelines are not listed: %w", err) } - now, asked := v.Hearer.now(), 0 - var first error + 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{} - var due []Asked for _, s := range p.Steps { - if !open(s) || s.Ask == "" || seen[s.Ask] || !containsString(silent, s.Seat) { + if !open(s) || s.Ask == "" || sharedRun(s) != "" || seen[s.Ask] { continue } seen[s.Ask] = true - a, err := v.askAgain(p, s, s.Ask, now) - if err != nil { - v.Hearer.logf("the ask %s of the %s step of %s on %s is not put again as %s starts: %v", s.Ask, s.Step, key, s.Seat, module, err) + since := s.TakenAt + if since == nil { + since = s.AskedAt + } + if since == nil { + since = s.Started + } + if since == nil { continue } - v.Hearer.logf("the ask %s of the %s step of %s is put again on %s: its holder %s started, and takes no ask put while it was down (H-A-243)", - s.Ask, s.Step, key, s.Seat, module) - due = append(due, *a) - } - if len(due) == 0 { - continue - } - st.carry(key, due) - if err := askAll(st.Steps, key, due, func(commit string) bool { return st.recovered(key, commit) }, nil, nil); err != nil && first == nil { - first = err - } - asked += len(due) - } - return asked, first -} - -// ErrHoldersNotRead is HolderStarted's answer before the seat-holders view's first view is read: the running is heard again. -var ErrHoldersNotRead = errors.New("the seat-holders view is not read yet") - -func containsString(list []string, s string) bool { - for _, x := range list { - if x == s { - return true + out = append(out, OpenAsk{Seat: s.Seat, Ask: s.Ask, Since: *since}) } } - return false + 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 @@ -273,18 +215,6 @@ func (v Verbs) runningAsk(onSeat, ask string) (string, seat.Pipeline, int, error return "", seat.Pipeline{}, -1, nil } -// askAgain is the ask of running step s as it was put, under id: a rerun's marks kept. -func (v Verbs) askAgain(p seat.Pipeline, s seat.StepResult, id string, now time.Time) (*Asked, error) { - a, err := v.placedAsk(p, s, id, now) - if err != nil { - return nil, err - } - if s.Rerun { - markRerun(a, s.By) - } - return a, nil -} - // markRerun marks a's body a rerun by caller. func markRerun(a *Asked, caller string) { switch b := a.Ask.(type) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go index ffec1d9a8..dab68c7b8 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go @@ -7,169 +7,129 @@ import ( "testing" "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" - sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" ) -// H-A-235, H-A-238: every ask but a test run's is bounded from its put until its holder's first word. A bound that passes asks -// once more under the ask's own id, its second bound armed; past the second the step ends could not run "its ask was not -// answered", the finding raised, and the pipeline ends; a first word deletes both bounds, and a late one clears the finding. -func TestAnAskNobodyAnswersIsAskedOnceMoreThenEndsItsStep(t *testing.T) { - v, store, said, asks, _, _ := verbsFixture() - var armed, unbound, raised, cleared []string - logs := []string{} - v.Hearer.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) } - c := &AskClock{Bound: 10 * time.Minute, - Arm: func(key string, ttl time.Duration) error { armed = append(armed, key+" "+ttl.String()); return nil }, - Unbound: func(key string) error { unbound = append(unbound, key); return nil }, - Raise: func(s, ask string, _ time.Duration) error { raised = append(raised, s+" "+ask); return nil }, - Clear: func(s, ask string) error { cleared = append(cleared, s+" "+ask); return nil }} - v.Starter.Asks, v.Hearer.Asks = c, c - - // A test run's ask keeps §725's bounds; a nil clock arms none. - if err := c.arm(Asked{Seat: nodebuildgo.Seat, ID: "r1", Run: "run"}); err != nil || len(armed) != 0 { - t.Fatalf("a test run's ask armed %v (%v)", armed, err) +// Design 47 H2 §778, H-A-395: every ask but a test run's is bounded from its put; its holder's ask-taken arms the bound again +// with what is left of it since taken-at, each step-progress from now, and the step's end deletes it. The bound passing ends +// the step could not run ("not answered" when taken, "not taken" naming the seat's holder when not), drops its ask through +// the seat's drop, raises the finding and ends the pipeline; a late ask-taken clears the finding. At a start an ask taken 9 +// minutes before, with a 10-minute bound, has one minute left. +func TestAnAsksBoundCountsFromThePullAndEndsItsStep(t *testing.T) { + for _, taken := range []bool{true, false} { + t.Run(fmt.Sprintf("taken %v", taken), func(t *testing.T) { + v, store, said, asks, _, _ := verbsFixture() + var armed, rearmed, unbound, raised, cleared, dropped []string + logs := []string{} + v.Hearer.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) } + now := time.Date(2026, 11, 22, 2, 0, 0, 0, time.UTC) + v.Hearer.Now = func() time.Time { return now } + c := &AskClock{Bound: 10 * time.Minute, + Arm: func(key string, ttl time.Duration) error { armed = append(armed, key+" "+ttl.String()); return nil }, + Rearm: func(key string, ttl time.Duration) error { rearmed = append(rearmed, key+" "+ttl.String()); return nil }, + Unbound: func(key string) error { unbound = append(unbound, key); return nil }, + Raise: func(s, ask string, _ time.Duration) error { raised = append(raised, s+" "+ask); return nil }, + Clear: func(s, ask string) error { cleared = append(cleared, s+" "+ask); return nil }} + v.Starter.Asks, v.Hearer.Asks = c, c + v.Hearer.DropRun = func(onSeat, node, ask, why string) error { + dropped = append(dropped, onSeat+" "+node+" "+ask+" "+why) + return nil + } + // A test run's ask keeps §725's bounds; a nil clock arms none. + if err := c.arm(Asked{Seat: nodebuildgo.Seat, ID: "r1", Run: "run"}); err != nil || len(armed) != 0 { + t.Fatalf("a test run's ask armed %v (%v)", armed, err) + } + if err := (*AskClock)(nil).arm(Asked{Seat: nodebuildgo.Seat, ID: "x"}); err != nil { + t.Fatal(err) + } + // Every step but app:program's lint has ended; that lint failed, and its rerun's ask is armed before it is put. + p, rev, _ := store.Get(pkey) + for i := range p.Steps { + if open(p.Steps[i]) { + p.Steps[i].Outcome = seat.StepResultOutcomePassed + } + } + p.Steps[stepIndex(p, "lint", "app:program")].Outcome = seat.StepResultOutcomeFailed + store.Update(pkey, p, rev) + r, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "retry"}) + if err != nil { + t.Fatal(err) + } + ask, key := r.Result.Ask, AskBoundKey(nodebuildgo.Seat, r.Result.Ask) + if want := []string{key + " 10m0s"}; !slices.Equal(armed, want) || len(asks.ids) != 1 { + t.Fatalf("armed %v asking %v, want %v before one ask", armed, asks.ids, want) + } + if got, _, _ := store.Get(pkey); got.Steps[stepIndex(got, "lint", "app:program")].AskedAt == nil { + t.Fatal("the step keeps no asked-at") + } + if taken { + // Taken 9 minutes ago: one minute is left of its bound, at its ask-taken as at a start. + at, err := v.Hearer.Taken(ask, "n1", now.Add(-9*time.Minute)) + if err != nil || at == nil || !at.Equal(now.Add(-9*time.Minute)) { + t.Fatalf("taken-at %v (%v)", at, err) + } + if err := c.Taken(nodebuildgo.Seat, ask, *at, now); err != nil || !slices.Equal(rearmed, []string{key + " 1m0s"}) { + t.Fatalf("re-armed %v (%v), want one minute left", rearmed, err) + } + open, err := v.OpenAsks() + if err != nil || len(open) != 1 || open[0].Ask != ask || !open[0].Since.Equal(*at) || c.Left(open[0].Since, now) != time.Minute { + t.Fatalf("open asks %+v (%v), want the ask since its taken-at", open, err) + } + if err := c.Progress(nodebuildgo.Seat, ask); err != nil || rearmed[1] != key+" 10m0s" { + t.Fatalf("a step-progress re-armed %v (%v), want the whole bound", rearmed, err) + } + } + // The bound passes: the step ends could not run and is dropped, the finding raised, the pipeline ended. + if err := v.AskUnanswered(nodebuildgo.Seat, ask); err != nil { + t.Fatal(err) + } + got, _, _ := store.Get(pkey) + s := got.Steps[stepIndex(got, "lint", "app:program")] + why, node := UntakenWhy(nodebuildgo.Seat), "" + if taken { + why, node = UnansweredWhy(ask), "n1" + } + if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != why || s.Ended == nil { + t.Errorf("the step = %+v, want could not run: %s", s, why) + } + if !taken && !strings.Contains(s.Why, "node-build-go's holder takes none") { + t.Errorf("an untaken ask's why %q names no holder that does not take", s.Why) + } + if !slices.Equal(dropped, []string{nodebuildgo.Seat + " " + node + " " + ask + " " + why}) { + t.Errorf("dropped %v, want the ask dropped through its seat", dropped) + } + // The end is one journal line, as every said end (H-A-320 (1), H-A-333). + if n := countLines(logs, pkey+" app lint ended could not run ("); n != 1 { + t.Errorf("the bound's end journaled %d times in %q, want once", n, logs) + } + if got.Outcome != seat.PipelineOutcomeFailed || len(asks.ids) != 1 { + t.Errorf("the pipeline is %s (%s), asked %d, want failed and nothing asked again", got.Outcome, got.Why, len(asks.ids)) + } + if !slices.Equal(raised, []string{nodebuildgo.Seat + " " + ask}) || !slices.Contains(*said, "pipeline-ended.main.5") { + t.Errorf("raised %v, said %v", raised, *said) + } + // A bound whose step ended asks nothing; the step's end deletes the bound; a late ask-taken clears the finding. + if err := v.AskUnanswered(nodebuildgo.Seat, ask); err != nil || len(asks.ids) != 1 || len(dropped) != 1 { + t.Errorf("a bound of an ended step asked %v, dropped %v (%v)", asks.ids, dropped, err) + } + if err := c.Ended(nodebuildgo.Seat, ask); err != nil || !slices.Equal(unbound, []string{key}) { + t.Errorf("the step's end deleted %v (%v)", unbound, err) + } + if _, err := v.Hearer.Taken(ask, "n2", now); err != nil || !slices.Equal(cleared, []string{nodebuildgo.Seat + " " + ask}) { + t.Errorf("a late ask-taken cleared %v (%v)", cleared, err) + } + }) } - if err := (*AskClock)(nil).arm(Asked{Seat: nodebuildgo.Seat, ID: "x"}); err != nil { - t.Fatal(err) + if s, a, ok := ParseAskBound(AskBoundKey("mesh-wants", "a1")); !ok || s != "mesh-wants" || a != "a1" { + t.Errorf("ParseAskBound = %s %s %v", s, a, ok) } - - // Every step but app:program's lint has ended; that lint failed, and its rerun's ask is armed before it is put. - p, rev, _ := store.Get(pkey) - for i := range p.Steps { - if open(p.Steps[i]) { - p.Steps[i].Outcome = seat.StepResultOutcomePassed - } - } - p.Steps[stepIndex(p, "lint", "app:program")].Outcome = seat.StepResultOutcomeFailed - store.Update(pkey, p, rev) - r, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "retry"}) - if err != nil { - t.Fatal(err) - } - ask := r.Result.Ask - if want := []string{AskBoundKey(nodebuildgo.Seat, ask) + " 10m0s"}; !slices.Equal(armed, want) || len(asks.ids) != 1 { - t.Fatalf("armed %v asking %v, want %v before one ask", armed, asks.ids, want) - } - // The first bound passes: asked once more under the id derived from the first, in the form the holders accept, a rerun - // still, the second bound armed under it and said (H-A-243 (3)). - again := AgainID(ask) - if err := sdkstep.CheckAskID(again); err != nil || again == ask || again[:len(again)-6] != ask[:len(ask)-6] { - t.Fatalf("AgainID(%s) = %s (%v), want the first's time part and other hex digits in the ask id form", ask, again, err) - } - if err := v.AskUnanswered(nodebuildgo.Seat, ask, false); err != nil { - t.Fatal(err) - } - if len(asks.ids) != 2 || asks.ids[1] != again || armed[1] != AskAgainBoundKey(nodebuildgo.Seat, again)+" 10m0s" { - t.Fatalf("asked %v, armed %v, want %s once more and its second bound", asks.ids, armed, again) - } - if b, ok := asks.asks[1].(nodebuildgo.StepAsk); !ok || b.ID != again || b.Step == nil || !b.Step.Rerun || b.Step.By != "operator" { - t.Errorf("the ask put again = %+v", asks.asks[1]) - } - if !slices.ContainsFunc(logs, func(l string) bool { return strings.Contains(l, "asked once more") }) { - t.Errorf("no journal line names the ask asked again: %v", logs) - } - if got, _, _ := store.Get(pkey); got.Steps[stepIndex(got, "lint", "app:program")].Outcome != seat.StepResultOutcomeRunning { - t.Fatal("the step ended at the first bound") - } - // The second bound passes: the step ends could not run, the finding is raised, the pipeline ends failed. - if err := v.AskUnanswered(nodebuildgo.Seat, again, true); err != nil { - t.Fatal(err) - } - got, _, _ := store.Get(pkey) - s := got.Steps[stepIndex(got, "lint", "app:program")] - if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != "its ask "+ask+" was not answered" || s.Ended == nil { - t.Errorf("the step = %+v", s) - } - // The unanswered-bound end is one journal line, as every said end (H-A-320 (1), H-A-333). - if n := countLines(logs, pkey+" app lint ended could not run (no node, "); n != 1 { - t.Errorf("the unanswered end journaled %d times in %q, want once", n, logs) - } - if got.Outcome != seat.PipelineOutcomeFailed || len(asks.ids) != 2 { - t.Errorf("the pipeline is %s (%s), asked %d, want failed and nothing more asked", got.Outcome, got.Why, len(asks.ids)) - } - if !slices.Equal(raised, []string{nodebuildgo.Seat + " " + ask}) || !slices.Contains(*said, "pipeline-ended.main.5") { - t.Errorf("raised %v, said %v", raised, *said) - } - // A bound whose step ended asks nothing; a first word deletes both bounds; a late end clears the finding. - if err := v.AskUnanswered(nodebuildgo.Seat, ask, false); err != nil || len(asks.ids) != 2 { - t.Errorf("a bound of an ended step asked %v (%v)", asks.ids, err) - } - if err := c.Answered(nodebuildgo.Seat, ask); err != nil || !slices.Equal(unbound, []string{AskBoundKey(nodebuildgo.Seat, ask), AskAgainBoundKey(nodebuildgo.Seat, ask), AskAgainBoundKey(nodebuildgo.Seat, again)}) { - t.Errorf("a first word deleted %v (%v)", unbound, err) - } - if err := v.Hearer.Taken(again, "n1"); err != nil || !slices.Equal(cleared, []string{nodebuildgo.Seat + " " + ask}) { - t.Errorf("a late ask-taken cleared %v (%v)", cleared, err) - } - for _, k := range []string{AskBoundKey("mesh-wants", "a1"), AskAgainBoundKey("mesh-wants", "a1")} { - if s, a, again, ok := ParseAskBound(k); !ok || s != "mesh-wants" || a != "a1" || again != strings.HasPrefix(k, AskAgainBoundPrefix) { - t.Errorf("ParseAskBound(%s) = %s %s %v %v", k, s, a, again, ok) - } - } - if _, _, _, ok := ParseAskBound(DropBoundKey("node-build-go", "a1")); ok { + if _, _, ok := ParseAskBound(DropBoundKey("node-build-go", "a1")); ok { t.Error("a drop's bound read as an ask's") } } -// H-A-243: an ask on a seat whose holder says nothing before the step's end (mesh-builds) is never asked again for a bound: -// its one bound, ask-silent-bound, ends the step could not run "its ask had no end", the finding raised. The node-engine's -// running of the seat's holder puts every open ask of the seat again under its own id, a module holding no such seat none. -func TestASilentSeatsAskIsPutAgainWhenItsHolderStartsAndEndsAtItsSilentBound(t *testing.T) { - v, store, _, asks, _, _ := verbsFixture() - var armed, raised []string - c := &AskClock{Bound: time.Minute, Silent: 45 * time.Minute, - Arm: func(key string, ttl time.Duration) error { armed = append(armed, key+" "+ttl.String()); return nil }, - Raise: func(s, ask string, within time.Duration) error { - raised = append(raised, s+" "+ask+" "+within.String()) - return nil - }} - v.Starter.Asks, v.Hearer.Asks = c, c - v.Starter.Holders = func(s string) ([]string, bool) { - return map[string][]string{builds.Seat: {"mesh-delivery-builder"}}[s], true - } - p, rev, _ := store.Get(pkey) - // Every step has ended but app's build, asked of mesh-builds. - b := stepIndex(p, seat.StepResultStepBuild, "") - for i := range p.Steps { - if open(p.Steps[i]) && i != b { - p.Steps[i].Outcome = seat.StepResultOutcomePassed - } - } - if p.Steps[b].Seat != builds.Seat || !open(p.Steps[b]) { - t.Fatalf("the build step = %+v, want running on mesh-builds", p.Steps[b]) - } - store.Update(pkey, p, rev) - build := p.Steps[b].Ask - if err := c.arm(Asked{Seat: builds.Seat, ID: build}); err != nil || !slices.Equal(armed, []string{AskBoundKey(builds.Seat, build) + " 45m0s"}) { - t.Fatalf("armed %v (%v), want the silent bound", armed, err) - } - // A module holding no silent seat puts nothing again; the builder's running puts the build again under its own id. - if n, err := v.HolderStarted("mesh-build-go"); err != nil || n != 0 || len(asks.ids) != 0 { - t.Fatalf("another holder's start put %d (%v)", n, err) - } - if n, err := v.HolderStarted("mesh-delivery-builder"); err != nil || n != 1 || len(asks.ids) != 1 || asks.ids[0] != build { - t.Fatalf("the builder's start put %d asks %v (%v), want %s again", n, asks.ids, err, build) - } - if _, ok := asks.asks[0].(builds.StepRun); !ok || asks.at[0] != builds.AcceptSubjectStep { - t.Errorf("the ask put again = %+v on %s", asks.asks[0], asks.at[0]) - } - // The silent bound passes: no ask again, the step ends could not run, the finding is raised, the pipeline ends. - if err := v.AskUnanswered(builds.Seat, build, false); err != nil { - t.Fatal(err) - } - got, _, _ := store.Get(pkey) - s := got.Steps[b] - if len(asks.ids) != 1 || s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != "its ask "+build+" had no end" || got.Outcome != seat.PipelineOutcomeFailed { - t.Errorf("asked %v; the build %+v; the pipeline %s", asks.ids, s, got.Outcome) - } - if !slices.Equal(raised, []string{builds.Seat + " " + build + " 45m0s"}) { - t.Errorf("raised %v", raised) - } -} - // H-A-252: the pipeline's one validate ask names no mover (H-A-51), and its holder's end names the mover whose body it took: // it is matched by its ask alone and ends the validate step of every mover under it, whichever mover it names. func TestAValidateEndNamingOneMoverEndsEveryMoversValidate(t *testing.T) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/bounds.go b/modules/mesh-delivery-pipeline/internal/pipeline/bounds.go index c95cba8bc..900fbf248 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/bounds.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/bounds.go @@ -5,6 +5,8 @@ import ( "strings" "time" + builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" + 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" nodebuildimage "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildimage" nodebuildnode "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildnode" @@ -22,13 +24,19 @@ type TakenSeat struct { Subject string } -// TakenSeats are the three build seats, from their contracts: the seat mesh-builds and the seat mesh-wants say no ask-taken. +// TakenSeats are the three build seats, from their contracts. var TakenSeats = []TakenSeat{ {Seat: nodebuildgo.Seat, Subject: nodebuildgo.SubjectAskTakenAll}, {Seat: nodebuildnode.Seat, Subject: nodebuildnode.SubjectAskTakenAll}, {Seat: nodebuildimage.Seat, Subject: nodebuildimage.SubjectAskTakenAll}, } +// AskTakenSeats are every seat the runner asks a step of whose holder says ask-taken at its pull: the three build seats, +// mesh-builds and mesh-wants (design 47 H2 §777), whose ask-taken the runner consumes. +var AskTakenSeats = append(append([]TakenSeat(nil), TakenSeats...), + TakenSeat{Seat: builds.Seat, Subject: builds.SubjectAskTakenAll}, + TakenSeat{Seat: wants.Seat, Subject: wants.SubjectAskTakenAll}) + // BoundSeat says whether the holder of a seat says ask-taken: a build seat, whose asks are dropped and whose test steps share // a run (§724). func BoundSeat(name string) bool { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go index 5579a5b0d..bc55b7643 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/endopen_test.go @@ -56,7 +56,7 @@ func TestAPipelineWithNothingOpenEndsAtOnce(t *testing.T) { {"its last open step ends could not run on the silent bound (H-A-243)", []seat.StepResult{step("a", seat.StepResultOutcomeRunning, "mwql5pio3390aa"), step("b", seat.StepResultOutcomePassed, "x2"), step("c", seat.StepResultOutcomePassed, "x3")}, func(t *testing.T, h *Hearer) { - if err := h.endUnanswered("main.117", "mwql5pio3390aa", SilentWhy("mwql5pio3390aa")); err != nil { + if err := h.endUnanswered("main.117", "mwql5pio3390aa", UntakenWhy("mesh-wants")); err != nil { t.Fatal(err) } }, seat.PipelineOutcomeFailed}, diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/place.go b/modules/mesh-delivery-pipeline/internal/pipeline/place.go index 51d97d6ac..f34cb9d43 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/place.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/place.go @@ -156,7 +156,7 @@ func place(p seat.Pipeline, m seat.Mover, reg catalog.Registration, entries map[ steps = append(steps, s) continue } - s.Seat, s.Outcome, s.Started = bs, seat.StepResultOutcomeRunning, &now + s.Seat, s.Outcome, s.Started, s.AskedAt = bs, seat.StepResultOutcomeRunning, &now, &now // asked-at (design 47 H2 §778) s.Ask = keptAskID(startOf(p, now), p.Pipeline, m.Module, a.Ref, step, kept) body := stepAsk(p, m, entry, a, language, step, s.Ask, asker, now) ask := Asked{Seat: bs, ID: s.Ask, Ask: body, Toolchain: a.Language, Commit: m.Commit} @@ -215,7 +215,7 @@ func place(p seat.Pipeline, m seat.Mover, reg catalog.Registration, entries map[ s.Ask, s.Why = "", "asked once validate passed" break } - s.Outcome, s.Started = seat.StepResultOutcomeRunning, &now + s.Outcome, s.Started, s.AskedAt = seat.StepResultOutcomeRunning, &now, &now asks = append(asks, wantsAsk(p, m, s.Ask, wants.StepRunStepComposition)) } // Review and approval wait. @@ -348,7 +348,7 @@ func (s *Starter) AskPinHeld(store Updater, key, module string, pins map[string] continue } a := entry.Artifacts[j] - st.Outcome, st.Why, st.Started = seat.StepResultOutcomeRunning, "", &now + st.Outcome, st.Why, st.Started, st.AskedAt = seat.StepResultOutcomeRunning, "", &now, &now st.Ask = keptAskID(startOf(p, now), p.Pipeline, module, a.Ref, st.Step, kept) ask := Asked{Seat: st.Seat, ID: st.Ask, Ask: stepAsk(p, m, entry, a, language, st.Step, st.Ask, s.Name, now), Toolchain: a.Language, Commit: m.Commit} if a.Package != nil { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go index a9736389e..1295645f4 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go @@ -740,7 +740,7 @@ func endOutcome(o string) bool { func validateStepOfAsk(p seat.Pipeline, id, module string) int { first, anyOpen := -1, -1 for i, s := range p.Steps { - if s.Step != seat.StepResultStepValidate || s.Ask == "" || (s.Ask != id && AgainID(s.Ask) != id) { + if s.Step != seat.StepResultStepValidate || s.Ask == "" || s.Ask != id { continue } if first < 0 { @@ -763,8 +763,7 @@ func validateStepOfAsk(p seat.Pipeline, id, module string) int { func stepOfAsk(p seat.Pipeline, id string) int { for i, s := range p.Steps { - // An ask put again is put under AgainID of the step's ask (H-A-243 (3)): its holder's answer under either is the step's. - if id != "" && s.Ask != "" && (s.Ask == id || AgainID(s.Ask) == id) { + if id != "" && s.Ask == id { return i } } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go b/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go index 7bb8b8b9c..bbb3ccc0c 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/supersede.go @@ -18,14 +18,16 @@ import ( type Drop func(buildSeat, node, ask, why string) error // Taken notes on the running step asked under ask the node whose holder said ask-taken for it (A-182), so a drop of it is -// asked of that node's holder. A step no pipeline keeps running under ask takes nothing. -func (h *Hearer) Taken(ask, node string) error { +// asked of that node's holder, and the ask-taken's time as the step's taken-at (design 47 H2 §778), kept from the first +// ask-taken heard. It answers the step's taken-at, which the ask's bound counts from; a step no pipeline keeps running +// under ask takes nothing and answers none. +func (h *Hearer) Taken(ask, node string, at time.Time) (*time.Time, error) { if ask == "" || node == "" { - return nil + return nil, nil } pkeys, err := allKeys(h.Store) if err != nil { - return fmt.Errorf("the pipelines are not listed: %w", err) + return nil, fmt.Errorf("the pipelines are not listed: %w", err) } for _, key := range pkeys { for { @@ -37,19 +39,29 @@ func (h *Hearer) Taken(ask, node string) error { if i >= 0 && !open(p.Steps[i]) { h.Asks.answeredLate(p.Steps[i], h.logf) } - if i < 0 || !open(p.Steps[i]) || p.Steps[i].Node == node { + if i < 0 || !open(p.Steps[i]) { break } - p.Steps[i].Node = node + s := &p.Steps[i] + if s.Node == node && s.TakenAt != nil { + return s.TakenAt, nil + } + s.Node = node + if s.TakenAt == nil { + if at.IsZero() { + at = h.now() + } + s.TakenAt = ptrTime(at.UTC()) + } 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) + return nil, fmt.Errorf("pipeline %s cannot be written: %w", key, err) } - return nil + return s.TakenAt, nil } } - return nil + return nil, nil } func ptrTime(t time.Time) *time.Time { return &t } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go index 7bac6baa4..a5bde5bdc 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/supersede_test.go @@ -11,7 +11,7 @@ import ( // supersedes it no more (design 47 H §748). func TestATakenAskNamesItsNode(t *testing.T) { h, store, _, _, _, _ := fixture() - if err := h.Taken("a1", "n7"); err != nil { + if _, err := h.Taken("a1", "n7", h.now()); err != nil { t.Fatal(err) } if p, _, _ := store.Get(pkey); p.Steps[0].Node != "n7" || p.Steps[1].Node != "" { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go b/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go index f153b6460..434468ca7 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/verbs.go @@ -302,7 +302,7 @@ func (v Verbs) Rerun(caller string, in seat.RerunInput) (seat.RerunAnswer, error *s = seat.StepResult{Step: s.Step, Module: s.Module, Artifact: s.Artifact, Status: s.Status, Fingerprint: s.Fingerprint, Seat: s.Seat, Outcome: seat.StepResultOutcomeWaiting, By: caller, Rerun: true} if ask != nil { - s.Outcome, s.Started, s.Ask = seat.StepResultOutcomeRunning, &now, ask.ID + s.Outcome, s.Started, s.Ask, s.AskedAt, s.TakenAt = seat.StepResultOutcomeRunning, &now, ask.ID, &now, nil } if ask != nil && s.Step == seat.StepResultStepValidate && s.Seat == wants.Seat { rerunValidate(p, i, oldAsk, caller, now) @@ -497,9 +497,9 @@ func rerunValidate(p *seat.Pipeline, i int, oldAsk, caller string, now time.Time } switch { case j != i && st.Step == seat.StepResultStepValidate && st.Seat == wants.Seat && oldAsk != "" && - (st.Ask == oldAsk || st.Ask == AgainID(oldAsk)) && st.Outcome != seat.StepResultOutcomeSuperseded && st.Outcome != seat.StepResultOutcomeCancelled: + st.Ask == oldAsk && st.Outcome != seat.StepResultOutcomeSuperseded && st.Outcome != seat.StepResultOutcomeCancelled: *st = seat.StepResult{Step: st.Step, Module: st.Module, Artifact: st.Artifact, Status: st.Status, Fingerprint: st.Fingerprint, Seat: st.Seat, - Outcome: seat.StepResultOutcomeRunning, Started: &now, Ask: id, By: caller, Rerun: true} + Outcome: seat.StepResultOutcomeRunning, Started: &now, AskedAt: &now, Ask: id, By: caller, Rerun: true} case st.Step == seat.StepResultStepComposition && st.Seat == wants.Seat && st.Outcome == seat.StepResultOutcomeNotAsked && strings.HasPrefix(st.Why, "validate ended "): *st = seat.StepResult{Step: st.Step, Module: st.Module, Artifact: st.Artifact, Status: st.Status, Fingerprint: st.Fingerprint, Seat: st.Seat,