The assigner orders a want's writes by the bus revision; run is information (design 47 H2 §813 (1), H-A-504 third addendum) #347

Merged
mesh-tools merged 1 commits from fix/want-order-by-revision into main 2026-11-23 00:51:19 +00:00
4 changed files with 82 additions and 47 deletions
@@ -96,9 +96,10 @@ func TestTheWantsAreSaidWantedFromTheKeptAssignmentGraphRevision(t *testing.T) {
if err := json.Unmarshal(events[len(events)-1].body, &w); err != nil || len(events) != 3 || !w.Removed || w.Fp != fpTest {
t.Errorf("a removing want said %+v (%v), %d events", w, err, len(events))
}
// Design 47 H2 §813 (1), H-A-504 corrected and its addendum: a want of a pipeline carries its run, the pipeline's sequence the
// want rule set; a want of no pipeline the newest source held. It is written by compare-and-set refusing a lower run, and
// one that differs only by its run stands.
// Design 47 H2 §813 (1), H-A-504 corrected, its addendum and its third addendum: a want of a pipeline carries its run, the
// pipeline's sequence the want rule set, as information; a want of no pipeline the newest source held. The order is the bus's:
// a want is written by compare-and-set on the key's revision the pass read, whatever its run, and one that differs only by its
// run stands; a pass whose read the key has moved past writes nothing, says so once, and the next pass writes it.
t.Run("a want's run", func(t *testing.T) {
wkey, _ := mwc.WantKey("g14", "db")
stored := func() mwc.Want { w, _ := mwc.DecodeWant(kv.values[wkey]); return w }
@@ -106,37 +107,51 @@ func TestTheWantsAreSaidWantedFromTheKeptAssignmentGraphRevision(t *testing.T) {
if w := stored(); w.Run != "212" || w.Pipeline != "main.12" {
t.Fatalf("the want of pipeline main.12 is %+v, want run 212", w)
}
p.putWant(ctx, "g14", "db", mwc.Want{Fp: fpTest, Version: "0.1.1", Pipeline: "main.11", Rank: 1, Run: "211"})
if w := stored(); w.Run != "211" || w.Version != "0.1.1" {
t.Errorf("a want of a lower run left %+v, want it written: the run is no gate", w)
}
puts := kv.puts
p.putWant(ctx, "g14", "db", mwc.Want{Fp: fpTest, Version: "0.1.1", Pipeline: "main.11", Rank: 1, Run: "211"})
if w := stored(); w.Run != "212" || w.Version != "0.1.2" || kv.puts != puts {
t.Errorf("a want of a lower run replaced %+v (%d writes)", w, kv.puts-puts)
}
p.putWant(ctx, "g14", "db", mwc.Want{Fp: fpTest, Version: "0.1.2", Pipeline: "main.12", Rank: 1, Run: "212"})
if kv.puts != puts {
t.Error("a want that stands was written again")
}
p.putWant(ctx, "g14", "db", mwc.Want{Fp: fpTest, Version: "0.1.2", Pipeline: "main.12", Rank: 1, Run: "212"})
skey, _ := mwc.WantKey("shanks", "db")
p.putWant(ctx, "shanks", "db", mwc.Want{Fp: fpTest, Put: true, Words: "put: by hand"})
if w, _ := mwc.DecodeWant(kv.values[skey]); w.Run != "9" {
t.Errorf("a put's want is %+v, want the newest source held, 9", w)
}
// a placement's build after the delivery of run 212: the newest source held for it is the want's own, never refused
// a placement's build after the delivery of run 212: the newest source held for it is the want's own
p.putWant(ctx, "g14", "db", mwc.Want{Fp: fpTest, Version: "0.1.0"})
if w := stored(); w.Run != "212" || w.Version != "0.1.0" {
t.Errorf("a placement's want after run 212 is %+v, want it written at run 212", w)
}
// A want written with an again run's number ("2") is replaced by any sequence; a lower sequence is refused.
// Wants written before the sequence carry a revision number (322) as their run: a want of a lower sequence is written.
nkey, _ := mwc.WantKey("novox", "node-tools")
atNovox := func() mwc.Want { w, _ := mwc.DecodeWant(kv.values[nkey]); return w }
p.putWant(ctx, "novox", "node-tools", mwc.Want{Fp: fpTest, Version: "0.1.96", Pipeline: "main.265.again.2", Rank: 1, Run: "2"})
p.putWant(ctx, "novox", "node-tools", mwc.Want{Fp: fpTest, Version: "0.1.115", Pipeline: "main.320.again.1", Rank: 1, Run: "270"})
if w := atNovox(); w.Run != "270" || w.Version != "0.1.115" {
t.Errorf("seq 270 after run 2 left %+v, want 0.1.115 at run 270", w)
p.putWant(ctx, "novox", "node-tools", mwc.Want{Fp: fpTest, Version: "0.1.96", Pipeline: "main.265.again.2", Rank: 1, Run: "322"})
p.putWant(ctx, "novox", "node-tools", mwc.Want{Fp: fpTest, Version: "0.1.115", Pipeline: "main.320.again.1", Rank: 1, Run: "312"})
if w := atNovox(); w.Run != "312" || w.Version != "0.1.115" {
t.Errorf("seq 312 after run 322 left %+v, want 0.1.115 at run 312", w)
}
// A stale pass: another write moved the key past the revision this pass read; it loses, says so once, writes nothing.
var lines []string
p.logf = func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) }
defer func() { p.logf = t.Logf }()
newer, _ := mwc.EncodeWant(mwc.Want{Fp: fpTest, Version: "0.1.116", Pipeline: "main.321", Rank: 1, Run: "313"})
if _, err := kv.Update(ctx, nkey, newer, kv.revision(nkey)); err != nil {
t.Fatal(err)
}
puts = kv.puts
p.putWant(ctx, "novox", "node-tools", mwc.Want{Fp: fpTest, Version: "0.1.114", Pipeline: "main.319", Rank: 1, Run: "269"})
if w := atNovox(); w.Run != "270" || kv.puts != puts {
t.Errorf("seq 269 after 270 replaced %+v (%d writes)", w, kv.puts-puts)
p.putWant(ctx, "novox", "node-tools", mwc.Want{Fp: fpTest, Version: "0.1.114", Pipeline: "main.319", Rank: 1, Run: "311"})
if w := atNovox(); w.Version != "0.1.116" || kv.puts != puts || len(lines) != 1 || !strings.Contains(lines[0], "the newer write stands") {
t.Errorf("a stale pass left %+v (%d writes), said %q", w, kv.puts-puts, lines)
}
p.w.putEntry(buckets.Wants(), nkey, newer, kv.revision(nkey), at) // the watch hands the newer write
p.putWant(ctx, "novox", "node-tools", mwc.Want{Fp: fpTest, Version: "0.1.117", Pipeline: "main.322", Rank: 1, Run: "314"})
if w := atNovox(); w.Version != "0.1.117" || len(lines) != 1 {
t.Errorf("the next pass left %+v, said %q", w, lines)
}
})
@@ -348,12 +348,16 @@ func runNumber(run string) int64 {
return n
}
// wantWritten writes the want of module on node under key by compare-and-set, carrying its run (design 47 H2 §813 (1)): a want
// that differs from the one written only by its run is not written again (its run stays, so a node never composes again for
// it), and one whose run is lower than the run written is refused, said in the journal. It answers whether it wrote.
// wantWritten writes the want of module on node under key, carrying its run as information (design 47 H2 §813 (1), H-A-504
// third addendum): the order is the bus's own, a compare-and-set on the key's revision this pass read, so a stale pass loses to
// the newer write, said once in the journal, and the next pass judges the key again from what it then reads. The run is the
// pipeline's sequence for the why and the tools, never a gate. A want that differs from the one written only by its run is
// not written again (its run stays, so a node never composes again for it). It answers whether it wrote.
func (p *passer) wantWritten(ctx context.Context, node, module, key string, want mwc.Want) bool {
raw := p.w.value(buckets.Wants(), key)
read := p.w.metaOf(buckets.Wants(), key).revision
var held *mwc.Want
if raw := p.w.value(buckets.Wants(), key); raw != nil {
if raw != nil {
if h, err := mwc.DecodeWant(raw); err == nil {
held = &h
}
@@ -364,9 +368,6 @@ func (p *passer) wantWritten(ctx context.Context, node, module, key string, want
same.Run = want.Run
if reflect.DeepEqual(same, want) {
want.Run = held.Run // it stands: nothing is written
} else if runNumber(held.Run) > runNumber(want.Run) {
p.logf("the want of %s on %s is not written: its run %q is lower than the run %q written (§813 (1))", module, node, want.Run, held.Run)
return false
}
}
value, err := mwc.EncodeWant(want)
@@ -374,20 +375,19 @@ func (p *passer) wantWritten(ctx context.Context, node, module, key string, want
p.logf("%s %s is not written: %v", buckets.Wants(), key, err)
return false
}
if held := p.w.value(buckets.Wants(), key); held != nil && bytes.Equal(held, value) {
if raw != nil && bytes.Equal(raw, value) {
return false
}
kv := p.kvs[buckets.Wants()]
entry, err := kv.Get(ctx, key)
switch {
case errors.Is(err, jetstream.ErrKeyNotFound):
_, err = kv.Create(ctx, key, value)
case err == nil:
if h, derr := mwc.DecodeWant(entry.Value()); derr == nil && runNumber(h.Run) > runNumber(want.Run) {
p.logf("the want of %s on %s is not written: its run %q is lower than the run %q written (§813 (1))", module, node, want.Run, h.Run)
return false
}
_, err = kv.Update(ctx, key, value, entry.Revision())
var revision uint64
if raw == nil {
revision, err = kv.Create(ctx, key, value)
} else {
revision, err = kv.Update(ctx, key, value, read)
}
if errors.Is(err, jetstream.ErrKeyExists) || errors.Is(err, jetstream.ErrKeyRevisionMismatch) || (err != nil && isWrongSequence(err)) {
p.logf("the want of %s on %s (run %q) is not written: its key moved past revision %d this pass read, and the newer write stands (§813 (1), H-A-504 third addendum); the next pass judges it again", module, node, want.Run, read)
return false
}
if err != nil {
if !errors.Is(err, context.Canceled) {
@@ -395,7 +395,7 @@ func (p *passer) wantWritten(ctx context.Context, node, module, key string, want
}
return false
}
p.w.put(buckets.Wants(), key, value)
p.w.putEntry(buckets.Wants(), key, value, revision, p.clock())
return true
}
@@ -46,19 +46,39 @@ import (
type memKV struct {
jetstream.KeyValue
values map[string][]byte
puts int // how many puts reached the bucket
puts int // how many puts reached the bucket
revs map[string]uint64 // each key's revision, as the bus numbers its writes (a key set by hand has none: its length stands in)
seq uint64
}
// written gives key the bucket's next revision, as a write on the bus does.
func (m *memKV) written(key string) uint64 {
if m.revs == nil {
m.revs = map[string]uint64{}
}
m.seq++
m.revs[key] = m.seq
return m.seq
}
// revision is key's revision as the bucket holds it.
func (m *memKV) revision(key string) uint64 {
if r, ok := m.revs[key]; ok {
return r
}
return uint64(len(m.values[key]))
}
func (m *memKV) Put(_ context.Context, key string, value []byte) (uint64, error) {
m.values[key] = value
m.puts++
return uint64(len(m.values)), nil
return m.written(key), nil
}
func (m *memKV) Create(_ context.Context, key string, value []byte, _ ...jetstream.KVCreateOpt) (uint64, error) {
m.values[key] = value
m.puts++ // a write that reaches the bucket (the wants' compare-and-set, design 47 H2 §813 (1))
return uint64(len(m.values)), nil
return m.written(key), nil
}
func (m *memKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOpt) error {
@@ -247,15 +267,15 @@ func (m *memKV) Get(_ context.Context, key string) (jetstream.KeyValueEntry, err
if !ok {
return nil, jetstream.ErrKeyNotFound
}
return memEntry{value: v, revision: uint64(len(v))}, nil
return memEntry{value: v, revision: m.revision(key)}, nil
}
func (m *memKV) Update(_ context.Context, key string, value []byte, revision uint64) (uint64, error) {
if uint64(len(m.values[key])) != revision {
return 0, errors.New("wrong last sequence")
if m.revision(key) != revision {
return 0, fmt.Errorf("wrong last sequence: %w", jetstream.ErrKeyRevisionMismatch)
}
m.values[key] = value
m.puts++
return uint64(len(value)), nil
return m.written(key), nil
}
// memStore is the runtime's states for the put's tools, over the same memKVs a pass writes.
@@ -266,7 +286,7 @@ func (s memStore) get(state, key string) ([]byte, uint64, bool, error) {
return nil, 0, false, nil
}
v, ok := s[state].values[key]
return v, uint64(len(v)), ok, nil
return v, s[state].revision(key), ok, nil
}
func (s memStore) put(state, key string, value []byte) error {
s[state].values[key] = value
@@ -745,12 +765,12 @@ func aPlacedNodesWantsNameTheBuildAloneAndItsYesComesFromItsHave(t *testing.T) {
wkey, _ := mwc.WantKey("g14", "web")
want, _ := mwc.EncodeWant(mwc.Want{Fp: fpOld, Version: "0.1.1", Pipeline: trunkBranch + ".1", Rank: 1})
wantsKV.values[wkey] = want
put(buckets.Wants(), wkey, want)
sync := func() {
sync := func() { // as the watch hands the wants, with their revisions
for k, v := range wantsKV.values {
put(buckets.Wants(), k, v)
p.w.putEntry(buckets.Wants(), k, v, wantsKV.revision(k), time.Now())
}
}
sync()
p.wants(context.Background())
p.nodeWants(context.Background())
@@ -98,8 +98,8 @@ func newOrderFixture(t *testing.T, mods []orderMod, history ...map[string]string
_, _ = p.ends.take(ended(trunkBranch, 1, mpr.PipelineEndedOutcomePassed, mover(m.name, m.trunk, "0.1.1", 1, mpr.StepResultOutcomePassed)))
wkey, _ := mwc.WantKey("ace", m.name)
old, _ := mwc.EncodeWant(mwc.Want{Fp: m.run, Self: true})
p.w.put(buckets.Wants(), wkey, old)
f.wants.values[wkey] = old
p.w.putEntry(buckets.Wants(), wkey, old, f.wants.revision(wkey), time.Now()) // as the watch hands it, with its revision
}
composes(t, p.w, "ace", digest)
values, err := mmac.AssignmentsValues(pl)