B16 events-by-key (2): a retake lists no key; the set of keys failed on a transport fault is filled by the start sweep and kept by every write

Builds: design 47 H2 H-A-683 (2), H-A-696
This commit is contained in:
2026-11-25 17:31:36 +01:00
parent d77bff56c4
commit d9f733318c
5 changed files with 180 additions and 20 deletions
@@ -0,0 +1,113 @@
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)
}
@@ -104,6 +104,10 @@ func (r *Retaker) Heard(ctx context.Context, subject string, data []byte, seq ui
return true, nil
}
}
// An event heard before the start sweep has filled the set of faulted keys waits for it (H-A-696), held under its node's key.
if err := r.Builder.WaitFaults(ctx); err != nil {
return true, err
}
var want func(seat, node string) bool
if running {
want, err = r.runningWant(ctx, data)
@@ -135,7 +139,7 @@ func (r *Retaker) runningWant(ctx context.Context, data []byte) (func(seat, node
r.logf("a running that does not read is not acted on: %v", err)
return nil, nil
}
if ev.Stopped || ev.Fingerprint == "" {
if ev.Stopped || ev.Fingerprint == "" || r.Builder.Faults.Len() == 0 { // no key to retake: no call (H-A-683 (2))
return nil, nil
}
e, err := r.Entry(ctx, ev.Fingerprint)
@@ -167,25 +171,9 @@ func (r *Retaker) returnedWant(data []byte) (func(seat, node string) bool, error
return func(_, node string) bool { return node == ev.Node }, nil
}
// retakeAll builds again every key failed on a transport fault whose seat and node want says passed.
// retakeAll builds again every key of the set of faulted keys whose seat and node want says passed; it lists no key of the bucket.
func (r *Retaker) retakeAll(ctx context.Context, want func(seat, node string) bool) error {
keys, err := r.Builder.Claimer.Store.Keys(ctx)
if err != nil {
return fmt.Errorf("the build keys cannot be listed: %w", err)
}
for _, key := range keys {
raw, _, found, err := r.Builder.Claimer.Store.Get(ctx, key)
if err != nil || !found {
continue
}
v, err := builds.DecodeBuilderBuild(raw)
if err != nil || v.State != builds.BuildFailed {
continue
}
_, seat, node, _, ok := builds.TransportFaultOf(v.Error)
if !ok || !want(seat, node) {
continue
}
for _, key := range r.Builder.Faults.Matching(want) {
started, ask, cur, rev, err := r.Builder.Retake(ctx, key)
if err != nil {
return err
@@ -5,7 +5,9 @@ import (
"encoding/json"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
@@ -52,6 +54,9 @@ func TestRetakeOnAnEvent(t *testing.T) {
if v := r.consider(t, "modules/m"); !builds.FailedOnTransportFault(v.Error) {
t.Fatalf("the key = %+v, want failed on a transport fault", v)
}
if err := r.b.Sweep(context.Background()); err != nil { // the start sweep fills the set of faulted keys (H-A-696)
t.Fatal(err)
}
var asked []string
rt := &Retaker{Builder: r.b, Acted: &actedStore{},
Entry: func(context.Context, string) (meshcatalog.CatalogEntry, error) {
@@ -113,6 +118,9 @@ func TestRetakeOnANodeReturned(t *testing.T) {
r.consider(t, "modules/m")
seed(t, r.st, builds.BuilderBuild{State: builds.BuildFailed, By: "novox.builder", Manifest: mdigest('a'),
Error: "transport fault: connection, node-build-go ace, dial tcp: connection refused"})
if err := r.b.Sweep(context.Background()); err != nil {
t.Fatal(err)
}
rt := &Retaker{Builder: r.b, Acted: &actedStore{}}
for seq, c := range []struct {
node, why string
@@ -128,3 +136,45 @@ func TestRetakeOnANodeReturned(t *testing.T) {
}
}
}
// Design 47 H2 H-A-683 (2), H-A-696: a running heard before the start sweep has filled the set of faulted keys waits for it, then
// retakes the keys its node names; one heard after, with the set empty, reads no entry and starts nothing.
func TestRetakeWaitsForTheStartSweep(t *testing.T) {
r := newRig(t, goBundle)
r.consider(t, "modules/m")
seed(t, r.st, builds.BuilderBuild{State: builds.BuildFailed, By: "novox.builder", Manifest: mdigest('a'),
Error: "transport fault: connection, node-build-go ace, dial tcp: connection refused"})
var entries atomic.Int32
rt := &Retaker{Builder: r.b, Acted: &actedStore{}, Entry: func(context.Context, string) (meshcatalog.CatalogEntry, error) {
entries.Add(1)
return meshcatalog.CatalogEntry{Serves: []string{"node-build-go.steps"}}, nil
}}
running, _ := json.Marshal(nodeengine.Running{Module: "mesh-build-go", Node: "ace", Fingerprint: "f1"})
done := make(chan error, 1)
go func() {
_, err := rt.Heard(context.Background(), "mesh.seat.node-engine.event.running.ace.mesh-build-go", running, 1)
done <- err
}()
select {
case err := <-done:
t.Fatalf("a running heard before the sweep ended returned (%v), want it waiting", err)
case <-time.After(50 * time.Millisecond):
}
if err := r.b.Sweep(context.Background()); err != nil {
t.Fatal(err)
}
if err := <-done; err != nil {
t.Fatal(err)
}
r.b.Wait()
if v := valueOf(t, r.st, r.key); v.State != builds.BuildDone || entries.Load() != 1 {
t.Fatalf("after the sweep: the key = %+v, entries read %d, want built and one entry read", v, entries.Load())
}
// The set is empty now: another running reads no entry.
if _, err := rt.Heard(context.Background(), "mesh.seat.node-engine.event.running.ace.mesh-build-go", running, 2); err != nil {
t.Fatal(err)
}
if entries.Load() != 1 {
t.Fatalf("a running with the set empty read %d entries, want none more", entries.Load()-1)
}
}
@@ -54,6 +54,9 @@ func theOperatorAnswers(t *testing.T, option string) {
r.consider(t, "modules/m")
seed(t, r.st, builds.BuilderBuild{State: builds.BuildFailed, By: "novox.builder", Manifest: mdigest('a'), EventBuilds: 3,
Error: "transport fault: connection, node-build-go ace, dial tcp: connection refused"})
if err := r.b.Sweep(context.Background()); err != nil {
t.Fatal(err)
}
var published []oc.Ask
acted := &actedStore{}
asker := &OperatorAsk{Builder: r.b, Acted: acted, Ctx: context.Background(), Now: askedAt,
@@ -133,7 +133,9 @@ type Builder struct {
// its single reference, and its needs. Nil: no node states anything, and the first forms are kept.
Forms func() (present, counted map[string][]string)
once sync.Once
once sync.Once
// Faults are the keys whose newest revision failed on a transport fault (H-A-683 (2)), filled by Sweep and kept by every write.
Faults *Faulted
sem chan struct{}
wg sync.WaitGroup
mu sync.Mutex
@@ -194,6 +196,8 @@ func (b *Builder) Looking(key string) bool {
func (b *Builder) init() {
b.once.Do(func() {
b.Faults = newFaulted()
b.Claimer.Store = faultNoting{Store: b.Claimer.Store, set: b.Faults}
limit := b.Limit
if limit <= 0 {
limit = 256
@@ -510,6 +514,7 @@ func (b *Builder) Sweep(ctx context.Context, pictured ...string) error {
if err != nil || !found {
continue
}
b.Faults.note(key, value, rev)
cur, err := builds.DecodeBuilderBuild(value)
if err != nil {
continue
@@ -529,6 +534,7 @@ func (b *Builder) Sweep(ctx context.Context, pictured ...string) error {
}
b.logf("%s: closed, left building by %s for a fingerprint the module no longer has", key, cur.By)
}
b.logf("the start sweep read %d keys and put %d in the set of keys failed on a transport fault", len(keys), b.Faults.Fill())
if newly {
// A key read before the scan may wait for a package the scan found published (ADR 0500 clause 6).
b.Reconsider(ctx)