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:
2026-11-25 17:33:19 +01:00
parent 5de68de0a0
commit 8aef19934e
11 changed files with 427 additions and 26 deletions
+12
View File
@@ -154,6 +154,18 @@ func (b JetStream) Update(ctx context.Context, o Object) error {
if o.Settings.History > 0 {
c.MaxMsgsPerSubject = o.Settings.History
}
// The discard, and a lifetime per key or per message turned on; a held one is never turned off, which the bus refuses
// (design 47 H2 H-A-686); a bucket's key lifetime brings its limit marker, as the bucket is made with it.
c.Discard = jetstream.DiscardOld
if o.Settings.Discard == DiscardNew {
c.Discard = jetstream.DiscardNew
}
if o.Settings.KeyTTL {
c.AllowMsgTTL = true
if o.Kind == KindBucket && c.SubjectDeleteMarkerTTL == 0 {
c.SubjectDeleteMarkerTTL = limitMarker(true)
}
}
_, err = b.JS.UpdateStream(ctx, c)
return retryable(err)
}
+1 -1
View File
@@ -312,7 +312,7 @@ func (k *Keeper) capRoom(ctx context.Context, held *Asked, m *Made) error {
sum += o.Settings.MaxBytes
}
if sum > lim.AccountBytes {
return fmt.Errorf("the plan takes the sum of the bus's caps to %d MiB, past the account's storage limit of %d MiB", sum/MiB, lim.AccountBytes/MiB)
return fmt.Errorf("the plan takes the sum of the bus's caps to %d MiB, past the account's storage limit of %d MiB, short by %d MiB", sum/MiB, lim.AccountBytes/MiB, (sum-lim.AccountBytes+MiB-1)/MiB)
}
return nil
}
+71 -2
View File
@@ -110,6 +110,9 @@ func (b *fakeBus) Create(_ context.Context, o Object) error {
func (b *fakeBus) Update(_ context.Context, o Object) error {
b.mu.Lock()
defer b.mu.Unlock()
if err := b.refuse["update "+o.Stream()]; err != nil {
return err
}
b.held[o.Stream()] = o.Settings
b.updated = append(b.updated, o.Stream())
return nil
@@ -1356,8 +1359,8 @@ func TestTheKeeperMakesWhatAVerdictAsksForAndSaysIt(t *testing.T) {
raw, _ := json.Marshal(Request{ID: "r8", Verb: VerbRecreate, Module: "m", Local: "findings", Ask: "bus-8"})
_, _ = store.Put(StateRequests, "r8", raw)
k.ActOnRequests()
if fmt.Sprint(bus.deleted) != "[KV_m_findings]" || bus.held["OBJ_m_findings-objects"].MaxBytes != 8*MiB {
t.Errorf("deleted %v, store held %+v: the recreate rebuilds the bucket alone", bus.deleted, bus.held["OBJ_m_findings-objects"])
if fmt.Sprint(bus.updated) != "[KV_m_findings]" || len(bus.deleted) != 0 || bus.held["OBJ_m_findings-objects"].MaxBytes != 8*MiB {
t.Errorf("updated %v, deleted %v, store held %+v: the recreate changes the bucket alone, in place", bus.updated, bus.deleted, bus.held["OBJ_m_findings-objects"])
}
}},
{"a recreate rebuilds a drifted entry after a snapshot", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) {
@@ -1378,6 +1381,72 @@ func TestTheKeeperMakesWhatAVerdictAsksForAndSaysIt(t *testing.T) {
t.Errorf("held %+v, said %v %v: rebuilt as asked, the drift cleared", bus.held["m_log"], out.events, out.conds)
}
}},
// Design 47 H2 H-A-686, H-A-706 (DQ-42): a held lifetime matches an ask with none; a recreate changes each object in place
// and stops at the bus's refusal, empties nothing and says no recreated; short of room it changes nothing and takes no
// snapshot, the warrant spent.
{"a recreate changes a ledger in place, stops at a refused change, and stops short of room", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) {
if Differs(Settings{MaxBytes: 8, Discard: DiscardNew}, Settings{MaxBytes: 8, Discard: DiscardNew, KeyTTL: true}) != "" ||
!Grows(Settings{MaxBytes: 16, Discard: DiscardNew}, Settings{MaxBytes: 8, Discard: DiscardNew, KeyTTL: true}) ||
Differs(Settings{MaxBytes: 8, Discard: DiscardNew, KeyTTL: true}, Settings{MaxBytes: 8, Discard: DiscardNew}) == "" {
t.Fatal("a held lifetime does not match an ask with none, or an asked one matches a held none")
}
store := k.Store.(*memStore)
big, small := entry("records", catalogue.StorageLedger, catalogue.TemplateLedger, map[string]string{"max-mib": "16"}),
entry("records", catalogue.StorageLedger, catalogue.TemplateLedger, map[string]string{"max-mib": "8"})
big.Types = []catalogue.LedgerType{{Name: "decision", Indexes: []catalogue.LedgerIndex{{Name: "by-topic", On: []string{"topic"}}}}}
small.Types = big.Types
k.Heard(Accepted{Module: "m", Revision: 1, BusState: []catalogue.BusStateEntry{big}})
k.Heard(Accepted{Module: "m", Revision: 2, BusState: []catalogue.BusStateEntry{small}})
if k.Held()["m"].Made["records"].Drift == "" {
t.Fatal("a smaller cap is no drift")
}
objects := k.Held()["m"].Made["records"].Plan.Objects
recreate := func(id string) Request {
ask := oc.Ask{ID: "bus-" + id, Who: occ.Operator, Expires: occ.FormatTime(k.now().Add(time.Hour)), Options: []oc.Option{{ID: "go", Label: "Rebuild", Level: occ.Destroy,
Binds: func() string {
b, _ := occ.ActDigest(occ.Act{"verb": "recreate", "module": "m", "local": "records"})
return b
}()}}}
w := &oc.Warrant{Ask: ask.ID, Asker: Module, Outcome: occ.OutcomeChosen, By: &oc.Person{Who: occ.Operator},
Option: "go", Label: "Rebuild", Level: occ.Destroy, At: occ.FormatTime(k.now()), AskDigest: occ.Digest(ask)}
k.Warrant = func(string) (*oc.Ask, *oc.Warrant, error) { return &ask, w, nil }
raw, _ := json.Marshal(Request{ID: id, Verb: VerbRecreate, Module: "m", Local: "records", Ask: ask.ID})
_, _ = store.Put(StateRequests, id, raw)
k.ActOnRequests()
raw, _, _, _ = store.Get(StateRequests, id)
var got Request
_ = json.Unmarshal(raw, &got)
return got
}
// Short of room: the account holds less than the caps ask.
bus.limits = Limits{AccountBytes: 4 * MiB}
out.reset()
snaps := len(bus.snaps)
if got := recreate("r1"); !strings.Contains(got.Error, "short by") || len(bus.updated) != 0 || len(bus.deleted) != 0 || len(bus.snaps) != snaps ||
len(out.events) != 0 {
t.Fatalf("short of room: %q, updated %v, deleted %v, snapshots %d, said %v", got.Error, bus.updated, bus.deleted, len(bus.snaps)-snaps, out.events)
}
if _, _, spent, _ := store.Get(StateSpent, "bus-r1"); !spent {
t.Error("the warrant of a recreate short of room is not spent")
}
// A refused change in place stops it: the first object changed, the second refused, none emptied or changed after.
bus.limits = Limits{}
bus.refuse["update "+objects[1].Stream()] = errors.New("the bus refuses this change")
out.reset()
got := recreate("r2")
if !strings.Contains(got.Error, objects[1].Stream()) || len(bus.deleted) != 0 || fmt.Sprint(bus.updated) != "["+objects[0].Stream()+"]" ||
len(out.events) != 0 || k.Held()["m"].Made["records"].Drift == "" {
t.Fatalf("a refused change: %q, updated %v, deleted %v, said %v", got.Error, bus.updated, bus.deleted, out.events)
}
// Without the refusal, a new warrant changes every object in place, keeping what they keep, and says what it held.
delete(bus.refuse, "update "+objects[1].Stream())
out.reset()
bus.updated = nil
if got := recreate("r3"); got.Error != "" || len(bus.deleted) != 0 || len(bus.updated) != len(objects) ||
fmt.Sprint(out.events) != "[recreated m records]" || k.Held()["m"].Made["records"].Drift != "" {
t.Fatalf("the change in place: %q, updated %v, deleted %v, said %v", got.Error, bus.updated, bus.deleted, out.events)
}
}},
// Review round 1 (security F2; design 25 § 12.3): a warrant acts once, spent by compare-and-set before anything is
// deleted; a second request naming the same ask is refused and deletes nothing.
{"a warrant acts once", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) {
+11 -2
View File
@@ -440,12 +440,19 @@ func (o options) bool(name string) (bool, error) {
// settings grow by nothing and are no difference (Differs).
func Grows(asked, held Settings) bool {
if asked.Discard != held.Discard || asked.WorkQueue != held.WorkQueue || asked.MaxMsgSize != held.MaxMsgSize ||
asked.DuplicatesSeconds != held.DuplicatesSeconds || asked.KeyTTL != held.KeyTTL {
asked.DuplicatesSeconds != held.DuplicatesSeconds || !lifetimeMatches(asked, held) {
return false
}
return asked.MaxBytes >= held.MaxBytes && longer(asked.MaxAgeSeconds, held.MaxAgeSeconds) && asked.History >= held.History
}
// lifetimeMatches is whether a held lifetime per key (a bucket's) or per message (a stream's) matches the asked one: equal,
// or held while the ask gives none, since allowing a lifetime is no setting a write must use and the bus refuses to turn it
// off in place (design 47 H2 H-A-686 (1)).
func lifetimeMatches(asked, held Settings) bool {
return asked.KeyTTL == held.KeyTTL || held.KeyTTL && !asked.KeyTTL
}
// longer is whether age a keeps at least as long as b, 0 being for ever.
func longer(a, b int64) bool {
if a == 0 {
@@ -469,7 +476,9 @@ func Differs(asked, held Settings) string {
add("work-queue", asked.WorkQueue, held.WorkQueue)
add("max-msg-size", asked.MaxMsgSize, held.MaxMsgSize)
add("duplicates-seconds", asked.DuplicatesSeconds, held.DuplicatesSeconds)
add("key-ttl", asked.KeyTTL, held.KeyTTL)
if !lifetimeMatches(asked, held) {
add("key-ttl", asked.KeyTTL, held.KeyTTL)
}
return strings.Join(out, "; ")
}
+70 -14
View File
@@ -241,6 +241,29 @@ func (k *Keeper) act(r *Request) (string, error) {
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
// A recreate reads each object before anything is changed: whether the bus takes its asked settings in place (all but a
// retention that changes, which it refuses to change), and what it holds now (design 47 H2 H-A-686, H-A-634 (3)).
var steps []rebuild
if r.Verb == VerbRecreate {
for _, o := range m.Plan.Objects {
st, fill, found, err := k.Bus.Held(ctx, o)
if err != nil {
return "nothing done", fmt.Errorf("%s cannot be read, so nothing was changed: %w", o.Stream(), err)
}
steps = append(steps, rebuild{o: o, found: found, inPlace: found && st.WorkQueue == o.Settings.WorkQueue, before: fill})
}
// Short of room for the caps it asks, no object is changed and no snapshot taken; the warrant is spent (H-A-706, DQ-42).
if err := k.capRoom(ctx, &held, &m); err != nil {
if errors.Is(err, ErrRetry) {
return "nothing done", err
}
if serr := k.spend(r); serr != nil {
return "nothing done", serr
}
k.record(r, fmt.Sprintf("recreate of %s of %s refused: %v", r.Local, r.Module, err))
return "nothing done", fmt.Errorf("%s of %s was not rebuilt, no snapshot was taken and no object was changed: %w", r.Local, r.Module, err)
}
}
var streams []string
for _, o := range m.Plan.Objects {
streams = append(streams, o.Stream())
@@ -258,12 +281,12 @@ func (k *Keeper) act(r *Request) (string, error) {
if err := k.spend(r); err != nil {
return "nothing done", err
}
for _, o := range m.Plan.Objects {
if err := k.Bus.Delete(ctx, o); err != nil {
return "partly done", fmt.Errorf("%s could not be removed after its snapshot at %s: %w", o.Stream(), path, err)
}
}
if r.Verb == VerbDrop {
for _, o := range m.Plan.Objects {
if err := k.Bus.Delete(ctx, o); err != nil {
return "partly done", fmt.Errorf("%s could not be removed after its snapshot at %s: %w", o.Stream(), path, err)
}
}
delete(held.Made, r.Local)
k.write(held)
k.emit(EventDropped, m.Plan, held.Revision, r.Why, nil, nil)
@@ -271,13 +294,38 @@ func (k *Keeper) act(r *Request) (string, error) {
k.record(r, fmt.Sprintf("dropped %s of %s on the operator's warrant (snapshot %s)", r.Local, r.Module, path))
return fmt.Sprintf("dropped %s, its snapshot at %s", BusName(r.Module, r.Local), path), nil
}
for _, o := range m.Plan.Objects {
if err := k.Bus.Create(ctx, o); err != nil {
m.Created, m.Refused = false, fmt.Sprintf("%s: %v", o.Stream(), err)
held.Made[r.Local] = m
k.write(held)
return "partly done", fmt.Errorf("%s was removed and could not be made again (its snapshot at %s): %w", o.Stream(), path, err)
// Each object changed in place where the bus takes the asked settings, keeping what they keep; made again empty only one
// whose retention changes. A change the bus refuses stops it: no object made empty, none changed after, the refusal naming
// the object, no recreated, drifted left naming what differs (H-A-686 (2)).
var words []string
for i, st := range steps {
o := st.o
switch {
case st.inPlace:
if err := k.Bus.Update(ctx, o); err != nil {
return "partly done", fmt.Errorf("%s was refused its change in place: %w; the rebuild stopped there: no object was made empty, "+
"none after it was changed, the drift stays, and its snapshot is at %s (%d of %d objects were changed before it)", o.Stream(), err, path, i, len(steps))
}
default:
if st.found {
if err := k.Bus.Delete(ctx, o); err != nil {
return "partly done", fmt.Errorf("%s could not be removed after its snapshot at %s: %w", o.Stream(), path, err)
}
}
if err := k.Bus.Create(ctx, o); err != nil {
m.Created, m.Refused = false, fmt.Sprintf("%s: %v", o.Stream(), err)
held.Made[r.Local] = m
k.write(held)
return "partly done", fmt.Errorf("%s was removed and could not be made again (its snapshot at %s): %w", o.Stream(), path, err)
}
}
_, after, _, _ := k.Bus.Held(ctx, o)
how := "changed in place"
if !st.inPlace {
how = "made again empty"
}
words = append(words, fmt.Sprintf("%s %s: held %d messages, %d bytes before, %d messages, %d bytes after", o.Stream(), how,
st.before.Messages, st.before.Bytes, after.Messages, after.Bytes))
}
wasDrift := m.Drift != ""
m.Drift = ""
@@ -291,12 +339,20 @@ func (k *Keeper) act(r *Request) (string, error) {
m.Said = &said
held.Made[r.Local] = m
k.write(held)
k.emit(EventRecreated, m.Plan, held.Revision, r.Why, nil, nil)
k.emit(EventRecreated, m.Plan, held.Revision, r.Why+" ("+strings.Join(words, "; ")+")", nil, nil)
if wasDrift {
k.clear(conditionDrifted, r.Module, r.Local)
}
k.record(r, fmt.Sprintf("recreated %s of %s on the operator's warrant (snapshot %s)", r.Local, r.Module, path))
return fmt.Sprintf("recreated %s as asked, its old data's snapshot at %s", BusName(r.Module, r.Local), path), nil
k.record(r, fmt.Sprintf("recreated %s of %s on the operator's warrant (snapshot %s): %s", r.Local, r.Module, path, strings.Join(words, "; ")))
return fmt.Sprintf("recreated %s as asked, its old data's snapshot at %s: %s", BusName(r.Module, r.Local), path, strings.Join(words, "; ")), nil
}
// rebuild is one object of a recreate: whether it was found, whether the bus takes its asked settings in place, and what it
// held before.
type rebuild struct {
o Object
found, inPlace bool
before Fill
}
// warranted holds a recreate or a drop to the operator's warrant on the ask its tool raised: chosen by the operator,