From 8a1605d2832f7565c7761ab35d0670322067ca18 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 23:05:51 +0100 Subject: [PATCH] assigner: a package-published wakes only the runs waiting for its artifact's package (issue 1984, review round 1) Every package-published heard woke every run waiting for any package, each asking mesh-builds' published again. Now the wake is kept per the artifact of a package's publisher: the assigner's consumer reads the heard event's artifact (hearPublished) and wakes only the runs whose `waits` names the package that artifact publishes, as the runner's hearPublished matches by artifact; a publish of another package wakes none and asks no published. An event that does not read is journaled and wakes none. Each bind of the consumer still wakes every waiting run (heardBind). Test: TestTheCompositionStepRunsOneAskPerMachine, the wait's row: a package-published of y:go, handed to the consumer's handler while the runs wait for x/go, leaves their wake standing and asks no published; x:go's own then wakes them (failed first with the wake ignoring the artifact). Builds: design 47 H2 H-A-630 (1) (hq 1cdfa6ff); H-A-628 (3) (hq cea6404c); H-A-326 --- .../mesh-delivery-assigner/compositionrun.go | 10 ++- .../compositionrun_test.go | 43 ++++++++++- .../mesh-delivery-assigner/compositionruns.go | 72 ++++++++++++++----- .../cmd/mesh-delivery-assigner/main.go | 13 ++-- 4 files changed, 111 insertions(+), 27 deletions(-) diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go index 75debacea..4e7827fc9 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun.go @@ -836,7 +836,14 @@ func (r *compositionRun) filledPins(ctx context.Context, what string, pins map[s func (r *compositionRun) awaitPins(ctx context.Context, key, ask, what string, pins map[string]string, publishers map[string]packagePublisher) (map[string]string, string, bool) { armed, said := false, "" for { - published := r.runs.publishWake() // taken before the read, so a publish heard meanwhile wakes the next one + // The wake of each empty pin's publisher, taken before the read, so a publish heard meanwhile wakes the next one; the run + // waits on the one of the package its `waits` names alone (H-A-630 (1)). + wakes := map[string]<-chan struct{}{} + for path, v := range pins { + if by, ok := publishers[path]; ok && v == "" { + wakes[path] = r.runs.publishWake(by.artifact) + } + } filled, waiting, err := r.filledPins(ctx, what, pins, publishers) if err != nil { return nil, err.Error(), false @@ -851,6 +858,7 @@ func (r *compositionRun) awaitPins(ctx context.Context, key, ask, what string, p return filled, "", true } path := waiting[0].path + published := wakes[path] since, held, err := r.runs.waitFor(key, ask, path) if err != nil { return nil, "its wait for package " + path + " is not written: " + err.Error(), false diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go index 2a907854d..41a5a5170 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionrun_test.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "git.novox.be/novox/mesh-sdk/go/process" + "github.com/nats-io/nats.go/jetstream" "maps" "slices" "strings" @@ -100,6 +101,29 @@ func (l *logLines) await(t *testing.T, words string, n int) { } } +// eventsOf is the assigner's hub as a test hands it events: each handler by its subject, each bind's hearer. +type eventsOf struct { + handle map[string]func(jetstream.Msg) + bound []func(time.Time) +} + +func (h *eventsOf) Handle(subject string, f func(jetstream.Msg)) error { + h.handle[subject] = f + return nil +} +func (h *eventsOf) OnBound(f func(time.Time)) { h.bound = append(h.bound, f) } + +// say hands the handler of subject the event ev, and fails the test when it is not acknowledged. +func (h *eventsOf) say(t *testing.T, subject string, ev any) { + t.Helper() + raw, _ := json.Marshal(ev) + m := &heardMsg{data: raw, subject: subject} + h.handle[subject](m) + if !m.acked { + t.Errorf("the event %s on %s is not acknowledged", raw, subject) + } +} + func newRunStand(t *testing.T, revisionEngine, trunkEngine, seatDigest string) *runStand { s := &runStand{asks: map[string]buildstep.StepAsk{}, requests: map[string]composer.Request{}, publishedAt: map[string]string{"src2:x1": "0.1.9"}} engine := func(fp, commit string) meshcatalog.CatalogEntry { @@ -326,10 +350,27 @@ func TestTheCompositionStepRunsOneAskPerMachine(t *testing.T) { u.runs.expired(runBoundPrefix + key) } } else { + // H-A-630 (1): a package-published of another package wakes no run waiting for x/go, so it asks no published; x's own + // wakes each. + hub := &eventsOf{handle: map[string]func(jetstream.Msg){}} + if err := hearPublished(hub, u.runs, t.Logf); err != nil { + t.Fatal(err) + } u.mu.Lock() + asked := len(u.published) + u.mu.Unlock() + hub.say(t, builds.SubjectPackagePublished, builds.PackagePublished{Module: "y", Artifact: "y:go", Branch: "main", Source: 7, Version: "0.2.0", V: 1}) + u.runs.mu.Lock() + _, open := u.runs.published["x:go"] + u.runs.mu.Unlock() + u.mu.Lock() + after := len(u.published) u.publishedAt = map[string]string{"src2:x1": "0.1.9"} u.mu.Unlock() - u.runs.heardPublished() + if !open || after != asked { + t.Errorf("a publish of y:go woke the runs waiting for x/go (their wake standing %v) or asked published %d times more", open, after-asked) + } + hub.say(t, builds.SubjectPackagePublished, builds.PackagePublished{Module: "x", Artifact: "x:go", Branch: "main", Source: 7, Version: "0.1.9", V: 1}) } var out stepOutcome select { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go index 9aab4f502..24ac30c72 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/compositionruns.go @@ -15,6 +15,7 @@ import ( "github.com/nats-io/nats.go/jetstream" + meshbuilds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-sdk/go/keys" @@ -274,9 +275,10 @@ type compositionRuns struct { mu sync.Mutex // changed is closed and replaced at each change of the state heard (heard): a step waiting on a run reads its key again. changed chan struct{} - // published is closed and replaced at each package-published of the seat mesh-builds heard (heardPublished): a run waiting for - // a package's publish asks published again (design 47 H2 H-A-628 (3), H-A-326). - published chan struct{} + // published holds, by the artifact of a package's publisher (:), what is closed and deleted at the seat + // mesh-builds' package-published of that artifact heard (heardPublished), and at each bind of the consumer: a run waiting for + // the package that artifact publishes asks published again (design 47 H2 H-A-628 (3), H-A-630 (1); H-A-326). + published map[string]chan struct{} // seen are the keys the state's watch handed and has not seen deleted: a listing of the bucket misses a key that is // being written at that moment, so every lookup takes these beside it (keys). seen map[string]bool @@ -290,24 +292,60 @@ type compositionRuns struct { } func newCompositionRuns(kv runsKV, set runSettings, now func() time.Time, logf func(string, ...any)) *compositionRuns { - return &compositionRuns{kv: kv, set: set, now: now, logf: logf, changed: make(chan struct{}), published: make(chan struct{})} + return &compositionRuns{kv: kv, set: set, now: now, logf: logf, changed: make(chan struct{}), published: map[string]chan struct{}{}} } -// heardPublished wakes every run waiting for a package's publish, at the seat mesh-builds' package-published heard and at each -// bind of the assigner's consumer (an event said before it was made is not delivered): each reads published again, the event -// only its second path (design 47 H2 H-A-628 (3); H-A-326, H-A-328). -func (r *compositionRuns) heardPublished() { - r.mu.Lock() - close(r.published) - r.published = make(chan struct{}) - r.mu.Unlock() -} - -// publishWake is closed at the next package-published heard. -func (r *compositionRuns) publishWake() <-chan struct{} { +// heardPublished wakes the runs waiting for the package artifact publishes, at the seat mesh-builds' package-published of that +// artifact heard: each reads published again, the event only its second path; a publish of another package wakes none, so it asks +// no published (design 47 H2 H-A-628 (3), H-A-630 (1); H-A-326, H-A-328). +func (r *compositionRuns) heardPublished(artifact string) { r.mu.Lock() defer r.mu.Unlock() - return r.published + if w, ok := r.published[artifact]; ok { + close(w) + delete(r.published, artifact) + } +} + +// heardBind wakes every run waiting for a package's publish, at each bind of the assigner's consumer of package-published, which +// hears no event said before it was made: each reads published again. +func (r *compositionRuns) heardBind() { + r.mu.Lock() + defer r.mu.Unlock() + for artifact, w := range r.published { + close(w) + delete(r.published, artifact) + } +} + +// publishWake is closed at the next package-published of artifact heard, or the consumer's next bind. +func (r *compositionRuns) publishWake(artifact string) <-chan struct{} { + r.mu.Lock() + defer r.mu.Unlock() + w, ok := r.published[artifact] + if !ok { + w = make(chan struct{}) + r.published[artifact] = w + } + return w +} + +// hearPublished hears the seat mesh-builds' package-published on the assigner's durable consumer: the runs waiting for the +// package its artifact publishes are woken (heardPublished, design 47 H2 H-A-630 (1)); one that does not read wakes none and is +// journaled. +func hearPublished(hub interface { + Handle(string, func(jetstream.Msg)) error +}, r *compositionRuns, logf func(string, ...any)) error { + return hub.Handle(meshbuilds.SubjectPackagePublished, func(m jetstream.Msg) { + ev, _, err := meshbuilds.DecodePackagePublishedEvent(m.Data()) + if err != nil || ev.Artifact == "" { + logf("%s holds what is no package-published naming its artifact, so it wakes no composition run: %v", m.Subject(), err) + _ = m.Ack() + return + } + r.heardPublished(ev.Artifact) + _ = m.Ack() + }) } // waitFor writes on the run of ask the package it waits for, `waits` (design 47 H2 H-A-629 (1)), by compare-and-set: the time diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go index d291c641e..d656ec854 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go @@ -10,7 +10,6 @@ import ( "context" "fmt" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" - meshbuilds "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/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" @@ -517,15 +516,13 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, if err := hearRunBounds(hub, runs, logger.Printf); err != nil { logger.Printf("the build seats' ask-taken and step-progress are not heard, so a composition run is ended by no bound: %v", err) } - // The seat mesh-builds' package-published wakes a run waiting for a package's publish, which reads published again; so does - // each bind of the consumer, which hears no event said before it was made (design 47 H2 H-A-628 (3); H-A-326, H-A-328). - if err := hub.Handle(meshbuilds.SubjectPackagePublished, func(m jetstream.Msg) { - runs.heardPublished() - _ = m.Ack() - }); err != nil { + // The seat mesh-builds' package-published wakes the runs waiting for the package its artifact publishes, which read published + // again; each bind of the consumer, which hears no event said before it was made, wakes every waiting run (design 47 H2 + // H-A-628 (3), H-A-630 (1); H-A-326, H-A-328). + if err := hearPublished(hub, runs, logger.Printf); err != nil { logger.Printf("the seat mesh-builds' package-published is not heard, so a composition run waiting for a package reads published only at its start: %v", err) } - hub.OnBound(func(time.Time) { runs.heardPublished() }) + hub.OnBound(func(time.Time) { runs.heardBind() }) } if inputs != nil { var runEnded func(buildstep.StepEnded) error