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
138 lines
4.2 KiB
Go
138 lines
4.2 KiB
Go
package process
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// sayPrune writes a prune's one journal line (design 47 H2 §812).
|
|
var sayPrune = func(line string) { log.Print(line) }
|
|
|
|
// PrunedState is a state a process prunes at its own writes (design 47 H2 §812; H-A-380): the entry's figures are read
|
|
// once when it is bound and once after each prune, and between those a running sum of the bytes each write puts
|
|
// (stdio.PutBytes, an upper bound) is the judgement; past prune-mark of the cap the oldest keys by their current
|
|
// revision are purged down to prune-target, skipping a key kept under stdio.KeepKey. No read per write, no poll, no timer.
|
|
// Its reads are the state's own.
|
|
type PrunedState struct {
|
|
State
|
|
pruner *stdio.Pruner
|
|
}
|
|
|
|
// Pruned binds the state for a prune by p, reading its figures once.
|
|
func (s State) Pruned(p stdio.Prune) (*PrunedState, error) {
|
|
ps := &PrunedState{State: s}
|
|
ps.pruner = &stdio.Pruner{State: s.Module + "'s " + s.Name, Prune: p, Say: func(line string) { sayPrune(line) },
|
|
Figures: s.figures, Entries: s.entries, Remove: s.purge}
|
|
if err := ps.pruner.Bind(); err != nil {
|
|
return nil, err
|
|
}
|
|
return ps, nil
|
|
}
|
|
|
|
// figures is the bucket's bytes and its cap, zero for none.
|
|
func (s State) figures() (uint64, uint64, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), stateTimeout)
|
|
defer cancel()
|
|
kv, err := s.store(ctx)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
st, err := kv.Status(ctx)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
return st.Bytes(), uint64(max(st.Config().MaxBytes, 0)), nil
|
|
}
|
|
|
|
// entries is every current value of the bucket with its revision, keeps included, read once.
|
|
func (s State) entries() ([]stdio.PruneEntry, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), stateTimeout)
|
|
defer cancel()
|
|
kv, err := s.store(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
w, err := kv.WatchAll(ctx, jetstream.IgnoreDeletes())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer w.Stop()
|
|
var out []stdio.PruneEntry
|
|
for {
|
|
select {
|
|
case e, open := <-w.Updates():
|
|
if !open {
|
|
return nil, errors.New("the read of the current values ended")
|
|
}
|
|
if e == nil {
|
|
return out, nil
|
|
}
|
|
if e.Operation() == jetstream.KeyValuePut {
|
|
out = append(out, stdio.PruneEntry{Key: e.Key(), Revision: e.Revision(), Bytes: stdio.PutBytes(e.Key(), e.Value()), Value: e.Value()})
|
|
}
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
}
|
|
}
|
|
|
|
// purge removes a key with its past values, so its bytes leave the bucket.
|
|
func (s State) purge(key string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), stateTimeout)
|
|
defer cancel()
|
|
kv, err := s.store(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return kv.Purge(ctx, key)
|
|
}
|
|
|
|
// wrote counts a write and prunes past the mark.
|
|
func (s *PrunedState) wrote(key string, value any, rev uint64, err error) (uint64, error) {
|
|
if err != nil {
|
|
return rev, err
|
|
}
|
|
data, _ := json.Marshal(value)
|
|
if _, err := s.pruner.Wrote(stdio.PutBytes(key, data)); err != nil {
|
|
return rev, err
|
|
}
|
|
return rev, nil
|
|
}
|
|
|
|
// Put writes a key whole, counted toward the prune.
|
|
func (s *PrunedState) Put(key string, value any) (uint64, error) {
|
|
rev, err := s.State.Put(key, value)
|
|
return s.wrote(key, value, rev, err)
|
|
}
|
|
|
|
// Create writes a key only when it has none, counted toward the prune.
|
|
func (s *PrunedState) Create(key string, value any) (uint64, error) {
|
|
rev, err := s.State.Create(key, value)
|
|
return s.wrote(key, value, rev, err)
|
|
}
|
|
|
|
// Update writes a key only when it is still at revision, counted toward the prune.
|
|
func (s *PrunedState) Update(key string, value any, revision uint64) (uint64, error) {
|
|
rev, err := s.State.Update(key, value, revision)
|
|
return s.wrote(key, value, rev, err)
|
|
}
|
|
|
|
// PruneNow prunes the state to its target now.
|
|
func (s *PrunedState) PruneNow() (stdio.Pruned, error) { return s.pruner.PruneNow() }
|
|
|
|
// Keep keeps key from the prune until until, written under stdio.KeepKey.
|
|
func (s *PrunedState) Keep(key string, until time.Time) error {
|
|
if err := stdio.KeepProblem(key, until); err != nil {
|
|
return err
|
|
}
|
|
_, err := s.Put(stdio.KeepKey(key), stdio.Keep{Until: until.UTC()})
|
|
return err
|
|
}
|