package build import ( "context" "encoding/json" "errors" "fmt" "sort" "strings" "time" ) // **The plans no seat builds** (design 47 H2 §795, H-A-425): a module whose artifact no build seat builds, or whose build seat // no module claims, is kept in the builder's state unbuildable, one key per module, written when its plan is judged and removed // when the module builds; builder_unbuildable reads that state. The judgement is made again at every start and every change // of the seat list (SayUnbuildable), and said in one summary line each time, never a line per module. const ( // UnbuildableState is the builder's state of the plans no seat builds, one key , 8 MiB. UnbuildableState = "unbuildable" // UnbuildableTool is builder_unbuildable, which lists that state. UnbuildableTool = "builder_unbuildable" // UnbuildableSaid opens the summary line. UnbuildableSaid = "modules have an artifact no seat builds" ) // Unbuildable is one key of the state unbuildable: the module, the fingerprint whose plan was judged, the first artifact's // language and kind no seat builds, and why. type Unbuildable struct { Module string `json:"module"` Fingerprint string `json:"fingerprint"` Language string `json:"language,omitempty"` Kind string `json:"kind"` Why string `json:"why"` At time.Time `json:"at"` } // Unbuildables keeps the state unbuildable: Put writes a module's key, Delete removes it (a key not there is no error) and Keys // lists the modules it holds. type Unbuildables interface { Put(ctx context.Context, module string, value []byte) error Delete(ctx context.Context, module string) error Keys(ctx context.Context) ([]string, error) } // unbuildableOf is the key value of module's plan judged at fp with none. func unbuildableOf(module, fp string, none *NoBuildModule, at time.Time) Unbuildable { return Unbuildable{Module: module, Fingerprint: fp, Language: none.Language, Kind: none.Kind, Why: none.Error(), At: at.UTC()} } // judgedUnbuildable writes module's key: its plan, judged at fp, names an artifact no seat builds. func (b *Builder) judgedUnbuildable(ctx context.Context, module, fp string, none *NoBuildModule) { if b.Unbuildables == nil { return } raw, _ := json.Marshal(unbuildableOf(module, fp, none, b.now())) if err := b.Unbuildables.Put(ctx, module, raw); err != nil { b.logf("%s: the plan no seat builds is not kept in the state %s: %v", module, UnbuildableState, err) } } // buildable removes module's key: the module builds. func (b *Builder) buildable(ctx context.Context, module string) { if b.Unbuildables == nil { return } if err := b.Unbuildables.Delete(ctx, module); err != nil { b.logf("%s: its key of the state %s is not removed: %v", module, UnbuildableState, err) } } // SayUnbuildable judges the plan of every module the builder knows again, keeps the ones no seat builds in the state unbuildable // (writing the modules it does not hold yet and removing the ones that build now), and says them in one line: how many, and // each language and kind with its count (design 47 H2 §795). It answers the modules judged so. func (b *Builder) SayUnbuildable(ctx context.Context) []Unbuildable { b.init() b.mu.Lock() known := sortedKeys(b.known) modules := make(map[string]string, len(known)) // the fingerprint each is known at digests := make(map[string]string, len(known)) for _, k := range known { modules[k], digests[k] = b.known[k].Fp2, b.known[k].Manifest } b.mu.Unlock() var out []Unbuildable now := b.now() for _, k := range known { if modules[k] == "" || digests[k] == "" { continue } _, err, _ := b.planOf(ctx, digests[k]) var none *NoBuildModule if errors.As(err, &none) { out = append(out, unbuildableOf(k, modules[k], none, now)) } } if b.Unbuildables != nil { held := map[string]bool{} keys, err := b.Unbuildables.Keys(ctx) if err != nil { b.logf("the state %s cannot be listed, so the plans no seat builds are written whole: %v", UnbuildableState, err) } for _, k := range keys { held[k] = true } judged := map[string]bool{} for _, u := range out { judged[u.Module] = true if held[u.Module] { continue // written when its plan was judged at its look } raw, _ := json.Marshal(u) if err := b.Unbuildables.Put(ctx, u.Module, raw); err != nil { b.logf("%s: the plan no seat builds is not kept in the state %s: %v", u.Module, UnbuildableState, err) } } for _, k := range keys { if !judged[k] { b.buildable(ctx, k) } } } b.logf("%s", UnbuildableLine(out)) return out } // UnbuildableLine is the one summary line of the plans no seat builds: "N modules have an artifact no seat builds:" with each // language and kind and its count, most first (design 47 H2 §795). func UnbuildableLine(us []Unbuildable) string { counts := map[string]int{} for _, u := range us { what := u.Kind if u.Language != "" { what = u.Language + " " + u.Kind } counts[what]++ } whats := make([]string, 0, len(counts)) for w := range counts { whats = append(whats, w) } sort.Slice(whats, func(i, j int) bool { if counts[whats[i]] != counts[whats[j]] { return counts[whats[i]] > counts[whats[j]] } return whats[i] < whats[j] }) parts := make([]string, len(whats)) for i, w := range whats { parts[i] = fmt.Sprintf("%s %d", w, counts[w]) } line := fmt.Sprintf("%d %s", len(us), UnbuildableSaid) if len(parts) > 0 { line += ": " + strings.Join(parts, ", ") } return line }