diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry.go index 0cba481d9..7fa5900bc 100644 --- a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry.go +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry.go @@ -145,6 +145,9 @@ func newRegistry(js jetstream.JetStream, conn *nats.Conn, cred process.Account, } ix.SeedWaits(context.Background(), ask, waiting) }, + Unwritten: func(name, ask, reason string, unwritten bool) { + ix.RegistrationUnwritten(context.Background(), name, ask, reason, unwritten) + }, Logf: logf} } @@ -160,7 +163,8 @@ func routerRecordOf(module string, call toolCaller, record func(bucket, id strin } // heardDecided is the handler of the decided events for this module: a warrant is applied by the registry; one it could not -// apply is left unacknowledged, so the bus gives it again, and said. +// apply is left for the bus to give again, and said. On the last delivery its consumer allows the warrant is given up: its ask +// is answered as not applied and the registration said unwritten (design 47 H2 H-A-470 (2)). func heardDecided(ctx context.Context, reg *index.Registry, m jetstream.Msg, logf func(string, ...any)) { var w oc.Warrant if err := json.Unmarshal(m.Data(), &w); err != nil { @@ -169,7 +173,14 @@ func heardDecided(ctx context.Context, reg *index.Registry, m jetstream.Msg, log return } if err := reg.Heard(ctx, w); err != nil { - logf("the warrant of the ask %s is left unapplied and given again: %s", w.Ask, strings.TrimSpace(err.Error())) + reason := strings.TrimSpace(err.Error()) + logf("the warrant of the ask %s is left unapplied and given again: %s", w.Ask, reason) + leftForBus(m, "the warrant of the ask "+w.Ask, err, logf) + if md, merr := m.Metadata(); merr == nil && md != nil && md.NumDelivered >= EventDeliveries { + if gerr := reg.GiveUp(ctx, w, reason); gerr != nil { + logf("the warrant of the ask %s is given up, but its ask is not answered: %v", w.Ask, gerr) + } + } return } _ = m.Ack() diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry_integration_test.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry_integration_test.go new file mode 100644 index 000000000..e1d177d5b --- /dev/null +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry_integration_test.go @@ -0,0 +1,153 @@ +//go:build integration + +package main + +import ( + "context" + "encoding/json" + "os" + "strings" + "testing" + "time" + + "github.com/nats-io/nats-server/v2/server" + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/jetstream" + + occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" + oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-sdk/go/buscompose" + "git.novox.be/novox/mesh-sdk/go/catalogue" + + "mesh-delivery-indexer/internal/index" +) + +// TestARegistrationIsKeptOnTheHistoryStreamItsManifestDeclares (design 47 H2 H-A-470 (3); the kind of fix-a-bug-met-twice: a +// test that does not use the real stream's subject): the program's registry, as newRegistry wires it, writes a warranted +// registration of a module (one token after registration.) and of a source-only repository (source.., H-A-408's +// key) through the stream registration-history on an embedded bus, made from the indexer's manifest's streams section as the +// grants part makes it (buscompose); each version lands on its subject, the name whole. +func TestARegistrationIsKeptOnTheHistoryStreamItsManifestDeclares(t *testing.T) { + srv, err := server.NewServer(&server.Options{Host: "127.0.0.1", Port: -1, JetStream: true, StoreDir: t.TempDir(), NoLog: true, NoSigs: true}) + if err != nil { + t.Fatal(err) + } + go srv.Start() + if !srv.ReadyForConnections(10 * time.Second) { + t.Fatal("the bus did not come up") + } + t.Cleanup(srv.Shutdown) + conn, err := nats.Connect(srv.ClientURL()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(conn.Close) + js, err := jetstream.New(conn) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + t.Cleanup(cancel) + + // The stream as the grants part makes it from the manifest: its events spelled as the bus spells them, on the module's subjects. + raw, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + var m struct { + Streams []struct { + Name string `json:"name"` + Events []string `json:"events"` + } `json:"streams"` + } + if err := json.Unmarshal(raw, &m); err != nil { + t.Fatal(err) + } + var wire []string + for _, s := range m.Streams { + if s.Name == index.RegistrationHistoryStream { + for _, e := range s.Events { + wire = append(wire, buscompose.WirePattern(e)) + } + } + } + subjects := (buscompose.ModuleStream{Module: testIndexer, Events: wire}).Subjects() + stream, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: catalogue.StreamBusName(index.RegistrationHistoryStream), Subjects: subjects}) + if err != nil { + t.Fatalf("the stream of %v: %v", subjects, err) + } + + // The seat git answers the default branch's head and a tree holding no module.json; the router's record holds the warrant + // of each ask the indexer publishes. + const head = "0123456789abcdef0123456789abcdef01234567" + call := func(key string, body any) (json.RawMessage, error) { + var out any + switch key { + case "seat:" + gitseat.Seat + "." + gitseat.VerbBranch: + in := body.(gitseat.BranchInput) + out = gitseat.BranchAnswer{Branch: "main", Repository: in.Repository, Head: head} + case gitVerb(gitseat.VerbTrees): + out = gitseat.TreesAnswer{Commit: head, Trees: map[string]string{"tools": "t1"}} + default: + t.Errorf("asked %s", key) + } + return json.Marshal(out) + } + asks := make(chan oc.Ask, 4) + sub, err := conn.Subscribe(oc.AcceptSubjectAsk(testIndexer), func(msg *nats.Msg) { + var a oc.Ask + if err := json.Unmarshal(msg.Data, &a); err == nil { + asks <- a + } + }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = sub.Unsubscribe() }) + warrants := map[string]oc.Warrant{} + where := func(id string) (string, *oc.Warrant, error) { + if w, ok := warrants[id]; ok { + return "chosen", &w, nil + } + return "open", nil, nil + } + reg := newRegistry(js, conn, testAccount, call, index.MemoryStore(), index.MemoryStore(), where, &index.Indexer{Module: testIndexer}, t.Logf) + + check := &meshcatalog.SourceCheck{Seat: meshcatalog.SourceCheckSeatNodeBuildGo, Step: meshcatalog.SourceCheckStepUnit} + for name, r := range map[string]meshcatalog.Registration{ + "avahi": {Module: "avahi", Origin: "novox/mesh-catalog", Path: "modules/avahi/module.json", Branches: []string{"main"}}, + "source.novox.hq": {Origin: "novox/hq", Check: check, Policy: meshcatalog.Policy{Steps: meshcatalog.StepStatuses{Unit: "required"}}}, + } { + change, _ := json.Marshal(meshcatalog.RegisterInput{Registration: r}) + got, err := reg.Propose(ctx, "register", "s@n", change) + if err != nil || got.Ask == "" || got.Module != name { + t.Fatalf("register %s: %+v, %v; want an ask", name, got, err) + } + var a oc.Ask + select { + case a = <-asks: + case <-ctx.Done(): + t.Fatalf("the ask of %s was not published", name) + } + o, _ := occ.OptionOf(a, "approve") + w := oc.Warrant{Ask: a.ID, Asker: testIndexer, Outcome: occ.OutcomeChosen, Option: "approve", Level: o.Level, Label: o.Label, + AskDigest: occ.Digest(a), At: occ.FormatTime(time.Now()), By: &oc.Person{Who: occ.Operator, Kind: "telegram", Verified: "user id verified"}} + warrants[a.ID] = w + if err := reg.Heard(ctx, w); err != nil { + t.Fatalf("the warrant of %s was not applied: %v", name, err) + } + msg, err := stream.GetLastMsgForSubject(ctx, historySubject(testIndexer, name)) + var kept meshcatalog.Registration + if err == nil { + err = json.Unmarshal(msg.Data, &kept) + } + if err != nil || kept.Version != 1 || index.RegistrationName(kept) != name { + t.Fatalf("registration-history of %s holds %+v (%v); want its version 1 kept on %v", name, kept, err, subjects) + } + if one, _, _ := reg.Registration(ctx, name); one.Version != 1 || !strings.Contains(one.Origin, "novox/") { + t.Errorf("the registration of %s is %+v, want version 1 written", name, one) + } + } +} diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/router_record_test.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/router_record_test.go index c242a8618..e6d355160 100644 --- a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/router_record_test.go +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/router_record_test.go @@ -1,11 +1,17 @@ package main import ( + "context" "encoding/json" + "errors" + "fmt" "strings" "testing" + oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" stdio "git.novox.be/novox/mesh-sdk/go" + + "mesh-delivery-indexer/internal/index" ) // Design 47 G §700, A-74: the router's record of an ask is read under the indexer's module's name from the state asks of the @@ -35,3 +41,43 @@ func TestTheRoutersRecordIsReadFromTheBucketOfTheSeatsHolder(t *testing.T) { t.Errorf("with no holder Where answered %v; want the reason", err) } } + +// Design 47 H2 H-A-470 (2), addendum (d): a warrant the registry cannot apply is left for the bus (naked) while the bus gives it +// again; on the delivery that reaches EventDeliveries it is given up: its key ends refused, not applied, and registration-unwritten +// is said naming the ask and the reason. +func TestAWarrantIsGivenUpOnItsLastDelivery(t *testing.T) { + ctx := context.Background() + var said []string + proposals := index.MemoryStore() + open, _ := json.Marshal(index.Proposal{V: 1, Verb: "register", Module: "source.novox.hq", Ask: "a1", State: index.ProposalOpen}) + if err := proposals.Create(ctx, "register.source.novox.hq", open); err != nil { + t.Fatal(err) + } + reg := &index.Registry{Module: testIndexer, Registrations: index.MemoryStore(), Proposals: proposals, + Where: func(string) (string, *oc.Warrant, error) { return "", nil, errors.New("nats: no response from stream") }, + Unwritten: func(name, ask, reason string, unwritten bool) { + said = append(said, fmt.Sprintf("%s %s %t %s", name, ask, unwritten, reason)) + }} + body, _ := json.Marshal(oc.Warrant{Ask: "a1", Outcome: "chosen", Option: "approve"}) + state := func() index.Proposal { + v, _, _, _ := proposals.Get(ctx, "register.source.novox.hq") + var p index.Proposal + _ = json.Unmarshal(v, &p) + return p + } + for n := uint64(1); n < EventDeliveries; n++ { + m := &deliveredMsg{subject: "decided", data: body, n: n} + heardDecided(ctx, reg, m, func(string, ...any) {}) + if !m.naked || state().State != index.ProposalOpen || len(said) != 0 { + t.Fatalf("delivery %d: naked %t, key %+v, said %v; want it left for the bus, the key open, nothing said", n, m.naked, state(), said) + } + } + m := &deliveredMsg{subject: "decided", data: body, n: EventDeliveries} + heardDecided(ctx, reg, m, func(string, ...any) {}) + if p := state(); !m.naked || p.State != index.ProposalRefused || !strings.HasPrefix(p.Outcome, "not applied: ") || !strings.Contains(p.Outcome, "no response from stream") { + t.Fatalf("the last delivery: naked %t, key %+v; want it ended refused, not applied, naming the reason", m.naked, p) + } + if len(said) != 1 || !strings.HasPrefix(said[0], "source.novox.hq a1 true ") || !strings.Contains(said[0], "no response from stream") { + t.Fatalf("said %v; want registration-unwritten of source.novox.hq naming the ask and the reason", said) + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/findings.go b/modules/mesh-delivery-indexer/internal/index/findings.go index f21c16227..05bc623be 100644 --- a/modules/mesh-delivery-indexer/internal/index/findings.go +++ b/modules/mesh-delivery-indexer/internal/index/findings.go @@ -23,7 +23,7 @@ const CallUnansweredKind = "indexer-call-unanswered" // (H-A-247 (1)); a views reader's and the emitter's (views.ViewKinds) are always the writer's besides. The tool runner's // writer over the same state `findings` owns its own. TestFindingKindsHoldEveryKindTheCodeRaises holds the code to this list. var FindingKinds = []string{TakeInWaitingKind, PinMissingKind, OutOfStepKind, PackagesNotPublishedKind, HistoryFillingKind, SeedWaitsKind, - SeedLeftOutKind, SeatDefinitionUnparsedKind, SeatDefinedTwiceKind, SeatOutsideItsDomainKind, CallUnansweredKind, + SeedLeftOutKind, RegistrationUnwrittenKind, SeatDefinitionUnparsedKind, SeatDefinedTwiceKind, SeatOutsideItsDomainKind, CallUnansweredKind, views.EventUnhandledKind, heard.Kind} // RetiredFindingKinds are kinds no code of the indexer raises any more, which its writer clears once at its start (H-A-247 diff --git a/modules/mesh-delivery-indexer/internal/index/registration_findings.go b/modules/mesh-delivery-indexer/internal/index/registration_findings.go index 0837acd6c..6dd143ec0 100644 --- a/modules/mesh-delivery-indexer/internal/index/registration_findings.go +++ b/modules/mesh-delivery-indexer/internal/index/registration_findings.go @@ -23,6 +23,10 @@ const ( // module, id the module's name), naming the artifact and its language or kind; cleared only when a later seed finds a // build seat for every artifact of it, or the module leaves the catalogue (A-324, A-329). SeedLeftOutKind = "seed-left-out" + // RegistrationUnwrittenKind is the finding of a warranted registration change the indexer gave up writing (scope module, id + // the registration's name), naming the ask and the reason; cleared when a later version of that registration is written + // (design 47 H2 H-A-470 (2) and its addendum). + RegistrationUnwrittenKind = "registration-unwritten" ) // HistoryFill writes the finding registration-history-filling when the stream holds used bytes of its max, 80 % or more, @@ -68,3 +72,25 @@ func (ix *Indexer) SeedWaits(ctx context.Context, ask string, waiting bool) { ix.logf("registrations: the finding %s is not cleared: %v", SeedWaitsKind, err) } } + +// RegistrationUnwritten writes the finding registration-unwritten of the registration name, naming the ask whose warrant was given +// up and the reason, and clears it when a later version of the registration is written (design 47 H2 H-A-470 (2)). +func (ix *Indexer) RegistrationUnwritten(ctx context.Context, name, ask, reason string, unwritten bool) { + if ix.Findings == nil { + return + } + if unwritten { + f := ix.observed(mcc.ScopeModule, name, RegistrationUnwrittenKind, + fmt.Sprintf("the registration of %s is not written (ask %s): %s", name, ask, reason), + "A registration change approved was not written", + fmt.Sprintf("The operator's warrant of the ask %s was given up after the bus gave it its last time, so the registration of %s is not written and the ask is answered as not applied: %s. Asking the change again raises a new ask.", ask, name, reason), + "indexer."+RegistrationUnwrittenKind) + if err := ix.Findings.Write(ctx, f); err != nil { + ix.logf("registrations: the finding %s of %s is not written: %v", RegistrationUnwrittenKind, name, err) + } + return + } + if err := ix.clearFinding(ctx, mcc.ScopeModule, name, RegistrationUnwrittenKind, "a later version of "+name+" is written (ask "+ask+")"); err != nil { + ix.logf("registrations: the finding %s of %s is not cleared: %v", RegistrationUnwrittenKind, name, err) + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/registration_findings_test.go b/modules/mesh-delivery-indexer/internal/index/registration_findings_test.go index ecf77a349..72d9ef1ce 100644 --- a/modules/mesh-delivery-indexer/internal/index/registration_findings_test.go +++ b/modules/mesh-delivery-indexer/internal/index/registration_findings_test.go @@ -2,6 +2,7 @@ package index import ( "context" + "strings" "testing" mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" @@ -45,4 +46,15 @@ func TestRegistrationFindings(t *testing.T) { if held(SeedWaitsKind) { t.Fatal("the warrant did not clear seed-waits") } + // Design 47 H2 H-A-470 (2): registration-unwritten is of the module, its id the registration's name, naming the ask and + // the reason; cleared when a later version is written. + ix.RegistrationUnwritten(ctx, "source.novox.hq", "ask-2", "nats: no response from stream", true) + if h := w.Held(); len(h) != 1 || h[0].Scope != mcc.ScopeModule || h[0].Kind != RegistrationUnwrittenKind || h[0].ID != keyID("source.novox.hq") || + !strings.Contains(h[0].Summary, "ask-2") || !strings.Contains(h[0].Summary, "no response from stream") { + t.Fatalf("registration-unwritten: %+v", h) + } + ix.RegistrationUnwritten(ctx, "source.novox.hq", "ask-3", "", false) + if len(w.Held()) != 0 { + t.Fatal("a later version written did not clear registration-unwritten") + } } diff --git a/modules/mesh-delivery-indexer/internal/index/registrations.go b/modules/mesh-delivery-indexer/internal/index/registrations.go index 41135a24b..2ecf26001 100644 --- a/modules/mesh-delivery-indexer/internal/index/registrations.go +++ b/modules/mesh-delivery-indexer/internal/index/registrations.go @@ -202,6 +202,9 @@ type Registry struct { Where func(id string) (state string, w *oc.Warrant, err error) // SeedWaits says the finding seed-waits naming the ask, and clears it (§8); nil says nothing. SeedWaits func(ask string, waiting bool) + // Unwritten says the finding registration-unwritten of a registration whose warrant was given up, naming the ask and the + // reason, and clears it when a later version of it is written (design 47 H2 H-A-470 (2)); nil says nothing. + Unwritten func(name, ask, reason string, unwritten bool) // Seed composes the first registrations (§8); nil proposes none. Seed func(ctx context.Context) (SeedProposal, error) Now func() time.Time @@ -899,6 +902,30 @@ func (r *Registry) Heard(ctx context.Context, w oc.Warrant) error { return r.apply(ctx, key, *p, *rec) } +// GiveUp answers the ask of a warrant the indexer gives up on, its message given the last time the bus gives it, as not applied: +// its key ends refused "not applied: ", and the registration it would have written is said as the finding +// registration-unwritten naming the ask and the reason; never left silent. The seed's key raises no such finding: its end +// already says seed-waits (design 47 H2 H-A-470 (2) and its two addenda). +func (r *Registry) GiveUp(ctx context.Context, w oc.Warrant, reason string) error { + r.mu.Lock() + defer r.mu.Unlock() + key, p, err := r.findAsk(ctx, w.Ask) + if err != nil { + return err + } + if p == nil || p.State != ProposalOpen { + r.logf("the warrant of the ask %s is given up and matches no open proposal, so nothing is answered", w.Ask) + return nil + } + if err := r.endKey(ctx, key, *p, ProposalRefused, "not applied: "+reason); err != nil { + return err + } + if key != SeedKey && r.Unwritten != nil { + r.Unwritten(p.Module, w.Ask, reason, true) + } + return nil +} + func (r *Registry) endKey(ctx context.Context, key string, p Proposal, state, outcome string) error { if key == SeedKey && state != ProposalApplied && r.SeedWaits != nil { state = ProposalSeedWaits @@ -977,6 +1004,9 @@ func (r *Registry) write(ctx context.Context, key string, p Proposal, next meshc return err } step("written") + if r.Unwritten != nil { + r.Unwritten(name, w.Ask, "", false) + } if r.Say != nil { defer step("registration-changed said") if err := r.Say(ctx, meshcatalog.RegistrationChangedEvent{V: 1, Module: name, Version: next.Version, Moved: p.Lines, By: p.Caller, Warrant: w.Ask, At: &at}); err != nil { diff --git a/modules/mesh-delivery-indexer/internal/index/registrations_test.go b/modules/mesh-delivery-indexer/internal/index/registrations_test.go index 48ecfd1d8..e3ac7d230 100644 --- a/modules/mesh-delivery-indexer/internal/index/registrations_test.go +++ b/modules/mesh-delivery-indexer/internal/index/registrations_test.go @@ -256,6 +256,66 @@ func TestRegistrationsAreWrittenOnTheirWarrantOnly(t *testing.T) { }) } +// Design 47 H2 H-A-470 (2) and its addenda: a warrant the indexer gives up on (its history write refused, given the last time) +// ends its key refused "not applied: " and says registration-unwritten of the registration, naming the ask and the +// reason; a later version written clears it; the seed's key ends so too, with no such finding (seed-waits says it). +func TestAWarrantGivenUpIsAnsweredNotAppliedAndSaid(t *testing.T) { + ctx := context.Background() + g := newRegRig() + type said struct { + name, ask, reason string + unwritten bool + } + var findings []said + g.r.Unwritten = func(name, ask, reason string, unwritten bool) { + findings = append(findings, said{name, ask, reason, unwritten}) + } + refused := errors.New("nats: no response from stream") + g.r.History = func(context.Context, meshcatalog.Registration) error { return refused } + got, _ := g.r.Propose(ctx, "register", "s@n", reg("a", meshcatalog.StepStatuses{}, "main")) + g.chosen[got.Ask] = "approve" + err := g.r.Heard(ctx, g.warrant(got.Ask, "approve")) + if err == nil { + t.Fatal("a history write refused was applied") + } + if err := g.r.GiveUp(ctx, g.warrant(got.Ask, "approve"), err.Error()); err != nil { + t.Fatal(err) + } + rows, _ := ListProposals(ctx, g.r.Proposals) + if len(rows) != 1 || rows[0].State != ProposalRefused || !strings.HasPrefix(rows[0].Outcome, "not applied: ") || !strings.Contains(rows[0].Outcome, refused.Error()) { + t.Fatalf("the key given up: %+v; want refused, not applied, naming the reason", rows) + } + if len(findings) != 1 || findings[0].name != "a" || findings[0].ask != got.Ask || !strings.Contains(findings[0].reason, refused.Error()) || !findings[0].unwritten { + t.Fatalf("findings said %+v; want registration-unwritten of a naming the ask and the reason", findings) + } + // Given up again (a late redelivery), an ended key says nothing more. + _ = g.r.GiveUp(ctx, g.warrant(got.Ask, "approve"), "again") + if len(findings) != 1 { + t.Fatalf("an ended key said %+v", findings) + } + // Asked again and written, the finding clears. + g.r.History = func(context.Context, meshcatalog.Registration) error { return nil } + again, _ := g.r.Propose(ctx, "register", "s@n", reg("a", meshcatalog.StepStatuses{}, "main")) + g.decide(t, again.Ask, "approve") + if len(findings) != 2 || findings[1].name != "a" || findings[1].unwritten { + t.Fatalf("findings said %+v; want registration-unwritten of a cleared by the version written", findings) + } + // The seed's warrant given up ends its key refused, says seed-waits and no registration-unwritten. + var waits []bool + g.r.SeedWaits = func(_ string, waiting bool) { waits = append(waits, waiting) } + seed, _ := json.Marshal(Proposal{V: 1, Verb: verbSeed, Ask: "ask-seed", State: ProposalOpen}) + if err := g.r.Proposals.Create(ctx, SeedKey, seed); err != nil { + t.Fatal(err) + } + if err := g.r.GiveUp(ctx, oc.Warrant{Ask: "ask-seed"}, "the store refused"); err != nil { + t.Fatal(err) + } + p, _, _, _ := g.r.load(ctx, SeedKey) + if p.State != ProposalSeedWaits || p.Outcome != "not applied: the store refused" || len(waits) != 1 || !waits[0] || len(findings) != 2 { + t.Fatalf("the seed given up: %+v, seed-waits %v, findings %+v; want it ended not applied, seed-waits said, no registration-unwritten", p, waits, findings) + } +} + func mustJSON(v any) json.RawMessage { b, _ := json.Marshal(v); return b } func TestProposalsSurviveARestartAndAreDropped(t *testing.T) { diff --git a/modules/mesh-delivery-indexer/manifest_test.go b/modules/mesh-delivery-indexer/manifest_test.go index af28dc77e..92d68c246 100644 --- a/modules/mesh-delivery-indexer/manifest_test.go +++ b/modules/mesh-delivery-indexer/manifest_test.go @@ -14,10 +14,12 @@ import ( "testing" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein" + "git.novox.be/novox/mesh-sdk/go/buscompose" "git.novox.be/novox/mesh-sdk/go/busretry" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/views" + "github.com/nats-io/nats-server/v2/server" "mesh-delivery-indexer/internal/index" ) @@ -228,6 +230,16 @@ func TestManifestDeclarations(t *testing.T) { // 1238/AC1, 1444 (reads the build m.Streams[1].Name != m.Module+"-findings" || m.Streams[1].MaxAgeDays != 7 || m.Streams[1].MaxMiB != 8 { t.Errorf("streams = %+v, want registration-history keeping its history, 64 MiB, and the findings stream at 8 MiB (§387, H2 §783)", m.Streams) } + // Design 47 H2 H-A-470 (1): the stream takes every registration's name whole, a module's one token and a source-only + // registration's source.. (H-A-408's key), so its write lands and is never dropped for want of a subject; the manifest spells the bus's `>` as `**`. + if len(m.Streams) > 0 { + for _, name := range []string{"mesh-delivery-indexer", "source.novox.hq"} { + subject := index.RegistrationHistoryEvent + "." + name + if !slices.ContainsFunc(m.Streams[0].Events, func(e string) bool { return server.SubjectMatchesFilter(subject, buscompose.WirePattern(e)) }) { + t.Errorf("registration-history takes %v, so the history of %s (%s) has no subject on it (H-A-470)", m.Streams[0].Events, name, subject) + } + } + } // Design 47 H2 §810, §814 (H-A-394 (1)): it claims a seat, so its class is core. §775: it makes kept calls, so it declares // the kept-call pace, 1 s to 30 s, read while it runs. §803 (H-A-461 (2)): a take-in's waiting line and bound, 1m and 5m. if m.Class != "core" { diff --git a/modules/mesh-delivery-indexer/module.json b/modules/mesh-delivery-indexer/module.json index e48cd603e..d7f1bbac5 100644 --- a/modules/mesh-delivery-indexer/module.json +++ b/modules/mesh-delivery-indexer/module.json @@ -173,7 +173,7 @@ { "name": "dry-runs", "history": 1, "ttl-seconds": 3600, "max-mib": 16 } ], "streams": [ - { "name": "registration-history", "events": ["registration.*"], "retention": "history", "max-mib": 64 }, + { "name": "registration-history", "events": ["registration.**"], "retention": "history", "max-mib": 64 }, { "name": "mesh-delivery-indexer-findings", "events": ["finding-written.*", "finding-cleared.*", "findings-stated.*"], "max-age-days": 7, "max-mib": 8 } ], "objects": [ diff --git a/modules/mesh-grants/cmd/mesh-grants/create.go b/modules/mesh-grants/cmd/mesh-grants/create.go index eb2445cee..2e489eef6 100644 --- a/modules/mesh-grants/cmd/mesh-grants/create.go +++ b/modules/mesh-grants/cmd/mesh-grants/create.go @@ -70,7 +70,8 @@ func (c creating) CreateDeclared(cfg jetstream.StreamConfig) error { } // AddSubjects adds subjects to a held stream and changes nothing else: it reads the stream's configuration, sets its -// subjects to the union and updates with the rest as it was. +// subjects to grants.WidenSubjects of what it holds and add (a held subject an added one covers replaced by it, since the +// bus refuses an overlap: design 47 H H-A-470, third addendum) and updates with the rest as it was. func (c creating) AddSubjects(stream string, add []string) error { ctx, cancel := context.WithTimeout(c.ctx, requestWithin) defer cancel() @@ -79,11 +80,7 @@ func (c creating) AddSubjects(stream string, add []string) error { return err } cfg := s.CachedInfo().Config - for _, a := range add { - if !slices.Contains(cfg.Subjects, a) { - cfg.Subjects = append(cfg.Subjects, a) - } - } + cfg.Subjects, _ = grants.WidenSubjects(cfg.Subjects, add) _, err = c.js.UpdateStream(ctx, cfg) return err } diff --git a/modules/mesh-grants/cmd/mesh-grants/integration_test.go b/modules/mesh-grants/cmd/mesh-grants/integration_test.go index b64f7cdfa..e6f3637f1 100644 --- a/modules/mesh-grants/cmd/mesh-grants/integration_test.go +++ b/modules/mesh-grants/cmd/mesh-grants/integration_test.go @@ -614,6 +614,27 @@ func TestTheGrantsPartWhole(t *testing.T) { if again, _ := stream.Info(ctx); !slices.Equal(again.Config.Subjects, []string{"a.>", "b.>"}) { t.Errorf("subjects after adding a held one: %v", again.Config.Subjects) } + // A held subject the added one covers is replaced by it, the messages kept: the bus refuses `registration.>` beside + // `registration.*` (JetStream 10052, overlapping subjects), the live registration history's case (design 47 H + // H-A-470, third addendum). + reg, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "REGHIST", Subjects: []string{"m.registration.*"}}) + if err != nil { + t.Fatal(err) + } + for _, subj := range []string{"m.registration.one", "m.registration.two"} { + if _, err := js.Publish(ctx, subj, []byte("x")); err != nil { + t.Fatal(err) + } + } + if err := c.AddSubjects("REGHIST", []string{"m.registration.>"}); err != nil { + t.Fatal(err) + } + if _, err := js.Publish(ctx, "m.registration.source.novox.hq", []byte("x")); err != nil { + t.Errorf("a source-only registration's write: %v", err) + } + if now, _ := reg.Info(ctx); !slices.Equal(now.Config.Subjects, []string{"m.registration.>"}) || now.State.Msgs != 3 { + t.Errorf("the registration history after the widening: subjects %v, %d messages, want m.registration.> and 3", now.Config.Subjects, now.State.Msgs) + } // A kept stream's source filter is brought to its definition's in place: the stream is not made again, its settings // stay, and an equal filter or a source of another stream changes nothing (design 47 H H-A-145, §746). if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "KEPTSRC", Subjects: []string{"ev.>"}}); err != nil { diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index 11e60b30f..5ae14e1d8 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -181,7 +181,7 @@ func (b *fakeBus) CreateConsumer(stream string, cfg jetstream.ConsumerConfig) er return nil } -// AddSubjects is the bus's addition of subjects to a held stream: the union, every other setting kept. +// AddSubjects is the bus's addition of subjects to a held stream: a covered subject replaced, every other setting kept. func (b *fakeBus) AddSubjects(stream string, add []string) error { b.mu.Lock() defer b.mu.Unlock() @@ -192,12 +192,7 @@ func (b *fakeBus) AddSubjects(stream string, add []string) error { for _, s := range b.streams { if s["name"] == stream { have, _ := s["subjects"].([]string) - for _, a := range add { - if !slices.Contains(have, a) { - have = append(have, a) - } - } - s["subjects"] = have + s["subjects"], _ = grants.WidenSubjects(have, add) return nil } } @@ -2270,7 +2265,8 @@ func TestGrants(t *testing.T) { // H-A-468 (design 47 H2): a seat with an accept that is not pulled by kind gets its one shared worker, SeatQueue's, made by // the grants part where the bus lacks it (its holders bind it, never make it); a seat pulled by kind gets none (its - // per-queue workers are its holders' seat traffic's); a worker the bus holds is left as it is, and no pass deletes it. + // per-queue workers are its holders' seat traffic's, made too where the bus lacks them: H-A-468, third addendum); a + // worker the bus holds is left as it is, and no pass deletes it. t.Run("a seat's shared worker is made where the bus lacks it", func(t *testing.T) { raw, err := os.ReadFile("module.json") if err != nil { @@ -2279,6 +2275,10 @@ func TestGrants(t *testing.T) { b := fixture() b.manifestOf("mesh-grants", string(raw)) b.assign("a", "mesh-grants") + // H-A-468 (third addendum): a holder of a seat pulled by kind, on a bus that holds one queue's plain worker and lacks + // the other's (live, the Node build module's install queue had none and pulled nothing). + b.manifestOf("go-builder", `{"module":"go-builder","version":"1","own-secrets":{"broker":"/var/lib/gb/broker"},"claims":[{"name":"node-build-go"}]}`) + b.assign("a", "go-builder") b.loaded = shadowOf(t, b) p := partOn(b) p.Create, p.Consumers = b, b @@ -2287,8 +2287,19 @@ func TestGrants(t *testing.T) { "node-build-go": {Seat: "node-build-go", PulledByKind: true, Accepts: []seatdef.Message{{Name: "check"}}}, "mesh-wants": {Seat: "mesh-wants", Accepts: []seatdef.Message{{Name: "step"}}}, } - b.consumers = map[string][]map[string]any{"SEAT_MESH_WANTS": {{"name": "SEAT_MESH_WANTS_worker", "filters": []string{"mesh.seat.mesh-wants.accept.>"}, "max_deliver": 9}}} + b.consumers = map[string][]map[string]any{"SEAT_MESH_WANTS": {{"name": "SEAT_MESH_WANTS_worker", "filters": []string{"mesh.seat.mesh-wants.accept.>"}, "max_deliver": 9}}, + "SEAT_NODE_BUILD_GO": {{"name": "SEAT_NODE_BUILD_GO_CHECK_worker", "filters": []string{"mesh.seat.node-build-go.accept.check"}, "max_deliver": 9}}} res := p.Compose() + if !slices.Contains(res.Consumers.Created, "SEAT_NODE_BUILD_GO.SEAT_NODE_BUILD_GO_DELIVERY_worker") || + !slices.Contains(res.Consumers.Created, "SEAT_NODE_BUILD_GO.SEAT_NODE_BUILD_GO_RECHECK_worker") || + slices.Contains(res.Consumers.Created, "SEAT_NODE_BUILD_GO.SEAT_NODE_BUILD_GO_CHECK_worker") { + t.Errorf("a pulled-by-kind seat's per-queue workers: created %v, want each queue's the bus lacks, the held check worker not", res.Consumers.Created) + } + for _, c := range b.consumers["SEAT_NODE_BUILD_GO"] { + if c["name"] == "SEAT_NODE_BUILD_GO_CHECK_worker" && c["max_deliver"] != 9 { + t.Errorf("the held check worker was changed: %v", c) + } + } var made map[string]any for _, c := range b.consumers["SEAT_MESH_BUILDS"] { if c["name"] == "SEAT_MESH_BUILDS_worker" { @@ -2306,7 +2317,7 @@ func TestGrants(t *testing.T) { t.Errorf("the wants seat's held worker was changed: %v", b.consumers["SEAT_MESH_WANTS"]) } p.Compose() - if len(b.consumers["SEAT_MESH_BUILDS"]) != 1 || len(b.consumers["SEAT_MESH_WANTS"]) != 1 { + if len(b.consumers["SEAT_MESH_BUILDS"]) != 1 || len(b.consumers["SEAT_MESH_WANTS"]) != 1 || len(b.consumers["SEAT_NODE_BUILD_GO"]) != 3 { t.Errorf("a pass changed the seats' workers: %v", b.consumers) } }) diff --git a/modules/mesh-grants/internal/buscompose/derived.go b/modules/mesh-grants/internal/buscompose/derived.go index 1c7f05b76..18955bd9e 100644 --- a/modules/mesh-grants/internal/buscompose/derived.go +++ b/modules/mesh-grants/internal/buscompose/derived.go @@ -286,13 +286,21 @@ func AccountConsumers(users []Principal) []ModuleConsumer { out = append(out, mc) } } - // A build seat's named workers (design 47 G §544, A-413): the seat traffic of each composed holder names them, and the part makes - // them, never only names them in a membership. Main's workers are held already; only the named ones are made here. + // A build seat's workers (design 47 G §544, A-413; H2 H-A-468, third addendum): the seat traffic of each composed holder + // names them, each queue's plain worker (SEAT___worker) and its named one, and the part makes them where the + // bus lacks them, never only names them in a membership. "Main's workers are held already" held only while the + // controller made them: a queue added since had none. made := map[string]bool{} for _, p := range users { + byKind := map[string]bool{} + for _, s := range p.Holds { + if s.PulledByKind { + byKind[seatStreamName(s.Name)] = true + } + } _, cs := SeatTrafficObjects([]Principal{p}) for _, c := range cs { - if !strings.HasSuffix(c.Name, "_named_worker") || made[c.Stream+"."+c.Name] { + if !(byKind[c.Stream] || strings.HasSuffix(c.Name, "_named_worker")) || made[c.Stream+"."+c.Name] { continue } made[c.Stream+"."+c.Name] = true diff --git a/modules/mesh-grants/internal/buscompose/named_asks_test.go b/modules/mesh-grants/internal/buscompose/named_asks_test.go index 4cb4c0273..42b57a374 100644 --- a/modules/mesh-grants/internal/buscompose/named_asks_test.go +++ b/modules/mesh-grants/internal/buscompose/named_asks_test.go @@ -62,14 +62,18 @@ func TestNamedAsks(t *testing.T) { t.Errorf("the work queue's consumers: %d, want 4 (a main and a named worker for each of two queues)", len(consumers)) } // A-413: the part makes the named workers (AccountConsumers), never only names them in a membership. - named := 0 + // H-A-468 (third addendum): and each queue's plain worker, which a queue added since the controller ended had none of. + named, plain := 0, 0 for _, mc := range AccountConsumers([]Principal{{Kind: KindModule, Node: "one", Module: "go-module", Holds: []Seat{held}}}) { - if strings.HasSuffix(mc.Consumer.Name, "_named_worker") { + switch { + case strings.HasSuffix(mc.Consumer.Name, "_named_worker"): named++ + case mc.Consumer.Name == "SEAT_NODE_BUILD_GO_CHECK_worker" || mc.Consumer.Name == "SEAT_NODE_BUILD_GO_DELIVERY_worker": + plain++ } } - if named != 2 { - t.Errorf("account consumers hold %d named workers, want 2", named) + if named != 2 || plain != 2 { + t.Errorf("account consumers hold %d named and %d plain workers, want 2 and 2", named, plain) } } diff --git a/modules/mesh-grants/internal/grants/assertions.go b/modules/mesh-grants/internal/grants/assertions.go index d3fb0dc78..3d37f0797 100644 --- a/modules/mesh-grants/internal/grants/assertions.go +++ b/modules/mesh-grants/internal/grants/assertions.go @@ -62,8 +62,9 @@ const CreatesMissing = "a bucket, log or stream the manifest declares and the bu // 08:54). A stream the bus holds already answers ErrHeld. type Creator interface { CreateDeclared(cfg jetstream.StreamConfig) error - // AddSubjects adds subjects to a stream the bus holds and nothing else: the stream's subjects become the union of what - // it holds and add; no subject is removed, no limit, retention or other setting changed, the stream not recreated. + // AddSubjects adds subjects to a stream the bus holds and nothing else: the stream's subjects become WidenSubjects of + // what it holds and add, a held subject an added one covers replaced by it (design 47 H H-A-470, third addendum) and + // no other removed; no limit, retention or other setting changed, the stream not recreated. AddSubjects(stream string, add []string) error // RaiseMaxBytes raises the size cap of a stream the bus holds to `to` when `to` is larger than the cap it holds, and // changes nothing else (the operator, 2026-11-09 09:07: "raise only"). It answers the cap it held and whether it @@ -208,7 +209,8 @@ func createMissing(c Creator, done *creations, a *Assertion, m catalogue.Manifes } } // A stream the bus holds whose manifest names events it has no subject for gains those subjects, and only ever those: a - // subject the manifest dropped stays, limits and retention are left as they are (the operator, 2026-11-05). + // subject the manifest dropped stays, limits and retention are left as they are (the operator, 2026-11-05). A held + // subject a gained one covers is replaced by it, nothing dropped (design 47 H H-A-470, third addendum). for i := range a.Items { it := &a.Items[i] if it.Held == nil || it.Kind != "stream" { @@ -234,7 +236,14 @@ func createMissing(c Creator, done *creations, a *Assertion, m catalogue.Manifes } done.cleared(it.Stream) it.Created = "subjects added: " + strings.Join(add, ", ") - made = append(made, "stream "+it.Stream+" gained "+strings.Join(add, ", ")) + gained := "stream " + it.Stream + " gained " + strings.Join(add, ", ") + // A held subject an added one covers is replaced by it, and said (design 47 H H-A-470, third addendum). + if _, replaced := WidenSubjects(it.Held.Subjects, add); len(replaced) > 0 { + why := " (replacing " + strings.Join(replaced, ", ") + ", which it covers: every message kept)" + it.Created += why + gained += why + } + made = append(made, gained) } // A held stream, of any kind, whose manifest declares a larger size cap gets that cap, and no other limit changes: a // cap the bus holds that is larger or equal stays (the operator, 2026-11-09 09:07). Compared once per manifest. diff --git a/modules/mesh-grants/internal/grants/grants.go b/modules/mesh-grants/internal/grants/grants.go index 252b3f2bf..636df6417 100644 --- a/modules/mesh-grants/internal/grants/grants.go +++ b/modules/mesh-grants/internal/grants/grants.go @@ -93,16 +93,37 @@ func GrantWaits(accounts []string, composed, loaded map[string]User, unknown str func notAllowed(a, b []string) []string { var out []string for _, x := range a { - if !slices.ContainsFunc(b, func(y string) bool { return subjectCovers(y, x) }) { + if !slices.ContainsFunc(b, func(y string) bool { return SubjectCovers(y, x) }) { out = append(out, x) } } return out } -// subjectCovers says whether the permission pattern p allows every subject the permission s allows, by the bus's wildcards: -// `*` one token, `>` one or more tokens at the end. -func subjectCovers(p, s string) bool { +// WidenSubjects is a held stream's subjects once add is given them: each subject of add the stream lacks is added, and a +// held subject an added one covers is replaced by it, since the bus refuses a subject overlapping one the stream holds +// (JetStream 10052) and the held subject's every message still matches the added one (design 47 H H-A-470, third +// addendum). No other held subject is removed. It answers the held subjects it replaced. +func WidenSubjects(held, add []string) (subjects, replaced []string) { + for _, h := range held { + if slices.ContainsFunc(add, func(a string) bool { return a != h && SubjectCovers(a, h) }) { + replaced = append(replaced, h) + continue + } + subjects = append(subjects, h) + } + for _, a := range add { + if !slices.Contains(subjects, a) { + subjects = append(subjects, a) + } + } + return subjects, replaced +} + +// SubjectCovers says whether the permission pattern p allows every subject the permission s allows, by the bus's wildcards: +// `*` one token, `>` one or more tokens at the end. It also +// says whether a stream subject p takes every message a held subject s takes (design 47 H H-A-470, third addendum). +func SubjectCovers(p, s string) bool { pt, st := strings.Split(p, "."), strings.Split(s, ".") for i, t := range pt { if t == ">" { diff --git a/modules/mesh-grants/internal/grants/grantwaits_test.go b/modules/mesh-grants/internal/grants/grantwaits_test.go index 71cd28882..96b3a2ada 100644 --- a/modules/mesh-grants/internal/grants/grantwaits_test.go +++ b/modules/mesh-grants/internal/grants/grantwaits_test.go @@ -25,8 +25,8 @@ func TestAGrantWaitsOnlyForWhatTheLoadedListDoesNotAllow(t *testing.T) { p, s string want bool }{{">", "a", true}, {">", "a.>", true}, {"a.>", "a", false}, {"a.*", "a.>", false}, {"a.*", "a.*", true}, {"a.b", "a.b", true}, {"a.b", "a.b.c", false}} { - if got := subjectCovers(c.p, c.s); got != c.want { - t.Errorf("subjectCovers(%q, %q) = %v, want %v", c.p, c.s, got, c.want) + if got := SubjectCovers(c.p, c.s); got != c.want { + t.Errorf("SubjectCovers(%q, %q) = %v, want %v", c.p, c.s, got, c.want) } } } diff --git a/modules/mesh-grants/internal/grants/statecap_test.go b/modules/mesh-grants/internal/grants/statecap_test.go index cf811ccee..08512d8dd 100644 --- a/modules/mesh-grants/internal/grants/statecap_test.go +++ b/modules/mesh-grants/internal/grants/statecap_test.go @@ -1,6 +1,8 @@ package grants import ( + "slices" + "strings" "testing" "git.novox.be/novox/mesh-sdk/go/catalogue" @@ -38,3 +40,31 @@ func TestAStatesDeclaredCapReachesItsBucket(t *testing.T) { t.Errorf("the held bucket was raised to %d (%v), want 256 MiB", r.to, raises) } } + +// adder is a Creator that records the subjects it is asked to add to a held stream. +type adder struct { + Creator + add []string +} + +func (a *adder) AddSubjects(_ string, add []string) error { a.add = add; return nil } + +// A held subject the composed one covers is replaced by it, and the assertion says so (design 47 H H-A-470, third +// addendum: the bus refuses `registration.>` beside `registration.*`, JetStream 10052); no other subject is dropped. +func TestAHeldSubjectTheComposedOneCoversIsReplaced(t *testing.T) { + held, replaced := WidenSubjects([]string{"m.registration.*", "kept.subject"}, []string{"m.registration.>"}) + if !slices.Equal(held, []string{"kept.subject", "m.registration.>"}) || !slices.Equal(replaced, []string{"m.registration.*"}) { + t.Errorf("widened to %v replacing %v", held, replaced) + } + if held, replaced := WidenSubjects([]string{"a.>"}, []string{"a.>", "b.*"}); !slices.Equal(held, []string{"a.>", "b.*"}) || replaced != nil { + t.Errorf("an equal or uncovered subject: %v replacing %v", held, replaced) + } + a := Assertion{Manifest: "d", Items: []Item{{Kind: "stream", Stream: "LOG_m_registration-history", + Composed: Limits{Subjects: []string{"m.registration.>"}}, Held: &Limits{Subjects: []string{"m.registration.*"}}}}} + c := &adder{} + made, _ := createMissing(c, &creations{}, &a, catalogue.Manifest{Module: "m"}, nil, false) + const said = "replacing m.registration.*, which it covers" + if !slices.Equal(c.add, []string{"m.registration.>"}) || len(made) != 1 || !strings.Contains(made[0], said) || !strings.Contains(a.Items[0].Created, said) { + t.Errorf("added %v, said %v and %q: want the replacement said", c.add, made, a.Items[0].Created) + } +}