The indexer's packages state is read through its read tool's first view and its views family (ADR 0615 §1), as the other four states the builder follows, not by its bucket: the manifest drops the read, invokes indexer_read_packages and consumes views.packages. A package built unnumbered because its name was not known at its claim was numbered only at its module's next claim. Now a series key heard naming a name looks at its module again (Builder.PackageNamed), and a look at a done trunk build numbers what it left unnumbered with the done key's digests (BuildClaim.Done), so version.numbered carries the digest for the build module's late publish. Woken by the views family, never a timer. Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
530 lines
26 KiB
Go
530 lines
26 KiB
Go
// Command mesh-delivery-builder is delivery's builder (novox/hq ADR 0453 clause 4, ADR 0478, issue 1607): it builds every
|
|
// new-form fingerprint the indexer writes, dark, into its own build keys (builds.<module>.<fingerprint>), through the build
|
|
// modules' fetch and build tools. Nothing reads its keys to deliver until release R4e. It serves no request subject and
|
|
// holds no timer: every reaction is a watch or a bus event (ADR 0437 §8).
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"maps"
|
|
"os"
|
|
"os/signal"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun"
|
|
"git.novox.be/novox/mesh-delivery/sdk/forms"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/delivery"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
const (
|
|
// defaultStepBound is how long one ask's step may run once a build node took it (a fetch downloads the Go modules, a
|
|
// build compiles): from its start, never from its put, so an ask waiting in its stream for a free slot is not failed
|
|
// (buildrun.Asker.Bound; ADR 0525 §3, §6).
|
|
defaultStepBound = 30 * time.Minute
|
|
)
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
if err := run(ctx, os.Getenv, os.Stderr); err != nil {
|
|
fmt.Fprintln(os.Stderr, "mesh-delivery-builder:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// run is the program: it returns when ctx ends or the bus cannot be used.
|
|
func run(ctx context.Context, getenv func(string) string, logOut io.Writer) error {
|
|
logger := log.New(logOut, "mesh-delivery-builder: ", log.LstdFlags)
|
|
cred, err := process.ReadBrokerAccount(getenv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
conn, js, err := cred.Dial()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Close()
|
|
builds, err := process.EssentialBucket(ctx, js, "builder", delivery.BuilderBuildsBucket, "claim or write a build key")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The indexer's modules, manifests, candidates and packages and the assigner's placement are their owners' states, read by the
|
|
// first view of their read tools and then their views families, never their buckets (ADR 0615 §1, DLV-M1). An owner that
|
|
// is down never holds the start: each is read on a goroutine once its first view is answered, and until then the
|
|
// unanswered first view is the builder's finding.
|
|
calls, err := process.Connect(cred, nats.Name(cred.Node+"."+cred.Module+".calls"), nats.RetryOnFailedConnect(true))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer calls.Close()
|
|
reader := &views.ProcessReader{Module: delivery.BuilderModule, Call: calls.Call, Logf: logger.Printf}
|
|
findingsKV := process.AwaitBucket(ctx, js, conn, findingsBucket, "the builder's findings, a first view unanswered past its bound among them, are said in its log only", logger.Printf)
|
|
reader.Findings = findingsKV.KV
|
|
modules, err := reader.Follow(delivery.IndexerModule, modulesState, delivery.IndexerModule+".indexer_read_modules", indexerViews)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
manifests, err := reader.Follow(delivery.IndexerModule, manifestsState, delivery.IndexerModule+".indexer_read_manifests", indexerViews)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The candidates' fingerprints are built too (issue 1684).
|
|
candidates, err := reader.Follow(delivery.IndexerModule, candidatesState, delivery.IndexerModule+".indexer_read_candidates", indexerViews)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The indexer's packages, whose names a package is numbered under (design 32).
|
|
packages, err := reader.Follow(delivery.IndexerModule, delivery.PackagesState, delivery.IndexerModule+".indexer_read_packages", indexerViews)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
placement, err := reader.Follow(delivery.AssignerModule, delivery.PlacementState, delivery.AssignerModule+".assigner_read_placement", assignerViews)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
bound := defaultStepBound
|
|
if v := strings.TrimSpace(getenv("MESH_BUILDER_STEP_BOUND")); v != "" {
|
|
if bound, err = time.ParseDuration(v); err != nil {
|
|
return fmt.Errorf("MESH_BUILDER_STEP_BOUND: %w", err)
|
|
}
|
|
}
|
|
// An unanswered tag ask is narrated every TagAskBound (design 51: five minutes); a test gives a shorter one.
|
|
tagAskBound := versions.TagAskBound
|
|
if v := strings.TrimSpace(getenv("MESH_BUILDER_TAG_ASK_BOUND")); v != "" {
|
|
if tagAskBound, err = time.ParseDuration(v); err != nil {
|
|
return fmt.Errorf("MESH_BUILDER_TAG_ASK_BOUND: %w", err)
|
|
}
|
|
}
|
|
tools := &buildrun.ToolchainSet{}
|
|
// Each build module's part of a build is an ask on its build seat's stream, pulled by a build node with a free slot
|
|
// (novox/hq ADR 0525): the builder chooses no node, so it reads neither presence nor have and holds no capacity.
|
|
asks := buildrun.JetStreamAsks{JS: js}
|
|
// The asks the build seats gave up on (issue 734): heard from DEAD_LETTERS, each ends the wait for its step at once,
|
|
// and the lane asks its part again (Builder.waitOnce) instead of waiting for the check bound.
|
|
// Heard in the background, and again at a reconnect or an ask while it is not; whether it is heard is kept in the
|
|
// state hearing, which builder_stuck says (dead_letters).
|
|
dead := &buildrun.DeadLetters{}
|
|
// The state may not be on the bus yet when a delivery started the builder (the grants part creates it, issue 1790): it
|
|
// is bound when the bus says it was created, and the latest hearing is written then.
|
|
hearingKV := process.AwaitBucket(ctx, js, conn, delivery.BuilderModule+"_"+buildrun.HearingState, "builder_stuck cannot say whether DEAD_LETTERS is heard", logger.Printf)
|
|
hearing := buildrun.NewDeadLetterHearing(ctx, buildrun.JetStreamDeadLetters{JS: js}, delivery.AskLanguages, dead, logger.Printf,
|
|
buildrun.SayHearing(ctx, hearingKV, logger.Printf))
|
|
defer hearing.Stop()
|
|
// Each ask's step is heard by its own watch and, at every step end of its build module and every reconnect, read
|
|
// (buildrun.HearEnds): a watch that goes deaf loses no end (the lost check of 2026-10-28).
|
|
var buildModules []string
|
|
for _, l := range delivery.AskLanguages {
|
|
if m, err := delivery.BuildModuleOf(l); err == nil {
|
|
buildModules = append(buildModules, m)
|
|
}
|
|
}
|
|
heard := buildrun.HearEnds(ctx, asks, asks, buildModules, logger.Printf)
|
|
conn.SetReconnectHandler(func(*nats.Conn) { hearing.Again(); heard.Nudge("") })
|
|
// The builder asks the controller, as its own account, whether the artifact store still holds a done key's blobs
|
|
// (the seat verb held, novox/hq issue 1937) before it leaves the key done, on its calls connection.
|
|
// The builder numbers each trunk build at its claim (novox/hq design 51 *The versions*): its state versions, bound once
|
|
// the bus holds it; the floor's three sources asked as its own account; each new number said as version.numbered.
|
|
versionsKV := process.AwaitBucket(ctx, js, conn, versions.Bucket, "no trunk build is numbered: each is built unnumbered and published by its digest", logger.Printf)
|
|
found := &findings{kv: findingsKV, logf: logger.Printf, now: time.Now}
|
|
floorWaits := &versions.FloorWaits{Logf: logger.Printf, Said: found.floorSaid(context.WithoutCancel(ctx))}
|
|
// The names waiting on their floor are kept in the state floor-waits (DLV-B4): a restarted builder takes them in, so
|
|
// their silent sources' events still wake them; until the state is bound they are kept here only.
|
|
process.AwaitBucket(ctx, js, conn, versions.FloorWaitsBucket, "the names waiting on their floor are kept in memory only, and builder_floor_waits shows none", logger.Printf).
|
|
Ready(func(kv jetstream.KeyValue) {
|
|
floorWaits.Keep(kvKeeper{ctx: ctx, kv: kv}, heldFloorWaits(ctx, kv, logger.Printf))
|
|
})
|
|
// A package is numbered under its name in its ecosystem, read from the indexer's packages state (design 32).
|
|
numberer := &versions.Numberer{Versions: versions.Versions{Store: awaitedVersions{versionsKV}}, Floor: floorVia(calls.Call), Waits: floorWaits,
|
|
Names: versions.NamesIn(indexerPackages{packages}),
|
|
Said: func(nb delivery.VersionNumbered) {
|
|
if err := emitNumbered(context.WithoutCancel(ctx), js, cred, nb); err != nil {
|
|
logger.Printf("%v", err)
|
|
}
|
|
}}
|
|
b := &build.Builder{
|
|
Claimer: build.Claimer{Store: kvStore{builds}, By: cred.Node + "." + cred.Module},
|
|
Asks: buildrun.Asker{Publisher: asks, Waiter: buildrun.LostAskWaiter{Inner: heard, Dead: dead, Hearing: hearing}, Bound: bound},
|
|
Held: heldOnTheBus{js},
|
|
// An ask that can no longer be answered is asked again (issue 1945), read at the step bound: a context's bound.
|
|
Gone: heldOnTheBus{js},
|
|
CheckAfter: bound,
|
|
Toolchains: tools,
|
|
Manifests: manifestReader{manifests},
|
|
Logf: logger.Printf,
|
|
Stored: storedVia(calls.Call),
|
|
Node: cred.Node,
|
|
}
|
|
tagAsksKV := process.AwaitBucket(ctx, js, conn, versions.TagAsksBucket, "builder_tag and builder_untag are not answered", logger.Printf)
|
|
// A check's build of a head a kept tag ask names numbers its packages tagged (design 51 *Tagged versions*), and the ask
|
|
// is marked numbered with its versions.
|
|
b.Tags = tagsVia(tagAsksKV.KV, logger.Printf)
|
|
b.Numbers = build.VersionNumbers{N: numberer, TagNumbered: tagNumbered(tagAsksKV.KV, logger.Printf, time.Now)}
|
|
// An ask not answered within five minutes is narrated with what it waits on: its bound's end on the bus says when.
|
|
boundsKV := process.AwaitBucket(ctx, js, conn, versions.TagAskBoundsBucket, "an unanswered tag ask is not narrated", logger.Printf)
|
|
bounds := &tagBounds{asks: tagAsksKV.KV, bounds: boundsKV.KV, bound: tagAskBound, waits: floorWaits.Waiting, logf: logger.Printf, now: time.Now,
|
|
log: func(e delivery.BuildLogEntry) { b.Log(e) }}
|
|
// 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.
|
|
acts := versions.Acts{Numberer: numberer, Done: doneVia(b), Reconsider: b.Reconsider, Remove: removeVia(calls.Call),
|
|
BuildAgain: func(ctx context.Context, module, fp, why, by string) (string, bool) {
|
|
r := b.BuildAgain(ctx, module, fp, why, by, buildAgainBound)
|
|
if r.Refused != "" {
|
|
return r.Refused, true
|
|
}
|
|
if r.Failed != "" {
|
|
return "built again, and it failed before its asks were put: " + r.Failed, false
|
|
}
|
|
return fmt.Sprintf("built again as revision %d", r.Revision), false
|
|
}}
|
|
// A candidate's preview is merged onto the trunk head the indexer previewed it onto: source.main at the candidate's base
|
|
// revision (the gate's source state). The source's revisions are watched with their history (a direct get of one revision
|
|
// is not what a read grants): a revision heard after the candidate that names it builds that candidate then. Where it
|
|
// cannot be read the trunk's keys are built and the candidates wait (issue 1945).
|
|
errs := make(chan error, 10)
|
|
trunks := &trunkSource{ctx: ctx, js: js, heads: &sourceHeads{}, moved: func(ctx context.Context) {
|
|
// A source revision heard is the forge's merge: it wakes a floor whose bridge_tags was silent (design 51 *The floor*).
|
|
(&floorWake{b: b, waits: floorWaits, logf: logger.Printf}).wake(ctx, versions.WakeForge, "", "")
|
|
b.TrunkMoved(ctx)
|
|
}, errs: errs, logf: logger.Printf}
|
|
trunks.start()
|
|
b.Trunk = trunks.head
|
|
// The builder's log (ADR 0571 §1): each build's start and end in its module log `build-log`, read by the view, the
|
|
// indexer and the assigner through their reads, and by the operator's command nox builds through the tool builder_builds_since.
|
|
b.Log = func(e delivery.BuildLogEntry) {
|
|
if e.Node == "" { // a narration of no one build (a tag ask, the old form's window) is this instance's
|
|
e.Node = cred.Node
|
|
}
|
|
if err := appendLog(context.WithoutCancel(ctx), js, cred, e); err != nil {
|
|
logger.Printf("%v", err)
|
|
}
|
|
// A failed trunk build is the builder's finding build-failed, cleared by a later done one (DLV-B7).
|
|
found.buildEnded(context.WithoutCancel(ctx), e)
|
|
}
|
|
b.Said = func(e build.AskedAgain) {
|
|
if err := emitAskedAgain(context.WithoutCancel(ctx), js, cred, e); err != nil {
|
|
logger.Printf("%v", err)
|
|
}
|
|
}
|
|
// **Said once every build module answered a reference, taken away when stopped** (issue 1886, ADR 0522): the
|
|
// controller delivers from this builder's keys only while an instance says it writes references. A bucket not there
|
|
// yet (the manifest's state not made) is said, and the controller's plan waits for the keys, named (no build-agent
|
|
// is asked since R4g, ADR 0524 §7).
|
|
switch kv := process.OptionalBucket(ctx, js, instancesBucket, "the controller is not told that this builder writes references until it starts again", logger.Printf); {
|
|
case kv == nil:
|
|
default:
|
|
says := newSayer(ctx, kv, cred.Node, []string{buildrun.GoBuildModule, buildrun.NodeBuildModule, build.ImageBuildModule}, logger.Printf)
|
|
b.Answered = says.Answered
|
|
defer func() {
|
|
stopping, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
|
defer cancel()
|
|
if err := says.unsay(stopping); err != nil {
|
|
logger.Printf("%v", err)
|
|
}
|
|
}()
|
|
}
|
|
// The toolchains first, so a build that starts from the first modules value finds the nodes.
|
|
watch := newToolchainWatch(ctx, js, tools, b.NodesChanged, logger)
|
|
// Only the toolchain reports of nodes the catalogue places the build module on count (issue 1940, ADR 0549 §3): the
|
|
// placement is settled first, so the toolchains settle through the gate already knowing it.
|
|
gate := buildrun.NewToolchainGate([]string{buildrun.GoBuildModule, buildrun.NodeBuildModule}, tools.Put, logger.Printf)
|
|
watch.put = gate.Toolchain
|
|
// What each counted node's engine reads (ADR 0618 §2, ADR 0624 §1): asked through the node-engine seat's verb as the
|
|
// builder's own account, for every node the placement names, and kept in the state engine-reads once it is bound.
|
|
reads := newEngineReads(calls.CallOn, gate, logger.Printf)
|
|
// The old reference form's window over them (DLV-B9), kept in the state old-form once it is bound; builder_old_form
|
|
// reads it and drops a retired node by hand.
|
|
reads.ctx = context.WithoutCancel(ctx)
|
|
reads.window = &oldForm{forms: forms.Current, findings: found, logf: logger.Printf, now: time.Now, log: func(e delivery.BuildLogEntry) { b.Log(e) },
|
|
said: func(body versions.OldFormClosedBody) {
|
|
if err := emitEvent(context.WithoutCancel(ctx), js, cred, versions.OldFormClosedEvent, "old-form-closed-"+body.At.Format(time.RFC3339Nano), body); err != nil {
|
|
logger.Printf("saying %s: %v", versions.OldFormClosedEvent, err)
|
|
}
|
|
}}
|
|
acts.DropOldForm = func(ctx context.Context, node, by, why string, at time.Time) (string, error) {
|
|
answer, err := reads.window.drop(ctx, node, by, why, at)
|
|
if err == nil {
|
|
reads.stepWindow()
|
|
}
|
|
return answer, err
|
|
}
|
|
process.AwaitBucket(ctx, js, conn, versions.OldFormBucket, "the old form's window is kept in memory only, and builder_old_form shows none", logger.Printf).
|
|
Ready(func(kv jetstream.KeyValue) {
|
|
reads.window.bind(context.WithoutCancel(ctx), kv)
|
|
reads.stepWindow()
|
|
})
|
|
gate.OnPlaced = reads.placed
|
|
b.Forms = reads.built
|
|
// Until the placement's first view is answered the gate passes every report on, as before the placement is settled.
|
|
placementErrs := make(chan error, 1)
|
|
go func() {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-placement.Follower().Ready():
|
|
}
|
|
pe, err := buildrun.FollowPlacement(ctx, reader, gate, b.NodesChanged, logger.Printf)
|
|
if err != nil {
|
|
placementErrs <- err
|
|
return
|
|
}
|
|
placementErrs <- <-pe
|
|
}()
|
|
if err := watch.start(); err != nil {
|
|
return err
|
|
}
|
|
// A build module says each time it wrote its toolchains key: its stream, and the builder's durable consumer on it, are
|
|
// made by the controller. Where the stream is not there yet the builder says so and is started again with the build
|
|
// module.
|
|
for _, module := range []string{buildrun.GoBuildModule, buildrun.NodeBuildModule} {
|
|
stream := toolchainStreams[module]
|
|
cons, err := cred.EventConsumer(ctx, js, stream)
|
|
if err != nil {
|
|
logger.Printf("the consumer %s_%s on %s does not exist yet, so %s's reports are not heard until the builder starts again: %v", cred.Node, cred.Module, stream, module, err)
|
|
continue
|
|
}
|
|
cc, err := process.HearEvents(cons, func(m jetstream.Msg) { watch.Reported(m) }, logger.Printf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cc.Stop()
|
|
}
|
|
// The seat mesh-artifact-sweep says artifacts-gone when it found a key's blob gone (issue 1937, ADR 0571 §2): the
|
|
// builder's durable consumer on EVENTS, made by the grants part from the manifest's consumes. Where it is not there yet the builder says
|
|
// so; it still asks held at its start and at every module value.
|
|
switch cons, err := cred.EventConsumer(ctx, js, eventsStream); {
|
|
case err != nil:
|
|
logger.Printf("the consumer %s_%s on %s does not exist yet, so %s is not heard until the builder starts again: %v",
|
|
cred.Node, cred.Module, eventsStream, delivery.ArtifactsGoneEvent, err)
|
|
default:
|
|
// The same consumer carries what wakes a floor (design 51 *The floor*): the indexer's manifest-accepted and the
|
|
// condition keeper's condition-cleared.
|
|
floor := floorWake{b: b, waits: floorWaits, logf: logger.Printf}
|
|
// And the four registry seats' published, refused and removed (DLV-B4), which move a numbered version.
|
|
seats := seatHearer{n: numberer, b: b, logf: logger.Printf}
|
|
cc, err := process.HearEvents(cons, func(m jetstream.Msg) {
|
|
if reads.heard(m, logger.Printf) || floor.heard(ctx, m) || seats.heard(ctx, m) { // reads: the node-engine seat's manifest-reads-changed (ADR 0618 §4)
|
|
return
|
|
}
|
|
heardGone(ctx, b, m, logger.Printf)
|
|
}, logger.Printf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cc.Stop()
|
|
}
|
|
reader.Start(ctx, js, cred, process.AwaitBucket(ctx, js, conn, firstViewsBucket, "builder_first_views reads nothing and builder_first_view_again is not heard", logger.Printf))
|
|
// The modules, once their first views and the manifests' are answered: what a module builds is read from the manifests.
|
|
go func() { errs <- buildFromModules(ctx, b, modules, manifests, candidates) }()
|
|
// A package's name heard in the indexer's packages looks at its module again, so a done build is numbered then.
|
|
go func() { errs <- followPackages(ctx, packages, b, logger.Printf) }()
|
|
// A person asks a key's build again with builder_ask_again, which writes the reasks state (issue 1945): watched, as
|
|
// every other input. The builder asks again by itself without it.
|
|
// A state not on the bus yet at this start is watched once it appears (issue 1790).
|
|
process.AwaitBucket(ctx, js, conn, build.ReAsksBucket, "builder_ask_again is not heard; an ask that can no longer be answered is still asked again by itself", logger.Printf).Ready(func(kv jetstream.KeyValue) {
|
|
eachReAsk := func(e jetstream.KeyValueEntry) { reAsked(ctx, b, acts, kv, e, logger.Printf) }
|
|
if rw, err := settle(ctx, kv, eachReAsk); err != nil {
|
|
process.NotWatched(logger.Printf, build.ReAsksBucket, err, "builder_ask_again is not heard until the builder starts again")
|
|
} else {
|
|
go func() { errs <- follow(ctx, rw, eachReAsk) }()
|
|
}
|
|
})
|
|
// A tag ask or a branch's removal by hand (builder_tag, builder_untag) is written to tag-asks and answered there
|
|
// (design 51 *Tagged versions*); the checker's tag-wanted and the branch's end join it on their consumers (DLV-B6).
|
|
tagAsksKV.Ready(func(kv jetstream.KeyValue) {
|
|
each := func(e jetstream.KeyValueEntry) {
|
|
tagAsked(ctx, acts, kv, e, logger.Printf)
|
|
if !opOf(e) {
|
|
bounds.disarm(ctx, e.Key())
|
|
} else if t, err := versions.DecodeTagAsk(e.Value()); err == nil {
|
|
bounds.asked(ctx, e.Key(), t)
|
|
}
|
|
}
|
|
if tw, err := settle(ctx, kv, each); err != nil {
|
|
process.NotWatched(logger.Printf, versions.TagAsksBucket, err, "builder_tag and builder_untag are not answered until the builder starts again")
|
|
} else {
|
|
go func() { errs <- follow(ctx, tw, each) }()
|
|
}
|
|
})
|
|
boundsKV.Ready(func(kv jetstream.KeyValue) {
|
|
each := func(e jetstream.KeyValueEntry) { bounds.ended(ctx, e) }
|
|
if bw, err := settle(ctx, kv, each); err != nil {
|
|
process.NotWatched(logger.Printf, versions.TagAskBoundsBucket, err, "an unanswered tag ask is not narrated until the builder starts again")
|
|
} else {
|
|
go func() { errs <- follow(ctx, bw, each) }()
|
|
}
|
|
})
|
|
// A person reads a build seat's stream with builder_seat_queue, which writes the queues state (issue 1945 note 62): the
|
|
// builder alone may read the stream, so it answers there.
|
|
process.AwaitBucket(ctx, js, conn, build.QueuesBucket, "builder_seat_queue is not answered", logger.Printf).Ready(func(kv jetstream.KeyValue) {
|
|
eachRead := func(e jetstream.KeyValueEntry) { queueRead(ctx, heldOnTheBus{js}, kv, e, logger.Printf) }
|
|
if qw, err := settle(ctx, kv, eachRead); err != nil {
|
|
process.NotWatched(logger.Printf, build.QueuesBucket, err, "builder_seat_queue is not answered until the builder starts again")
|
|
} else {
|
|
go func() { errs <- follow(ctx, qw, eachRead) }()
|
|
}
|
|
})
|
|
// A branch's end removes its tagged versions (design 51 *Tagged versions*, DLV-B6): the forge bridge's merged, a pull's
|
|
// closed or branch-deleted on the builder's durable consumer of its stream; each forge message also wakes a floor whose
|
|
// bridge_tags was silent. The checker's tag-wanted is kept in tag-asks. A consumer not there yet is said.
|
|
ends := &branchEnds{acts: acts, findings: found, tagAsks: tagAsksKV.KV, logf: logger.Printf, now: time.Now,
|
|
wake: func(ctx context.Context) {
|
|
(&floorWake{b: b, waits: floorWaits, logf: logger.Printf}).wake(ctx, versions.WakeForge, "", "")
|
|
}}
|
|
for _, c := range []struct {
|
|
stream, what string
|
|
hear func(jetstream.Msg)
|
|
}{
|
|
{forgeStream, "a branch's end", func(m jetstream.Msg) { ends.heard(ctx, m) }},
|
|
{checkerAsksStream, "the checker's tag-wanted", func(m jetstream.Msg) {
|
|
tagWanted(ctx, tagAsksKV.KV(), m, delivery.CheckerModule, time.Now(), logger.Printf)
|
|
}},
|
|
} {
|
|
cons, err := cred.EventConsumer(ctx, js, c.stream)
|
|
if err != nil {
|
|
logger.Printf("the consumer %s_%s on %s does not exist yet, so %s is not heard until the builder starts again: %v", cred.Node, cred.Module, c.stream, c.what, err)
|
|
continue
|
|
}
|
|
cc, err := process.HearEvents(cons, c.hear, logger.Printf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cc.Stop()
|
|
}
|
|
// The state engine-reads, new with ADR 0618 (issue 1790's kind): until it is on the bus each statement is kept in memory
|
|
// only, and builder_engine_reads answers nothing.
|
|
process.AwaitBucket(ctx, js, conn, engineReadsBucket, "what each node's engine reads is kept in memory only", logger.Printf).Ready(func(kv jetstream.KeyValue) {
|
|
reads.bind(ctx, kv, errs, logger.Printf)
|
|
})
|
|
logger.Printf("started on %s", cred.Node)
|
|
select {
|
|
case <-ctx.Done():
|
|
b.Wait()
|
|
return nil
|
|
case err := <-errs:
|
|
return err
|
|
case err := <-placementErrs:
|
|
return err
|
|
case err := <-watch.errs:
|
|
return err
|
|
}
|
|
}
|
|
|
|
// The builder's own state of its first views (views.StatusState); its findings (findingsBucket) are where the condition
|
|
// keeper raises a first view unanswered past its bound (ADR 0615 §6).
|
|
var firstViewsBucket = delivery.BuilderModule + "_" + views.StatusState
|
|
|
|
// The indexer's states the builder follows (delivery.ModulesBucket, ManifestsBucket, CandidatesBucket), and the owners'
|
|
// views families as their manifests' `streams` name them.
|
|
const (
|
|
indexerViews = "indexer-views"
|
|
assignerViews = "assigner-views"
|
|
modulesState = "modules"
|
|
manifestsState = "manifests"
|
|
candidatesState = "candidates"
|
|
)
|
|
|
|
// buildFromModules builds from the indexer's modules and candidates once the first views of the modules, the manifests and
|
|
// the candidates are answered (never at the start: an indexer that is down holds no other part of it), and follows them.
|
|
func buildFromModules(ctx context.Context, b *build.Builder, modules, manifests, candidates *views.Picture) error {
|
|
for _, p := range []*views.Picture{modules, manifests, candidates} {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case <-p.Follower().Ready():
|
|
}
|
|
}
|
|
each := func(e jetstream.KeyValueEntry) {
|
|
if opOf(e) {
|
|
b.Module(ctx, e.Key(), e.Value())
|
|
} else {
|
|
b.Forget(e.Key())
|
|
}
|
|
}
|
|
w, err := settle(ctx, modules, each)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// A key left building for a fingerprint its module no longer has is closed (issue 1660); the modules are all known now.
|
|
if err := b.Sweep(ctx); err != nil {
|
|
return err
|
|
}
|
|
// The fingerprints the indexer gives the candidates' modules are built too (issue 1684), once the trunk's modules are known.
|
|
eachCandidate := func(e jetstream.KeyValueEntry) {
|
|
if opOf(e) {
|
|
b.Candidate(ctx, e.Key(), e.Value())
|
|
} else {
|
|
b.ForgetCandidate(e.Key())
|
|
}
|
|
}
|
|
cw, err := settle(ctx, candidates, eachCandidate)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
go func() { _ = follow(ctx, cw, eachCandidate) }()
|
|
return follow(ctx, w, each)
|
|
}
|
|
|
|
// sourceHeads are the trunk heads of each revision of the gate's source.main the watch delivered, the newest
|
|
// delivery.SourceHistory of them: what a candidate's base revision names.
|
|
type sourceHeads struct {
|
|
mu sync.Mutex
|
|
byRev map[uint64]map[string]string
|
|
}
|
|
|
|
// put takes one revision in and answers whether it is new.
|
|
func (s *sourceHeads) put(e jetstream.KeyValueEntry) bool {
|
|
if !opOf(e) {
|
|
return false
|
|
}
|
|
src, err := delivery.DecodeSource(e.Value())
|
|
if err != nil {
|
|
return false
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.byRev == nil {
|
|
s.byRev = map[uint64]map[string]string{}
|
|
}
|
|
if _, had := s.byRev[e.Revision()]; had {
|
|
return false
|
|
}
|
|
s.byRev[e.Revision()] = src.Heads
|
|
for len(s.byRev) > delivery.SourceHistory {
|
|
delete(s.byRev, slices.Min(slices.Collect(maps.Keys(s.byRev))))
|
|
}
|
|
return true
|
|
}
|
|
|
|
// head is the head of repository in revision rev; an error when the revision is not heard (yet, or any more).
|
|
func (s *sourceHeads) head(_ context.Context, rev uint64, repository string) (string, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
heads, ok := s.byRev[rev]
|
|
if !ok {
|
|
return "", fmt.Errorf("source revision %d is not heard (it is built once it is)", rev)
|
|
}
|
|
for r, head := range heads {
|
|
if delivery.SameRepository(r, repository) {
|
|
return head, nil
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|