Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/imports_test.go
T
jschoubben dc4f1dff39 Indexer: the start replay keeps the view once, and only when a build end wrote
lead answer A-391: the start replay keeps the view once (measured: 50 min CPU per start).
applyBuildEnd reports whether it wrote; ApplyBuildEnd keeps the view only then;
ApplyBuildEndsAtStart applies every entry and keeps the view once if any wrote.

Builds: design 47 G lead answer A-391

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-14 04:04:45 +01:00

450 lines
20 KiB
Go

package index
import (
"context"
"errors"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker"
"git.novox.be/novox/mesh-sdk/go/process"
)
const meshSDK = "git.novox.be/novox/mesh-sdk/go"
// Full commits of mesh-sdk and of the catalogue.
var (
c1 = strings.Repeat("1", 40)
c2 = strings.Repeat("2", 40)
c3 = strings.Repeat("3", 40)
cat1 = strings.Repeat("a", 40)
)
// sdkFiles is a listing of mesh-sdk's go directory (gitea_commit_trees with files): announce and buildstep at the
// given blobs.
func sdkFiles(announce, buildstep string) map[string]string {
return map[string]string{"go.mod": "m1", "go.sum": "s1", "stdio.go": "r1", "announce/announce.go": announce, "buildstep/step.go": buildstep}
}
// forge answers Files from the listing of each commit of mesh-sdk.
type forge map[string]map[string]string
func (f forge) files(_ context.Context, repository, sha string, dirs []string) (map[string]map[string]string, error) {
out := map[string]map[string]string{}
for _, d := range dirs {
if repository != "novox/mesh-sdk" || d != "go" || f[sha] == nil {
return nil, &missingDir{d}
}
out[d] = f[sha]
}
return out, nil
}
type missingDir struct{ dir string }
func (m *missingDir) Error() string { return "no directory " + m.dir }
// ADR 0593 §2-§4 (D1, D2, D3): a build's imports are kept with no fingerprint moving; a mesh-sdk merge that changes
// a package the module does not import leaves it; one that changes an imported package moves it to the import form;
// and the packages state holds one key with a digest per package.
func TestImportsReportedMoveTheFingerprintOnlyWithAnImportedPackage(t *testing.T) {
t.Run("a build of an older fingerprint keeps no imports", buildOfAnOlderFingerprintKeepsNoImports)
t.Run("a change of a sibling directory alone moves its importer", aSiblingDirectoryAloneMovesItsImporter)
t.Run("an unread packages state writes no fingerprint, and they are computed again by hand (issue 1966)", anUnreadPackagesStateWritesNoFingerprintAndFingerprintsAreComputedAgain)
t.Run("an accepted series' newest published version pins it, by that version's contents", anAcceptedSeriesPinsItsNewest)
ctx := context.Background()
ix, mods := newFormIndexer(t, mod{"avahi", catalogueManifest(t, "avahi")})
reportToolchain(t, ix, "mesh-build-go", "novox", "go", "go1.27.1")
listings := forge{c1: sdkFiles("a1", "b1"), c2: sdkFiles("a1", "b2"), c3: sdkFiles("a2", "b2")}
ix.Packages, ix.Files = newMem(), listings.files
// avahi's go.sum names mesh-sdk (the version line), as every Go module's does; mesh-sdk's module is held at c1.
setPackages(t, mods, "avahi", []string{meshSDK})
sdkModule, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: "novox/mesh-sdk", Path: ".", Commit: c1, None2: "a TypeScript package"})
if err != nil {
t.Fatal(err)
}
mods.put("mesh-sdk", sdkModule)
held, _ := ix.heldModules(ctx)
if err := ix.publish(ctx, "novox/mesh-sdk", c1, held, false); err != nil {
t.Fatal(err)
}
pub := ix.publishedAll(ctx)[meshSDK]
if pub.Commit != c1 || len(pub.Packages) != 3 || pub.Requires == "" {
t.Fatalf("packages.%s = %+v, want commit c1 and a digest per package (go, announce, buildstep)", meshSDK, pub)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
before, _ := moduleOf(t, mods, "avahi")
if before.Fp2 == "" {
t.Fatalf("no fingerprint to start from: %q", before.None2)
}
// The build of that fingerprint ends, reporting its imports: kept, and the fingerprint does not move.
end := buildEnd(t, "avahi", before.Fp2, meshSDK, meshSDK+"/announce")
if err := ix.ApplyBuildEnd(ctx, end); err != nil {
t.Fatal(err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
after, _ := moduleOf(t, mods, "avahi")
if after.Fp2 != before.Fp2 || strings.Join(after.Inputs2.Imports, ",") != meshSDK+","+meshSDK+"/announce" || after.Inputs2.ImportForm == "" {
t.Fatalf("after the build's imports: fp2 %q (was %q), imports %v, import form %q; want the same fingerprint and the imports kept",
after.Fp2, before.Fp2, after.Inputs2.Imports, after.Inputs2.ImportForm)
}
// mesh-sdk merges a change of buildstep only: avahi does not import it.
held, _ = ix.heldModules(ctx)
if err := ix.publish(ctx, "novox/mesh-sdk", c2, held, false); err != nil {
t.Fatal(err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
if m, _ := moduleOf(t, mods, "avahi"); m.Fp2 != before.Fp2 {
t.Fatalf("a mesh-sdk merge changing only buildstep moved avahi's fingerprint to %q", m.Fp2)
}
// mesh-sdk merges a change of announce: avahi moves, to the import form, pinned to the new version.
held, _ = ix.heldModules(ctx)
if err := ix.publish(ctx, "novox/mesh-sdk", c3, held, false); err != nil {
t.Fatal(err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
moved, _ := moduleOf(t, mods, "avahi")
if moved.Fp2 == before.Fp2 || moved.Fp2 != moved.Inputs2.ImportForm || moved.Packages[meshSDK] != c3 {
t.Fatalf("a change of an imported package: fp2 %q (was %q), import form %q, versions %v; want the import form at c3",
moved.Fp2, before.Fp2, moved.Inputs2.ImportForm, moved.Packages)
}
}
// A build of a fingerprint the module no longer holds says nothing of its inputs now: its imports are not kept.
func buildOfAnOlderFingerprintKeepsNoImports(t *testing.T) {
ctx := context.Background()
ix, mods := newFormIndexer(t, mod{"avahi", catalogueManifest(t, "avahi")})
reportToolchain(t, ix, "mesh-build-go", "novox", "go", "go1.27.1")
if err := ix.ApplyBuildEnd(ctx, buildEnd(t, "avahi", "src2:"+strings.Repeat("e", 64), meshSDK)); err != nil {
t.Fatal(err)
}
if m, _ := moduleOf(t, mods, "avahi"); m.Inputs2 == nil || len(m.Inputs2.Imports) != 0 {
t.Fatalf("imports %v kept from a build of another fingerprint", m.Inputs2)
}
}
// Review finding 2 (ADR 0593 §2 "never left stale", §3): avahi, with no reported import set, replaces a mesh Go module
// of its own repository by a directory (its version line names it). A merge of its repository that changes only that
// sibling's files moves avahi's fingerprint; one that changes nothing it reaches leaves it; its build is pinned to no
// version of the sibling.
func aSiblingDirectoryAloneMovesItsImporter(t *testing.T) {
const sibling = "git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
ctx := context.Background()
ix, mods := newFormIndexer(t, mod{"avahi", catalogueManifest(t, "avahi")})
reportToolchain(t, ix, "mesh-build-go", "novox", "go", "go1.27.1")
listing := map[string]map[string]string{
cat1: {"go.mod": "m1", "step.go": "s1"},
strings.Repeat("b", 40): {"go.mod": "m1", "step.go": "s2"}, // only the sibling's file changed
strings.Repeat("c", 40): {"go.mod": "m1", "step.go": "s2"}, // nothing it reaches changed
}
ix.Packages = newMem()
ix.Files = func(_ context.Context, repository, sha string, dirs []string) (map[string]map[string]string, error) {
out := map[string]map[string]string{}
for _, d := range dirs {
if repository != catalogueRepo || d != "modules/mesh-build-go/buildstep" || listing[sha] == nil {
return nil, &missingDir{d}
}
out[d] = listing[sha]
}
return out, nil
}
setPackages(t, mods, "avahi", []string{sibling})
fpAt := func(commit string) meshcatalog.ModulesByModuleValue {
t.Helper()
held, _ := ix.heldModules(ctx)
if err := ix.publish(ctx, catalogueRepo, commit, held, false); err != nil {
t.Fatal(err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
m, _ := moduleOf(t, mods, "avahi")
if m.Fp2 == "" {
t.Fatalf("no fingerprint at %s: %q", commit, m.None2)
}
return m
}
first := fpAt(cat1)
moved := fpAt(strings.Repeat("b", 40))
if moved.Fp2 == first.Fp2 {
t.Fatalf("a change of the sibling directory alone left avahi at %q", first.Fp2)
}
if again := fpAt(strings.Repeat("c", 40)); again.Fp2 != moved.Fp2 {
t.Errorf("a merge changing nothing avahi reaches moved it from %q to %q", moved.Fp2, again.Fp2)
}
if _, pinned := moved.Packages[sibling]; pinned {
t.Errorf("the build is pinned to a version of its sibling directory: %v", moved.Packages)
}
}
func buildEnd(t *testing.T, module, fp string, imports ...string) []byte {
t.Helper()
raw, err := builds.EncodeBuildLogEntry(builds.BuildLogEntry{Event: builds.BuildEndedEvent, Module: module, FP: fp,
Repository: catalogueRepo, Directory: "modules/" + module, Commit: cat1, Ref: "main", Node: "n1", At: time.Now(),
Outcome: builds.BuildDone, Imports: imports})
if err != nil {
t.Fatal(err)
}
return raw
}
// setPackages writes a module's version-line packages, as a merge reading its go.sum keeps them.
func setPackages(t *testing.T, mods *memStore, module string, packages []string) {
t.Helper()
m, _ := moduleOf(t, mods, module)
in := *m.Inputs2
in.Packages = packages
m.Inputs2 = &in
raw, err := meshcatalogclient.EncodeModule(m)
if err != nil {
t.Fatal(err)
}
mods.put(module, raw)
}
// DLV-I6 (ADR 0606 §4, ADR 0609 §1; design 32 *A package, its series, and the series its users accept*): avahi accepts
// mesh-sdk/go's default series. Once a seat publishes 0.1.15 from c3 (announce changed), avahi is pinned to v0.1.15 and
// fingerprinted by c3's digests; a publish of another series and a later trunk merge that changes announce back (c2) do
// not move it; before any publish it kept the commit form.
func anAcceptedSeriesPinsItsNewest(t *testing.T) {
ctx := context.Background()
ix, mods := newFormIndexer(t, mod{"avahi", catalogueManifest(t, "avahi")})
reportToolchain(t, ix, "mesh-build-go", "novox", "go", "go1.27.1")
ix.Packages, ix.Files = newMem(), forge{c1: sdkFiles("a1", "b1"), c2: sdkFiles("a1", "b2"), c3: sdkFiles("a2", "b2")}.files
setPackages(t, mods, "avahi", []string{meshSDK})
sdkModule, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: "novox/mesh-sdk", Path: ".", Commit: c1, None2: "a TypeScript package"})
if err != nil {
t.Fatal(err)
}
mods.put("mesh-sdk", sdkModule)
step := func(what string, act func() error) meshcatalog.ModulesByModuleValue {
t.Helper()
if err := act(); err != nil {
t.Fatalf("%s: %v", what, err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
m, _ := moduleOf(t, mods, "avahi")
return m
}
trunk := func(commit string) func() error {
return func() error {
held, _ := ix.heldModules(ctx)
return ix.publish(ctx, "novox/mesh-sdk", commit, held, false)
}
}
hear := func(version, commit string) func() error {
return func() error {
_, err := ix.HearVersion(ctx, HeardVersion{Package: "mesh-sdk/go", Seat: "go-package-registry", Version: version,
Commit: commit, GoModule: meshSDK, Published: true})
return err
}
}
before := step("trunk at c1", trunk(c1))
if before.Packages[meshSDK] != c1 {
t.Fatalf("with no series published, versions %v, want the commit form c1", before.Packages)
}
step("the build reports its imports", func() error {
return ix.ApplyBuildEnd(ctx, buildEnd(t, "avahi", before.Fp2, meshSDK, meshSDK+"/announce"))
})
pinned := step("0.1.15 published from c3", hear("0.1.15", c3))
if pinned.Fp2 == before.Fp2 || pinned.Packages[meshSDK] != "v0.1.15" {
t.Fatalf("after 0.1.15: fp2 %q (was %q), versions %v; want moved by c3's announce, pinned to v0.1.15", pinned.Fp2, before.Fp2, pinned.Packages)
}
if m := step("0.2.0 published from c1", hear("0.2.0", c1)); m.Fp2 != pinned.Fp2 || m.Packages[meshSDK] != "v0.1.15" {
t.Errorf("a publish of a series avahi does not accept moved it: %q %v", m.Fp2, m.Packages)
}
if m := step("trunk at c2", trunk(c2)); m.Fp2 != pinned.Fp2 || m.Packages[meshSDK] != "v0.1.15" {
t.Errorf("a trunk merge moved avahi off its accepted series' newest: %q %v", m.Fp2, m.Packages)
}
}
// Issue 1966 (the indexer started while the bus's storage was down): a read of the packages state that failed, or that
// answered no published package where held modules were computed from published ones, writes no fingerprint (the version
// line over every import form is what an empty state computes); a version line written leaves no import form behind
// that a later pass takes for the build's; and indexer_fingerprints_again computes the import form again by hand.
func anUnreadPackagesStateWritesNoFingerprintAndFingerprintsAreComputedAgain(t *testing.T) {
ctx := context.Background()
// avahi at the import form, pinned to mesh-sdk at c3, as the test above leaves it.
setup := func(t *testing.T) (*Indexer, *memStore, meshcatalog.ModulesByModuleValue) {
ix, mods := newFormIndexer(t, mod{"avahi", catalogueManifest(t, "avahi")})
reportToolchain(t, ix, "mesh-build-go", "novox", "go", "go1.27.1")
ix.Packages, ix.Files = newMem(), forge{c1: sdkFiles("a1", "b1"), c3: sdkFiles("a2", "b2")}.files
setPackages(t, mods, "avahi", []string{meshSDK})
raw, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: "novox/mesh-sdk", Path: ".", Commit: c1, None2: "a TypeScript package"})
if err != nil {
t.Fatal(err)
}
mods.put("mesh-sdk", raw)
held, _ := ix.heldModules(ctx)
if err := ix.publish(ctx, "novox/mesh-sdk", c1, held, false); err != nil {
t.Fatal(err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
first, _ := moduleOf(t, mods, "avahi")
if err := ix.ApplyBuildEnd(ctx, buildEnd(t, "avahi", first.Fp2, meshSDK, meshSDK+"/announce")); err != nil {
t.Fatal(err)
}
held, _ = ix.heldModules(ctx)
if err := ix.publish(ctx, "novox/mesh-sdk", c3, held, false); err != nil {
t.Fatal(err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
m, _ := moduleOf(t, mods, "avahi")
if m.Fp2 == first.Fp2 || m.Fp2 != m.Inputs2.ImportForm {
t.Fatalf("setup: fp2 %q (was %q), import form %q; want the import form", m.Fp2, first.Fp2, m.Inputs2.ImportForm)
}
return ix, mods, m
}
t.Run("a pass whose read of the packages state failed or answered nothing writes no fingerprint", func(t *testing.T) {
for _, c := range []struct {
name string
bad func(ix *Indexer)
why string
}{
{"the listing fails", func(ix *Indexer) { ix.Packages.(*memStore).keysErr = errors.New("jetstream not enabled") }, "cannot be listed"},
{"the state is not on the bus yet", func(ix *Indexer) { ix.Packages.(*memStore).keysErr = process.ErrNotYet }, "answers no published package"}, // a mesh with no import form computes as before: TestIndexerWritesModulesAtAMerge
{"the state answers no package", func(ix *Indexer) { ix.Packages = newMem() }, "answers no published package"},
} {
t.Run(c.name, func(t *testing.T) {
ix, mods, _ := setup(t)
was, _ := moduleOf(t, mods, "avahi")
c.bad(ix)
err := ix.Recompute(ctx)
if err == nil || !strings.Contains(err.Error(), c.why) {
t.Fatalf("recompute with an unread packages state: err %v, want one saying %q", err, c.why)
}
if now, _ := moduleOf(t, mods, "avahi"); now.Fp2 != was.Fp2 {
t.Fatalf("a pass over an unread packages state moved avahi's fingerprint to %q (was %q)", now.Fp2, was.Fp2)
}
})
}
})
t.Run("a version line written keeps no import form, and a fingerprint computed again by hand is the import form", func(t *testing.T) {
ix, mods, good := setup(t)
// What the pass that read nothing wrote on novox: the version line's fingerprint, the import form still kept.
bad := good
bad.Fp2 = "src2:" + strings.Repeat("d", 64)
raw, err := meshcatalogclient.EncodeModule(bad)
if err != nil {
t.Fatal(err)
}
mods.put("avahi", raw)
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
if now, _ := moduleOf(t, mods, "avahi"); now.Fp2 != bad.Fp2 {
t.Fatalf("the held version line is not kept by the build's shortcut (fp2 %q); the case the tool recovers has changed", now.Fp2)
}
r := ix.FingerprintsAgain(ctx, "avahi")
if !r.Stored || len(r.Fingerprints) != 1 || r.Fingerprints[0].Old != bad.Fp2 || r.Fingerprints[0].New != good.Fp2 {
t.Fatalf("fingerprints again: %+v, want avahi from %q to %q", r, bad.Fp2, good.Fp2)
}
if now, _ := moduleOf(t, mods, "avahi"); now.Fp2 != good.Fp2 || ix.fresh != nil {
t.Fatalf("avahi holds %q after it, want %q (fresh %v)", now.Fp2, good.Fp2, ix.fresh)
}
// It runs a merge's recompute, candidates included: a candidate store that cannot be listed fails it, said.
ix.Store, ix.previews = &memStore{keysErr: errors.New("no bus")}, map[string]meshdeliverychecker.PreviewsByRepositoryHeadTrunkValue{"k": {}}
if r := ix.FingerprintsAgain(ctx, "avahi"); r.Stored || !strings.Contains(r.Answer, "listing the candidates") {
t.Fatalf("fingerprints again over candidates that cannot be listed: %+v", r)
}
if r := ix.FingerprintsAgain(ctx, "nothing"); r.Stored {
t.Fatalf("fingerprints again of a module not held answered stored: %+v", r)
}
})
t.Run("formOf builds a package no module publishes at the source's commit of its repository", func(t *testing.T) { // design 47 G A-365
head := strings.Repeat("c", 40)
contracts := "git.novox.be/novox/mesh-contracts/go/seats/findings"
held := map[string]meshcatalog.ModulesByModuleValue{"other": {Repository: "novox/mesh-sdk", Commit: strings.Repeat("d", 40), Path: "go"}}
for _, c := range []struct {
name string
heads map[string]string
held map[string]meshcatalog.ModulesByModuleValue
want string
}{
{"the source names its repository", map[string]string{"novox/mesh-contracts": head}, held, head},
{"the source names another repository only", map[string]string{"novox/mesh-sdk": head}, held, ""},
{"a module the indexer holds publishes it", map[string]string{"novox/mesh-contracts": head},
map[string]meshcatalog.ModulesByModuleValue{"c": {Repository: "novox/mesh-contracts", Commit: strings.Repeat("e", 40), Path: "go/seats/findings"}}, strings.Repeat("e", 40)},
} {
kept := meshcatalog.ModulesByModuleValueInputs2{Tree: "t", Packages: []string{contracts}}
f := formOf("avahi", "novox/mesh-catalog", &kept, nil, meshcatalog.ModulesByModuleValue{}, c.held, nil, nil, c.heads, nil)
if got := f.versions[contracts]; got != c.want {
t.Errorf("%s: version of %s = %q, want %q", c.name, contracts, got, c.want)
}
}
})
t.Run("formOf clears the import form when it writes the version line", func(t *testing.T) {
for _, c := range []struct {
name string
imports []string
}{{"an import no package holds", []string{meshSDK + "/announce"}}, {"no import reported", nil}} {
kept := meshcatalog.ModulesByModuleValueInputs2{Tree: "t", Imports: c.imports, ImportForm: "src2:" + strings.Repeat("a", 64)}
f := formOf("avahi", "novox/mesh-catalog", &kept, nil, meshcatalog.ModulesByModuleValue{}, nil, nil, nil, nil, nil)
if kept.ImportForm != "" {
t.Errorf("%s: kept import form %q after the version line %q", c.name, kept.ImportForm, f.fp)
}
}
})
}
// A start replays every build end of the build log: an entry that writes no module keeps no view, and the entries that
// write keep it once, after all of them (measured: the view kept per entry cost 50 min CPU per start).
func TestBuildEndsKeepTheViewOnlyWhenTheyWrite(t *testing.T) {
ctx := context.Background()
ix, _ := newFormIndexer(t, mod{"avahi", catalogueManifest(t, "avahi")})
reportToolchain(t, ix, "mesh-build-go", "novox", "go", "go1.27.1")
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
raw, _, _, _ := ix.Modules.Get(ctx, "avahi")
cur, err := meshcatalogclient.DecodeModule(raw)
if err != nil || cur.Fp2 == "" {
t.Fatalf("no fingerprint to start from: %v %q", err, cur.None2)
}
older := buildEnd(t, "avahi", "src2:"+strings.Repeat("e", 64), meshSDK)
writes := buildEnd(t, "avahi", cur.Fp2, meshSDK)
kept := 0
ix.keptView = func(context.Context) error { kept++; return nil }
for _, c := range []struct {
name string
entries [][]byte
want int
}{
{"no entry", nil, 0},
{"entries that write nothing", [][]byte{older, []byte("not a build entry"), older}, 0},
{"a write among entries that write nothing", [][]byte{older, writes, older}, 1},
{"the same write again keeps nothing", [][]byte{writes, older}, 0},
} {
kept = 0
if err := ix.ApplyBuildEndsAtStart(ctx, c.entries, nil); err != nil || kept != c.want {
t.Errorf("%s: the view was kept %d times (%v), want %d", c.name, kept, err, c.want)
}
}
// The live path, one entry: the view is kept when it wrote.
kept = 0
if err := ix.ApplyBuildEnd(ctx, older); err != nil || kept != 0 {
t.Errorf("a live entry that writes nothing kept the view %d times (%v)", kept, err)
}
}