diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go index 8ce3d06ed..5caf84658 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go @@ -19,6 +19,7 @@ import ( "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" 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/gopackageregistry" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-sdk/go/process" @@ -492,4 +493,435 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) { t.Errorf("the health four minutes in = %+v, want healthy", a) } }) + // Design 47 H2 H-A-613 (4), H-A-620 (3), H-A-593 (5), H-A-588 to H-A-590 (whole, against the bus and a fake registry holder): the + // builder's reserving, its publish items and its work queue as main joins them, the publish a kept call through a process + // connection to a holder on the bus, which answers `versions` with each version's digest and kept fingerprint and `publish` as the + // seats do (held, other bytes, a refusal). A publish met by no responders is called again and passes once the holder answers; a + // refused one is ended and waits for the next running; a build whose two of three artifacts a holder holds for its fingerprint + // stores the third and releases the same number; the same with no fingerprint kept withdraws the number, builds again and releases + // the next number, clearing version-held-elsewhere; other bytes of another build end the item at once. + t.Run("a publish is a kept call and a registry's record is read", func(t *testing.T) { + const holder, fpOf = "goreg", "src2:" + reg := &fakeRegistry{held: map[string]map[string]heldRecord{}, away: true, firstAway: make(chan struct{}, 1)} + reg.listen(t, b.js.Conn()) + conn, err := process.Connect(process.Account{URL: b.srvURL, Module: "mesh-delivery-builder", User: "ctl", Password: "pw"}) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + entries := map[string]meshcatalog.CatalogEntry{} + entryOf := func(module, fp string, arts ...string) { + e := meshcatalog.CatalogEntry{Module: module, Fp: fp} + for _, a := range arts { + e.Artifacts = append(e.Artifacts, meshcatalog.Artifact{Ref: module + ":" + a, Kind: versions.KindPackage, Language: "go", + Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "x.example/" + module + "/" + a}}) + } + entries[fp] = e + } + call := func(string, any) (json.RawMessage, error) { + return json.Marshal(meshcatalog.VersionsAnswer{Module: "m"}) + } + found := &memFindings{vals: map[string][]byte{}} + rv := newReservations(&lockedReserve{memReserve: &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}}, call, + func() jetstream.KeyValue { return nil }, + &findings{module: builds.BuilderModule, w: found, logf: t.Logf, now: time.Now}, t.Logf) + rv.say = func(context.Context, meshbuilds.PackagePublished) error { return nil } + pq := &publishQueue{js: b.js, ackWait: time.Minute, logf: t.Logf, binding: publishesOf("mesh-delivery-builder")} + republish := &versions.Republisher{Reserved: rv.r, Put: pq.put, Logf: t.Logf, Started: time.Now(), + Entry: func(_ context.Context, fp string) (meshcatalog.CatalogEntry, error) { + if e, ok := entries[fp]; ok { + return e, nil + } + return meshcatalog.CatalogEntry{}, fmt.Errorf("no entry %s", fp) + }, + Send: sendVia(conn.CallKeptAsk, publishUnanswered(ctx, &findings{module: builds.BuilderModule, w: found, logf: t.Logf, now: time.Now})), + Unreleased: rv.unreleasedWords, Holders: func(string) []string { return []string{holder} }} + rv.r.HeldRecords = func(ctx context.Context, module, fp string, refs []string) ([]versions.HeldArtifact, error) { + return republish.HeldRecords(ctx, module, fp, refs, registryVersionsVia(conn.Call)) + } + rv.r.MovedFrom = rv.movedFrom + republish.Unstored = rv.unstoredAgain + republish.AsHeld = rv.sayPublished + rv.judge = func(ctx context.Context, v versions.ReservedVersion) (versions.Judged, error) { + return republish.Held(ctx, v, registryVersionsVia(conn.Call)) + } + rv.r.HeldElsewhere = func(ctx context.Context, module string, skipped []string) { + rv.heldElsewhere(ctx, module, "the registries hold "+strings.Join(skipped, ", ")+", so the reserve skipped them") + } + items := &publishItems{now: time.Now, logf: t.Logf, unreleased: republish.Unreleased, republish: republish.Republish, + dropMark: func(context.Context, string, string, string, string) error { return nil }, + reserved: func(ctx context.Context, module, version string) (versions.ReservedVersion, bool, error) { + return rv.r.Of(ctx, module, version) + }, + judge: rv.judge, markHeld: rv.markHeld, moveElsewhere: rv.movedElsewhere, send: republish.SendQueued} + republish.Ended = items.Ended + pq.take = items.take + wirePublishItems(rv, items, &lockedReserve{memReserve: &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}}, versions.DefaultReleasedKept) + // build is the reserve and the end of a build of module at fp: its digests are built+artifact. + build := func(module, fp, built string, arts ...string) versions.ReservedVersion { + t.Helper() + var refs []string + digests := map[string]string{} + for _, a := range arts { + refs = append(refs, module+":"+a) + digests[module+":"+a] = "sha256:" + built + a + } + res, err := rv.Reserve(ctx, module, "0.1", refs, versions.Source{Fingerprint: fp}) + if err != nil { + t.Fatal(err) + } + if err := rv.Built(ctx, module, res.Version, digests, nil); err != nil { + t.Fatal(err) + } + return res + } + stored := func(module, version, a string) string { return reg.digest("x.example/"+module+"/"+a, version) } + + // H-A-613 (4): the first call finds no responders; the holder answers, and the publish passes, called again by itself. + entryOf("kept", fpOf+"k", "a") + build("kept", fpOf+"k", "k", "a") + select { + case <-reg.firstAway: + case <-time.After(testbus.SetupBound(t)): + t.Fatal("the publish was not called") + } + reg.answer() + reg.await(t, "x.example/kept/a", "0.1.0") + if got := reg.callsOf("x.example/kept/a"); got < 2 { + t.Errorf("the publish was called %d times, want it met by no responders and called again", got) + } + reg.published(t, rv, "x.example/kept/a", "0.1.0") // its publish heard: the item is acknowledged + reg.settle(t, b.js, bind.Stream, holder, func() { items.heard(ctx, "kept", "0.1.0") }) + + // H-A-613 (4): an answered refusal ends the send; nothing calls again; the next running puts it again and it passes. + entryOf("refused", fpOf+"r", "a") + reg.refuse("x.example/refused/a", "the cap") + build("refused", fpOf+"r", "r", "a") + reg.await(t, "x.example/refused/a", "0.1.0") + reg.settle(t, b.js, bind.Stream, holder, nil) + if got := reg.callsOf("x.example/refused/a"); got != 1 { + t.Errorf("the refused publish was called %d times, want once: it waits for the next running", got) + } + reg.refuse("x.example/refused/a", "") + republish.Started = time.Now() // the holder's next running, in a run later than the build's: its put is of the start class, another message id + if _, err := republish.Republish(ctx, "refused", "0.1.0"); err != nil { + t.Fatal(err) + } + reg.await(t, "x.example/refused/a", "0.1.0") + if got := stored("refused", "0.1.0", "a"); got != "sha256:ra" { + t.Errorf("the refused publish put again stored %q, want sha256:ra", got) + } + reg.published(t, rv, "x.example/refused/a", "0.1.0") + reg.settle(t, b.js, bind.Stream, holder, func() { items.heard(ctx, "refused", "0.1.0") }) + + // H-A-593 (5): a build of F publishes two of three artifacts at V and fails on the third (the holder refuses it). Built again, the + // two held for F keep their held digests, the third is stored and V is released, with no refusal and no new number. + entryOf("three", fpOf+"3", "a", "b", "c") + reg.refuse("x.example/three/c", "the cap") + res := build("three", fpOf+"3", "first-", "a", "b", "c") + reg.await(t, "x.example/three/c", res.Version) + reg.settle(t, b.js, bind.Stream, holder, nil) + for _, a := range []string{"a", "b"} { + if got := stored("three", res.Version, a); got != "sha256:first-"+a { + t.Fatalf("%s of three at %s is held at %q, want what the first run stored", a, res.Version, got) + } + } + reg.refuse("x.example/three/c", "") + republish.Started = time.Now().Add(time.Hour) // the build again is of a run before the next: its put is of the start class + again, err := rv.Reserve(ctx, "three", "0.1", []string{"three:a", "three:b", "three:c"}, versions.Source{Fingerprint: fpOf + "3"}) + if err != nil || again.Version != res.Version { + t.Fatalf("the build of three again reserved %q (%v), want the standing %s", again.Version, err, res.Version) + } + rv.MarkRegistryHeld(ctx, again) // before a build module is asked again (H-A-588 (4)) + if err := rv.Built(ctx, "three", res.Version, map[string]string{"three:c": "sha256:again-c"}, nil); err != nil { + t.Fatal(err) + } + reg.await(t, "x.example/three/c", res.Version) + reg.published(t, rv, "x.example/three/c", res.Version) + reg.settle(t, b.js, bind.Stream, holder, func() { items.heard(ctx, "three", res.Version) }) + cur, ok, _ := rv.r.Of(ctx, "three", "") + if !ok || cur.Version != res.Version || len(cur.Withdrawn) != 0 || len(cur.Missing()) != 0 || cur.Artifacts["three:a"] != "sha256:first-a" || cur.Artifacts["three:c"] != "sha256:again-c" { + t.Errorf("three stands %+v, want %s with every artifact published, the held digests of a and b and the third stored, and no number withdrawn", cur, res.Version) + } + if got := reg.callsOf("x.example/three/a"); got != 1 { + t.Errorf("a of three was published %d times, want once: the second run answered it from the registry's record", got) + } + + // H-A-593 (5): the same with no fingerprint kept withdraws V, builds again and releases the next number, clearing + // version-held-elsewhere at that release. + entryOf("bare", fpOf+"b", "a", "b", "c") + rv.rebuild = func(ctx context.Context, module, fp, _ string) error { + var refs []string + digests := map[string]string{} + for _, a := range []string{"a", "b", "c"} { + refs = append(refs, module+":"+a) + digests[module+":"+a] = "sha256:next-" + a + } + again, err := rv.Reserve(ctx, module, "0.1", refs, versions.Source{Fingerprint: fp, Rebuild: true}) + if err != nil { + return err + } + return rv.Built(ctx, module, again.Version, digests, nil) + } + first := func() versions.ReservedVersion { + res, err := rv.Reserve(ctx, "bare", "0.1", []string{"bare:a", "bare:b", "bare:c"}, versions.Source{Fingerprint: fpOf + "b"}) + if err != nil { + t.Fatal(err) + } + return res + }() + reg.hold("x.example/bare/a", first.Version, "sha256:old-a", "") + reg.hold("x.example/bare/b", first.Version, "sha256:old-b", "") + if err := rv.Built(ctx, "bare", first.Version, map[string]string{"bare:a": "sha256:mine-a", "bare:b": "sha256:mine-b", "bare:c": "sha256:mine-c"}, nil); err != nil { + t.Fatal(err) + } + reg.await(t, "x.example/bare/c", "0.1.1") + raised := func() bool { + for k := range found.vals { + if strings.Contains(k, versions.VersionHeldElsewhere) && strings.Contains(k, "bare") { + return true + } + } + return false + } + if !raised() { + t.Errorf("no version-held-elsewhere raised about bare: %v", found.vals) + } + if got := reg.digest("x.example/bare/a", first.Version); got != "sha256:old-a" { + t.Errorf("a of bare at %s is held at %q, want what the holder held, never overwritten", first.Version, got) + } + for _, a := range []string{"a", "b", "c"} { + reg.published(t, rv, "x.example/bare/"+a, "0.1.1") + } + if err := rv.release(ctx, meshcatalog.VersionReleasedEvent{V: 1, Module: "bare", Version: "0.1.1"}); err != nil { + t.Fatal(err) + } + if raised() { + t.Errorf("version-held-elsewhere still raised about bare after its release: %v", found.vals) + } + reg.settle(t, b.js, bind.Stream, holder, func() { items.heard(ctx, "bare", "0.1.1") }) + + // H-A-588, H-A-590: a holder answering other bytes of another build ends the item at once, its tries unspent: it is called once. + entryOf("other", fpOf+"o", "a") + res, err = rv.Reserve(ctx, "other", "0.1", []string{"other:a"}, versions.Source{Fingerprint: fpOf + "o"}) + if err != nil { + t.Fatal(err) + } + reg.hold("x.example/other/a", res.Version, "sha256:theirs", fpOf+"another") + reg.hide("x.example/other/a") // the record is not read, so the item is sent and the holder answers other bytes (H-A-729) + if err := rv.Built(ctx, "other", res.Version, map[string]string{"other:a": "sha256:mine"}, nil); err != nil { + t.Fatal(err) + } + reg.await(t, "x.example/other/a", res.Version) + reg.settle(t, b.js, bind.Stream, holder, nil) + if got := reg.callsOf("x.example/other/a"); got != 1 { + t.Errorf("other bytes of another build: called %d times, want once", got) + } + }) +} + +// lockedReserve is the unit tests' reservations store, safe for the pull goroutines of a queue and a test at once. +type lockedReserve struct { + mu sync.Mutex + *memReserve +} + +func (l *lockedReserve) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) { + l.mu.Lock() + defer l.mu.Unlock() + return l.memReserve.Get(ctx, key) +} +func (l *lockedReserve) Create(ctx context.Context, key string, v []byte) (uint64, error) { + l.mu.Lock() + defer l.mu.Unlock() + return l.memReserve.Create(ctx, key, v) +} +func (l *lockedReserve) Update(ctx context.Context, key string, v []byte, rev uint64) (uint64, error) { + l.mu.Lock() + defer l.mu.Unlock() + return l.memReserve.Update(ctx, key, v, rev) +} +func (l *lockedReserve) KeysOf(ctx context.Context, filter string) ([]string, error) { + l.mu.Lock() + defer l.mu.Unlock() + return l.memReserve.KeysOf(ctx, filter) +} +func (l *lockedReserve) Delete(ctx context.Context, key string) error { + l.mu.Lock() + defer l.mu.Unlock() + return l.memReserve.Delete(ctx, key) +} + +// heldRecord is what a fake registry holder keeps of one name at one version: its digest and the fingerprint it keeps (none: ""). +type heldRecord struct{ digest, fingerprint string } + +// fakeRegistry is a registry seat's holder on the bus (the seat go-package-registry): it answers `versions` with each version's digest +// and kept fingerprint, and `publish` as the seats do: stored when it holds nothing there, held for the same digest, other bytes +// (with the kept fingerprint) for another digest, or a refusal in words. While away it answers as the bus does when no one is +// listening (a 503 status without data), which the caller meets as no responders. +type fakeRegistry struct { + mu sync.Mutex + held map[string]map[string]heldRecord // name -> version -> record + away bool + firstAway chan struct{} + refused map[string]string // name -> the words it is refused in + hidden map[string]bool + calls map[string]int + answered chan string // " " of each publish answered, not away +} + +func (r *fakeRegistry) listen(t *testing.T, nc *nats.Conn) { + t.Helper() + r.answered = make(chan string, 64) + sub, err := nc.Subscribe("mesh.seat."+versions.SeatGoPackages+".call.>", func(m *nats.Msg) { + reply := func(result any, words string) { + data, _ := json.Marshal(map[string]any{"result": result, "error": words, "node": "ctl"}) + _ = m.Respond(data) + } + switch strings.Split(m.Subject, ".")[4] { + case "versions": + var in struct{ Name string } + _ = json.Unmarshal(m.Data, &in) + r.mu.Lock() + list := []map[string]string{} + if !r.hidden[in.Name] { + for v, rec := range r.held[in.Name] { + list = append(list, map[string]string{"version": v, "digest": rec.digest, "fingerprint": rec.fingerprint}) + } + } + r.mu.Unlock() + reply(map[string]any{"versions": list}, "") + case "publish": + var in gopackageregistry.PublishInput + _ = json.Unmarshal(m.Data, &in) + r.mu.Lock() + if r.calls == nil { + r.calls = map[string]int{} + } + r.calls[in.Name]++ + if r.away { + r.mu.Unlock() + select { + case r.firstAway <- struct{}{}: + default: + } + _ = m.RespondMsg(&nats.Msg{Header: nats.Header{"Status": {"503"}}}) + return + } + var answer gopackageregistry.PublishAnswer + words := r.refused[in.Name] + if rec, ok := r.held[in.Name][in.Version]; words == "" && ok { + if rec.digest == in.Digest { + answer.Held = true + } else { + answer.OtherBytes = &gopackageregistry.PublishAnswerOtherBytes{Digest: rec.digest, Fingerprint: rec.fingerprint} + } + } else if words == "" { + if r.held[in.Name] == nil { + r.held[in.Name] = map[string]heldRecord{} + } + r.held[in.Name][in.Version] = heldRecord{digest: in.Digest, fingerprint: in.Fingerprint} + answer.Transfer = "t" + } + r.mu.Unlock() + reply(answer, words) + r.answered <- in.Name + " " + in.Version + } + }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = sub.Unsubscribe() }) +} + +func (r *fakeRegistry) answer() { r.mu.Lock(); r.away = false; r.mu.Unlock() } + +func (r *fakeRegistry) refuse(name, words string) { + r.mu.Lock() + defer r.mu.Unlock() + if r.refused == nil { + r.refused = map[string]string{} + } + if words == "" { + delete(r.refused, name) + return + } + r.refused[name] = words +} + +func (r *fakeRegistry) hide(name string) { + r.mu.Lock() + defer r.mu.Unlock() + if r.hidden == nil { + r.hidden = map[string]bool{} + } + r.hidden[name] = true +} + +func (r *fakeRegistry) hold(name, version, digest, fingerprint string) { + r.mu.Lock() + defer r.mu.Unlock() + if r.held[name] == nil { + r.held[name] = map[string]heldRecord{} + } + r.held[name][version] = heldRecord{digest: digest, fingerprint: fingerprint} +} + +func (r *fakeRegistry) digest(name, version string) string { + r.mu.Lock() + defer r.mu.Unlock() + return r.held[name][version].digest +} + +func (r *fakeRegistry) callsOf(name string) int { + r.mu.Lock() + defer r.mu.Unlock() + return r.calls[name] +} + +// await reads the publishes answered until name's at version, within the level's set-up share. +func (r *fakeRegistry) await(t *testing.T, name, version string) { + t.Helper() + bound := time.After(testbus.SetupBound(t)) + for { + select { + case got := <-r.answered: + if got == name+" "+version { + return + } + case <-bound: + t.Fatalf("no publish of %s %s was answered", name, version) + } + } +} + +// settle waits until the holder's queue holds nothing sent and unacknowledged: every item taken is ended. +func (r *fakeRegistry) settle(t *testing.T, js jetstream.JetStream, stream string, holder string, each func()) { + t.Helper() + c, err := js.Consumer(context.Background(), stream, publishesConsumer(holder, versions.PublishClassLive)) + if err != nil { + t.Fatal(err) + } + for deadline := time.Now().Add(testbus.SetupBound(t)); ; { + if each != nil { + each() + } + info, err := c.Info(context.Background()) + if err != nil { + t.Fatal(err) + } + if info.NumAckPending == 0 && info.NumPending == 0 { + return + } + if time.Now().After(deadline) { + t.Fatalf("the queue of %s holds %d unacknowledged and %d pending", holder, info.NumAckPending, info.NumPending) + } + time.Sleep(10 * time.Millisecond) + } +} + +// published says the holder's published of name at version, as it says it after a publish: the reservation marks it from the digest held. +func (r *fakeRegistry) published(t *testing.T, rv *reservations, name, version string) { + t.Helper() + rv.published(context.Background(), versions.Outcome{Name: name, Version: version, Digest: r.digest(name, version), Kind: versions.KindPackage, Holder: "go-registry"}, nil) }