diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go index d10bfce0..d37e6f7e 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/drops_test.go @@ -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) + } +} diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go index 8148e161..f5f8af67 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -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 { diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go index 4d5b85e7..6ae613ff 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go @@ -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