diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits.go index e3781b5d9..115444a76 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits.go @@ -29,7 +29,10 @@ type waitBody struct { How string `json:"how,omitempty"` } -const waitStepBuilding = "building" +const ( + waitStepBuilding = "building" + waitStepBuild = "build" +) // waitsKept keeps the builder's toolchain waits in its old state waits, one key . (design 47 G §672), // and says wait-held and wait-ended on its old stream waits; at its start it says wait-held again for every key. @@ -48,7 +51,20 @@ func newWaitsKept(kv *process.AwaitedBucket, say func(context.Context, string, s } func (w *waitsKept) body(t build.ToolchainWait, how string) waitBody { - return waitBody{Step: waitStepBuilding, On: t.On(), Part: t.Package, Module: t.Module, Fingerprint: t.Fingerprint, Since: t.Since, Bound: w.bound.String(), How: how} + b := waitBody{Step: waitStepBuilding, On: t.On(), Part: t.Package, Module: t.Module, Fingerprint: t.Fingerprint, Since: t.Since, Bound: w.bound.String(), How: how} + if t.IsRevision() { // a step ask's wait for its revision: `step` build, its own bound (design 47 H2 H-A-577 (1)) + b.Step, b.Bound = waitStepBuild, t.Bound + } + return b +} + +// slotOf is the part of a wait's key and subject after `.`: a revision wait keeps its own, so it is told apart +// from that key's toolchain wait. +func slotOf(t build.ToolchainWait) string { + if t.IsRevision() { + return ".revision" + } + return "" } // start reads the state whole and says wait-held again for each key. @@ -76,7 +92,7 @@ func (w *waitsKept) start(ctx context.Context, kv jetstream.KeyValue) { } func (w *waitsKept) said(ctx context.Context, event string, t build.ToolchainWait, how string) { - if err := w.say(ctx, event+"."+t.Module+"."+t.Fingerprint, event+"-"+t.Module+"-"+t.Fingerprint+"-"+t.Since.UTC().Format(time.RFC3339Nano)+how, w.body(t, how)); err != nil { + if err := w.say(ctx, event+"."+t.Module+"."+t.Fingerprint+slotOf(t), event+"-"+t.Module+"-"+t.Fingerprint+slotOf(t)+"-"+t.Since.UTC().Format(time.RFC3339Nano)+how, w.body(t, how)); err != nil { w.logf("saying %s of %s at %s: %v", event, t.Module, t.Fingerprint, err) } } @@ -88,6 +104,7 @@ func (w *waitsKept) Hold(ctx context.Context, t build.ToolchainWait) { w.logf("a wait of %s at %s has no key: %v", t.Module, t.Fingerprint, err) return } + key += slotOf(t) w.mu.Lock() if _, had := w.held[key]; had { w.mu.Unlock() @@ -106,14 +123,24 @@ func (w *waitsKept) Hold(ctx context.Context, t build.ToolchainWait) { // End deletes the wait and says wait-ended with how; it answers whether one was held. func (w *waitsKept) End(ctx context.Context, module, fp, how string) bool { + return w.end(ctx, module, fp, "", how) +} + +// EndRevision ends the revision wait of module's fingerprint, kept under its own key. +func (w *waitsKept) EndRevision(ctx context.Context, module, fp, how string) bool { + return w.end(ctx, module, fp, ".revision", how) +} + +func (w *waitsKept) end(ctx context.Context, module, fp, slot, how string) bool { key, err := builds.BuilderBuildKey(module, fp) if err != nil { return false } + key += slot w.mu.Lock() t, had := w.held[key] // A key queued behind a package ends only when it is published, and a toolchain wait never by that (design 47 G A-192). - if had && (t.Package != "") != (how == build.WaitPublished) { + if had && slot == "" && (t.Package != "") != (how == build.WaitPublished) { had = false } if had { diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits_test.go index b21a290fb..08c055f1d 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits_test.go @@ -18,4 +18,9 @@ func TestAWaitBodyNamesItsPart(t *testing.T) { if tc := w.body(build.ToolchainWait{Module: "m", Fingerprint: "src2:f", Language: "go", Version: "go1.26.0"}, ""); tc.Part != "" || tc.On != "toolchain:go@go1.26.0" { t.Errorf("a toolchain wait says %+v", tc) } + // H2 H-A-577 (1): a revision wait says `step` build, `on` revision: and its own bound, under the subject token `.revision` + rev := build.ToolchainWait{Module: "m", Fingerprint: "src2:f", Kind: build.WaitKindRevision, Revision: 7, Bound: "2m0s"} + if rv := w.body(rev, ""); rv.Step != "build" || rv.On != "revision:7" || rv.Bound != "2m0s" || slotOf(rev) != ".revision" || slotOf(build.ToolchainWait{}) != "" { + t.Errorf("a revision wait says %+v, slot %q", rv, slotOf(rev)) + } } diff --git a/modules/mesh-delivery-builder/internal/build/revisionwait.go b/modules/mesh-delivery-builder/internal/build/revisionwait.go index 417f92c43..c3b6f3d9b 100644 --- a/modules/mesh-delivery-builder/internal/build/revisionwait.go +++ b/modules/mesh-delivery-builder/internal/build/revisionwait.go @@ -163,26 +163,71 @@ func (b *Builder) awaitRevision(ctx context.Context, run meshbuilds.StepRun) (ou wait, stop := context.WithTimeout(ctx, bound) // a context's bound, no timer (ADR 0437 §8) defer stop() told := false + var keys []string + // Each entry of the ask is a wait said on the builder's stream waits (design 47 H2 H-A-577 (1)): `wait-held` while the picture + // does not hold the revision, `wait-ended` with how it ended: ran, failed at the bound, released by builder_release. + endWaits := func(how string) { + for _, e := range run.Entries { + if b.Waits != nil { + b.Waits.EndRevision(context.WithoutCancel(ctx), e.Module, e.Fingerprint, how) + } + } + b.mu.Lock() + for _, k := range keys { + delete(b.revisionWaiting, k) + delete(b.revisionReleased, k) + } + b.mu.Unlock() + } for { b.mu.Lock() moved := b.moved + released := false + for _, k := range keys { + released = released || b.revisionReleased[k] + } 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) + endWaits(WaitRan) } return "", "" } + if released { + endWaits(WaitReleased) + b.logf("%s: %s: the wait was released by hand, the step fails, nothing is built", run.Pipeline, notHeld) + return meshbuilds.StepEndedOutcomeFailed, notHeld + ": the wait was released by hand, and a build with no version is never made" + } if !told { b.logf("%s: %s yet: the ask waits for it, at most %s", run.Pipeline, notHeld, bound) told = true + since := b.now() + for _, e := range run.Entries { + if run.Revision <= 0 { + break // an ask naming no revision has no `revision:` to say + } + k, err := builds.BuilderBuildKey(e.Module, e.Fingerprint) + if err != nil { + continue + } + keys = append(keys, k) + b.mu.Lock() + b.revisionWaiting[k] = true + b.mu.Unlock() + if b.Waits != nil { + b.Waits.Hold(ctx, ToolchainWait{Module: e.Module, Fingerprint: e.Fingerprint, Since: since, Kind: WaitKindRevision, Revision: run.Revision, Bound: bound.String()}) + } + } } select { case <-moved: case <-wait.Done(): if ctx.Err() != nil { + endWaits(WaitFailed) return meshbuilds.StepEndedOutcomeCouldNotRun, notHeld + ": the builder stopped before it was" } + endWaits(WaitFailed) 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 cb4cd5f2b..805087e4c 100644 --- a/modules/mesh-delivery-builder/internal/build/run.go +++ b/modules/mesh-delivery-builder/internal/build/run.go @@ -165,6 +165,9 @@ type Builder struct { events map[string]bool // released are the keys builder_release is building now, whatever toolchain their fingerprint names (§276, §672). released map[string]bool + // revisionWaiting are the keys whose step ask waits for its revision now, revisionReleased those builder_release ended + // (design 47 H2 H-A-577 (1)). + revisionWaiting, revisionReleased map[string]bool // 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 @@ -205,6 +208,7 @@ func (b *Builder) init() { b.hands, b.events = map[string]*handWatch{}, map[string]bool{} b.builds = map[string]context.CancelCauseFunc{} b.moved = make(chan struct{}) + b.revisionWaiting, b.revisionReleased = map[string]bool{}, map[string]bool{} }) } diff --git a/modules/mesh-delivery-builder/internal/build/step_test.go b/modules/mesh-delivery-builder/internal/build/step_test.go index 4294b85cd..6081f7558 100644 --- a/modules/mesh-delivery-builder/internal/build/step_test.go +++ b/modules/mesh-delivery-builder/internal/build/step_test.go @@ -417,6 +417,8 @@ func aStepAskWaitsForItsRevision(t *testing.T) { held.Store(40) asked := make(chan struct{}, 1) r, res, entry := revisionRig(t, &held, asked) + rw := &memWaits{} + r.b.Waits = rw var keptReads atomic.Int64 // H-A-205, H-A-222: a revision above the newest is read by number once; not answered, it is waited for r.b.KeptRevision = func(context.Context, string, int64) (int64, map[string]string, error) { keptReads.Add(1) @@ -449,6 +451,11 @@ func aStepAskWaitsForItsRevision(t *testing.T) { if end.Outcome != meshbuilds.StepEndedOutcomePassed || len(res.sources) != 1 || res.sources[0].Revision != 41 || keptReads.Load() != 1 { t.Errorf("end = %+v, reserved from %+v, kept revisions read %d; want passed with one version reserved from revision 41, read by number once", end, res.sources, keptReads.Load()) } + // H-A-577 (1): the wait is held `revision:7` while the picture lacks it and ended `ran` once it holds it + if rw.mu.Lock(); !slices.Equal(rw.ons, []string{"revision:7"}) || !slices.Equal(rw.ended, []string{"revision:ran"}) || len(rw.held) != 0 { + t.Errorf("the revision wait: held %v, ended %v, standing %v; want one revision:7 ended ran", rw.ons, rw.ended, rw.held) + } + rw.mu.Unlock() } @@ -643,12 +650,19 @@ func aStepAskPastItsRevisionBoundFails(t *testing.T) { held.Store(40) r, res, _ := revisionRig(t, &held, make(chan struct{}, 1)) r.b.RevisionWait = time.Nanosecond + rw := &memWaits{} + r.b.Waits = rw end := r.b.BuildStep(t.Context(), "a1", 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) } + // H-A-577 (1): the wait is held and ended `failed` at its bound + if rw.mu.Lock(); !slices.Equal(rw.ended, []string{"revision:failed"}) || len(rw.held) != 0 { + t.Errorf("the revision wait: ended %v, standing %v; want one ended failed", rw.ended, rw.held) + } + rw.mu.Unlock() // A step ask with no source (a run on request) does not wait (H-A-156). end = r.b.BuildStep(t.Context(), "a1", 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") { diff --git a/modules/mesh-delivery-builder/internal/build/waits.go b/modules/mesh-delivery-builder/internal/build/waits.go index e51f0b14d..53dde5899 100644 --- a/modules/mesh-delivery-builder/internal/build/waits.go +++ b/modules/mesh-delivery-builder/internal/build/waits.go @@ -29,6 +29,10 @@ const ( // WaitPublished ends the wait of a key queued behind a package, when the key is claimed (the package is published). WaitPublished = "published" WaitReleased = "released" + // WaitFailed ends a revision wait at its bound (design 47 H2 H-A-577 (1)). + WaitFailed = "failed" + // WaitKindRevision is the Kind of a revision wait. + WaitKindRevision = "revision" ) // ToolchainWait is one wait: the entry's module and fingerprint, and the toolchain it waits for. @@ -41,13 +45,24 @@ type ToolchainWait struct { // Package is the package version a key queued behind it waits for, `@` (ADR 0603 §8; design 47 G A-192): the wait is the // package's, not a toolchain's, and Language and Version are empty. Package string `json:"package,omitempty"` + // Kind is "revision" on a step ask's wait for its revision; Revision is then the module graph revision the ask names (design + // 47 H2 H-A-577 (1)); Bound is that wait's own bound, said as `bound` (the toolchain wait's is the builder's). + Kind string `json:"kind,omitempty"` + Revision int64 `json:"revision,omitempty"` + Bound string `json:"bound,omitempty"` } +// IsRevision says whether the wait is a step ask's wait for its revision, kept under `..revision`. +func (w ToolchainWait) IsRevision() bool { return w.Kind == WaitKindRevision } + // On is what the wait waits for, as the wait's `on` says it. func (w ToolchainWait) On() string { if w.Package != "" { return "package:" + w.Package } + if w.IsRevision() { + return fmt.Sprintf("revision:%d", w.Revision) + } return "toolchain:" + w.Language + "@" + w.Version } @@ -56,6 +71,9 @@ func (w ToolchainWait) On() string { type Waits interface { Hold(ctx context.Context, w ToolchainWait) End(ctx context.Context, module, fp, how string) bool + // EndRevision ends the revision wait of module's fingerprint, saying how (ran, failed or released), and answers whether + // one was held (design 47 H2 H-A-577 (1)). + EndRevision(ctx context.Context, module, fp, how string) bool } // toolchainWait is the toolchain wait of a module's fingerprint, or false when no language of its plan names a version @@ -100,7 +118,18 @@ func (b *Builder) Release(ctx context.Context, module, fp, why, by string) strin if !b.Waits.End(ctx, module, fp, WaitReleased) { b.mu.Lock() delete(b.released, key) + revision := b.revisionWaiting[key] b.mu.Unlock() + // A step ask's wait for its revision ends as its bound does (design 47 H2 H-A-577 (1)): the ask fails, since a build + // with no version is never made (H-A-155). + if revision { + b.mu.Lock() + b.revisionReleased[key] = true + b.mu.Unlock() + b.pictureMoved() + b.logf("%s: the revision wait is released by %s (%s): the ask fails, nothing is built", key, by, why) + return "" + } return fmt.Sprintf("%s holds no toolchain wait: %s builds a failed key again", key, BuildAgainTool) } b.logf("%s: released by %s (%s): built with the running build module", key, by, why) diff --git a/modules/mesh-delivery-builder/internal/build/waits_test.go b/modules/mesh-delivery-builder/internal/build/waits_test.go index 180b24a46..d1ee5795b 100644 --- a/modules/mesh-delivery-builder/internal/build/waits_test.go +++ b/modules/mesh-delivery-builder/internal/build/waits_test.go @@ -13,6 +13,7 @@ type memWaits struct { mu sync.Mutex held map[string]ToolchainWait ended []string + ons []string // what each wait held waited on, in the order held } func (w *memWaits) Hold(_ context.Context, t ToolchainWait) { @@ -21,7 +22,20 @@ func (w *memWaits) Hold(_ context.Context, t ToolchainWait) { if w.held == nil { w.held = map[string]ToolchainWait{} } - w.held[t.Module+"."+t.Fingerprint] = t + w.ons = append(w.ons, t.On()) + w.held[t.Module+"."+t.Fingerprint+map[bool]string{true: ".revision"}[t.IsRevision()]] = t +} + +func (w *memWaits) EndRevision(_ context.Context, module, fp, how string) bool { + w.mu.Lock() + defer w.mu.Unlock() + k := module + "." + fp + ".revision" + if _, ok := w.held[k]; !ok { + return false + } + delete(w.held, k) + w.ended = append(w.ended, "revision:"+how) + return true } func (w *memWaits) End(_ context.Context, module, fp, how string) bool {