diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go index df3617cd5..dedbddff3 100644 --- a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/main.go @@ -649,6 +649,12 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro hear(meshcatalog.StreamRegistrationChanged, "a newly watched branch's source revisions are not taken in until the indexer starts again", func(m jetstream.Msg) { _ = m.Ack() sources.refresh() + // A module that left the registrations, or a branch that left its registration, frees its failed record (H-A-572 (2)). + if registered, err := registry.RegisteredFailedRecords(ctx); err != nil { + logger.Printf("the failed records are not freed after a registration changed: %v", err) + } else if err := ix.FreeFailedRecords(ctx, registered); err != nil { + logger.Printf("the failed records are not freed after a registration changed: %v", err) + } }) // The held take-ins are taken in again from the state held at the start, before a branch's consumer gives a later one // (design 47 H, H-A-114, H-A-118); a chain's key a stop left is deleted first (H2 H-A-497 addendum). diff --git a/modules/mesh-delivery-indexer/internal/index/pins.go b/modules/mesh-delivery-indexer/internal/index/pins.go index eb29829b2..48cc0b60a 100644 --- a/modules/mesh-delivery-indexer/internal/index/pins.go +++ b/modules/mesh-delivery-indexer/internal/index/pins.go @@ -22,7 +22,8 @@ import ( // The indexer keeps the records pinning each catalog graph revision and removes a revision only on an event (design 47 G §153, // §178, §189, §215, §216, §579, A-169, A-171): its state pins holds one key per revision, `.` (the watched branch // and the revision's number, the branch escaped as a name is), whose value lists the records that pin it: a take-in's, each -// running pipeline's (design 47 H §743, §749) and each passed pipeline's release until a newer one is the want (H-A-168, H-A-170). +// running pipeline's (design 47 H §743, §749), each passed pipeline's release until a newer one is the want (H-A-168, H-A-170), +// and each failed pipeline's mover that did not pass until it passes or a newer revision moves it (H2 H-A-565, H-A-566). const ( // PinsState is the state's name; PinsCapMiB is its cap in the manifest (about 1,800 keys of about 0.2 KiB). PinsState = "pins" @@ -74,7 +75,9 @@ func pinObject(key string) (string, bool) { // HeardPins applies one event of the seat mesh-pipeline-runner the indexer's durable consumers gave it (design 47 H §749): a // pipeline-started pins the revision it names, and each revision its movers are kept at (design 47 H2 H-A-496), under the -// record `pipeline.`, its pipeline-ended frees it and every other revision the record pins (H-A-147). A body +// record `pipeline.`, its pipeline-ended frees it and every other revision the record pins (H-A-147); a failed +// one first pins them under `failed..` for each mover that did not pass, freed when the mover passes in a +// later pipeline or a pipeline of a newer revision moves it (H-A-565 (1), H-A-566). A body // that is no such event, or names no revision, changes nothing; an error leaves the event unacknowledged, so the bus gives it // again; one applied twice changes nothing. func (ix *Indexer) HeardPins(ctx context.Context, started *runner.PipelineStarted, ended *runner.PipelineEnded) error { @@ -94,6 +97,16 @@ func (ix *Indexer) HeardPins(ctx context.Context, started *runner.PipelineStarte if err := ix.pinKept(ctx, started.Branch, started.Revision, started.Kept, record); err != nil { return err } + // A delivering pipeline of a newer revision that moves a mover a failed pipeline left, carried or at a newer + // fingerprint, frees that mover's failed record from every older revision of the branch: its own pin covers it now + // (design 47 H2 H-A-565 (1), H-A-566). + if started.Run == "" { + for _, m := range started.Movers { + if err := ix.unpinOlder(ctx, started.Branch, started.Revision, FailedRecord(started.Branch, m.Module)); err != nil { + return err + } + } + } // The reservations of the pipeline's revision settle at its start, if they gather (design 47 H2 §805, H-A-424 (1)). if err := ix.Settle(ctx, BranchObject(started.Branch, started.Revision)); err != nil { ix.logf("versions: the reservations of %s are not re-pinned at its pipeline-started: %v", BranchObject(started.Branch, started.Revision), err) @@ -125,6 +138,24 @@ func (ix *Indexer) HeardPins(ctx context.Context, started *runner.PipelineStarte } } record := PipelineRecord(ended.Pipeline) + if ended.Run == "" { + // A mover that passed frees its failed record from every older revision of the branch; a delivering pipeline that + // ended failed keeps its own revision and each kept revision its record pins under failed.. for + // each mover that did not pass, before its own record is freed, so a rerun or a carry still reads them (design 47 + // H2 H-A-565 (1), H-A-566). + for _, m := range ended.Movers { + if ended.Outcome == runner.PipelineEndedOutcomePassed || moverPassed(m) { + if err := ix.unpinOlder(ctx, ended.Branch, ended.Revision, FailedRecord(ended.Branch, m.Module)); err != nil { + return err + } + } + } + if ended.Outcome == runner.PipelineEndedOutcomeFailed || ended.Outcome == runner.PipelineEndedOutcomeCancelled { + if err := ix.pinFailed(ctx, ended, record); err != nil { + return err + } + } + } if err := ix.unpin(ctx, PinKey(ended.Branch, ended.Revision), record); err != nil { return err } @@ -139,6 +170,212 @@ func ReleaseRecord(branch, module string) string { return "release." + keys.EscapeName(branch) + "." + keys.EscapeName(module) } +// FailedRecord is the record a failed pipeline pins its own and its kept revisions under, one per mover that did not pass: +// `failed..`, each name escaped as a key's name is (design 47 H2 H-A-565 (1), H-A-566). +func FailedRecord(branch, module string) string { + return "failed." + keys.EscapeName(branch) + "." + keys.EscapeName(module) +} + +// moverPassed says whether a mover of a pipeline-ended passed: it holds steps and each required one passed, was carried or +// had nothing to run, as the runner counts a step passed. +func moverPassed(m runner.MoverEnded) bool { + if len(m.Steps) == 0 { + return false + } + for _, s := range m.Steps { + if s.Status != runner.StepResultStatusRequired { + continue + } + switch s.Outcome { + case runner.StepResultOutcomePassed, runner.StepResultOutcomeCarried, runner.StepResultOutcomeNothingToRun: + default: + return false + } + } + return true +} + +// movedOut says whether a mover of a pipeline-ended left it for a newer revision: a required step of it is superseded. +func movedOut(m runner.MoverEnded) bool { + return slices.ContainsFunc(m.Steps, func(s runner.StepResult) bool { + return s.Status == runner.StepResultStatusRequired && s.Outcome == runner.StepResultOutcomeSuperseded + }) +} + +// pinFailed pins, for each mover a pipeline-ended that failed or was cancelled did not deliver (a mover with a required +// step that did not pass, and each module its `unfinished` names, not one moved out of it for a newer revision), the revision +// the mover's entry comes from and that revision's `base`, under the mover's failed record: the kept revision the mover's +// entry is at, else the pipeline's own (design 47 H2 H-A-566, H-A-572 (1), H-A-595, H-A-608). At most two revisions per record. +func (ix *Indexer) pinFailed(ctx context.Context, ended *runner.PipelineEnded, record string) error { + var failed []string + fingerprint := map[string]string{} + for _, m := range ended.Movers { + fingerprint[m.Module] = m.Fingerprint + if !moverPassed(m) && !movedOut(m) { + failed = append(failed, m.Module) + } + } + for _, module := range ended.Unfinished { + if !slices.Contains(failed, module) { + failed = append(failed, module) + } + } + if len(failed) == 0 { + return nil + } + slices.Sort(failed) + pinKeys, err := ix.Pins.Keys(ctx) + if err != nil { + return err + } + slices.Sort(pinKeys) + var kept []int64 // the revisions the pipeline's record pins above its own: where its movers are kept + for _, k := range pinKeys { + b, r, ok := ParsePinKey(k) + if !ok || b != ended.Branch || r <= ended.Revision { + continue + } + v, _, found, err := ix.Pins.Get(ctx, k) + if err != nil { + return err + } + var pv PinValue + if found && json.Unmarshal(v, &pv) == nil && slices.Contains(pv.Records, record) { + kept = append(kept, r) + } + } + slices.Reverse(kept) // the newest first + for _, module := range failed { + at := ended.Revision + for _, r := range kept { + if ix.entryAt(ctx, ended.Branch, r, module) == fingerprint[module] && fingerprint[module] != "" { + at = r + break + } + } + revisions := []string{PinKey(ended.Branch, at)} + if base := ix.baseOf(ctx, ended.Branch, at); base > 0 && base != at { + revisions = append(revisions, PinKey(ended.Branch, base)) + } + for _, k := range revisions { + if err := ix.pin(ctx, k, FailedRecord(ended.Branch, module)); err != nil { + // A write the full state refuses is said, and the event is left unacknowledged so the bus gives it again at its + // kept-call pace (H-A-572 (1)). + ix.logf("pins: %s is not written to %s, so %s is heard again: %v", FailedRecord(ended.Branch, module), k, ended.Pipeline, err) + return err + } + } + ix.logf("pins: %s ended %s: %s keeps %s until it passes in a later pipeline or a newer revision moves it (H-A-566, H-A-608)", + ended.Pipeline, ended.Outcome, FailedRecord(ended.Branch, module), strings.Join(revisions, ", ")) + } + return nil +} + +// entryAt is the fingerprint of module's entry in revision r of branch, empty when the revision or the entry is not read. +func (ix *Indexer) entryAt(ctx context.Context, branch string, r int64, module string) string { + _, entries, err := ReadRevision(ctx, ix.Revisions, RevisionObject(meshcatalog.CatalogRevision{Branch: branch, Revision: r})) + if err != nil { + return "" + } + for _, e := range entries { + if e.Module == module { + return e.Fp + } + } + return "" +} + +// baseOf is the revision revision r of branch names as its base (H-A-151), zero when it names none or is not read. +func (ix *Indexer) baseOf(ctx context.Context, branch string, r int64) int64 { + rev, _, err := ReadRevision(ctx, ix.Revisions, RevisionObject(meshcatalog.CatalogRevision{Branch: branch, Revision: r})) + if err != nil { + return 0 + } + return rev.Base +} + +// FreeFailedRecords frees every failed record (`failed..`) from every pin that holds it when no registration +// of its module names its branch any more: the module left the registrations, or, until a change of a module's branch is a +// take-in of that branch's head for that module alone, its branch left the module's registration. registered says the +// registrations as a set of records (design 47 H2 H-A-572 (2), heard on registration-changed, ADR 0666 section 2). +func (ix *Indexer) FreeFailedRecords(ctx context.Context, registered map[string]bool) error { + pinKeys, err := ix.Pins.Keys(ctx) + if err != nil { + return err + } + for _, k := range pinKeys { + v, _, found, err := ix.Pins.Get(ctx, k) + if err != nil { + return err + } + var pv PinValue + if !found || json.Unmarshal(v, &pv) != nil { + continue + } + for _, record := range pv.Records { + if strings.HasPrefix(record, "failed.") && !registered[record] { + if err := ix.unpin(ctx, k, record); err != nil { + return err + } + ix.logf("pins: %s is freed from %s: its module is no longer registered on that branch (H-A-572 (2))", record, k) + } + } + } + return nil +} + +// RegisteredFailedRecords are the failed records every registration of the registry names, one per module and branch. +func (r *Registry) RegisteredFailedRecords(ctx context.Context) (map[string]bool, error) { + all, err := r.Registrations.Keys(ctx) + if err != nil { + return nil, err + } + out := map[string]bool{} + for _, k := range all { + v, _, found, err := r.Registrations.Get(ctx, k) + if err != nil { + return nil, err + } + var reg meshcatalog.Registration + if !found || json.Unmarshal(v, ®) != nil { + continue + } + if reg.Module == "" { + return nil, fmt.Errorf("the registration %s names no module, so no failed record is freed by this read", k) + } + for _, b := range reg.Branches { + out[FailedRecord(b, reg.Module)] = true + } + } + return out, nil +} + +// unpinOlder frees record from every revision of branch older than revision (H-A-566). +func (ix *Indexer) unpinOlder(ctx context.Context, branch string, revision int64, record string) error { + pinKeys, err := ix.Pins.Keys(ctx) + if err != nil { + return err + } + for _, k := range pinKeys { + b, r, ok := ParsePinKey(k) + if !ok || b != branch || r >= revision { + continue + } + v, _, found, err := ix.Pins.Get(ctx, k) + if err != nil { + return err + } + var pv PinValue + if !found || json.Unmarshal(v, &pv) != nil || !slices.Contains(pv.Records, record) { + continue + } + if err := ix.unpin(ctx, k, record); err != nil { + return err + } + } + return nil +} + // HeardPipelineWanted applies one pipeline-wanted of the seat mesh-wants, said when a pipeline's wants are written to the end // (design 47 H §751, H-A-168, H-A-170, H-A-173, H-A-174). Outcome running: a module whose release record the pipeline's own // kept revision holds released its newer version there, and that is the want everywhere, so the module's record is freed from diff --git a/modules/mesh-delivery-indexer/internal/index/pins_test.go b/modules/mesh-delivery-indexer/internal/index/pins_test.go index f8c203b0b..706fe6ebe 100644 --- a/modules/mesh-delivery-indexer/internal/index/pins_test.go +++ b/modules/mesh-delivery-indexer/internal/index/pins_test.go @@ -52,6 +52,58 @@ func TestAPipelinesKeptRevisionsArePinnedUntilItEnds(t *testing.T) { heard(&runner.PipelineStarted{Branch: "main", Pipeline: "main.1", Revision: 1, Kept: []int64{3, 4}}, nil, "main.1", "main.3", "main.4", "main.6") heard(nil, &runner.PipelineEnded{Branch: "main", Pipeline: "main.1", Revision: 1, Outcome: runner.PipelineEndedOutcomeFailed}, "main.6") + + // H-A-565 (1), H-A-566: a failed delivering pipeline keeps its own and its kept revisions under failed.main. for + // each mover that did not pass (app failed, lib never ran; ok passed, gone was superseded); a pipeline of a newer + // revision moving lib frees lib's, app passing in a later pipeline frees app's, and only then the revisions go. + pending = 1 + for source := int64(7); source <= 10; source++ { + e := reader + e.Commit = fmt.Sprintf("c%d", source) + if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, nil); err != nil { + t.Fatal(err) + } + } + pending = 0 + step := func(outcome string) []runner.StepResult { + return []runner.StepResult{{Step: runner.StepResultStepUnit, Status: runner.StepResultStatusRequired, Outcome: outcome}} + } + heard(&runner.PipelineStarted{Branch: "main", Pipeline: "main.7", Revision: 7, Kept: []int64{8}}, nil, "main.10", "main.7", "main.8") + heard(nil, &runner.PipelineEnded{Branch: "main", Pipeline: "main.7", Revision: 7, Outcome: runner.PipelineEndedOutcomeFailed, + Movers: []runner.MoverEnded{{Module: "app", Steps: step(runner.StepResultOutcomeFailed)}, {Module: "lib", Steps: step(runner.StepResultOutcomeNotAsked)}, + {Module: "ok", Steps: step(runner.StepResultOutcomePassed)}, {Module: "gone", Steps: step(runner.StepResultOutcomeSuperseded)}}}, + "main.10", "main.7") + for _, k := range []string{"main.7"} { + if v, _, _, _ := ix.Pins.Get(ctx, k); !slices.Equal(mustRecords(t, v), []string{"failed.main.app", "failed.main.lib"}) { + t.Errorf("the pin of %s holds %s, want app's and lib's failed records", k, v) + } + } + heard(&runner.PipelineStarted{Branch: "main", Pipeline: "main.10", Revision: 10, Movers: []runner.StartedMover{{Module: "lib"}}}, nil, + "main.10", "main.7") + if v, _, _, _ := ix.Pins.Get(ctx, "main.7"); !slices.Equal(mustRecords(t, v), []string{"failed.main.app"}) { + t.Errorf("the pin of main.7 holds %s after main.10 moved lib, want app's failed record alone", v) + } + heard(nil, &runner.PipelineEnded{Branch: "main", Pipeline: "main.10", Revision: 10, Outcome: runner.PipelineEndedOutcomeFailed, + Movers: []runner.MoverEnded{{Module: "app", Steps: step(runner.StepResultOutcomePassed)}, {Module: "lib", Steps: step(runner.StepResultOutcomePassed)}}}, + "main.10") + + // H-A-595, H-A-608: a cancelled end pins as a failed one does, for a mover its `unfinished` names as for one whose step + // failed, so the revision its entry comes from stays after the pipeline's end. + pending = 1 + for source := int64(11); source <= 12; source++ { + e := reader + e.Commit = fmt.Sprintf("c%d", source) + if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, nil); err != nil { + t.Fatal(err) + } + } + pending = 0 + heard(&runner.PipelineStarted{Branch: "main", Pipeline: "main.11", Revision: 11}, nil, "main.11", "main.12") + heard(nil, &runner.PipelineEnded{Branch: "main", Pipeline: "main.11", Revision: 11, Outcome: runner.PipelineEndedOutcomeCancelled, + Unfinished: []string{"carried"}}, "main.11", "main.12") + if v, _, _, _ := ix.Pins.Get(ctx, "main.11"); !slices.Equal(mustRecords(t, v), []string{"failed.main.carried"}) { + t.Errorf("the pin of main.11 holds %s after a cancelled end naming carried unfinished, want its failed record", v) + } } // Design 47 H §749 (G §579, A-169, A-171): the indexer pins a revision on a pipeline-started that names it and frees it on its @@ -227,6 +279,7 @@ func TestATakeInPinsTheBranchsPreviousRevisionForItsPipeline(t *testing.T) { unreleased bool // its mover released no version there, so nothing pins main.5 (H-A-173) then []int64 // sources taken in with no mover after the end released []string + failed []string // main.5's mover did not pass in the failed pipeline: its failed record keeps these (H-A-565 (1), H-A-566) }{ {name: "the previous revision kept and pinned", before: []int64{3, 4}, at: 3, whileRuns: []string{"main.2", "main.3"}, afterTheEnd: []string{"main.3"}, base: 2}, {name: "the newest kept before, whatever its source", before: []int64{2}, at: 2, whileRuns: []string{"main.1", "main.2"}, afterTheEnd: []string{"main.2"}, base: 1}, @@ -237,6 +290,8 @@ func TestATakeInPinsTheBranchsPreviousRevisionForItsPipeline(t *testing.T) { base: 1, passed: true, unreleased: true, then: []int64{6}, afterTheEnd: []string{"main.3"}}, {name: "a failed pipeline's revision goes once no longer current", before: []int64{4}, at: 2, whileRuns: []string{"main.1", "main.2"}, base: 1, then: []int64{6}, afterTheEnd: []string{"main.3"}}, + {name: "a failed pipeline's mover that did not pass keeps its revisions", before: []int64{4}, at: 2, whileRuns: []string{"main.1", "main.2"}, + base: 1, then: []int64{6}, afterTheEnd: []string{"main.1", "main.2", "main.3"}, failed: []string{"main.1", "main.2"}}, } { t.Run(tc.name, func(t *testing.T) { ix := &Indexer{Module: testSelf, Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings(), @@ -273,7 +328,11 @@ func TestATakeInPinsTheBranchsPreviousRevisionForItsPipeline(t *testing.T) { } } end := runner.PipelineEnded{Branch: "main", Pipeline: at, Revision: tc.at, Outcome: runner.PipelineEndedOutcomeFailed, - Movers: []runner.MoverEnded{{Module: "reader", Fingerprint: "f", Version: "0.1.5"}}} + Movers: []runner.MoverEnded{{Module: "reader", Fingerprint: "f", Version: "0.1.5", + Steps: []runner.StepResult{{Step: runner.StepResultStepUnit, Status: runner.StepResultStatusRequired, Outcome: runner.StepResultOutcomePassed}}}}} + if tc.failed != nil { + end.Movers[0].Steps[0].Outcome = runner.StepResultOutcomeFailed + } if tc.unreleased { end.Movers[0].Version = "" } @@ -296,6 +355,9 @@ func TestATakeInPinsTheBranchsPreviousRevisionForItsPipeline(t *testing.T) { if slices.Contains(tc.released, k) { want = []string{ReleaseRecord("main", "reader")} } + if slices.Contains(tc.failed, k) { + want = []string{FailedRecord("main", "reader")} + } if v, _, _, _ := ix.Pins.Get(ctx, k); !slices.Equal(append([]string{}, mustRecords(t, v)...), want) { t.Errorf("after the pipeline's end the pin of %s holds %s, want %v", k, v, want) } @@ -304,3 +366,20 @@ func TestATakeInPinsTheBranchsPreviousRevisionForItsPipeline(t *testing.T) { }) } } + +// Design 47 H2 H-A-572 (2): a failed record is freed from every pin when no registration of its module names its branch. +func TestAFailedRecordIsFreedWhenItsModuleLeavesTheRegistrations(t *testing.T) { + ctx := context.Background() + ix := &Indexer{Module: testSelf, Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings()} + for _, r := range []string{FailedRecord("main", "gone"), FailedRecord("main", "stays"), PipelineRecord("main.1")} { + if err := ix.pin(ctx, "main.3", r); err != nil { + t.Fatal(err) + } + } + if err := ix.FreeFailedRecords(ctx, map[string]bool{FailedRecord("main", "stays"): true}); err != nil { + t.Fatal(err) + } + if v, _, _, _ := ix.Pins.Get(ctx, "main.3"); !slices.Equal(mustRecords(t, v), []string{FailedRecord("main", "stays"), PipelineRecord("main.1")}) { + t.Errorf("the pin holds %s, want the registered module's failed record and the pipeline's alone", v) + } +}