Files
mesh-delivery/modules/mesh-delivery-builder/internal/build/faulted.go
T

114 lines
3.1 KiB
Go

package build
import (
"context"
"sort"
"sync"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
)
// Faulted is the set of build keys whose newest revision failed on a transport fault, with the seat and node the fault names
// (design 47 H2 H-A-683 (2), H-A-696): filled by the start sweep (Builder.Sweep) and kept by every write of a build (the store
// Builder.init wraps), held in memory so that an event retakes only the keys whose seat or node it names, and lists no key of the
// bucket. An event heard before the sweep has filled the set waits (WaitFilled) until it has.
type Faulted struct {
mu sync.Mutex
revs map[string]uint64 // the newest revision noted of each key, so that an older read never replaces a newer write
faults map[string]fault
filled chan struct{}
once sync.Once
}
type fault struct{ seat, node string }
func newFaulted() *Faulted {
return &Faulted{revs: map[string]uint64{}, faults: map[string]fault{}, filled: make(chan struct{})}
}
// note keeps what key holds at revision rev: in the set when it failed on a transport fault, else out of it.
func (f *Faulted) note(key string, value []byte, rev uint64) {
var fl fault
in := false
if v, err := builds.DecodeBuilderBuild(value); err == nil && v.State == builds.BuildFailed {
_, seat, node, _, ok := builds.TransportFaultOf(v.Error)
fl, in = fault{seat, node}, ok
}
f.mu.Lock()
defer f.mu.Unlock()
if rev < f.revs[key] {
return
}
f.revs[key] = rev
if in {
f.faults[key] = fl
} else {
delete(f.faults, key)
}
}
// Fill says the start sweep has read every key, and returns how many keys the set holds.
func (f *Faulted) Fill() int {
f.once.Do(func() { close(f.filled) })
return f.Len()
}
// Len is how many keys the set holds.
func (f *Faulted) Len() int {
f.mu.Lock()
defer f.mu.Unlock()
return len(f.faults)
}
// WaitFilled returns once the start sweep has filled the set, or ctx ends.
func (f *Faulted) WaitFilled(ctx context.Context) error {
select {
case <-f.filled:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// Matching are the keys of the set whose seat and node want says passed, in order.
func (f *Faulted) Matching(want func(seat, node string) bool) []string {
f.mu.Lock()
defer f.mu.Unlock()
var out []string
for k, fl := range f.faults {
if want(fl.seat, fl.node) {
out = append(out, k)
}
}
sort.Strings(out)
return out
}
// faultNoting is the builds store that notes every write in the set.
type faultNoting struct {
Store
set *Faulted
}
func (s faultNoting) Create(ctx context.Context, key string, value []byte) (uint64, error) {
rev, err := s.Store.Create(ctx, key, value)
if err == nil {
s.set.note(key, value, rev)
}
return rev, err
}
func (s faultNoting) Update(ctx context.Context, key string, value []byte, revision uint64) (uint64, error) {
rev, err := s.Store.Update(ctx, key, value, revision)
if err == nil {
s.set.note(key, value, rev)
}
return rev, err
}
// WaitFaults returns once the start sweep has filled the set of faulted keys, or ctx ends.
func (b *Builder) WaitFaults(ctx context.Context) error {
b.init()
return b.Faults.WaitFilled(ctx)
}