B8 again-queue: per holder two durables, -live with four sends in flight and -again with one, each with the stream's max age as inactive threshold; a put reads the subject first
Builds: design 47 H2 H-A-700 (1) (2), H-A-719, H-A-620 (1) (2), H-A-613
This commit is contained in:
+33
-13
@@ -109,22 +109,20 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) {
|
||||
}
|
||||
return send{}
|
||||
}
|
||||
// The two class durables an earlier release made, the live one holding an item given and not acknowledged.
|
||||
for _, class := range []string{versions.PublishClassLive, versions.PublishClassStart} {
|
||||
if _, err := b.js.CreateOrUpdateConsumer(ctx, bind.Stream, jetstream.ConsumerConfig{Durable: publishesConsumer(holder) + "-" + class,
|
||||
FilterSubject: bind.Subject + ".publish." + holder + "." + class + ".>", AckPolicy: jetstream.AckExplicitPolicy, AckWait: time.Hour}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The one durable an earlier build made, holding an item given and not acknowledged (H-A-700 (2)).
|
||||
if _, err := b.js.CreateOrUpdateConsumer(ctx, bind.Stream, jetstream.ConsumerConfig{Durable: publishesOneConsumer(holder),
|
||||
FilterSubject: bind.Subject + ".publish." + holder + ".>", AckPolicy: jetstream.AckExplicitPolicy, AckWait: time.Hour}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
put("given", versions.PublishClassLive)
|
||||
old, err := b.js.Consumer(ctx, bind.Stream, publishesConsumer(holder)+"-"+versions.PublishClassLive)
|
||||
old, err := b.js.Consumer(ctx, bind.Stream, publishesOneConsumer(holder))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m, err := old.Next(); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if md, _ := m.Metadata(); md.NumDelivered != 1 {
|
||||
t.Fatalf("the old live durable gave %+v", md)
|
||||
t.Fatalf("the old durable gave %+v", md)
|
||||
}
|
||||
pq.learn(ctx, holder)
|
||||
stream, err := b.js.Stream(ctx, bind.Stream)
|
||||
@@ -133,12 +131,34 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) {
|
||||
}
|
||||
var names []string
|
||||
for n := range stream.ConsumerNames(ctx).Name() {
|
||||
if strings.HasPrefix(n, publishesConsumer(holder)) {
|
||||
if strings.HasPrefix(n, publishesOneConsumer(holder)) {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
if !slices.Equal(names, []string{publishesConsumer(holder)}) {
|
||||
t.Fatalf("the consumers of %s are %v, want only %s: the class durables retired", holder, names, publishesConsumer(holder))
|
||||
want := []string{publishesConsumer(holder, versions.PublishClassLive), publishesConsumer(holder, versions.PublishClassStart)}
|
||||
slices.Sort(names)
|
||||
slices.Sort(want)
|
||||
if !slices.Equal(names, want) {
|
||||
t.Fatalf("the consumers of %s are %v, want %v: the one durable removed", holder, names, want)
|
||||
}
|
||||
// H-A-719, H-A-700 (1), H-A-620 (2): each of the two carries the stream's maximum age as its inactive threshold; four live sends
|
||||
// in flight and one again.
|
||||
sinfo, err := stream.Info(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for class, inflight := range map[string]int{versions.PublishClassLive: 4, versions.PublishClassStart: 1} {
|
||||
c, err := b.js.Consumer(ctx, bind.Stream, publishesConsumer(holder, class))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := c.Info(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Config.InactiveThreshold != sinfo.Config.MaxAge || sinfo.Config.MaxAge == 0 || info.Config.MaxAckPending != inflight {
|
||||
t.Fatalf("%s: inactive threshold %s (stream max age %s), %d in flight, want the stream's max age and %d", class, info.Config.InactiveThreshold, sinfo.Config.MaxAge, info.Config.MaxAckPending, inflight)
|
||||
}
|
||||
}
|
||||
if s := next(); s.module != "given" {
|
||||
t.Fatalf("sent %s first, want the item the old durable held unacknowledged", s.module)
|
||||
@@ -158,7 +178,7 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) {
|
||||
t.Log(answer)
|
||||
}
|
||||
// Every item is acknowledged and the dropped one is gone: nothing is left to come back.
|
||||
c, err := b.js.Consumer(ctx, bind.Stream, publishesConsumer(holder))
|
||||
c, err := b.js.Consumer(ctx, bind.Stream, publishesConsumer(holder, versions.PublishClassLive))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -242,7 +262,7 @@ func TestThePublishQueueLosesNoItemAndNeverSendsADroppedItem(t *testing.T) {
|
||||
if err := pq.learnAll(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for range 4 { // the items in flight: at most four held unacknowledged, their publish unheard
|
||||
for range 1 { // the again items in flight: at most one held unacknowledged, its publish unheard (H-A-620 (2))
|
||||
select {
|
||||
case <-sentCh:
|
||||
case <-time.After(30 * time.Second):
|
||||
|
||||
@@ -355,19 +355,29 @@ func publishesOf(module string) func() (publishesStream, error) {
|
||||
return func() (publishesStream, error) { return b, nil }
|
||||
}
|
||||
|
||||
// The consumer's numbers (H-A-446 (2)): at most four sends in flight per holder, and ten deliveries, the work queue's largest;
|
||||
// the builder ends an item at publish-tries anyway.
|
||||
// defaultPublishAckMinutes is the setting publish-ack-minutes' default: G section 369's publish bound (H-A-606 round 3 (3)).
|
||||
const defaultPublishAckMinutes = 10
|
||||
|
||||
const (
|
||||
publishesMaxAckPending = versions.PublishSendsAtOnce
|
||||
publishesMaxDeliver = 10
|
||||
)
|
||||
// The consumers' numbers (H-A-700 (1), H-A-620 (2)): per holder, at most four `live` sends in flight and one `again` send, and ten
|
||||
// deliveries, the work queue's largest.
|
||||
const publishesMaxDeliver = 10
|
||||
|
||||
// publishesConsumer is the one durable consumer of holder's items (H-A-483 addendum).
|
||||
func publishesConsumer(holder string) string {
|
||||
return versions.PublishesLocal + "-" + holder
|
||||
// publishesConsumer is the durable consumer of holder's items of a class: `publishes-<holder>-live` and `publishes-<holder>-again`
|
||||
// (H-A-700 (1)).
|
||||
func publishesConsumer(holder, class string) string {
|
||||
return versions.PublishesLocal + "-" + holder + "-" + class
|
||||
}
|
||||
|
||||
// publishesOneConsumer is the one durable of holder's items an earlier build made: its filter overlaps both classes', which a work
|
||||
// queue refuses, so a pull deletes it before it makes the two (H-A-700 (2)).
|
||||
func publishesOneConsumer(holder string) string { return versions.PublishesLocal + "-" + holder }
|
||||
|
||||
// inFlight is the sends of a class at once: four `live`, one `again` (ADR 0674 §3, H-A-620 (2)).
|
||||
func inFlight(class string) int {
|
||||
if class == versions.PublishClassStart {
|
||||
return 1
|
||||
}
|
||||
return versions.PublishSendsAtOnce
|
||||
}
|
||||
|
||||
// subject is the item's subject on the bus.
|
||||
@@ -386,6 +396,18 @@ func (pq *publishQueue) put(ctx context.Context, q versions.QueuedPublish) (bool
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// No form of a manifest's stream refuses a new message while one is held, so the subject's last message is read first: one held
|
||||
// there means the version is queued or being sent, and nothing is put (design 47 H2 H-A-620 (1); ADR 0674 §3).
|
||||
st, err := pq.js.Stream(ctx, b.Stream)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err := st.GetLastMsgForSubject(ctx, pq.subject(b, q)); err == nil {
|
||||
pq.learn(ctx, q.Holder)
|
||||
return true, nil
|
||||
} else if !errors.Is(err, jetstream.ErrMsgNotFound) {
|
||||
return false, err
|
||||
}
|
||||
ack, err := pq.js.PublishMsg(ctx, &nats.Msg{Subject: pq.subject(b, q), Data: data}, jetstream.WithMsgID(versions.PublishMsgID(q.Holder, q.Class, q.Module, q.Version)))
|
||||
if err != nil {
|
||||
if w := err.Error(); strings.Contains(w, "maximum bytes") || strings.Contains(w, "maximum messages") || errors.Is(err, jetstream.ErrMaxBytesExceeded) {
|
||||
@@ -446,41 +468,53 @@ func (pq *publishQueue) learn(ctx context.Context, holder string) {
|
||||
}
|
||||
}
|
||||
|
||||
// pull makes holder's one consumer and dispatches its items (design 47 H2 §793, H-A-483 and its addenda): first it retires the
|
||||
// holder's class durables an earlier release made (`-live`, `-again`), whose filters a work queue refuses beside the one; their
|
||||
// unacknowledged items stay on the queue and reach the one consumer.
|
||||
// pull makes holder's two consumers and dispatches their items (design 47 H2 §793, H-A-700, H-A-719): first it deletes the one
|
||||
// consumer `publishes-<holder>` an earlier build made, whose filter overlaps both and a work queue refuses overlapping filters, not
|
||||
// found ignored and a removal said; its unacknowledged items stay on the queue and reach the two. Each of the two takes the bus's
|
||||
// inactive threshold equal to the stream's own maximum age, so the bus removes the durable of a holder nothing pulled for that long
|
||||
// and no deleter is built here (H-A-719).
|
||||
func (pq *publishQueue) pull(ctx context.Context, holder string) error {
|
||||
b, err := pq.binding()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, class := range []string{versions.PublishClassLive, versions.PublishClassStart} {
|
||||
old := publishesConsumer(holder) + "-" + class
|
||||
err := pq.js.DeleteConsumer(ctx, b.Stream, old)
|
||||
switch {
|
||||
case err == nil:
|
||||
pq.logf("the consumer %s of an earlier release is removed: the publishes of %s have one consumer (H-A-483)", old, holder)
|
||||
case !errors.Is(err, jetstream.ErrConsumerNotFound):
|
||||
return fmt.Errorf("the consumer %s of an earlier release is not removed: %w", old, err)
|
||||
}
|
||||
}
|
||||
c, err := pq.js.CreateOrUpdateConsumer(ctx, b.Stream, jetstream.ConsumerConfig{Durable: publishesConsumer(holder),
|
||||
FilterSubject: b.Subject + ".publish." + versions.EscapeToken(holder) + ".>", AckPolicy: jetstream.AckExplicitPolicy,
|
||||
AckWait: pq.ackWait, MaxAckPending: publishesMaxAckPending, MaxDeliver: publishesMaxDeliver})
|
||||
st, err := pq.js.Stream(ctx, b.Stream)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the consumer %s: %w", publishesConsumer(holder), err)
|
||||
return err
|
||||
}
|
||||
info, err := st.Info(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
old := publishesOneConsumer(holder)
|
||||
switch err := pq.js.DeleteConsumer(ctx, b.Stream, old); {
|
||||
case err == nil:
|
||||
pq.logf("the consumer %s of an earlier build is removed: the publishes of %s have two, one per class (H-A-700)", old, holder)
|
||||
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),
|
||||
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)
|
||||
go pq.dispatch(ctx, holder, c)
|
||||
for i, class := range []string{versions.PublishClassLive, versions.PublishClassStart} {
|
||||
go pq.dispatch(ctx, holder, class, cs[i])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// dispatch runs the items of one holder in the order its one consumer gives them, at most four at once (design 47 H2 §793,
|
||||
// H-A-483): each free slot takes the next item through a pull that waits, so an item put or handed back after its pause is
|
||||
// received the moment the bus gives it; an item handed back reappears only after its pause, so an item put meanwhile goes
|
||||
// before it. 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 string, c jetstream.Consumer) {
|
||||
sem := make(chan struct{}, versions.PublishSendsAtOnce)
|
||||
// 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{}{}:
|
||||
@@ -497,7 +531,7 @@ func (pq *publishQueue) dispatch(ctx context.Context, holder string, c jetstream
|
||||
}
|
||||
if err != nil && !errors.Is(err, jetstream.ErrNoMessages) && !errors.Is(err, nats.ErrTimeout) && ctx.Err() == nil {
|
||||
<-sem
|
||||
pq.logf("the publishes of %s are not pulled: %v; they are pulled when an item of it is put", holder, err)
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user