package index import ( "context" "errors" "fmt" "maps" "slices" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/process" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph" ) // The module graph (novox/hq ADR 0603 §2): at every recompute the indexer writes, in its state graph // (GraphBucket), one key per module it holds the inputs of, its build, provision and verb edges as that recompute // computed them (ADR 0614 §1), the verb edges on a cycle left out over the whole graph (§3), and deletes the key of a module the modules state no longer holds. A key is written only when its value // changes, so its history of 5 is five changes of the module's edges. The delivery view, the checker and the seat // mesh-catalog's verbs read it; nothing stores an order (ADR 0603 §1). // GraphMost is the most modules the graph holds (ADR 0603 §2): past it, the modules after the first GraphMost in name // order are not written, and it is said in the log. The cap is the indexer's code, not a manifest field, until a judge // that knows such a field runs (the lead's decision of L3). const GraphMost = 2000 // graphEdges is what a recompute knows of one module for its graph node. type graphEdges struct { module string cur meshcatalog.ModulesByModuleValue manifest catalogue.Manifest fp, none string tcs map[string]meshcatalog.ToolchainUsed pkgs sdkstep.PackageVersions } // graphNode is the graph node of e: its edges, the awaited provisions judged against providers (catalogue.Awaited, as // the assigner judges them). func graphNode(e graphEdges, providers func(string) []catalogue.Manifest, builders map[string]BuildModule) graph.Node { if e.fp == "" && e.none == "" { e.none = "its fingerprint was not computed" } n := graph.EdgesOf(e.manifest, providers) n.FP, n.None, n.Repository, n.Path = e.fp, e.none, e.cur.Repository, e.cur.Path n.Packages = slices.Sorted(maps.Keys(e.pkgs)) tcs := e.tcs if len(tcs) == 0 { tcs = e.cur.Toolchains // no fingerprint now: the toolchain it was last computed with still builds it } for language, t := range tcs { b, known := builders[language] if !known { continue } builder := b.Module if n.Toolchains == nil { n.Toolchains = map[string]graph.Toolchain{} } n.Toolchains[language] = graph.Toolchain{Module: builder, Version: t.Version, Recipe: t.Recipe} } return n } // writeGraph writes the graph node of every module of edges whose value changed, at most GraphMost of them, then // deletes each graph key whose module the modules state no longer holds. Nil Graph writes nothing, and neither does a // graph state that is not on the bus yet (process.ErrNotYet; main.go: until then no graph is written), said in the log; // a listing that fails otherwise skips the deletion pass, said, and never fails the merge or the recompute that wrote. func (ix *Indexer) writeGraph(ctx context.Context, edges map[string]graphEdges) error { if ix.Graph == nil { return nil } keys, listErr := ix.Graph.Keys(ctx) if errors.Is(listErr, process.ErrNotYet) { ix.logf("the module graph is not written: its state is not on the bus yet") return nil } names := slices.Sorted(maps.Keys(edges)) if len(names) > GraphMost { ix.logf("the module graph holds at most %d modules: %d are known, so %s and the %d after it are not written", GraphMost, len(names), names[GraphMost], len(names)-GraphMost-1) names = names[:GraphMost] } whole := wholeGraph(edges, ix.BuildModules()) for _, name := range names { key, err := graph.Key(name) if err != nil { ix.logf("graph.%s is not written: %v", name, err) continue } next := whole[name] err = write(ctx, ix.Graph, key, func(value []byte, found bool) ([]byte, bool, error) { if found { if cur, err := graph.Decode(value); err == nil && graph.Same(cur, next) { return nil, false, nil } } data, err := graph.Encode(next) if err != nil { return nil, false, fmt.Errorf("graph.%s: %w", key, err) } return data, false, nil }) if err != nil { ix.logf("graph.%s is not written: %v", key, err) } } held, err := ix.Modules.Keys(ctx) switch { case listErr != nil: ix.logf("the graph keys of modules the modules state no longer holds are not deleted: listing the graph: %v", listErr) return nil case err != nil: ix.logf("the graph keys of modules the modules state no longer holds are not deleted: listing the modules: %v", err) return nil } for _, key := range keys { if slices.Contains(held, key) { continue } if err := write(ctx, ix.Graph, key, func(_ []byte, found bool) ([]byte, bool, error) { return nil, found, nil }); err != nil { ix.logf("graph.%s is not deleted: %v", key, err) continue } ix.logf("graph.%s deleted: the modules state holds no module %s", key, key) } return nil } // wholeGraph is the module graph of every module of edges, the verb edges on a cycle left out over the whole graph // (ADR 0614 §3), so a module past GraphMost still closes the cycle it lies on. func wholeGraph(edges map[string]graphEdges, builders map[string]BuildModule) graph.Graph { providers := func(provision string) []catalogue.Manifest { var out []catalogue.Manifest for _, name := range slices.Sorted(maps.Keys(edges)) { if slices.Contains(edges[name].manifest.Offers(), provision) { out = append(out, edges[name].manifest) } } return out } whole := graph.Graph{} for name, e := range edges { whole[name] = graphNode(e, providers, builders) } whole.LeaveOut() return whole }