Compare commits

...
4 changed files with 171 additions and 8 deletions
@@ -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 }) {
@@ -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
@@ -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) {}}
@@ -0,0 +1,74 @@
// Package publishconformance holds the cases every holder of a registry seat's `publish` is held to, so that two holders of
// one contract cannot each read it their own way (novox/hq design 47 G A-73, A-417, A-490): a publish with a version, a
// digest and no parts is the builder's publish again. A holder holding what the digest names under the name says it published
// (it moves nothing, or, where the bytes sit in the artifact store, as distribution's A-417 takes them, tags them); a holder
// that holds none of it, or holds the version under another digest, refuses it as not stored, naming the digest, and says it
// refused (A-490, A-490a). The cases live here beside the build modules' conformance kit (design 47 G §680, A-490a). A
// holder's own test runs Run over its own test bed, for every seat it fills.
package publishconformance
import (
"strings"
"testing"
)
// Name, Version and Digest are what the cases publish.
const (
Name = "novox/example"
Version = "0.1.0"
Digest = "sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
// Other is a digest the holder holds under the version in the case where it holds another; it does not contain Digest.
Other = "sha256:fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
)
// Outcome is what a holder did with a publish: whether the transfer ended with it, how many parts it asked for, the event
// it said on its seat (published or refused) with its reason, and the error it answered.
type Outcome struct {
Ended bool
Lacks int
Event string
Reason string
Err error
}
// Bed is one holder under test, fresh for a case.
type Bed struct {
// Hold makes the holder hold the digest under Name at Version, as the builder's reservation of Version would find it.
Hold func(digest string)
// Publish sends the publish of name at version with the digest and no parts, and reports what the holder did.
Publish func(name, version, digest string) Outcome
}
// Run runs the cases over beds made by fresh.
func Run(t *testing.T, fresh func(t *testing.T) Bed) {
t.Helper()
t.Run("a publish with a digest and no parts is published when the holder holds it", func(t *testing.T) {
b := fresh(t)
b.Hold(Digest)
o := b.Publish(Name, Version, Digest)
if o.Err != nil || !o.Ended || o.Lacks != 0 || o.Event != "published" {
t.Fatalf("a held digest answered %+v; want ended, no part lacked, published said", o)
}
})
t.Run("a publish with a digest and no parts is refused as not stored, naming the digest, when the holder holds none", func(t *testing.T) {
b := fresh(t)
o := b.Publish(Name, Version, Digest)
if o.Err == nil || !strings.Contains(o.Err.Error(), "not stored") || !strings.Contains(o.Err.Error(), Digest) {
t.Fatalf("a digest not held answered %+v; want the error to say not stored and name %s", o, Digest)
}
if o.Event != "refused" || !strings.Contains(o.Reason, "not stored") || !strings.Contains(o.Reason, Digest) || o.Lacks != 0 || !o.Ended {
t.Errorf("a digest not held answered %+v; want refused said with the digest in its reason, ended, no part lacked", o)
}
})
t.Run("a publish with a digest and no parts is refused as not stored, naming the digest, when the holder holds the version under another digest", func(t *testing.T) {
b := fresh(t)
b.Hold(Other)
o := b.Publish(Name, Version, Digest)
if o.Err == nil || !strings.Contains(o.Err.Error(), "not stored") || !strings.Contains(o.Err.Error(), Digest) {
t.Fatalf("another digest held answered %+v; want the error to say not stored and name %s", o, Digest)
}
if o.Event != "refused" || !strings.Contains(o.Reason, "not stored") || !strings.Contains(o.Reason, Digest) || o.Lacks != 0 || !o.Ended {
t.Errorf("another digest held answered %+v; want refused said as not stored with the digest, ended, no part lacked", o)
}
})
}