Files
mesh-catalog/modules/mesh-config/cmd/mesh-config/ownerviews.go
T

223 lines
8.5 KiB
Go

package main
// What mesh-config reads of the delivery owners' state, through their tools and views families (novox/hq ADR 0615 §1,
// §4 to §6, ADR 0596): never a bucket of theirs. For each of the four states it follows (the indexer's predefined and
// config-candidates, the assigner's assignments and wants) it asks the owner's read tool for the whole
// state at its start, keeps the picture from the owner's `views.<state>` events, and asks again only on a gap. Started
// while an owner is down it keeps asking, and past two minutes writes a finding naming the owner and the tool.
//
// The ordinary read tools are asked, not the first-view tools: those answer a few named callers (mesh-grants,
// node-tools, the delivery parts), and mesh-config is not one. Which modules exist, and each one's manifest, are not the
// indexer's: they are the catalog half's, followed per watched branch by meshcatalogclient on the same reader (design 47 G
// §196, §594, §614; catalogfeed.go).
import (
"context"
"encoding/json"
"errors"
"fmt"
catalog "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
"sort"
"strings"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/views"
)
// The tools of the owners that mesh-config asks, by the state they read.
const (
IndexerPredefTool = "indexer_read_predefined"
IndexerCandidatesTool = "indexer_read_config_candidates"
AssignerAssignmentsTool = "assigner_read_assignments"
AssignerWantsTool = "assigner_read_wants"
FirstViewsTool = "config_first_views"
FirstViewAgainTool = "config_first_view_again"
)
// followed is each state read through its owner: the `<owner>.<state>` name this module's code uses, the owner, its
// state and its tool.
var followed = []struct{ Read, Owner, State, Tool string }{
{IndexerPredefinedRead, indexerModule, catalog.StatePredefined, IndexerPredefTool},
{CandidatesRead, indexerModule, catalog.StateConfigCandidates, IndexerCandidatesTool},
{AssignmentsRead, assignerModule, "assignments", AssignerAssignmentsTool},
{WantsRead, assignerModule, "wants", AssignerWantsTool},
}
// ownerViews are the pictures, by the read name, followed through one views.Reader holding the module's writer (design 47 G
// A-28): the reader raises and clears the first views' findings and hears each owner's views event, and asks the first view
// again on a gap.
type ownerViews struct {
by map[string]*views.Follower
reader *views.Reader
// catalog is the followers of the catalog half (the registrations and each watched branch's revision view), which the
// first-view tools list and ask again beside the owners' (design 47 G §614); nil before they are followed.
catalog func() []*views.Follower
}
// newOwnerViews makes a follower for each state; changed is told when any picture changes. findings is the module's writer,
// which the reader writes the first views' findings through.
func newOwnerViews(ask func(key string, body any) (json.RawMessage, error), changed func(), findings views.WireWriter) *ownerViews {
o := &ownerViews{by: map[string]*views.Follower{}}
o.reader = &views.Reader{Module: Module, Findings: findings, Logf: logf,
Changed: func(string, string, json.RawMessage, bool) { changed() }}
for _, f := range followed {
f := f
fol := o.reader.Follow(f.Owner, f.State, f.Owner+"."+f.Tool)
// the owner's ordinary read tool answers wrapped as a tool's answer, or refused with its output
fol.Ask = func(_ context.Context, after string) (views.Answer, error) {
body := map[string]any{"values": true}
if after != "" {
body["after"] = after
}
raw, err := ask(f.Owner+"."+f.Tool, body)
if err != nil {
return views.Answer{}, err
}
answer, output, ok := unwrapAnswer(raw)
if !ok {
return views.Answer{}, fmt.Errorf("%s refused: %s", f.Tool, output)
}
return views.DecodeAnswer(answer)
}
o.by[f.Read] = fol
}
return o
}
// Start subscribes the owners' views events and asks every first view until answered, each on its goroutine.
func (o *ownerViews) Start(ctx context.Context) { o.reader.Start(ctx) }
// read is the picture of a read name, and whether the name is one followed.
func (o *ownerViews) read(state string) (*views.Follower, bool) {
f, ok := o.by[state]
return f, ok
}
// errUnanswered says a first view is not answered yet: a reader waits, and its next resolution reads it.
func errUnanswered(f *views.Follower) error {
s := f.Status()
return fmt.Errorf("the first view of %s's %s is not answered yet (%s asked %d times)", s.Owner, s.State, s.Tool, s.Asks)
}
// get is one key of a followed state.
func (o *ownerViews) get(state, key string) (*Entry, bool, error) {
f, ok := o.read(state)
if !ok {
return nil, false, nil
}
select {
case <-f.Ready():
default:
return nil, true, errUnanswered(f)
}
v, held := f.Get(key)
if !held {
return nil, true, nil
}
return &Entry{Key: key, Value: v}, true, nil
}
// keys are the keys of a followed state, sorted.
func (o *ownerViews) keys(state string) ([]string, bool, error) {
f, ok := o.read(state)
if !ok {
return nil, false, nil
}
select {
case <-f.Ready():
default:
return nil, true, errUnanswered(f)
}
var out []string
for k := range f.Values() {
out = append(out, k)
}
sort.Strings(out)
return out, true, nil
}
// catalogFollowers are the catalog half's followers, none before they are followed.
func (o *ownerViews) catalogFollowers() []*views.Follower {
if o.catalog == nil {
return nil
}
return o.catalog()
}
// statuses are the first views' statuses, by owner and state: the owners', then the catalog half's.
func (o *ownerViews) statuses() []views.Status {
var out []views.Status
for _, f := range followed {
out = append(out, o.by[f.Read].Status())
}
for _, f := range o.catalogFollowers() {
out = append(out, f.Status())
}
return out
}
// again asks one first view once more by hand ("<owner>.<state>", or all when empty).
func (o *ownerViews) again(ctx context.Context, which, why string) ([]string, error) {
var done []string
all := make([]*views.Follower, 0, len(followed))
for _, f := range followed {
all = append(all, o.by[f.Read])
}
all = append(all, o.catalogFollowers()...)
for _, f := range all {
name := f.Owner + "." + f.State
if which != "" && which != name {
continue
}
if err := f.Again(ctx, why); err != nil {
return done, err
}
done = append(done, name)
}
if len(done) == 0 {
return nil, errors.New("no first view is named " + which)
}
return done, nil
}
// viewTools are the read tool of the first views and its re-read (design 25 § 12.2): the status of each, and a hand ask.
func viewTools(c *Config, o func() *ownerViews) []stdio.Tool {
return []stdio.Tool{
{Name: FirstViewsTool,
Description: "mesh-config's first views of the delivery owners' state: for each of the indexer's " +
"predefined and config-candidates, the assigner's assignments and wants and the catalog half's registrations and " +
"each watched branch's revision, the tool it asks, the revision its " +
"picture holds, when it asked and was answered, how many asks (and by hand), and whether its condition is raised.",
Run: func(map[string]any) (any, error) {
ov := o()
if ov == nil {
return nil, errors.New("mesh-config reads its owners through views only when it runs as the module")
}
return map[string]any{"first-views": ov.statuses()}, nil
}},
{Name: FirstViewAgainTool,
Description: "Ask a first view once more by hand, `<owner>.<state>` (all when not given), recorded as a hand act with " +
"its why.",
Input: map[string]any{"type": "object", "required": []string{"why"}, "properties": map[string]any{
"view": map[string]any{"type": "string", "description": "<owner>.<state>, such as mesh-delivery-assigner.wants"},
"why": map[string]any{"type": "string"}}},
Run: func(a map[string]any) (any, error) {
ov := o()
if ov == nil {
return nil, errors.New("mesh-config reads its owners through views only when it runs as the module")
}
why, _ := a["why"].(string)
view, _ := a["view"].(string)
if strings.TrimSpace(why) == "" {
return nil, errors.New("say why: a hand act keeps its why")
}
if err := c.Store.HandAct("config_first_view_again "+view, strings.TrimSpace(why), "config_first_view_again"); err != nil {
return nil, fmt.Errorf("the hand-act log does not hold it, so nothing is asked: %w", err)
}
done, err := ov.again(context.Background(), strings.TrimSpace(view), strings.TrimSpace(why))
return map[string]any{"asked-again": done}, err
}},
}
}