The builder leaves an event it cannot take for the bus to give again, and the bus drops it after the fifth delivery its durable allows without a word: the 0.1.6 published was lost so. On that last delivery the builder now says it gives the message up, naming its subject, what it carries (a registry outcome's name and version, a version-released's module and version) and the last refusal. Builds: design 47 G A-420
148 lines
5.4 KiB
Go
148 lines
5.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync/atomic"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
|
"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/meshcatalog"
|
|
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// The builder's publish again (design 47 G §588, A-73, A-76): the wiring of versions.Republisher to the bus.
|
|
|
|
// awaitedQueue is the state publish-queue as versions.QueueStore: bound once the bus holds it; its bytes are the cap's.
|
|
type awaitedQueue struct{ awaitedReserved }
|
|
|
|
func (s awaitedQueue) Bytes(ctx context.Context) (uint64, error) {
|
|
kv, err := s.kv()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
st, err := kv.Status(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return st.Bytes(), nil
|
|
}
|
|
|
|
// entryVia reads a catalog graph entry by its fingerprint through the seat mesh-catalog's verb entry.
|
|
func entryVia(call toolCaller) func(ctx context.Context, fingerprint string) (meshcatalog.CatalogEntry, error) {
|
|
return func(_ context.Context, fingerprint string) (meshcatalog.CatalogEntry, error) {
|
|
raw, err := call("seat:"+meshcatalog.Seat+"."+meshcatalog.VerbEntry, meshcatalog.EntryInput{Fingerprint: fingerprint})
|
|
if err != nil {
|
|
return meshcatalog.CatalogEntry{}, err
|
|
}
|
|
var a meshcatalog.EntryAnswer
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return meshcatalog.CatalogEntry{}, fmt.Errorf("the catalog answered what is no entry: %w", err)
|
|
}
|
|
return a.Entry, nil
|
|
}
|
|
}
|
|
|
|
// sendVia sends a publish to the registry seat with the digest and no parts (A-73): the holder takes the bytes from the
|
|
// artifact store, addressed by digest. The four seats' publish take one wire form; an answer that lacks parts, or a fault of
|
|
// the call, is the holder's refusal, said in its words (not stored when the holder lacks the bytes).
|
|
func sendVia(call toolCaller) func(ctx context.Context, p versions.Publish) error {
|
|
return func(_ context.Context, p versions.Publish) error {
|
|
raw, err := call("seat:"+p.Seat+"."+versions.VerbPublish, publishBody(p))
|
|
if err != nil {
|
|
return fmt.Errorf("%s's publish of %s %s: %w", p.Seat, p.Name, p.Version, err)
|
|
}
|
|
var a gopackageregistry.PublishAnswer
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return fmt.Errorf("%s answered what is no publish answer: %w", p.Seat, err)
|
|
}
|
|
if len(a.Lacks) > 0 {
|
|
return fmt.Errorf("%s%s holds %d of the parts of %s %s", versions.NotStoredWords, p.Seat, len(a.Lacks), p.Name, p.Version)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// publishBody is the seat's publish input for p: the digest, its kind and name and version, no parts.
|
|
func publishBody(p versions.Publish) gopackageregistry.PublishInput {
|
|
return gopackageregistry.PublishInput{Commit: p.Commit, Digest: p.Digest, Fingerprint: p.Fingerprint, Kind: p.Kind, Name: p.Name, Transfer: p.Transfer, Version: p.Version}
|
|
}
|
|
|
|
// republishMaxOf reads the setting republish-max-versions from the file the manifest declares (design 47 G §633, A-71's
|
|
// form): no file or an empty one is the default; a content that is no positive number is an error naming the file.
|
|
func republishMaxOf(path string) (int, error) {
|
|
if path == "" {
|
|
return versions.DefaultRepublishMax, nil
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return versions.DefaultRepublishMax, nil
|
|
}
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
v := strings.TrimSpace(string(raw))
|
|
if v == "" {
|
|
return versions.DefaultRepublishMax, nil
|
|
}
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil || n < 1 {
|
|
return 0, fmt.Errorf("%s holds %q, which is not a number of versions", path, v)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// republishing starts the publish again once both its states are bound: the queue is taken up again and the last running
|
|
// of every node's module is read (§556), acted on where its sequence is newer than the kept one.
|
|
type republishing struct {
|
|
rp *versions.Republisher
|
|
ready atomic.Int32
|
|
}
|
|
|
|
func (g *republishing) bound(ctx context.Context, js jetstream.JetStream, logf func(string, ...any)) func(jetstream.KeyValue) {
|
|
return func(jetstream.KeyValue) {
|
|
if g.ready.Add(1) != 2 {
|
|
return
|
|
}
|
|
if err := g.rp.Resume(ctx); err != nil {
|
|
logf("the queued publishes are not taken up again: %v", err)
|
|
}
|
|
err := lastPerSubject(ctx, js, nodeengine.StreamRunning, nodeengine.SubjectRunningAll, func(subject string, data []byte, seq uint64) {
|
|
if _, err := g.rp.Heard(ctx, subject, data, seq); err != nil {
|
|
logf("the running %s is not acted on at the start: %v", subject, err)
|
|
}
|
|
})
|
|
if err != nil {
|
|
logf("the running of the node-engine seat is not read for the publish again at the start: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// heard hands a message of the events consumer to the publish again: one of its subjects whose state cannot take it is left
|
|
// for the bus to give again (Nak); every message goes on to the other parts.
|
|
func (g *republishing) heard(ctx context.Context, m jetstream.Msg, logf func(string, ...any)) (stop bool) {
|
|
meta, err := m.Metadata()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
handled, err := g.rp.Heard(ctx, m.Subject(), m.Data(), meta.Sequence.Stream)
|
|
if handled && err != nil {
|
|
logf("%v; heard again", err)
|
|
if nakErr := m.Nak(); nakErr != nil {
|
|
logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
|
|
}
|
|
givenUp(m, "its event", err, logf)
|
|
return true
|
|
}
|
|
return false
|
|
}
|