The watchman follows the one model in one pass. Its take-in waits are the seat's take-in.pending and take-in.taken, keyed <branch>.<source>, timed from the message's own time by the setting take-in-bound (60 minutes by default), and seeded from indexer_pending's held take-ins; the group bound goes. A taken-in mover's bus state is read from its catalog entry by fingerprint through the seat mesh-catalog's verb entry, bus-state-overdue timed from the take-in. The manifest verdicts and their findings, the head timing of source-moved, acting-part-late and delivery-waits go, with their readers, consumes and the indexer_verdicts invoke. A want awaiting its hand placement is not timed, and the want readers follow the H1 want. A record-opened is heard on record-opened.<call>. Builds: design 47 H §741, §744, §764 (the indexer: manifest-refused, manifest-accepted, the watchman's findings; the assigner: acting-part-late, delivery-waits), §772; lead answers H-A-25, H-A-37, H-A-49, H-A-70, H-A-78 (2), H-A-79, H-A-91 (2), H-A-94, H-A-95 (1), H-A-97, H-A-102 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
304 lines
12 KiB
Go
304 lines
12 KiB
Go
package watch
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshwatchdogsclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
)
|
|
|
|
// **A refused or overdue creation of bus state is loud** (novox/hq ADR 0595 §4; design 25 § 12.2; DLV-W1). The watchman
|
|
// learns the bus-state entries a mover's manifest asks for from the mover's catalog entry, read by its fingerprint through
|
|
// the seat mesh-catalog's verb `entry` once the indexer's `take-in.taken` names the mover (design 47 H-A-94 (1), H-A-49),
|
|
// and hears the bus module's own `created` and `refused`. An entry a taken-in mover asks for that has no `created` or
|
|
// `refused` within OverdueBound of its `take-in.taken` is the finding bus-state-overdue, naming the module and the entry;
|
|
// a `refused` is the finding bus-state-refused, with why. A later `created` clears both, and so does a later take-in of
|
|
// that module whose entry no longer asks for the entry. Never the bus module's to raise: it cannot while it is down.
|
|
//
|
|
// The bound is a row of the signals table: a pulse key per entry with OverdueBound to live, whose expiry is the finding,
|
|
// so no loop looks and a pulse that stood across a restart still acts. Both findings outlive a restart (signalKind),
|
|
// since what ends them is heard, not read.
|
|
//
|
|
// The bus module says `created` once per entry, so an entry it said created is not waited on again when a later take-in
|
|
// of its module asks for the same entry (review round 1): its mark, a pulse key with no time to live under the
|
|
// row bus-state-created, is read back at a restart (SeedSignal), and goes when a refused is heard or an entry no
|
|
// longer asks for it, so a later ask of it waits again.
|
|
|
|
// OverdueBound is how long an accepted entry may wait for the bus module's created or refused.
|
|
const OverdueBound = 5 * time.Minute
|
|
|
|
// The kinds, and the signals table's row.
|
|
const (
|
|
KindBusStateOverdue = "bus-state-overdue"
|
|
KindBusStateRefused = "bus-state-refused"
|
|
rowBusState = "bus-state"
|
|
rowBusCreated = "bus-state-created"
|
|
)
|
|
|
|
// BrokerSaid is the part of the bus module's created or refused the watchman reads: the module, the entry, and why a
|
|
// create was refused. It stands until the seat mesh-broker's definition is generated in mesh-contracts.
|
|
type BrokerSaid struct {
|
|
Module string `json:"module"`
|
|
// Local is the entry: the contract's `local-name` (mesh-contracts meshbroker.Said), or `local` where a body names it so.
|
|
Local string `json:"local,omitempty"`
|
|
LocalName string `json:"local-name,omitempty"`
|
|
Why string `json:"why,omitempty"`
|
|
}
|
|
|
|
// busStateID is the signal's and the findings' id of one entry: `<module>.<local>`.
|
|
func busStateID(module, local string) string { return module + "." + local }
|
|
|
|
func overdueKey(module, local string) string {
|
|
return FindingKey(ScopeModule, module, KindBusStateOverdue+"-"+local)
|
|
}
|
|
|
|
func refusedKey(module, local string) string {
|
|
return FindingKey(ScopeModule, module, KindBusStateRefused+"-"+local)
|
|
}
|
|
|
|
// EntryReader reads a mover's catalog entry by its fingerprint through the seat mesh-catalog's verb `entry`
|
|
// (meshcatalogclient.Entries): Need answers one kept or asks it once, telling its Changed when answered; Release forgets it.
|
|
type EntryReader interface {
|
|
Need(ctx context.Context, fingerprint string) (meshcatalog.CatalogEntry, meshcatalogclient.EntryState)
|
|
Release(fingerprint string)
|
|
}
|
|
|
|
// ReadEntriesWith sets the reader of the movers' catalog entries; with none, a take-in's movers wait on no bus state.
|
|
func (w *Watchman) ReadEntriesWith(r EntryReader) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
w.entries = r
|
|
}
|
|
|
|
// moverTaken is a mover of a take-in whose catalog entry is asked: its module and the take-in's time.
|
|
type moverTaken struct {
|
|
module string
|
|
at time.Time
|
|
}
|
|
|
|
// moversTaken asks the catalog entry of each mover a take-in.taken names, w.mu held: an entry already answered is read
|
|
// now, any other once its answer comes (EntryChanged).
|
|
func (w *Watchman) moversTaken(movers []meshcatalog.TakeInEntry, at time.Time) {
|
|
if w.entries == nil {
|
|
return
|
|
}
|
|
for _, m := range movers {
|
|
if m.Fingerprint == "" || m.Module == "" {
|
|
continue
|
|
}
|
|
w.movers[m.Fingerprint] = moverTaken{module: m.Module, at: at}
|
|
if e, state := w.entries.Need(context.Background(), m.Fingerprint); state == meshcatalogclient.EntryAnswered {
|
|
w.entryRead(m.Fingerprint, e)
|
|
}
|
|
}
|
|
}
|
|
|
|
// EntryChanged hears the answer of a mover's entry asked by its fingerprint (the Entries' Changed): an answered entry's
|
|
// bus state is waited on; one the holder does not hold is said, since its bus state cannot be known.
|
|
func (w *Watchman) EntryChanged(row meshcatalogclient.EntryRow) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
m, ok := w.movers[row.Fingerprint]
|
|
if !ok {
|
|
return
|
|
}
|
|
switch {
|
|
case row.State == meshcatalogclient.EntryAnswered && row.Entry != nil:
|
|
w.entryRead(row.Fingerprint, *row.Entry)
|
|
case row.State == meshcatalogclient.EntryNotFound:
|
|
w.say("the catalog entry of %s at %s is not held (%s), so its bus state is not waited on", m.module, short(row.Fingerprint), row.Detail)
|
|
delete(w.movers, row.Fingerprint)
|
|
w.entries.Release(row.Fingerprint)
|
|
}
|
|
}
|
|
|
|
// entryRead is a mover's entry answered, w.mu held: its bus state is waited on from the take-in's time, and the entry
|
|
// is forgotten.
|
|
func (w *Watchman) entryRead(fingerprint string, e meshcatalog.CatalogEntry) {
|
|
m, ok := w.movers[fingerprint]
|
|
if !ok {
|
|
return
|
|
}
|
|
delete(w.movers, fingerprint)
|
|
defer w.entries.Release(fingerprint)
|
|
asked, err := BusStateOfEntry(e)
|
|
if err != nil {
|
|
w.say("the catalog entry of %s at %s does not read (%v), so its bus state is not waited on", m.module, short(fingerprint), err)
|
|
return
|
|
}
|
|
w.busStateAsked(m.module, fingerprint, asked, m.at)
|
|
}
|
|
|
|
// BusStateOfEntry is the bus state a catalog entry's manifest asks for: the stored manifest keeps it beside requires, under
|
|
// catalogue.BusStateField.
|
|
func BusStateOfEntry(e meshcatalog.CatalogEntry) ([]catalogue.BusStateEntry, error) {
|
|
raw, err := json.Marshal(e.Manifest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var m map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
return nil, err
|
|
}
|
|
var entries []catalogue.BusStateEntry
|
|
if b, ok := m[catalogue.BusStateField]; ok {
|
|
if err := json.Unmarshal(b, &entries); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
// busStateAsked is a taken-in mover's bus state, w.mu held: each entry it asks for waits OverdueBound from the take-in
|
|
// for the bus module's word, and every entry of the module this watchman waits on or holds a finding of that it no
|
|
// longer asks for is ended.
|
|
func (w *Watchman) busStateAsked(module, fingerprint string, entries []catalogue.BusStateEntry, at time.Time) {
|
|
asked := map[string]bool{}
|
|
for _, e := range entries {
|
|
if e.Name == "" {
|
|
continue
|
|
}
|
|
asked[e.Name] = true
|
|
if w.busCreated[signalKey(rowBusCreated, busStateID(module, e.Name))] {
|
|
continue // said created already, and created is said once per entry: nothing to wait for
|
|
}
|
|
// One refused since an earlier take-in waits again: a new take-in asks the bus module again.
|
|
f := Finding{
|
|
Scope: ScopeModule, ID: module, Token: KindBusStateOverdue + "-" + e.Name, Kind: KindBusStateOverdue,
|
|
Severity: Warning, Source: rowBusState, Since: at.Add(OverdueBound).UTC().Truncate(time.Second),
|
|
Summary: fmt.Sprintf("the bus module said nothing of %s's %s (%s) within %s of its take-in at %s", module,
|
|
e.Name, e.Storage, OverdueBound, short(fingerprint)),
|
|
Headline: "The bus state " + e.Name + " of " + module + " was not created",
|
|
Explanation: "The indexer took in " + module + ", whose manifest asks the bus module for " + e.Name + ", and the bus " +
|
|
"module said neither created nor refused within " + OverdueBound.String() + ": it is down, or behind. " + module +
|
|
" fails at its start while the entry's binding is missing (novox/hq ADR 0595 §1).",
|
|
Needs: "a look at the bus module's health and its process's log; nats_first_view_again asks it to read the entries again"}
|
|
if left := OverdueBound - w.now().Sub(at); left > 0 {
|
|
w.signal(rowBusState, busStateID(module, e.Name), left, signalDue{Do: dueFind, Finding: f})
|
|
} else {
|
|
w.unsignal(rowBusState, busStateID(module, e.Name))
|
|
w.find(f)
|
|
}
|
|
}
|
|
for _, local := range w.busStateEntriesOf(module) {
|
|
if asked[local] {
|
|
continue
|
|
}
|
|
w.unsignal(rowBusState, busStateID(module, local))
|
|
w.unfind(overdueKey(module, local))
|
|
w.unfind(refusedKey(module, local))
|
|
w.uncreated(module, local)
|
|
}
|
|
}
|
|
|
|
// markCreated keeps that the bus module said module's local created, on the pulse bucket with no time to live.
|
|
func (w *Watchman) markCreated(module, local string) {
|
|
pk := signalKey(rowBusCreated, busStateID(module, local))
|
|
owes, _ := json.Marshal(signalDue{Do: dueCreated})
|
|
value, err := meshwatchdogsclient.EncodePulse(meshwatchdogsclient.Pulse{Bucket: signalBucket, Key: strings.TrimPrefix(pk, signalBucket+"."), Owes: string(owes)})
|
|
if err == nil {
|
|
err = w.out.PutPulse(pk, value, 0)
|
|
}
|
|
if err != nil {
|
|
w.say("cannot keep that %s of %s was created (%v): its next accepted manifest waits on it again", local, module, err)
|
|
}
|
|
w.busCreated[pk] = true
|
|
}
|
|
|
|
// uncreated drops the mark of module's local said created, so its next ask waits again.
|
|
func (w *Watchman) uncreated(module, local string) {
|
|
pk := signalKey(rowBusCreated, busStateID(module, local))
|
|
if !w.busCreated[pk] {
|
|
return
|
|
}
|
|
delete(w.busCreated, pk)
|
|
if err := w.out.DeletePulse(pk); err != nil {
|
|
w.say("cannot delete the mark %s: %v", pk, err)
|
|
}
|
|
}
|
|
|
|
// BrokerCreated is the bus module's created heard: the entry's wait ends, and its findings with it.
|
|
func (w *Watchman) BrokerCreated(s BrokerSaid) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
w.unsignal(rowBusState, busStateID(s.Module, s.Local))
|
|
w.unfind(overdueKey(s.Module, s.Local))
|
|
w.unfind(refusedKey(s.Module, s.Local))
|
|
w.markCreated(s.Module, s.Local)
|
|
}
|
|
|
|
// BrokerRefused is the bus module's refused heard: the entry's wait ends in the finding bus-state-refused, until a later
|
|
// created.
|
|
func (w *Watchman) BrokerRefused(s BrokerSaid) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
w.unsignal(rowBusState, busStateID(s.Module, s.Local))
|
|
w.unfind(overdueKey(s.Module, s.Local))
|
|
w.uncreated(s.Module, s.Local)
|
|
why := strings.TrimSpace(s.Why)
|
|
if why == "" {
|
|
why = "it gave no reason"
|
|
}
|
|
w.find(Finding{Scope: ScopeModule, ID: s.Module, Token: KindBusStateRefused + "-" + s.Local, Kind: KindBusStateRefused,
|
|
Severity: Urgent, Source: rowBusState, Since: w.now().UTC().Truncate(time.Second), Said: why,
|
|
Summary: fmt.Sprintf("the bus module refused to create %s's %s: %s", s.Module, s.Local, why),
|
|
Headline: "The bus state " + s.Local + " of " + s.Module + " was refused",
|
|
Explanation: "The bus module could not create " + s.Local + " for " + s.Module + ", so no binding is handed and " + s.Module +
|
|
" fails at its start, naming the entry (novox/hq ADR 0595 §1): " + why,
|
|
Needs: "a pull request that mends the entry in the manifest of " + s.Module + ", or the bus module's cause fixed"})
|
|
}
|
|
|
|
// busStateEntriesOf are the entries of module this watchman waits on or holds a finding of, by local name, sorted.
|
|
func (w *Watchman) busStateEntriesOf(module string) []string {
|
|
seen := map[string]bool{}
|
|
prefix := signalKey(rowBusState, module) + "."
|
|
for pk := range w.signals {
|
|
if local, ok := strings.CutPrefix(pk, prefix); ok && !strings.Contains(local, ".") {
|
|
seen[local] = true
|
|
}
|
|
}
|
|
for _, kind := range []string{KindBusStateOverdue, KindBusStateRefused} {
|
|
p := FindingKey(ScopeModule, module, kind+"-")
|
|
for key := range w.findings {
|
|
if local, ok := strings.CutPrefix(key, p); ok {
|
|
seen[local] = true
|
|
}
|
|
}
|
|
}
|
|
out := make([]string, 0, len(seen))
|
|
for l := range seen {
|
|
out = append(out, l)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// ReadBrokerSaid reads the bus module's created or refused.
|
|
func ReadBrokerSaid(body []byte) (BrokerSaid, error) {
|
|
var s BrokerSaid
|
|
if err := json.Unmarshal(body, &s); err != nil {
|
|
return BrokerSaid{}, fmt.Errorf("the bus module's word cannot be read: %w", err)
|
|
}
|
|
if s.Local == "" {
|
|
s.Local = s.LocalName
|
|
}
|
|
if s.Module == "" || s.Local == "" {
|
|
return BrokerSaid{}, fmt.Errorf("the bus module's word names no module or no entry")
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func short(digest string) string {
|
|
if len(digest) > 12 {
|
|
return digest[:12]
|
|
}
|
|
return digest
|
|
}
|