diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/asks.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/asks.go new file mode 100644 index 000000000..46b666e8d --- /dev/null +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/asks.go @@ -0,0 +1,131 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "sort" + "strings" + "time" + + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" + stdio "git.novox.be/novox/mesh-sdk/go" + + "mesh-delivery-builder/internal/build" +) + +// dropVerbKey is the seat verb builder_ask_drop asks (the manifest's invokes). +const dropVerbKey = "seat:" + meshbuilds.Seat + "." + meshbuilds.VerbDrop + +// takenReader is the builder's state taken, as the bundle reads it (stdio.State). +type takenReader interface { + Keys() ([]string, error) + Get(key string) (*stdio.StateEntry, error) +} + +// TakenAsk is an ask of the seat mesh-builds the builder took (its id in the state taken, kept for ask-taken-bound), or +// whose drop it keeps before its pull, as builder_asks shows it. +type TakenAsk struct { + Ask string `json:"ask"` + At time.Time `json:"at,omitzero"` + By string `json:"by,omitempty"` + Why string `json:"why,omitempty"` +} + +// WaitingRow is an ask the runtime reads as waiting on the seat's work queue, as builder_asks shows it: State is +// waiting (taken by no holder yet), waiting again (handed out before and given back, its id not taken) or held (handed +// out again while the builder holds its id in the state taken: its run answers it). +type WaitingRow struct { + stdio.WaitingAsk + State string `json:"state"` +} + +// The states of a waiting ask. +const ( + StateWaiting = "waiting" + StateWaitingAgain = "waiting again" + StateHeld = "held" +) + +// asksTools are builder_asks, what waits on and is taken from the seat mesh-builds' work queue: the waiting asks read +// through the runtime (stdio.QueueWaiting, H-A-410), the taken ones and the drops kept before a pull from the builder's +// state taken; and builder_ask_drop, which ends one ask through the seat's verb drop once the hand act is recorded +// (design 47 H2 §777, §778; H-A-401, H-A-404, H-A-410). +func asksTools(waiting func(seat string) (stdio.Waiting, error), taken takenReader, drop func(meshbuilds.DropInput) (json.RawMessage, error), now func() time.Time, acts meshhandactsclient.Recorder) []stdio.Tool { + str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) } + return []stdio.Tool{ + {Name: build.AsksTool, + Description: "What waits on and is taken from the seat mesh-builds' work queue: waiting, each build step ask the queue holds unacknowledged, oldest " + + "first, with its id, the module that asked it, its age and its state: " + StateWaiting + " (taken by no holder yet), " + StateWaitingAgain + + " (handed out before and given back) or " + StateHeld + " (its id taken by the builder, its run answers it); capped when the runtime cut the read; taken, each ask the builder took " + + "within ask-taken-bound, with when; dropped, each drop kept for an ask not pulled yet, with who dropped it and why. Read-only.", + Input: map[string]any{"type": "object", "properties": map[string]any{}}, + Run: func(map[string]any) (any, error) { + w, err := waiting(meshbuilds.Seat) + if err != nil { + return nil, fmt.Errorf("the waiting asks of %s are not read: %w", meshbuilds.Seat, err) + } + keys, err := taken.Keys() + if err != nil { + return nil, fmt.Errorf("the state taken is not read: %w", err) + } + sort.Strings(keys) + tk, dr := []TakenAsk{}, []TakenAsk{} + for _, k := range keys { + e, err := taken.Get(k) + if err != nil || e == nil { + continue + } + if id, ok := strings.CutPrefix(k, build.DroppedPrefix); ok { + var d build.Dropped + _ = json.Unmarshal(e.Value, &d) + dr = append(dr, TakenAsk{Ask: id, At: e.At, By: d.By, Why: d.Why}) + } else if id, ok := strings.CutPrefix(k, meshbuilds.Seat+"."); ok { + tk = append(tk, TakenAsk{Ask: id, At: e.At}) + } + } + held := map[string]bool{} + for _, a := range tk { + held[a.Ask] = true + } + rows := make([]WaitingRow, 0, len(w.Asks)) + for _, a := range w.Asks { + r := WaitingRow{WaitingAsk: a, State: StateWaiting} + if held[a.ID] { + r.State = StateHeld + } else if a.Again { + r.State = StateWaitingAgain + } + rows = append(rows, r) + } + return map[string]any{"seat": meshbuilds.Seat, "waiting": rows, "capped": w.Capped, "taken": tk, "dropped": dr}, nil + }}, + {Name: build.AskDropTool, + Description: "End one ask on the seat mesh-builds' work queue through the seat's verb drop: an ask the builder runs is cancelled, one not " + + "pulled yet is kept dropped for ask-taken-bound; either way its step ends could-not-run naming the drop and who asked it. Written in " + + "the hand-act log (mesh-hand-acts).", + Input: map[string]any{"type": "object", "required": []string{"ask", "why"}, "properties": map[string]any{ + "ask": map[string]any{"type": "string", "description": "the ask's id"}, + "why": map[string]any{"type": "string", "description": "why it is dropped, in plain words"}}}, + Run: func(args map[string]any) (any, error) { + ask, why := str(args, "ask"), str(args, "why") + if ask == "" || why == "" { + return nil, errors.New("give the ask's id and why it is dropped") + } + if _, err := acts.Record(meshhandactsclient.Request{What: fmt.Sprintf("dropped the ask %s of the seat %s by hand", ask, meshbuilds.Seat), + Why: why, Cause: build.AskDropTool, Verb: meshbuilds.VerbDrop}); err != nil { + return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was dropped: %w", err) + } + raw, err := drop(meshbuilds.DropInput{ID: ask, Why: why, By: stdio.CallerOf(args)}) + if err != nil { + return nil, fmt.Errorf("the seat %s's drop did not answer for %s: %w", meshbuilds.Seat, ask, err) + } + var a meshbuilds.DropAnswer + if err := json.Unmarshal(raw, &a); err != nil { + return nil, err + } + return map[string]any{"ask": ask, "dropped": true, "held": a["held"]}, nil + }}, + } +} diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go index 37a94c661..7cf542486 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go @@ -442,6 +442,9 @@ func main() { } return e.Value, true, nil }} + tools = append(tools, asksTools(stdio.QueueWaiting, stdio.State(stdio.TakenState), func(in meshbuilds.DropInput) (json.RawMessage, error) { + return stdio.Ask(dropVerbKey, in) + }, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...) tools = append(tools, waitsTools(wr, buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...) tools = append(tools, toolchainsTools(stdio.State(buildrun.ToolchainsHeardState), meshhandactsclient.Recorder{Ask: stdio.Ask}, time.Now)...) // The builder's expiring keys (state bounds): what each bounds and when it ends. diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/toolchains_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/toolchains_test.go index cfaa9dd2e..59364f5e5 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/toolchains_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/toolchains_test.go @@ -2,14 +2,19 @@ package main import ( "encoding/json" + "errors" + "reflect" "strings" "testing" "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" stdio "git.novox.be/novox/mesh-sdk/go" + + "mesh-delivery-builder/internal/build" ) type fakeHeard struct{ m map[string]json.RawMessage } @@ -70,4 +75,38 @@ func TestToolchainsToolsListAndDropAKeyAfterTheHandAct(t *testing.T) { if _, err := run(buildrun.ToolchainsDropTool, map[string]any{"key": "ace.go", "why": "ace left"}); err != nil || len(st.m) != 0 || asked < 2 { t.Fatalf("the drop: %v, keys left %d", err, len(st.m)) } + + // Design 47 H2 §777, H-A-410: builder_asks answers the waiting asks the runtime reads, held told from waiting again by + // the state taken, and the taken asks and the drops kept; builder_ask_drop asks the seat's drop after the hand act. + taken := &fakeHeard{m: map[string]json.RawMessage{"mesh-builds.a2": json.RawMessage(`{"seat":"mesh-builds","ask":"a2"}`), + "dropped.a4": json.RawMessage(`{"by":"op@shanks","why":"by hand"}`)}} + var dropped []meshbuilds.DropInput + asks := asksTools(func(seat string) (stdio.Waiting, error) { + if seat != meshbuilds.Seat { + t.Errorf("the waiting asks were read of %s", seat) + } + return stdio.Waiting{Asks: []stdio.WaitingAsk{{ID: "a1", Module: "mesh-delivery-pipeline"}, {ID: "a2", Again: true}, {ID: "a3", Again: true}}}, nil + }, taken, func(in meshbuilds.DropInput) (json.RawMessage, error) { + dropped = append(dropped, in) + return json.RawMessage(`{"held":true}`), nil + }, time.Now, acts) + tools = asks + got, err = run(build.AsksTool, nil) + if err != nil { + t.Fatal(err) + } + a := got.(map[string]any) + w := a["waiting"].([]WaitingRow) + if len(w) != 3 || w[0].State != StateWaiting || w[1].State != StateHeld || w[2].State != StateWaitingAgain || + !reflect.DeepEqual(a["taken"], []TakenAsk{{Ask: "a2"}}) || !reflect.DeepEqual(a["dropped"], []TakenAsk{{Ask: "a4", By: "op@shanks", Why: "by hand"}}) { + t.Errorf("builder_asks = %+v", a) + } + failAct = errors.New("no log") + if _, err := run(build.AskDropTool, map[string]any{"ask": "a1", "why": "stuck"}); err == nil || len(dropped) != 0 { + t.Fatalf("a hand act not recorded still dropped: %v", err) + } + failAct = nil + if got, err := run(build.AskDropTool, map[string]any{"ask": "a1", "why": "stuck"}); err != nil || len(dropped) != 1 || dropped[0].ID != "a1" || dropped[0].Why != "stuck" || got.(map[string]any)["held"] != true { + t.Fatalf("the drop: %v, %v, asked %+v", got, err, dropped) + } } diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go index af5ae06d4..6f667f13f 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go @@ -475,19 +475,6 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // modules again (design 47 G A-82, §204). // The runner's build step ask, the builder's one entry point for a candidate (design 47 G A-97). b.StepEntry = newEntryWaits(branches, half.manifests).entry - // The seat mesh-builds' verbs reserved, answered here (A-115, A-127): the reservations, the catalog half's carriers and the seat - // mesh-catalog's versions; and published, from the reservations and releases (H-A-328). - stopReserved, err := calls.AnswerSeat(meshbuilds.Seat, seatlist.Definition(seatGate.Reader, meshbuilds.Seat), map[string]process.SeatHandler{ - meshbuilds.VerbReserved: reservedVerb(ctx, reservedReader(ctx, reserved.r.Store), versions.CarriersFrom(versions.EntriesOf(branches)), catalogReleased(calls.Call), - func(name string) { - logger.Printf("reserved: no entry carries the name %s; the entries looked at: %s", name, versions.LookedAt(versions.EntriesOf(branches))) - }), - // published (H-A-328): whether a package built at a fingerprint is published, asked by the runner writing a wait on it. - meshbuilds.VerbPublished: publishedVerb(ctx, reserved.r.PublishedOf)}) - if err != nil { - return fmt.Errorf("answering the verb reserved: %w", err) - } - defer stopReserved() // The seat's work queue (design 47 H2 §777): pulled through its one worker, each ask taken once in the state taken. takenBound, err := askTakenBound() if err != nil { @@ -500,6 +487,21 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro say: func(ctx context.Context, subject, id string, body any) error { return emitSeatEvent(ctx, js, cred, subject, id, body) }, logf: logger.Printf, now: time.Now, running: map[string]*runningAsk{}} + // The seat mesh-builds' verbs reserved, answered here (A-115, A-127): the reservations, the catalog half's carriers and the seat + // mesh-catalog's versions; and published, from the reservations and releases (H-A-328). + stopReserved, err := calls.AnswerSeat(meshbuilds.Seat, seatlist.Definition(seatGate.Reader, meshbuilds.Seat), map[string]process.SeatHandler{ + meshbuilds.VerbReserved: reservedVerb(ctx, reservedReader(ctx, reserved.r.Store), versions.CarriersFrom(versions.EntriesOf(branches)), catalogReleased(calls.Call), + func(name string) { + logger.Printf("reserved: no entry carries the name %s; the entries looked at: %s", name, versions.LookedAt(versions.EntriesOf(branches))) + }), + // published (H-A-328): whether a package built at a fingerprint is published, asked by the runner writing a wait on it. + meshbuilds.VerbPublished: publishedVerb(ctx, reserved.r.PublishedOf), + // drop (§778; H-A-401, H-A-404): an ask its caller no longer waits for ends could-not-run naming the drop. + meshbuilds.VerbDrop: dropVerb(steps)}) + if err != nil { + return fmt.Errorf("answering the verb reserved: %w", err) + } + defer stopReserved() if err := serveSteps(ctx, conn, js, steps, &ackWait); err != nil { return err } diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reservedverb_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reservedverb_test.go index e7f1040cc..0d652727f 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reservedverb_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reservedverb_test.go @@ -3,14 +3,94 @@ package main import ( "context" "encoding/json" + "strings" + "sync" "testing" + "time" + + "github.com/nats-io/nats.go" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + stdio "git.novox.be/novox/mesh-sdk/go" + + "mesh-delivery-builder/internal/build" "mesh-delivery-builder/internal/versions" ) +// takenFake is the state taken: keys created once, with their time to live. +type takenFake struct { + mu sync.Mutex + vals map[string][]byte + ttls map[string]time.Duration +} + +func (f *takenFake) TakeOnce(seat, ask string, bound time.Duration) (bool, error) { + _, err := f.CreateTTL(seat+"."+ask, map[string]string{"seat": seat, "ask": ask}, bound) + return err == nil, nil +} +func (f *takenFake) Get(key string) (*stdio.StateEntry, error) { + f.mu.Lock() + defer f.mu.Unlock() + if v, ok := f.vals[key]; ok { + return &stdio.StateEntry{Key: key, Value: v}, nil + } + return nil, nil +} +func (f *takenFake) CreateTTL(key string, value any, ttl time.Duration) (uint64, error) { + f.mu.Lock() + defer f.mu.Unlock() + if _, ok := f.vals[key]; ok { + return 0, stdio.ErrChanged + } + raw, _ := json.Marshal(value) + f.vals[key], f.ttls[key] = raw, ttl + return 1, nil +} + +// blockingBuilder runs a build step until its context ends, and answers the drop that ended it. +type blockingBuilder struct{ started chan string } + +func (b blockingBuilder) BuildStep(ctx context.Context, ask string, run meshbuilds.StepRun) meshbuilds.StepEnded { + b.started <- ask + <-ctx.Done() + end := meshbuilds.StepEnded{ID: ask, Pipeline: run.Pipeline, Outcome: meshbuilds.StepEndedOutcomeCouldNotRun} + if d := build.DroppedOf(ctx); d != nil { + end.Said = d.Error() + } + return end +} + +// msgFake is a message pulled from the seat's work queue. +type msgFake struct { + h nats.Header + data []byte + acked chan struct{} +} + +func (m *msgFake) Data() []byte { return m.data } +func (m *msgFake) Headers() nats.Header { return m.h } +func (m *msgFake) Ack() error { close(m.acked); return nil } +func (m *msgFake) Nak() error { return nil } +func (m *msgFake) Term() error { return nil } +func (m *msgFake) InProgress() error { return nil } + +// await fails the test unless c yields within 5 s. +func await[T any](t *testing.T, c <-chan T, what string) { + t.Helper() + select { + case <-c: + case <-time.After(5 * time.Second): + t.Fatalf("%s did not happen within 5 s", what) + } +} + +func stepMsg(id string) *msgFake { + data, _ := json.Marshal(meshbuilds.StepRun{Pipeline: "main.1.m", Step: meshbuilds.StepRunStepBuild, Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: "src2:x"}}}) + return &msgFake{h: nats.Header{"x-event-id": []string{id}}, data: data, acked: make(chan struct{})} +} + // Design 47 G A-115, A-127: the builder's process answers the verb reserved, given a name, from the catalog half's carriers and the // seat mesh-catalog's versions, and given a module from its reservations. func TestTheProcessAnswersReserved(t *testing.T) { @@ -47,4 +127,55 @@ func TestTheProcessAnswersReserved(t *testing.T) { if _, err := handle("", json.RawMessage(`{"module":`)); err == nil { t.Error("arguments that are no JSON were answered") } + + // Design 47 H2 §778, H-A-401, H-A-404: the verb drop cancels an ask that runs here, and keeps the drop of one not + // pulled yet in the state taken for ask-taken-bound; both end could-not-run naming the drop and who asked it. + taken := &takenFake{vals: map[string][]byte{}, ttls: map[string]time.Duration{}} + started := make(chan string, 2) + var mu sync.Mutex + said := map[string]meshbuilds.StepEnded{} + var subjects []string + q := &stepQueue{node: "n1", builder: blockingBuilder{started: started}, taken: taken, takenBound: 10 * time.Minute, + inProgress: func() time.Duration { return 0 }, logf: t.Logf, now: time.Now, running: map[string]*runningAsk{}, + say: func(_ context.Context, subject, id string, body any) error { + mu.Lock() + defer mu.Unlock() + subjects = append(subjects, subject) + if end, ok := body.(meshbuilds.StepEnded); ok { + said[id] = end + } + return nil + }} + drop := dropVerb(q) + running := stepMsg("ask-1") + q.take(t.Context(), running) + await(t, started, "the build of ask-1") + if got, err := drop("mesh-delivery-pipeline", json.RawMessage(`{"id":"ask-1","why":"its step ended by its bound"}`)); err != nil || got.(meshbuilds.DropAnswer)["held"] != true { + t.Fatalf("drop of a running ask = %v, %v; want held", got, err) + } + await(t, running.acked, "the end of ask-1") + if got, err := drop("mesh-delivery-pipeline", json.RawMessage(`{"id":"ask-2","by":"op@shanks","why":"by hand"}`)); err != nil || got.(meshbuilds.DropAnswer)["held"] != false { + t.Fatalf("drop of an ask not pulled = %v, %v; want not held", got, err) + } + if taken.ttls[build.DroppedKey("ask-2")] != 10*time.Minute { + t.Errorf("the drop of ask-2 is kept for %v, want ask-taken-bound", taken.ttls[build.DroppedKey("ask-2")]) + } + later := stepMsg("ask-2") + q.take(t.Context(), later) + await(t, started, "the build step of ask-2") + await(t, later.acked, "the end of ask-2") + mu.Lock() + if e := said["ask-1"]; e.Outcome != meshbuilds.StepEndedOutcomeCouldNotRun || e.Said != "the ask was dropped by mesh-delivery-pipeline through the seat mesh-builds' drop: its step ended by its bound" { + t.Errorf("the running ask ended %+v", e) + } + if e := said["ask-2"]; e.Outcome != meshbuilds.StepEndedOutcomeCouldNotRun || !strings.Contains(e.Said, "dropped by op@shanks") { + t.Errorf("the ask dropped before its pull ended %+v", e) + } + if _, ok := taken.vals["mesh-builds.ask-2"]; ok || strings.Count(strings.Join(subjects, " "), "ask-taken") != 1 { + t.Errorf("the ask dropped before its pull was taken: subjects said %v", subjects) + } + mu.Unlock() + if _, err := drop("", json.RawMessage(`{"id":"ask-3"}`)); err == nil { + t.Error("a drop with no why was taken") + } } diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/stepqueue.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/stepqueue.go index 9b1640d1f..29597fdbf 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/stepqueue.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/stepqueue.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" "sync" "sync/atomic" "time" @@ -17,6 +18,8 @@ import ( "git.novox.be/novox/mesh-sdk/go/buscompose" "git.novox.be/novox/mesh-sdk/go/process" "git.novox.be/novox/mesh-sdk/go/seatdef" + + "mesh-delivery-builder/internal/build" ) // **The seat mesh-builds takes its asks from a work queue and says ask-taken** (design 47 H2 §777, §775; ADR 0525 §1, §2, @@ -32,9 +35,12 @@ const ( defaultAskTakenBound = 10 * time.Minute ) -// askTaker is the state taken (process.State.TakeOnce). +// askTaker is the state taken (process.State): an ask taken once by its id, and the drop of an ask not pulled yet kept +// under build.DroppedKey (H-A-404). type askTaker interface { TakeOnce(seat, ask string, bound time.Duration) (bool, error) + Get(key string) (*stdio.StateEntry, error) + CreateTTL(key string, value any, ttl time.Duration) (uint64, error) } // stepBuilder is build.Builder's BuildStep. @@ -104,6 +110,17 @@ func (q *stepQueue) take(ctx context.Context, m queueMsg) { q.logf("the ask %s (%s) was handed again while it runs here: its run answers it", id, run.Pipeline) return } + // An ask dropped before it was pulled ends could-not-run naming the drop, with no build (H-A-404). + if d := q.droppedAsk(id); d != nil { + work, cancel := context.WithCancelCause(ctx) + cancel(d) + ra := &runningAsk{run: run, taken: q.now(), cancel: cancel} + q.running[id] = ra + q.mu.Unlock() + q.logf("the ask %s (%s) was dropped before it was pulled: %s", id, run.Pipeline, d.Error()) + go q.build(work, m, id, ra) + return + } first, err := q.taken.TakeOnce(meshbuilds.Seat, id, q.takenBound) if err != nil { q.mu.Unlock() @@ -159,6 +176,59 @@ func (q *stepQueue) build(ctx context.Context, m queueMsg, id string, ra *runnin _ = m.Ack() } +// droppedAsk is the drop kept for the ask id in the state taken, nil for none. +func (q *stepQueue) droppedAsk(id string) *build.Dropped { + e, err := q.taken.Get(build.DroppedKey(id)) + if err != nil || e == nil { + return nil + } + var d build.Dropped + if json.Unmarshal(e.Value, &d) != nil { + return nil + } + return &d +} + +// drop is the seat mesh-builds' verb drop (§778; H-A-401, H-A-404): the drop is kept in the state taken under +// build.DroppedKey for ask-taken-bound, so a pull of the ask within the bound ends it, and an ask that runs here is +// cancelled; both end the step could-not-run naming the drop and who asked it. held says whether it ran here. +func (q *stepQueue) drop(id, by, why string) (held bool, err error) { + if strings.TrimSpace(id) == "" || strings.TrimSpace(why) == "" { + return false, errors.New("give the ask's id and why it is dropped") + } + d := &build.Dropped{By: by, Why: why} + if _, err := q.taken.CreateTTL(build.DroppedKey(id), d, q.takenBound); err != nil && !errors.Is(err, stdio.ErrChanged) { + return false, fmt.Errorf("the drop of %s is not kept in the state %s: %w", id, process.TakenState, err) + } + q.mu.Lock() + ra := q.running[id] + q.mu.Unlock() + if ra != nil { + ra.cancel(d) + } + q.logf("the ask %s is dropped by %s: %s", id, by, why) + return ra != nil, nil +} + +// dropVerb is the handler of the seat mesh-builds' verb drop: who drops it is the input's by, else the caller. +func dropVerb(q *stepQueue) process.SeatHandler { + return func(caller string, body json.RawMessage) (any, error) { + var in meshbuilds.DropInput + if err := json.Unmarshal(body, &in); err != nil { + return nil, fmt.Errorf("the arguments do not fit the verb drop: %w", err) + } + by := in.By + if by == "" { + by = caller + } + held, err := q.drop(in.ID, by, in.Why) + if err != nil { + return nil, err + } + return meshbuilds.DropAnswer{"held": held}, nil + } +} + // running answers the asks this builder runs now, by id. func (q *stepQueue) runningAsks() map[string]runningAsk { q.mu.Lock() diff --git a/modules/mesh-delivery-builder/internal/build/drop.go b/modules/mesh-delivery-builder/internal/build/drop.go new file mode 100644 index 000000000..ef026b636 --- /dev/null +++ b/modules/mesh-delivery-builder/internal/build/drop.go @@ -0,0 +1,48 @@ +package build + +import ( + "context" + "errors" + "fmt" +) + +// **A dropped ask ends its step could-not-run, naming the drop** (design 47 H2 §777, §778; H-A-401, H-A-404; ADR 0664 §4): +// the seat mesh-builds' drop ends an ask its caller no longer waits for. A taken ask that runs is cancelled with a Dropped +// cause; one not pulled yet is kept in the state taken under DroppedKey, and its pull ends it the same way. Either way the +// build step's finish answers it could-not-run with Dropped's words, so no build runs for it after. + +// The tools of the seat's queue (§777's tools table): AsksTool reads what waits and what is taken, AskDropTool ends one +// ask through the seat's verb drop, the hand act recorded first. +const ( + AsksTool = "builder_asks" + AskDropTool = "builder_ask_drop" +) + +// DroppedPrefix is the key prefix in the state taken of a drop of an ask not pulled yet (H-A-404). +const DroppedPrefix = "dropped." + +// DroppedKey is the key in the state taken of the drop of the ask id. +func DroppedKey(id string) string { return DroppedPrefix + id } + +// Dropped is why a step's context was cancelled by the seat's verb drop, and the value its key keeps: who asked it and why. +type Dropped struct { + By string `json:"by,omitempty"` + Why string `json:"why"` +} + +func (d *Dropped) Error() string { + by := d.By + if by == "" { + by = "its asker" + } + return fmt.Sprintf("the ask was dropped by %s through the seat mesh-builds' drop: %s", by, d.Why) +} + +// DroppedOf is the drop that cancelled ctx, nil when none did. +func DroppedOf(ctx context.Context) *Dropped { + var d *Dropped + if errors.As(context.Cause(ctx), &d) { + return d + } + return nil +} diff --git a/modules/mesh-delivery-builder/internal/build/step.go b/modules/mesh-delivery-builder/internal/build/step.go index 8002e4712..cd27c0ad8 100644 --- a/modules/mesh-delivery-builder/internal/build/step.go +++ b/modules/mesh-delivery-builder/internal/build/step.go @@ -27,6 +27,10 @@ func (b *Builder) BuildStep(ctx context.Context, ask string, run meshbuilds.Step // finish is the one place the ask is answered: one journal line, "answered " // (design 47 H2 §792; H-A-320 (1), H-A-333). finish := func(outcome, said string) meshbuilds.StepEnded { + // An ask dropped through the seat's drop ends could-not-run naming the drop, whatever its run had reached (H-A-401). + if d := DroppedOf(ctx); d != nil { + outcome, said = meshbuilds.StepEndedOutcomeCouldNotRun, d.Error() + } at := b.now() end.Ended, end.Outcome, end.Said = &at, outcome, said movers := make([]string, 0, len(run.Entries)) @@ -40,6 +44,9 @@ func (b *Builder) BuildStep(ctx context.Context, ask string, run meshbuilds.Step b.logf("answered %s %s %s %s %s", ask, run.Pipeline, mv, end.Step, outcome) return end } + if DroppedOf(ctx) != nil { // dropped before it was pulled (H-A-404) + return finish(meshbuilds.StepEndedOutcomeCouldNotRun, "") + } if len(run.Entries) == 0 { return finish(meshbuilds.StepEndedOutcomeNothingToRun, "the ask names no entry") } diff --git a/modules/mesh-delivery-builder/internal/build/step_test.go b/modules/mesh-delivery-builder/internal/build/step_test.go index 2025f3988..1cab29a70 100644 --- a/modules/mesh-delivery-builder/internal/build/step_test.go +++ b/modules/mesh-delivery-builder/internal/build/step_test.go @@ -58,6 +58,13 @@ func TestABuildStepAnswersItsEnd(t *testing.T) { if end := ask(meshbuilds.BuildEntry{Module: "m", Fingerprint: fp1, Commit: "other"}); end.Outcome != meshbuilds.StepEndedOutcomeCouldNotRun { t.Errorf("an entry at another commit: %+v", end) } + // H-A-401, H-A-404: an ask dropped through the seat's drop ends could-not-run naming the drop and who asked it. + dropped, drop := context.WithCancelCause(t.Context()) + drop(&Dropped{By: "mesh-delivery-pipeline", Why: "its step ended by its bound"}) + if end := r.b.BuildStep(dropped, "ask9", meshbuilds.StepRun{Pipeline: "main.1.m", Step: meshbuilds.StepRunStepBuild, Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}); end.Outcome != meshbuilds.StepEndedOutcomeCouldNotRun || + end.Said != "the ask was dropped by mesh-delivery-pipeline through the seat mesh-builds' drop: its step ended by its bound" || end.ID != "ask9" { + t.Errorf("a dropped ask: %+v", end) + } // A build already done is passed at once, naming each entry as it was asked. done, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, Manifest: mdigest('a')}) if _, err := r.st.Create(t.Context(), r.key, done); err != nil { diff --git a/modules/mesh-delivery-builder/manifest_test.go b/modules/mesh-delivery-builder/manifest_test.go index 3bd70a2a3..9c6fa84ed 100644 --- a/modules/mesh-delivery-builder/manifest_test.go +++ b/modules/mesh-delivery-builder/manifest_test.go @@ -146,7 +146,7 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1 // The watchdogs seat's presence says a node's machine is heard (issue 1966, ADR 0596 §2, ADR 0668 §3). if want := []string{"seat:mesh-catalog.entry", "seat:mesh-catalog.registrations", "seat:mesh-catalog.seat-definitions", "seat:mesh-catalog.revision", "seat:mesh-delivery.source", "seat:container-image-registry.publish", "seat:container-image-registry.remove", "seat:container-image-registry.versions", "seat:git.tag", "seat:git.tags", - "seat:go-package-registry.publish", "seat:go-package-registry.remove", "seat:go-package-registry.versions", "seat:mesh-artifact-sweep.held", "seat:mesh-builds.reserved", "seat:mesh-catalog.versions", "seat:mesh-hand-acts.hand-act", "seat:mesh-module-assigner.assignments", "seat:mesh-watchdogs.presence", "seat:node-build-go.drop", "seat:node-build-image.drop", "seat:node-build-node.drop", "seat:node-engine.manifest-reads", "seat:npm-package-registry.publish", "seat:npm-package-registry.remove", + "seat:go-package-registry.publish", "seat:go-package-registry.remove", "seat:go-package-registry.versions", "seat:mesh-artifact-sweep.held", "seat:mesh-builds.reserved", "seat:mesh-builds.drop", "seat:mesh-catalog.versions", "seat:mesh-hand-acts.hand-act", "seat:mesh-module-assigner.assignments", "seat:mesh-watchdogs.presence", "seat:node-build-go.drop", "seat:node-build-image.drop", "seat:node-build-node.drop", "seat:node-engine.manifest-reads", "seat:npm-package-registry.publish", "seat:npm-package-registry.remove", "seat:npm-package-registry.versions", "seat:oci-artifact-registry.publish", "seat:oci-artifact-registry.remove", "seat:oci-artifact-registry.versions", // A-231: the build asks on each build seat's queue delivery (accept.delivery.). "seat:node-build-go.accept.delivery", "seat:node-build-node.accept.delivery", "seat:node-build-image.accept.delivery"}; !reflect.DeepEqual(sorted(m.Invokes), sorted(want)) { @@ -176,7 +176,7 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1 t.Errorf("secrets-owner = %q", m.SecretsOwner) } // 1945: the builder's tools (builder_seat_queue: note 62), served by its tools bundle, and the event it says when it asks an ask again. - if want := []string{"builder_announce_again", "builder_ask_again", "builder_bounds", "builder_build", "builder_build_again", "builder_builds_since", "builder_engine_reads", "builder_first_view_again", "builder_first_views", + if want := []string{"builder_announce_again", "builder_ask_again", "builder_ask_drop", "builder_asks", "builder_bounds", "builder_build", "builder_build_again", "builder_builds_since", "builder_engine_reads", "builder_first_view_again", "builder_first_views", "builder_floor_again", "builder_floor_waits", "builder_old_form", "builder_publish_again", "builder_publish_queue", "builder_release", "builder_reserve_done", "builder_reserved", "builder_reserved_drop", "builder_seat_list", "builder_seat_list_again", "builder_seat_queue", "builder_stuck", "builder_toolchains", "builder_toolchains_drop", "builder_versions", "builder_waits"}; !reflect.DeepEqual(sorted(m.Tools), want) { t.Errorf("tools = %v, want %v", m.Tools, want) } @@ -202,9 +202,9 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1 if len(m.Logs) != 1 || m.Logs[0].Name != builds.BuilderLog || m.Logs[0].MaxAgeDays != builds.BuilderLogDays || m.Logs[0].MaxMiB != builds.BuilderLogMiB { t.Errorf("logs = %+v, want %s kept %d days in %d MiB", m.Logs, builds.BuilderLog, builds.BuilderLogDays, builds.BuilderLogMiB) } - // Design 47 G §52, §369, H-A-328: the seat mesh-builds, held once for the mesh, serving reserved and published. - if len(m.Claims) != 1 || m.Claims[0].Name != "mesh-builds" || m.Claims[0].Scope != "mesh" || !reflect.DeepEqual(m.Claims[0].Serves, []string{"reserved", "published"}) { - t.Errorf("claims = %+v, want mesh-builds, scope mesh, serving reserved and published", m.Claims) + // Design 47 G §52, §369, H-A-328, H2 §778: the seat mesh-builds, held once for the mesh, serving reserved, published and drop. + if len(m.Claims) != 1 || m.Claims[0].Name != "mesh-builds" || m.Claims[0].Scope != "mesh" || !reflect.DeepEqual(m.Claims[0].Serves, []string{"reserved", "published", "drop"}) { + t.Errorf("claims = %+v, want mesh-builds, scope mesh, serving reserved, published and drop", m.Claims) } // A-115: the process answers the verb itself and says so in `serves` beside its process, with no `bus`; the bundle serves no tool named so. var resources []map[string]any @@ -212,8 +212,8 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1 t.Fatal(err) } for _, r := range resources { - if r["type"] == "process" && !reflect.DeepEqual(r["serves"], []any{"seat:mesh-builds.reserved", "seat:mesh-builds.published"}) { - t.Errorf("the process serves %v, want seat:mesh-builds.reserved and published (A-115, H-A-328)", r["serves"]) + if r["type"] == "process" && !reflect.DeepEqual(r["serves"], []any{"seat:mesh-builds.reserved", "seat:mesh-builds.published", "seat:mesh-builds.drop"}) { + t.Errorf("the process serves %v, want seat:mesh-builds.reserved, published and drop (A-115, H-A-328, H2 §778)", r["serves"]) } } if slices.Contains(m.Tools, "reserved") { diff --git a/modules/mesh-delivery-builder/module.json b/modules/mesh-delivery-builder/module.json index 30ac44c5c..709ebe62f 100644 --- a/modules/mesh-delivery-builder/module.json +++ b/modules/mesh-delivery-builder/module.json @@ -8,7 +8,8 @@ "scope": "mesh", "serves": [ "reserved", - "published" + "published", + "drop" ] } ], @@ -36,6 +37,8 @@ "builder_toolchains", "builder_toolchains_drop", "builder_waits", + "builder_ask_drop", + "builder_asks", "builder_versions", "builder_first_views", "builder_first_view_again" @@ -89,6 +92,7 @@ "seat:go-package-registry.versions", "seat:mesh-artifact-sweep.held", "seat:mesh-builds.reserved", + "seat:mesh-builds.drop", "seat:mesh-catalog.versions", "seat:mesh-hand-acts.hand-act", "seat:mesh-module-assigner.assignments", @@ -317,7 +321,8 @@ "artifact": "program", "serves": [ "seat:mesh-builds.reserved", - "seat:mesh-builds.published" + "seat:mesh-builds.published", + "seat:mesh-builds.drop" ], "run": [ "./mesh-delivery-builder"