Builder: hand each event to a worker of the name it carries

The builder handled its events consumer one event at a time, each registry
published and version-released taking seconds, so a pipeline's flood of them
held every later event. Each event now goes to the worker of the name it
carries, one name's events in order and the names side by side; the events
still waiting are said in progress at each end, with no timer.

Builds: design 47 H2 H-A-514 addendum and second addendum
This commit is contained in:
2026-11-23 02:44:21 +01:00
parent b337ee639e
commit a1a8e7cc34
3 changed files with 139 additions and 1 deletions
@@ -1,12 +1,18 @@
package main
import (
"encoding/json"
"errors"
"slices"
"sync"
"time"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
stdio "git.novox.be/novox/mesh-sdk/go"
"mesh-delivery-builder/internal/versions"
)
// slowEventSetting is the setting slow-event (design 47 H2 §794; H-A-461 (2), P11): how long the handling of one heard event
@@ -53,3 +59,79 @@ func takeEvent(m jetstream.Msg, takers []eventTaker, rest eventTaker, slow time.
logf("the event %s took %s to handle, over %s: %s took it", m.Subject(), took.Round(time.Millisecond), slow, part)
}
}
// eventWorkers hands each event of the builder's durable to a worker of its key (design 47 H2 H-A-514 addenda): the name the
// event carries (a registry seat's outcome's package name, a version-released's module), one shared worker for the events that
// name none. One key's events are handled in the order heard, the keys side by side, so a slow event of one name delays no
// other name's. A worker ends when its queue is empty. The builder holds no timer (ADR 0437 §8), so the events still waiting in
// a queue are said in progress to the bus each time its worker ends a handling, and none waits out the ack wait behind another.
type eventWorkers struct {
handle func(jetstream.Msg)
keyOf func(jetstream.Msg) string
mu sync.Mutex
queues map[string]*eventQueue
}
// eventQueue is one key's events not yet handled, the one handled now first.
type eventQueue struct{ msgs []jetstream.Msg }
// take queues m on its key's worker, starting the worker when none runs.
func (w *eventWorkers) take(m jetstream.Msg) {
key := w.keyOf(m)
w.mu.Lock()
if w.queues == nil {
w.queues = map[string]*eventQueue{}
}
q, running := w.queues[key]
if !running {
q = &eventQueue{}
w.queues[key] = q
}
q.msgs = append(q.msgs, m)
w.mu.Unlock()
if !running {
go w.run(key, q)
}
}
// run handles key's events in order until its queue is empty, saying those still waiting in progress after each.
func (w *eventWorkers) run(key string, q *eventQueue) {
for {
w.mu.Lock()
if len(q.msgs) == 0 {
delete(w.queues, key)
w.mu.Unlock()
return
}
m := q.msgs[0]
w.mu.Unlock()
w.handle(m)
w.mu.Lock()
q.msgs = q.msgs[1:]
waiting := slices.Clone(q.msgs)
w.mu.Unlock()
for _, m := range waiting {
_ = m.InProgress()
}
}
}
// eventKey is the worker key of a builder's event (H-A-514 second addendum): a registry seat's outcome's name, a
// version-released's module, else "" (one worker for the events naming none).
func eventKey(m jetstream.Msg) string {
if _, ok := seatEvents[m.Subject()]; ok {
var o versions.Outcome
if json.Unmarshal(m.Data(), &o) == nil {
return "name " + o.Name
}
return ""
}
if m.Subject() == meshcatalog.SubjectVersionReleased {
var ev meshcatalog.VersionReleasedEvent
if json.Unmarshal(m.Data(), &ev) == nil {
return "module " + ev.Module
}
}
return ""
}
@@ -501,7 +501,9 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
{"the registry seats' outcomes", func(m jetstream.Msg) bool { return seatHeard.heard(ctx, m) }},
}
gone := eventTaker{"the artifacts gone", func(m jetstream.Msg) bool { heardGone(ctx, b, m, logger.Printf); return true }}
events := func(m jetstream.Msg) { takeEvent(m, takers, gone, slow, nil, logger.Printf) }
// Each event goes to a worker of the name it carries, one name's events in order, the names side by side (H-A-514 addenda).
workers := &eventWorkers{keyOf: eventKey, handle: func(m jetstream.Msg) { takeEvent(m, takers, gone, slow, nil, logger.Printf) }}
events := workers.take
// The seats' events (artifacts-gone, the registry seats' words, the build seats' toolchain-reported, the indexer's take-in.taken and
// manifest-reads-changed and the seat mesh-conditions' condition-cleared: seatEventPatterns); the owners' views events and the
// catalog half's catalog-revision and registration-changed and the gate's source-moved are the views reader's (a message has
@@ -7,6 +7,7 @@ import (
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"slices"
"strings"
"sync/atomic"
"testing"
"time"
@@ -14,6 +15,7 @@ import (
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/gopackageregistry"
"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"
stdio "git.novox.be/novox/mesh-sdk/go"
@@ -674,6 +676,58 @@ func TestAPublishedThatMarksNothingIsSaidFromWhatItRead(t *testing.T) {
})
}
// Design 47 H2 H-A-514 addenda: each event goes to the worker of the name it carries, so a slow handling of one name holds
// no other name's events, one name's events are handled in the order heard, and an event waiting in its worker's queue is
// said in progress at each tick, so the bus does not give it again.
func TestEventsAreHandledByAWorkerPerName(t *testing.T) {
pub := func(name, v string) *queuedEvent {
return &queuedEvent{wakeMsg: wakeMsg{subject: gopackageregistry.SubjectPublished, data: []byte(`{"name":"` + name + `","version":"` + v + `","digest":"sha256:aa"}`)}}
}
if got := eventKey(&pub("a", "1").wakeMsg); got != "name a" {
t.Errorf("a published's key is %q, want its name", got)
}
if got := eventKey(&wakeMsg{subject: meshcatalog.SubjectVersionReleased, data: []byte(`{"module":"m","version":"0.1.0"}`)}); got != "module m" {
t.Errorf("a version-released's key is %q, want its module", got)
}
if got := eventKey(&wakeMsg{subject: takenInSubject, data: []byte(`{}`)}); got != "" {
t.Errorf("a take-in's key is %q, want the shared worker's", got)
}
release := make(chan struct{})
handled := make(chan string, 4)
w := &eventWorkers{keyOf: eventKey, handle: func(m jetstream.Msg) {
var o versions.Outcome
_ = json.Unmarshal(m.Data(), &o)
if o.Name == "a" && o.Version == "1" {
<-release
}
handled <- o.Name + "@" + o.Version
}}
a1, a2, a3, b1 := pub("a", "1"), pub("a", "2"), pub("a", "3"), pub("b", "1")
w.take(a1)
w.take(a2)
w.take(a3)
w.take(b1)
if got := <-handled; got != "b@1" {
t.Fatalf("handled %s first, want b's event while a's first one is slow", got)
}
close(release)
if got := []string{<-handled, <-handled, <-handled}; !slices.Equal(got, []string{"a@1", "a@2", "a@3"}) {
t.Errorf("a's events handled %v, want in the order heard", got)
}
// a's worker said its waiting events in progress at each end: a2 and a3 after a1, a3 again after a2.
if a1.progressed.Load() != 0 || a2.progressed.Load() != 1 || a3.progressed.Load() != 2 {
t.Errorf("in progress said %d, %d and %d times of a's events, want 0, 1 and 2", a1.progressed.Load(), a2.progressed.Load(), a3.progressed.Load())
}
}
// queuedEvent is an event of the builder's durable that counts its in-progress.
type queuedEvent struct {
wakeMsg
progressed atomic.Int32
}
func (m *queuedEvent) InProgress() error { m.progressed.Add(1); return nil }
// oneHandler is an event hub with one handler.
type oneHandler struct{ handle func(jetstream.Msg) }