Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/fill.go
T
jschoubben 03ee0930e1 RN17 (unlogged): a step's end and its log entry are one write, appended after the end is said
The pipeline gains `unlogged`: a step's end is written in its key with its step-log entry in one compare-and-set, said as
step-ended, then appended to the step log and cleared; a log that refuses an append ends no step, says full and starts no new
pipeline; at its start the runner appends every unlogged entry in order after reading back the log's last entry of that
pipeline, and once a minute while the log is full. The tools runner_unlogged and runner_unlogged_append (a hand act).

Builds: design 47 G §486, §606, §549
Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 06:03:08 +01:00

135 lines
3.8 KiB
Go

package pipeline
import (
"errors"
"fmt"
"sync"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// The fill at which a store says `filling` (80 %, past it) and `room` (70 %, below it) on the seat (design 47 G §366, §181).
const (
FillingPercent = 80
RoomPercent = 70
)
// ErrFull is a start refused because a store of the runner is full (§366): said, and heard again by the bus.
var ErrFull = errors.New("a store of the runner is full")
// Fills says each of the runner's stores' fill before it is full (design 47 G §366): `filling` past 80 % of the cap, `full` at
// the cap, and `room` below 70 % once it said either; a store full refuses a new pipeline, loudly, and nothing is dropped to make room.
type Fills struct {
// Read is each store's fill now: pipelines, step-outputs, kept and step-log.
Read func() ([]seat.Fill, error)
// Say says one of the seat's events, filling, full or room, with the store's fill.
Say func(event string, f seat.Fill) error
Logf func(string, ...any)
mu sync.Mutex
said map[string]string // the store's last event said, "" before the first check
full map[string]bool
}
// Check reads each store's fill and says what changed since the last check; it is read at each step's end and at each start.
func (f *Fills) Check() {
if f == nil || f.Read == nil {
return
}
fills, err := f.Read()
if err != nil {
f.logf("the stores' fill is not read: %v", err)
}
f.mu.Lock()
defer f.mu.Unlock()
if f.said == nil {
f.said, f.full = map[string]string{}, map[string]bool{}
}
for _, s := range fills {
if s.Cap <= 0 {
continue
}
event := ""
switch last := f.said[s.Store]; {
case s.Bytes >= s.Cap:
event = seat.EventFull
case s.Bytes*100 > s.Cap*FillingPercent:
if last != seat.EventFilling && last != seat.EventFull {
event = seat.EventFilling
}
case s.Bytes*100 < s.Cap*RoomPercent:
// A store below 70 % says room once it said otherwise, and at the first check after a start: a keeper holding a
// finding of the store from before is told it cleared.
if last != seat.EventRoom {
event = seat.EventRoom
}
}
f.full[s.Store] = s.Bytes >= s.Cap
if event == "" || event == f.said[s.Store] {
continue
}
if err := f.Say(event, s); err != nil {
f.logf("%s of %s is not said: %v", event, s.Store, err)
continue
}
f.said[s.Store] = event
}
}
// MarkFull says a store full because the bus refused a write to it (the step log, §486): it refuses starts until a check reads it
// below its cap.
func (f *Fills) MarkFull(store string) {
if f == nil {
return
}
f.mu.Lock()
if f.said == nil {
f.said, f.full = map[string]string{}, map[string]bool{}
}
f.full[store] = true
first := f.said[store] != seat.EventFull
f.said[store] = seat.EventFull
f.mu.Unlock()
if first && f.Say != nil {
if err := f.Say(seat.EventFull, seat.Fill{Store: store}); err != nil {
f.logf("full of %s is not said: %v", store, err)
}
}
}
// Full is the stores full now, as the last check read them.
func (f *Fills) Full() []string {
if f == nil {
return nil
}
f.mu.Lock()
defer f.mu.Unlock()
var out []string
for s, full := range f.full {
if full {
out = append(out, s)
}
}
return out
}
func (f *Fills) logf(format string, args ...any) {
if f != nil && f.Logf != nil {
f.Logf(format, args...)
}
}
// refuseStart is the error of a start while a store is full, naming each store; it is said in the journal, and the start is heard
// again by the bus, so the runner takes it up on room (§366).
func (f *Fills) refuseStart(key string) error {
if f == nil {
return nil
}
f.Check()
full := f.Full()
if len(full) == 0 {
return nil
}
f.logf("the start of %s is refused: %v full", key, full)
return fmt.Errorf("%w: %v; %s starts on room", ErrFull, full, key)
}