diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go index 1eb4cc98e..868be980d 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept.go @@ -366,7 +366,7 @@ func (p *passer) capabilityDigests() []meshwants.AssignmentRevisionCapabilitiesI // sayWanted says `wanted` for a want written or removed now, from the revision the pass keeps (A-61, A-67); with none kept, it is // said in the log that it was not said. fp is the want's, and for a removal the fingerprint last wanted there. -func (p *passer) sayWanted(node, module, fp, pipeline string, removed bool) { +func (p *passer) sayWanted(node, module, fp, pipeline string, removed bool, wantRevision uint64) { if p.publishWants == nil { return } @@ -382,12 +382,10 @@ func (p *passer) sayWanted(node, module, fp, pipeline string, removed bool) { return } at := p.clock().UTC() - body := meshwants.WantedEvent{V: keys.Version, Node: node, Module: module, Fp: fp, Pipeline: pipeline, Removed: removed, Revision: kept.Revision, Source: source, At: &at} + body := meshwants.WantedEvent{V: keys.Version, Node: node, Module: module, Fp: fp, Pipeline: pipeline, Removed: removed, Revision: kept.Revision, Source: source, At: &at, WantRevision: int64(wantRevision)} event := strings.TrimPrefix(meshwants.SubjectWanted(keys.EscapeName(node), keys.EscapeName(module)), "mesh.seat."+meshwants.Seat+".event.") - id := fmt.Sprintf("wanted.%s.%s.%s.%t.%d.%s", keys.EscapeName(node), keys.EscapeName(module), fp, removed, kept.Revision, at.Format(time.RFC3339Nano)) - if err := p.wantsSay(event, body, id); err != nil { - p.logf("wanted of %s on %s is not said: %v", module, node, err) - } + id := fmt.Sprintf("wanted.%s.%s.%s.%t.%d", keys.EscapeName(node), keys.EscapeName(module), fp, removed, wantRevision) + p.wantsOwe("want."+keys.EscapeName(node)+"."+keys.EscapeName(module), event, body, id, nil) } // namedBuild answers what a want of module at fp names of its build (design 47 G §379, §526, A-96): a kept build's or another watched diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go index 1bb038e65..91fde228a 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/graphkept_test.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "slices" + "strconv" "strings" "sync" "testing" @@ -102,6 +103,28 @@ func TestTheWantsAreSaidWantedFromTheKeptAssignmentGraphRevision(t *testing.T) { // 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. + // Design 47 H §832 3. (H-A-830, H-A-832): the id of `wanted` names the want's key revision, never the say's time or the graph + // revision, so puts of F1, F0 and F1 say three under three ids, and a removal is the id of the write it removes. + t.Run("a want's ids name its key revision", func(t *testing.T) { + fp0 := fpTest[:len(fpTest)-1] + "0" + events = nil + for _, f := range []string{fpTest, fp0, fpTest} { + p.putWant(ctx, "ace", "owed", mwc.Want{Fp: f, Self: true}) + } + p.putWant(ctx, "ace", "owed", mwc.Want{Removed: true}) + ids := map[string]bool{} + for _, e := range events { + ids[e.id] = true + } + var last meshwants.WantedEvent + _ = json.Unmarshal(events[len(events)-1].body, &last) + if len(events) != 4 || len(ids) != 4 || !strings.Contains(events[0].id, "wanted.ace.owed."+fpTest+".false.") || strings.Contains(events[0].id, "T") { + t.Errorf("ids %v", events) + } + if !last.Removed || last.WantRevision == 0 || !strings.HasSuffix(events[3].id, ".true."+strconv.FormatInt(last.WantRevision, 10)) { + t.Errorf("the removal said %+v under %s", last, events[3].id) + } + }) 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 } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go index a23fd1475..33971edfb 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go @@ -598,7 +598,10 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, if err != nil { return err } - p.tierBound, p.tierWake = tierBound, wake + p.tierBound, p.wake = tierBound, wake + if p.owed.first, p.owed.last, err = owedSetting(getenv); err != nil { + return err + } // The setting pass-stall-seconds: a pass still running after it says which phase it is in (design 47 H2 H-A-635). if p.passStall, err = passStallSetting(getenv); err != nil { return err diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go index 721f9df33..c12f5a044 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go @@ -21,6 +21,7 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/catalogkey" + "git.novox.be/novox/mesh-sdk/go/keys" "git.novox.be/novox/mesh-sdk/go/seatdef" "git.novox.be/novox/mesh-sdk/go/views" @@ -330,11 +331,10 @@ func (p *passer) outsideApply(ctx context.Context, key, hex, node, module, fp st } w := *d.Wait if d.Held { - body := p.outsideBody(w) - if err := p.wantsSay(meshwants.EventWaitHeld, body, key+"."+w.On+"."+w.ClockFrom.UTC().Format(time.RFC3339Nano)); err != nil { - p.logf("the wait of %s on %s is not said, and is said at the next pass: %v", module, node, err) - return - } + // The record is written with `held-said` raised first, and the event said under the id it names (§832 3.). + w.HeldSaid++ + p.keepWait(ctx, key, w) + p.wantsOwe(waitOrder(key, w.ClockFrom), meshwants.EventWaitHeld, p.outsideBody(w), waitEventID(node, module, hex, "held."+strconv.Itoa(w.HeldSaid), w.ClockFrom), nil) p.outsideFinding(w, true) } if d.Ask && w.Ask == "" { @@ -351,9 +351,9 @@ func (p *passer) outsideApply(ctx context.Context, key, hex, node, module, fp st } o.mu.Unlock() if w.Settling { - p.wakeAt(w.SettleEnds, now) + p.wakeSet("wait."+key, w.SettleEnds) } else if w.Standing() { - p.wakeAt(w.ClockFrom.Add(o.bound), now) + p.wakeSet("wait."+key, w.ClockFrom.Add(o.bound)) } p.keepWait(ctx, key, w) } @@ -386,10 +386,7 @@ func (p *passer) outsideEnd(ctx context.Context, key string, w outside.Wait, how if w.Standing() && how != "" { body := p.outsideBody(w) body.How = how - if err := p.wantsSay(meshwants.EventWaitEnded, body, key+"."+how+"."+now.UTC().Format(time.RFC3339Nano)); err != nil { - p.logf("the end of the wait of %s on %s is not said, and is said at the next pass: %v", w.Module, w.Node, err) - return - } + p.wantsOwe(waitOrder(key, w.ClockFrom), meshwants.EventWaitEnded, body, waitEventID(w.Node, w.Module, key[strings.LastIndex(key, ".")+1:], "ended."+how, w.ClockFrom), nil) } p.outsideFinding(w, false) if ask, err := asks.OutsideAskKey(w.Node, w.Module, w.FP); err == nil { @@ -451,10 +448,13 @@ func (p *passer) outsideAsk(ctx context.Context, node, module, fp string, w outs return "" } -// wakeAt makes a pass when t comes: the settle's end and the bound are the waits' own times, the one wake (no polling). -func (p *passer) wakeAt(t, now time.Time) { - if p.outside == nil || p.outside.wake == nil { - return - } - time.AfterFunc(max(t.Sub(now), 0)+time.Millisecond, p.outside.wake) +// waitOrder is the order the events of one wait are said in: its key `..` and its clock-from (§832 1.). +func waitOrder(key string, clockFrom time.Time) string { + return "wait." + key + "." + clockFrom.UTC().Format(time.RFC3339Nano) +} + +// waitEventID is the message id of a wait's event: `....`, the fingerprint the 64 hex +// its record's key holds, the names escaped by keys.EscapeName, the time RFC 3339 with nanoseconds in UTC (§832 3.; H-A-831). +func waitEventID(node, module, hex, what string, clockFrom time.Time) string { + return keys.EscapeName(node) + "." + keys.EscapeName(module) + "." + hex + "." + what + "." + clockFrom.UTC().Format(time.RFC3339Nano) } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go index 7c8c383e7..8254069cb 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside_test.go @@ -31,6 +31,9 @@ type outsideRig struct { unread bool events []string bodies []meshwants.Wait + ids []string + // refuse makes the bus refuse the next n says, as a publish it does not take. + refuse int found map[string]bool woke chan struct{} health string @@ -48,6 +51,11 @@ func newOutsideRig(t *testing.T) *outsideRig { return json.Marshal(map[string]any{"conditions": r.conds}) }, func() { r.woke <- struct{}{} }, func(string, ...any) {}, 30*time.Minute, 90*time.Second) r.p.publishWants = func(event string, body any, id string) error { + if r.refuse > 0 { + r.refuse-- + return context.DeadlineExceeded + } + r.ids = append(r.ids, id) r.events = append(r.events, event) r.bodies = append(r.bodies, body.(meshwants.Wait)) return nil @@ -213,6 +221,74 @@ func TestTheWaitOnAnOutsideCause(t *testing.T) { t.Errorf("events %v", r.events) } }) + t.Run("an event the bus refuses is owed under one id and said again in order, at 2s, 4s, doubling to the pace's end (§832 1., 2.)", func(t *testing.T) { + r := newOutsideRig(t) + var armed []time.Duration + r.p.wake = func() {} + r.p.after = func(d time.Duration, f func()) func() bool { + armed = append(armed, d) + return func() bool { return true } + } + r.p.owed.last = 8 * time.Second + r.cond("machine.hub.unreachable", "ace") + r.refuse = 3 // the wait-held is refused now, and at its first two says again + r.review(false, nil) + if len(r.events) != 0 { + t.Fatalf("a refused event was said: %v", r.events) + } + // the wait ends while its wait-held is owed: the end is owed behind it, never tried before it + r.conds, r.health, r.now = nil, nodeengineclient.HealthOK, r.now.Add(time.Minute) + r.review(false, nil) + if len(r.events) != 0 || len(r.p.owed.list) != 2 { + t.Fatalf("events %v owed %d, want the held and the end owed in order", r.events, len(r.p.owed.list)) + } + heldID, endID := r.p.owed.list[0].id, r.p.owed.list[1].id + for _, want := range []struct { + tries int + delay time.Duration + }{{1, 4 * time.Second}, {2, 8 * time.Second}} { + r.now = r.p.owedNext() + if r.p.sayOwed() || r.p.owed.list[0].tries != want.tries || r.p.owed.list[0].next.Sub(r.now) != want.delay { + t.Fatalf("try %d: said %v owed %+v, want the next delay %v", want.tries, r.events, r.p.owed.list[0], want.delay) + } + } + if d := r.p.owed.list[0].next.Sub(r.now); d != 8*time.Second { + t.Errorf("the third delay %v, want 8s (the pace's end)", d) + } + r.now = r.p.owedNext() + if !r.p.sayOwed() { + t.Fatal("the bus took nothing") + } + if len(r.events) != 2 || r.events[0] != meshwants.EventWaitHeld || r.events[1] != meshwants.EventWaitEnded || r.ids[0] != heldID || r.ids[1] != endID || len(r.p.owed.list) != 0 { + t.Errorf("events %v ids %v, owed %d: want held then ended under the ids first owed (%s, %s)", r.events, r.ids, len(r.p.owed.list), heldID, endID) + } + if !strings.Contains(heldID, ".held.1.") || !strings.Contains(endID, ".ended.healthy.") || len(armed) == 0 { + t.Errorf("ids %s %s, armed %v", heldID, endID, armed) + } + }) + t.Run("an on going from A to B and back to A says three wait-held under three ids; a name with a dot is escaped (§832 3.; H-A-831)", func(t *testing.T) { + r := newOutsideRig(t) + r.cond("machine.hub.unreachable", "ace") + r.review(false, nil) + for _, key := range []string{"machine.hub.network", "machine.hub.unreachable"} { + r.conds = nil + r.cond(key, "ace") + r.now = r.now.Add(time.Minute) + r.p.outside.Heard("mesh.seat.mesh-conditions.event.condition-raised", []byte(`{"key":"`+key+`","subject":{"id":"hub","scope":"machine","machine":"hub","also":["ace"]}}`)) + <-r.woke + r.review(false, nil) + } + seen := map[string]bool{} + for _, id := range r.ids { + seen[id] = true + } + if len(r.ids) != 3 || len(seen) != 3 { + t.Errorf("wait-held ids %v, want three different", r.ids) + } + if id := waitEventID("a.b", "m.x", "00ff", "held.1", r.now); !strings.HasPrefix(id, "a=2eb.m=2ex.00ff.held.1.") { + t.Errorf("id %q holds a dot unescaped", id) + } + }) t.Run("its bounds are read at the start from the files the manifest declares (A-112)", func(t *testing.T) { dir := t.TempDir() write := func(name, content string) string { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/owed.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/owed.go new file mode 100644 index 000000000..a84fa8411 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/owed.go @@ -0,0 +1,230 @@ +package main + +import ( + "slices" + "sync" + "time" +) + +// An event the bus refuses is owed in order and said again, never dropped (design 47 H §832 1., 2.; H-A-777, H-A-801): the +// events the assigner says on the seat mesh-wants (`wanted`, `pipeline-wanted`, `wait-held`, `wait-ended`) are kept in its memory +// when the bus refuses them or does not acknowledge them, in the order they were made within one wait, one pipeline or one want, +// and the first of each order is said again on the one deadline timer under the message id fixed when it was first owed. + +// The defaults of the settings say-again-pace-first and say-again-pace-max (design 47 H §832 2.). +const ( + defaultSayAgainFirst = 2 * time.Second + defaultSayAgainMax = time.Minute +) + +// owedEvent is one event owed: what the bus was to be told, and where its saying again stands. +type owedEvent struct { + order, event string + body any + id string + firstOwed time.Time + tries int + next time.Time + // taken is what waited on the bus taking it (a record kept until then); nil for none. It runs once, at the say or at a drop. + taken func() +} + +// owedKept is the events owed, in the order they were made; the memory of the process, never the bus that refused them. +type owedKept struct { + mu sync.Mutex + list []*owedEvent + first, last time.Duration // the setting's pace; zero: the defaults +} + +func (o *owedKept) pace() (first, max time.Duration) { + first, max = o.first, o.last + if first <= 0 { + first = defaultSayAgainFirst + } + if max <= 0 { + max = defaultSayAgainMax + } + return first, max +} + +func (o *owedKept) has(id string) bool { + return slices.ContainsFunc(o.list, func(e *owedEvent) bool { return e.id == id }) +} + +func (o *owedKept) ordered(order string) bool { + return slices.ContainsFunc(o.list, func(e *owedEvent) bool { return e.order == order }) +} + +// owedSetting is a setting kept as a Go duration, read at the start from the file the manifest declares; zero, its default, +// for a file missing or empty. +func owedSetting(getenv func(string) string) (first, max time.Duration, err error) { + if first, err = durationFile(getenv("MESH_ASSIGNER_SAY_AGAIN_PACE_FIRST_FILE")); err != nil { + return 0, 0, err + } + max, err = durationFile(getenv("MESH_ASSIGNER_SAY_AGAIN_PACE_MAX_FILE")) + return first, max, err +} + +// wantsOwe says one event of the seat mesh-wants, and owes it when the bus does not take it: an id already owed makes nothing, an +// event whose order has an earlier one owed is owed behind it and not tried, and one the bus refuses is kept with `first-owed`, +// `tries` and `next`. taken runs once the bus has taken the event (at once when it takes it now). With no bus, nothing is said. +func (p *passer) wantsOwe(order, event string, body any, id string, taken func()) { + if p.publishWants == nil { + if taken != nil { + taken() + } + return + } + o := &p.owed + o.mu.Lock() + defer o.mu.Unlock() + if o.has(id) { + return + } + first, _ := o.pace() + now := p.clock().UTC() + e := &owedEvent{order: order, event: event, body: body, id: id, firstOwed: now, next: now.Add(first), taken: taken} + if o.ordered(order) { + o.list = append(o.list, e) + p.logf("%s %s is owed behind an earlier event of its order %s", event, id, order) + return + } + if err := p.publishWants(event, body, id); err != nil { + o.list = append(o.list, e) + p.logf("%s %s is not said and is owed, said again from %s: %v", event, id, e.next.Format(time.RFC3339Nano), err) + return + } + if taken != nil { + taken() + } +} + +// sayOwed says again each owed event whose time has come, the first of each order and then the next of that order once it is taken +// (§832 2.): a refusal doubles the delay up to say-again-pace-max. It answers whether the bus took any. +func (p *passer) sayOwed() bool { + if p.publishWants == nil { + return false + } + o := &p.owed + o.mu.Lock() + defer o.mu.Unlock() + now := p.clock().UTC() + first, max := o.pace() + said, blocked, freed := false, map[string]bool{}, map[string]bool{} + for i := 0; i < len(o.list); { + e := o.list[i] + if blocked[e.order] { + i++ + continue + } + if e.next.After(now) && !freed[e.order] { // the next of an order is said once the one before it is taken + blocked[e.order] = true + i++ + continue + } + if err := p.publishWants(e.event, e.body, e.id); err != nil { + e.tries++ + delay := first + for n := 0; n < e.tries && delay < max; n++ { + delay *= 2 + } + e.next = now.Add(min(delay, max)) + p.logf("%s %s is not said again (try %d), said again from %s: %v", e.event, e.id, e.tries, e.next.Format(time.RFC3339Nano), err) + blocked[e.order] = true + i++ + continue + } + p.logf("%s %s is said again (try %d)", e.event, e.id, e.tries+1) + o.list = slices.Delete(o.list, i, i+1) + if e.taken != nil { + e.taken() + } + said, freed[e.order] = true, true + } + return said +} + +// owedNext is the earliest time an owed event is to be said again, the first of its order only; zero for none. +func (p *passer) owedNext() time.Time { + o := &p.owed + o.mu.Lock() + defer o.mu.Unlock() + var next time.Time + seen := map[string]bool{} + for _, e := range o.list { + if seen[e.order] { + continue + } + seen[e.order] = true + if next.IsZero() || e.next.Before(next) { + next = e.next + } + } + return next +} + +// wakes are the times a pass is to be made at by itself, by source: the tiers' earliest deadline, each wait's bound or settle end, +// and the owed events' earliest `next`. One timer stands, armed at the earliest (H-A-846; principle 7). +type wakes struct { + mu sync.Mutex + at map[string]time.Time + stop func() bool + armed time.Time +} + +// wakeSet records the time source is to wake the pass at (zero: none) and arms the one timer at the earliest recorded. +func (p *passer) wakeSet(source string, at time.Time) { + wake := p.wake + if wake == nil && p.outside != nil { + wake = p.outside.wake + } + if wake == nil { + return + } + w := &p.wakes + w.mu.Lock() + defer w.mu.Unlock() + if w.at == nil { + w.at = map[string]time.Time{} + } + if at.IsZero() { + delete(w.at, source) + } else { + w.at[source] = at + } + var earliest time.Time + for _, t := range w.at { + if earliest.IsZero() || t.Before(earliest) { + earliest = t + } + } + if earliest.Equal(w.armed) && w.stop != nil { + return + } + if w.stop != nil { + w.stop() + w.stop = nil + } + w.armed = earliest + if earliest.IsZero() { + return + } + d := max(earliest.Sub(p.clock()), 0) + time.Millisecond + fire := func() { + w.mu.Lock() + for k, t := range w.at { + if !t.After(earliest) { + delete(w.at, k) // heard: a pass that still needs it sets it again + } + } + w.armed, w.stop = time.Time{}, nil + w.mu.Unlock() + wake() + } + if p.after != nil { + w.stop = p.after(d, fire) + return + } + t := time.AfterFunc(d, fire) + w.stop = t.Stop +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go index 1f6d6233a..b0c8b5979 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -115,11 +115,15 @@ type passer struct { // entries are the catalog entries by fingerprint asked of the seat mesh-catalog's verb `entry` (design 47 G §614, A-217): a // held revert's running entry and its want's (revert.go); nil: no revert is asked about. entries *meshcatalogclient.Entries - // tierBound is the setting tier-bound (design 47 H §751; zero: its default), and tierTimer the one timer that wakes the pass at - // the earliest tier's deadline (tiers.go), made with tierWake; nil tierWake sets no timer. + // tierBound is the setting tier-bound (design 47 H §751; zero: its default). wake makes a pass; wakes are the times the one + // timer wakes it at, armed at the earliest (owed.go, H-A-846), made with after (nil: time.AfterFunc; a test's seam); nil wake + // sets no timer. tierBound time.Duration - tierWake func() - tierTimer *time.Timer + wake func() + wakes wakes + after func(time.Duration, func()) func() bool + // owed are the events of the seat mesh-wants the bus refused, owed in order and said again (owed.go, design 47 H §832). + owed owedKept // passStall is the setting pass-stall-seconds (passstall.go; zero: its default): a pass still running after it says which phase // it is in (design 47 H2 H-A-635). passStall time.Duration @@ -158,8 +162,10 @@ func (p *passer) pass(ctx context.Context) { watch := p.watchPass(start) // a pass past pass-stall-seconds says which phase it is in (design 47 H2 H-A-635) defer func() { watch.end() + p.wakeSet("owed", p.owedNext()) p.logf("the pass took %s", time.Since(start).Round(time.Millisecond)) // design 47 H-A-259 }() + p.sayOwed() watch.in(phaseAssignmentsSeat) p.assignmentsSeat(ctx) watch.in(phaseKeptGraph) @@ -362,8 +368,14 @@ func (p *passer) putWant(ctx context.Context, node, module string, want mwc.Want fp = before.Fp } } + // The want-revision `wanted` says is the revision of the want's key it says; for a removal, of the write it removes (§832 3.). + removes := p.w.metaOf(buckets.Wants(), key).revision if p.wantWritten(ctx, node, module, key, want) { - p.sayWanted(node, module, fp, want.Pipeline, want.Removed) + revision := removes + if !want.Removed { + revision = p.w.metaOf(buckets.Wants(), key).revision + } + p.sayWanted(node, module, fp, want.Pipeline, want.Removed, revision) } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go index 37fe8fb60..212378749 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits.go @@ -88,11 +88,8 @@ func (p *passer) putWaits(ctx context.Context, all map[string]map[string]puts.Pu } body := putWaitBody(node, module, was.Put) body.How = how - if err := p.wantsSay(meshwants.EventWaitEnded, body, was.ID+".put."+how); err != nil { - p.logf("the end of the put %s's wait is not said, and is said at the next pass: %v", key, err) - continue - } - p.remove(ctx, buckets.PutWaits(), key) + // The kept record stays until the bus has taken the end, so a restart says it again (§832 4.; H-A-830). + p.wantsOwe(putWaitOrder(node, module), meshwants.EventWaitEnded, body, was.ID+".put."+how, func() { p.remove(ctx, buckets.PutWaits(), key) }) delete(waiting, key) } for module, byNode := range all { @@ -105,12 +102,12 @@ func (p *passer) putWaits(ctx context.Context, all map[string]map[string]puts.Pu if w, said := waiting[key]; said && w.ID == id { continue } - if err := p.wantsSay(meshwants.EventWaitHeld, putWaitBody(node, module, put), id+".put"); err != nil { - p.logf("the put %s's wait is not said, and is said at the next pass: %v", key, err) - continue - } + p.wantsOwe(putWaitOrder(node, module), meshwants.EventWaitHeld, putWaitBody(node, module, put), id+".put", nil) kept := putWaiting{ID: id, Put: put} p.put(ctx, buckets.PutWaits(), key, func() ([]byte, error) { return json.Marshal(kept) }) } } } + +// putWaitOrder is the order the events of a put's wait are said in: its `.` (design 47 H §832 1.; H-A-830). +func putWaitOrder(node, module string) string { return "put-wait." + node + "." + module } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go index c5ce059a5..e36c14237 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/putwaits_test.go @@ -2,6 +2,7 @@ package main import ( "context" + "strings" "testing" "time" @@ -89,4 +90,29 @@ func TestAPutWithoutHoldIsSaidAsAWait(t *testing.T) { if a := answerOf("ace", "web", held); !a.Since.Equal(at.Add(time.Minute)) || a.Bound != "" { t.Errorf("a held put answers since %v, bound %q", a.Since, a.Bound) } + // Design 47 H §832 (H-A-830): the bus refuses the wait-held once, then the put ends; the end is owed behind the held, and the kept + // record stays until the bus took the end. Both are said after, in that order, under main's ids. + refused := true + events, bodies = nil, nil + var ids []string + p.publishWants = func(event string, body any, id string) error { + if refused { + return context.DeadlineExceeded + } + events, bodies, ids = append(events, event), append(bodies, body.(meshwants.Wait)), append(ids, id) + return nil + } + fresh := puts.Put{FP: fpTest, Why: "a try again", At: at.Add(3 * time.Hour)}.Said(at.Add(3*time.Hour), puts.Wanted, "the want is written") + p.putWaits(ctx, map[string]map[string]puts.Put{"db": {"g14": fresh}}) + p.putWaits(ctx, map[string]map[string]puts.Put{"db": {"g14": fresh.Said(at.Add(4*time.Hour), puts.Done, "runs")}}) + if len(events) != 0 || kept.values["g14.db"] == nil { + t.Fatalf("a refused say was kept as said: %v, record %q", events, kept.values["g14.db"]) + } + refused = false + p.after = nil + p.owed.list[0].next = p.clock() + if !p.sayOwed() || len(events) != 2 || events[0] != meshwants.EventWaitHeld || events[1] != meshwants.EventWaitEnded || + !strings.HasSuffix(ids[0], ".put") || !strings.HasSuffix(ids[1], ".put."+putWaitRan) || kept.values["g14.db"] != nil { + t.Errorf("the owed said %v under %v, record %q", events, ids, kept.values["g14.db"]) + } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers.go index d66bd317b..4ff239895 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tiers.go @@ -395,24 +395,11 @@ func (p *passer) sayPipelineWanted(pipeline, branch, number string, source int64 } body := meshwants.PipelineWantedEvent{V: keys.Version, Pipeline: pipeline, Branch: branch, Source: source, Revision: revision, Outcome: outcome, NotRunning: notRunning} event := meshwants.EventPipelineWanted + "." + pipeline - if err := p.wantsSay(event, body, "pipeline-wanted."+pipeline+"."+outcome); err != nil { - p.logf("pipeline-wanted of %s is not said: %v", pipeline, err) - } + p.wantsOwe("pipeline."+pipeline, event, body, "pipeline-wanted."+pipeline+"."+outcome, nil) } -// tierDeadline sets the one timer that wakes the pass at the earliest tier's deadline (H-A-56); zero stops it. -func (p *passer) tierDeadline(at time.Time) { - if p.tierWake == nil { - return - } - if p.tierTimer != nil { - p.tierTimer.Stop() - } - if at.IsZero() { - return - } - p.tierTimer = time.AfterFunc(max(at.Sub(p.clock()), 0), p.tierWake) -} +// tierDeadline sets the earliest tier's deadline the one timer wakes the pass at (H-A-56, H-A-846); zero for none. +func (p *passer) tierDeadline(at time.Time) { p.wakeSet("tiers", at) } // tierBoundSetting is the setting tier-bound in minutes (design 47 H §751), read at the start from the file the manifest declares // with the content ${config:tier-bound}; zero, its default, for a file missing or empty. diff --git a/modules/mesh-delivery-assigner/internal/outside/outside.go b/modules/mesh-delivery-assigner/internal/outside/outside.go index 47235075b..31fa6391f 100644 --- a/modules/mesh-delivery-assigner/internal/outside/outside.go +++ b/modules/mesh-delivery-assigner/internal/outside/outside.go @@ -112,6 +112,9 @@ type Wait struct { Release string `json:"release,omitempty"` By string `json:"by,omitempty"` Why string `json:"why,omitempty"` + // HeldSaid counts the wait-held the wait was the record of, raised by one in the write that makes a new one, so an `on` back at + // an earlier cause makes a new message id (design 47 H §832 3.). + HeldSaid int `json:"held-said,omitempty"` // Decided is `go-on` or `failed` once the wait ended so. Decided string `json:"decided,omitempty"` } diff --git a/modules/mesh-delivery-assigner/manifest_test.go b/modules/mesh-delivery-assigner/manifest_test.go index c3e1abd32..28d723f9e 100644 --- a/modules/mesh-delivery-assigner/manifest_test.go +++ b/modules/mesh-delivery-assigner/manifest_test.go @@ -186,7 +186,7 @@ func TestManifest(t *testing.T) { } file, _ := os.ReadFile("module.json") if err := json.Unmarshal(file, &raw); err != nil || raw.Config["outside-wait-minutes"].Default != float64(30) || raw.Config["outside-settle-seconds"].Default != float64(90) || - raw.Config["ask-operator-for"].Default != "take-off,remove-node,stops" || len(raw.Config) != 18 || raw.Config["tier-bound"].Default != float64(30) || raw.Config["entry-asks"].Default != float64(5) || raw.Config["reads-bound"].Default != float64(8) || + raw.Config["ask-operator-for"].Default != "take-off,remove-node,stops" || len(raw.Config) != 20 || raw.Config["tier-bound"].Default != float64(30) || raw.Config["entry-asks"].Default != float64(5) || raw.Config["reads-bound"].Default != float64(8) || raw.Config["composition-runs-cap-mib"].Default != float64(16) || raw.Config["composition-runs-kept"].Default != "24h" || raw.Config["composition-step-bound"].Default != "30m" || raw.Config["run-untaken-bound"].Default != "24h" || raw.Config["blame-tries"].Default != float64(20) || // design 47 H2 H-A-628 (3): the bound of a run's wait for a package's publish, the builder's default @@ -194,7 +194,9 @@ func TestManifest(t *testing.T) { // design 47 H2 H-A-635: the bound a pass runs before it says which phase it is in raw.Config["pass-stall-seconds"].Default != float64(60) || // design 47 H2 §775, H-A-381 (2): the taken key's bound and the kept-call pace, runtime settings - raw.Config["ask-taken-bound"].Default != "10m" || raw.Config["kept-call-pace-first"].Default != "1s" || raw.Config["kept-call-pace-max"].Default != "30s" || raw.Config["compose-ask-bound"].Default != "10s" || raw.Config["keeping-retry"].Default != "30s" { + raw.Config["ask-taken-bound"].Default != "10m" || raw.Config["kept-call-pace-first"].Default != "1s" || raw.Config["kept-call-pace-max"].Default != "30s" || raw.Config["compose-ask-bound"].Default != "10s" || raw.Config["keeping-retry"].Default != "30s" || + // design 47 H §832 2.: the pace an owed event is said again at + raw.Config["say-again-pace-first"].Default != "2s" || raw.Config["say-again-pace-max"].Default != "1m" { t.Errorf("config = %+v (%v), want outside-wait-minutes of 30, outside-settle-seconds of 90 and ask-operator-for of take-off,remove-node,stops (A-425a)", raw.Config, err) } for _, f := range []struct{ env, path, key string }{ @@ -207,11 +209,13 @@ func TestManifest(t *testing.T) { {"MESH_ASSIGNER_COMPOSITION_STEP_BOUND_FILE", "${dir:state}/composition-step-bound", "composition-step-bound"}, {"MESH_ASSIGNER_RUN_UNTAKEN_BOUND_FILE", "${dir:state}/run-untaken-bound", "run-untaken-bound"}, {"MESH_ASSIGNER_BLAME_TRIES_FILE", "${dir:state}/blame-tries", "blame-tries"}, - {"MESH_ASSIGNER_PACKAGE_WAIT_FILE", "${dir:state}/package-wait-minutes", "package-wait-minutes"}, // design 47 H2 H-A-628 (3) - {"MESH_ASSIGNER_PASS_STALL_FILE", "${dir:state}/pass-stall-seconds", "pass-stall-seconds"}, // design 47 H2 H-A-635 - {"MESH_ASSIGNER_TIER_BOUND_FILE", "${dir:state}/tier-bound", "tier-bound"}, // design 47 H §751 - {"MESH_ASSIGNER_ENTRY_ASKS_FILE", "${dir:state}/entry-asks", "entry-asks"}, // design 47 H H-A-60 - {"MESH_ASSIGNER_READS_BOUND_FILE", "${dir:state}/reads-bound", "reads-bound"}, // design 47 H2 H-A-708 (3) + {"MESH_ASSIGNER_PACKAGE_WAIT_FILE", "${dir:state}/package-wait-minutes", "package-wait-minutes"}, // design 47 H2 H-A-628 (3) + {"MESH_ASSIGNER_PASS_STALL_FILE", "${dir:state}/pass-stall-seconds", "pass-stall-seconds"}, // design 47 H2 H-A-635 + {"MESH_ASSIGNER_TIER_BOUND_FILE", "${dir:state}/tier-bound", "tier-bound"}, // design 47 H §751 + {"MESH_ASSIGNER_ENTRY_ASKS_FILE", "${dir:state}/entry-asks", "entry-asks"}, // design 47 H H-A-60 + {"MESH_ASSIGNER_READS_BOUND_FILE", "${dir:state}/reads-bound", "reads-bound"}, // design 47 H2 H-A-708 (3) + {"MESH_ASSIGNER_SAY_AGAIN_PACE_FIRST_FILE", "${dir:state}/say-again-pace-first", "say-again-pace-first"}, // design 47 H §832 2. + {"MESH_ASSIGNER_SAY_AGAIN_PACE_MAX_FILE", "${dir:state}/say-again-pace-max", "say-again-pace-max"}, } { if !strings.Contains(string(file), `"`+f.env+`": "`+f.path+`"`) || !strings.Contains(string(file), `"path": "`+f.path+`"`) || !strings.Contains(string(file), `"content": "${config:`+f.key+`}"`) { diff --git a/modules/mesh-delivery-assigner/module.json b/modules/mesh-delivery-assigner/module.json index 78d76c7e9..16e18429f 100644 --- a/modules/mesh-delivery-assigner/module.json +++ b/modules/mesh-delivery-assigner/module.json @@ -250,6 +250,18 @@ "trusted": false, "default": 10 }, + "say-again-pace-first": { + "type": "string", + "description": "the first wait before the assigner says again an event of the seat mesh-wants that the bus refused or did not acknowledge, under the same message id, doubling at each try to say-again-pace-max, a Go duration (design 47 H section 832 2.).", + "trusted": false, + "default": "2s" + }, + "say-again-pace-max": { + "type": "string", + "description": "the longest wait between two says again of an owed event, a Go duration (design 47 H section 832 2.).", + "trusted": false, + "default": "1m" + }, "pass-stall-seconds": { "type": "number", "description": "seconds a pass of the assigner may run before it says in the journal which phase it is in, and again at each further bound until it ends (design 47 H2 H-A-635).", @@ -413,6 +425,22 @@ "mode": "0600", "content": "${config:package-wait-minutes}" }, + { + "id": "say-again-pace-first", + "type": "file", + "path": "${dir:state}/say-again-pace-first", + "owner": "mesh-delivery-assigner", + "mode": "0600", + "content": "${config:say-again-pace-first}" + }, + { + "id": "say-again-pace-max", + "type": "file", + "path": "${dir:state}/say-again-pace-max", + "owner": "mesh-delivery-assigner", + "mode": "0600", + "content": "${config:say-again-pace-max}" + }, { "id": "pass-stall-seconds", "type": "file", @@ -451,7 +479,9 @@ "MESH_ASSIGNER_RUN_UNTAKEN_BOUND_FILE": "${dir:state}/run-untaken-bound", "MESH_ASSIGNER_BLAME_TRIES_FILE": "${dir:state}/blame-tries", "MESH_ASSIGNER_PACKAGE_WAIT_FILE": "${dir:state}/package-wait-minutes", - "MESH_ASSIGNER_PASS_STALL_FILE": "${dir:state}/pass-stall-seconds" + "MESH_ASSIGNER_PASS_STALL_FILE": "${dir:state}/pass-stall-seconds", + "MESH_ASSIGNER_SAY_AGAIN_PACE_FIRST_FILE": "${dir:state}/say-again-pace-first", + "MESH_ASSIGNER_SAY_AGAIN_PACE_MAX_FILE": "${dir:state}/say-again-pace-max" }, "user": "mesh-delivery-assigner", "health": {