nats: the keeper wakes on decided, spends a warrant once, and says again what the bus did not take
Review round 1 of the registry milestone (design 25 § 12.2, § 12.3, § 12.7): - the fill pass runs from the start, while the first view is asked; - recreate and drop wake on the operator channel's decided on the keeper's durable consumer, never a minute's tick; - a warrant is spent by compare-and-set (acted.<ask id> in requests) before anything is deleted; a second request on it is refused; a request whose ask ended unanswered is ended, said; the ask is raised before the request is handed; - created is marked only once said; an event or a condition the bus did not take is kept, named by the health and said again at the next fill pass with its one id, on the subjects left (the double publish); - the health judges the first view past its bound and never skips an unread consumer backlog; - an unrecordable hand act is said on the request; nats_requests reads what waits, what ended and each warrant spent, and drops a waiting request by hand; a tool waits for its request's end on one watch of requests; - the whole-module test reads the warrant from the router's record under a process connection. Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
This commit is contained in:
@@ -2,9 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -32,9 +31,10 @@ import (
|
||||
)
|
||||
|
||||
// The keeper (`nats-tools keep`): the bus module's process on its own account (own secret broker-keeper, ADR 0436 §1),
|
||||
// started by the node-engine as the manifest's process keeper. It asks the indexer's first view once, then hears
|
||||
// manifest-accepted on its durable consumer, makes what each asks for, reads the fill every minute, and acts on the
|
||||
// persons' requests its tools put in its own state requests (requests.go). It serves nothing (ADR 0436 §3).
|
||||
// started by the node-engine as the manifest's process keeper. It reads the fill every minute from its start, asks the
|
||||
// indexer's first view once, then hears manifest-accepted and the operator's decided on its durable consumer, makes what
|
||||
// each verdict asks for, and acts on the persons' requests its tools put in its own state requests (requests.go) as they
|
||||
// are written or answered. It serves nothing (ADR 0436 §3).
|
||||
|
||||
// AcceptedOf reads a manifest-accepted body: mesh-sdk's delivery.ManifestAccepted, with the indexer's revision of the
|
||||
// verdict when the event carries it (a field mesh-sdk is asked to add; 0 until then).
|
||||
@@ -58,6 +58,25 @@ var AcceptedSubject = "mesh.mod." + keeper.FirstViewOwner + ".event." + delivery
|
||||
// The router's records of asks, read under this module's own name (ADR 0259 §3), as the mail sender reads them.
|
||||
const asksBucket = "messenger_asks"
|
||||
|
||||
// WarrantFrom reads the operator's warrant on one of the bus module's asks from the router's own record of it, under the
|
||||
// keeper's own account (read: process.Conn.Ask): the ask and its warrant, or nil while the operator has not answered.
|
||||
func WarrantFrom(read func(bucket, id string) (*stdio.StateEntry, error)) func(id string) (*oc.Ask, *oc.Warrant, error) {
|
||||
return func(id string) (*oc.Ask, *oc.Warrant, error) {
|
||||
e, err := read(asksBucket, id)
|
||||
if err != nil || e == nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var rec struct {
|
||||
Ask *oc.Ask `json:"ask"`
|
||||
Warrant *oc.Warrant `json:"warrant"`
|
||||
}
|
||||
if err := json.Unmarshal(e.Value, &rec); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return rec.Ask, rec.Warrant, nil
|
||||
}
|
||||
}
|
||||
|
||||
// processStore is the keeper's own state through its process connection.
|
||||
type processStore struct{ c *process.Conn }
|
||||
|
||||
@@ -77,34 +96,55 @@ func (s processStore) Delete(state, key string) error { return s.c.State(state).
|
||||
|
||||
func (s processStore) Keys(state string) ([]string, error) { return s.c.State(state).Keys() }
|
||||
|
||||
// streamEmitter says an event of the seat mesh-broker, with the SDK's headers, twice under one event id (the lead's ruling,
|
||||
func (s processStore) Create(state, key string, value []byte) (uint64, error) {
|
||||
rev, err := s.c.State(state).Create(key, json.RawMessage(value))
|
||||
if errors.Is(err, stdio.ErrChanged) {
|
||||
return 0, keeper.ErrExists
|
||||
}
|
||||
return rev, err
|
||||
}
|
||||
|
||||
// streamEmitter says an event of the seat mesh-broker, with the SDK's headers, under its one event id (the lead's ruling,
|
||||
// registry milestone, 13:0x; design 25 § 12.3): on the seat's own subject `mesh.seat.mesh-broker.event.<event>`, which the
|
||||
// shared events stream keeps 7 days and every consumer of `mesh-broker.<event>` hears, like any seat's event; then on the
|
||||
// bus module's own stream (its old `streams` section, 30 days, 256 MiB), which its tool `history` reads. The seat's subject
|
||||
// first: a reader hears it before the history holds it, never the other way round.
|
||||
func streamEmitter(js jetstream.JetStream, a process.Account) func(string, keeper.Event) error {
|
||||
return func(event string, body keeper.Event) error {
|
||||
data, err := json.Marshal(body)
|
||||
// first: a reader hears it before the history holds it, never the other way round. It says it on s.Left (every subject
|
||||
// when nil) and leaves in s.Left what the bus did not take, so the keeper says that again with the same id and the bus
|
||||
// keeps it once (the two publishes are not one act: a failed second one is kept and said again, never silent).
|
||||
func streamEmitter(js jetstream.JetStream, a process.Account) func(*keeper.Saying) error {
|
||||
return func(s *keeper.Saying) error {
|
||||
data, err := json.Marshal(s.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id := eventID()
|
||||
at := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
for _, subject := range EventSubjects(a, event) {
|
||||
if s.ID == "" {
|
||||
s.ID = keeper.EventID()
|
||||
}
|
||||
at := s.At
|
||||
if at.IsZero() {
|
||||
at = time.Now().UTC()
|
||||
}
|
||||
subjects := s.Left
|
||||
if subjects == nil {
|
||||
subjects = EventSubjects(a, s.Event)
|
||||
}
|
||||
for i, subject := range subjects {
|
||||
msg := nats.NewMsg(subject)
|
||||
msg.Header.Set("x-event-id", id)
|
||||
msg.Header.Set("x-event-id", s.ID)
|
||||
msg.Header.Set("x-source", a.Module)
|
||||
msg.Header.Set("x-node", a.Node)
|
||||
msg.Header.Set("x-time", at)
|
||||
msg.Header.Set("x-time", at.UTC().Format(time.RFC3339Nano))
|
||||
msg.Header.Set("content-type", "application/json")
|
||||
msg.Data = data
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
_, err = js.PublishMsg(ctx, msg, jetstream.WithMsgID(id))
|
||||
_, err = js.PublishMsg(ctx, msg, jetstream.WithMsgID(s.ID))
|
||||
cancel()
|
||||
if err != nil {
|
||||
return fmt.Errorf("saying %s on %s: %w", event, subject, err)
|
||||
s.Left = append([]string(nil), subjects[i:]...)
|
||||
return fmt.Errorf("saying %s on %s: %w", s.Event, subject, err)
|
||||
}
|
||||
}
|
||||
s.Left = nil
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -115,12 +155,6 @@ func EventSubjects(a process.Account, event string) []string {
|
||||
return []string{seatdef.EventSubject(catalogue.BrokerSeat, event), a.StreamSubject(event)}
|
||||
}
|
||||
|
||||
func eventID() string {
|
||||
var b [16]byte
|
||||
_, _ = rand.Read(b[:])
|
||||
return hex.EncodeToString(b[:])
|
||||
}
|
||||
|
||||
// sayer raises and clears the keeper's conditions through the condition keeper, as every module says them.
|
||||
func sayer(js jetstream.JetStream) func(oc.Condition) error {
|
||||
return func(c oc.Condition) error {
|
||||
@@ -201,22 +235,9 @@ func runKeeper(ctx context.Context, getenv func(string) string, logOut io.Writer
|
||||
_, err := recorder.Record(handacts.Request{What: what, Why: why, Cause: cause})
|
||||
return err
|
||||
},
|
||||
Warrant: func(id string) (*oc.Ask, *oc.Warrant, error) {
|
||||
e, err := conn.Ask(asksBucket, id)
|
||||
if err != nil || e == nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var rec struct {
|
||||
Ask *oc.Ask `json:"ask"`
|
||||
Warrant *oc.Warrant `json:"warrant"`
|
||||
}
|
||||
if err := json.Unmarshal(e.Value, &rec); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return rec.Ask, rec.Warrant, nil
|
||||
},
|
||||
Logf: logger.Printf,
|
||||
Raise: percent(getenv, "MESH_NATS_FULL_RAISE", 75), Clear: percent(getenv, "MESH_NATS_FULL_CLEAR", 70),
|
||||
Warrant: WarrantFrom(conn.Ask),
|
||||
Logf: logger.Printf,
|
||||
Raise: percent(getenv, "MESH_NATS_FULL_RAISE", 75), Clear: percent(getenv, "MESH_NATS_FULL_CLEAR", 70),
|
||||
}
|
||||
logger.Printf("started on the account %s: asking %s.%s for the first view", account.User, keeper.FirstViewOwner, keeper.FirstViewVerb)
|
||||
return Keep(ctx, k, hearer(js, account, logger.Printf), requestsOf(conn), logger.Printf)
|
||||
@@ -246,11 +267,20 @@ func hearer(js jetstream.JetStream, a process.Account, logf func(string, ...any)
|
||||
}
|
||||
}
|
||||
|
||||
// Keep is the keeper's loop, apart from the bus so a test drives it: the first view asked until it answers, on its
|
||||
// backoff; then hear, which applies each manifest-accepted; a fill pass every minute; the waiting entries made again
|
||||
// on the same backoff; and the persons' requests acted on as they come. It returns when ctx ends.
|
||||
// DecidedSubject is where the operator channel says the operator's answer to one of the bus module's own asks (ADR
|
||||
// 0259 §6; ADR 0620 §2), heard on the keeper's durable consumer: the warrant a recreate or a drop waits for.
|
||||
var DecidedSubject = oc.SubjectDecided(keeper.Module)
|
||||
|
||||
// Keep is the keeper's loop, apart from the bus so a test drives it: a fill pass at once and every minute, from its
|
||||
// start, so the store's one guard runs while the first view is still asked (design 25 § 12.3); the first view asked
|
||||
// until it answers, on its backoff; then hear, which applies each manifest-accepted and wakes the persons' requests on
|
||||
// the operator channel's decided for the bus module's own asks; the waiting entries made again on the same backoff; and
|
||||
// the persons' requests acted on as they are written or answered, never on a timer. It returns when ctx ends.
|
||||
func Keep(ctx context.Context, k *keeper.Keeper, hear func(context.Context, func(string, []byte) error) error,
|
||||
requests func(context.Context, func()) error, logf func(string, ...any)) error {
|
||||
tick := time.NewTicker(time.Minute)
|
||||
defer tick.Stop()
|
||||
k.FillPass()
|
||||
var wait time.Duration
|
||||
for {
|
||||
next, err := k.AskFirstView(wait)
|
||||
@@ -259,22 +289,36 @@ func Keep(ctx context.Context, k *keeper.Keeper, hear func(context.Context, func
|
||||
}
|
||||
logf("the first view did not answer (%v): nothing is created or orphaned until it does; asked again in %s", err, next)
|
||||
wait = next
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(next):
|
||||
again := time.After(next)
|
||||
waiting:
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-tick.C:
|
||||
k.FillPass()
|
||||
case <-again:
|
||||
break waiting
|
||||
}
|
||||
}
|
||||
}
|
||||
logf("the first view answered at revision %d", k.Status().FirstView.Revision)
|
||||
heard := make(chan struct{}, 1)
|
||||
poke := func() {
|
||||
select {
|
||||
case heard <- struct{}{}:
|
||||
default:
|
||||
wake := func(c chan struct{}) func() {
|
||||
return func() {
|
||||
select {
|
||||
case c <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
heard, asked := make(chan struct{}, 1), make(chan struct{}, 1)
|
||||
for backoff := 10 * time.Second; ; backoff = min(backoff*2, 5*time.Minute) {
|
||||
err := hear(ctx, func(subject string, body []byte) error {
|
||||
if subject == DecidedSubject {
|
||||
// The operator answered one of the bus module's asks: the waiting requests are read again now.
|
||||
wake(asked)()
|
||||
return nil
|
||||
}
|
||||
if subject != AcceptedSubject && !strings.HasSuffix(subject, ".event."+delivery.ManifestAcceptedEvent) {
|
||||
return nil
|
||||
}
|
||||
@@ -284,7 +328,7 @@ func Keep(ctx context.Context, k *keeper.Keeper, hear func(context.Context, func
|
||||
return nil
|
||||
}
|
||||
k.Heard(a)
|
||||
poke()
|
||||
wake(heard)()
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
@@ -297,22 +341,15 @@ func Keep(ctx context.Context, k *keeper.Keeper, hear func(context.Context, func
|
||||
case <-time.After(backoff):
|
||||
}
|
||||
}
|
||||
asked := make(chan struct{}, 1)
|
||||
if requests != nil {
|
||||
if err := requests(ctx, func() {
|
||||
select {
|
||||
case asked <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}); err != nil {
|
||||
logf("the persons' requests cannot be watched (%v): acted on at each fill pass", err)
|
||||
if err := requests(ctx, wake(asked)); err != nil {
|
||||
logf("the persons' requests cannot be watched (%v): acted on as the operator answers", err)
|
||||
}
|
||||
}
|
||||
tick := time.NewTicker(time.Minute)
|
||||
defer tick.Stop()
|
||||
// What waited while the keeper was down, or before its first view, is acted on now.
|
||||
k.ActOnRequests()
|
||||
retry := 10 * time.Second
|
||||
var again <-chan time.Time
|
||||
k.FillPass()
|
||||
for {
|
||||
if len(k.Waiting()) > 0 && again == nil {
|
||||
again = time.After(retry)
|
||||
@@ -322,8 +359,6 @@ func Keep(ctx context.Context, k *keeper.Keeper, hear func(context.Context, func
|
||||
return ctx.Err()
|
||||
case <-tick.C:
|
||||
k.FillPass()
|
||||
// A recreate or a drop waits for the operator's warrant: read again once a minute, as an asker reads its asks.
|
||||
k.ActOnRequests()
|
||||
case <-asked:
|
||||
k.ActOnRequests()
|
||||
case <-heard:
|
||||
|
||||
@@ -67,6 +67,18 @@ func (s kvStore) Delete(state, key string) error {
|
||||
return kv.Delete(context.Background(), key)
|
||||
}
|
||||
|
||||
func (s kvStore) Create(state, key string, value []byte) (uint64, error) {
|
||||
kv, err := s.kv(state)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rev, err := kv.Create(context.Background(), key, value)
|
||||
if errors.Is(err, jetstream.ErrKeyExists) {
|
||||
return 0, keeper.ErrExists
|
||||
}
|
||||
return rev, err
|
||||
}
|
||||
|
||||
func (s kvStore) Keys(state string) ([]string, error) {
|
||||
kv, err := s.kv(state)
|
||||
if err != nil {
|
||||
@@ -124,10 +136,23 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// The keeper's consumer hears the indexer's verdicts and the operator channel's decided for the bus module's own
|
||||
// asks, as the grants part composes it from its consumes and the seats it uses.
|
||||
if _, err := js.CreateOrUpdateConsumer(ctx, "EVENTS", jetstream.ConsumerConfig{Durable: account.QueueConsumerName(),
|
||||
FilterSubject: AcceptedSubject, AckPolicy: jetstream.AckExplicitPolicy, MaxDeliver: 5}); err != nil {
|
||||
FilterSubjects: []string{AcceptedSubject, DecidedSubject}, AckPolicy: jetstream.AckExplicitPolicy, MaxDeliver: 5}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The router's own record of the asks, which the keeper reads under its own process account (process.Conn.Ask), never
|
||||
// a stand-in: an ask's record is `<asker>.<ask id>`, as the router keeps it.
|
||||
asksKV, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: asksBucket, History: 5})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pconn, err := process.Connect(process.Account{URL: s.ClientURL(), Node: account.Node, Module: account.Module, User: account.User, Password: "p"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pconn.Close()
|
||||
for _, st := range []string{keeper.StateAsked, keeper.StateRequests} {
|
||||
if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "nats_" + st, History: 5}); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -138,8 +163,6 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
view := keeper.FirstView{Revision: 1}
|
||||
var acts []string
|
||||
var ask *oc.Ask
|
||||
var warrant *oc.Warrant
|
||||
newKeeper := func() *keeper.Keeper {
|
||||
return &keeper.Keeper{Bus: keeper.JetStream{JS: js, Snap: bussnap.Into(nc, snapshots)}, Store: store,
|
||||
Emit: streamEmitter(js, account), Say: sayer(js),
|
||||
@@ -150,7 +173,7 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
acts = append(acts, cause+": "+what)
|
||||
return nil
|
||||
},
|
||||
Warrant: func(string) (*oc.Ask, *oc.Warrant, error) { mu.Lock(); defer mu.Unlock(); return ask, warrant, nil },
|
||||
Warrant: WarrantFrom(pconn.Ask),
|
||||
Logf: t.Logf}
|
||||
}
|
||||
run := func(k *keeper.Keeper) func() {
|
||||
@@ -221,6 +244,15 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
state := entry("drafts", "key-value", "state", nil)
|
||||
log := entry("log", "stream", "log", map[string]string{"max-mib": "1"})
|
||||
|
||||
// Review round 1 (running F1): the fill pass runs from the start, while the first view is still asked, so the store's
|
||||
// one guard never waits on the indexer.
|
||||
t.Run("the fill pass runs while the first view is asked", func(t *testing.T) {
|
||||
down := newKeeper()
|
||||
down.View = func() (keeper.FirstView, error) { return keeper.FirstView{}, errors.New("no responders") }
|
||||
stopDown := run(down)
|
||||
eventually(t, "a fill pass with no first view", func() bool { return !down.Status().Pass.IsZero() && down.Status().FirstView.Answered.IsZero() })
|
||||
stopDown()
|
||||
})
|
||||
k := newKeeper()
|
||||
stop := run(k)
|
||||
t.Run("a verdict, then created, with its binding", func(t *testing.T) {
|
||||
@@ -317,23 +349,35 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
}
|
||||
put(keeper.Request{ID: "r1", Verb: keeper.VerbDrop, Module: "m", Local: "drafts", Caller: "person.jo", Why: "unused"})
|
||||
eventually(t, "the drop with no ask refused", func() bool { return strings.Contains(get("r1").Error, "operator's warrant") })
|
||||
r := keeper.Request{ID: "r2", Verb: keeper.VerbDrop, Module: "m", Local: "drafts", Caller: "person.jo", Why: "unused"}
|
||||
r := keeper.Request{ID: "r2", Verb: keeper.VerbDrop, Module: "m", Local: "drafts", Caller: "person.jo", Why: "unused", At: time.Now()}
|
||||
a, _ := askFor(r, time.Now())
|
||||
r.Ask = a.ID
|
||||
mu.Lock()
|
||||
ask = &a
|
||||
mu.Unlock()
|
||||
record := func(w *oc.Warrant) {
|
||||
raw, _ := json.Marshal(map[string]any{"asker": keeper.Module, "ask": a, "state": "open", "warrant": w})
|
||||
if _, err := asksKV.Put(ctx, keeper.Module+"."+a.ID, raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
record(nil)
|
||||
put(r)
|
||||
k.ActOnRequests()
|
||||
eventually(t, "the request read, waiting for the operator", func() bool { return get("r2").Waiting != "" })
|
||||
if _, ok := held("KV_m_drafts"); !ok || !get("r2").Done.IsZero() {
|
||||
t.Fatal("dropped before the operator chose")
|
||||
}
|
||||
mu.Lock()
|
||||
warrant = &oc.Warrant{Ask: a.ID, Asker: keeper.Module, Outcome: occ.OutcomeChosen, By: &oc.Person{Who: occ.Operator},
|
||||
// The operator chooses: the router records the warrant and says decided; the keeper acts on decided, on its
|
||||
// durable consumer, with no timer between and no call of its pass by the test (seams F1).
|
||||
w := &oc.Warrant{Ask: a.ID, Asker: keeper.Module, Outcome: occ.OutcomeChosen, By: &oc.Person{Who: occ.Operator},
|
||||
Option: "go", Label: "Remove", Level: occ.Destroy, At: occ.FormatTime(time.Now()), AskDigest: occ.Digest(a)}
|
||||
mu.Unlock()
|
||||
k.ActOnRequests()
|
||||
record(w)
|
||||
decided := time.Now()
|
||||
body, _ := json.Marshal(w)
|
||||
if _, err := js.Publish(ctx, DecidedSubject, body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eventually(t, "dropped m drafts", saidHas("dropped m drafts"))
|
||||
if since := time.Since(decided); since > 5*time.Second {
|
||||
t.Errorf("dropped %s after decided: within seconds", since)
|
||||
}
|
||||
if _, ok := held("KV_m_drafts"); ok {
|
||||
t.Error("KV_m_drafts is still on the bus")
|
||||
}
|
||||
@@ -348,6 +392,18 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
if !strings.Contains(recorded, "nats_drop: dropped drafts of m") {
|
||||
t.Errorf("recorded %s: the drop as a hand act", recorded)
|
||||
}
|
||||
// The same warrant handed in a second request is refused and deletes nothing (security F2): drafts asked for and
|
||||
// orphaned again, then a request naming the spent ask.
|
||||
accepted("m", 9, state, entry("log", "stream", "log", map[string]string{"max-mib": "4"}))
|
||||
eventually(t, "m drafts made again", func() bool { _, ok := held("KV_m_drafts"); return ok })
|
||||
accepted("m", 10, entry("log", "stream", "log", map[string]string{"max-mib": "4"}))
|
||||
eventually(t, "m drafts orphaned again", func() bool { return k.Held()["m"].Made["drafts"].Orphaned })
|
||||
again := keeper.Request{ID: "r2b", Verb: keeper.VerbDrop, Module: "m", Local: "drafts", Caller: "person.jo", Why: "again", Ask: a.ID, At: time.Now()}
|
||||
put(again)
|
||||
eventually(t, "the second request on the spent warrant refused", func() bool { return strings.Contains(get("r2b").Error, "already acted") })
|
||||
if _, ok := held("KV_m_drafts"); !ok {
|
||||
t.Error("the spent warrant deleted KV_m_drafts a second time")
|
||||
}
|
||||
})
|
||||
t.Run("nats_first_view_again asks at once and records the hand act", func(t *testing.T) {
|
||||
raw, _ := json.Marshal(keeper.Request{ID: "r3", Verb: keeper.VerbFirstViewAgain, Why: "a verdict thought missed", Caller: "person.jo"})
|
||||
|
||||
@@ -5,12 +5,16 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/go/seats/operatorchannel"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/handacts"
|
||||
|
||||
"nats-tools/internal/keeper"
|
||||
)
|
||||
@@ -23,16 +27,31 @@ import (
|
||||
|
||||
// requestStore is the keeper's state requests, as this bundle reaches it.
|
||||
type requestStore struct {
|
||||
Put func(key string, r keeper.Request) error
|
||||
Get func(key string) (*keeper.Request, error)
|
||||
Ask func(a oc.Ask) error
|
||||
Now func() time.Time
|
||||
Wait func(d time.Duration)
|
||||
Put func(key string, r keeper.Request) error
|
||||
Get func(key string) (*keeper.Request, error)
|
||||
Keys func() ([]string, error)
|
||||
Delete func(key string) error
|
||||
// Raw reads one key's value as it is (an acted key is no request).
|
||||
Raw func(key string) ([]byte, error)
|
||||
Ask func(a oc.Ask) error
|
||||
Now func() time.Time
|
||||
// Ended answers the request's end once the keeper wrote it, or nil once within passed: heard on the state's watch,
|
||||
// never read again on a timer.
|
||||
Ended func(id string, within time.Duration) *keeper.Request
|
||||
// Record records a hand act through the seat mesh-hand-acts.
|
||||
Record Recorded
|
||||
}
|
||||
|
||||
// RequestsInState is the store through the runtime that launched this bundle.
|
||||
// RequestsInState is the store through the runtime that launched this bundle: one watch of the state requests for the
|
||||
// bundle's life (the runtime's watches last as long as the process), handing each end to the call waiting for it.
|
||||
func RequestsInState() requestStore {
|
||||
s := stdio.State(keeper.StateRequests)
|
||||
ends := newEnds(func(handle func(key string, value []byte)) error {
|
||||
return s.Watch("", func(c stdio.StateChange) error {
|
||||
handle(c.Key, c.Value)
|
||||
return nil
|
||||
})
|
||||
})
|
||||
return requestStore{
|
||||
Put: func(key string, r keeper.Request) error { _, err := s.Put(key, r); return err },
|
||||
Get: func(key string) (*keeper.Request, error) {
|
||||
@@ -43,9 +62,72 @@ func RequestsInState() requestStore {
|
||||
var r keeper.Request
|
||||
return &r, json.Unmarshal(e.Value, &r)
|
||||
},
|
||||
Ask: func(a oc.Ask) error { return stdio.SeatPublish(oc.AcceptSubjectAsk(keeper.Module), a, a.ID) },
|
||||
Now: func() time.Time { return time.Now().UTC() },
|
||||
Wait: time.Sleep,
|
||||
Keys: s.Keys,
|
||||
Delete: s.Delete,
|
||||
Raw: func(key string) ([]byte, error) {
|
||||
e, err := s.Get(key)
|
||||
if err != nil || e == nil {
|
||||
return nil, err
|
||||
}
|
||||
return e.Value, nil
|
||||
},
|
||||
Ask: func(a oc.Ask) error { return stdio.SeatPublish(oc.AcceptSubjectAsk(keeper.Module), a, a.ID) },
|
||||
Now: func() time.Time { return time.Now().UTC() },
|
||||
Ended: ends.wait,
|
||||
Record: RecordedBySeat,
|
||||
}
|
||||
}
|
||||
|
||||
// ends hands a request's end to the call waiting for it, from one watch of the state started at the first wait.
|
||||
type ends struct {
|
||||
watch func(handle func(key string, value []byte)) error
|
||||
mu sync.Mutex
|
||||
started bool
|
||||
waiting map[string][]chan *keeper.Request
|
||||
}
|
||||
|
||||
func newEnds(watch func(handle func(key string, value []byte)) error) *ends {
|
||||
return &ends{watch: watch, waiting: map[string][]chan *keeper.Request{}}
|
||||
}
|
||||
|
||||
func (e *ends) heard(key string, value []byte) {
|
||||
var r keeper.Request
|
||||
if json.Unmarshal(value, &r) != nil || r.Done.IsZero() {
|
||||
return
|
||||
}
|
||||
e.mu.Lock()
|
||||
chans := e.waiting[key]
|
||||
delete(e.waiting, key)
|
||||
e.mu.Unlock()
|
||||
for _, c := range chans {
|
||||
c <- &r
|
||||
}
|
||||
}
|
||||
|
||||
// wait waits for id's end at most within: nil when it did not come, or the watch could not be taken.
|
||||
func (e *ends) wait(id string, within time.Duration) *keeper.Request {
|
||||
c := make(chan *keeper.Request, 1)
|
||||
e.mu.Lock()
|
||||
e.waiting[id] = append(e.waiting[id], c)
|
||||
start := !e.started
|
||||
e.started = true
|
||||
e.mu.Unlock()
|
||||
if start {
|
||||
if err := e.watch(e.heard); err != nil {
|
||||
e.mu.Lock()
|
||||
e.started = false
|
||||
e.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
select {
|
||||
case r := <-c:
|
||||
return r
|
||||
case <-time.After(within):
|
||||
e.mu.Lock()
|
||||
e.waiting[id] = slices.DeleteFunc(e.waiting[id], func(w chan *keeper.Request) bool { return w == c })
|
||||
e.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +145,7 @@ func askFor(r keeper.Request, now time.Time) (oc.Ask, error) {
|
||||
}
|
||||
what := map[string]string{keeper.VerbDrop: "remove", keeper.VerbRecreate: "rebuild"}[r.Verb]
|
||||
name := keeper.BusName(r.Module, r.Local)
|
||||
return oc.Ask{ID: "bus-" + r.ID, Who: occ.Operator, Expires: occ.FormatTime(now.Add(24 * time.Hour)),
|
||||
return oc.Ask{ID: "bus-" + r.ID, Who: occ.Operator, Expires: occ.FormatTime(now.Add(keeper.AskLasts)),
|
||||
OnExpiry: "nothing is removed and the entry stays as it is",
|
||||
About: keeper.ConditionKey(map[string]string{keeper.VerbDrop: "orphaned", keeper.VerbRecreate: "drifted"}[r.Verb], r.Module, r.Local),
|
||||
Headline: fmt.Sprintf("%s's kept data %s: %s it?", r.Module, r.Local, what),
|
||||
@@ -100,12 +182,14 @@ func requestTools(rs requestStore) []stdio.Tool {
|
||||
return nil, err
|
||||
}
|
||||
r.Ask = ask.ID
|
||||
if err := rs.Put(r.ID, r); err != nil {
|
||||
return nil, fmt.Errorf("the request cannot be handed to the keeper: %w", err)
|
||||
}
|
||||
// The ask first: a request is never handed for an ask that was not raised (an ask with no request acts on
|
||||
// nothing, since the keeper acts only on a request).
|
||||
if err := rs.Ask(ask); err != nil {
|
||||
return nil, fmt.Errorf("the operator's ask cannot be raised, so nothing will be removed: %w", err)
|
||||
}
|
||||
if err := rs.Put(r.ID, r); err != nil {
|
||||
return nil, fmt.Errorf("the request cannot be handed to the keeper, so nothing will be removed whatever the operator answers: %w", err)
|
||||
}
|
||||
return map[string]any{"request": r.ID, "ask": ask.ID, "state": "the operator is asked; the keeper acts once they approve, after a snapshot",
|
||||
"read": fmt.Sprintf("nats_%s with request %s", verb, r.ID)}, nil
|
||||
}
|
||||
@@ -137,6 +221,7 @@ func requestTools(rs requestStore) []stdio.Tool {
|
||||
return awaitEnd(rs, r, "nats_first_view_again"), nil
|
||||
}}
|
||||
return []stdio.Tool{
|
||||
requestsTool(rs),
|
||||
firstView,
|
||||
tool(keeper.VerbCreate, "Create one entry of a module's kept data on the bus now, as its accepted manifest asks for it (design 25 § 12.3): handed to the bus module's keeper, which alone may create, and recorded as a hand act."),
|
||||
tool(keeper.VerbApply, "Apply what a module's manifest asks of one entry where the bus holds less (a larger cap, a longer age): only a difference that grows; handed to the keeper and recorded as a hand act."),
|
||||
@@ -145,14 +230,83 @@ func requestTools(rs requestStore) []stdio.Tool {
|
||||
}
|
||||
}
|
||||
|
||||
// awaitEnd reads a request's end back: the keeper acts on its watch of requests at once, so the end is read for at most
|
||||
// half a minute, and otherwise how to read it later is answered.
|
||||
// awaitEnd waits for a request's end, which the keeper writes as soon as its watch of requests hands it the request;
|
||||
// past half a minute it answers how to read the end later.
|
||||
func awaitEnd(rs requestStore, r keeper.Request, tool string) any {
|
||||
for wait := 100 * time.Millisecond; wait < 20*time.Second; wait *= 2 {
|
||||
rs.Wait(wait)
|
||||
if got, err := rs.Get(r.ID); err == nil && got != nil && !got.Done.IsZero() {
|
||||
if rs.Ended != nil {
|
||||
if got := rs.Ended(r.ID, 30*time.Second); got != nil {
|
||||
return got
|
||||
}
|
||||
}
|
||||
if got, err := rs.Get(r.ID); err == nil && got != nil && !got.Done.IsZero() {
|
||||
return got
|
||||
}
|
||||
return map[string]any{"request": r.ID, "state": "handed to the keeper, not ended yet", "read": fmt.Sprintf("%s with request %s", tool, r.ID)}
|
||||
}
|
||||
|
||||
// requestsTool reads the keeper's state requests (design 25 § 12.3; review round 1, tools F3): every request with what
|
||||
// waits, what ended and why, and each warrant spent; and drops one request not ended yet by hand, recorded as a hand act,
|
||||
// so a request nobody will answer leaves by a person's act and never only by its key ageing out.
|
||||
func requestsTool(rs requestStore) stdio.Tool {
|
||||
return stdio.Tool{Name: "nats_requests",
|
||||
Description: "The persons' requests the bus module's tools handed its keeper (nats_create, nats_apply, nats_recreate, nats_drop, nats_first_view_again): each one waiting (and for what: the operator's warrant on its ask, or the keeper's first view) or ended, with its outcome or why not, and each warrant spent (acted.<ask id>, a warrant acts once). With drop and why, removes one request not ended yet, recorded as a hand act; the operator's ask it raised then acts on nothing.",
|
||||
Input: map[string]any{"drop": str("a request not ended yet, to remove by hand"), "why": str("why, in plain words: kept with the act")},
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
if id := strings.TrimSpace(arg(a, "drop")); id != "" {
|
||||
why := strings.TrimSpace(arg(a, "why"))
|
||||
if why == "" {
|
||||
return nil, fmt.Errorf("nats_requests drop says why")
|
||||
}
|
||||
r, err := rs.Get(id)
|
||||
if err != nil || r == nil || strings.HasPrefix(id, keeper.ActedPrefix) {
|
||||
return nil, fmt.Errorf("no request %s is held (%v)", id, err)
|
||||
}
|
||||
if !r.Done.IsZero() {
|
||||
return nil, fmt.Errorf("the request %s ended at %s (%s): only a request that waits is dropped", id, r.Done.Format(time.RFC3339), r.Outcome)
|
||||
}
|
||||
if err := rs.Record(handacts.Request{What: fmt.Sprintf("dropped the waiting request %s (%s of %s's %s, ask %s)", id, r.Verb, r.Module, r.Local, r.Ask),
|
||||
Why: why, Cause: "nats_requests"}); err != nil {
|
||||
return nil, fmt.Errorf("the hand act cannot be recorded, so the request stays: %w", err)
|
||||
}
|
||||
if err := rs.Delete(id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"dropped": id, "ask": r.Ask, "state": "the keeper acts on it no more; the operator's ask it raised acts on nothing"}, nil
|
||||
}
|
||||
keys, err := rs.Keys()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Strings(keys)
|
||||
var waiting, ended []keeper.Request
|
||||
spent := []keeper.Acted{}
|
||||
for _, key := range keys {
|
||||
if strings.HasPrefix(key, keeper.ActedPrefix) {
|
||||
raw, err := rs.Raw(key)
|
||||
var acted keeper.Acted
|
||||
if err == nil && json.Unmarshal(raw, &acted) == nil {
|
||||
spent = append(spent, acted)
|
||||
}
|
||||
continue
|
||||
}
|
||||
r, err := rs.Get(key)
|
||||
if err != nil || r == nil {
|
||||
continue
|
||||
}
|
||||
if r.Done.IsZero() {
|
||||
waiting = append(waiting, *r)
|
||||
} else {
|
||||
ended = append(ended, *r)
|
||||
}
|
||||
}
|
||||
return map[string]any{"waiting": orEmpty(waiting), "ended": orEmpty(ended), "warrants-spent": spent,
|
||||
"why-waiting": "a recreate or a drop waits for the operator's warrant on its ask, acted on once they answer, and ends unanswered when its ask ends; any request waits while the keeper holds no first view"}, nil
|
||||
}}
|
||||
}
|
||||
|
||||
func orEmpty(rs []keeper.Request) []keeper.Request {
|
||||
if rs == nil {
|
||||
return []keeper.Request{}
|
||||
}
|
||||
return rs
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -8,6 +10,7 @@ import (
|
||||
occ "git.novox.be/novox/mesh-contracts/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/go/seats/operatorchannel"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/handacts"
|
||||
|
||||
"nats-tools/internal/keeper"
|
||||
)
|
||||
@@ -18,6 +21,7 @@ import (
|
||||
func TestAPersonsRequestIsHandedToTheKeeperAsTheProvedCaller(t *testing.T) {
|
||||
held := map[string]keeper.Request{}
|
||||
var raised []oc.Ask
|
||||
var acts []handacts.Request
|
||||
rs := requestStore{
|
||||
Put: func(key string, r keeper.Request) error {
|
||||
if r.Verb == keeper.VerbCreate {
|
||||
@@ -34,9 +38,18 @@ func TestAPersonsRequestIsHandedToTheKeeperAsTheProvedCaller(t *testing.T) {
|
||||
}
|
||||
return &r, nil
|
||||
},
|
||||
Ask: func(a oc.Ask) error { raised = append(raised, a); return nil },
|
||||
Now: func() time.Time { return time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC) },
|
||||
Wait: func(time.Duration) {},
|
||||
Keys: func() ([]string, error) {
|
||||
var out []string
|
||||
for k := range held {
|
||||
out = append(out, k)
|
||||
}
|
||||
return append(out, keeper.ActedKey("bus-old")), nil
|
||||
},
|
||||
Delete: func(key string) error { delete(held, key); return nil },
|
||||
Raw: func(string) ([]byte, error) { return []byte(`{"ask":"bus-old","request":"r0","verb":"drop"}`), nil },
|
||||
Ask: func(a oc.Ask) error { raised = append(raised, a); return nil },
|
||||
Now: func() time.Time { return time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC) },
|
||||
Record: func(q handacts.Request) error { acts = append(acts, q); return nil },
|
||||
}
|
||||
tools := map[string]stdio.Tool{}
|
||||
for _, tl := range requestTools(rs) {
|
||||
@@ -68,4 +81,65 @@ func TestAPersonsRequestIsHandedToTheKeeperAsTheProvedCaller(t *testing.T) {
|
||||
if got, _ := tools["nats_drop"].Run(map[string]any{"request": r.ID}); got.(*keeper.Request).ID != r.ID {
|
||||
t.Errorf("reading the request back answered %+v", got)
|
||||
}
|
||||
// Review round 1 (tools F3): nats_requests reads what waits, what ended and each warrant spent, and drops a waiting
|
||||
// request by hand, recorded; an ended one is not dropped.
|
||||
out, err = tools["nats_requests"].Run(map[string]any{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
list := out.(map[string]any)
|
||||
if w, e, spent := list["waiting"].([]keeper.Request), list["ended"].([]keeper.Request), list["warrants-spent"].([]keeper.Acted); len(w) != 1 ||
|
||||
w[0].ID != r.ID || len(e) != 1 || len(spent) != 1 || spent[0].Ask != "bus-old" {
|
||||
t.Errorf("nats_requests answered %+v: the drop waiting, the create ended, one warrant spent", list)
|
||||
}
|
||||
if _, err := tools["nats_requests"].Run(map[string]any{"drop": r.ID}); err == nil || !strings.Contains(err.Error(), "says why") {
|
||||
t.Errorf("a drop by hand without why: %v", err)
|
||||
}
|
||||
for id, r := range held {
|
||||
if r.Verb == keeper.VerbCreate {
|
||||
if _, err := tools["nats_requests"].Run(map[string]any{"drop": id, "why": "x"}); err == nil || !strings.Contains(err.Error(), "only a request that waits") {
|
||||
t.Errorf("dropping an ended request: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err := tools["nats_requests"].Run(map[string]any{"drop": r.ID, "why": "nobody will answer"}); err != nil || len(acts) != 1 ||
|
||||
!strings.Contains(acts[0].What, "dropped the waiting request "+r.ID) {
|
||||
t.Errorf("dropped by hand (%v), recorded %+v", err, acts)
|
||||
}
|
||||
if _, still := held[r.ID]; still {
|
||||
t.Error("the dropped request is still held")
|
||||
}
|
||||
// The ask first: an ask that cannot be raised hands no request to the keeper.
|
||||
before := len(held)
|
||||
rs.Ask = func(oc.Ask) error { return errors.New("no responders") }
|
||||
tools = map[string]stdio.Tool{}
|
||||
for _, tl := range requestTools(rs) {
|
||||
tools[tl.Name] = tl
|
||||
}
|
||||
if _, err := tools["nats_recreate"].Run(map[string]any{"module": "m", "local": "log", "why": "drifted"}); err == nil || len(held) != before {
|
||||
t.Errorf("an ask not raised: %v, %d requests held (was %d)", err, len(held), before)
|
||||
}
|
||||
// A request's end is heard on the one watch of the state, handed to the call that waits for it.
|
||||
var handle func(string, []byte)
|
||||
watches := 0
|
||||
e := newEnds(func(h func(string, []byte)) error { watches++; handle = h; return nil })
|
||||
got := make(chan *keeper.Request, 1)
|
||||
go func() { got <- e.wait("r1", time.Minute) }()
|
||||
for {
|
||||
e.mu.Lock()
|
||||
ready := len(e.waiting["r1"]) == 1 && handle != nil
|
||||
e.mu.Unlock()
|
||||
if ready {
|
||||
break
|
||||
}
|
||||
runtime.Gosched()
|
||||
}
|
||||
handle("r1", []byte(`{"id":"r1"}`))
|
||||
handle("r1", []byte(`{"id":"r1","done":"2026-11-04T12:00:01Z","outcome":"created"}`))
|
||||
if r := <-got; r == nil || r.Outcome != "created" || watches != 1 {
|
||||
t.Errorf("the end heard %+v on %d watch(es): created, on one", r, watches)
|
||||
}
|
||||
if r := e.wait("r2", time.Millisecond); r != nil || watches != 1 {
|
||||
t.Errorf("an end not written: %+v on %d watch(es)", r, watches)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user