Design 47 *Each part acts on its own entries* and *Every part takes a first view, then follows the events*: the builder reads its placements through the seat mesh-wants' view, never the assigner's state. The seat's client gets Placed over assignment graph revisions (ADR 0625 §4: the logic the gate's three callers share), and the gate a Revision feed; the first revision settles it, a pair new in a later one stands since that revision's time, as the state's write time did (issue 1940). Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ Builds: design 47 G §348, §195, §200
110 lines
3.5 KiB
Go
110 lines
3.5 KiB
Go
package meshwantsclient
|
|
|
|
import (
|
|
"maps"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshwants"
|
|
)
|
|
|
|
// Placed is where the assignment half (the seat mesh-wants' assignment graph revisions; design 47 *The revision view*)
|
|
// places each module, and since when: the one reading of "does module stand on node" for every part that takes its view
|
|
// of the assignment half (the builder's, the indexer's and the checker's toolchain gate), so no part reads the assigner's
|
|
// state for it (design 47 *Each part acts on its own entries*; ADR 0596). Its input is the revisions a part's view holds,
|
|
// in the order it holds them (assignmentview's Held): Take takes each whole, from its entries' nodes and modules alone, so
|
|
// no entry is asked. A pair stands since the revision that first named it in an unbroken run: in the first revision taken
|
|
// since the zero time (the module may have run there before), in a later one since that revision's time (its `at`; the
|
|
// zero time when it carries none). A revision not newer than the one taken moves nothing.
|
|
//
|
|
// Safe for use from a view's goroutine and a reader at once.
|
|
type Placed struct {
|
|
mu sync.Mutex
|
|
since map[string]map[string]time.Time // node -> module -> since when it stands
|
|
revision int64 // the revision taken last, 0 before the first
|
|
}
|
|
|
|
// NewPlaced is a Placed that holds no revision yet.
|
|
func NewPlaced() *Placed { return &Placed{since: map[string]map[string]time.Time{}} }
|
|
|
|
// Take takes one assignment graph revision in and answers, by node, the modules whose standing on it changed (placed or
|
|
// removed), each in name order; nothing for a revision not newer than the one taken.
|
|
func (p *Placed) Take(r seat.AssignmentRevision) map[string][]string {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
if r.Revision <= p.revision {
|
|
return nil
|
|
}
|
|
at := time.Time{}
|
|
if p.revision > 0 && r.At != nil {
|
|
at = *r.At
|
|
}
|
|
p.revision = r.Revision
|
|
now := map[string]map[string]bool{}
|
|
for _, e := range r.Entries {
|
|
if now[e.Node] == nil {
|
|
now[e.Node] = map[string]bool{}
|
|
}
|
|
now[e.Node][e.Module] = true
|
|
}
|
|
changed := map[string][]string{}
|
|
next := map[string]map[string]time.Time{}
|
|
for node, modules := range now {
|
|
next[node] = map[string]time.Time{}
|
|
for m := range modules {
|
|
if s, ok := p.since[node][m]; ok {
|
|
next[node][m] = s
|
|
continue
|
|
}
|
|
next[node][m] = at
|
|
changed[node] = append(changed[node], m)
|
|
}
|
|
}
|
|
for node, modules := range p.since {
|
|
for m := range modules {
|
|
if !now[node][m] {
|
|
changed[node] = append(changed[node], m)
|
|
}
|
|
}
|
|
}
|
|
for node := range changed {
|
|
slices.Sort(changed[node])
|
|
}
|
|
p.since = next
|
|
return changed
|
|
}
|
|
|
|
// Revision is the assignment graph revision taken last; 0 before the first.
|
|
func (p *Placed) Revision() int64 {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
return p.revision
|
|
}
|
|
|
|
// Seeded says the revision taken places some module somewhere.
|
|
func (p *Placed) Seeded() bool {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
return len(p.since) > 0
|
|
}
|
|
|
|
// Since says whether module stands on node, and since when (the zero time: since before the first revision taken).
|
|
func (p *Placed) Since(node, module string) (time.Time, bool) {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
s, ok := p.since[node][module]
|
|
return s, ok
|
|
}
|
|
|
|
// ByNode is, by node, the modules placed on it in name order.
|
|
func (p *Placed) ByNode() map[string][]string {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
out := make(map[string][]string, len(p.since))
|
|
for node, mods := range p.since {
|
|
out[node] = slices.Sorted(maps.Keys(mods))
|
|
}
|
|
return out
|
|
}
|