A process judges a running sum of what its writes put against the figures read at the bind and after each prune; a bundle judges the fill the runtime's put answer carries. Past prune-mark (70 %) the oldest keys by revision go down to prune-target (50 %), in one journal line; a key kept under _keep/<key> stays, and no reader counts the keep. The generic tools <module>_state_prune_now and <module>_state_keep act through the hand-act log. Builds: design 47 H2 §812, H-A-380, H-A-386
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package process
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// pruneKV is a fakeKV with a cap: its bytes are what its current values put (stdio.PutBytes), its Status says them
|
|
// with the cap, WatchAll hands every current value, and Purge removes a key.
|
|
type pruneKV struct {
|
|
*fakeKV
|
|
cap int64
|
|
statuss int
|
|
}
|
|
|
|
type pruneStatus struct {
|
|
jetstream.KeyValueStatus
|
|
bytes uint64
|
|
cap int64
|
|
}
|
|
|
|
func (s pruneStatus) Bytes() uint64 { return s.bytes }
|
|
func (s pruneStatus) Config() jetstream.KeyValueConfig {
|
|
return jetstream.KeyValueConfig{MaxBytes: s.cap}
|
|
}
|
|
|
|
func (f *pruneKV) held() uint64 {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
var n uint64
|
|
for k, v := range f.values {
|
|
n += stdio.PutBytes(k, v)
|
|
}
|
|
return n
|
|
}
|
|
|
|
func (f *pruneKV) Status(context.Context) (jetstream.KeyValueStatus, error) {
|
|
f.statuss++
|
|
return pruneStatus{bytes: f.held(), cap: f.cap}, nil
|
|
}
|
|
|
|
func (f *pruneKV) WatchAll(context.Context, ...jetstream.WatchOpt) (jetstream.KeyWatcher, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
w := &fakeWatcher{updates: make(chan jetstream.KeyValueEntry, len(f.values)+1)}
|
|
for k, v := range f.values {
|
|
w.updates <- putEntry{fakeEntry{key: k, value: v, rev: f.revs[k]}}
|
|
}
|
|
w.updates <- nil
|
|
return w, nil
|
|
}
|
|
|
|
func (f *pruneKV) Purge(ctx context.Context, key string, _ ...jetstream.KVDeleteOpt) error {
|
|
return f.Delete(ctx, key)
|
|
}
|
|
|
|
type putEntry struct{ fakeEntry }
|
|
|
|
func (putEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }
|
|
|
|
// Design 47 H2 §812 (H-A-380, H-A-386): a process's write that takes its state past prune-mark prunes it, oldest first
|
|
// by revision, to prune-target, in one journal line, reading the figures only at the bind and after the prune; a key
|
|
// kept under _keep/<key> survives, and no reader counts the keep.
|
|
func TestAProcessWritePastItsMarkPrunesToItsTargetAndAKeptKeySurvives(t *testing.T) {
|
|
var lines []string
|
|
sayPrune = func(line string) { lines = append(lines, line) }
|
|
t.Cleanup(func() { sayPrune = func(string) {} })
|
|
kv := &pruneKV{fakeKV: newFakeKV(), cap: 10_000}
|
|
s, err := stateOn(kv).Pruned(stdio.Prune{Mark: 70, Target: 50})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value := strings.Repeat("x", 300)
|
|
if _, err := s.Put("k00", value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Keep("k00", time.Now().Add(time.Hour)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Keep("k01", time.Now().Add(-time.Hour)); err == nil {
|
|
t.Fatal("a keep until a time past was taken")
|
|
}
|
|
for i := 1; i < 18; i++ {
|
|
if _, err := s.Put(fmt.Sprintf("k%02d", i), value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(lines) == 0 && kv.held()*100 > 70*10_000 {
|
|
t.Fatalf("the state holds %d of 10000 bytes after k%02d and was not pruned", kv.held(), i)
|
|
}
|
|
}
|
|
if len(lines) != 1 || !strings.Contains(lines[0], "pruned the state mesh-mail's sends") {
|
|
t.Fatalf("the prunes said %q, want one line", lines)
|
|
}
|
|
if kv.statuss != 2 {
|
|
t.Fatalf("the figures were read %d times, want 2: at the bind and after the prune", kv.statuss)
|
|
}
|
|
if held := kv.held(); held*100 > 50*10_000 {
|
|
t.Fatalf("the prune left %d of 10000 bytes, above its target of 50 %%", held)
|
|
}
|
|
keys, err := s.Keys()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if keys[0] != "k00" || strings.Contains(strings.Join(keys, " "), "_keep") || keys[1] == "k01" || keys[len(keys)-1] != "k17" {
|
|
t.Fatalf("after the prune the readers see %v: want k00 kept, the oldest others gone, k17 there, no keep", keys)
|
|
}
|
|
if e, _ := s.Get(stdio.KeepKey("k00")); e == nil {
|
|
t.Fatal("the keep itself was pruned")
|
|
}
|
|
}
|