B7/B8 again-class: an event's publish again always puts the class again, its message id ending in the event's stream sequence

Builds: design 47 H2 H-A-613 (1), H-A-620 (1)
This commit is contained in:
2026-11-25 19:17:56 +01:00
parent a28caf25f6
commit d4f9fd013d
4 changed files with 33 additions and 9 deletions
@@ -407,7 +407,7 @@ func (pq *publishQueue) put(ctx context.Context, q versions.QueuedPublish) (bool
} 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)))
ack, err := pq.js.PublishMsg(ctx, &nats.Msg{Subject: pq.subject(b, q), Data: data}, jetstream.WithMsgID(q.MsgID()))
if err != nil {
if w := err.Error(); strings.Contains(w, "maximum bytes") || strings.Contains(w, "maximum messages") || errors.Is(err, jetstream.ErrMaxBytesExceeded) {
return false, fmt.Errorf("%w: %v", versions.ErrQueueFull, err)
@@ -60,7 +60,7 @@ func (a *Againer) Heard(ctx context.Context, subject string, data []byte, seq ui
holders = a.returnedHolders(data)
}
if holders != nil {
if err := a.again(ctx, holders); err != nil {
if err := a.again(ctx, holders, seq); err != nil {
return true, err
}
}
@@ -159,7 +159,7 @@ func (a *Againer) unreleased(ctx context.Context) ([]ReservedVersion, error) {
}
// again queues the unreleased versions at the holders holdersOf names, at most MaxVersions of them.
func (a *Againer) again(ctx context.Context, holdersOf func(seat string) []string) error {
func (a *Againer) again(ctx context.Context, holdersOf func(seat string) []string, seq uint64) error {
vs, err := a.unreleased(ctx)
if err != nil {
return fmt.Errorf("the reserved versions cannot be read: %w", err)
@@ -171,7 +171,7 @@ func (a *Againer) again(ctx context.Context, holdersOf func(seat string) []strin
if max <= 0 {
max = DefaultRepublishMaxVersions
}
queued, err := a.Republisher.queue(ctx, vs, holdersOf, max)
queued, err := a.Republisher.queue(ctx, vs, holdersOf, max, seq)
if len(queued) > 0 {
a.logf("publish again on an event: %d versions put on the work queue: %s", len(queued), strings.Join(queued, ", "))
}
@@ -94,6 +94,19 @@ type QueuedPublish struct {
Queued time.Time `json:"queued"`
// Class is PublishClassLive or PublishClassStart (H-A-290 (1)); a value written before it had one is of the start.
Class string `json:"class,omitempty"`
// Sequence is the stream sequence of the event whose publish again this item is, the last token of its message id (H-A-613 (1)); zero
// for an item no event asked, which is not kept on the item.
Sequence uint64 `json:"-"`
}
// MsgID is the message id of the item: PublishMsgID, and for an event's publish again the event's stream sequence after it, so the same
// event heard again puts nothing (design 47 H2 H-A-613 (1)).
func (q QueuedPublish) MsgID() string {
id := PublishMsgID(q.Holder, q.Class, q.Module, q.Version)
if q.Sequence > 0 {
id += "." + strconv.FormatUint(q.Sequence, 10)
}
return id
}
// streamOrder sorts the keys of the work queue's items in stream order: each key is its item's stream sequence (the builder's
@@ -369,7 +382,7 @@ type view struct {
// queue queues vs newest first, at most max of them (0 is no limit), each artifact to the first holder holdersOf names for
// its seat. It answers the versions queued, `<module> <version>`.
func (r *Republisher) queue(ctx context.Context, vs []ReservedVersion, holdersOf func(seat string) []string, max int) ([]string, error) {
func (r *Republisher) queue(ctx context.Context, vs []ReservedVersion, holdersOf func(seat string) []string, max int, event uint64) ([]string, error) {
sort.SliceStable(vs, func(i, j int) bool { return vs[i].At.After(vs[j].At) })
var queued []string
var errs []error
@@ -420,7 +433,7 @@ func (r *Republisher) queue(ctx context.Context, vs []ReservedVersion, holdersOf
}
did := false
for holder, arts := range byHolder {
ok, err := r.enqueue(ctx, v, holder, arts)
ok, err := r.enqueue(ctx, v, holder, arts, event)
if err != nil {
return queued, err
}
@@ -435,8 +448,13 @@ func (r *Republisher) queue(ctx context.Context, vs []ReservedVersion, holdersOf
// enqueue puts one holder's artifacts of a version on the work queue under its message id: not again while the queue holds that
// id (its duplicates window), which stands queued, and not past the queue's cap (said as queue full).
func (r *Republisher) enqueue(ctx context.Context, v ReservedVersion, holder string, arts map[string]string) (bool, error) {
// An event's publish again (event is its stream sequence, not zero) is always of the class again, whenever its build ended
// (H-A-613 (1), H-A-620 (1)).
func (r *Republisher) enqueue(ctx context.Context, v ReservedVersion, holder string, arts map[string]string, event uint64) (bool, error) {
q := QueuedPublish{V: 1, Holder: holder, Module: v.Module, Version: v.Version, Artifacts: arts, Queued: r.queuedAt(), Class: r.classOf(v)}
if event > 0 {
q.Class, q.Sequence = PublishClassStart, event
}
dup, err := r.Put(ctx, q)
switch {
case errors.Is(err, ErrQueueFull):
@@ -495,7 +513,7 @@ func (r *Republisher) Republish(ctx context.Context, module, version string) ([]
return nil, fmt.Errorf("%s holds no reserved version%s with an artifact unpublished", module, map[bool]string{true: " " + version, false: ""}[version != ""])
}
}
return r.queue(ctx, vs, r.holdersFn, 0)
return r.queue(ctx, vs, r.holdersFn, 0, 0)
}
// Queued is builder_publish_queue's answer, small at any size of the queue (design 47 H H-A-290 (5)): the queued versions'
@@ -84,7 +84,7 @@ func newRepublishBed(t *testing.T) *republishBed {
if b.full {
return false, fmt.Errorf("%w: maximum bytes exceeded", ErrQueueFull)
}
id := PublishMsgID(q.Holder, q.Class, q.Module, q.Version)
id := q.MsgID()
if b.window && b.ids[id] {
return true, nil
}
@@ -645,6 +645,12 @@ func TestAPublishIsPutAgainOnAHoldersEventsOnceBySequence(t *testing.T) {
if !handled || err != nil || !slices.Equal(puts(), c.want) {
t.Errorf("%s: handled %v, %v, put %v, want %v", c.name, handled, err, puts(), c.want)
}
// An event's publish again is always of the class again, its message id ending in the event's stream sequence (H-A-613 (1), H-A-620 (1)).
for _, q := range b.puts {
if id := q.Holder + ".again." + q.Module + "." + EscapeToken(q.Version) + "." + strconv.FormatUint(c.seq, 10); q.Class != PublishClassStart || q.MsgID() != id {
t.Errorf("%s: %s %s put as class %q with the id %q, want class again and the id %q", c.name, q.Module, q.Version, q.Class, q.MsgID(), id)
}
}
}
if _, _, found, _ := acted.Get(ctx, againKey(sub+"holder")); !found {
t.Error("the running's mark is not kept in the state acted")