steprun: retry the seat list reader's start with backoff and say why asks are given back
Builds: design 47 G §543, A-163, §543 (lead-47 2026-11-15: the reader's start is retried)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"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"
|
||||
@@ -118,33 +119,86 @@ func (p *Puller) refuseAsker(msg *Pulled, ask sdkstep.StepAsk, why string) {
|
||||
_ = msg.Term()
|
||||
}
|
||||
|
||||
// giveBack hands an ask back to be judged again a little later, once the seat list says more.
|
||||
// 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) {
|
||||
p.logf("the ask %s is given back on %s: the seat list holds no definition of its kind yet", ask.ID, p.Node)
|
||||
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 the asks are given back until it can (design 47 G §543, A-163).
|
||||
// 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()
|
||||
r, err := seatlist.Start(seatlist.Config{Bus: seatlist.StdioBus(), Bootstrap: seatlist.Bootstrapped()})
|
||||
if err != nil && logf != nil {
|
||||
logf("following the seat list: %v", err)
|
||||
}
|
||||
reader.Store(r)
|
||||
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.Load()
|
||||
r := reader()
|
||||
if r == nil {
|
||||
say("the seat list reader is not started yet")
|
||||
return nil, false
|
||||
}
|
||||
return CallersFromDefinitions(language, r.Definitions)(kind)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ 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) {
|
||||
@@ -95,3 +97,40 @@ func TestCallersFromDefinitionsReadsTheSeatsCallers(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user