Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/packages.go
T
jschoubben cf93d30150 Names N3 (indexer): the indexer names its own buckets from its credential's module
Its 26 `<Module>_<state>` bucket constants (candidates, manifests, verdicts, packages, taken, the
control records, pins, pending and the rest), its consumers, findings and first views were named
from the constant Module; they are now index.Bucket(cred.Module, <state>), and its words name
the state where they named the bucket.

Builds: design 47 G A-296

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 19:18:56 +01:00

432 lines
15 KiB
Go

package index
import (
"context"
"errors"
"fmt"
"git.novox.be/novox/mesh-sdk/go/process"
"maps"
"path"
"reflect"
"slices"
"strings"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein"
"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-sdk/trunk"
"mesh-delivery-indexer/internal/fingerprint"
)
// The published packages (novox/hq ADR 0593 §2): the indexer alone reads, from the forge's listing, the content digest
// of each package of every mesh Go module a held module reaches, and writes the module's latest published version and
// digests in its state `packages`, one key per Go module. The builder, the checker and the
// build modules read it there; nobody else computes a digest or a version.
//
// A Go module is reached when a held module's go.sum names it (KeptInputs2.Packages) or holds an import a build
// reported (KeptInputs2.Imports). Its key is written at each merge of its repository, before the merge's recompute, so
// a merge that changes a package moves its importers in the same recompute (§3); and once at the start, for each
// reached module the state does not hold yet (FillPackages).
// publishedAll is every value of the packages state, by Go module path; empty with no state or one that does not answer.
func (ix *Indexer) publishedAll(ctx context.Context) map[string]meshcatalog.PackagesByModuleValue {
out, err := ix.publishedRead(ctx)
if err != nil {
ix.logf("the packages state cannot be listed, so every fingerprint takes the version line: %v", err)
}
return out
}
// publishedRead is publishedAll with a read that failed said as an error, never as an empty map: a pass that takes an
// unread state for "no package is published" writes the version line over every module's fingerprint (issue 1966, the
// indexer started while the bus's storage was down). No state wired is no error: nothing is published then.
func (ix *Indexer) publishedRead(ctx context.Context) (map[string]meshcatalog.PackagesByModuleValue, error) {
out := map[string]meshcatalog.PackagesByModuleValue{}
if ix.Packages == nil {
return out, nil
}
keys, err := ix.Packages.Keys(ctx)
if errors.Is(err, process.ErrNotYet) {
return out, nil // the state is not on the bus yet: nothing is published, as before
}
if err != nil {
return out, fmt.Errorf("the packages state cannot be listed: %w", err)
}
for _, key := range keys {
if IsSeriesKey(key) {
continue // a package's series (series.go), not a Go module's digests
}
value, _, found, err := ix.Packages.Get(ctx, key)
if errors.Is(err, process.ErrNotYet) {
continue
}
if err != nil {
return out, fmt.Errorf("packages.%s cannot be read: %w", key, err)
}
if !found {
continue
}
p, err := DecodePublishedPackages(value)
if err != nil {
ix.logf("packages.%s holds what is not a published module, left out: %v", key, err)
continue
}
out[p.Module] = p
}
return out, nil
}
// publishedForPass is the packages state a recompute computes fingerprints from, or why it must write none: a read that
// failed, or that answered no published module where the held modules show packages were published before (a module
// whose fingerprint was computed from the import form, or that holds resolved package versions). The first start of a
// mesh, whose held modules show none, computes from an empty state as before.
func (ix *Indexer) publishedForPass(ctx context.Context, held map[string]meshcatalog.ModulesByModuleValue) (map[string]meshcatalog.PackagesByModuleValue, error) {
published, err := ix.publishedRead(ctx)
if err != nil {
return nil, err
}
if len(published) > 0 {
return published, nil
}
var had []string
for key, m := range held {
if m.Inputs2 != nil && m.Inputs2.ImportForm != "" { // not Packages: a version line holds those too, with nothing published
had = append(had, key)
}
}
if len(had) > 0 {
slices.Sort(had)
return nil, fmt.Errorf("the packages state answers no published package, though %d held modules (%s ...) were computed from published ones", len(had), had[0])
}
return published, nil
}
// reached are, by repository, the mesh Go module paths the held modules' go.sum names and the imports their builds
// reported.
func reached(held map[string]meshcatalog.ModulesByModuleValue) (modules, imports map[string][]string) {
modules, imports = map[string][]string{}, map[string][]string{}
add := func(to map[string][]string, p string) {
repo, _, ok := trunk.PackageRepository(p)
if ok && !slices.Contains(to[repo], p) {
to[repo] = append(to[repo], p)
}
}
for _, m := range held {
if m.Inputs2 == nil {
continue
}
for _, p := range m.Inputs2.Packages {
add(modules, p)
}
for _, p := range m.Inputs2.Imports {
add(imports, p)
}
}
for _, by := range []map[string][]string{modules, imports} {
for r := range by {
slices.Sort(by[r])
}
}
return modules, imports
}
// packagesAt are the Go modules of repository at commit that hold modules (module paths) or imports (import paths),
// computed from the forge's listing (Files), by module path. An import is held by a known module when one holds its
// directory, else by the Go module whose go.mod is at its directory or the nearest above it (asked of the forge
// directory by directory). A module the listing has no go.mod for is said and left out.
func (ix *Indexer) packagesAt(ctx context.Context, repository, commit string, modules, imports []string) (map[string]meshcatalog.PackagesByModuleValue, error) {
if ix.Files == nil || commit == "" {
return nil, nil
}
dirOf := map[string]string{} // module path to its directory in the repository
for _, m := range modules {
if _, dir, ok := trunk.PackageRepository(m); ok {
dirOf[m] = dir
}
}
holds := func(imp string) bool {
for m := range dirOf {
if imp == m || strings.HasPrefix(imp, m+"/") {
return true
}
}
return false
}
listings := map[string]map[string]string{} // by directory
list := func(dirs []string) error {
var ask []string
for _, d := range dirs {
if _, done := listings[d]; !done {
ask = append(ask, d)
}
}
if len(ask) == 0 {
return nil
}
got, err := ix.Files(ctx, repository, commit, ask)
if err != nil {
return fmt.Errorf("the forge's listing of %s at %s (%s): %w", repository, commit, strings.Join(ask, ", "), err)
}
for _, d := range ask {
listings[d] = got[d]
}
return nil
}
for _, imp := range imports {
if holds(imp) {
continue
}
_, dir, _ := trunk.PackageRepository(imp)
for d := dir; ; d = parent(d) {
if err := list([]string{d}); err != nil {
return nil, err
}
if _, ok := listings[d]["go.mod"]; ok {
m := strings.TrimSuffix(trunk.ForgeModulePrefix+strings.TrimPrefix(repository, "novox/")+"/"+d, "/")
dirOf[m] = d
break
}
if d == "" {
ix.logf("%s at %s: no go.mod holds the import %s, so it has no digest", repository, commit, imp)
break
}
}
}
// The modules are read in one call; when the tree at the commit lacks a directory of them (a module published at an earlier
// merge that this one removed or moved), each is read alone and the missing one is left out, said, so it never keeps the
// others from being published (issue 1585): a revision's own new module is published at the revision's own merge.
absent := map[string]bool{}
dirs := slices.Sorted(maps.Values(dirOf))
if err := list(dirs); err != nil {
if !errors.Is(err, ErrNotInTree) || len(dirs) < 2 {
return nil, err
}
whole := err
for _, d := range dirs {
if err := list([]string{d}); err != nil {
if !errors.Is(err, ErrNotInTree) {
return nil, err
}
ix.logf("%s at %s: the directory %q is not in the tree, so the module there is not read: %v", repository, commit, d, err)
absent[d] = true
}
}
if len(absent) == len(dirs) {
return nil, whole // the tree itself fails: the caller asks once per head (candidatePublished)
}
}
out := map[string]meshcatalog.PackagesByModuleValue{}
for _, m := range slices.Sorted(maps.Keys(dirOf)) {
dir := dirOf[m]
if absent[dir] {
continue
}
requires, pkgs, err := fingerprint.Packages(m, listings[dir])
if err != nil {
ix.logf("%s at %s: %v; it is not published", repository, commit, err)
continue
}
d := dir
if d == "" {
d = meshcatalogclient.RootPath
}
out[m] = meshcatalog.PackagesByModuleValue{Module: m, Repository: trunk.RepositoryName(repository), Dir: d, Commit: commit,
Requires: requires, Packages: pkgs}
}
return out, nil
}
// parent is the directory above d, "" for the repository's root.
func parent(d string) string {
if d == "" {
return ""
}
p := path.Dir(d)
if p == "." {
return ""
}
return p
}
// publish writes the packages state for the Go modules of repository the held modules (and the merge's overlay) reach,
// as the merge commit holds them: a key whose commit, requirements and digests are what it holds already is left.
func (ix *Indexer) publish(ctx context.Context, repository, commit string, held map[string]meshcatalog.ModulesByModuleValue, onlyMissing bool) error {
if ix.Packages == nil || ix.Files == nil {
return nil
}
modules, imports := reached(held)
repo := trunk.RepositoryName(repository)
if len(modules[repo]) == 0 && len(imports[repo]) == 0 {
return nil
}
have := ix.publishedAll(ctx)
if onlyMissing {
// Imports a held module already resolves are not asked again.
var ms, is []string
for _, m := range modules[repo] {
if _, ok := have[m]; !ok {
ms = append(ms, m)
}
}
for _, imp := range imports[repo] {
if len(fingerprint.Resolve(repo, []string{imp}, have).Missing) > 0 {
is = append(is, imp)
}
}
if len(ms) == 0 && len(is) == 0 {
return nil
}
modules[repo], imports[repo] = ms, is
}
// An import a published module of the repository already holds is resolved by its module's directory.
for m, p := range have {
if trunk.SameRepository(p.Repository, repo) && !slices.Contains(modules[repo], m) && !onlyMissing {
modules[repo] = append(modules[repo], m)
}
}
got, err := ix.packagesAt(ctx, repo, commit, modules[repo], imports[repo])
if err != nil {
return err
}
for _, m := range slices.Sorted(maps.Keys(got)) {
next := got[m]
key, err := PackagesKey(m)
if err != nil {
ix.logf("%s: %v", m, err)
continue
}
err = write(ctx, ix.Packages, key, func(value []byte, found bool) ([]byte, bool, error) {
if found {
if cur, err := DecodePublishedPackages(value); err == nil && cur.Commit == next.Commit &&
cur.Requires == next.Requires && reflect.DeepEqual(cur.Packages, next.Packages) {
return nil, false, nil
}
}
next.At = ptr(ix.now().UTC())
data, err := EncodePublishedPackages(next)
return data, false, err
})
if err != nil {
return fmt.Errorf("packages.%s: %w", key, err)
}
}
return nil
}
// FillPackages publishes, at the repository's taken merge (the trunk the modules say), each reached Go module the
// packages state does not hold yet: the state of a new indexer, or of a module a build newly reported an import of.
// A repository with no taken merge is left for its next merge.
func (ix *Indexer) FillPackages(ctx context.Context) error {
if ix.Packages == nil || ix.Files == nil || ix.Modules == nil || ix.Taken == nil {
return nil
}
held, err := ix.heldModules(ctx)
if err != nil {
return err
}
modules, imports := reached(held)
repos := map[string]bool{}
for r := range modules {
repos[r] = true
}
for r := range imports {
repos[r] = true
}
for _, repo := range slices.Sorted(maps.Keys(repos)) {
key, err := trunk.RepositoryKey(repo)
if err != nil {
continue
}
value, _, found, err := ix.Taken.Get(ctx, key)
if err != nil || !found {
continue
}
t, err := takein.DecodeTaken(value)
if err != nil {
continue
}
if err := ix.publish(ctx, repo, t.Merge, held, true); err != nil {
ix.logf("%s: its packages are not published at %s: %v", repo, t.Merge, err)
}
}
return nil
}
// candidatePublished is the packages state as a candidate builds against it (novox/hq ADR 0573 §4): each Go module a
// member's repository publishes at the member's head (members, by repository), every other at its latest published
// version. A head's modules are read from the forge once and kept while the head is a member somewhere; one the forge
// cannot list is said and left at its published version.
func (ix *Indexer) candidatePublished(ctx context.Context, members map[string]string, held map[string]meshcatalog.ModulesByModuleValue) map[string]meshcatalog.PackagesByModuleValue {
out := ix.publishedAll(ctx)
if len(members) == 0 {
return out
}
modules, imports := reached(held)
for _, repo := range slices.Sorted(maps.Keys(members)) {
head := members[repo]
if len(modules[repo]) == 0 && len(imports[repo]) == 0 {
continue
}
at := repo + "@" + head
ix.mu.Lock()
got, ok := ix.atHeads[at]
ix.mu.Unlock()
ix.mu.Lock()
failed := ix.failedAt[repo] == head
ix.mu.Unlock()
if failed {
continue // said once for this head; the candidate takes the published version until the head changes
}
if !ok {
var ms []string
ms = append(ms, modules[repo]...)
for m, p := range out {
if trunk.SameRepository(p.Repository, repo) && !slices.Contains(ms, m) {
ms = append(ms, m)
}
}
var err error
if got, err = ix.packagesAt(ctx, repo, head, ms, imports[repo]); err != nil {
// A failure of the tree itself (the directory is not in the tree at that head) no retry changes: it is asked once per
// head. Any other failure may be transient: it is asked again at the next pass, and said once per head.
ix.mu.Lock()
said := ix.saidAt[repo] == head
if ix.saidAt == nil {
ix.saidAt = map[string]string{}
}
ix.saidAt[repo] = head
if errors.Is(err, ErrNotInTree) {
if ix.failedAt == nil {
ix.failedAt = map[string]string{}
}
ix.failedAt[repo] = head
}
ix.mu.Unlock()
if !said {
ix.logf("%s at the head %s: its packages are not read, so the candidate takes their published version: %v", repo, head, err)
}
continue
}
ix.mu.Lock()
delete(ix.failedAt, repo)
delete(ix.saidAt, repo)
if ix.atHeads == nil || len(ix.atHeads) >= maxHeadsKept {
ix.atHeads = map[string]map[string]meshcatalog.PackagesByModuleValue{}
}
ix.atHeads[at] = got
ix.mu.Unlock()
}
maps.Copy(out, got)
}
return out
}
// ErrNotInTree is a failure of the tree itself: a directory asked of the forge is not in the tree at that commit. No retry
// changes it, so a head that fails so is asked once (candidatePublished).
var ErrNotInTree = errors.New("the directory is not in the tree at that commit")
// maxHeadsKept bounds the member heads' packages kept between recomputes; past it they are read again.
const maxHeadsKept = 64