A-282 (join 11): an ask's step bound key is created once, and a second waiter takes it as armed

stepBounds.Arm deleted the key and created it again, so two builder instances waiting on one ask raced: one's
create met the other's and failed "wrong last sequence", and the ask's build failed (the builder bed's two
instances together). Arm now creates the key only if absent; one already there is armed, and nothing deletes
it to make it again.

Builds: design 47 G A-282, A-115, §544

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
This commit is contained in:
2026-11-12 13:13:50 +01:00
parent daa7737392
commit c663b27848
2 changed files with 58 additions and 4 deletions
@@ -19,16 +19,17 @@ type stepBounds struct {
logf func(string, ...any)
}
// Arm is buildrun.StepBounds' Arm: the key is written afresh, so a step that moved has its bound counted from its movement.
// Arm is buildrun.StepBounds' Arm: the key is created once (design 47 G A-282). A waiter that finds it already there, made by
// another waiter of the same ask, takes it as armed and waits on the same key; it is never deleted to be made again.
func (s *stepBounds) Arm(ctx context.Context, key, why string, bound time.Duration) error {
kv := s.bounds()
if kv == nil {
return fmt.Errorf("%s is not on the bus yet", versions.BoundsBucket)
}
if err := kv.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
return err
}
_, err := kv.Create(ctx, key, versions.EncodeBound(why, s.now().Add(bound)), jetstream.KeyTTL(bound))
if errors.Is(err, jetstream.ErrKeyExists) {
return nil // armed already
}
return err
}
@@ -0,0 +1,53 @@
//go:build integration
package main
import (
"sync"
"testing"
"time"
"github.com/nats-io/nats.go/jetstream"
"mesh-delivery-builder/internal/versions"
)
// A-282: an ask's step bound key is created once. Two waiters on one ask arm it side by side and each takes it as armed; a
// waiter that arms it again finds it there and leaves it as it stands, never deleted to be made again.
func TestTwoWaitersOnOneAskArmItsStepBoundOnce(t *testing.T) {
b := startBed(t)
kv, err := b.js.KeyValue(b.ctx, versions.BoundsBucket)
if err != nil {
t.Fatal(err)
}
at := time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC)
waiter := func() *stepBounds {
return &stepBounds{bounds: func() jetstream.KeyValue { return kv }, now: func() time.Time { return at }, logf: t.Logf}
}
const key = "stand.mwaskaaaaaaaaa"
var wg sync.WaitGroup
errs := make(chan error, 2)
for range 2 {
wg.Add(1)
go func() { defer wg.Done(); errs <- waiter().Arm(b.ctx, key, "the step stood still", time.Minute) }()
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
t.Errorf("a waiter of the ask did not take the bound as armed: %v", err)
}
}
first, err := kv.Get(b.ctx, key)
if err != nil {
t.Fatal(err)
}
at = at.Add(30 * time.Second)
if err := waiter().Arm(b.ctx, key, "the step stood still", time.Minute); err != nil {
t.Fatalf("a later waiter: %v", err)
}
again, err := kv.Get(b.ctx, key)
if err != nil || again.Revision() != first.Revision() || string(again.Value()) != string(first.Value()) {
t.Errorf("the bound was made again: revision %d (was %d), %v", again.Revision(), first.Revision(), err)
}
}