package stdio import ( "fmt" "git.novox.be/novox/mesh-sdk/go/busretry" "sort" "sync" "time" ) // WatchedStates is a copy of the states a module reads, each filled by its own watch and kept by its changes (novox/hq // ADR 0580 clause 1): a read of a whole state is one watch's current values, never a get per key, which took a // composition past 30 seconds and a compare past its call bound. A state is watched on its first read, or on Follow, // and the watch hands every current value over before the read is answered, so the copy is complete from the first // answer. A key whose value never changes once written (a manifest, by its digest) is read once with Once and kept. // A state helper, no rule: what a module reads is its own. type WatchedStates struct { watch func(state string, handle func(StateChange) error) error get func(state, key string) ([]byte, bool, error) mu sync.Mutex states map[string]*watchedState // once holds each value read with Once, by state and key, for the process's life: a few kilobytes per manifest // digest. Past onceAtMost keys it is emptied, which costs only a read again. once map[string][]byte // born is when the copy was made: a state not yet asked for is unread since then (Unread). born time.Time } // onceAtMost bounds the values Once keeps. const onceAtMost = 4096 type watchedState struct { asked time.Time // when its watch began (Unread) ready chan struct{} // closed once the watch's current values are all in the copy, or it was refused err error mu sync.Mutex values map[string][]byte // revs is, by key, the revision its value in the copy is at, and latest the highest revision the copy has seen: a // change the watch hands at a revision not newer than its key's is one the copy already took (Wrote, Removed). revs map[string]uint64 latest uint64 then []func(StateChange) error started bool // the current values are in, so a change runs then at once pending []StateChange // the current values, run through then once all are in thenMu sync.Mutex // then runs one change at a time } // NewWatchedStates is the copy over this bundle's runtime: each state by KeptState.Watch, Once by KeptState.Get. func NewWatchedStates() *WatchedStates { return WatchedStatesOver( func(state string, handle func(StateChange) error) error { return State(state).Watch("", handle) }, func(state, key string) ([]byte, bool, error) { e, err := State(state).Get(key) if err != nil || e == nil { return nil, false, err } return e.Value, true, nil }) } // WatchedStatesOver is the copy over a watch and a get of one's own (a process's bus, a test's): watch hands handle // every current value and returns once they are handled, then hands every change; get reads one key. func WatchedStatesOver(watch func(state string, handle func(StateChange) error) error, get func(state, key string) ([]byte, bool, error)) *WatchedStates { return &WatchedStates{watch: watch, get: get, states: map[string]*watchedState{}, once: map[string][]byte{}, born: time.Now()} } // watched is the state's copy, its watch started now when it has none, the caller waiting until its current values // are in. then, when given, runs after the copy took each change: the current values once they are all in (a then // that reads the copy reads it whole), each later change as it comes. No lock is held while the watch starts, so a // then may read any state of the copy. func (w *WatchedStates) watched(state string, then func(StateChange) error) (*watchedState, error) { w.mu.Lock() s, ok := w.states[state] if ok { w.mu.Unlock() <-s.ready if s.err != nil { return nil, s.err } if then != nil { s.join(state, then) } return s, nil } s = &watchedState{asked: time.Now(), ready: make(chan struct{}), values: map[string][]byte{}, revs: map[string]uint64{}} if then != nil { s.then = []func(StateChange) error{then} } w.states[state] = s w.mu.Unlock() err := w.watch(state, func(c StateChange) error { s.mu.Lock() switch { case c.Revision != 0 && c.Revision <= s.revs[c.Key]: // Already in the copy, or older than what is: this process wrote the key since (Wrote, Removed). case c.Op == OpPut: s.values[c.Key] = append([]byte(nil), c.Value...) s.revs[c.Key] = c.Revision default: delete(s.values, c.Key) s.revs[c.Key] = c.Revision } s.latest = max(s.latest, c.Revision) if !s.started { s.pending = append(s.pending, c) s.mu.Unlock() return nil } s.mu.Unlock() return s.run(state, c) }) if err != nil { s.err = fmt.Errorf("the state %s is not watched: %w", state, err) w.mu.Lock() delete(w.states, state) // asked again on the next read w.mu.Unlock() close(s.ready) return nil, s.err } s.mu.Lock() s.started = true pending := s.pending s.pending = nil s.mu.Unlock() close(s.ready) for _, c := range pending { if err := s.run(state, c); err != nil { sayf("%v", err) } } return s, nil } // Unread says whether the copy of state has not taken in its current values yet: the fault `away` (busretry.Away, design // 47 H2 §776) with `part` naming the state and `words` "not read yet since