B19 pin-wait-kept: a build key holds one wait, a Hold over another kind ends it as replaced and failed ends any kind; the wait gains publisher, revision and until, says package-wait-minutes as its bound; builder_stuck names a key's wait

Builds: design 47 H2 H-A-723, H-A-707 (5), H-A-717 (DQ-53)
This commit is contained in:
2026-11-25 17:52:46 +01:00
parent f77d6d76e1
commit 697716ca48
7 changed files with 124 additions and 21 deletions
@@ -329,7 +329,7 @@ const (
func main() {
reasks := stdio.State(build.ReAsksState)
r := build.StuckReader{Logf: func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-builder-tools] "+f+"\n", a...) }, Builds: read(stdio.State("builds")), ReAsks: read(reasks), StepsOf: stepsOfHolder(func(seat string) (string, error) { return seatlist.HolderOf(stdio.Ask, seat) }, func(name string) build.StateRead { return read(stdio.State(name)) }),
r := build.StuckReader{Logf: func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-builder-tools] "+f+"\n", a...) }, Builds: read(stdio.State("builds")), ReAsks: read(reasks), Waits: read(stdio.State(build.WaitsState)), StepsOf: stepsOfHolder(func(seat string) (string, error) { return seatlist.HolderOf(stdio.Ask, seat) }, func(name string) build.StateRead { return read(stdio.State(name)) }),
Hearing: read(stdio.State(buildrun.HearingState))}
put := func(key string, value []byte) error {
_, err := reasks.Put(key, json.RawMessage(value))
@@ -23,7 +23,7 @@ func waitsTools(rr versions.Reader, ask queueAsker, now func() time.Time, acts m
return []stdio.Tool{
{Name: build.WaitsTool,
Description: "The builder's toolchain waits: each entry held because its fingerprint names a toolchain version of its language other than the " +
"agreed one, with its module, fingerprint, what it waits on (toolchain:<language>@<version>) and since when. It ends when the agreed toolchain " +
"agreed one, and each build waiting for a package's blank pin (package:<path>, with its publisher, the catalog revision it holds and until when), with its module, fingerprint, what it waits on (toolchain:<language>@<version>) and since when. It ends when the agreed toolchain " +
"names that version (ran) or when " + build.ReleaseTool + " builds it with the running build module (released). Read-only.",
Input: map[string]any{"type": "object", "properties": map[string]any{}},
Run: func(map[string]any) (any, error) {
@@ -27,6 +27,10 @@ type waitBody struct {
Since time.Time `json:"since"`
Bound string `json:"bound,omitempty"`
How string `json:"how,omitempty"`
// Publisher, Revision and Until are a pin wait's (design 47 H2 H-A-707 (5), DQ-53).
Publisher string `json:"publisher,omitempty"`
Revision int64 `json:"revision,omitempty"`
Until time.Time `json:"until,omitzero"`
}
const (
@@ -51,7 +55,11 @@ func newWaitsKept(kv *process.AwaitedBucket, say func(context.Context, string, s
}
func (w *waitsKept) body(t build.ToolchainWait, how string) waitBody {
b := waitBody{Step: waitStepBuilding, On: t.On(), Part: t.Package, Module: t.Module, Fingerprint: t.Fingerprint, Since: t.Since, Bound: w.bound.String(), How: how}
b := waitBody{Step: waitStepBuilding, On: t.On(), Part: t.Package, Module: t.Module, Fingerprint: t.Fingerprint, Since: t.Since, Bound: w.bound.String(), How: how,
Publisher: t.Publisher, Until: t.Until}
if t.Publisher != "" { // a pin wait says the bound of `package-wait-minutes`, which it carries, and the revision it holds
b.Bound, b.Revision = t.Bound, t.Revision
}
if t.IsRevision() { // a step ask's wait for its revision: `step` build, its own bound (design 47 H2 H-A-577 (1))
b.Step, b.Bound = waitStepBuild, t.Bound
}
@@ -97,7 +105,9 @@ func (w *waitsKept) said(ctx context.Context, event string, t build.ToolchainWai
}
}
// Hold keeps the wait and says wait-held, once.
// Hold keeps the wait and says wait-held, once. A key holds the build's one current wait (design 47 H2 H-A-723 (1)): a Hold over a
// standing wait of another kind or other fields ends that wait, said wait-ended with how replaced, and holds the new one; a Hold equal
// to the standing wait changes nothing.
func (w *waitsKept) Hold(ctx context.Context, t build.ToolchainWait) {
key, err := builds.BuilderBuildKey(t.Module, t.Fingerprint)
if err != nil {
@@ -106,13 +116,17 @@ func (w *waitsKept) Hold(ctx context.Context, t build.ToolchainWait) {
}
key += slotOf(t)
w.mu.Lock()
if _, had := w.held[key]; had {
old, had := w.held[key]
if had && old.SameWait(t) {
w.mu.Unlock()
return
}
w.held[key] = t
w.mu.Unlock()
if kv := w.kv.KV(); kv != nil {
if had {
w.said(ctx, "wait-ended", old, build.WaitReplaced)
}
if kv := w.bucket(); kv != nil {
data, _ := json.Marshal(t)
if _, err := kv.Put(ctx, key, data); err != nil {
w.logf("%s.%s could not be written, so the wait is kept in memory only: %v", build.WaitsState, key, err)
@@ -139,8 +153,9 @@ func (w *waitsKept) end(ctx context.Context, module, fp, slot, how string) bool
key += slot
w.mu.Lock()
t, had := w.held[key]
// A key queued behind a package ends only when it is published, and a toolchain wait never by that (design 47 G A-192).
if had && slot == "" && (t.Package != "") != (how == build.WaitPublished) {
// A key queued behind a package ends only when it is published, and a toolchain wait never by that (design 47 G A-192); a wait
// ended failed is ended whatever its kind (design 47 H2 H-A-723 (2)).
if had && slot == "" && how != build.WaitFailed && (t.Package != "") != (how == build.WaitPublished) {
had = false
}
if had {
@@ -150,7 +165,7 @@ func (w *waitsKept) end(ctx context.Context, module, fp, slot, how string) bool
if !had {
return false
}
if kv := w.kv.KV(); kv != nil {
if kv := w.bucket(); kv != nil {
if err := kv.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
w.logf("%s.%s could not be deleted: %v", build.WaitsState, key, err)
}
@@ -158,3 +173,11 @@ func (w *waitsKept) end(ctx context.Context, module, fp, slot, how string) bool
w.said(ctx, "wait-ended", t, how)
return true
}
// bucket is the state waits once it is on the bus; none before, and none for a form kept in memory alone.
func (w *waitsKept) bucket() jetstream.KeyValue {
if w.kv == nil {
return nil
}
return w.kv.KV()
}
@@ -1,6 +1,9 @@
package main
import (
"context"
"slices"
"strings"
"testing"
"time"
@@ -24,3 +27,44 @@ func TestAWaitBodyNamesItsPart(t *testing.T) {
t.Errorf("a revision wait says %+v, slot %q", rv, slotOf(rev))
}
}
// Design 47 H2 H-A-723, H-A-707 (5), DQ-53 (H-A-717): a key holds one wait. A pin wait held over a toolchain wait ends it as `replaced`
// and stands; an equal Hold writes nothing; a pin wait ended `failed` is deleted; its body says `package-wait-minutes` as its bound
// and carries publisher, revision and until, the package its path alone.
func TestAKeyHoldsTheBuildsOneWait(t *testing.T) {
var said []string
var bodies []waitBody
w := newWaitsKept(nil, func(_ context.Context, event, _ string, body any) error {
said = append(said, strings.SplitN(event, ".", 2)[0])
bodies = append(bodies, body.(waitBody))
return nil
}, time.Hour, t.Logf)
ctx := context.Background()
since := time.Unix(100, 0)
tc := build.ToolchainWait{Module: "m", Fingerprint: fpw, Language: "go", Version: "go1.26.0", Since: since}
pin := build.ToolchainWait{Module: "m", Fingerprint: fpw, Package: "git.x/y/sdk", Publisher: "sdk@src2:p", Revision: 7, Bound: "10m0s", Since: since, Until: since.Add(10 * time.Minute)}
w.Hold(ctx, tc)
w.Hold(ctx, tc)
if !slices.Equal(said, []string{"wait-held"}) {
t.Fatalf("a toolchain wait held twice said %v, want one wait-held", said)
}
w.Hold(ctx, pin)
if !slices.Equal(said, []string{"wait-held", "wait-ended", "wait-held"}) || bodies[1].How != build.WaitReplaced || bodies[1].On != "toolchain:go@go1.26.0" {
t.Fatalf("a pin wait over a toolchain wait said %v, %+v, want the toolchain wait ended replaced", said, bodies)
}
if p := bodies[2]; p.Bound != "10m0s" || p.Publisher != "sdk@src2:p" || p.Revision != 7 || !p.Until.Equal(pin.Until) || p.Part != "git.x/y/sdk" || p.On != "package:git.x/y/sdk" {
t.Errorf("the pin wait says %+v", p)
}
w.Hold(ctx, pin)
if len(said) != 3 {
t.Errorf("a Hold equal to the standing wait said %v, want nothing", said[3:])
}
if w.End(ctx, "m", fpw, build.WaitRan) {
t.Error("a pin wait ended ran")
}
if !w.End(ctx, "m", fpw, build.WaitFailed) || len(w.held) != 0 || bodies[len(bodies)-1].How != build.WaitFailed {
t.Errorf("a pin wait ended failed: held %v, said %v", w.held, said)
}
}
var fpw = "src2:" + strings.Repeat("a", 64)
@@ -106,4 +106,13 @@ func TestBuilderStuckAnswersTheFingerprintWhole(t *testing.T) {
if len(rows) != 1 || rows[0].FP != fp1 {
t.Fatalf("stuck = %+v, want the fingerprint %s whole", rows, fp1)
}
// design 47 H2 H-A-707 (5): a key building with no ask names the wait the state waits holds for it
pin, _ := json.Marshal(ToolchainWait{Module: "m", Fingerprint: fp1, Package: "git.x/y/sdk", Publisher: "sdk@src2:p", Revision: 7})
out, err = StuckReader{Builds: stateOf(map[string][]byte{k: building}), Waits: stateOf(map[string][]byte{k: pin})}.Stuck("m")
if err != nil {
t.Fatal(err)
}
if rows = out["stuck"].([]Stuck); len(rows) != 1 || len(rows[0].Waits) != 1 || rows[0].Waits[0].Publisher != "sdk@src2:p" || rows[0].Waits[0].Package != "git.x/y/sdk" {
t.Errorf("a key building with no ask: stuck = %+v, want its pin wait", rows)
}
}
@@ -229,24 +229,28 @@ type StuckAsk struct {
// Stuck is one build key that waits on an ask.
type Stuck struct {
Key string `json:"key"`
Module string `json:"module"`
FP string `json:"fp"`
State string `json:"state"`
Step string `json:"step"`
By string `json:"by"`
Since time.Time `json:"since,omitzero"`
Age string `json:"age,omitempty"`
Asks []StuckAsk `json:"asks"`
ReAsk *ReAsk `json:"reask,omitempty"`
Unasked bool `json:"unasked,omitempty"`
NoneStep int `json:"asks_with_no_step"`
Key string `json:"key"`
Module string `json:"module"`
FP string `json:"fp"`
State string `json:"state"`
Step string `json:"step"`
By string `json:"by"`
Since time.Time `json:"since,omitzero"`
Age string `json:"age,omitempty"`
Asks []StuckAsk `json:"asks"`
ReAsk *ReAsk `json:"reask,omitempty"`
// Waits is the wait a key building with no ask holds in the state waits, so a key waiting for a pin is told apart from one stuck.
Waits []ToolchainWait `json:"waits,omitempty"`
Unasked bool `json:"unasked,omitempty"`
NoneStep int `json:"asks_with_no_step"`
}
// StuckReader reads what builder_stuck answers.
type StuckReader struct {
Builds StateRead
ReAsks StateRead
// Waits is the builder's state waits: a key building with no ask gains the wait it holds, read from it (design 47 H2 H-A-707 (5)).
Waits StateRead
// Steps are the build seats' steps states (`<seat>.steps`, design 47 G A-82), by build seat.
Steps map[string]StateRead
// StepsOf, when set, reads the steps state of a build seat the seat list names (`<seat>.steps`), instead of Steps: the seats
@@ -404,6 +408,15 @@ func (r StuckReader) Stuck(module string) (map[string]any, error) {
}
sort.Slice(s.Asks, func(i, j int) bool { return s.Asks[i].Language < s.Asks[j].Language })
s.Unasked = v.State == builds.BuildBuilding && len(v.Asks) == 0
if s.Unasked && r.Waits.Get != nil {
reads++
if raw, _, err := r.Waits.Get(key); err == nil && raw != nil {
var w ToolchainWait
if json.Unmarshal(raw, &w) == nil && w.Module != "" {
s.Waits = append(s.Waits, w)
}
}
}
if r.ReAsks.Get != nil {
reads++
if raw, _, err := r.ReAsks.Get(key); err == nil && raw != nil {
@@ -31,6 +31,8 @@ const (
WaitReleased = "released"
// WaitFailed ends a revision wait at its bound (design 47 H2 H-A-577 (1)).
WaitFailed = "failed"
// WaitReplaced ends a wait a build's next wait took the key from (design 47 H2 H-A-723 (1)).
WaitReplaced = "replaced"
// WaitKindRevision is the Kind of a revision wait.
WaitKindRevision = "revision"
)
@@ -50,6 +52,18 @@ type ToolchainWait struct {
Kind string `json:"kind,omitempty"`
Revision int64 `json:"revision,omitempty"`
Bound string `json:"bound,omitempty"`
// Publisher (`<module>@<fingerprint>`) and Until are the pin wait's: a build waiting for a blank pin's version holds Package, the
// package's path alone, Publisher the build it waits for, Revision the catalog revision the build holds and Until its start plus
// `package-wait-minutes` (design 47 H2 H-A-707 (5), DQ-53, H-A-717).
Publisher string `json:"publisher,omitempty"`
Until time.Time `json:"until,omitzero"`
}
// SameWait says w and o are one wait: every field but when it began and when it ends, so a Hold of the wait that stands changes
// nothing (design 47 H2 H-A-723 (1)).
func (w ToolchainWait) SameWait(o ToolchainWait) bool {
w.Since, w.Until, o.Since, o.Until = time.Time{}, time.Time{}, time.Time{}, time.Time{}
return w == o
}
// IsRevision says whether the wait is a step ask's wait for its revision, kept under `<module>.<fingerprint>.revision`.