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 c177afd98..52bc05df1 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -321,6 +321,9 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return err } if err := hearStepProgress(heard, func(onSeat, ask string) error { + if hearer.LateRunAsk("step-progress", ask) { + return nil // an ended run's ask: acknowledged at once, nothing read but its ask key and run (H-A-500 (a)) + } progressed(clock, onSeat, ask, logger.Printf) // the ask's bound is armed again from now (H-A-395) return hearer.RunProgress(ask) }, logger.Printf); err != nil { @@ -333,6 +336,9 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return err } taken := func(ask, node string, at time.Time) (*time.Time, error) { + if hearer.LateRunAsk("ask-taken", ask) { + return nil, nil // an ended run's ask: acknowledged at once, nothing read but its ask key and run (H-A-500 (a)) + } takenAt, err := hearer.Taken(ask, node, at) if err != nil { return nil, err diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go index c59cb63d6..775a1e897 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/testruns.go @@ -166,6 +166,39 @@ func (t testRunsKV) Has(key string) (bool, error) { return err == nil, err } +// askValue is an ask key's value: the key of the run the ask is of (H-A-500 addendum (1)). +type askValue struct { + Run string `json:"run"` +} + +// MarkAsk creates the ask key of ask holding run; one written already is kept. +func (t testRunsKV) MarkAsk(ask, run string) error { + raw, err := json.Marshal(askValue{Run: run}) + if err != nil { + return err + } + if _, err := t.kv.Create(t.ctx, pipeline.AskKey(ask), raw); err != nil && !errors.Is(err, jetstream.ErrKeyExists) { + return err + } + return nil +} + +// RunOfAsk reads the ask key of ask: the run it holds. +func (t testRunsKV) RunOfAsk(ask string) (string, bool, error) { + e, err := t.kv.Get(t.ctx, pipeline.AskKey(ask)) + if errors.Is(err, jetstream.ErrKeyNotFound) { + return "", false, nil + } + if err != nil { + return "", false, err + } + var v askValue + if err := json.Unmarshal(e.Value(), &v); err != nil || v.Run == "" { + return "", false, fmt.Errorf("the ask key of ask %s holds no run: %v", ask, err) + } + return v.Run, true, nil +} + func (t testRunsKV) Keys() ([]string, error) { all, err := t.kv.Keys(t.ctx) if errors.Is(err, jetstream.ErrNoKeysFound) { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/declined.go b/modules/mesh-delivery-pipeline/internal/pipeline/declined.go index ea3fbd385..1cdd6b43e 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/declined.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/declined.go @@ -96,6 +96,7 @@ func (h *Hearer) putDeclined(key, old string, s seat.StepResult, a Asked, ev Ste } else if err != nil { return fmt.Errorf("the test run %s is not written: %w", a.Run, err) } + markAsk(h.Clock, a.ID, a.Run, h.logf) // H-A-500 addendum (1) break } } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/runends.go b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go index ac1ad1ddb..e2645135a 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/runends.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/runends.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "sync/atomic" "time" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" @@ -54,6 +55,77 @@ type RunClock interface { Has(key string) (bool, error) // Keys are test-runs' keys. Keys() ([]string, error) + // MarkAsk writes the ask key of ask (AskKey) holding run, create-only: one written already is kept (H-A-500 addendum (1)). + MarkAsk(ask, run string) error + // RunOfAsk reads the ask key of ask: the key of the run it holds; ok false when it is not written. + RunOfAsk(ask string) (run string, ok bool, err error) +} + +// AskPrefix begins the ask key of a test run's ask in test-runs, `ask.`, holding the run's key: written with each ask +// a run is given and deleted with the run, so an ask-taken or step-progress finds its run in one read and never lists the +// pipelines (design 47 H2 H-A-500 addendum (1)). +const AskPrefix = "ask." + +// AskKey is the ask key of ask. +func AskKey(ask string) string { return AskPrefix + ask } + +// IsRunAsk says whether a key of test-runs is an ask key rather than a run. +func IsRunAsk(key string) bool { return strings.HasPrefix(key, AskPrefix) } + +// markAsk writes the ask key of a run's ask; one not written is said, and the ask's events are then found by the pipelines. +func markAsk(clock RunClock, ask, run string, logf func(string, ...any)) { + if clock == nil || ask == "" || run == "" { + return + } + if err := clock.MarkAsk(ask, run); err != nil && logf != nil { + logf("the ask key of ask %s of the test run %s is not written, so its events are found by the pipelines: %v", ask, run, err) + } +} + +// unmarkAsks deletes, with the run under run, every ask key holding it (H-A-500 addendum (1)): the ask it names now and each +// it was given before (a rerun, a declined step asked again). A key not read or not deleted is said. +func (h *Hearer) unmarkAsks(run string) { + all, err := h.Clock.Keys() + if err != nil { + h.logf("the ask keys of the deleted test run %s are not listed, so they are kept: %v", run, err) + return + } + for _, k := range all { + if !IsRunAsk(k) { + continue + } + if held, ok, err := h.Clock.RunOfAsk(strings.TrimPrefix(k, AskPrefix)); err != nil || !ok || held != run { + continue + } + if err := h.Clock.Unbound(k); err != nil { + h.logf("the ask key %s of the deleted test run %s is not deleted: %v", k, run, err) + } + } +} + +// LateEveryLine is how many late events of ended runs' asks one journal line says (H-A-500 (a)). +const LateEveryLine = 100 + +// LateRunAsk says whether ask is the ask of a test run that has ended, or whose run was asked again under another ask +// since, read from its ask key and its run, never from the pipelines (design 47 H2 H-A-500 (a) and its addenda): such an +// event is acknowledged at once and moves nothing, which replaces H-A-311 (2)'s placing and H-A-238 (2)'s clearing for +// it. One journal line says every LateEveryLine of them. An ask with no ask key, or whose run is gone, is not judged here. +func (h *Hearer) LateRunAsk(event, ask string) bool { + if h.TestRuns == nil || h.Clock == nil || ask == "" { + return false + } + run, ok, err := h.Clock.RunOfAsk(ask) + if err != nil || !ok { + return false + } + r, _, err := h.TestRuns.Get(run) + if err != nil || (r.Ended == nil && r.Ask == ask) { + return false + } + if n := atomic.AddInt64(&h.lateRuns, 1); n%LateEveryLine == 0 { + h.logf("%d late events of ended test runs' asks acknowledged at once, moving nothing; the last the %s of ask %s of the test run %s (H-A-500)", n, event, ask, run) + } + return true } // ParseRunBound reads a key of test-runs as a run's bound: the run's key and whether it is the untaken bound. @@ -84,6 +156,13 @@ func (h *Hearer) runAndPipelineOfAsk(ask string) (string, string, bool) { if h.TestRuns == nil || ask == "" { return "", "", false } + // The ask key names the run in one read (H-A-500 addendum (1)); the pipeline is then left unnamed, the run's readers + // naming every pipeline that reads it, the asking one among them. + if h.Clock != nil { + if run, ok, err := h.Clock.RunOfAsk(ask); err == nil && ok { + return run, "", true + } + } pkeys, err := allKeys(h.Store) if err != nil { return "", "", false @@ -254,7 +333,7 @@ func (h *Hearer) TakeUp() (int, error) { ended := 0 var first error for _, run := range all { - if IsRunBound(run) { + if IsRunBound(run) || IsRunAsk(run) { continue } r, _, err := h.TestRuns.Get(run) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go index 293616bed..35156c372 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/steps.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/steps.go @@ -194,6 +194,9 @@ type Hearer struct { // queue (Starter.PutAgain); with either nil a declined step ends could not run. AskDeclined func(p seat.Pipeline, s seat.StepResult, now time.Time) (*Asked, error) PutAgain func(key string, a Asked) error + + // lateRuns counts the late events of ended runs' asks acknowledged at once (LateRunAsk, H-A-500 (a)). + lateRuns int64 } // EndReaders ends each of readers' steps of run with r's result (§725, §726): a step that read an ended run, or the steps diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go b/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go index a46e85e85..422badffb 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/testruns.go @@ -250,6 +250,8 @@ func (s *Starter) started(a Asked, write func() error) error { if err := write(); err != nil { return err } + // The run's ask gets its ask key, so its events find the run in one read (H-A-500 addendum (1)). + markAsk(s.Clock, a.ID, a.Run, s.logf) if s.Clock != nil { untaken := s.Runs.Untaken if untaken <= 0 { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/testruns_read.go b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_read.go index b725c2193..5595d64ac 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/testruns_read.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_read.go @@ -38,7 +38,7 @@ func (r RunsRead) TestRuns(in seat.TestRunsInput) (seat.TestRunsAnswer, error) { sort.Strings(all) a := seat.TestRunsAnswer{Runs: []seat.TestRun{}} for _, key := range all { - if IsRunBound(key) || (in.Candidate != "" && !strings.HasPrefix(key, in.Candidate+".")) { + if IsRunBound(key) || IsRunAsk(key) || (in.Candidate != "" && !strings.HasPrefix(key, in.Candidate+".")) { continue } raw, err := r.Runs.Value(key) @@ -165,6 +165,7 @@ func (v Verbs) TestRunsDrop(caller string, in seat.TestRunsDropInput) (seat.Test for _, k := range []string{BoundPrefix + in.Key, UntakenPrefix + in.Key} { _ = h.Clock.Unbound(k) } + h.unmarkAsks(in.Key) } gone := r.Answer() gone.Key = in.Key diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go index 0e8337406..8e79a9529 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/testruns_test.go @@ -20,6 +20,8 @@ type clockMem struct { asks map[string]string revs map[string]uint64 rev uint64 + // askRuns are the ask keys, each holding its run (H-A-500 addendum (1)). + askRuns map[string]string // before is called once at the next Bound, a write that comes between its caller's read and its write. before func() } @@ -40,7 +42,11 @@ func (c *clockMem) Bounded(key string) (string, uint64, bool, error) { _, ok := c.bound[key] return c.asks[key], c.revs[key], ok, nil } -func (c *clockMem) Unbound(key string) error { delete(c.bound, key); return nil } +func (c *clockMem) Unbound(key string) error { + delete(c.bound, key) + delete(c.askRuns, key) + return nil +} func (c *clockMem) UnboundAt(key string, rev uint64) error { if c.revs[key] == rev { delete(c.bound, key) @@ -48,6 +54,20 @@ func (c *clockMem) UnboundAt(key string, rev uint64) error { return nil } func (c *clockMem) Has(key string) (bool, error) { _, ok := c.bound[key]; return ok, nil } +func (c *clockMem) MarkAsk(ask, run string) error { + if _, ok := c.askRuns[AskKey(ask)]; ok { + return nil + } + if c.askRuns == nil { + c.askRuns = map[string]string{} + } + c.askRuns[AskKey(ask)] = run + return nil +} +func (c *clockMem) RunOfAsk(ask string) (string, bool, error) { + run, ok := c.askRuns[AskKey(ask)] + return run, ok, nil +} func (c *clockMem) Keys() ([]string, error) { var out []string for k := range c.runs { @@ -56,6 +76,9 @@ func (c *clockMem) Keys() ([]string, error) { for k := range c.bound { out = append(out, k) } + for k := range c.askRuns { + out = append(out, k) + } return out, nil } @@ -631,6 +654,44 @@ func TestNoWriteOfATestRunActsOnAReadBeforeItsLatestChange(t *testing.T) { t.Errorf("the late step-progress placed in %v, want %s's waiting movers placed", released, p.Pipeline) } }) + // H-A-500 (a) and its addenda: a late ask-taken or step-progress of an ended run's ask is judged from its ask key and + // its run alone, never the pipelines, one journal line per hundred; the ask key is deleted with the run. + t.Run("a late event of an ended run's ask reads no pipeline", func(t *testing.T) { + f := newRunFixture() + p := f.start(t, 4, 9) + unit := stepNamed(p, "app:program", "unit") + run := unit.Shared[0].Run + if got, ok, _ := f.clock.RunOfAsk(unit.Ask); !ok || got != run { + t.Fatalf("the ask key of %s holds %q, %v; want the run %s", unit.Ask, got, ok, run) + } + if f.h.LateRunAsk("step-progress", unit.Ask) { + t.Fatal("the ask of a running run is judged late") + } + if err := f.h.EndRun(run, seat.TestRun{Ask: unit.Ask, Outcome: seat.StepResultOutcomePassed}, nil); err != nil { + t.Fatal(err) + } + store := f.h.Store + f.h.Store, f.logged = nil, nil // a read of the pipelines would panic + for i := 0; i < 2*LateEveryLine; i++ { + if !f.h.LateRunAsk("ask-taken", unit.Ask) { + t.Fatalf("event %d of the ended run's ask is not judged late", i) + } + } + if f.h.LateRunAsk("ask-taken", "anaskofnorun") { + t.Error("an ask with no ask key is judged late") + } + if len(f.logged) != 2 || !strings.Contains(f.logged[1], "200 late events") { + t.Errorf("journal %q; want one line per hundred late events", f.logged) + } + f.h.Store = store + v := Verbs{Hearer: f.h, Starter: f.s, Acts: &actLog{}} + if _, err := v.TestRunsDrop("op@shanks", seat.TestRunsDropInput{Key: run, Why: "gone"}); err != nil { + t.Fatal(err) + } + if _, ok, _ := f.clock.RunOfAsk(unit.Ask); ok { + t.Error("the ask key is kept after its run is deleted") + } + }) // A run's end is written as its step-ended is heard, before the finding the step says (A-470o). t.Run("the run ends before the step's finding is written", func(t *testing.T) { f := newRunFixture()