Files
mesh-delivery/modules/mesh-grants/internal/grants/purge.go
T
jschoubben 126b40e6f5
mesh/delivery merged: source.main moved to novox/mesh-delivery@4f9b15a43f05
mesh/check checking 126b40e6f585 on 4f9b15a43f05
Grants purges, once at its start, the sealed keys a delete left behind (issue 1966)
The markers and the values of the lists taken away before the purge still fill
the state user-list. Before its first pass the process purges every
sealed-grants key whose last operation is a delete or purge marker, and every
user's key of a list the index of kept lists does not name; never a kept
list's key nor another key of the state, and nothing when the index cannot be
read. It logs the counts and the state's bytes before and after.

Claude-Session: https://claude.ai/code/session_01NcbgfFfQ4Z9UE5jK32PmE5
2026-11-09 00:22:00 +01:00

115 lines
4.7 KiB
Go

package grants
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
// Purger takes keys of the part's own state away whole (issue 1966, 2026-11-08): a key-value delete only adds a marker
// and leaves every value the key kept in the bucket's stream, so the per-user keys of the sealed lists older than the
// two kept filled the state user-list to 94% of its cap (5,968 keys, 13,228 values kept, 23:20 UTC). A purge takes the
// key's values with it, leaving one marker. The process's own connection; nil in the tools, where a key taken away is deleted.
type Purger interface {
// Purge takes key of state away with every value it kept.
Purge(state, key string) error
// LastOps is every key of state that pattern names (one key, or a prefix ending `.>`), each with whether its last
// operation is a delete or purge marker: the keys a key listing leaves out.
LastOps(state, pattern string) (map[string]bool, error)
// Bytes is how many bytes the state holds on the bus, values and markers.
Bytes(state string) (uint64, error)
}
// takeAway takes a key of the part's own state away: purged where the part holds a purger, else deleted.
func (p *Part) takeAway(state, key string) error {
if p.Purging != nil {
return p.Purging.Purge(state, key)
}
return p.Store.Delete(state, key)
}
// sealedPrefix is the prefix of every user's key of the sealed list of digest.
func sealedPrefix(digest string) string { return SealedUserKey(digest, "") }
// SealedSweep is what SweepSealed took away of the state user-list.
type SealedSweep struct {
// Marked are the sealed-grants keys whose last operation was a delete or purge marker, purged; Orphaned the live keys
// of a list the index of kept lists does not name, purged.
Marked int `json:"marked"`
Orphaned int `json:"orphaned"`
// Kept is the digests of the kept lists, whose keys the sweep never touches.
Kept []string `json:"kept"`
// NoIndex says the state holds no index of kept lists: then only the marked keys are purged.
NoIndex bool `json:"no_index,omitempty"`
BytesBefore uint64 `json:"bytes_before"`
BytesAfter uint64 `json:"bytes_after"`
Errors []string `json:"errors,omitempty"`
}
// SweepSealed purges, once, the keys of the sealed lists that a delete left in the state user-list (issue 1966): every
// sealed-grants key whose last operation is a delete or purge marker, and every user's key of a list the index of kept
// lists does not name. Never a key of a kept list, and never another key of the state. Run at the process's start,
// before its first pass, so no pass writes a list's keys beside it. Refused whole when the index cannot be read.
func (p *Part) SweepSealed() (SealedSweep, error) {
var out SealedSweep
if p.Purging == nil {
return out, errors.New("the part holds no purger: nothing is taken away")
}
raw, _, found, err := p.Store.Get(StateUserList, KeySealedLists)
if err != nil {
return out, fmt.Errorf("the index of kept lists cannot be read, so which keys are kept is not known: %w", err)
}
var idx SealedLists
if found {
if err := json.Unmarshal(raw, &idx); err != nil {
return out, fmt.Errorf("the index of kept lists cannot be read, so which keys are kept is not known: %w", err)
}
}
out.NoIndex = !found
var kept []string
for _, l := range idx.Lists {
out.Kept = append(out.Kept, l.Digest)
kept = append(kept, sealedPrefix(l.Digest))
}
if out.BytesBefore, err = p.Purging.Bytes(StateUserList); err != nil {
return out, fmt.Errorf("the state %s cannot be read: %w", StateUserList, err)
}
isKept := func(key string) bool {
for _, pre := range kept {
if strings.HasPrefix(key, pre) {
return true
}
}
return false
}
purge := func(key string, n *int) {
if err := p.Purging.Purge(StateUserList, key); err != nil {
out.Errors = append(out.Errors, fmt.Sprintf("its own %s %s cannot be purged: %v", StateUserList, key, err))
return
}
*n++
}
// The one value written before the index (KeySealedGrants), once deleted, and every user's key.
for _, pattern := range []string{KeySealedGrants, KeySealedGrants + ".>"} {
ops, err := p.Purging.LastOps(StateUserList, pattern)
if err != nil {
return out, fmt.Errorf("the keys %s of %s cannot be listed: %w", pattern, StateUserList, err)
}
for _, key := range sortedKeys(ops) {
switch {
case key != KeySealedGrants && !strings.HasPrefix(key, KeySealedGrants+"."):
case key != KeySealedGrants && isKept(key):
case ops[key]:
purge(key, &out.Marked)
case key != KeySealedGrants && found:
purge(key, &out.Orphaned)
}
}
}
if out.BytesAfter, err = p.Purging.Bytes(StateUserList); err != nil {
out.Errors = append(out.Errors, fmt.Sprintf("the state %s cannot be read after the sweep: %v", StateUserList, err))
}
return out, nil
}