Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/imports.go
T

218 lines
8.8 KiB
Go

package index
import (
"context"
"fmt"
"maps"
"slices"
"strings"
"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-sdk/trunk"
"mesh-delivery-indexer/internal/fingerprint"
)
// ApplyBuildEnd keeps the imports a done build reports at its end (builds.BuildLogEntry.Imports; novox/hq ADR 0593
// §2) on the module it built, when the build's fingerprint is the one the module holds: the import set of the module's
// last trunk build. With them it keeps ImportForm, the import-form fingerprint of exactly what the build was built from:
// the module's kept inputs and the toolchains its fingerprint was computed with, and each imported package's digest at
// the version the build took it at (another repository's at the version the module's fingerprint pins, its own
// repository's at the commit built). A recompute that gives that value again keeps the fingerprint (§4); one that
// differs writes the import form. The fingerprint itself is not changed here.
//
// An entry that is no done build's end, reports no imports, or is of a fingerprint the module no longer holds is left.
// Imports that cannot be resolved (a package the forge cannot list) are kept with no ImportForm: the next recompute that
// resolves them writes the import form, one rebuild too many, never a stale module.
func (ix *Indexer) ApplyBuildEnd(ctx context.Context, value []byte) error {
wrote, err := ix.applyBuildEnd(ctx, value)
if err != nil || !wrote {
return err // an entry that wrote nothing leaves the view as it is
}
return ix.keepView(ctx) // the imports kept with the view (KeepView), so a restart reads them back
}
// ApplyBuildEndsAtStart applies every entry of the build log read at start as ApplyBuildEnd does, and keeps the view
// once, after them, when any entry wrote a module: KeepView writes the whole catalog view, so once per entry is a start
// that never ends (measured: 50 min CPU per start). An entry that fails is passed to failed with its position.
func (ix *Indexer) ApplyBuildEndsAtStart(ctx context.Context, values [][]byte, failed func(i int, err error)) error {
began := time.Now()
ix.logf("replaying %d build ends of the build log at start", len(values))
wrote := 0
slow := 0 // entries that took over a second
for i, v := range values {
if (i+1)%50000 == 0 {
ix.logf("replaying the build ends at start: %d of %d, %d wrote a module, %d took over a second, %s so far", i+1, len(values), wrote, slow, time.Since(began).Round(time.Millisecond))
}
t := time.Now()
w, err := ix.applyBuildEnd(ctx, v)
if d := time.Since(t); d > time.Second {
slow++
ix.logf("replaying the build ends at start: entry %d took %s", i+1, d.Round(time.Millisecond))
}
if err != nil {
if failed != nil {
failed(i, err)
}
continue
}
if w {
wrote++
}
}
var err error
if wrote > 0 {
err = ix.keepView(ctx)
}
ix.logf("replayed %d build ends at start: %d wrote a module, in %s", len(values), wrote, time.Since(began).Round(time.Millisecond))
return err
}
// keepView is KeepView; a test replaces it to count the writes of the view.
func (ix *Indexer) keepView(ctx context.Context) error {
if ix.keptView != nil {
return ix.keptView(ctx)
}
return ix.KeepView(ctx)
}
// applyBuildEnd reports whether it wrote the module (its final write changed the held value).
func (ix *Indexer) applyBuildEnd(ctx context.Context, value []byte) (bool, error) {
if !builds.IsBuildEntry(value) {
return false, nil
}
e, err := builds.DecodeBuildLogEntry(value)
if err != nil {
return false, err
}
if e.Event != builds.BuildEndedEvent || e.Outcome != builds.BuildDone || len(e.Imports) == 0 || ix.Modules == nil || e.FP == "" {
return false, nil
}
key, err := meshcatalogclient.ModulesKey(e.Module)
if err != nil {
return false, nil
}
ix.recompute.Lock()
defer ix.recompute.Unlock()
raw, _, found, err := ix.Modules.Get(ctx, key)
if err != nil || !found {
return false, err
}
cur, err := meshcatalogclient.DecodeModule(raw)
if err != nil || cur.Fp2 != e.FP || cur.Inputs2 == nil || cur.Manifest == "" {
return false, nil // a build of a fingerprint the module no longer holds says nothing of its inputs now
}
if slices.Equal(cur.Inputs2.Imports, e.Imports) && cur.Inputs2.ImportForm != "" {
return false, nil
}
_, m, err := ix.manifestRaw(ctx, cur.Manifest)
if err != nil {
return false, fmt.Errorf("modules.%s: its manifest: %w", key, err)
}
toolchains := map[string]meshcatalogclient.Toolchain2{}
for artifact, language := range languagesOf(m) {
used, ok := cur.Toolchains[language]
if !ok {
ix.logf("%s: its fingerprint names no %s toolchain, so the imports of its build are not kept", e.Module, language)
return false, nil
}
toolchains[artifact] = meshcatalogclient.Toolchain2{Language: language, Version: used.Version, Recipe: used.Recipe}
}
kept := *cur.Inputs2
kept.Imports, kept.ImportForm = slices.Clone(e.Imports), ""
if built, err := ix.publishedAsBuilt(ctx, cur, e); err != nil {
ix.logf("%s: the packages its build imported cannot be read as built (%v): its imports are kept, and its next recompute writes the import form", e.Module, err)
} else if r := fingerprint.Resolve(cur.Repository, e.Imports, built); len(r.Missing) > 0 {
ix.logf("%s: no published package holds %v: its imports are kept, and the recompute that resolves them writes the import form", e.Module, r.Missing)
} else {
in := inputsFrom(e.Module, kept, toolchains)
in.Imports, in.Requires = r.Digests, r.Requires
if fp, _ := fingerprint.Of(in); fp != "" {
kept.ImportForm = fp
}
}
wrote := false
err = write(ctx, ix.Modules, key, func(value []byte, found bool) ([]byte, bool, error) {
wrote = false // a conflict computes again
if !found {
return nil, false, nil
}
now, err := meshcatalogclient.DecodeModule(value)
if err != nil || now.Fp2 != e.FP || now.Manifest != cur.Manifest || now.Inputs2 == nil {
return nil, false, nil
}
in := *now.Inputs2
in.Imports, in.ImportForm = kept.Imports, kept.ImportForm
now.Inputs2 = &in
data, err := meshcatalogclient.EncodeModule(now)
wrote = err == nil && !unchanged(data, false, value, found)
return data, false, err
})
return wrote && err == nil, err
}
// publishedAsBuilt are the published Go modules holding the imports of build e of module cur, each as the build took
// it: a module of the built module's own repository at the commit built, one of another repository at the version the
// module's fingerprint pins (cur.Packages), else its latest published version. A module whose published commit is the
// one built is taken from the packages state; any other is read from the forge's listing at that commit.
func (ix *Indexer) publishedAsBuilt(ctx context.Context, cur meshcatalog.ModulesByModuleValue, e builds.BuildLogEntry) (map[string]meshcatalog.PackagesByModuleValue, error) {
published := ix.publishedAll(ctx)
if r := fingerprint.Resolve(cur.Repository, e.Imports, published); len(r.Missing) > 0 {
if err := ix.FillPackages(ctx); err != nil {
return nil, err
}
published = ix.publishedAll(ctx)
}
// A pin that is an accepted series' version (DLV-I6) is taken by the contents its series keeps for it.
byVersion := map[string]meshcatalog.PackagesByModuleValue{}
for _, s := range ix.SeriesAll(ctx) {
if s.GoModule != "" && s.Newest != nil && s.Newest.Contents != nil {
byVersion[s.GoModule+"@v"+s.Newest.Version] = publishedOfContents(*s.Newest.Contents)
}
}
type at struct{ repository, commit string }
reread := map[at][]string{}
var asVersion map[string]meshcatalog.PackagesByModuleValue
for _, imp := range e.Imports {
r := fingerprint.Resolve(cur.Repository, []string{imp}, published)
for mod := range r.Requires {
p := published[mod]
commit := p.Commit
switch pin := cur.Packages[mod]; {
case trunk.SameRepository(p.Repository, cur.Repository) && e.Commit != "":
commit = e.Commit
case strings.HasPrefix(pin, "v"):
if c, ok := byVersion[mod+"@"+pin]; ok {
if asVersion == nil {
asVersion = map[string]meshcatalog.PackagesByModuleValue{}
}
c.Commit, asVersion[mod] = pin, c
} else {
ix.logf("%s: the version %s of %s it was pinned to is no series' newest any more, so its imports are read at the latest published commit", e.Module, pin, mod)
}
case pin != "":
commit = pin
}
if commit != p.Commit {
k := at{p.Repository, commit}
if !slices.Contains(reread[k], mod) {
reread[k] = append(reread[k], mod)
}
}
}
}
out := maps.Clone(published)
for k, mods := range reread {
got, err := ix.packagesAt(ctx, k.repository, k.commit, mods, nil)
if err != nil {
return nil, err
}
maps.Copy(out, got)
}
maps.Copy(out, asVersion)
return out, nil
}