180 lines
6.0 KiB
Go
180 lines
6.0 KiB
Go
package versions
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
|
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
|
)
|
|
|
|
// DefaultRepublishMaxVersions is the setting republish-max-versions' default: the most versions one event queues (design 47 G §633).
|
|
const DefaultRepublishMaxVersions = 50
|
|
|
|
// Againer puts the publish of every reserved version again on the events of ADR 0674 §2 (design 47 H2 H-A-606, G §588): the node-engine
|
|
// seat's `running` of a module whose catalog entry claims a registry seat, and its `manifest-reads-changed` with `why` `returned` of a
|
|
// node a registry seat's holder runs on. Each message is acted on once, by its stream sequence (state `acted`, key `publish:<subject>`),
|
|
// and queues at most MaxVersions versions per event, newest first, at that holder only; what the work queue holds already is not queued
|
|
// again (ADR 0674 §3).
|
|
type Againer struct {
|
|
Republisher *Republisher
|
|
// Acted is the state acted, written with the lifetime of acted-days.
|
|
Acted ReserveStore
|
|
// Nodes answers each holder of a registry seat with its node, from the seat-holders view (seatlist.HolderNodesIn); nil answers none.
|
|
Nodes func(seat string) []seatlist.HolderNode
|
|
// MaxVersions is the setting republish-max-versions; zero is DefaultRepublishMaxVersions.
|
|
MaxVersions int
|
|
}
|
|
|
|
func (a *Againer) logf(f string, args ...any) { a.Republisher.logf(f, args...) }
|
|
|
|
func againKey(subject string) string { return ActedKey("publish:" + subject) }
|
|
|
|
// Heard takes one message of the node-engine seat's running or manifest-reads-changed in, at stream sequence seq, and acts on it once:
|
|
// handled is false for a subject that is neither. An error leaves it not acted on, so it is read again.
|
|
func (a *Againer) Heard(ctx context.Context, subject string, data []byte, seq uint64) (handled bool, err error) {
|
|
running := strings.HasPrefix(subject, strings.TrimSuffix(nodeengine.SubjectRunningAll, "*.*"))
|
|
changed := strings.HasPrefix(subject, "mesh.seat.node-engine.event."+nodeengine.EventManifestReadsChanged+".")
|
|
if !running && !changed {
|
|
return false, nil
|
|
}
|
|
key := againKey(subject)
|
|
raw, rev, found, err := a.Acted.Get(ctx, key)
|
|
if err != nil {
|
|
return true, fmt.Errorf("the acted sequence of %s cannot be read: %w", subject, err)
|
|
}
|
|
if found {
|
|
var done Acted
|
|
if json.Unmarshal(raw, &done) == nil && done.Sequence >= seq {
|
|
return true, nil
|
|
}
|
|
}
|
|
var holders func(seat string) []string
|
|
if running {
|
|
holders = a.runningHolders(ctx, data)
|
|
} else {
|
|
holders = a.returnedHolders(data)
|
|
}
|
|
if holders != nil {
|
|
if err := a.again(ctx, holders, seq); err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
val, _ := json.Marshal(Acted{Sequence: seq})
|
|
if found {
|
|
_, err = a.Acted.Update(ctx, key, val, rev)
|
|
} else {
|
|
_, err = a.Acted.Create(ctx, key, val)
|
|
}
|
|
if err != nil && !errors.Is(err, ErrConflict) {
|
|
return true, fmt.Errorf("the acted sequence of %s cannot be kept: %w", subject, err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// runningHolders is the holder function of a running: the module that is running holds each registry seat its catalog entry claims.
|
|
// nil when it holds none, or when no version waits (no call is made then).
|
|
func (a *Againer) runningHolders(ctx context.Context, data []byte) func(seat string) []string {
|
|
var ev nodeengine.Running
|
|
if err := json.Unmarshal(data, &ev); err != nil || ev.Module == "" {
|
|
a.logf("a running that does not read is not acted on: %v", err)
|
|
return nil
|
|
}
|
|
if ev.Stopped || ev.Fingerprint == "" {
|
|
return nil
|
|
}
|
|
if vs, err := a.unreleased(ctx); err != nil || len(vs) == 0 {
|
|
return nil
|
|
}
|
|
e, err := a.Republisher.Entry(ctx, ev.Fingerprint)
|
|
if err != nil {
|
|
a.logf("the catalog entry %s of %s cannot be read, so no publish is put again for it: %v", ev.Fingerprint, ev.Module, err)
|
|
return nil
|
|
}
|
|
var claimed []string
|
|
for _, s := range e.Serves {
|
|
if seat, _, _ := strings.Cut(s, "."); slices.Contains(RegistrySeats, seat) && !slices.Contains(claimed, seat) {
|
|
claimed = append(claimed, seat)
|
|
}
|
|
}
|
|
if len(claimed) == 0 {
|
|
return nil
|
|
}
|
|
return func(seat string) []string {
|
|
if slices.Contains(claimed, seat) {
|
|
return []string{ev.Module}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// returnedHolders is the holder function of a manifest-reads-changed with why returned: the holders of each registry seat that run on
|
|
// the node it names. nil for another why, or when none runs there.
|
|
func (a *Againer) returnedHolders(data []byte) func(seat string) []string {
|
|
var ev nodeengine.ManifestReadsChanged
|
|
if err := json.Unmarshal(data, &ev); err != nil || ev.Node == "" {
|
|
a.logf("a manifest-reads-changed that does not read is not acted on: %v", err)
|
|
return nil
|
|
}
|
|
if ev.Why != nodeengine.ManifestReadsChangedWhyReturned || a.Nodes == nil {
|
|
return nil
|
|
}
|
|
on := map[string]string{}
|
|
for _, seat := range RegistrySeats {
|
|
for _, h := range a.Nodes(seat) {
|
|
if h.Node == ev.Node {
|
|
on[seat] = h.Module
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if len(on) == 0 {
|
|
return nil
|
|
}
|
|
return func(seat string) []string {
|
|
if m, ok := on[seat]; ok {
|
|
return []string{m}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// unreleased are the reserved versions whose build is done and with an artifact unpublished.
|
|
func (a *Againer) unreleased(ctx context.Context) ([]ReservedVersion, error) {
|
|
all, err := a.Republisher.Reserved.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []ReservedVersion
|
|
for _, v := range all {
|
|
if !v.BuiltAt.IsZero() && len(v.Missing()) > 0 {
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// again queues the unreleased versions at the holders holdersOf names, at most MaxVersions of them.
|
|
func (a *Againer) again(ctx context.Context, holdersOf func(seat string) []string, seq uint64) error {
|
|
vs, err := a.unreleased(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("the reserved versions cannot be read: %w", err)
|
|
}
|
|
if len(vs) == 0 {
|
|
return nil
|
|
}
|
|
max := a.MaxVersions
|
|
if max <= 0 {
|
|
max = DefaultRepublishMaxVersions
|
|
}
|
|
queued, err := a.Republisher.queue(ctx, vs, holdersOf, max, seq)
|
|
if len(queued) > 0 {
|
|
a.logf("publish again on an event: %d versions put on the work queue: %s", len(queued), strings.Join(queued, ", "))
|
|
}
|
|
return err
|
|
}
|