The numbering from before G goes (design 47 H §756, G §372): the builder keeps no state versions, marks no numbered entry on a registry seat's word and arms no numbered version's publish bound; the publishes' findings and the reservations still hear the same events (DQ-11), and a reserved version's bound is the reservation's (DQ-10). builder_versions keeps answering, from the reservations alone (DMC-5): what the seat mesh-builds' reserved answers for a name and, with a version and a digest, whether that version is reserved or released with that digest. Builds: design 47 H §756, G §372, G §381, H2 H-A-532 (DMC-5, DQ-10, DQ-11), spec 29, 41, 48
144 lines
6.3 KiB
Go
144 lines
6.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strconv"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
|
"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/npmpackageregistry"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// seatEvent is one registry seat's event the builder hears: the seat and the event's name.
|
|
type seatEvent struct{ seat, event string }
|
|
|
|
// seatEvents are the four registry seats' published, refused, removed and kept, by subject (design 51 *How a publish ends*,
|
|
// DLV-B4): each a seat event `<seat>.<event>` on the shared events stream, heard on the builder's durable consumer of
|
|
// it through the manifest's consumes. The subjects are the seats' contracts' (mesh-contracts).
|
|
var seatEvents = map[string]seatEvent{
|
|
containerimageregistry.SubjectPublished: {containerimageregistry.Seat, containerimageregistry.EventPublished},
|
|
containerimageregistry.SubjectRefused: {containerimageregistry.Seat, containerimageregistry.EventRefused},
|
|
containerimageregistry.SubjectRemoved: {containerimageregistry.Seat, containerimageregistry.EventRemoved},
|
|
containerimageregistry.SubjectKept: {containerimageregistry.Seat, containerimageregistry.EventKept},
|
|
ociartifactregistry.SubjectPublished: {ociartifactregistry.Seat, ociartifactregistry.EventPublished},
|
|
ociartifactregistry.SubjectRefused: {ociartifactregistry.Seat, ociartifactregistry.EventRefused},
|
|
ociartifactregistry.SubjectRemoved: {ociartifactregistry.Seat, ociartifactregistry.EventRemoved},
|
|
ociartifactregistry.SubjectKept: {ociartifactregistry.Seat, ociartifactregistry.EventKept},
|
|
gopackageregistry.SubjectPublished: {gopackageregistry.Seat, gopackageregistry.EventPublished},
|
|
gopackageregistry.SubjectRefused: {gopackageregistry.Seat, gopackageregistry.EventRefused},
|
|
gopackageregistry.SubjectRemoved: {gopackageregistry.Seat, gopackageregistry.EventRemoved},
|
|
gopackageregistry.SubjectKept: {gopackageregistry.Seat, gopackageregistry.EventKept},
|
|
npmpackageregistry.SubjectPublished: {npmpackageregistry.Seat, npmpackageregistry.EventPublished},
|
|
npmpackageregistry.SubjectRefused: {npmpackageregistry.Seat, npmpackageregistry.EventRefused},
|
|
npmpackageregistry.SubjectRemoved: {npmpackageregistry.Seat, npmpackageregistry.EventRemoved},
|
|
npmpackageregistry.SubjectKept: {npmpackageregistry.Seat, npmpackageregistry.EventKept},
|
|
}
|
|
|
|
// seatHearer takes the registry seats' outcomes in: the publishes' findings and the reservations hear them (design 47 G §369,
|
|
// A-415); the numbering from before G, which marked its state versions with them, is gone (design 47 H §756).
|
|
type seatHearer struct {
|
|
b *build.Builder
|
|
logf func(string, ...any)
|
|
// pub raises and clears the findings of a publish that ended badly; nil raises none.
|
|
pub *publishes
|
|
// res marks a reservation's artifacts published; nil marks none.
|
|
res *reservations
|
|
}
|
|
|
|
// heard takes a message of the events consumer in when it is a registry seat's outcome, and answers whether it was.
|
|
// One the state could not take is left unacknowledged (Nak), so the bus gives it again; one that is no outcome is said
|
|
// and dropped.
|
|
func (h *seatHearer) heard(ctx context.Context, m jetstream.Msg) bool {
|
|
se, ok := seatEvents[m.Subject()]
|
|
if !ok {
|
|
return false
|
|
}
|
|
var o versions.Outcome
|
|
if err := json.Unmarshal(m.Data(), &o); err != nil || o.Name == "" {
|
|
if err == nil {
|
|
err = errors.New("it names no name")
|
|
}
|
|
h.logf("%s holds what is no outcome of %s, left: %v", m.Subject(), se.seat, err)
|
|
if h.pub != nil {
|
|
h.pub.unread(ctx, se.seat, m.Subject(), err)
|
|
}
|
|
h.ack(m)
|
|
return true
|
|
}
|
|
if h.pub != nil {
|
|
h.pub.read(ctx, se.seat)
|
|
}
|
|
if h.pub != nil {
|
|
h.pub.heard(ctx, se.seat, se.event, o)
|
|
}
|
|
if h.res != nil && se.event == versions.HeardPublished {
|
|
// A pass over many reservations may outlast a third of the ack wait: the bus is told in progress before it (H-A-280).
|
|
h.res.published(ctx, o, func() {
|
|
if err := m.InProgress(); err != nil {
|
|
h.logf("in progress of %s not said: %v", m.Subject(), err)
|
|
}
|
|
})
|
|
}
|
|
if se.event == versions.HeardPublished && o.Kind == versions.KindPackage && h.b != nil {
|
|
// A package registry seat's published pins a package an entry names with none, at the version it carries (H-A-192).
|
|
h.b.PackagePublished(o.Name, o.Version)
|
|
}
|
|
h.ack(m)
|
|
return true
|
|
}
|
|
|
|
// deliveryOf is the message's delivery count as the bus says it, so a message the bus stops giving after its consumer's
|
|
// bound is readable in the log; "unknown" when it says none.
|
|
func deliveryOf(m jetstream.Msg) string {
|
|
md, err := m.Metadata()
|
|
if err != nil || md == nil {
|
|
return "unknown"
|
|
}
|
|
return strconv.FormatUint(md.NumDelivered, 10)
|
|
}
|
|
|
|
// EventDeliveries is how many times the bus gives a message of the builder's events consumer: the MaxDeliver the grants part
|
|
// composes a module's durable with (mesh-grants buscompose).
|
|
const EventDeliveries = 5
|
|
|
|
// givenUp says, on the last delivery the events consumer allows, that the builder gives the message up, naming its subject, what
|
|
// it carries and the last refusal (design 47 G A-420): past it the bus drops the message without a word.
|
|
func givenUp(m jetstream.Msg, what string, err error, logf func(string, ...any)) {
|
|
if md, merr := m.Metadata(); merr == nil && md != nil && md.NumDelivered >= EventDeliveries {
|
|
logf("gives up %s: %s, delivered %d times, the most its consumer allows; the last refusal: %v", m.Subject(), what, md.NumDelivered, err)
|
|
}
|
|
}
|
|
|
|
func (h *seatHearer) ack(m jetstream.Msg) {
|
|
if err := m.Ack(); err != nil {
|
|
h.logf("acknowledging %s: %v", m.Subject(), err)
|
|
}
|
|
}
|
|
|
|
// kvKeeper is a state of the builder as a keeper of keys: Put a key's value, Delete its key.
|
|
type kvKeeper struct {
|
|
ctx context.Context
|
|
kv jetstream.KeyValue
|
|
}
|
|
|
|
func (k kvKeeper) Put(key string, value []byte) error {
|
|
_, err := k.kv.Put(k.ctx, key, value)
|
|
return err
|
|
}
|
|
|
|
func (k kvKeeper) Delete(key string) error {
|
|
err := k.kv.Delete(k.ctx, key)
|
|
if errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|