From 5bca54bb59b31f1df2f92ecc43106cf630461a04 Mon Sep 17 00:00:00 2001 From: jochen Date: Thu, 19 Nov 2026 14:31:52 +0100 Subject: [PATCH] assigner: the verb assignments with seats answers from the watched copy of seat-holders, falling back to a direct listing only while it cannot be read (issue 1982) --- .../cmd/mesh-delivery-assigner/seat_tools.go | 39 ++++++++--- .../mesh-delivery-assigner/seat_tools_test.go | 64 +++++++++++++++++++ .../cmd/mesh-delivery-assigner/tools.go | 2 + 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go index 6be29e84..8d4b894f 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools.go @@ -93,6 +93,11 @@ type seatTools struct { // seats is the seat list the bundle reads (seatlist.go): until it is read, propose judges no mesh seat the bootstrapped // definitions do not name as held once (ADR 0636 §1). Nil reads nothing. seatList *seatlist.Part + // watched is the tools process's watched copy of the state seat-holders (tools.go wants.watched, which gate.Holders + // also reads: A-298), read by the verb assignments with seats instead of a bus get per key (issue 1982: under load + // that took about 14 s, where the watched copy answers at once). Nil, or a call it cannot answer, falls back to a + // direct listing. + watched *stdio.WatchedStates } func newSeatTools(st seatStore, ask func(string, any) (json.RawMessage, error)) *seatTools { @@ -554,25 +559,43 @@ func (s *seatTools) tools() []stdio.Tool { return out } -// seats is the verb assignments with seats (ADR 0571 §4): who holds each seat and on which node, as the state seat-holders -// holds it; with node, the keys of seats held on that node. -func (s *seatTools) seats(node string) (any, error) { +// seatHolders is every key of the state seat-holders and its raw value: from the watched copy (s.watched) when +// there is one and it answers, else a direct listing, key by key, as the verb answered before (issue 1982). +func (s *seatTools) seatHolders() (map[string][]byte, error) { + if s.watched != nil { + if vals, err := s.watched.Values(mma.StateSeatHolders); err == nil { + return vals, nil + } + } keys, err := s.st.keys(mma.StateSeatHolders) if err != nil { return nil, fmt.Errorf("seat-holders cannot be listed: %w", err) } - sort.Strings(keys) - out := []mma.SeatHolders{} + out := make(map[string][]byte, len(keys)) for _, key := range keys { raw, _, found, err := s.st.get(mma.StateSeatHolders, key) if err != nil { return nil, fmt.Errorf("seat-holders %s cannot be read: %w", key, err) } - if !found { - continue + if found { + out[key] = raw } + } + return out, nil +} + +// seats is the verb assignments with seats (ADR 0571 §4): who holds each seat and on which node, as the state seat-holders +// holds it; with node, the keys of seats held on that node. +func (s *seatTools) seats(node string) (any, error) { + raws, err := s.seatHolders() + if err != nil { + return nil, err + } + keys := slices.Sorted(maps.Keys(raws)) + out := []mma.SeatHolders{} + for _, key := range keys { var sh mma.SeatHolders - if json.Unmarshal(raw, &sh) != nil { + if json.Unmarshal(raws[key], &sh) != nil { continue } if node != "" && sh.Node != node && !slices.ContainsFunc(sh.Holders, func(h mma.SeatHoldersHoldersItem) bool { return h.Node == node }) { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go index 1916b044..fbe233ca 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go @@ -352,6 +352,53 @@ func TestTheSeatsVerbs(t *testing.T) { t.Errorf("seats on ace = %+v, %v", out, err) } }) + t.Run("issue 1982: assignments with seats reads the tools process's watched copy of seat-holders, the store only while it cannot", func(t *testing.T) { + seeded := map[string][]byte{} + put := func(seat, node string, holders ...mma.SeatHoldersHoldersItem) { + raw, err := json.Marshal(mma.SeatHolders{Seat: seat, Node: node, Holders: holders}) + if err != nil { + t.Fatal(err) + } + seeded[seat] = raw + } + put("mesh-broker", "", mma.SeatHoldersHoldersItem{Module: "nats", Node: "ace"}) + put("node-shell.g14", "g14") + st := &countingSeatStore{memStore: memStore{mma.StateSeatHolders: &memKV{values: seeded}}} + sv := newSeatTools(st, nil) + watches := 0 + sv.watched = stdio.WatchedStatesOver(func(state string, handle func(stdio.StateChange) error) error { + watches++ + for k, v := range seeded { + if err := handle(stdio.StateChange{Key: k, Op: stdio.OpPut, Value: v, Current: true}); err != nil { + return err + } + } + return nil + }, func(state, key string) ([]byte, bool, error) { + v, ok := seeded[key] + return v, ok, nil + }) + out, err := sv.seats("") + if err != nil { + t.Fatal(err) + } + if got := out.(map[string]any)["seats"].([]mma.SeatHolders); len(got) != 2 || watches != 1 || st.keysCalls != 0 || st.getCalls != 0 { + t.Errorf("seats from the watched copy = %+v, watched %d time(s), the store's own keys %d get %d (want none)", + got, watches, st.keysCalls, st.getCalls) + } + // The lead's answer on issue 1982: the verb falls back to the store's own listing only while the watched copy + // cannot be read (here, its watch refused); the answer's form does not change. + sv.watched = stdio.WatchedStatesOver(func(string, func(stdio.StateChange) error) error { + return errors.New("refused") + }, func(state, key string) ([]byte, bool, error) { return nil, false, nil }) + out, err = sv.seats("") + if err != nil { + t.Fatal(err) + } + if got := out.(map[string]any)["seats"].([]mma.SeatHolders); len(got) != 2 || st.keysCalls != 1 || st.getCalls != 2 { + t.Errorf("seats falling back to the store = %+v, the store's own keys %d get %d (want 1, 2)", got, st.keysCalls, st.getCalls) + } + }) t.Run("research 072 row 37: a node is written as its keys, and history reads the node key", func(t *testing.T) { ctx := context.Background() kv := &revKV{values: map[string][]byte{}, revs: map[string]uint64{}} @@ -750,6 +797,23 @@ func readManifest() (catalogue.Manifest, error) { return catalogue.Decode(raw) } +// countingSeatStore is a memStore that counts its calls to keys and get, to prove a reader takes the state from the +// watched copy and the store only while that copy cannot be read (issue 1982). +type countingSeatStore struct { + memStore + keysCalls, getCalls int +} + +func (c *countingSeatStore) keys(state string) ([]string, error) { + c.keysCalls++ + return c.memStore.keys(state) +} + +func (c *countingSeatStore) get(state, key string) ([]byte, uint64, bool, error) { + c.getCalls++ + return c.memStore.get(state, key) +} + // countingKV is a memKV that counts its puts. type countingKV struct { memKV diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go index 0382caf9..e1d31602 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go @@ -419,6 +419,8 @@ func serveTools() error { wants.watched = stdio.NewWatchedStates() // the large states read whole from one watched copy (ADR 0580 clause 3) // The callers' seat entries are resolved from the assigner's own seat-holders, its watched copy (A-298). gate.Holders(seatlist.HoldersIn(func() (map[string][]byte, error) { return wants.watched.Values(mma.StateSeatHolders) })) + // The verb assignments with seats answers from the same watched copy, so every caller of it is fast too (issue 1982). + seatVerbs.watched = wants.watched wants.revisions = runtimeObjects{busstate.ObjectsOf(assigngraph.RevisionsObjects)} tools = append(tools, wants.tools()...) keeper := &inputKeeper{vals: stateobjects.ForState(buckets.InputsState), now: time.Now, logf: func(string, ...any) {}} -- 2.54.0