From f77d6d76e1fed675b7b067b2852eee6149b71125 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:51:21 +0100 Subject: [PATCH] B12 dispatch-rebinds: the publishes' dispatch takes the step queue's shape, a Consume bound again on the consumer's created and deleted advisories, so a pull error never ends it Builds: design 47 H2 H-A-654, H-A-703 --- .../publishes_integration_test.go | 14 +++ .../cmd/mesh-delivery-builder/publishitem.go | 101 +++++++++++------- .../mesh-delivery-builder/manifest_test.go | 5 + 3 files changed, 83 insertions(+), 37 deletions(-) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go index 70b2ace2d..3fd5d84e2 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishes_integration_test.go @@ -172,6 +172,20 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) { if s := next(); s.module != "live" { t.Fatalf("sent %s, want the live item put after the failed ones", s.module) } + // design 47 H2 H-A-654, H-A-703: the consumer deleted under a running dispatch and made again, the dispatch binds again on the + // bus's created advisory and sends the next item. + if err := b.js.DeleteConsumer(ctx, bind.Stream, publishesConsumer(holder, versions.PublishClassLive)); err != nil { + t.Fatal(err) + } + if _, err := b.js.CreateOrUpdateConsumer(ctx, bind.Stream, jetstream.ConsumerConfig{Durable: publishesConsumer(holder, versions.PublishClassLive), + FilterSubject: bind.Subject + ".publish." + holder + ".live.>", AckPolicy: jetstream.AckExplicitPolicy, AckWait: time.Minute, + MaxAckPending: 4, MaxDeliver: publishesMaxDeliver}); err != nil { + t.Fatal(err) + } + put("rebound", versions.PublishClassLive) + if s := next(); s.module != "rebound" { + t.Fatalf("sent %s, want the item put after the consumer was made again", s.module) + } if answer, err := pq.drop(ctx, items, "dropme", "0.1.0", "op@node", "a test drops it"); err != nil { t.Fatalf("drop: %v", err) } else { diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go index 787bcbbcc..6adbae169 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/publishitem.go @@ -506,59 +506,86 @@ func (pq *publishQueue) pull(ctx context.Context, holder string) error { case !errors.Is(err, jetstream.ErrConsumerNotFound): return fmt.Errorf("the consumer %s of an earlier build is not removed: %w", old, err) } - var cs []jetstream.Consumer for _, class := range []string{versions.PublishClassLive, versions.PublishClassStart} { - c, err := pq.js.CreateOrUpdateConsumer(ctx, b.Stream, jetstream.ConsumerConfig{Durable: publishesConsumer(holder, class), + _, err := pq.js.CreateOrUpdateConsumer(ctx, b.Stream, jetstream.ConsumerConfig{Durable: publishesConsumer(holder, class), FilterSubject: b.Subject + ".publish." + versions.EscapeToken(holder) + "." + class + ".>", AckPolicy: jetstream.AckExplicitPolicy, AckWait: pq.ackWait, MaxAckPending: inFlight(class), MaxDeliver: publishesMaxDeliver, InactiveThreshold: info.Config.MaxAge}) if err != nil { return fmt.Errorf("the consumer %s: %w", publishesConsumer(holder, class), err) } - cs = append(cs, c) } pq.logf("pulling the publishes of %s", holder) - for i, class := range []string{versions.PublishClassLive, versions.PublishClassStart} { - go pq.dispatch(ctx, holder, class, cs[i]) + for _, class := range []string{versions.PublishClassLive, versions.PublishClassStart} { + if err := pq.dispatch(ctx, b.Stream, holder, class); err != nil { + return err + } } return nil } -// dispatch runs the items of one holder's class in the order its consumer gives them, at most inFlight(class) at once (design 47 H2 -// §793, H-A-483, H-A-700): each free slot takes the next item through a pull that waits. A pull the bus refuses ends the dispatch, -// said; the holder is pulled again when an item of it is put. -func (pq *publishQueue) dispatch(ctx context.Context, holder, class string, c jetstream.Consumer) { - sem := make(chan struct{}, inFlight(class)) - for { - select { - case sem <- struct{}{}: - case <-ctx.Done(): - return +// dispatch runs the items of one holder's class as the consumer gives them, at most inFlight(class) at once (design 47 H2 §793, +// H-A-483, H-A-700, H-A-654): in the step queue's shape (serveSteps), a Consume bound to the durable, which the bus's advisories of +// the consumer's creation and deletion bind again, so a pull error never ends it. +func (pq *publishQueue) dispatch(ctx context.Context, stream, holder, class string) error { + name := publishesConsumer(holder, class) + var mu sync.Mutex + var pulling jetstream.ConsumeContext + bind := func() bool { + mu.Lock() + defer mu.Unlock() + if pulling != nil || ctx.Err() != nil { + return pulling != nil } - var got jetstream.Msg - b, err := c.Fetch(1) - if err == nil { - for m := range b.Messages() { - got = m - } - err = b.Error() + c, err := pq.js.Consumer(ctx, stream, name) + if err != nil { + return false } - if err != nil && !errors.Is(err, jetstream.ErrNoMessages) && !errors.Is(err, nats.ErrTimeout) && ctx.Err() == nil { - <-sem - pq.logf("the %s publishes of %s are not pulled: %v; they are pulled when an item of it is put", class, holder, err) - pq.mu.Lock() - delete(pq.pulling, holder) - pq.mu.Unlock() - return + cc, err := c.Consume(func(m jetstream.Msg) { go pq.take(ctx, m) }, jetstream.PullMaxMessages(inFlight(class)), + jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) { + pq.logf("the %s publishes of %s: the pull met %v; it goes on", class, holder, err) + })) + if err != nil { + pq.logf("the %s publishes of %s are not pulled: %v; they are pulled when the bus says the consumer was created", class, holder, err) + return false } - if got == nil { - <-sem - continue - } - go func(m jetstream.Msg) { - defer func() { <-sem }() - pq.take(ctx, m) - }(got) + pulling = cc + return true } + release := func() { + mu.Lock() + defer mu.Unlock() + if pulling == nil { + return + } + pulling.Stop() + pulling = nil + pq.logf("the consumer %s was deleted: its items wait, and it is pulled again when the bus says it was created", name) + } + conn := pq.js.Conn() + created, err := conn.Subscribe("$JS.EVENT.ADVISORY.CONSUMER.CREATED."+stream+"."+name, func(*nats.Msg) { go bind() }) + if err != nil { + return fmt.Errorf("hearing the creation of the consumer %s: %w", name, err) + } + deleted, err := conn.Subscribe("$JS.EVENT.ADVISORY.CONSUMER.DELETED."+stream+"."+name, func(*nats.Msg) { + go func() { + release() + bind() // made again before the deletion was heard + }() + }) + if err != nil { + _ = created.Unsubscribe() + return fmt.Errorf("hearing the deletion of the consumer %s: %w", name, err) + } + go func() { + <-ctx.Done() + _ = created.Unsubscribe() + _ = deleted.Unsubscribe() + release() + }() + if !bind() { + return fmt.Errorf("the consumer %s is not bound", name) + } + return nil } // itemsOf are the items the work queue holds of module's version in class (empty for any class), each with its subject. diff --git a/modules/mesh-delivery-builder/manifest_test.go b/modules/mesh-delivery-builder/manifest_test.go index 68e0d869a..6540eedad 100644 --- a/modules/mesh-delivery-builder/manifest_test.go +++ b/modules/mesh-delivery-builder/manifest_test.go @@ -352,6 +352,11 @@ func TestServesNoRequestSubjectAndHoldsNoTicker(t *testing.T) { // 1607/AC6 (ADR if (f == "Subscribe(" || f == "time.NewTicker") && filepath.ToSlash(path) == "cmd/mesh-delivery-builder/stepqueue.go" { continue } + // Design 47 H2 H-A-654: the publishes' dispatch takes the step queue's shape, bound again on the consumer's created and + // deleted advisories: the same one kind of subscription. + if f == "Subscribe(" && filepath.ToSlash(path) == "cmd/mesh-delivery-builder/publishitem.go" { + continue + } if strings.Contains(string(src), f) { t.Errorf("%s holds %s: the builder serves no request subject but its seat's verbs and accepted kinds (A-107) and holds no timer", path, f) }