diff --git a/modules/mesh-delivery-sdk/steprun/askers.go b/modules/mesh-delivery-sdk/steprun/askers.go index 2f8caa64..76eaf2a0 100644 --- a/modules/mesh-delivery-sdk/steprun/askers.go +++ b/modules/mesh-delivery-sdk/steprun/askers.go @@ -3,22 +3,15 @@ package steprun import ( "encoding/json" "fmt" - "slices" - "strings" - "sync/atomic" - "time" buildrun "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildrun" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - stdio "git.novox.be/novox/mesh-sdk/go" - "git.novox.be/novox/mesh-sdk/go/seatdef" - "git.novox.be/novox/mesh-sdk/go/seatlist" ) -// **A holder refuses a kind its asker may not ask** (design 47 G §543, A-163, A-170). An ask rides -// `mesh.seat..accept..`, the asker the asking seat's own name, and its kind stays in its body. The -// holder takes the asker from the subject and checks the kind against the callers the seat's definition lists for it (read -// from the seat list); on main's subject, which names no asker, it takes only a build and a run. +// **A holder takes every asker's kind** (design 47 H §752, H-A-41: every account may call every verb; the asker judgement +// goes). An ask rides `mesh.seat..accept..`, the asker the asking seat's own name, and its kind stays in +// its body; the holder keys the step under that asker (design 47 G §481). On main's subject, which names no asker, it takes +// only a build and a run (design 47 G §543, A-184). // KindOf is the word the seat definition gives an ask's kind: run for a run or a compose, else the ask's own kind. func KindOf(ask sdkstep.StepAsk) string { @@ -28,67 +21,24 @@ func KindOf(ask sdkstep.StepAsk) string { return ask.Kind } -// CallersFromDefinitions is a Puller.Callers: the seats the definition of language's build seat lists as callers of an -// accepted kind, from the seat list's definitions (seatlist.Reader.Definitions). found is false when the definition or the -// kind is not held yet. A caller entry is `seat:`. -func CallersFromDefinitions(language string, definitions func() map[string]seatdef.Definition) func(kind string) (callers []string, found bool) { - return func(kind string) ([]string, bool) { - seat, err := sdkstep.BuildSeat(language) - if err != nil { - return nil, false - } - def, ok := definitions()[seat] - if !ok { - return nil, false - } - var out []string - found := false - for _, m := range def.Accepts { - if m.Name != kind { - continue - } - found = true - if m.Callers != nil { - for _, e := range m.Callers.List { - if name, ok := strings.CutPrefix(e, "seat:"); ok { - out = append(out, name) - } - } - } - } - return out, found - } -} - -// askerRefusal is why an ask must not be taken from its subject's asker: "" when it may be, and judged false when the seat -// list does not say yet (the ask is given back, not refused). -func (p *Puller) askerRefusal(msg *Pulled, ask sdkstep.StepAsk) (why string, judged bool) { +// askerRefusal is why an ask must not be taken from its subject: "" when it may be. Any named asker may ask any kind. +func (p *Puller) askerRefusal(msg *Pulled, ask sdkstep.StepAsk) (why string) { if msg.Subject == "" { - return "", true // a source that gives no subject (a test's) + return "" // a source that gives no subject (a test's) } _, _, asker, err := sdkstep.ParseAskSubjectBy(msg.Subject) if err != nil { - return fmt.Sprintf("the ask came on %s, which is no accept subject of this seat", msg.Subject), true + return fmt.Sprintf("the ask came on %s, which is no accept subject of this seat", msg.Subject) } kind := KindOf(ask) if asker == "" { // Main's subject takes a build, a run and, until it goes, the checker's check and recheck (design 47 G A-184, §543). if (ask.Build != nil && ask.Kind == sdkstep.AskDelivery) || kind == "run" || kind == sdkstep.AskCheck || kind == sdkstep.AskRecheck { - return "", true + return "" } - return fmt.Sprintf("the kind %s is not taken on main's subject, which takes only a build, a run and the checker's check and recheck (asker: none named)", kind), true + return fmt.Sprintf("the kind %s is not taken on main's subject, which takes only a build, a run and the checker's check and recheck (asker: none named)", kind) } - if p.Callers == nil { - return "", true - } - callers, found := p.Callers(kind) - if !found { - return "", false - } - if !slices.Contains(callers, asker) { - return fmt.Sprintf("the kind %s is not one %s may ask: its callers are %s", kind, asker, strings.Join(callers, ", ")), true - } - return "", true + return "" } // refuseAsker ends an ask whose asker may not ask its kind: its step key says failed with the reason, naming the kind and the @@ -143,87 +93,3 @@ func PriorRuns(get func(key string) (json.RawMessage, error)) func(string) int { return s.Run } } - -// giveBack hands an ask back to be judged again a little later, once the seat list says more. Why it cannot be judged is -// said once per change by the callers (SeatListCallers), not once per ask. -func (p *Puller) giveBack(msg *Pulled, ask sdkstep.StepAsk) { - if msg.NakAfter != nil && msg.NakAfter(DefaultPullWait) == nil { - return - } - _ = msg.Nak() -} - -// Seat list start retries: the first wait and its cap (a failed start is tried again, 1s doubling to 1 minute). -const ( - seatListRetryFirst = time.Second - seatListRetryMax = time.Minute -) - -// startSeatList tries start until it succeeds: each failure is said with its next try, the success once. wait is the -// pause a test replaces; the reader is returned once started (design 47 G §543, A-163, lead-47 2026-11-15: the reader's -// start is retried). -func startSeatList(start func() (*seatlist.Reader, error), wait func(time.Duration), logf func(format string, args ...any)) *seatlist.Reader { - if logf == nil { - logf = func(string, ...any) {} - } - delay := seatListRetryFirst - for { - r, err := start() - if err == nil { - logf("the seat list is followed") - return r - } - logf("following the seat list: %v; the asks are given back, the next try in %s", err, delay) - wait(delay) - delay = min(delay*2, seatListRetryMax) - } -} - -// SeatListCallers is a Puller.Callers that follows the seat list: it starts the reader once the runtime serves -// (seatlist.Start through the runtime's own way in) and answers from its definitions, found false until it has read them. -// A reader that cannot start is said and tried again with backoff while the asks are given back (design 47 G §543, A-163); -// why an ask is given back (reader not started, definition of the seat not held, kind not in it) is said once per change. -func SeatListCallers(language string, logf func(format string, args ...any)) func(kind string) ([]string, bool) { - var reader atomic.Pointer[seatlist.Reader] - go func() { - <-stdio.Served() - reader.Store(startSeatList(func() (*seatlist.Reader, error) { - return seatlist.Start(seatlist.Config{Bus: seatlist.StdioBus(), Bootstrap: seatlist.Bootstrapped()}) - }, time.Sleep, logf)) - }() - return callersOf(language, reader.Load, logf) -} - -// callersOf answers from the reader once started, saying once per change why a kind is not found. -func callersOf(language string, reader func() *seatlist.Reader, logf func(format string, args ...any)) func(kind string) ([]string, bool) { - var said atomic.Value // the last reason said - say := func(why string) { - if prev, _ := said.Swap(why).(string); prev != why && logf != nil { - logf("the asks are given back: %s", why) - } - } - return func(kind string) ([]string, bool) { - r := reader() - if r == nil { - say("the seat list reader is not started yet") - return nil, false - } - defs := r.Definitions() - seat, err := sdkstep.BuildSeat(language) - if err != nil { - say("the build seat of " + language + " is unknown: " + err.Error()) - return nil, false - } - if _, ok := defs[seat]; !ok { - say("the seat list holds no definition of the seat " + seat) - return nil, false - } - callers, found := CallersFromDefinitions(language, func() map[string]seatdef.Definition { return defs })(kind) - if !found { - say("the definition of " + seat + " does not accept the kind " + kind) - } else { - said.Store("") - } - return callers, found - } -} diff --git a/modules/mesh-delivery-sdk/steprun/askers_test.go b/modules/mesh-delivery-sdk/steprun/askers_test.go index eac24966..1057c63e 100644 --- a/modules/mesh-delivery-sdk/steprun/askers_test.go +++ b/modules/mesh-delivery-sdk/steprun/askers_test.go @@ -2,45 +2,34 @@ package steprun import ( "context" - "fmt" "strings" "testing" "time" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" - "git.novox.be/novox/mesh-sdk/go/seatdef" - "git.novox.be/novox/mesh-sdk/go/seatlist" ) -func callersFor(table map[string][]string) func(string) ([]string, bool) { - return func(kind string) ([]string, bool) { c, ok := table[kind]; return c, ok } -} - -// Design 47 G §543 (BG2, BG3, A-163, A-170): an ask whose kind its asker, the last token of its subject, is no caller of is -// answered error naming the kind and the asker and ended failed; a step on main's subject is refused as well (its check and recheck -// taken, A-184); one the -// seat list does not judge yet is given back. -func TestPullRefusesAKindItsAskerMayNotAsk(t *testing.T) { +// Design 47 H §752, H-A-41: every account may ask every kind, so an ask of any asker its subject names runs, keyed under +// that asker, and none is given back to be judged; a step on main's subject, which names no asker, is refused (design 47 G +// §543, A-184: main's subject takes a build, a run and the checker's check and recheck). +func TestPullTakesEveryAskersKind(t *testing.T) { step := testAsk(sdkstep.AskStep) step.Check = nil step.Step = &sdkstep.StepRun{Pipeline: "p", Step: sdkstep.StepUnit, Module: "m", Artifact: "m:a", Commit: "0123456789abcdef0123456789abcdef01234567", Fingerprint: "fp", Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: "0123456789abcdef0123456789abcdef01234567"}} build := testAsk(sdkstep.AskDelivery) - table := map[string][]string{"step": {"mesh-pipeline-runner"}, "delivery": {"mesh-builds"}} for _, c := range []struct { name, subject string ask sdkstep.StepAsk - callers func(string) ([]string, bool) - want string // "" runs it, "refused" refuses it, "back" gives it back + want string // "" runs it, "refused" refuses it }{ - {"the indexer's step", "mesh.seat.node-build-go.accept.check.mesh-catalog", step, callersFor(table), "refused"}, - {"the runner's step", "mesh.seat.node-build-go.accept.check.mesh-pipeline-runner", step, callersFor(table), ""}, - {"a step on main's subject", "mesh.seat.node-build-go.accept.check", step, callersFor(table), "refused"}, - {"a build on main's subject", "mesh.seat.node-build-go.accept.delivery", build, callersFor(table), ""}, + {"the indexer's step", "mesh.seat.node-build-go.accept.check.mesh-catalog", step, ""}, + {"the runner's step", "mesh.seat.node-build-go.accept.check.mesh-pipeline-runner", step, ""}, + {"a step on main's subject", "mesh.seat.node-build-go.accept.check", step, "refused"}, + {"a build on main's subject", "mesh.seat.node-build-go.accept.delivery", build, ""}, // A-184: until main's subject goes, it also takes the checker's check and recheck asks. - {"the checker's check on main's subject", "mesh.seat.node-build-go.accept.check", testAsk(sdkstep.AskCheck), callersFor(table), ""}, - {"the checker's recheck on main's subject", "mesh.seat.node-build-go.accept.recheck", testAsk(sdkstep.AskRecheck), callersFor(table), ""}, - {"a kind the seat list does not hold", "mesh.seat.node-build-go.accept.check.mesh-pipeline-runner", step, callersFor(nil), "back"}, + {"the checker's check on main's subject", "mesh.seat.node-build-go.accept.check", testAsk(sdkstep.AskCheck), ""}, + {"the checker's recheck on main's subject", "mesh.seat.node-build-go.accept.recheck", testAsk(sdkstep.AskRecheck), ""}, } { t.Run(c.name, func(t *testing.T) { src, st, sy := NewFakeAsks(), newSteps(), &sayer{} @@ -52,7 +41,7 @@ func TestPullRefusesAKindItsAskerMayNotAsk(t *testing.T) { src.PublishWithSubject(queue, raw, c.subject) _, _, asker, _ := sdkstep.ParseAskSubjectBy(c.subject) ran := make(chan struct{}, 1) - p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 }, Callers: c.callers, + p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 }, PullWait: 5 * time.Millisecond, Heartbeat: time.Hour, Say: sy.say, ProgressEvery: time.Hour, Run: func(context.Context, sdkstep.StepAsk) (any, error) { ran <- struct{}{}; return nil, nil }} ctx, cancel := context.WithCancel(context.Background()) @@ -79,58 +68,7 @@ func TestPullRefusesAKindItsAskerMayNotAsk(t *testing.T) { t.Fatal("a refused ask ran") default: } - case "back": - waitFor(t, "the ask given back", func() bool { src.mu.Lock(); defer src.mu.Unlock(); return len(src.Delayed) > 0 }) } }) } } - -func TestCallersFromDefinitionsReadsTheSeatsCallers(t *testing.T) { - def := seatdef.Definition{Seat: "node-build-go", Accepts: []seatdef.Message{ - {Name: "step", Callers: &seatdef.Callers{List: []string{"seat:mesh-pipeline-runner"}}}}} - callers := CallersFromDefinitions(sdkstep.AskGo, func() map[string]seatdef.Definition { return map[string]seatdef.Definition{"node-build-go": def} }) - if got, ok := callers("step"); !ok || len(got) != 1 || got[0] != "mesh-pipeline-runner" { - t.Fatalf("callers of step = %v, %v", got, ok) - } - if _, ok := callers("run"); ok { - t.Fatal("a kind the definition does not list is found") - } -} - -func TestStartSeatListRetriesWithBackoffAndSaysIt(t *testing.T) { - var lines []string - var waits []time.Duration - tries := 0 - start := func() (*seatlist.Reader, error) { - tries++ - if tries <= 8 { - return nil, fmt.Errorf("binding the consumer: context deadline exceeded") - } - return &seatlist.Reader{}, nil - } - r := startSeatList(start, func(d time.Duration) { waits = append(waits, d) }, func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) }) - if r == nil || tries != 9 || len(waits) != 8 { - t.Fatalf("reader %v tries %d waits %v", r, tries, waits) - } - if waits[0] != time.Second || waits[1] != 2*time.Second || waits[7] != time.Minute { - t.Fatalf("waits = %v, want 1s doubling to 1m", waits) - } - if len(lines) != 9 || !strings.Contains(lines[0], "next try in 1s") || lines[8] != "the seat list is followed" { - t.Fatalf("lines = %q", lines) - } -} - -func TestCallersOfSaysWhyOncePerChange(t *testing.T) { - var lines []string - var r *seatlist.Reader - c := callersOf(sdkstep.AskGo, func() *seatlist.Reader { return r }, func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) }) - for range 3 { - if _, ok := c("step"); ok { - t.Fatal("found without a reader") - } - } - if len(lines) != 1 || !strings.Contains(lines[0], "not started yet") { - t.Fatalf("lines = %q", lines) - } -} diff --git a/modules/mesh-delivery-sdk/steprun/pull.go b/modules/mesh-delivery-sdk/steprun/pull.go index d753a92b..72ef627d 100644 --- a/modules/mesh-delivery-sdk/steprun/pull.go +++ b/modules/mesh-delivery-sdk/steprun/pull.go @@ -87,9 +87,6 @@ type Puller struct { // as the object named object, which the key and a run's step-ended name and the seat's verb `output` answers (A-254); a // holder keeps it in the object store its manifest requires. Nil keeps none: such an answer ends the ask failed, saying so. KeepOutput func(object string, body []byte) error - // Callers says which seats the build seat's definition lists as callers of an accepted kind (CallersFromDefinitions); - // found is false while the seat list holds no such kind. Nil checks no named asker (design 47 G §543, A-163). - Callers func(kind string) (callers []string, found bool) // Prior says how many runs of the ask under a step key started before (the Run of the step the key holds, PriorRuns; 0 for // none), so the poison rule and the transport fault's run count only runs that started, never an ask given back before it // ran. Nil counts the bus's deliveries (a test's). @@ -710,10 +707,7 @@ func (p *Puller) take(ctx context.Context, msg *Pulled) { p.endUnrun(msg, ask, key, step, prior) return } - if why, judged := p.askerRefusal(msg, ask); !judged { - p.giveBack(msg, ask) - return - } else if why != "" { + if why := p.askerRefusal(msg, ask); why != "" { p.refuseAsker(msg, ask, why) return }