The seat list's build seat per artifact (language or kind), a kept call
<seat>.steps with {module, artifact, fingerprint, fetch}, and the finding
call-unanswered (scope call, id <seat>.steps) after five minutes, cleared on an answer.
Builds: design 47 G §704
Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
179 lines
6.3 KiB
Go
179 lines
6.3 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"maps"
|
|
"path"
|
|
"slices"
|
|
"time"
|
|
|
|
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/nodebuildgo"
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
)
|
|
|
|
// StepsVerb is the verb of each build seat the seed asks (design 47 G §704).
|
|
const StepsVerb = "steps"
|
|
|
|
// StepsCaller asks the build seat seat its verb steps (a kept call, ADR 0668 §1): the answer, or why none came.
|
|
type StepsCaller func(ctx context.Context, seat string, in nodebuildgo.StepsInput) (nodebuildgo.StepsAnswer, error)
|
|
|
|
// ModuleManifest is the file name of a module's manifest in its directory.
|
|
const ModuleManifest = "module.json"
|
|
|
|
// buildSeatOf is the seat whose acting rule `build` has `on` matching the artifact's language or kind (design 47 G §704,
|
|
// §203, §324), read from the seat list defs as BuildModulesOf reads it; none, or more than one, is said.
|
|
func buildSeatOf(defs map[string]seatdef.Definition, a meshcatalog.Artifact) (string, error) {
|
|
var seats []string
|
|
for _, name := range slices.Sorted(maps.Keys(defs)) {
|
|
for _, r := range defs[name].Acts {
|
|
if r.Step != seatdef.StepBuild {
|
|
continue
|
|
}
|
|
if r.ActsOn(a.Language) || (r.On == seatdef.OnKindImage && a.Kind == "image") {
|
|
seats = append(seats, name)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
switch len(seats) {
|
|
case 0:
|
|
return "", fmt.Errorf("no seat builds the artifact %s (language %q, kind %q)", a.Ref, a.Language, a.Kind)
|
|
case 1:
|
|
return seats[0], nil
|
|
}
|
|
return "", fmt.Errorf("the seats %v each build the artifact %s", seats, a.Ref)
|
|
}
|
|
|
|
// SeedModulesFrom reads the modules the newest trunk revision holds for the seed (design 47 G §704, §8): for each artifact of
|
|
// each module the seat list's build seat answers steps with {module, artifact, fingerprint, fetch} from the module's trunk
|
|
// entry, the fetch its repository, commit, directory and the artifact's named paths. A seat that does not answer leaves
|
|
// its artifact unanswered, which holds the seed; its call-unanswered finding (scope call, id <seat>.steps) is written once
|
|
// it is unanswered five minutes and cleared when it answers.
|
|
func (ix *Indexer) SeedModulesFrom(ctx context.Context, defs map[string]seatdef.Definition, steps StepsCaller) ([]SeedModule, error) {
|
|
if ix.Revisions == nil {
|
|
return nil, fmt.Errorf("the catalog graph is not kept here: no trunk revision to seed from")
|
|
}
|
|
rev, found, err := newestTrunk(ctx, ix.Revisions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !found {
|
|
return nil, fmt.Errorf("the indexer keeps no trunk revision yet: the seed waits for its first")
|
|
}
|
|
_, entries, err := ReadRevision(ctx, ix.Revisions, fmt.Sprintf("trunk.%d", rev.Revision))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
seatOf := map[string]string{} // artifact reference -> its build seat, all found before any call is made
|
|
for _, e := range entries {
|
|
for _, a := range e.Artifacts {
|
|
seat, err := buildSeatOf(defs, a)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", e.Module, err)
|
|
}
|
|
seatOf[a.Ref] = seat
|
|
}
|
|
}
|
|
answered := map[string]bool{}
|
|
unanswered := map[string]error{}
|
|
var out []SeedModule
|
|
for _, e := range entries {
|
|
m := SeedModule{Module: e.Module, Origin: e.Repository, Path: path.Join(e.Path, ModuleManifest)}
|
|
for _, a := range e.Artifacts {
|
|
seat := seatOf[a.Ref]
|
|
name := artifactName(a.Ref)
|
|
sa := SeedArtifact{Name: name, Seat: seat}
|
|
in := nodebuildgo.StepsInput{Module: e.Module, Artifact: nodebuildgo.DetectedStepsByModuleArtifactKey(e.Module, name), Fingerprint: e.Fp,
|
|
Fetch: &nodebuildgo.Fetch{Repository: e.Repository, Commit: e.Commit, Dir: e.Path, Paths: namedPaths(e, name)}}
|
|
ans, err := steps(ctx, seat, in)
|
|
switch {
|
|
case err != nil:
|
|
unanswered[seat] = err
|
|
case ans.Error != "":
|
|
unanswered[seat] = fmt.Errorf("%s", ans.Error)
|
|
default:
|
|
sa.Answered = true
|
|
answered[seat] = true
|
|
for _, s := range ans.Steps {
|
|
sa.Steps = append(sa.Steps, s.Step)
|
|
}
|
|
}
|
|
m.Artifacts = append(m.Artifacts, sa)
|
|
}
|
|
out = append(out, m)
|
|
}
|
|
ix.stepsFindings(ctx, answered, unanswered)
|
|
return out, nil
|
|
}
|
|
|
|
// artifactName is the artifact's name in an artifact reference <module>:<artifact>.
|
|
func artifactName(ref string) string {
|
|
for i := len(ref) - 1; i >= 0; i-- {
|
|
if ref[i] == ':' {
|
|
return ref[i+1:]
|
|
}
|
|
}
|
|
return ref
|
|
}
|
|
|
|
// stepsFindings writes call-unanswered for each seat unanswered for five minutes and clears it for each that answered.
|
|
func (ix *Indexer) stepsFindings(ctx context.Context, answered map[string]bool, unanswered map[string]error) {
|
|
if ix.Findings == nil {
|
|
return
|
|
}
|
|
ix.mu.Lock()
|
|
if ix.stepsSince == nil {
|
|
ix.stepsSince = map[string]time.Time{}
|
|
}
|
|
now := ix.now()
|
|
var raise []string
|
|
for seat := range unanswered {
|
|
if _, ok := ix.stepsSince[seat]; !ok {
|
|
ix.stepsSince[seat] = now
|
|
}
|
|
if now.Sub(ix.stepsSince[seat]) >= mcc.CallUnansweredAfter {
|
|
raise = append(raise, seat)
|
|
}
|
|
}
|
|
for seat := range answered {
|
|
if _, bad := unanswered[seat]; !bad {
|
|
delete(ix.stepsSince, seat)
|
|
}
|
|
}
|
|
since := maps.Clone(ix.stepsSince)
|
|
ix.mu.Unlock()
|
|
for _, seat := range slices.Sorted(slices.Values(raise)) {
|
|
id := seat + "." + StepsVerb
|
|
f := ix.observed(mcc.ScopeCall, id, mcc.KindCallUnanswered,
|
|
fmt.Sprintf("the call %s is unanswered since %s", id, since[seat].UTC().Format(time.RFC3339)),
|
|
"A call whose answer is kept is not answered",
|
|
fmt.Sprintf("The seed asks %s of every artifact its build seat builds; it is not answered since %s (%v), so the first registrations wait for it.", id, since[seat].UTC().Format(time.RFC3339), unanswered[seat]),
|
|
"indexer."+mcc.KindCallUnanswered)
|
|
if err := ix.Findings.Write(ctx, f); err != nil {
|
|
ix.logf("seed: the finding %s is not written: %v", id, err)
|
|
}
|
|
}
|
|
for seat := range answered {
|
|
if _, bad := unanswered[seat]; bad {
|
|
continue
|
|
}
|
|
if err := ix.clearFinding(ctx, mcc.ScopeCall, seat+"."+StepsVerb, mcc.KindCallUnanswered, "the seat answers steps"); err != nil {
|
|
ix.logf("seed: the finding %s.%s is not cleared: %v", seat, StepsVerb, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// namedPaths are the named paths the entry keeps for the artifact, by its name or its reference.
|
|
func namedPaths(e meshcatalog.CatalogEntry, name string) []string {
|
|
if e.Inputs == nil {
|
|
return nil
|
|
}
|
|
if p, ok := e.Inputs.Paths[name]; ok {
|
|
return p
|
|
}
|
|
return e.Inputs.Paths[ArtifactRefOf(e.Module, name)]
|
|
}
|