G15 recreate-in-place: a recreate reads the account's room first, changes each object in place where the bus takes it, makes empty only a retention that changes, stops at a refused change; the ask names each object
Builds: design 47 H2 H-A-686 (1) (2) (3), H-A-634 (3), H-A-706 (DQ-42) (hq 95cf2052, 999e6525, 543e6963)
This commit is contained in:
@@ -66,8 +66,9 @@ func capsOf(all map[string]keeper.Asked, status func() (keeper.Status, error)) *
|
||||
type busStream struct {
|
||||
Config jetstream.StreamConfig `json:"config"`
|
||||
State struct {
|
||||
Messages uint64 `json:"messages"`
|
||||
Bytes uint64 `json:"bytes"`
|
||||
Messages uint64 `json:"messages"`
|
||||
Bytes uint64 `json:"bytes"`
|
||||
FirstTS time.Time `json:"first_ts"`
|
||||
} `json:"state"`
|
||||
}
|
||||
|
||||
|
||||
@@ -401,7 +401,7 @@ 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", At: time.Now(), AskExpires: time.Now().Add(keeper.AskLasts)}
|
||||
a, _ := askFor(r)
|
||||
a, _ := askFor(r, "")
|
||||
r.Ask = a.ID
|
||||
record := func(w *oc.Warrant) {
|
||||
raw, _ := json.Marshal(map[string]any{"asker": keeper.Module, "ask": a, "state": "open", "warrant": w})
|
||||
@@ -757,3 +757,71 @@ func entry(name, storage, template string, options map[string]string) catalogue.
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// Design 47 H2 H-A-686: the change the bus takes in place keeps what the object holds. On the real bus, a stream holding
|
||||
// messages is changed to a shorter age, a lifetime per message and a refusal when full, keeps its messages, and takes a lifetime
|
||||
// on its next write; a bucket changed to a lifetime per key keeps its keys.
|
||||
func TestTheBusTakesARecreatesChangeInPlace(t *testing.T) {
|
||||
s, err := server.NewServer(&server.Options{Port: -1, JetStream: true, StoreDir: t.TempDir(), NoLog: true, NoSigs: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go s.Start()
|
||||
if !s.ReadyForConnections(5 * time.Second) {
|
||||
t.Fatal("the server did not start")
|
||||
}
|
||||
defer s.Shutdown()
|
||||
nc, err := nats.Connect(s.ClientURL())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer nc.Close()
|
||||
js, _ := jetstream.New(nc)
|
||||
ctx := context.Background()
|
||||
bus := keeper.JetStream{JS: js}
|
||||
stream := keeper.Object{Kind: keeper.KindStream, Name: "m_events", Subjects: []string{"m.events.>"},
|
||||
Settings: keeper.Settings{MaxBytes: 4 << 20, MaxAgeSeconds: 0, Discard: keeper.DiscardOld}}
|
||||
bucket := keeper.Object{Kind: keeper.KindBucket, Name: "m_state", Settings: keeper.Settings{MaxBytes: 4 << 20, History: 1, Discard: keeper.DiscardNew}}
|
||||
for _, o := range []keeper.Object{stream, bucket} {
|
||||
if err := bus.Create(ctx, o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for i := range 5 {
|
||||
if _, err := js.Publish(ctx, fmt.Sprintf("m.events.%d", i), []byte("x")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
kv, _ := js.KeyValue(ctx, "m_state")
|
||||
for _, k := range []string{"a", "b"} {
|
||||
if _, err := kv.Put(ctx, k, []byte("v")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
stream.Settings = keeper.Settings{MaxBytes: 4 << 20, MaxAgeSeconds: 3600, Discard: keeper.DiscardNew, KeyTTL: true}
|
||||
bucket.Settings.KeyTTL = true
|
||||
for _, o := range []keeper.Object{stream, bucket} {
|
||||
if err := bus.Update(ctx, o); err != nil {
|
||||
t.Fatalf("%s: %v", o.Stream(), err)
|
||||
}
|
||||
}
|
||||
held, fill, _, err := bus.Held(ctx, stream)
|
||||
if err != nil || fill.Messages != 5 || held.MaxAgeSeconds != 3600 || held.Discard != keeper.DiscardNew || !held.KeyTTL {
|
||||
t.Fatalf("the stream holds %+v, %+v (%v): it keeps its messages and takes the settings", held, fill, err)
|
||||
}
|
||||
msg := &nats.Msg{Subject: "m.events.9", Data: []byte("x"), Header: nats.Header{"Nats-TTL": []string{"60s"}}}
|
||||
if _, err := js.PublishMsg(ctx, msg); err != nil {
|
||||
t.Fatalf("a message with a lifetime of its own is refused after the change: %v", err)
|
||||
}
|
||||
if _, fill, _, _ := bus.Held(ctx, bucket); fill.Messages != 2 {
|
||||
t.Fatalf("the bucket holds %d keys, want 2", fill.Messages)
|
||||
}
|
||||
if _, err := kv.Create(ctx, "c", []byte("v"), jetstream.KeyTTL(time.Minute)); err != nil {
|
||||
t.Fatalf("a key with a lifetime of its own is refused after the change: %v", err)
|
||||
}
|
||||
// A held lifetime is never turned off by an Update: the bus would refuse it, and the keeper's comparison matches it.
|
||||
stream.Settings.KeyTTL = false
|
||||
if err := bus.Update(ctx, stream); err != nil {
|
||||
t.Fatalf("an ask of no lifetime over a held one: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,6 +197,17 @@ func allTools() []stdio.Tool {
|
||||
out = append(out, keeperHealthTool(StatusInState, HeldInState, jsz))
|
||||
out = append(out, readTools(jsz, LiveMsgGet(LiveBus()))...)
|
||||
rs := RequestsInState()
|
||||
rs.Explain = func(r keeper.Request) (string, error) {
|
||||
held, err := HeldInState()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
raw, err := jsz()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return recreateWords(held, StatusInState, raw, time.Now(), LiveOlderThan(LiveBus()), r.Module, r.Local)
|
||||
}
|
||||
out = append(out, snapshotListTool(rs, bundleKeepDays)...)
|
||||
out = append(out, plansTool(PlansInState))
|
||||
return append(out, requestTools(rs)...)
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
// What the operator's ask for a recreate names (design 47 H2 H-A-686 (3), H-A-706 on DQ-42): per object whether it is changed in
|
||||
// place or made again empty, its held and asked settings, the messages, bytes and oldest message's time it holds, read when
|
||||
// the ask is raised, and what the change removes; and the room the caps it asks need on the bus's account.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"nats-tools/internal/keeper"
|
||||
)
|
||||
|
||||
// olderThan counts the messages of a stream stored before a time.
|
||||
type olderThan func(stream string, before time.Time) (uint64, error)
|
||||
|
||||
// LiveOlderThan counts them on the bus by an ordered consumer that starts at the time: the messages before it are those the
|
||||
// stream holds less the ones the consumer has pending.
|
||||
func LiveOlderThan(bus Bus) olderThan {
|
||||
return func(stream string, before time.Time) (uint64, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
js, closeBus, err := bus(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer closeBus()
|
||||
s, err := js.Stream(ctx, stream)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
info, err := s.Info(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
c, err := s.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig{DeliverPolicy: jetstream.DeliverByStartTimePolicy,
|
||||
OptStartTime: &before, AckPolicy: jetstream.AckNonePolicy, InactiveThreshold: time.Minute})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
ci, err := c.Info(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
_ = s.DeleteConsumer(ctx, ci.Name)
|
||||
if info.State.Msgs < ci.NumPending {
|
||||
return 0, nil
|
||||
}
|
||||
return info.State.Msgs - ci.NumPending, nil
|
||||
}
|
||||
}
|
||||
|
||||
func settingsWords(s keeper.Settings) string {
|
||||
age := "no age"
|
||||
if s.MaxAgeSeconds > 0 {
|
||||
age = "age " + (time.Duration(s.MaxAgeSeconds) * time.Second).String()
|
||||
}
|
||||
return fmt.Sprintf("cap %d MiB, %s, history %d, discard %s, lifetime %v", s.MaxBytes/keeper.MiB, age, s.History, s.Discard, s.KeyTTL)
|
||||
}
|
||||
|
||||
// recreateWords says, for each object of the entry, what the rebuild does and removes, read from the entry the keeper made and
|
||||
// the bus's streams as the monitoring endpoint answers them; and the room its caps need.
|
||||
func recreateWords(held map[string]keeper.Asked, status func() (keeper.Status, error), jsz []byte, now time.Time, older olderThan,
|
||||
module, local string) (string, error) {
|
||||
m, ok := held[module].Made[local]
|
||||
if !ok || !m.Created {
|
||||
return "", fmt.Errorf("the bus module made no %s of %s", local, module)
|
||||
}
|
||||
streams, err := streamsHeld(jsz)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var lines []string
|
||||
var caps int64
|
||||
for _, o := range m.Plan.Objects {
|
||||
caps += o.Settings.MaxBytes
|
||||
s, found := streams[o.Stream()]
|
||||
if !found {
|
||||
lines = append(lines, fmt.Sprintf("%s: the bus holds none, so it is made, holding nothing to remove", o.Stream()))
|
||||
continue
|
||||
}
|
||||
have := keeper.SettingsOf(o.Kind, s.Config)
|
||||
inPlace := have.WorkQueue == o.Settings.WorkQueue
|
||||
oldest := "none"
|
||||
if !s.State.FirstTS.IsZero() && s.State.Messages > 0 {
|
||||
oldest = s.State.FirstTS.UTC().Format(time.RFC3339)
|
||||
}
|
||||
how, removes := "changed in place", []string{}
|
||||
if !inPlace {
|
||||
how, removes = "made again empty, its retention changes", []string{fmt.Sprintf("every message (%d)", s.State.Messages)}
|
||||
} else {
|
||||
if over := int64(s.State.Bytes) - o.Settings.MaxBytes; over > 0 {
|
||||
removes = append(removes, fmt.Sprintf("the oldest messages beyond the cap, %d bytes over it", over))
|
||||
}
|
||||
if age := o.Settings.MaxAgeSeconds; age > 0 && (have.MaxAgeSeconds == 0 || age < have.MaxAgeSeconds) && s.State.Messages > 0 {
|
||||
cutoff := now.Add(-time.Duration(age) * time.Second)
|
||||
switch {
|
||||
case !s.State.FirstTS.Before(cutoff):
|
||||
removes = append(removes, "no message older than the age")
|
||||
case older == nil:
|
||||
removes = append(removes, "the messages older than the age, not counted here")
|
||||
default:
|
||||
n, err := older(o.Stream(), cutoff)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the messages of %s older than the age cannot be counted: %w", o.Stream(), err)
|
||||
}
|
||||
removes = append(removes, fmt.Sprintf("%d messages older than the age", n))
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(removes) == 0 {
|
||||
removes = []string{"nothing"}
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%s %s; held {%s}, asked {%s}; it holds %d messages, %d bytes, the oldest from %s; the change removes: %s",
|
||||
o.Stream(), how, settingsWords(have), settingsWords(o.Settings), s.State.Messages, s.State.Bytes, oldest, strings.Join(removes, ", ")))
|
||||
}
|
||||
room := fmt.Sprintf("The caps it asks sum to %d MiB", caps/keeper.MiB)
|
||||
if st, err := status(); err == nil && st.Limits.AccountBytes > 0 {
|
||||
need := caps + keeper.CapSum(held, module, local) + st.OldBytes
|
||||
room += fmt.Sprintf(", %d MiB with every other cap of the bus, against the account's storage limit of %d MiB", need/keeper.MiB, st.Limits.AccountBytes/keeper.MiB)
|
||||
}
|
||||
return room + ". Per object: " + strings.Join(lines, " | "), nil
|
||||
}
|
||||
@@ -43,6 +43,9 @@ type requestStore struct {
|
||||
// Standing reads the router's record of one of this module's asks, by its id, or nil while it holds none (design 47
|
||||
// H-A-237, H-A-239).
|
||||
Standing func(askID string) (*stdio.StateEntry, error)
|
||||
// Explain says what a recreate of the request's entry does to each of its objects and removes, and the room its caps need,
|
||||
// read when the ask is raised (design 47 H2 H-A-686 (3), H-A-706); nil says none.
|
||||
Explain func(r keeper.Request) (string, error)
|
||||
}
|
||||
|
||||
// RequestsInState is the store through the runtime that launched this bundle: one watch of the state requests for the
|
||||
@@ -175,7 +178,7 @@ func askExpiry(a map[string]any) (time.Duration, error) {
|
||||
// the approve level until a channel carries a destroy answer, ending at the request's AskExpires, the caller's (design
|
||||
// 47 H-A-237); the bend ends with the research effort that designs the channel carrying the destroy proof (design 47 H2
|
||||
// §813 (11)).
|
||||
func askFor(r keeper.Request) (oc.Ask, error) {
|
||||
func askFor(r keeper.Request, explained string) (oc.Ask, error) {
|
||||
binds, err := occ.ActDigest(r.Act())
|
||||
if err != nil {
|
||||
return oc.Ask{}, err
|
||||
@@ -187,7 +190,7 @@ func askFor(r keeper.Request) (oc.Ask, error) {
|
||||
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),
|
||||
Explanation: fmt.Sprintf("%s asks to %s %s on the bus, %s. A snapshot of it is taken first; its data is lost from the bus.",
|
||||
r.Caller, what, name, r.Why),
|
||||
r.Caller, what, name, r.Why) + explained,
|
||||
Options: []oc.Option{
|
||||
{ID: "go", Label: strings.ToUpper(what[:1]) + what[1:], Does: fmt.Sprintf("%s %s after a snapshot", what, name), Level: occ.Approve, Binds: binds},
|
||||
{ID: "keep", Label: "Keep it", Does: "nothing is removed", Level: occ.Acknowledge},
|
||||
@@ -289,7 +292,15 @@ func requestTools(rs requestStore) []stdio.Tool {
|
||||
return nil, err
|
||||
}
|
||||
r.AskExpires = r.At.Add(lasts)
|
||||
ask, err := askFor(r)
|
||||
var explained string
|
||||
if verb == keeper.VerbRecreate && rs.Explain != nil {
|
||||
words, err := rs.Explain(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the ask cannot name what the rebuild changes and removes, so nothing is asked: %w", err)
|
||||
}
|
||||
explained = " " + words
|
||||
}
|
||||
ask, err := askFor(r, explained)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ func TestAPersonsRequestIsHandedToTheKeeperAsTheProvedCaller(t *testing.T) {
|
||||
func TestTheAskLastsTheCallersExpiryAndTheToolSaysTheRoutersStanding(t *testing.T) {
|
||||
now := time.Now()
|
||||
r := keeper.Request{ID: "r1", Verb: keeper.VerbDrop, Module: "m", Local: "drafts", Why: "unused", Caller: "person.jo", AskExpires: now.Add(1430 * time.Minute)}
|
||||
a, err := askFor(r)
|
||||
a, err := askFor(r, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -200,3 +200,40 @@ func TestTheAskLastsTheCallersExpiryAndTheToolSaysTheRoutersStanding(t *testing.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 H2 H-A-686 (3), H-A-706 (DQ-42): the ask of a recreate names, per object, in place or made empty, what it holds and
|
||||
// what the change removes (a shorter age's older messages, a smaller cap's bytes over it, every message of a retention change),
|
||||
// and the room the caps need.
|
||||
func TestTheAskOfARecreateNamesEachObjectAndWhatItRemoves(t *testing.T) {
|
||||
now := time.Date(2026, 11, 25, 12, 0, 0, 0, time.UTC)
|
||||
hist := keeper.Object{Kind: keeper.KindStream, Name: "m_records", Settings: keeper.Settings{MaxBytes: 4 * keeper.MiB, MaxAgeSeconds: 3600, Discard: keeper.DiscardNew}}
|
||||
state := keeper.Object{Kind: keeper.KindBucket, Name: "m_records_state", Settings: keeper.Settings{MaxBytes: 8 * keeper.MiB, History: 1, Discard: keeper.DiscardNew}}
|
||||
queue := keeper.Object{Kind: keeper.KindStream, Name: "m_queue", Settings: keeper.Settings{MaxBytes: 8 * keeper.MiB, Discard: keeper.DiscardNew, WorkQueue: true}}
|
||||
held := map[string]keeper.Asked{"m": {Module: "m", Made: map[string]keeper.Made{"records": {Created: true,
|
||||
Plan: keeper.Plan{Module: "m", Local: "records", Objects: []keeper.Object{hist, state, queue}}}}}}
|
||||
old := now.Add(-3 * time.Hour).Format(time.RFC3339)
|
||||
jsz := []byte(`{"account_details":[{"stream_detail":[
|
||||
{"name":"m_records","config":{"name":"m_records","max_bytes":16777216,"max_age":0,"discard":"new"},"state":{"messages":50,"bytes":1048576,"first_ts":"` + old + `"}},
|
||||
{"name":"KV_m_records_state","config":{"name":"KV_m_records_state","max_bytes":33554432,"max_msgs_per_subject":1,"discard":"new"},"state":{"messages":9,"bytes":12582912,"first_ts":"` + old + `"}},
|
||||
{"name":"m_queue","config":{"name":"m_queue","max_bytes":8388608,"retention":"limits","discard":"new"},"state":{"messages":7,"bytes":100,"first_ts":"` + old + `"}}]}]}`)
|
||||
var asked time.Time
|
||||
words, err := recreateWords(held, func() (keeper.Status, error) {
|
||||
return keeper.Status{Limits: keeper.Limits{AccountBytes: 100 * keeper.MiB}}, nil
|
||||
}, jsz, now, func(stream string, before time.Time) (uint64, error) { asked = before; return 42, nil }, "m", "records")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"m_records changed in place", "42 messages older than the age", "50 messages, 1048576 bytes",
|
||||
"KV_m_records_state changed in place", "4194304 bytes over it", "m_queue made again empty", "every message (7)", "against the account's storage limit of 100 MiB"} {
|
||||
if !strings.Contains(words, want) {
|
||||
t.Errorf("the ask does not say %q:\n%s", want, words)
|
||||
}
|
||||
}
|
||||
if !asked.Equal(now.Add(-time.Hour)) {
|
||||
t.Errorf("the messages were counted before %v", asked)
|
||||
}
|
||||
ask, err := askFor(keeper.Request{Verb: keeper.VerbRecreate, Module: "m", Local: "records", Why: "w", Caller: "c", AskExpires: now}, " "+words)
|
||||
if err != nil || !strings.Contains(ask.Explanation, "42 messages older than the age") {
|
||||
t.Fatalf("the ask %+v %v", ask, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user