# Conflicts: # modules/mesh-delivery-builder/buildrun/asks_test.go # modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go # modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go # modules/mesh-delivery-builder/internal/build/numbers.go # modules/mesh-delivery-builder/internal/build/run.go # modules/mesh-delivery-builder/internal/build/stuck.go # modules/mesh-delivery-builder/internal/versions/publish.go # modules/mesh-delivery-builder/internal/versions/reserve.go # modules/mesh-delivery-builder/manifest_test.go # modules/mesh-delivery-builder/module.json
275 lines
8.4 KiB
Go
275 lines
8.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// The floor's two module tools (novox/hq design 51 *The floor*); the seat's is `seat:<registry>.versions`.
|
|
const (
|
|
bridgeTagsTool = "bridge_tags"
|
|
requirementsTool = "indexer_requirements"
|
|
)
|
|
|
|
// awaitedVersions is the state versions as versions.Store: bound once the bus holds it (the grants part makes it, issue
|
|
// 1790); until then every use is an error, so a trunk build is built unnumbered and said (build.Builder.number).
|
|
type awaitedVersions struct {
|
|
b *process.AwaitedBucket
|
|
// bucket names the state in the error of one not on the bus yet; empty is the versions'.
|
|
bucket string
|
|
}
|
|
|
|
var errVersionsMissing = errors.New("the state " + versions.Bucket + " is not on the bus yet")
|
|
|
|
func (s awaitedVersions) kv() (jetstream.KeyValue, error) {
|
|
if kv := s.b.KV(); kv != nil {
|
|
return kv, nil
|
|
}
|
|
if s.bucket != "" {
|
|
return nil, errors.New("the state " + s.bucket + " is not on the bus yet")
|
|
}
|
|
return nil, errVersionsMissing
|
|
}
|
|
|
|
func (s awaitedVersions) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) {
|
|
kv, err := s.kv()
|
|
if err != nil {
|
|
return nil, 0, false, err
|
|
}
|
|
e, err := kv.Get(ctx, key)
|
|
if errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
return nil, 0, false, nil
|
|
}
|
|
if err != nil {
|
|
return nil, 0, false, err
|
|
}
|
|
return e.Value(), e.Revision(), true, nil
|
|
}
|
|
|
|
func versionsConflict(err error) error {
|
|
if errors.Is(err, jetstream.ErrKeyExists) || errors.Is(err, jetstream.ErrKeyRevisionMismatch) {
|
|
return versions.ErrConflict
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s awaitedVersions) Create(ctx context.Context, key string, value []byte) (uint64, error) {
|
|
kv, err := s.kv()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
rev, err := kv.Create(ctx, key, value)
|
|
return rev, versionsConflict(err)
|
|
}
|
|
|
|
func (s awaitedVersions) Update(ctx context.Context, key string, value []byte, revision uint64) (uint64, error) {
|
|
kv, err := s.kv()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
rev, err := kv.Update(ctx, key, value, revision)
|
|
return rev, versionsConflict(err)
|
|
}
|
|
|
|
func (s awaitedVersions) KeysOf(ctx context.Context, filter string) ([]string, error) {
|
|
kv, err := s.kv()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
l, err := kv.ListKeysFiltered(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = l.Stop() }()
|
|
var out []string
|
|
for k := range l.Keys() {
|
|
out = append(out, k)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// floorVia is the floor's three sources as calls of the builder's own account (design 51 *The floor*): the seat's verb
|
|
// versions, the forge bridge's bridge_tags and the indexer's indexer_requirements. A call that fails is that source
|
|
// silent.
|
|
func floorVia(call toolCaller, holder func(seat string) (string, bool)) versions.FloorSources {
|
|
sourceTool := func(source, tool string) (string, error) {
|
|
seat := versions.SourceSeats[source]
|
|
module, ok := holder(seat)
|
|
if !ok {
|
|
return "", fmt.Errorf("no module is known to hold the seat %s, the source of %s", seat, tool)
|
|
}
|
|
return module + "." + tool, nil
|
|
}
|
|
return versions.FloorSources{
|
|
Seat: func(_ context.Context, seat, name string) ([]string, error) {
|
|
raw, err := call("seat:"+seat+".versions", map[string]any{"name": name})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var a struct {
|
|
Versions []struct {
|
|
Version string `json:"version"`
|
|
} `json:"versions"`
|
|
}
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return nil, fmt.Errorf("%s's versions answered what is no list of versions: %w", seat, err)
|
|
}
|
|
var out []string
|
|
for _, v := range a.Versions {
|
|
if v.Version != "" {
|
|
out = append(out, v.Version)
|
|
}
|
|
}
|
|
return out, nil
|
|
},
|
|
Tags: func(_ context.Context, repository, prefix string) ([]string, error) {
|
|
tool, err := sourceTool(versions.SourceTags, bridgeTagsTool)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
raw, err := call(tool, map[string]any{"repository": repository, "prefix": prefix})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var a struct {
|
|
Tags []string `json:"tags"`
|
|
}
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return nil, fmt.Errorf("bridge_tags answered what is no list of tags: %w", err)
|
|
}
|
|
return a.Tags, nil
|
|
},
|
|
Requirements: func(_ context.Context, name string) ([]string, error) {
|
|
tool, err := sourceTool(versions.SourceRequirements, requirementsTool)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
raw, err := call(tool, map[string]any{"package": name})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var a struct {
|
|
Requirements []string `json:"requirements"`
|
|
}
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return nil, fmt.Errorf("indexer_requirements answered what is no list of versions: %w", err)
|
|
}
|
|
return a.Requirements, nil
|
|
},
|
|
}
|
|
}
|
|
|
|
// emitEvent says one of the builder's events, mesh.mod.<module>.event.<event>, with the SDK's envelope headers and id as
|
|
// the bus's dedup id.
|
|
func emitEvent(ctx context.Context, js jetstream.JetStream, cred process.Account, event, id string, body any) error {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
msg := nats.NewMsg(cred.EventSubject(event))
|
|
msg.Header.Set("x-event-id", id)
|
|
msg.Header.Set("x-source", cred.Module)
|
|
msg.Header.Set("x-node", cred.Node)
|
|
msg.Header.Set("x-time", time.Now().UTC().Format(time.RFC3339Nano))
|
|
msg.Header.Set("content-type", "application/json")
|
|
msg.Data = data
|
|
pub, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
_, err = js.PublishMsg(pub, msg, jetstream.WithMsgID(id))
|
|
return err
|
|
}
|
|
|
|
// emitSeatEvent says an event of a seat the builder holds, on the seat's subject (mesh.seat.<seat>.event.<event>).
|
|
func emitSeatEvent(ctx context.Context, js jetstream.JetStream, cred process.Account, subject, id string, body any) error {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
msg := nats.NewMsg(subject)
|
|
msg.Header.Set("x-event-id", id)
|
|
msg.Header.Set("x-source", cred.Module)
|
|
msg.Header.Set("x-node", cred.Node)
|
|
msg.Header.Set("x-time", time.Now().UTC().Format(time.RFC3339Nano))
|
|
msg.Header.Set("content-type", "application/json")
|
|
msg.Data = data
|
|
pub, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
_, err = js.PublishMsg(pub, msg, jetstream.WithMsgID(id))
|
|
return err
|
|
}
|
|
|
|
// The events on the builder's consumer of EVENTS that wake a floor (design 51 *The floor*, DLV-B3b).
|
|
var (
|
|
manifestAcceptedSubject = meshcatalog.SubjectManifestAccepted
|
|
conditionClearedSubject = meshconditions.SubjectConditionCleared
|
|
)
|
|
|
|
// floorWake asks the floor of the names an event wakes again, by looking at every module again: a trunk build is
|
|
// numbered before its claim (build.Builder.number).
|
|
type floorWake struct {
|
|
b *build.Builder
|
|
waits *versions.FloorWaits
|
|
logf func(string, ...any)
|
|
}
|
|
|
|
// heard takes a message of the consumer in when it is one that wakes a floor, acknowledged, and answers whether it was.
|
|
func (f *floorWake) heard(ctx context.Context, m jetstream.Msg) bool {
|
|
switch m.Subject() {
|
|
case manifestAcceptedSubject:
|
|
f.ack(m)
|
|
f.wake(ctx, versions.WakeAccepted, "", "")
|
|
case conditionClearedSubject:
|
|
f.ack(m)
|
|
var e meshconditions.ConditionEvent
|
|
if err := json.Unmarshal(m.Data(), &e); err != nil {
|
|
f.logf("%s holds what is no condition, left: %v", m.Subject(), err)
|
|
return true
|
|
}
|
|
if e.Subject == nil {
|
|
return true
|
|
}
|
|
for _, module := range conditionModules(e.Subject.ID) {
|
|
f.wake(ctx, versions.WakeCleared, "", module)
|
|
}
|
|
default:
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (f *floorWake) ack(m jetstream.Msg) {
|
|
if err := m.Ack(); err != nil {
|
|
f.logf("acknowledging %s: %v", m.Subject(), err)
|
|
}
|
|
}
|
|
|
|
// wake looks at every module again when the event wakes a name, and says which.
|
|
func (f *floorWake) wake(ctx context.Context, kind, seat, module string) {
|
|
names := f.waits.Wake(kind, seat, module, "")
|
|
if len(names) == 0 {
|
|
return
|
|
}
|
|
f.logf("the floor of %s is asked again: %s", strings.Join(names, ", "), kind)
|
|
f.b.Reconsider(ctx)
|
|
}
|
|
|
|
// conditionModules are the tokens of a condition's subject id (`<node>/<module>`, `<module>.<check>`): a module named
|
|
// among them is the one whose condition cleared.
|
|
func conditionModules(id string) []string {
|
|
return strings.FieldsFunc(id, func(r rune) bool { return r == '/' || r == '.' || r == ':' })
|
|
}
|