diff --git a/modules/mesh-config/cmd/mesh-config/main.go b/modules/mesh-config/cmd/mesh-config/main.go index 1447b5062..3d1a2d9d5 100644 --- a/modules/mesh-config/cmd/mesh-config/main.go +++ b/modules/mesh-config/cmd/mesh-config/main.go @@ -58,6 +58,30 @@ func (r runtimeStore) Get(state, key string) (*Entry, error) { return &Entry{Key: e.Key, Value: e.Value, Revision: e.Revision}, nil } +// Away is the fault `away` while a copy of this module's own states is not read yet: a verb or tool handler asks it +// (asking), the module's own start never does (design 47 H2 H-A-708 (2)). The first views of the owners stay the fault +// `away` at the read itself, since a picture not answered yet is no answer for a pass to resolve from. +func (r runtimeStore) Away() error { + if r.own == nil { + return nil + } + return r.own.Unread() +} + +// asking is a tool whose handler answers `away` at once while what it reads is not read yet (design 47 H2 H-A-708 (2)). +func asking(c *Config, t stdio.Tool) stdio.Tool { + run := t.Run + t.Run = func(a map[string]any) (any, error) { + if s, ok := c.Store.(interface{ Away() error }); ok { + if err := s.Away(); err != nil { + return nil, err + } + } + return run(a) + } + return t +} + func (r runtimeStore) Put(state, key string, value any) error { if r.own != nil && ownStates[state] { return r.own.write(state, key, value) @@ -418,7 +442,11 @@ func Tools(c *Config) []stdio.Tool { rows, err := c.Renames(strings.TrimSpace(m), true) return map[string]any{"renames": seatRenames(rows)}, err }} - return append(append(switchTools(c), viewTools(c, func() *ownerViews { return theViews })...), renames, stdio.Tool{ + var own []stdio.Tool + for _, t := range switchTools(c) { + own = append(own, asking(c, t)) + } + return append(append(own, viewTools(c, func() *ownerViews { return theViews })...), asking(c, renames), asking(c, stdio.Tool{ Name: Seat + "." + Verb, Description: "Set, clear, list or read the history of config values (novox/hq ADR 0348). A value is set at " + "one level: mesh config (no module, no node) or node config (a node) for a predefined key, or a " + @@ -452,7 +480,7 @@ func Tools(c *Config) []stdio.Tool { call.Caller = stdio.CallerOf(args) return c.Do(call) }, - }) + })) } // callOf reads a call's arguments strictly: an argument it does not know is refused, not ignored. diff --git a/modules/mesh-config/cmd/mesh-config/ownstates.go b/modules/mesh-config/cmd/mesh-config/ownstates.go index e7036caec..e1c7e3a1a 100644 --- a/modules/mesh-config/cmd/mesh-config/ownstates.go +++ b/modules/mesh-config/cmd/mesh-config/ownstates.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "sort" stdio "git.novox.be/novox/mesh-sdk/go" ) @@ -36,10 +37,8 @@ func newOwnCopy() *ownCopy { } func (o *ownCopy) get(state, key string) (*Entry, error) { - // A state whose copy is not read yet is the fault `away` at once, never a wait (design 47 H2 H-A-708 (2)). - if err := o.copy.Unread(state); err != nil { - return nil, err - } + // A read waits for a state whose copy is not read yet: the module's own start (the first decision, the deadline, the + // start judgement) is one. A verb or tool handler asks Unread first (design 47 H2 H-A-708 (2)). raw, ok, err := o.copy.Get(state, key) if err != nil || !ok { return nil, err @@ -48,14 +47,28 @@ func (o *ownCopy) get(state, key string) (*Entry, error) { } func (o *ownCopy) keys(state string) ([]string, error) { - if err := o.copy.Unread(state); err != nil { - return nil, err - } return o.copy.Keys(state) } -// follow starts the watch of every state the copy holds, each on its goroutine, so a read asked before one is in is -// answered `away` and the copy is read by the time it is asked again (Unread; design 47 H2 H-A-708 (2)). +// Unread is the fault `away` naming the first state, in name order, whose copy is not read yet, nil when every one is: a +// verb or tool handler asks it before it reads, so a call made while the module starts is answered at once (design 47 H2 +// H-A-708 (2)); the module's own reads wait for the copy. +func (o *ownCopy) Unread() error { + states := make([]string, 0, len(ownStates)) + for state := range ownStates { + states = append(states, state) + } + sort.Strings(states) + for _, state := range states { + if err := o.copy.Unread(state); err != nil { + return err + } + } + return nil +} + +// follow starts the watch of every state the copy holds, each on its goroutine, so a handler's ask is answered `away` until +// the copy is read (Unread; design 47 H2 H-A-708 (2)). func (o *ownCopy) follow() { for state := range ownStates { go o.copy.Follow(state, nil) diff --git a/modules/mesh-config/cmd/mesh-config/quiet_test.go b/modules/mesh-config/cmd/mesh-config/quiet_test.go index 4bfdaa057..0036270b3 100644 --- a/modules/mesh-config/cmd/mesh-config/quiet_test.go +++ b/modules/mesh-config/cmd/mesh-config/quiet_test.go @@ -204,15 +204,24 @@ func TestAnOwnStateIsReadFromItsWatchedCopyNotKeyByKey(t *testing.T) { } n := len(b.held[ValuesState]) own := b.ownCopy() - // H-A-708 (2): before the state's copy is read a read is the fault `away` at once, naming the state; the start's watch reads it. + // H-A-708 (2): a handler asks Unread and is answered `away` at once, naming the state; the module's own read waits for the + // copy, which the start's watch reads, so its first decision and its deadline are never lost to `away`. var away *busretry.Away - if _, err := own.keys(ValuesState); !errors.As(err, &away) || away.Part != "the state "+ValuesState || !strings.HasPrefix(away.Words, "not read yet since ") { + if err := own.Unread(); !errors.As(err, &away) || !strings.HasPrefix(away.Part, "the state ") || !strings.HasPrefix(away.Words, "not read yet since ") { t.Fatalf("before the copy is read: %v", err) } - if _, err := own.get(ValuesState, "mesh.key-0s"); !errors.As(err, &away) { - t.Fatalf("a get before the copy is read: %v", err) + c := &Config{Store: runtimeStore{own: own}} + ran := false + tool := asking(c, stdio.Tool{Name: "t", Run: func(map[string]any) (any, error) { ran = true; return nil, nil }}) + if _, err := tool.Run(nil); !errors.As(err, &away) || ran { + t.Fatalf("a tool's handler before the copy is read: ran %v, %v", ran, err) } + waited := make(chan error, 1) + go func() { _, err := own.keys(ValuesState); waited <- err }() own.copy.Follow(ValuesState, nil) + if err := <-waited; err != nil { + t.Fatalf("the module's own read before the copy is read must wait for it, not answer away: %v", err) + } for pass := 0; pass < 3; pass++ { keys, err := own.keys(ValuesState) if err != nil || len(keys) != n { diff --git a/modules/mesh-provisioner/cmd/mesh-provisioner/holder.go b/modules/mesh-provisioner/cmd/mesh-provisioner/holder.go index 963ad9157..460739f58 100644 --- a/modules/mesh-provisioner/cmd/mesh-provisioner/holder.go +++ b/modules/mesh-provisioner/cmd/mesh-provisioner/holder.go @@ -1738,22 +1738,38 @@ func (w watchingStore) copied(state string) bool { return slices.Contains(WatchedReads, state) || slices.Contains(OwnStates, state) } -// unread is the fault `away` while the copy of a state the holder follows from its start is not read yet (design 47 H2 -// H-A-708 (2), H-A-717 DQ-55): a call made then is answered at once, never waits for the copy. -func (w watchingStore) unread(state string) error { +// Unread is the fault `away` while the copy of a state the holder follows from its start is not read yet (design 47 H2 +// H-A-708 (2), H-A-717 DQ-55). A verb or tool handler asks it (Holder.Away) so a call made then is answered at once; the +// store's own reads, the passes' and the start's, wait for the copy. +func (w watchingStore) Unread(state string) error { if !slices.Contains(WatchedReads, state) { return nil } return w.ws.Unread(state) } +// Away is the fault `away` when the copy of any of these states, or of every followed state when none is named, is not +// read yet: a verb or tool handler asks it before it reads, never the passes (design 47 H2 H-A-708 (2)). +func (h *Holder) Away(states ...string) error { + u, ok := h.Store.(interface{ Unread(string) error }) + if !ok { + return nil + } + if len(states) == 0 { + states = WatchedReads + } + for _, state := range states { + if err := u.Unread(state); err != nil { + return err + } + } + return nil +} + func (w watchingStore) Get(state, key string) (*Entry, error) { if !w.copied(state) { return w.Store.Get(state, key) } - if err := w.unread(state); err != nil { - return nil, err - } v, found, err := w.ws.Get(state, key) if err != nil || !found { return nil, err @@ -1765,9 +1781,6 @@ func (w watchingStore) Keys(state string) ([]string, error) { if !w.copied(state) { return w.Store.Keys(state) } - if err := w.unread(state); err != nil { - return nil, err - } return w.ws.Keys(state) } diff --git a/modules/mesh-provisioner/cmd/mesh-provisioner/holder_test.go b/modules/mesh-provisioner/cmd/mesh-provisioner/holder_test.go index e851efd0b..d18157f19 100644 --- a/modules/mesh-provisioner/cmd/mesh-provisioner/holder_test.go +++ b/modules/mesh-provisioner/cmd/mesh-provisioner/holder_test.go @@ -489,8 +489,16 @@ func TestAModuleHeldWithoutAWantIsComposedAtTheBuildItRuns(t *testing.T) { wk := &work{pending: map[string]bool{}, wake: make(chan struct{}, 1)} // H-A-708 (2): a state the holder follows from its start, asked before its copy is read, is the fault `away` at once. var away *busretry.Away - if _, err := m.h.Store.Keys(WantsRead); !errors.As(err, &away) || !strings.HasPrefix(away.Words, "not read yet since ") || len(watches) != 0 { - t.Fatalf("a read before the copy: %v, watches %v", err, watches) + // A handler asks Away and is answered at once; the holder's own reads (the passes, the start) wait, so the first read + // starts the watch and is answered from it, never `away`. + if err := m.h.Away(); !errors.As(err, &away) || !strings.HasPrefix(away.Words, "not read yet since ") || len(watches) != 0 { + t.Fatalf("a handler's ask before the copy: %v, watches %v", err, watches) + } + if _, err := m.h.Store.Keys(WantsRead); err != nil || watches[WantsRead] != 1 { + t.Fatalf("the holder's own read before the copy is read: %v, watches %v", err, watches) + } + if _, err := m.h.read(WantsRead, "", "", ""); err != nil { + t.Fatalf("a handler's read of a state whose copy is read: %v", err) } for _, r := range []string{AssignmentsRead, WantsRead} { ws.Follow(r, nil) diff --git a/modules/mesh-provisioner/cmd/mesh-provisioner/holdertools.go b/modules/mesh-provisioner/cmd/mesh-provisioner/holdertools.go index 227695642..e535607ea 100644 --- a/modules/mesh-provisioner/cmd/mesh-provisioner/holdertools.go +++ b/modules/mesh-provisioner/cmd/mesh-provisioner/holdertools.go @@ -79,6 +79,9 @@ func holderTools(h *Holder) []stdio.Tool { if arg(a, "node") == "" { return nil, fmt.Errorf("compose names the node") } + if err := h.Away(); err != nil { + return nil, err + } many, _ := a["retire-many"].(bool) return h.ComposeNodeWith(context.Background(), arg(a, "node"), many) }}, @@ -118,6 +121,9 @@ func (h *Holder) health() health.Answer { // read is a state's keys narrowed, or one whole, with the state's fill. func (h *Holder) read(state, key, node, want string) (any, error) { + if err := h.Away(state); err != nil { + return nil, err + } if key != "" { e, err := h.Store.Get(state, key) if err != nil {