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 0d652727f..022a454c1 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 @@ -178,4 +178,39 @@ func TestTheProcessAnswersReserved(t *testing.T) { if _, err := drop("", json.RawMessage(`{"id":"ask-3"}`)); err == nil { t.Error("a drop with no why was taken") } + + // Design 47 H2 H-A-514: each ask's take and its ask-taken are its own worker's, started at the pull, so an ask whose + // ask-taken hangs holds neither the pull nor the next ask's ask-taken. + hang, release := make(chan struct{}), make(chan struct{}) + saidTaken := make(chan string, 4) + slow := &stepQueue{node: "n1", builder: blockingBuilder{started: make(chan string, 2)}, taken: &takenFake{vals: map[string][]byte{}, ttls: map[string]time.Duration{}}, + 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, _ string, _ any) error { + if subject == meshbuilds.SubjectAskTaken("ask-5") { + close(hang) + <-release + } + if strings.Contains(subject, "ask-taken") { + saidTaken <- subject + } + return nil + }} + pulled := make(chan struct{}) + go func() { + slow.take(t.Context(), stepMsg("ask-5")) + <-hang + slow.take(t.Context(), stepMsg("ask-6")) + close(pulled) + }() + await(t, pulled, "the pull of ask-6 while ask-5's ask-taken hangs") + select { + case s := <-saidTaken: + if s != meshbuilds.SubjectAskTaken("ask-6") { + t.Errorf("said %s first, want ask-6's ask-taken", s) + } + case <-time.After(5 * time.Second): + t.Error("ask-6's ask-taken waited behind ask-5's") + } + close(release) + await(t, saidTaken, "ask-5's ask-taken once let go") } 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 f664d419f..1f30a7084 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/stepqueue.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/stepqueue.go @@ -91,10 +91,9 @@ func askIDOf(h nats.Header) string { } // take handles one ask pulled from the queue: an ask that is no build step with an id is terminated, said; one this -// builder runs already (handed again while it runs) is left to the run that holds it; any other is taken (ask-taken said -// the first time its id is taken) and built on its own goroutine, in progress while it runs, acknowledged once its end is -// said. A repeat of an id taken before that runs here no longer (the builder started again) is answered as its builds -// stand now, read afresh by BuildStep (H-A-381 (1)): an entry built is answered built, never built twice. +// builder runs already (handed again while it runs) is left to the run that holds it; any other is held here and handed to +// a worker of its own, started at once (design 47 H2 H-A-514): the pull never waits on a state call or a publish of an +// earlier ask, so each ask's ask-taken is said within its bound whatever the others do. func (q *stepQueue) take(ctx context.Context, m queueMsg) { id := askIDOf(m.Headers()) var run meshbuilds.StepRun @@ -110,36 +109,43 @@ 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() - q.logf("the ask %s (%s) is not taken, the state %s refused it: %v; it is handed again", id, run.Pipeline, process.TakenState, err) - _ = m.Nak() - return - } work, cancel := context.WithCancelCause(ctx) ra := &runningAsk{run: run, taken: q.now(), cancel: cancel} q.running[id] = ra q.mu.Unlock() + go q.work(ctx, work, m, id, ra) +} + +// work is one ask's worker (H-A-514): an ask dropped before it was pulled ends could-not-run naming the drop, with no build +// (H-A-404); any other is taken once by its id in the state taken (ask-taken said the first time it is taken) and built, in +// progress while it runs, acknowledged once its end is said. A repeat of an id taken before that runs here no longer (the +// builder started again) is answered as its builds stand now, read afresh by BuildStep (H-A-381 (1)): an entry built is +// answered built, never built twice. An ask the state taken refuses is let go and handed again. +func (q *stepQueue) work(ctx, work context.Context, m queueMsg, id string, ra *runningAsk) { + if d := q.droppedAsk(id); d != nil { + ra.cancel(d) + q.logf("the ask %s (%s) was dropped before it was pulled: %s", id, ra.run.Pipeline, d.Error()) + q.build(work, m, id, ra) + return + } + first, err := q.taken.TakeOnce(meshbuilds.Seat, id, q.takenBound) + if err != nil { + ra.cancel(nil) + q.mu.Lock() + delete(q.running, id) + q.mu.Unlock() + q.logf("the ask %s (%s) is not taken, the state %s refused it: %v; it is handed again", id, ra.run.Pipeline, process.TakenState, err) + _ = m.Nak() + return + } if first { if err := q.say(ctx, meshbuilds.SubjectAskTaken(id), id+"-taken", meshbuilds.AskTakenEvent{Ask: id, Node: q.node, At: ra.taken.UTC()}); err != nil { q.logf("saying ask-taken of %s: %v", id, err) } } else { - q.logf("the ask %s (%s) was taken before: it is answered as its builds stand now", id, run.Pipeline) + q.logf("the ask %s (%s) was taken before: it is answered as its builds stand now", id, ra.run.Pipeline) } - go q.build(work, m, id, ra) + q.build(work, m, id, ra) } // build runs one taken ask to its end and says it.