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 bb78255d3..0b0dfe905 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/outside.go @@ -70,17 +70,80 @@ type outsideActs struct { // bound and settle are the settings outside-wait-minutes and outside-settle-seconds. bound, settle time.Duration // reading is a read in flight by machine, answered the answers it brought, not yet judged by. - reading map[string]bool - answered map[string][]outside.Cond - // dirty is a machine whose waits are judged again, cleared a wait's key whose cause's clearing was heard. - dirty map[string]bool - cleared map[string]bool + reading map[string]bool + // answered is, by machine, the newest answer of the conditions and when it was received, kept for the pass that first takes it + // and replaced by each new one (design 47 H2b §829 1.); condAt is the time of the last condition event naming the machine. + answered map[string]*machineAnswer + condAt map[string]time.Time + // dirty is a machine whose waits are judged again, cleared a wait's key whose cause's clearing was heard; each is read by every + // judging of one pass alike (marks). + dirty marks + cleared marks + // pass is the number of the pass judging now; clock is the passer's, nil for the wall clock. + pass int64 + clock func() time.Time // sigs is each check's last signature, by node.module. sigs map[string]string // causes is, by condition key, the nodes whose waits stand on it, as the last pass kept them. causes map[string][]string - // started says the first pass judged every wait again. - started bool + // started says a pass began before this one, so the first pass judges every wait again. + started, first bool +} + +// machineAnswer is one answer of the keeper's `conditions` for a machine, the pass that first took it (zero: none yet). +type machineAnswer struct { + conds []outside.Cond + at time.Time + takenIn int64 +} + +// marks are the machines (or keys) whose waits are judged again, read alike by every judging of one pass: a mark is cleared at the +// next pass's start only when the pass saw it, so an event heard after a judging is judged by the next (§829 2.). +type marks struct { + n uint64 + at, done, seen map[string]uint64 +} + +func (m *marks) set(k string) { + if m.at == nil { + m.at, m.done, m.seen = map[string]uint64{}, map[string]uint64{}, map[string]uint64{} + } + m.n++ + m.at[k] = m.n +} + +func (m *marks) is(k string) bool { + v := m.at[k] + if v <= m.done[k] { + return false + } + m.seen[k] = max(m.seen[k], v) + return true +} + +func (m *marks) next() { + for k, v := range m.seen { + m.done[k] = v + delete(m.seen, k) + } +} + +// beginPass starts a pass of the judgings: what the last pass judged by is spent, and the first pass judges every wait again. +func (o *outsideActs) beginPass(now func() time.Time) { + o.mu.Lock() + defer o.mu.Unlock() + o.clock = now + o.pass++ + o.dirty.next() + o.cleared.next() + o.first, o.started = !o.started, true +} + +func (o *outsideActs) now() time.Time { + if o.clock != nil { + return o.clock() + } + return time.Now() } func newOutsideActs(call func(string, any) (json.RawMessage, error), wake func(), logf func(string, ...any), bound, settle time.Duration) *outsideActs { @@ -90,8 +153,7 @@ func newOutsideActs(call func(string, any) (json.RawMessage, error), wake func() if settle <= 0 { settle = outside.DefaultSettle } - return &outsideActs{call: call, wake: wake, logf: logf, bound: bound, settle: settle, reading: map[string]bool{}, answered: map[string][]outside.Cond{}, - dirty: map[string]bool{}, cleared: map[string]bool{}, sigs: map[string]string{}, causes: map[string][]string{}} + return &outsideActs{call: call, wake: wake, logf: logf, bound: bound, settle: settle, reading: map[string]bool{}, answered: map[string]*machineAnswer{}, condAt: map[string]time.Time{}, sigs: map[string]string{}, causes: map[string][]string{}} } // outsideSettings are the bounds of the wait, the settings outside-wait-minutes and outside-settle-seconds (design 47 G §633, §706), @@ -143,29 +205,44 @@ func (o *outsideActs) Heard(subject string, body []byte) { } cleared := strings.HasSuffix(subject, "."+mcseat.EventConditionCleared) o.mu.Lock() + at := o.now() if cleared { for _, node := range o.causes[e.Key] { - o.dirty[node] = true - o.cleared[node+"|"+e.Key] = true + o.dirty.set(node) + o.cleared.set(node + "|" + e.Key) + o.condAt[node] = at } - } else if e.Subject != nil { - o.dirty[e.Subject.Machine] = true + } + if e.Subject != nil { + if !cleared { + o.dirty.set(e.Subject.Machine) + } + o.condAt[e.Subject.Machine] = at for _, m := range e.Subject.Also { - o.dirty[m] = true + if !cleared { + o.dirty.set(m) + } + o.condAt[m] = at } } o.mu.Unlock() o.wake() } -// take is the conditions read for machine, consumed (one answer is one judging), or false while the kept call is unanswered, -// which it starts when none is in flight. -func (o *outsideActs) take(machine string) ([]outside.Cond, bool) { +// take is the conditions read for machine, or false while the kept call is unanswered, which it starts when none is in flight +// (design 47 H2b §829). The answer is kept for the whole pass that first takes it, so every wait judged on the machine in that pass +// is judged by it; a later pass reads again, but at a settle's end (settleEnd, the failing check at since) the newest answer serves +// when it was received after the failing check and no condition event naming the machine came after it. +func (o *outsideActs) take(machine string, since time.Time, settleEnd bool) ([]outside.Cond, bool) { o.mu.Lock() defer o.mu.Unlock() - if c, ok := o.answered[machine]; ok { - delete(o.answered, machine) - return c, true + if a := o.answered[machine]; a != nil { + if a.takenIn == 0 { + a.takenIn = o.pass + } + if a.takenIn == o.pass || (settleEnd && !a.at.Before(since) && !o.condAt[machine].After(a.at)) { + return a.conds, true + } } if o.reading[machine] { return nil, false @@ -180,10 +257,10 @@ func (o *outsideActs) take(machine string) ([]outside.Cond, bool) { o.mu.Lock() o.reading[machine] = false if err != nil { - o.logf("the conditions of %s are not read (%v): the move waits on %s", machine, err, outside.OnUnread) + o.logf("the conditions of %s are not read (%v): its waits stand on %s until a judging on an input reads again", machine, err, outside.OnUnread) } else { - o.answered[machine] = conds - o.dirty[machine] = true // the kept call answered: the move is judged by it + o.answered[machine] = &machineAnswer{conds: conds, at: o.now()} + o.dirty.set(machine) // the kept call answered: every wait on the machine is judged by it } o.mu.Unlock() if err == nil { @@ -227,11 +304,11 @@ func (p *passer) outsideReview(ctx context.Context, module, fp string, nodes []s running := runs && run.Fingerprint == fp && !run.Stopped // §827: the node-engine seat's last running names the wait's fingerprint sig := fmt.Sprintf("%s|%s|%s|%t", h.Fp, h.Phase, h.Health, running) o.mu.Lock() - first := !o.started + first := o.first changed := o.sigs[node+"."+module] != sig o.sigs[node+"."+module] = sig - dirty := o.dirty[node] - cleared := prev != nil && prev.Cause != "" && o.cleared[node+"|"+prev.Cause] + dirty := o.dirty.is(node) + cleared := prev != nil && prev.Cause != "" && o.cleared.is(node+"|"+prev.Cause) o.mu.Unlock() failing, healthy := ok && h.Fp == fp && assign.Failed(h), n.RunsHealthy(node, module, fp) if !failing && !healthy && prev == nil { @@ -256,16 +333,15 @@ func (p *passer) outsideReview(ctx context.Context, module, fp string, nodes []s } else { f := outside.Facts{Now: now, Failing: failing, Healthy: healthy, Node: node, KeeperSelf: keeperSelf, Bound: o.bound, Settle: o.settle, Moved: func(owner string) bool { return moved(owner, node) }, CauseCleared: cleared, Running: running} - if failing && !keeperSelf { - f.Conds, f.Read = o.take(node) + if failing && !keeperSelf && (prev == nil || prev.Decided == "") { // a decided record reads nothing (§829 3.) + settleEnd := prev != nil && prev.Settling && !now.Before(prev.SettleEnds) + since := now + if prev != nil { + since = prev.Since + } + f.Conds, f.Read = o.take(node, since, settleEnd) } d = outside.Judge(prev, node, module, fp, f) - o.mu.Lock() - delete(o.dirty, node) - if prev != nil { - delete(o.cleared, node+"|"+prev.Cause) - } - o.mu.Unlock() } switch { case d.Waiting: @@ -275,9 +351,6 @@ func (p *passer) outsideReview(ctx context.Context, module, fp string, nodes []s } p.outsideApply(ctx, key, hex, node, module, fp, prev, d, now, running) } - o.mu.Lock() - o.started = true - o.mu.Unlock() if len(override) == 0 { return n } 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 550594682..c4714d943 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 @@ -15,6 +15,7 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" 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" "mesh-delivery-assigner/internal/assign" "mesh-delivery-assigner/internal/buckets" @@ -87,9 +88,11 @@ func (r *outsideRig) review(keeperSelf bool, moved func(string, string) bool) as if moved == nil { moved = func(string, string) bool { return false } } + r.p.outside.beginPass(r.p.clock) out := r.p.outsideReview(context.Background(), "nats", fpTest, []string{"ace"}, n, keeperSelf, moved) select { case <-r.woke: // the read was answered: the next pass judges by it + r.p.outside.beginPass(r.p.clock) out = r.p.outsideReview(context.Background(), "nats", fpTest, []string{"ace"}, n, keeperSelf, moved) case <-time.After(50 * time.Millisecond): } @@ -264,6 +267,54 @@ func TestTheWaitOnAnOutsideCause(t *testing.T) { } } }) + t.Run("one read of a machine's conditions serves every wait judged on it in one pass, a decided one reads nothing, a settle's end uses the newest answer (§829)", func(t *testing.T) { + both := func(r *outsideRig) assign.Nodes { + n := assign.Nodes{Online: map[string]bool{"ace": true}, Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"ace": {}}} + for _, m := range []string{"nats", "web", "db"} { + n.Haves["ace"][m] = nodeengine.HaveByNodeModuleValue{Fp: fpTest, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthFailed} + } + return n + } + judge := func(r *outsideRig, modules ...string) { + r.p.outside.beginPass(r.p.clock) + for _, m := range modules { + r.p.outsideReview(context.Background(), m, fpTest, []string{"ace"}, both(r), false, func(string, string) bool { return false }) + } + } + r := newOutsideRig(t) + r.cond("machine.hub.unreachable", "ace") + hex, _ := catalogkey.KeyPart(fpTest) + decided, _ := outside.Encode(outside.Wait{Node: "ace", Module: "db", FP: fpTest, Since: r.now, ClockFrom: r.now, Decided: outside.HowGoOn}) + r.p.w.put(buckets.Waits(), outside.Key("ace", "db", hex), decided) + judge(r, "nats", "web", "db") // the first judgings start the one read + <-r.woke + judge(r, "nats", "web", "db") // the pass the answer wakes: every wait on the machine is judged by it + if r.reads != 1 || len(r.events) != 2 || r.bodies[0].On != "unreachable:hub" || r.bodies[1].On != "unreachable:hub" { + t.Fatalf("two waits and a decided one, one pass: %d reads, events %v bodies %+v", r.reads, r.events, r.bodies) + } + // a settle's end: no cause open, the answer read in it ends own-failure, never on conditions-unread + r2 := newOutsideRig(t) + judge(r2, "nats") // settle starts, read starts + <-r2.woke + judge(r2, "nats") // the answer: no cause open, a settle + r2.now = r2.now.Add(91 * time.Second) + judge(r2, "nats") // the settle's end: the answer read before it was received after the failing check and no condition came since + if r2.reads != 1 || r2.wait().Decided != outside.HowOwnFailure { + t.Fatalf("the settle's end read again (%d reads) or did not end own-failure: %+v", r2.reads, r2.wait()) + } + // a restart: the answer is gone, a settle's end starts a read + r3 := newOutsideRig(t) + judge(r3, "nats") + <-r3.woke + judge(r3, "nats") + r3.p.outside = newOutsideActs(r3.p.outside.call, r3.p.outside.wake, func(string, ...any) {}, 30*time.Minute, 90*time.Second) + r3.now = r3.now.Add(91 * time.Second) + judge(r3, "nats") + <-r3.woke // the read it started is answered + if r3.reads != 2 { + t.Errorf("a settle's end after a restart did not start a read: %d reads", r3.reads) + } + }) t.Run("past the bound the operator is asked; Go on counts it healthy and Count as failed holds it, each said ended", func(t *testing.T) { for how, want := range map[string]string{assign.OptionGoOn: nodeengineclient.HealthOK, assign.OptionFailed: nodeengineclient.HealthFailed} { r := newOutsideRig(t) 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 eefa9eee5..fe64b4ecd 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -167,6 +167,9 @@ func (p *passer) pass(ctx context.Context) { p.wakeSet("owed", p.owedNext()) p.logf("the pass took %s", time.Since(start).Round(time.Millisecond)) // design 47 H-A-259 }() + if p.outside != nil { + p.outside.beginPass(p.clock) + } p.sayOwed() watch.in(phaseAssignmentsSeat) p.assignmentsSeat(ctx)