From 256eb34a0d25300baf8f26ecc620acfb618e15d3 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 17:12:50 +0100 Subject: [PATCH 1/3] Builder: a build step ask waits for its revision before it builds (H-A-155) A step ask reached the builder before its picture of main's catalog revisions held the revision the pipeline builds; the fingerprint was built as no first branch's build, reserved no version, and the step failed on builder_reserve_done's words. The ask now waits, woken by each entry the picture takes in, until the picture holds its source revision and the asked entry from its branch; past the bound it fails naming the revision and builds nothing. Builds: design 47 H H-A-155, H-A-141 --- .../internal/build/revisionwait.go | 94 +++++++++++++++++++ .../internal/build/run.go | 11 +++ .../internal/build/step.go | 4 + .../internal/build/step_test.go | 74 +++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 modules/mesh-delivery-builder/internal/build/revisionwait.go diff --git a/modules/mesh-delivery-builder/internal/build/revisionwait.go b/modules/mesh-delivery-builder/internal/build/revisionwait.go new file mode 100644 index 00000000..e67548a8 --- /dev/null +++ b/modules/mesh-delivery-builder/internal/build/revisionwait.go @@ -0,0 +1,94 @@ +package build + +import ( + "context" + "fmt" + "time" + + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" +) + +// **A build waits for its revision; a build with no version is never made** (design 47 H H-A-155, H-A-156; H-A-141): a step +// ask names its branch and source revision, and the builder reserves the version before it builds. When the builder's picture +// of that branch does not hold that source revision yet, or holds it but has not taken in the entry of the asked fingerprint +// from it, the ask waits for the picture to take it in, woken by each entry the picture takes in (take, EntryGone), never a +// timer or a poll, up to RevisionWait (the setting revision-wait-minutes); past it the ask fails, naming the revision and the +// branch, and nothing is built. A step ask with no source (a run on request) does not wait. + +// DefaultRevisionWait is the bound of a step ask's wait for its revision, its setting revision-wait-minutes' default (H-A-156). +const DefaultRevisionWait = 2 * time.Minute + +func (b *Builder) revisionWait() time.Duration { + if b.RevisionWait > 0 { + return b.RevisionWait + } + return DefaultRevisionWait +} + +// pictureMoved wakes every step ask waiting for the picture: an entry was taken in or went. +func (b *Builder) pictureMoved() { + b.mu.Lock() + moved := b.moved + b.moved = make(chan struct{}) + b.mu.Unlock() + close(moved) +} + +// holdsRevision says whether the builder's picture of branch holds the source revision and has taken in every entry of the +// ask from that branch. +func (b *Builder) holdsRevision(branch string, source int64, entries []meshbuilds.BuildEntry) bool { + held, ok := b.Picture(branch) + if !ok || held < source { + return false + } + b.mu.Lock() + defer b.mu.Unlock() + for _, e := range entries { + key, err := builds.BuilderBuildKey(e.Module, e.Fingerprint) + if err != nil { + return true // the build key is refused where the entry is built, with why + } + if b.origin[key].Branch != branch && b.heard[branch+"\x00"+e.Module].Fp2 != e.Fingerprint { + return false + } + } + return true +} + +// awaitRevision returns no outcome once the builder's picture of the ask's branch holds its source revision; past the bound the +// step's outcome failed, at the builder's stop could not run, each with its words. +func (b *Builder) awaitRevision(ctx context.Context, run meshbuilds.StepRun) (outcome, said string) { + if b.Picture == nil || run.Source <= 0 || run.Branch == "" { + return "", "" + } + bound := b.revisionWait() + notHeld := fmt.Sprintf("revision %d of %s not in the builder's picture", run.Source, run.Branch) + wait, stop := context.WithTimeout(ctx, bound) // a context's bound, no timer (ADR 0437 §8) + defer stop() + told := false + for { + b.mu.Lock() + moved := b.moved + b.mu.Unlock() + if b.holdsRevision(run.Branch, run.Source, run.Entries) { + if told { + b.logf("%s: revision %d of %s is in the builder's picture: the build goes on", run.Pipeline, run.Source, run.Branch) + } + return "", "" + } + if !told { + b.logf("%s: %s yet: the ask waits for it, at most %s", run.Pipeline, notHeld, bound) + told = true + } + select { + case <-moved: + case <-wait.Done(): + if ctx.Err() != nil { + return meshbuilds.StepEndedOutcomeCouldNotRun, notHeld + ": the builder stopped before it was" + } + b.logf("%s: %s within %s: the step fails, nothing is built", run.Pipeline, notHeld, bound) + return meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s within %s", notHeld, bound) + } + } +} diff --git a/modules/mesh-delivery-builder/internal/build/run.go b/modules/mesh-delivery-builder/internal/build/run.go index fcabe73a..242707b5 100644 --- a/modules/mesh-delivery-builder/internal/build/run.go +++ b/modules/mesh-delivery-builder/internal/build/run.go @@ -107,6 +107,12 @@ type Builder struct { Numbers Numbers // Reserved reserves the version of a trunk build at its start (design 47 G §369); nil reserves nothing. Reserved Reservations + // Picture answers the source revision the builder's picture of a watched branch holds, false while it holds none (design 47 + // H H-A-155, H-A-141): a step ask naming a newer one waits for it. Nil waits for nothing. + Picture func(branch string) (source int64, held bool) + // RevisionWait bounds a step ask's wait for its revision, its setting revision-wait-minutes (H-A-156); DefaultRevisionWait + // when 0. + RevisionWait time.Duration // Forms answers what each node's engine states it reads (ADR 0618 §2, ADR 0624 §1): present, the nodes whose engine // answered when last asked, and counted, every node the assignments names, present or away, a node with no statement // stating nothing (the baseline's first forms). A done key names each artifact in the forms these keep, the oldest in @@ -149,6 +155,8 @@ type Builder struct { // builds are the ends that withdraw the asks of a key's build in flight, by build key (ADR 0664 §4): a newer fingerprint of // the module takes its older one's build out, and the asker drops that build's asks. builds map[string]context.CancelCauseFunc + // moved is closed and made again at each entry the picture takes in or drops: the step asks waiting for a revision wake. + moved chan struct{} } type planned struct { @@ -178,6 +186,7 @@ func (b *Builder) init() { b.published, b.held, b.waiting = map[string]bool{}, map[string]bool{}, map[string]map[string]chan string{} b.hands, b.events = map[string]*handWatch{}, map[string]bool{} b.builds = map[string]context.CancelCauseFunc{} + b.moved = make(chan struct{}) }) } @@ -230,6 +239,7 @@ func (b *Builder) EntryGone(branch string, first bool, module string) { b.mu.Lock() delete(b.heard, branch+"\x00"+module) b.mu.Unlock() + b.pictureMoved() if first { b.Forget(module) } @@ -256,6 +266,7 @@ func (b *Builder) take(ctx context.Context, o Origin, key string, m meshcatalog. b.edges[bk] = e } b.mu.Unlock() + b.pictureMoved() if superseded != nil { // A newer fingerprint of the module: the build of the older one in flight is taken out, and its asks are withdrawn // from the build seats (ADR 0664 §4). diff --git a/modules/mesh-delivery-builder/internal/build/step.go b/modules/mesh-delivery-builder/internal/build/step.go index ebfa5729..eb40ed3a 100644 --- a/modules/mesh-delivery-builder/internal/build/step.go +++ b/modules/mesh-delivery-builder/internal/build/step.go @@ -34,6 +34,10 @@ func (b *Builder) BuildStep(ctx context.Context, run meshbuilds.StepRun) meshbui if b.StepEntry == nil { return finish(meshbuilds.StepEndedOutcomeCouldNotRun, "the builder reads no entry of the seat mesh-catalog") } + // The version is reserved before the build (H-A-155): the ask waits for the builder's picture to hold its revision. + if outcome, said := b.awaitRevision(ctx, run); outcome != "" { + return finish(outcome, said) + } var failed []string for _, e := range run.Entries { outcome, said := b.buildEntry(ctx, e) diff --git a/modules/mesh-delivery-builder/internal/build/step_test.go b/modules/mesh-delivery-builder/internal/build/step_test.go index 0cd9c551..7dbbeb1c 100644 --- a/modules/mesh-delivery-builder/internal/build/step_test.go +++ b/modules/mesh-delivery-builder/internal/build/step_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "strings" + "sync/atomic" "testing" "time" @@ -112,6 +113,79 @@ func TestABuildStepPassesAReservingBuildOnlyWithItsReservedVersion(t *testing.T) } }) } + t.Run("H-A-155: an ask waits for the picture to take its revision in, then builds with its version reserved", aStepAskWaitsForItsRevision) + t.Run("H-A-155: past the bound the ask fails naming its revision, and nothing is built", aStepAskPastItsRevisionBoundFails) +} + +// revisionRig is a rig whose builder reserves, its step entry m at fp1, its picture of main holding the source revision held. +func revisionRig(t *testing.T, held *atomic.Int64, asked chan struct{}) (*rig, *fakeReservations, meshcatalog.CatalogEntry) { + r := newRig(t, goBundle) + r.mf.byDigest[mdigest('a')] = catalogue.Manifest{Module: "m", Version: "0.1", Build: &catalogue.Build{Artifacts: []catalogue.Artifact{goBundle}}} + res := &fakeReservations{filled: true} + r.b.Reserved = res + entry := meshcatalog.CatalogEntry{Module: "m", Repository: "novox/mesh-catalog", Path: "modules/m", Commit: "7a74cee83f88e6585a5511c3cc53bdf0f5303d20", Fp: fp1, + ManifestDigest: mdigest('a'), Inputs: &meshcatalog.EntryInputs{Toolchains: CatalogToolchains(agreedToolchains)}} + r.b.StepEntry = func(context.Context, string) (meshcatalog.CatalogEntry, error) { return entry, nil } + r.b.Picture = func(branch string) (int64, bool) { + select { + case asked <- struct{}{}: + default: + } + return held.Load(), branch == "main" && held.Load() > 0 + } + return r, res, entry +} + +func aStepAskWaitsForItsRevision(t *testing.T) { + var held atomic.Int64 + held.Store(40) + asked := make(chan struct{}, 1) + r, res, entry := revisionRig(t, &held, asked) + ended := make(chan meshbuilds.StepEnded, 1) + go func() { + ended <- r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.7.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Revision: 7, Source: 41, + Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) + }() + select { + case <-asked: // the ask read the picture, which holds source revision 40 + case end := <-ended: + t.Fatalf("the ask ended before the picture held its revision: %+v, reserved from %+v", end, res.sources) + case <-time.After(5 * time.Second): + t.Fatal("the ask never read the builder's picture") + } + if _, ok := r.st.vals[r.key]; ok { + t.Fatal("the ask built before the picture held its revision") + } + held.Store(41) + r.b.Entry(context.Background(), Origin{Branch: "main", Source: 41, First: true}, entry) // the picture takes revision 41 in + var end meshbuilds.StepEnded + select { + case end = <-ended: + case <-time.After(5 * time.Second): + t.Fatal("the ask was not woken by the entry the picture took in") + } + r.b.Wait() + if end.Outcome != meshbuilds.StepEndedOutcomePassed || len(res.sources) != 1 || res.sources[0].Revision != 41 { + t.Errorf("end = %+v, reserved from %+v; want passed with one version reserved from revision 41", end, res.sources) + } +} + +func aStepAskPastItsRevisionBoundFails(t *testing.T) { + var held atomic.Int64 + held.Store(40) + r, res, _ := revisionRig(t, &held, make(chan struct{}, 1)) + r.b.RevisionWait = time.Nanosecond + end := r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.7.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Revision: 7, Source: 41, + Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) + r.b.Wait() + if _, built := r.st.vals[r.key]; end.Outcome != meshbuilds.StepEndedOutcomeFailed || !strings.Contains(end.Said, "revision 41 of main not in the builder's picture") || built || len(res.sources) != 0 { + t.Errorf("end = %+v, built %v, reserved %+v; want failed naming revision 41 of main, nothing built", end, built, res.sources) + } + // A step ask with no source (a run on request) does not wait (H-A-156). + end = r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "run.1.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) + if strings.Contains(end.Said, "not in the builder's picture") { + t.Errorf("an ask with no source waited: %+v", end) + } } // digestOf is the manifest digest a step's entry names: the rig's manifest, or one no store holds, whose plan cannot be read. From 53199f16ce3f687a8837e4b85d18ec4cd20cd57b Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 17:15:28 +0100 Subject: [PATCH 2/3] Builder: the revision wait reads the picture and its setting revision-wait-minutes (H-A-155, H-A-156) The process gives the builder its picture of each watched branch (the source revision the branch's view holds) and the bound of a step ask's wait for its revision, read from the setting revision-wait-minutes (default 2) carried as a file under the state dir like toolchain-wait-minutes. Builds: design 47 H H-A-155, H-A-156, H-A-141 --- .../cmd/mesh-delivery-builder/catalog.go | 14 ++++++++++ .../catalogholder_integration_test.go | 9 +++++-- .../cmd/mesh-delivery-builder/main.go | 8 ++++++ .../step_integration_test.go | 26 +++++++++++++++++++ .../mesh-delivery-builder/manifest_test.go | 6 ++--- modules/mesh-delivery-builder/module.json | 17 ++++++++++++ 6 files changed, 75 insertions(+), 5 deletions(-) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go index 8aaec502..1bb73b98 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go @@ -132,6 +132,20 @@ func (c *catalogHalf) Changed(ctx context.Context) func(branch, module string, e } } +// picture is build.Builder.Picture: the source revision the view of branch holds (design 47 H H-A-155), false while the branch +// is not followed or its view holds none. +func (c *catalogHalf) picture(branch string) (int64, bool) { + cat, ok := c.branches.Catalog(branch) + if !ok { + return 0, false + } + half, ok := cat.View() + if !ok { + return 0, false + } + return half.Revision.Source, true +} + // namesOf are the package names an entry's artifacts carry, in one line. func namesOf(e meshcatalog.CatalogEntry) string { var names []string diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalogholder_integration_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalogholder_integration_test.go index 8c541bac..a3cdf179 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalogholder_integration_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalogholder_integration_test.go @@ -33,8 +33,13 @@ type catalogHolder struct { mu sync.Mutex entries map[string]meshcatalog.CatalogEntry revision int64 + // source is the source revision each trunk revision is taken in from; 0 is 1. + source int64 } +// sourceLocked is the source revision of the revisions put now. The caller holds mu. +func (h *catalogHolder) sourceLocked() int64 { return max(h.source, 1) } + func newCatalogHolder(t *testing.T, ctx context.Context, js jetstream.JetStream, nc *nats.Conn) *catalogHolder { t.Helper() h := &catalogHolder{ctx: ctx, js: js, entries: map[string]meshcatalog.CatalogEntry{}} @@ -84,7 +89,7 @@ func (h *catalogHolder) whole() meshcatalog.RevisionAnswer { all = append(all, e) } at := time.Now() - return meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{Revision: h.revision, Kind: "trunk", Branch: "main", Entries: refs, Source: 1, At: &at}, Entries: all} + return meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{Revision: h.revision, Kind: "trunk", Branch: "main", Entries: refs, Source: h.sourceLocked(), At: &at}, Entries: all} } // put sets one module's entry in a new trunk revision of main and says catalog-revision. @@ -100,7 +105,7 @@ func (h *catalogHolder) putAll(t *testing.T, es ...meshcatalog.CatalogEntry) { h.entries[e.Module] = e changed = append(changed, e.Module) } - ev, _ := json.Marshal(meshcatalog.CatalogRevisionEvent{Branch: "main", Kind: "trunk", Revision: h.revision, Source: 1, Entries: int64(len(h.entries)), Changed: changed, V: 1}) + ev, _ := json.Marshal(meshcatalog.CatalogRevisionEvent{Branch: "main", Kind: "trunk", Revision: h.revision, Source: h.sourceLocked(), Entries: int64(len(h.entries)), Changed: changed, V: 1}) h.mu.Unlock() if _, err := h.js.Publish(h.ctx, meshcatalog.SubjectCatalogRevision("main"), ev); err != nil { t.Fatal(err) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go index 1435795a..716c523b 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go @@ -212,6 +212,13 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro if err != nil { return err } + // A build step ask waits for the builder's picture of its branch to hold its source revision (design 47 H H-A-155); its + // bound is its setting revision-wait-minutes (H-A-156). + revisionWait, err := numberFile(getenv("MESH_BUILDER_REVISION_WAIT_FILE"), int(build.DefaultRevisionWait/time.Minute)) + if err != nil { + return err + } + b.RevisionWait = time.Duration(revisionWait) * time.Minute waitsBucket := process.AwaitBucket(ctx, js, conn, builds.StateBucket(cred.Module, build.WaitsState), "the toolchain waits are kept in memory only, and builder_waits shows none", logger.Printf) b.Waits = newWaitsKept(waitsBucket, func(ctx context.Context, event, id string, body any) error { return emitEvent(context.WithoutCancel(ctx), js, cred, event, id, body) @@ -450,6 +457,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // fingerprint no branch carries is closed (issue 1660) once the first view of every followed branch is in (never at the start: // a holder of the seat that is down holds no other part of it). half.b = b + b.Picture = half.picture half.ready = func() { if err := b.Sweep(ctx, half.pictured()...); err != nil { errs <- err diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/step_integration_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/step_integration_test.go index 8c4a19ca..8251652b 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/step_integration_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/step_integration_test.go @@ -4,6 +4,7 @@ package main import ( "encoding/json" + "strings" "testing" "time" @@ -49,4 +50,29 @@ func TestTheRunnersBuildStepAskIsAnsweredAsStepEnded(t *testing.T) { case <-time.After(20 * time.Second): t.Fatal("no step-ended within 20 s") } + // H-A-155: an ask naming a source revision the builder's picture of main does not hold yet waits for it, said in the + // journal, and is answered once a revision taken in from it arrives. + run.Pipeline, run.Revision, run.Source = "main.2.netcheck", 2, 2 + data, _ = json.Marshal(run) + msg = nats.NewMsg(meshbuilds.AcceptSubjectStep) + msg.Data = data + msg.Header.Set("x-event-id", "ask-2") + if err := sub.PublishMsg(msg); err != nil { + t.Fatal(err) + } + b.awaitSaid(t, "main.2.netcheck: revision 2 of main not in the builder's picture yet") + b.catalog.mu.Lock() + b.catalog.source = 2 + b.catalog.mu.Unlock() + // Revision 2, taken in from source 2, moves another module: netcheck's entry is the one the picture holds already. + b.moduleFP(t, "ca-trust", "catalogue-a67f0196-ca-trust.module.json", "modules/ca-trust", nil, commit, "src2:"+strings.Repeat("4", 64), 0) + select { + case m := <-ended: + var end meshbuilds.StepEnded + if err := json.Unmarshal(m.Data, &end); err != nil || end.ID != "ask-2" || end.Outcome != meshbuilds.StepEndedOutcomePassed { + t.Fatalf("step-ended = %+v, %v; want the ask ask-2 passed once the picture held source revision 2", end, err) + } + case <-time.After(20 * time.Second): + t.Fatal("no step-ended for ask-2 within 20 s") + } } diff --git a/modules/mesh-delivery-builder/manifest_test.go b/modules/mesh-delivery-builder/manifest_test.go index 638ea6aa..c2728780 100644 --- a/modules/mesh-delivery-builder/manifest_test.go +++ b/modules/mesh-delivery-builder/manifest_test.go @@ -186,9 +186,9 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1 m.Streams[2].Name != "mesh-delivery-builder-findings" || !reflect.DeepEqual(m.Streams[2].Events, []string{"finding-written.*", "finding-cleared.*", "findings-stated.*"}) { t.Errorf("streams = %+v, want waits with wait-held.** and wait-ended.**, 7 days, 16 MiB, the builds' stream (build.ended.**, 7 days, kept last per subject: design 47 G A-181) and the findings' stream", m.Streams) } - if c, ok := m.Config["toolchain-wait-minutes"]; !ok || c.Default != float64(60) || len(m.Config) != 5 || + if c, ok := m.Config["toolchain-wait-minutes"]; !ok || c.Default != float64(60) || len(m.Config) != 6 || m.Config["revision-wait-minutes"].Default != float64(2) || m.Config["publish-pause"].Default != float64(30) || m.Config["publish-pause-max"].Default != float64(30) || m.Config["publish-tries"].Default != float64(8) { - t.Errorf("config = %+v, want toolchain-wait-minutes of 60, the publish settings 30 s, 30 min and 8 tries (design 47 G §588, §633, A-457a)", m.Config) + t.Errorf("config = %+v, want toolchain-wait-minutes of 60, revision-wait-minutes of 2 (H-A-156), the publish settings 30 s, 30 min and 8 tries (design 47 G §588, §633, A-457a)", m.Config) } // Design 47 G §349: version.numbered is no longer said; old-form-closed on the family version, apart from the builds' ends. if want := []string{"build.asked-again", "version.old-form-closed"}; !reflect.DeepEqual(m.Emits, want) { @@ -241,7 +241,7 @@ func TestManifestProcess(t *testing.T) { // 1607/AC1 t.Errorf("process = %v", p) } env, _ := p["env"].(map[string]any) - if len(env) != 6 || env["MESH_BUILDER_PUBLISH_TRIES_FILE"] != "${dir:state}/publish-tries" || env["MESH_BROKER_FILE"] != "${dir:state}/broker" || env["MESH_BUILDER_TOOLCHAIN_WAIT_FILE"] != "${dir:state}/toolchain-wait-minutes" { + if len(env) != 7 || env["MESH_BUILDER_REVISION_WAIT_FILE"] != "${dir:state}/revision-wait-minutes" || env["MESH_BUILDER_PUBLISH_TRIES_FILE"] != "${dir:state}/publish-tries" || env["MESH_BROKER_FILE"] != "${dir:state}/broker" || env["MESH_BUILDER_TOOLCHAIN_WAIT_FILE"] != "${dir:state}/toolchain-wait-minutes" { t.Errorf("env = %v, want the broker file and the path of the toolchain wait's file, never the setting itself (design 47 G §672, A-71)", p["env"]) } for k, v := range env { diff --git a/modules/mesh-delivery-builder/module.json b/modules/mesh-delivery-builder/module.json index 10a21eae..f0dbe647 100644 --- a/modules/mesh-delivery-builder/module.json +++ b/modules/mesh-delivery-builder/module.json @@ -147,6 +147,14 @@ "trusted": false, "default": 60 }, + "revision-wait-minutes": { + "type": "number", + "description": "minutes a build step ask waits for the builder's picture of its branch to hold its source revision before it fails, building nothing (design 47 H H-A-155, H-A-156).", + "minimum": 1, + "maximum": 60, + "trusted": false, + "default": 2 + }, "publish-pause": { "type": "number", "description": "seconds before a version's second publish try, doubled per try up to publish-pause-max (design 47 G A-457a).", @@ -223,6 +231,14 @@ "mode": "0600", "content": "${config:toolchain-wait-minutes}" }, + { + "id": "revision-wait", + "type": "file", + "path": "${dir:state}/revision-wait-minutes", + "owner": "mesh-delivery-builder", + "mode": "0600", + "content": "${config:revision-wait-minutes}" + }, { "id": "publish-pause", "type": "file", @@ -269,6 +285,7 @@ "env": { "MESH_BROKER_FILE": "${dir:state}/broker", "MESH_BUILDER_TOOLCHAIN_WAIT_FILE": "${dir:state}/toolchain-wait-minutes", + "MESH_BUILDER_REVISION_WAIT_FILE": "${dir:state}/revision-wait-minutes", "MESH_BUILDER_PUBLISH_PAUSE_FILE": "${dir:state}/publish-pause", "MESH_BUILDER_PUBLISH_PAUSE_MAX_FILE": "${dir:state}/publish-pause-max", "MESH_BUILDER_PUBLISH_TRIES_FILE": "${dir:state}/publish-tries", From 0cbabe98600e47e1ccfb6cf0eacfb8b07b02379a Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 17:30:56 +0100 Subject: [PATCH 3/3] Runner and builder: a run's build step ask names no source; a held revision wakes the builder's wait (H-A-162) A run on request copied its pipeline's source onto its build step ask, so the builder waited for the run's candidate fingerprints in its picture of the branch, where they never come, and failed every run past the bound. The seat mesh-builds' contract leaves a run's source absent: the runner now does. A revision the builder's view takes in whole that changes no entry now also wakes a waiting ask, told by the catalog client's new Held. Builds: design 47 H H-A-162, H-A-155 --- .../cmd/mesh-delivery-builder/main.go | 2 ++ .../internal/build/revisionwait.go | 12 +++++-- .../internal/build/step_test.go | 32 +++++++++++++++++++ .../go/clients/meshcatalogclient/branches.go | 7 ++++ .../go/clients/meshcatalogclient/catalog.go | 10 +++++- .../meshcatalogclient/catalog_view_test.go | 21 ++++++++++-- .../internal/pipeline/place.go | 8 ++++- .../internal/pipeline/run_test.go | 14 ++++++++ 8 files changed, 100 insertions(+), 6 deletions(-) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go index 716c523b..df3db166 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go @@ -35,6 +35,7 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" sweep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshartifactsweep" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-sdk/go/operatorask" "git.novox.be/novox/mesh-sdk/go/process" @@ -458,6 +459,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // a holder of the seat that is down holds no other part of it). half.b = b b.Picture = half.picture + branches.Held = func(string, meshcatalog.CatalogRevision) { b.PictureHeld() } // a revision held wakes a step ask's wait (H-A-162) half.ready = func() { if err := b.Sweep(ctx, half.pictured()...); err != nil { errs <- err diff --git a/modules/mesh-delivery-builder/internal/build/revisionwait.go b/modules/mesh-delivery-builder/internal/build/revisionwait.go index e67548a8..32c15fcf 100644 --- a/modules/mesh-delivery-builder/internal/build/revisionwait.go +++ b/modules/mesh-delivery-builder/internal/build/revisionwait.go @@ -12,7 +12,8 @@ import ( // **A build waits for its revision; a build with no version is never made** (design 47 H H-A-155, H-A-156; H-A-141): a step // ask names its branch and source revision, and the builder reserves the version before it builds. When the builder's picture // of that branch does not hold that source revision yet, or holds it but has not taken in the entry of the asked fingerprint -// from it, the ask waits for the picture to take it in, woken by each entry the picture takes in (take, EntryGone), never a +// from it, the ask waits for the picture to take it in, woken by each entry the picture takes in (take, EntryGone) and each +// revision its view holds (PictureHeld, H-A-162), never a // timer or a poll, up to RevisionWait (the setting revision-wait-minutes); past it the ask fails, naming the revision and the // branch, and nothing is built. A step ask with no source (a run on request) does not wait. @@ -26,7 +27,14 @@ func (b *Builder) revisionWait() time.Duration { return DefaultRevisionWait } -// pictureMoved wakes every step ask waiting for the picture: an entry was taken in or went. +// PictureHeld is told each revision a branch's view takes in whole, whether or not an entry changed (H-A-162): every step +// ask waiting for its revision looks again. +func (b *Builder) PictureHeld() { + b.init() + b.pictureMoved() +} + +// pictureMoved wakes every step ask waiting for the picture: an entry was taken in or went, or a revision was held. func (b *Builder) pictureMoved() { b.mu.Lock() moved := b.moved diff --git a/modules/mesh-delivery-builder/internal/build/step_test.go b/modules/mesh-delivery-builder/internal/build/step_test.go index 7dbbeb1c..920a4f74 100644 --- a/modules/mesh-delivery-builder/internal/build/step_test.go +++ b/modules/mesh-delivery-builder/internal/build/step_test.go @@ -115,6 +115,38 @@ func TestABuildStepPassesAReservingBuildOnlyWithItsReservedVersion(t *testing.T) } t.Run("H-A-155: an ask waits for the picture to take its revision in, then builds with its version reserved", aStepAskWaitsForItsRevision) t.Run("H-A-155: past the bound the ask fails naming its revision, and nothing is built", aStepAskPastItsRevisionBoundFails) + t.Run("H-A-162: a revision held that changes no entry wakes the ask", aRevisionHeldWakesTheAsk) +} + +func aRevisionHeldWakesTheAsk(t *testing.T) { + var held atomic.Int64 + held.Store(40) + asked := make(chan struct{}, 1) + r, res, entry := revisionRig(t, &held, asked) + r.b.Entry(context.Background(), Origin{Branch: "main", Source: 40, First: true}, entry) // the entry, unchanged since 40 + r.b.Wait() + ended := make(chan meshbuilds.StepEnded, 1) + go func() { + ended <- r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.8.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Revision: 8, Source: 41, + Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) + }() + select { + case <-asked: + case end := <-ended: + t.Fatalf("the ask ended before the picture held its revision: %+v", end) + case <-time.After(5 * time.Second): + t.Fatal("the ask never read the builder's picture") + } + held.Store(41) + r.b.PictureHeld() // revision 8, from source 41, changes no entry + select { + case end := <-ended: + if end.Outcome != meshbuilds.StepEndedOutcomePassed || len(res.sources) != 1 { + t.Errorf("end = %+v, reserved from %+v; want passed with its version reserved", end, res.sources) + } + case <-time.After(5 * time.Second): + t.Fatal("a revision held did not wake the ask") + } } // revisionRig is a rig whose builder reserves, its step entry m at fp1, its picture of main holding the source revision held. diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go index 7aa1ea1a..56b3b122 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go @@ -74,6 +74,8 @@ type Branches struct { holders atomic.Pointer[seatHolders] // Changed, when not nil, is told of each change to a branch's view (module, its entry, deleted), set before Wire. Changed func(branch, module string, entry json.RawMessage, deleted bool, at time.Time) + // Held, when not nil, is told of each revision a branch's view takes in whole (Catalog.Held; H-A-162), set before Wire. + Held func(branch string, revision seat.CatalogRevision) // Adopted, when not nil, is told on a goroutine of its own of each branch whose view was adopted, with that view (a part that waits for // its first branch's first view, design 47 G §507), set before Wire. Adopted func(branch string, c *Catalog) @@ -253,6 +255,11 @@ func (b *Branches) reconcile() { b.Changed(branch, module, entry, deleted, at) } } + c.Held = func(r seat.CatalogRevision) { + if b.Held != nil { + b.Held(branch, r) + } + } // A catalog-revision also asks again each entry whose ask failed in transport (design 47 H, H-A-60). hear := func(ctx context.Context, data []byte) error { err := c.Hear(ctx, data) diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go index 8e6feb62..27fe86aa 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog.go @@ -59,6 +59,9 @@ type Catalog struct { waits int64 // made is when the view's numbering last started again (H-A-141), the follower's Created; zero is catalogCreated. made time.Time + // Held, when not nil, is told each revision the view takes in whole, once it holds it, whether or not an entry + // changed (design 47 H H-A-162): a part waiting for a revision wakes on it. Set before Start. + Held func(seat.CatalogRevision) } // created is madeLocked, taking mu. @@ -232,7 +235,9 @@ func (c *Catalog) ask(_ context.Context, after string) (views.Answer, error) { } rev := a.Revision.Revision holds := false - if a.Next == "" { + whole := a.Next == "" + held := *a.Revision + if whole { c.held, c.paging = c.paging, nil if c.waits <= rev { c.waits, holds = 0, true @@ -242,6 +247,9 @@ func (c *Catalog) ask(_ context.Context, after string) (views.Answer, error) { if holds { c.Follower.Holds(ViewName(rev)) } + if whole && c.Held != nil { + c.Held(held) + } return views.Answer{V: 1, Owner: seat.Seat, State: c.Follower.State, Revision: uint64(rev), Created: c.created(), Values: values, Next: a.Next, At: time.Now()}, nil } diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go index 2b9ba612..b9baaef1 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/catalog_view_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "slices" "strings" "sync" "testing" @@ -83,6 +84,14 @@ func TestCatalogIsAViewPerWatchedBranch(t *testing.T) { c.Follower.Wait = time.Nanosecond c.Follower.Logf = logf } + // H-A-162: each revision the view takes in whole is told to Held. + var heldMu sync.Mutex + var held []int64 + main.Held = func(r seat.CatalogRevision) { + heldMu.Lock() + defer heldMu.Unlock() + held = append(held, r.Revision) + } if main.Follower.State == rel.Follower.State || main.Follower.State != "catalog-revisions.main" || rel.Follower.State != views.BranchState(seat.StateCatalogRevisions, "release/1") { t.Fatalf("views are named %q and %q", main.Follower.State, rel.Follower.State) } @@ -116,17 +125,25 @@ func TestCatalogIsAViewPerWatchedBranch(t *testing.T) { } deadline := time.Now().Add(5 * time.Second) for { - if h, _ := main.View(); h.Revision.Revision == 4 { + heldMu.Lock() + told := len(held) + heldMu.Unlock() + if h, _ := main.View(); h.Revision.Revision == 4 && told == 2 { break } if time.Now().After(deadline) { - t.Fatal("main never held revision 4") + t.Fatal("main never held revision 4, told to Held") } time.Sleep(time.Millisecond) } if n := f.asked("release/1"); n != 1 { t.Errorf("release/1 asked %d times after main's revision", n) } + heldMu.Lock() + if !slices.Equal(held, []int64{3, 4}) { + t.Errorf("Held was told %v, want 3 then 4", held) + } + heldMu.Unlock() logMu.Lock() said := false for _, l := range logged { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/place.go b/modules/mesh-delivery-pipeline/internal/pipeline/place.go index cd3b8a93..af5758d6 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/place.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/place.go @@ -98,8 +98,14 @@ func place(p seat.Pipeline, m seat.Mover, reg catalog.Registration, entries map[ build := seat.StepResult{Step: seat.StepResultStepBuild, Module: m.Module, Status: seat.StepResultStatusRequired, Fingerprint: m.Fingerprint, Seat: builds.Seat, Outcome: seat.StepResultOutcomeRunning, Started: &now, Ask: AskID(startOf(p, now), p.Pipeline, m.Module, "", seat.StepResultStepBuild)} steps = append(steps, build) + // A run on request's build step ask names no source (the seat mesh-builds' contract; H-A-162): its candidate's + // fingerprints are never in the builder's picture of the branch, so it waits for no revision there. + source := p.Source + if p.Run != "" { + source = 0 + } asks = append(asks, Asked{Seat: builds.Seat, ID: build.Ask, Commit: m.Commit, Ask: builds.StepRun{Pipeline: p.Pipeline, Step: builds.StepRunStepBuild, Branch: p.Branch, - Revision: p.Revision, Source: p.Source, Entries: []builds.BuildEntry{{Module: m.Module, Commit: m.Commit, Fingerprint: m.Fingerprint}}}}) + Revision: p.Revision, Source: source, Entries: []builds.BuildEntry{{Module: m.Module, Commit: m.Commit, Fingerprint: m.Fingerprint}}}}) for _, a := range arts { name := a.Ref if _, after, ok := strings.Cut(a.Ref, ":"); ok { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/run_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/run_test.go index c7ab5276..0b27ddcb 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/run_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/run_test.go @@ -5,6 +5,7 @@ import ( "strings" "testing" + 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" ) @@ -97,6 +98,19 @@ func TestRunOnRequest(t *testing.T) { if ids, _ := s.UnplacedRuns(); len(ids) != 0 { t.Errorf("a placed run is still unplaced: %v", ids) } + // H-A-162: a run's build step asks name no source, as the seat mesh-builds' contract says of a run on request. + builtAsks := 0 + for _, x := range asks.asks { + if r, ok := x.(builds.StepRun); ok { + builtAsks++ + if r.Source != 0 { + t.Errorf("the run's build step ask names source %d, want none", r.Source) + } + } + } + if builtAsks == 0 { + t.Errorf("the run asked no build step: %v", asks.asks) + } if n, _ := s.PlaceRun(ev, Half{Revision: 12, Entries: cand}, base); n != 0 { t.Errorf("the revision heard again placed %d", n) }