T4: the runtime answers mesh/queue.waiting, a holder's own queue read without consuming
mesh/queue.waiting {seat} answers {asks: [{id, module, age_seconds}], capped}: for each worker the
holder's membership lists on the seat's stream, every message after the worker's delivered
sequence, read by a direct get by subject, never consumed; at most 768 KB; refused for a seat the
module holds no queue of (ADR 0596: a holder reads its own store).
Builds: design 47 H2 §777, H-A-410
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package bus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
)
|
||||
|
||||
// QueueWaitingMost is the most a waiting read holds, in its asks' ids and modules (design 47 H2 H-A-410).
|
||||
const QueueWaitingMost = 768 << 10
|
||||
|
||||
// WaitingAsk is one ask that waits on a seat's work queue: its id (the publisher's own, as it published it), the module that
|
||||
// put it and how long it has waited.
|
||||
type WaitingAsk struct {
|
||||
ID string `json:"id"`
|
||||
Module string `json:"module"`
|
||||
AgeSeconds int64 `json:"age_seconds"`
|
||||
}
|
||||
|
||||
// QueueWaiting is what waits on a seat's work queue, oldest first, and whether the read stopped at QueueWaitingMost.
|
||||
type QueueWaiting struct {
|
||||
Asks []WaitingAsk `json:"asks"`
|
||||
Capped bool `json:"capped,omitempty"`
|
||||
}
|
||||
|
||||
// seatStream is the work-queue stream a seat's asks are kept on.
|
||||
func seatStream(seat string) string {
|
||||
return "SEAT_" + strings.ToUpper(strings.ReplaceAll(seat, "-", "_"))
|
||||
}
|
||||
|
||||
// QueueWaitingOf reads, without consuming, the asks that wait on the seat's work queue for module, the holder whose
|
||||
// membership lists the queue's workers: its own store (ADR 0596). For each of its workers on the seat's stream, every message
|
||||
// the worker has not yet been handed (after its delivered sequence) is read by a direct get by subject, never by a consumer
|
||||
// of its own (design 47 H2 H-A-410).
|
||||
func (c *Conn) QueueWaitingOf(module, seat string) (QueueWaiting, error) {
|
||||
t, err := c.trafficOf(module)
|
||||
if err != nil {
|
||||
return QueueWaiting{}, err
|
||||
}
|
||||
stream := seatStream(seat)
|
||||
var workers []SeatWorker
|
||||
for _, w := range t.Workers {
|
||||
if w.Stream == stream {
|
||||
workers = append(workers, w)
|
||||
}
|
||||
}
|
||||
if len(workers) == 0 {
|
||||
return QueueWaiting{}, fmt.Errorf("%s holds no work queue of the seat %s: its membership lists no worker on %s "+
|
||||
"(novox/hq ADR 0596: a holder reads its own queue)", module, seat, stream)
|
||||
}
|
||||
js, err := jetstream.New(c.nc)
|
||||
if err != nil {
|
||||
return QueueWaiting{}, err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), StateTimeout)
|
||||
defer cancel()
|
||||
kept, err := js.Stream(ctx, stream)
|
||||
if err != nil {
|
||||
return QueueWaiting{}, fmt.Errorf("reading %s's queue %s: %w", module, stream, err)
|
||||
}
|
||||
out := QueueWaiting{Asks: []WaitingAsk{}}
|
||||
size, now := 0, time.Now()
|
||||
for _, w := range workers {
|
||||
bound, err := c.pullConsumer(module, w)
|
||||
if err != nil {
|
||||
return QueueWaiting{}, err
|
||||
}
|
||||
info, err := bound.Info(ctx)
|
||||
if err != nil {
|
||||
return QueueWaiting{}, fmt.Errorf("reading %s's worker %s: %w", module, w.Consumer, err)
|
||||
}
|
||||
filter := w.Filter
|
||||
if filter == "" {
|
||||
filter = ">"
|
||||
}
|
||||
for seq := info.Delivered.Stream + 1; ; {
|
||||
m, err := kept.GetMsg(ctx, seq, jetstream.WithGetMsgSubject(filter))
|
||||
if errors.Is(err, jetstream.ErrMsgNotFound) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return QueueWaiting{}, fmt.Errorf("reading %s's queue %s: %w", module, stream, err)
|
||||
}
|
||||
seq = m.Sequence + 1
|
||||
ask := WaitingAsk{Module: m.Header.Get("x-source"), AgeSeconds: int64(now.Sub(m.Time) / time.Second)}
|
||||
ask.ID = strings.TrimPrefix(m.Header.Get("x-event-id"), ask.Module+".")
|
||||
if size += len(ask.ID) + len(ask.Module) + 48; size > QueueWaitingMost {
|
||||
out.Capped = true
|
||||
return out, nil
|
||||
}
|
||||
out.Asks = append(out.Asks, ask)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user