Builder: read the last versions-filled at start from its kept stream, not EVENTS

The seat event's own stream is heard from its first message through the SDK's HearSeatEvents with FromFirst; nothing of EVENTS is read and no new grant is needed.

Builds: design 47 G A-411 (replaces A-408's read of EVENTS)
This commit is contained in:
2026-11-14 23:39:06 +01:00
parent c625733a33
commit 218d5cb67f
3 changed files with 49 additions and 14 deletions
@@ -3,38 +3,43 @@ package main
import (
"context"
"errors"
"sync/atomic"
"time"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/process"
)
// lastKept reads the last message kept on a subject: found false when the stream or the message is not there.
// lastKept reads whether the stream keeps a message on a subject: found false when the stream or the message is not there.
type lastKept func(ctx context.Context, subject string) (found bool, err error)
// lastKeptOf reads from the stream the events are kept on, by a direct read of the last message of the subject.
func lastKeptOf(js jetstream.JetStream, stream string) lastKept {
// lastKeptOf reads the seat event's own kept stream (the contract's StreamVersionsFilled, kept by age) the way any kept event
// is heard (design 47 G A-411, A-233): the SDK's hearing of it from its first message, whose start read hands every message
// kept to the handler; the reading is stopped once the read ended, the hub's own hearing following the durable. Nothing of
// EVENTS is read.
func lastKeptOf(js jetstream.JetStream, acct process.Account, logf func(string, ...any)) lastKept {
return func(ctx context.Context, subject string) (bool, error) {
s, err := js.Stream(ctx, stream)
var found atomic.Bool
cc, err := acct.HearSeatEvents(ctx, js, meshcatalog.StreamVersionsFilled, func(m jetstream.Msg) {
if m.Subject() == subject {
found.Store(true)
}
_ = m.Ack() // a message of the durable is taken as the hub takes it; an ordered read's has nothing to acknowledge
}, logf, process.FromFirst())
if err != nil {
if errors.Is(err, jetstream.ErrStreamNotFound) {
return false, nil
}
return false, err
}
if _, err := s.GetLastMsgForSubject(ctx, subject); err != nil {
if errors.Is(err, jetstream.ErrMsgNotFound) {
return false, nil
}
return false, err
}
return true, nil
cc.Stop()
return found.Load(), nil
}
}
// readFilled reads at the builder's start the last versions-filled the stream keeps (design 47 G §369, §372: kept with
// no age limit) and takes it as heard; the event heard later sets it as before. Nothing found leaves it unset, said; a
// readFilled reads at the builder's start the last versions-filled the stream keeps (design 47 G §369, §372, A-411) and takes it as heard; the event heard later sets it as before. Nothing found leaves it unset, said; a
// read that failed is said and tried again in the background with backoff, never holding the start.
func (rv *reservations) readFilled(ctx context.Context, read lastKept, wait func(context.Context, time.Duration) bool) {
const first, most = time.Second, time.Minute
@@ -14,6 +14,8 @@ import (
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
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/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/process"
)
// A done key whose blob the artifact store no longer holds is built again, by the builder whole against the bus
@@ -185,3 +187,31 @@ func TestArtifactsGoneLeadsTheBuilderToBuildTheKeyAgain(t *testing.T) {
b.fake.count("mesh-build-go.build_go_build"))
}
}
// The last versions-filled is read at the start from the seat event's own kept stream, kept by age, through the SDK's hearing
// from the first message (design 47 G A-411), never from EVENTS: a message the stream keeps is found, an empty one is none.
func TestTheLastVersionsFilledIsReadFromItsKeptStream(t *testing.T) {
b := startBed(t)
acct := process.Account{Node: "n1", Module: "mesh-delivery-builder"}
if err := b.js.DeleteStream(b.ctx, eventsStream); err != nil { // the bus keeps a kept event off EVENTS: its subject is nowhere else
t.Fatal(err)
}
if _, err := b.js.CreateOrUpdateStream(b.ctx, jetstream.StreamConfig{Name: meshcatalog.StreamVersionsFilled,
Subjects: []string{meshcatalog.SubjectVersionsFilled}, MaxAge: 24 * time.Hour}); err != nil {
t.Fatal(err)
}
if _, err := b.js.CreateOrUpdateConsumer(b.ctx, meshcatalog.StreamVersionsFilled, jetstream.ConsumerConfig{
Durable: acct.EventConsumerName(), AckPolicy: jetstream.AckExplicitPolicy, FilterSubject: meshcatalog.SubjectVersionsFilled}); err != nil {
t.Fatal(err)
}
read := lastKeptOf(b.js, acct, t.Logf)
if found, err := read(b.ctx, meshcatalog.SubjectVersionsFilled); err != nil || found {
t.Fatalf("empty stream: found %v, %v", found, err)
}
if _, err := b.js.Publish(b.ctx, meshcatalog.SubjectVersionsFilled, []byte(`{"v":1}`)); err != nil {
t.Fatal(err)
}
if found, err := read(b.ctx, meshcatalog.SubjectVersionsFilled); err != nil || !found {
t.Fatalf("kept message: found %v, %v", found, err)
}
}
@@ -227,7 +227,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
}
b.Reserved = reserved
// The last versions-filled the stream keeps is read at the start (design 47 G §369, §372): a restart does not lose it.
reserved.readFilled(ctx, lastKeptOf(js, eventsStream), sleeps)
reserved.readFilled(ctx, lastKeptOf(js, cred, logger.Printf), sleeps)
// A state bound later numbers the builds looked at without it.
versionsKV.Ready(func(jetstream.KeyValue) { b.Reconsider(ctx) })
// The versions' hand acts (builder_publish_again, builder_floor_again) come through reasks, as builder_build_again.