Files
mesh-catalog/modules/distribution/cmd/distribution/main.go
T
jschoubben 0be81ae036 Join 11: the merged heads compile where no area's fix is owed
The join's importers name the delivery repository's moved contracts and sdk
(MV1), node-tools' tests use mesh-sdk's gate, the duplicate node-engine claim
rule of the reader release gives way to G's, and the assigner names its first
watched branch, never a constant. The seat holders' AnswerSeat callers are
area 6's.

Builds: design 47 G join 11 (merge of fix round 11)

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 10:41:44 +01:00

160 lines
5.9 KiB
Go

// Command distribution is distribution's program (novox/hq design 51): it serves the artifact store's port in front of
// the registry container, which listens on its node's loopback only. Reads (GET, HEAD) go through to every node's
// container runtime; a write is refused but while the direct-write window is open, never over a tag the seat holds
// published (holder.Front). The windows are distribution's state windows:
// the program moves them (windows.go) and follows the direct-write window's record on each change; it holds no timer.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"time"
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-sdk/go/process"
"git.novox.be/novox/mesh-sdk/go/views"
"github.com/nats-io/nats.go/jetstream"
"distribution/internal/holder"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := run(ctx, os.Getenv); err != nil {
fmt.Fprintln(os.Stderr, "distribution:", err)
os.Exit(1)
}
}
func run(ctx context.Context, getenv func(string) string) error {
logger := log.New(os.Stderr, "distribution: ", log.LstdFlags)
store, err := url.Parse(or(getenv("MESH_REGISTRY_LOOPBACK"), "http://127.0.0.1:5001"))
if err != nil {
return err
}
oci := &holder.OCI{Base: store}
var open atomic.Bool
open.Store(true) // until the window's state says it closed: no wider than today's store
cred, err := process.ReadBrokerAccount(getenv)
if err != nil {
return err
}
conn, js, err := cred.Dial()
if err != nil {
return err
}
defer conn.Close()
// The direct-write window's record, written by the tools bundle on distribution's own account.
windows := process.OptionalBucket(ctx, js, "distribution_windows", "the direct-write window is read as open until the program starts again", logger.Printf)
if windows != nil {
go follow(ctx, windows, &open, logger)
calls, err := process.Connect(cred)
if err != nil {
return err
}
defer calls.Close()
// The program's findings go through the module's writer (design 47 G §689); it owns the driver's kinds, the tools
// bundle's writer the transfers'.
findings := &holder.Findings{Logf: logger.Printf}
w, err := views.NewHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
Asked: meshconditions.SubjectFindingsAsked, Module: holder.Self, Node: cred.Node, State: calls.State("findings"), Kinds: holder.DriverFindingKinds,
JS: js, Conn: conn, Logf: logger.Printf})
if err != nil {
return err
}
findings.Writer = w
// the keys of the older form end in no kind the writer owns, so it does not clear them (A-31)
if err := holder.DeleteOldFindings(calls.State("findings"), holder.Self, holder.DriverFindingKinds); err != nil {
logger.Printf("the old findings are not deleted: %v", err)
}
if err := w.Start(ctx); err != nil {
return err
}
defer w.Close()
// Which modules are the build modules is the catalog half's (design 47 G §610, A-156): the branch follower of the seat
// mesh-catalog's client on a views reader that shares the program's one events hub. The driver wakes again when a
// build module's entry comes, changes or goes, so the start's wake, made before the first view, is not the last.
hub := process.NewEventHub(logger.Printf)
reader := &views.ProcessReader{Module: holder.Self, Call: calls.Call, Logf: logger.Printf, Hub: hub}
branches, err := meshcatalogclient.FollowBranchesProcess(reader, calls.Call)
if err != nil {
return err
}
branches.Logf = logger.Printf
catalogMoved := make(chan struct{}, 1)
branches.Changed = func(_, module string, entry json.RawMessage, deleted bool, _ time.Time) {
if !deleted && !claimsBuildSeat(module, entry) {
return
}
select {
case catalogMoved <- struct{}{}:
default:
}
}
reader.Start(ctx, js, cred, nil)
go drive(ctx, js, cred, hub, catalogMoved, &holder.Driver{Delivery: busDelivery{calls}, State: kvWindows{windows},
BuildModules: branchesBuilds{branches}.Modules, Closed: sayClosed(js, cred), Finding: findings.Say}, logger)
}
front := &holder.Front{Store: store, Window: &open, Logf: logger.Printf,
Published: func(name, tag string) (bool, error) {
d, err := oci.Digest(name, tag)
return d != "", err
}}
srv := &http.Server{Addr: or(getenv("MESH_REGISTRY_LISTEN"), ":5100"), Handler: front, ReadHeaderTimeout: 30 * time.Second}
errs := make(chan error, 1)
go func() { errs <- srv.ListenAndServe() }()
logger.Printf("serving the store's port %s in front of %s", srv.Addr, store)
select {
case <-ctx.Done():
shut, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
defer cancel()
return srv.Shutdown(shut)
case err := <-errs:
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}
}
// follow keeps open as the direct-write window's record says, on every change of it (a watch, no timer).
func follow(ctx context.Context, kv jetstream.KeyValue, open *atomic.Bool, logger *log.Logger) {
w, err := kv.Watch(ctx, holder.DirectWritesWindow)
if err != nil {
logger.Printf("the direct-write window cannot be watched (%v): read as open", err)
return
}
for e := range w.Updates() {
if e == nil || e.Operation() != jetstream.KeyValuePut {
continue
}
var win holder.Window
if json.Unmarshal(e.Value(), &win) != nil {
continue
}
closed := !win.Open && !win.Closed.IsZero()
if closed && open.Swap(false) {
logger.Printf("the direct-write window is closed: every write but distribution's own is refused, naming publish")
}
}
}
func or(v, d string) string {
if strings.TrimSpace(v) == "" {
return d
}
return v
}