Indexer: catalog-revision's duplicate id names the branch and it is published through the bus's acknowledgement

Builds: design 47 G A-409
This commit is contained in:
2026-11-14 22:55:59 +01:00
parent da82c2cf80
commit c625733a33
3 changed files with 74 additions and 20 deletions
@@ -1,6 +1,12 @@
package main
import (
"context"
"errors"
"strings"
"git.novox.be/novox/mesh-sdk/go/process"
"github.com/nats-io/nats.go"
"testing"
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
@@ -23,3 +29,40 @@ func TestACatalogRevisionIsSaidOnTheSeatsSubjectWithItsBranch(t *testing.T) {
}
}
}
// The duplicate id names the branch, and the publish goes through the acknowledging publisher, its error returned.
func TestACatalogRevisionIsPublishedAcknowledgedUnderAnIdNamingItsBranch(t *testing.T) {
cred := process.Account{Module: "mesh-delivery-indexer", Node: "n1"}
ev := func(branch string) meshcatalog.CatalogRevisionEvent {
return meshcatalog.CatalogRevisionEvent{Kind: "candidate", Branch: branch, Revision: 7}
}
var ids []string
var subjects []string
publish := func(_ context.Context, m *nats.Msg, id string) error {
ids = append(ids, id)
subjects = append(subjects, m.Subject)
if m.Header.Get(nats.MsgIdHdr) != id {
t.Errorf("header id %q, passed id %q", m.Header.Get(nats.MsgIdHdr), id)
}
return nil
}
say := sayCatalogRevision(cred, publish)
for _, b := range []string{"main", "fix/x"} {
if err := say(context.Background(), ev(b)); err != nil {
t.Fatal(err)
}
}
if len(ids) != 2 || ids[0] == ids[1] || !strings.Contains(ids[0], "main") || !strings.Contains(ids[1], "fix/x") {
t.Errorf("ids %v: the same number on two branches must be two ids naming each branch", ids)
}
if subjects[0] != "mesh.seat.mesh-catalog.event.catalog-revision.main" {
t.Errorf("subject %q", subjects[0])
}
boom := errors.New("stream refused")
if err := sayCatalogRevision(cred, func(context.Context, *nats.Msg, string) error { return boom })(context.Background(), ev("main")); !errors.Is(err, boom) {
t.Errorf("a publish the bus refused returned %v", err)
}
if err := say(context.Background(), meshcatalog.CatalogRevisionEvent{Kind: "trunk", Revision: 1}); err == nil {
t.Error("an event with no branch was published")
}
}
@@ -467,7 +467,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
Revisions: revisions, EntryIndex: awaitedStore{b: process.AwaitBucket(ctx, js, conn, index.Bucket(cred.Module, index.EntryIndexState), "no entry is found by its fingerprint", logger.Printf)}, // Message a kept call: a read, called again through a transport fault (ADR 0668 §1)
// Each catalog revision may bring another holder of a build seat: the build modules are found again (buildmodules.go).
// A new trunk revision that changes the modules is said on the views family of modules too (design 47 G A-360).
SayCatalogRevision: afterRevision(refreshingBuildModules(sayingModulesView(sayCatalogRevision(conn, cred), cred, views.JetStreamPublish(js)), &buildFollow), &fillFollow),
SayCatalogRevision: afterRevision(refreshingBuildModules(sayingModulesView(sayCatalogRevision(cred, views.JetStreamPublish(js)), cred, views.JetStreamPublish(js)), &buildFollow), &fillFollow),
SayFill: sayFill(conn, cred),
// The catalog's released versions (design 47 G §349): the state versions, bound when it appears, and the event version-released.
Versions: awaitedStore{b: process.AwaitBucket(ctx, js, conn, index.Bucket(cred.Module, index.VersionsState), "no released version is kept, so no package-published makes a version", logger.Printf)},
@@ -135,28 +135,39 @@ func catalogRevisionSubject(e meshcatalog.CatalogRevisionEvent) (string, error)
return meshcatalog.SubjectCatalogRevision(e.Branch), nil
}
// sayCatalogRevision publishes the seat mesh-catalog's event catalog-revision under the duplicate id of its kind and
// number, so a revision said twice is heard once (design 47, Two graphs).
func sayCatalogRevision(conn *nats.Conn, cred process.Account) func(context.Context, meshcatalog.CatalogRevisionEvent) error {
return func(_ context.Context, e meshcatalog.CatalogRevisionEvent) error {
data, err := json.Marshal(e)
// catalogRevisionMessage is the seat mesh-catalog's event catalog-revision as it is published, with its duplicate id: the
// event's name, its branch, its kind and its number, so a revision said twice is heard once and the same number on two
// branches (a candidate's and the trunk's) is two events (design 47 G A-409).
func catalogRevisionMessage(cred process.Account, e meshcatalog.CatalogRevisionEvent) (*nats.Msg, string, error) {
data, err := json.Marshal(e)
if err != nil {
return nil, "", err
}
subject, err := catalogRevisionSubject(e)
if err != nil {
return nil, "", err
}
msg := nats.NewMsg(subject)
id := fmt.Sprintf("%s-%s-%s-%d%s", meshcatalog.EventCatalogRevision, e.Branch, e.Kind, e.Revision, e.Heads)
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.Header.Set(nats.MsgIdHdr, id)
msg.Data = data
return msg, id, nil
}
// sayCatalogRevision publishes the event through the bus's acknowledgement (views.JetStreamPublish), an error returned when
// the bus did not keep it: a core publish says nothing of a stream that refused it (design 47 G A-409).
func sayCatalogRevision(cred process.Account, publish func(context.Context, *nats.Msg, string) error) func(context.Context, meshcatalog.CatalogRevisionEvent) error {
return func(ctx context.Context, e meshcatalog.CatalogRevisionEvent) error {
msg, id, err := catalogRevisionMessage(cred, e)
if err != nil {
return err
}
subject, err := catalogRevisionSubject(e)
if err != nil {
return err
}
msg := nats.NewMsg(subject)
id := fmt.Sprintf("%s-%s-%d%s", meshcatalog.EventCatalogRevision, e.Kind, e.Revision, e.Heads)
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.Header.Set(nats.MsgIdHdr, id)
msg.Data = data
return conn.PublishMsg(msg)
return publish(ctx, msg, id)
}
}