Judge a missed first bound by the first read's own entries (H-A-238)

The start read the second bound with a get, which says not found both
for a key never armed and for one the holder's ask-taken deleted, so
every later start asked an answered step once more. The first read
keeps each key's last entry; a first bound's expiry is taken only when
its second bound left no entry at all, and with both passed only the
second is taken, ending the step without asking it again.

Builds: design 47 H H-A-238 (5)
This commit is contained in:
2026-11-21 12:02:02 +01:00
parent 374d060cc0
commit 6b6e4c81f0
3 changed files with 58 additions and 26 deletions
@@ -2,9 +2,12 @@ package main
import (
"context"
"slices"
"testing"
"time"
"github.com/nats-io/nats.go/jetstream"
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
@@ -50,3 +53,25 @@ func TestADropBoundsExpirySaysCallUnanswered(t *testing.T) {
t.Errorf("an answered drop: %v, cleared %v, unbound %v", err, w.cleared, unbound)
}
}
// H-A-238 (5), review: at a start, a first bound's expiry is taken only when the first read holds no entry for its second
// bound (put again and taken: a delete marker; put again: a live put; both passed: an expiry, the second taken alone); a
// second bound's expiry is taken; a delete marker or a drop's expiry is none.
func TestMissedBoundsAtTheStart(t *testing.T) {
const s = "node-build-go"
key := func(a string) string { return pipeline.AskBoundKey(s, a) }
again := func(a string) string { return pipeline.AskAgainBoundKey(s, pipeline.AgainID(a)) }
last := map[string]jetstream.KeyValueOp{
key("lostaaaa000001"): jetstream.KeyValuePurge, // passed while down, never asked again: taken
key("takenaaa000002"): jetstream.KeyValuePurge, again("takenaaa000002"): jetstream.KeyValueDelete, // asked again and taken
key("askedaaa000003"): jetstream.KeyValuePurge, again("askedaaa000003"): jetstream.KeyValuePut, // asked again, bound live
key("bothaaaa000004"): jetstream.KeyValuePurge, again("bothaaaa000004"): jetstream.KeyValuePurge, // both passed
key("wordaaaa000005"): jetstream.KeyValueDelete, // a first word
pipeline.DropBoundKey(s, "dropaaaa000006"): jetstream.KeyValuePurge,
}
want := []string{key("lostaaaa000001"), again("bothaaaa000004")}
slices.Sort(want)
if got := missedBounds(last); !slices.Equal(got, want) {
t.Errorf("missedBounds = %v, want %v", got, want)
}
}
@@ -331,7 +331,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
dropExpired(key)
}
}
if err := watchBounds(ctx, bounds, expired, missedAskBound(ctx, bounds), logger.Printf); err != nil {
if err := watchBounds(ctx, bounds, expired, logger.Printf); err != nil {
return err
}
if err := hearDecided(heard, hearer, cred.Module, logger.Printf); err != nil {
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"github.com/nats-io/nats.go/jetstream"
@@ -183,16 +184,15 @@ func hearAskTaken(hub handlers, taken func(ask, node string) error, c *pipeline.
// watchBounds hears the end of each drop's and each ask's bound, the bus's marker of the key's expiry (A-222, A-77; H-A-235,
// H-A-238 (5)): no timer in the process. An ask's bound that expired while the runner was down left its marker before the
// watch began: the watch's first entries, each key's last, are read to their end, and each ask's key whose last entry is
// an expiry marker is taken as expired now (missed tells it); a delete marker is a first word, never an expiry. Then every
// expiry is heard as it comes.
func watchBounds(ctx context.Context, bounds jetstream.KeyValue, expired func(key string), missed func(key string) bool, logf func(string, ...any)) error {
// watch began: the watch's first entries, each key's last, are kept to their end, and the ask bounds missedBounds names are
// taken as expired then. Then every expiry is heard as it comes.
func watchBounds(ctx context.Context, bounds jetstream.KeyValue, expired func(key string), logf func(string, ...any)) error {
w, err := bounds.WatchAll(ctx)
if err != nil {
return err
}
go func() {
initial := true
last := map[string]jetstream.KeyValueOp{} // each key's last entry, until the first read ends
for {
select {
case <-ctx.Done():
@@ -203,39 +203,46 @@ func watchBounds(ctx context.Context, bounds jetstream.KeyValue, expired func(ke
return
}
if e == nil {
initial = false // the last entries are read: what follows is new
continue
}
if e.Operation() != jetstream.KeyValuePurge {
continue
}
if initial {
if _, _, _, isAsk := pipeline.ParseAskBound(e.Key()); !isAsk || !missed(e.Key()) {
continue
for _, key := range missedBounds(last) {
logf("the bound %s expired while the runner was away: taken now", key)
expired(key)
}
logf("the bound %s expired while the runner was away: taken now", e.Key())
last = nil // the first read is over: what follows is new
continue
}
if last != nil {
last[e.Key()] = e.Operation()
continue
}
if e.Operation() == jetstream.KeyValuePurge {
expired(e.Key())
}
expired(e.Key())
}
}
}()
return nil
}
// missedAskBound says whether an ask's bound found expired at the start is still to be taken: a first bound whose ask was
// asked again already (its second bound is held) is not, so a start never asks it once more again.
func missedAskBound(ctx context.Context, bounds jetstream.KeyValue) func(key string) bool {
return func(key string) bool {
// missedBounds are the ask bounds the first read found expired that are still to be taken (H-A-238 (5)): a second bound's
// expiry always; a first bound's only when the read holds no entry at all for its second bound's key, since a live second
// bound, its expiry or its delete (the holder's ask-taken) says the ask was asked again already. A delete marker is a first
// word, never an expiry; a drop's bound is not taken here.
func missedBounds(last map[string]jetstream.KeyValueOp) []string {
var out []string
for key, op := range last {
if op != jetstream.KeyValuePurge {
continue
}
onSeat, ask, again, ok := pipeline.ParseAskBound(key)
if !ok {
return false
continue
}
if again {
return true
if _, asked := last[pipeline.AskAgainBoundKey(onSeat, pipeline.AgainID(ask))]; again || !asked {
out = append(out, key)
}
e, err := bounds.Get(ctx, pipeline.AskAgainBoundKey(onSeat, pipeline.AgainID(ask)))
return err != nil || e.Operation() != jetstream.KeyValuePut
}
sort.Strings(out)
return out
}
// keptReader reads the kept results of the packages named (§25, §416): the entries of the state kept whose package is a