From 917195a3020b4e5daaf516be9f7f89a5f3d04edf Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 22 Nov 2026 00:58:23 +0100 Subject: [PATCH] T4: the runtime answers mesh/queue.waiting, a holder's own queue read without consuming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- node-tools/internal/bus/queue.go | 99 +++++++++++++++++++ node-tools/internal/launch/launch.go | 6 ++ node-tools/internal/runtime/runtime.go | 14 ++- .../internal/runtime/seat_traffic_test.go | 40 ++++++++ 4 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 node-tools/internal/bus/queue.go diff --git a/node-tools/internal/bus/queue.go b/node-tools/internal/bus/queue.go new file mode 100644 index 0000000..5f313f9 --- /dev/null +++ b/node-tools/internal/bus/queue.go @@ -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 +} diff --git a/node-tools/internal/launch/launch.go b/node-tools/internal/launch/launch.go index 59dea2d..22996f0 100644 --- a/node-tools/internal/launch/launch.go +++ b/node-tools/internal/launch/launch.go @@ -500,6 +500,12 @@ func Start(module, entry string, env []string, mesh Bus, logf func(string, ...an if answered, err = mesh.Seat(strings.TrimPrefix(m.Method, "mesh/seat."), m.Params, nil); err == nil { result = answered } + case "mesh/queue.waiting": + // The asks that wait on the holder's own queue of a seat (design 47 H2 H-A-410). + var answered json.RawMessage + if answered, err = mesh.Seat("waiting", m.Params, nil); err == nil { + result = answered + } case "mesh/seat.take", "mesh/seat.answer": mu.Lock() seatTaker = c diff --git a/node-tools/internal/runtime/runtime.go b/node-tools/internal/runtime/runtime.go index 362f4ca..939c8d0 100644 --- a/node-tools/internal/runtime/runtime.go +++ b/node-tools/internal/runtime/runtime.go @@ -1441,8 +1441,10 @@ type seatAsked struct { Body json.RawMessage `json:"body"` ID string `json:"id"` Worker string `json:"worker"` - Bucket string `json:"bucket"` - Key string `json:"key"` + // Seat is the seat whose work queue a waiting read reads (design 47 H2 H-A-410). + Seat string `json:"seat"` + Bucket string `json:"bucket"` + Key string `json:"key"` // WaitMS is how long a pull waits for a message (none when 0); Fate what a settle gives one (issue 1887 A11). WaitMS int `json:"wait_ms"` // DelayMS is how long a settle with the fate nak-after waits before the bus gives the message again. @@ -1481,6 +1483,14 @@ func (b *moduleBus) Seat(verb string, params json.RawMessage, handOn func(string return json.RawMessage("null"), err } return json.Marshal(entry) + case "waiting": + // mesh/queue.waiting: the asks that wait on the holder's own queue of a seat, read without consuming them (design 47 + // H2 H-A-410, ADR 0596). + waiting, err := conn.QueueWaitingOf(b.module, asked.Seat) + if err != nil { + return nil, err + } + return json.Marshal(waiting) case "pull": // One message from a worker the membership lists, never a prefetch: a build module pulls an ask only when it // has room, from its workers in their order (novox/hq ADR 0525 §4; issue 1887 A11). diff --git a/node-tools/internal/runtime/seat_traffic_test.go b/node-tools/internal/runtime/seat_traffic_test.go index 357e6f8..6a191dd 100644 --- a/node-tools/internal/runtime/seat_traffic_test.go +++ b/node-tools/internal/runtime/seat_traffic_test.go @@ -3,6 +3,7 @@ package runtime import ( "encoding/json" "errors" + "fmt" "strings" "sync" "testing" @@ -219,4 +220,43 @@ func TestABundlePullsAndSettlesThroughItsModulesBus(t *testing.T) { if none, err := building.Seat("pull", json.RawMessage(`{"worker":"SEAT_NODE_BUILD_GO_DELIVERY_worker"}`), nil); err != nil || string(none) != "null" { t.Fatalf("an empty worker answered %s, %v", none, err) } + // Design 47 H2 H-A-410: the holder reads what waits on its own queue of the seat without consuming it (mesh/queue.waiting), + // each ask with its id, the module that put it and its age; another module is refused, as is a seat it holds no queue of. + waiting := func(want string) { + t.Helper() + raw, err := building.Seat("waiting", json.RawMessage(`{"seat":"node-build-go"}`), nil) + var w bus.QueueWaiting + if err != nil || json.Unmarshal(raw, &w) != nil || w.Capped { + t.Fatalf("waiting = %s, %v", raw, err) + } + var got []string + for _, a := range w.Asks { + got = append(got, fmt.Sprintf("%s:%s:%v", a.Module, a.ID, a.AgeSeconds < 60)) + } + if strings.Join(got, ",") != want { + t.Errorf("waiting is %s, want %s", raw, want) + } + } + waiting("") + for _, id := range []string{"ask2", "ask3"} { + m := nats.NewMsg("mesh.seat.node-build-go.accept.delivery") + m.Header.Set("x-source", "mesh-delivery-pipeline") + m.Header.Set("x-event-id", "mesh-delivery-pipeline."+id) + m.Data = []byte(`{"id":"` + id + `"}`) + if _, err := js.PublishMsg(m); err != nil { + t.Fatal(err) + } + } + waiting("mesh-delivery-pipeline:ask2:true,mesh-delivery-pipeline:ask3:true") + waiting("mesh-delivery-pipeline:ask2:true,mesh-delivery-pipeline:ask3:true") // read again: nothing was consumed + if _, err := elsewhere.Seat("waiting", json.RawMessage(`{"seat":"node-build-go"}`), nil); err == nil { + t.Error("another module read the Go build seat's queue") + } + if _, err := building.Seat("waiting", json.RawMessage(`{"seat":"operator-channel"}`), nil); err == nil { + t.Error("the holder read the queue of a seat it holds no worker on") + } + if _, err := building.Seat("pull", json.RawMessage(`{"worker":"SEAT_NODE_BUILD_GO_DELIVERY_worker","wait_ms":1000}`), nil); err != nil { + t.Fatal(err) + } + waiting("mesh-delivery-pipeline:ask3:true") // the pulled ask is taken, no longer waiting }