Merge pull request 'mesh-delivery-indexer: the tools runtime reads its view of the trunk again until it holds the newest revision heard (design 47 H2 H-A-766, H-A-767; issue 1984)' (#369) from fix/47-H2-766-view-reread into main

This commit was merged in pull request #369.
This commit is contained in:
2026-11-26 01:13:20 +00:00
5 changed files with 321 additions and 15 deletions
@@ -179,7 +179,11 @@ func main() {
say := func(format string, a ...any) {
fmt.Fprintf(os.Stderr, "[mesh-delivery-indexer-tools] "+format+"\n", a...)
}
kept := &index.KeptTrunk{Say: say}
kept, err := keptTrunk(say)
if err != nil {
fmt.Fprintf(os.Stderr, "[mesh-delivery-indexer-tools] %v\n", err)
os.Exit(1)
}
trunk := index.TrunkStates{Objects: revisions, Under: keptSource, Kept: kept}
follow := &views.Reader{Module: os.Getenv(stdio.ServedAs), Logf: say, Acts: acts}
if err := follow.Adopt(trunkFollower(kept, revisions), meshcatalog.Seat, meshcatalog.EventCatalogRevision, func(ctx context.Context, data []byte) error {
@@ -200,6 +204,10 @@ func main() {
go watched.Follow(state, nil)
}
tools = append(tools, catalogTools(trunk.State, kept, watched)...)
tools = append(tools, index.TrunkAgainTools(kept, revisions, func(what, why string) error {
_, err := acts.Record(meshhandactsclient.Request{What: what, Why: why, Cause: index.TrunkAgainTool, Verb: "trunk-again"})
return err
})...)
// The seat mesh-catalog's verb revision, a first view of the catalog half answered in this runtime of its own to exactly
// the parts whose entry declares it (novox/hq ADR 0663).
tools = append(tools, index.RevisionTool(revisions, kept), index.EntryTool(revisions, keptRead{stdio.State(index.EntryIndexState)}), index.EntriesTool(revisions, keptRead{stdio.State(index.EntryIndexState)}, keptRead{stdio.State(index.NamedState)}), entryReadTool(revisions), index.VersionsTool(keptRead{stdio.State(index.VersionsState)}),
@@ -254,3 +262,20 @@ func trunkFollower(kept *index.KeptTrunk, revisions index.Objects) *views.Follow
return views.Answer{V: views.Version, Owner: meshcatalog.Seat, State: "catalog-half", Created: time.Unix(1, 0).UTC(), Values: map[string]json.RawMessage{}, At: time.Now()}, nil
}}
}
// keptTrunk is the view of the trunk with the wait before its re-read is made again read from the module's settings
// view-reread-pace-first and view-reread-pace-max (design 47 H2 H-A-766 (1), H-A-767): 5 s doubling to 1 min.
func keptTrunk(say func(string, ...any)) (*index.KeptTrunk, error) {
first, err := stdio.SettingDuration(index.RereadPaceFirstSetting, index.DefaultRereadFirst)
if err != nil {
return nil, err
}
most, err := stdio.SettingDuration(index.RereadPaceMaxSetting, index.DefaultRereadMax)
if err != nil {
return nil, err
}
if most < first {
return nil, fmt.Errorf("the setting %s (%s) is below %s (%s)", index.RereadPaceMaxSetting, most, index.RereadPaceFirstSetting, first)
}
return &index.KeptTrunk{Say: say, First: first, Max: most}, nil
}
@@ -17,6 +17,7 @@ import (
runner "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/busretry"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/keys"
@@ -682,6 +683,11 @@ type TrunkStates struct {
// lists the bucket. Say, when set, says each read of a revision with its number, its module count and how long it took.
type KeptTrunk struct {
Say func(format string, a ...any)
// First and Max are the wait before a re-read that failed, or that came out below the newest revision heard, is made again,
// doubling to Max (design 47 H2 H-A-766 (1)); zero is DefaultRereadFirst and DefaultRereadMax. Pause waits one of them and
// answers false once ctx ended; nil waits on a timer of ctx's own.
First, Max time.Duration
Pause func(ctx context.Context, wait time.Duration) bool
read sync.Mutex // one re-read at a time; a read of the view never holds it
mu sync.Mutex
@@ -691,11 +697,16 @@ type KeptTrunk struct {
reading bool
readingRev int64
readingSince time.Time
at int64
mods Store
held Store
rev meshcatalog.CatalogRevision
entries []meshcatalog.CatalogEntry
// heard is the newest trunk revision any heard event named: the view is read again until it holds it (H-A-766 (1)).
// retrying is set while the goroutine doing so runs, and said is the revision whose failure was said already.
heard int64
retrying bool
said int64
at int64
mods Store
held Store
rev meshcatalog.CatalogRevision
entries []meshcatalog.CatalogEntry
// manifests are the manifests the revision's entries carry whole, as JSON, by their digest as a module's value names it
// (A-487c: the verbs module and provides answer them from here, never from the manifests state).
manifests map[string]string
@@ -719,8 +730,14 @@ func (k *KeptTrunk) reread(ctx context.Context, o Objects) error {
k.mu.Unlock()
}()
fail := func(err error) error {
if k.Say != nil {
k.Say("the view of the trunk is BEHIND, its re-read failed after %s (the view held stays, the next heard catalog-revision reads again): %v", time.Since(began).Round(time.Millisecond), err)
k.mu.Lock()
again := k.heard != 0 && k.said != k.heard // said once per revision heard, whatever the number of tries
if again {
k.said = k.heard
}
k.mu.Unlock()
if k.Say != nil && (again || k.heard == 0) {
k.Say("the view of the trunk is BEHIND, its re-read failed after %s (the view held stays; it is read again until it holds the newest revision heard): %v", time.Since(began).Round(time.Millisecond), err)
}
return err
}
@@ -778,21 +795,110 @@ func (k *KeptTrunk) Manifest(digest string) (string, error) {
return raw, nil
}
// DefaultRereadFirst and DefaultRereadMax are the waits of a re-read made again: 5 s doubling to 1 min (H-A-766 (1)).
const (
DefaultRereadFirst = 5 * time.Second
DefaultRereadMax = time.Minute
)
// Heard is the seat's event catalog-revision: one of a trunk revision newer than the one held wakes one re-read of the
// objects; any other event is no change. A failed re-read is an error, so the event is offered again.
// objects; any other event is no change. A re-read that fails, or that comes out below the newest revision heard, is made
// again by a goroutine of its own with a backoff (First, doubling to Max) until the view holds that revision, with no further
// event (H-A-766 (1)); the error of the first try is answered as before.
func (k *KeptTrunk) Heard(ctx context.Context, o Objects, e meshcatalog.CatalogRevisionEvent) error {
if e.Kind != "trunk" || !isTrunkBranch(e.Branch) {
return nil
}
k.read.Lock()
defer k.read.Unlock()
k.mu.Lock()
held := k.started && e.Revision <= k.at
if !held && e.Revision > k.heard {
k.heard = e.Revision
}
k.mu.Unlock()
if held {
k.read.Unlock()
return nil
}
return k.reread(ctx, o)
err := k.reread(ctx, o)
k.read.Unlock()
if err != nil || k.behind() {
k.again(context.WithoutCancel(ctx), o)
}
return err
}
// behind says whether the view holds a revision below the newest one heard.
func (k *KeptTrunk) behind() bool {
k.mu.Lock()
defer k.mu.Unlock()
return k.heard > k.at
}
// Reached is the newest trunk revision any heard event named.
func (k *KeptTrunk) Reached() int64 {
k.mu.Lock()
defer k.mu.Unlock()
return k.heard
}
// again reads the view again, after a wait that doubles, until it holds the newest revision heard; one goroutine at a time.
func (k *KeptTrunk) again(ctx context.Context, o Objects) {
k.mu.Lock()
if k.retrying {
k.mu.Unlock()
return
}
k.retrying = true
k.mu.Unlock()
go func() {
defer func() {
k.mu.Lock()
k.retrying = false
k.mu.Unlock()
}()
wait, most := k.First, k.Max
if wait <= 0 {
wait = DefaultRereadFirst
}
if most <= 0 {
most = DefaultRereadMax
}
for k.behind() {
if k.Say != nil {
k.Say("the view of the trunk holds revision %d, below the %d heard: read again in %s", k.HeldRevision(), k.Reached(), wait)
}
if !k.pause(ctx, wait) {
return
}
k.read.Lock()
err := k.reread(ctx, o)
k.read.Unlock()
if err == nil && !k.behind() && k.Say != nil {
k.Say("the view of the trunk reached the newest revision heard, %d", k.HeldRevision())
}
if wait *= 2; wait > most {
wait = most
}
}
}()
}
// HeldRevision is the trunk revision the view held was read at, 0 before the first read.
func (k *KeptTrunk) HeldRevision() int64 {
k.mu.Lock()
defer k.mu.Unlock()
return k.at
}
func (k *KeptTrunk) pause(ctx context.Context, wait time.Duration) bool {
if k.Pause != nil {
return k.Pause(ctx, wait)
}
p, cancel := context.WithTimeout(ctx, wait)
<-p.Done()
cancel()
return ctx.Err() == nil
}
// read is the view's modules and nodes, and the revision they were read at; none kept yet is ErrNoRevision.
@@ -1026,3 +1132,39 @@ func (ix *Indexer) NewestRevision(ctx context.Context, branch string) int64 {
}
return newest.Revision
}
// TrunkAgainTool reads the indexer's view of the trunk again now, by hand (design 47 H2 H-A-766 (3), principle 4).
const TrunkAgainTool = "indexer_trunk_again"
// The settings of the wait before a re-read of the view of the trunk is made again (H-A-766 (1), H-A-767): 5 s doubling to 1 min.
const (
RereadPaceFirstSetting = "view-reread-pace-first"
RereadPaceMaxSetting = "view-reread-pace-max"
)
// TrunkAgainTools is the tool indexer_trunk_again: the view of the trunk read again from the newest revision of the objects now,
// whatever event was heard. It acts first and says the hand act after, as the fingerprints' tool does: record is told what was done.
func TrunkAgainTools(kept *KeptTrunk, o Objects, record func(what, why string) error) []stdio.Tool {
return []stdio.Tool{{Name: TrunkAgainTool,
Description: "Read the indexer's view of the trunk (its modules and module graph, the newest catalog graph revision) again now, " +
"from the indexer's own objects, whatever event was heard. For a view the tools runtime holds behind the revision the " +
"indexer said (its re-read timed out): the runtime reads it again by itself with a backoff, this reads it now. Answers the " +
"revision held before and after. Acts first, then records the hand act through mesh-hand-acts; when that cannot be recorded the answer says so.",
Input: map[string]any{"type": "object", "required": []string{"why"}, "properties": map[string]any{
"why": map[string]any{"type": "string", "description": "why it is read again now, in plain words"}}},
Run: func(args map[string]any) (any, error) {
why, _ := args["why"].(string)
if why = strings.TrimSpace(why); why == "" {
return nil, errors.New("say why: a hand act is said in the journal")
}
was := kept.HeldRevision()
if err := kept.Reread(context.Background(), o); err != nil {
return nil, fmt.Errorf("the view of the trunk could not be read again (it stays at revision %d): %w", was, err)
}
res := map[string]any{"was": was, "revision": kept.HeldRevision(), "heard": kept.Reached()}
if err := record("read the view of the trunk again by hand", why); err != nil {
res["recorded"] = "the hand act was not recorded through mesh-hand-acts: " + err.Error()
}
return res, nil
}}}
}
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -53,7 +54,8 @@ func TestTheKeptTrunkIsReadAtTheStartAndOncePerHeardRevision(t *testing.T) {
}
}
said := []string{}
kept := &KeptTrunk{Say: func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }}
// A re-read made again waits on a pause: here none, so the next heard event is what reads again (H-A-766 has its own test).
kept := &KeptTrunk{Say: func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }, Pause: func(context.Context, time.Duration) bool { return false }}
modules := TrunkStates{Objects: objects, Under: func(string) views.Source { return nil }, Kept: kept}.State("modules")
keep(1, "avahi")
if err := kept.Reread(context.Background(), objects); err != nil { // the start's read
@@ -380,3 +382,125 @@ func TestAViewOfTheTrunkAnswersAwayWhileTheStartsReadIsHeld(t *testing.T) {
t.Errorf("revision after the read answered %+v (%v), want revision 7", a, err)
}
}
// H-A-766 (1): a re-read of the trunk that fails after a heard catalog-revision is made again with a backoff (First, doubling to
// Max) until the view holds the revision heard, with no second event; the failure is said once for the revision.
func TestTheKeptTrunkReadsAgainAfterAFailedReReadUntilItHoldsTheRevisionHeard(t *testing.T) {
objects := &listedObjects{memObjects: newMemObjects(0)}
keep := func(revision int64) {
t.Helper()
data, _ := json.Marshal(meshcatalog.CatalogEntry{Module: "avahi", Repository: catalogueRepo, None: "not built", ManifestDigest: keptDigest("avahi"), Manifest: map[string]any{"module": "avahi"}})
digest := fmt.Sprintf("%d-avahi", revision)
rev := meshcatalog.CatalogRevision{Revision: revision, Kind: "trunk", Entries: []meshcatalog.EntryRef{{Module: "avahi", Digest: digest}}}
if err := KeepRevision(context.Background(), objects, rev, map[string][]byte{digest: data}); err != nil {
t.Fatal(err)
}
}
var mu sync.Mutex
var said, waits []string
held := make(chan struct{})
var tries int
kept := &KeptTrunk{First: 5 * time.Second, Max: 10 * time.Second}
kept.Say = func(f string, a ...any) {
mu.Lock()
defer mu.Unlock()
m := fmt.Sprintf(f, a...)
said = append(said, m)
if strings.Contains(m, "reached the newest revision heard") {
close(held)
}
}
kept.Pause = func(_ context.Context, wait time.Duration) bool {
mu.Lock()
defer mu.Unlock()
waits = append(waits, wait.String())
if tries++; tries == 3 {
objects.fail.Store(false) // the bucket answers again at the third wait
}
return true
}
keep(1)
if err := kept.Reread(context.Background(), objects); err != nil {
t.Fatal(err)
}
keep(2)
objects.fail.Store(true)
if err := kept.Heard(context.Background(), objects, meshcatalog.CatalogRevisionEvent{Kind: "trunk", Revision: 2}); err == nil {
t.Fatal("a failed re-read was taken")
}
select {
case <-held:
case <-time.After(10 * time.Second):
t.Fatal("the view never read again to the revision heard, with no second event")
}
mu.Lock()
defer mu.Unlock()
if kept.HeldRevision() != 2 {
t.Errorf("the view holds revision %d, want the 2 heard", kept.HeldRevision())
}
if strings.Join(waits, " ") != "5s 10s 10s" {
t.Errorf("waits = %v, want 5s doubling to the most, 10s", waits)
}
behind := 0
for _, m := range said {
if strings.Contains(m, "BEHIND") {
behind++
}
}
if behind != 1 {
t.Errorf("the failure was said %d times, want once for the revision: %q", behind, said)
}
}
// H-A-766 (3): the tool indexer_trunk_again reads the view of the trunk again now, with no event heard, says the revision held
// before and after, and refuses a call that says no why; the hand act is recorded.
func TestTheTrunkAgainToolReadsTheViewNow(t *testing.T) {
objects := newMemObjects(0)
keep := func(revision int64) {
data, _ := json.Marshal(meshcatalog.CatalogEntry{Module: "avahi", Repository: catalogueRepo, None: "not built", ManifestDigest: keptDigest("avahi"), Manifest: map[string]any{"module": "avahi"}})
digest := fmt.Sprintf("%d-avahi", revision)
rev := meshcatalog.CatalogRevision{Revision: revision, Kind: "trunk", Entries: []meshcatalog.EntryRef{{Module: "avahi", Digest: digest}}}
if err := KeepRevision(context.Background(), objects, rev, map[string][]byte{digest: data}); err != nil {
t.Fatal(err)
}
}
keep(1)
kept := &KeptTrunk{}
if err := kept.Reread(context.Background(), objects); err != nil {
t.Fatal(err)
}
keep(2)
var recorded []string
tool := TrunkAgainTools(kept, objects, func(what, why string) error { recorded = append(recorded, what+": "+why); return nil })[0]
if _, err := tool.Run(map[string]any{}); err == nil {
t.Error("a call with no why was taken")
}
got, err := tool.Run(map[string]any{"why": "the re-read timed out"})
if err != nil {
t.Fatal(err)
}
if res, _ := got.(map[string]any); res["was"] != int64(1) || res["revision"] != int64(2) || kept.HeldRevision() != 2 || len(recorded) != 1 {
t.Errorf("answered %v, held %d, recorded %q, want revision 1 then 2 and one hand act", got, kept.HeldRevision(), recorded)
}
}
// H-A-767: the first view of modules over TrunkStates answers the revision its values are read at (a KeptTrunk at 529), never the
// revision of the mark under it (531), so a reader is never told it holds a revision it does not. It holds on the trunk already.
func TestTheModulesFirstViewOverTheTrunkAnswersTheRevisionItsValuesAreReadAt(t *testing.T) {
objects := newMemObjects(0)
data, _ := json.Marshal(meshcatalog.CatalogEntry{Module: "avahi", Repository: catalogueRepo, None: "not built", ManifestDigest: keptDigest("avahi"), Manifest: map[string]any{"module": "avahi"}})
rev := meshcatalog.CatalogRevision{Revision: 529, Kind: "trunk", Entries: []meshcatalog.EntryRef{{Module: "avahi", Digest: "529-avahi"}}}
if err := KeepRevision(context.Background(), objects, rev, map[string][]byte{"529-avahi": data}); err != nil {
t.Fatal(err)
}
kept := &KeptTrunk{}
if err := kept.Reread(context.Background(), objects); err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(views.Mark{V: views.Version, State: "modules", Revision: 531})
ts := TrunkStates{Objects: objects, Kept: kept, Under: func(string) views.Source { return memSource{"modules": {Value: raw}} }}
a, err := views.FirstView("indexer", "modules", ts.State("modules"), ts.State(views.MarksState), "", time.Now())
if err != nil || a.Revision != 529 {
t.Errorf("the first view answered revision %d (%v), want 529", a.Revision, err)
}
}
@@ -215,7 +215,7 @@ func TestManifestDeclarations(t *testing.T) { // 1238/AC1, 1444 (reads the build
t.Errorf("%s answers the state %s, which the views family does not say", v.Tool, v.State)
}
}
want = append(want, "indexer_first_views", "indexer_first_view_again", index.ConfigReadersTool, index.BusStateTool, index.ImportsTool, index.PackageUsersTool, // H-A-527; DLV-F3r, DLV-I10, DLV-I11, DLV-I7, DLV-I8, DLV-I9c
want = append(want, "indexer_first_views", "indexer_first_view_again", index.TrunkAgainTool, index.ConfigReadersTool, index.BusStateTool, index.ImportsTool, index.PackageUsersTool, // H-A-527; DLV-F3r, DLV-I10, DLV-I11, DLV-I7, DLV-I8, DLV-I9c
index.SeatDefinitionTool, index.SeatDefinitionsAgainTool, index.OutOfStepTool, // DLV-I19
index.FingerprintsAgainTool, // issue 1966
// milestone 47/G W4.12 (design 47, the indexer's tools): the catalog graph revisions read and steered.
@@ -223,7 +223,7 @@ func TestManifestDeclarations(t *testing.T) { // 1238/AC1, 1444 (reads the build
index.EntryIndexTool, index.EntryPinsTool, index.NamedDropTool, // design 47 G §675, H-A-204
index.ProposalsTool, index.ProposalDropTool, index.SeedAgainTool, index.InputsTool, index.PinsTool, index.PinDropTool, index.PendingTool, index.TakeInPendingTool, index.TakeInAgainTool, index.TakeInSkipTool, index.RunsTool, index.RunDropTool, // design 47 G §667, §8, §579, §64 (A-194)
index.DryRunsTool, index.RegistrationsTool) // design 47 H2 §804, §806
if !reflect.DeepEqual(m.Tools, want) || len(want) != 46 || !slices.Contains(m.Tools, "modules") {
if !reflect.DeepEqual(m.Tools, want) || len(want) != 47 || !slices.Contains(m.Tools, "modules") {
t.Errorf("tools = %v, want %v", m.Tools, want)
}
// Design 47 G §2, §9: every version of every registration is kept with no age limit, 64 MiB, on the old streams section.
@@ -246,7 +246,7 @@ func TestManifestDeclarations(t *testing.T) { // 1238/AC1, 1444 (reads the build
if m.Class != "core" {
t.Errorf("class = %q, want core", m.Class)
}
for key, def := range map[string]string{busretry.PaceFirstSetting: "1s", busretry.PaceMaxSetting: "30s", index.WaitingLineSetting: "1m", index.WaitingBoundSetting: "5m"} {
for key, def := range map[string]string{busretry.PaceFirstSetting: "1s", busretry.PaceMaxSetting: "30s", index.WaitingLineSetting: "1m", index.WaitingBoundSetting: "5m", index.RereadPaceFirstSetting: "5s", index.RereadPaceMaxSetting: "1m"} {
if c, _ := m.Config[key].(map[string]any); c["default"] != def || c["runtime"] != true || c["type"] != "string" {
t.Errorf("config %s = %v, want a runtime string setting defaulting to %s", key, m.Config[key], def)
}
+15
View File
@@ -44,6 +44,7 @@
"indexer_read_seat_definitions",
"indexer_first_views",
"indexer_first_view_again",
"indexer_trunk_again",
"indexer_config_readers",
"indexer_bus_state",
"indexer_imports",
@@ -116,6 +117,20 @@
"default": "30s",
"runtime": true
},
"view-reread-pace-first": {
"type": "string",
"description": "the first wait before the tools runtime reads its view of the trunk again after a re-read that failed or came out below the newest catalog-revision heard, doubling to view-reread-pace-max, a Go duration (design 47 H2 H-A-766 (1), H-A-767).",
"trusted": false,
"default": "5s",
"runtime": true
},
"view-reread-pace-max": {
"type": "string",
"description": "the longest wait between two re-reads of the view of the trunk, a Go duration (design 47 H2 H-A-766 (1), H-A-767).",
"trusted": false,
"default": "1m",
"runtime": true
},
"npm-test-files": {
"type": "string",
"description": "the patterns of the files of an npm package that leave its digest, comma-separated: a segment as path.Match reads it, ** any number of directories; node's own test patterns and everything under a test directory (design 47 H2 H-A-573, H-A-598).",