From 602aeadec022f585b351d98fefa4bc761575a553 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 18:04:24 +0100 Subject: [PATCH] P3 carry-closed-groups, P4 take-in-carries: the movers a failed pipeline leaves go in closed groups, tied to a failed mover they await; a group joins the head's pipeline that waits or runs, said by mover-added; a take-in whose key is held adds its entries; a key names its carrier Builds: design 47 H2 H-A-581, H-A-594 (1) (2) (3) (4), H-A-607 (2), H-A-609 (3), H-A-610, H-A-645, H-A-646, H-A-649 --- .../mesh-delivery-pipeline-tools/main_test.go | 2 +- .../cmd/mesh-delivery-pipeline/main.go | 15 ++ .../internal/pipeline/carry.go | 152 +++++++++++ .../internal/pipeline/carry_test.go | 120 +++++++++ .../internal/pipeline/start.go | 22 ++ .../internal/pipeline/start_test.go | 7 +- .../internal/pipeline/unfinished.go | 242 +++++++++++++++++- 7 files changed, 554 insertions(+), 6 deletions(-) create mode 100644 modules/mesh-delivery-pipeline/internal/pipeline/carry.go create mode 100644 modules/mesh-delivery-pipeline/internal/pipeline/carry_test.go diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go index c8392a828..b35ce4f37 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go @@ -97,7 +97,7 @@ func TestTools(t *testing.T) { } // design 47 H2 §786: runner_unfinished reads each key and its entry; runner_unfinished_drop names why, drops nothing when // its hand act is not recorded, and drops the key once it is. - if got, err := tools[0].Run(nil); err != nil || fmt.Sprint(got) != "[{main.app {src2:aa 4 main.4 failed}}]" { + if got, err := tools[0].Run(nil); err != nil || fmt.Sprint(got) != "[{main.app {src2:aa 4 main.4 failed []}}]" { t.Errorf("runner_unfinished = %v (%v)", got, err) } for _, c := range []struct { 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 a7b5812fb..56ef3ffc5 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -264,6 +264,21 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro revise := func(ev catalog.CatalogRevisionEvent) (int, error) { return starter.Revised(ev, widener.Revision) } reviser.Store(&revise) starter.Store = store + // The movers a failed pipeline leaves are carried in closed groups (design 47 H2 H-A-594, H-A-607): their edges are read at + // the pipeline's revision, their ties end with the failed module's registration, and a group joins the head's pipeline. + unfinished.Entries = func(branch string, revision int64) (map[string]catalog.CatalogEntry, bool) { + half, err := widener.Revision(branch, revision) + return half.Entries, err == nil + } + unfinished.Pipeline = func(key string) (seat.Pipeline, bool) { p, _, err := store.Get(key); return p, err == nil } + unfinished.Registered = func(module string) bool { _, ok := rv.Registrations.Registration(module); return ok } + unfinished.Add = func(from seat.Pipeline, group []seat.Mover) (string, bool) { + half, ok := rv.Catalog.View(from.Branch) + if !ok { + return "", false + } + return starter.CarryToHead(from, group, half.Entries, rv.Catalog.View) + } // The queue (design 47 H §748): at most pipelines-at-once pipelines have their steps asked, the others wait in order in // the state queue, placed at the start from it and on every end. atOnce, waitingCap, err := queueSettings(getenv) diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/carry.go b/modules/mesh-delivery-pipeline/internal/pipeline/carry.go new file mode 100644 index 000000000..4c0c77a53 --- /dev/null +++ b/modules/mesh-delivery-pipeline/internal/pipeline/carry.go @@ -0,0 +1,152 @@ +package pipeline + +import ( + "errors" + "fmt" + "slices" + + runnerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient" + catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" + "git.novox.be/novox/mesh-sdk/go/keys" +) + +// AddedFrom says where a mover added to a pipeline came from; exactly one field is set (design 47 H2 H-A-645). +type AddedFrom struct { + // From is the key of the pipeline the mover came from. + From string + // Caller is the caller of start, as the runtime proved it. + Caller string + // TakeIn is the source revision of the take-in whose entries were added to a pipeline held already. + TakeIn int64 +} + +// openPipeline says whether p waits or runs: a delivering pipeline that has not ended. +func openPipeline(p seat.Pipeline) bool { + return p.Run == "" && p.Outcome == seat.PipelineOutcomeRunning && p.Ended == nil +} + +// AddMovers adds to the pipeline key, which waits or runs, each of movers it does not move yet, tiers them with the rest from +// entries, says one mover-added per mover added with the origin (H-A-594 (3), (4), H-A-609 (3)), and places their steps from +// the branch's view. It answers the modules added. +func (s *Starter) AddMovers(key string, movers []seat.Mover, entries map[string]catalog.CatalogEntry, from AddedFrom, view func(branch string) (Half, bool)) ([]string, error) { + if s.Store == nil { + return nil, errors.New("the runner reads no pipeline") + } + var p seat.Pipeline + var added []seat.Mover + for { + cur, rev, err := s.Store.Get(key) + if err != nil { + return nil, fmt.Errorf("the pipeline %s is not read: %w", key, err) + } + if !openPipeline(cur) { + return nil, fmt.Errorf("the pipeline %s neither waits nor runs: no mover is added", key) + } + added = added[:0] + for _, m := range movers { + if !Moves(cur, m.Module) { + added = append(added, m) + } + } + if len(added) == 0 { + return nil, nil + } + cur.Movers = Tiers(append(slices.Clone(cur.Movers), added...), entries, s.logf) + if _, err := s.Store.Update(key, cur, rev); errors.Is(err, ErrChanged) { + continue + } else if err != nil { + return nil, fmt.Errorf("the pipeline %s is not written: %w", key, err) + } + p = cur + break + } + var names []string + for _, m := range added { + names = append(names, m.Module) + if s.Say == nil { + continue + } + ev := seat.MoverAdded{Pipeline: p.Pipeline, Revision: p.Revision, Module: m.Module, Fingerprint: m.Fingerprint, From: from.From, Caller: from.Caller, TakeIn: from.TakeIn} + if err := s.Say(runnerclient.RevisionSubject(seat.EventMoverAdded, p.Branch, subjectRevision(p)), ev); err != nil { + s.logf("mover-added of %s in %s is not said: %v", m.Module, key, err) + } + } + s.logf("%s: %v added (%s)", key, names, from.describe()) + if view != nil { + if _, err := s.Waiting(s.Store, p.Branch, view); err != nil { + return names, err + } + } + return names, nil +} + +func (f AddedFrom) describe() string { + switch { + case f.From != "": + return "carried from " + f.From + case f.Caller != "": + return "started by " + f.Caller + } + return fmt.Sprintf("a take-in of source %d", f.TakeIn) +} + +// head is the key of the newest pipeline of branch, by its catalog revision, and the pipeline: the head's pipeline (H-A-594 (1)). +func (s *Starter) head(branch string) (seat.Pipeline, bool) { + pkeys, err := s.Store.Keys(keys.EscapeName(branch) + ".*") + if err != nil { + return seat.Pipeline{}, false + } + var best seat.Pipeline + found := false + for _, k := range pkeys { + p, _, err := s.Store.Get(k) + if err != nil || p.Run != "" || p.Branch != branch { + continue + } + if !found || p.Revision > best.Revision { + best, found = p, true + } + } + return best, found +} + +// CarryToHead adds the closed group of movers pipeline from left to the head's pipeline of its branch when that waits or runs, +// and takes the movers out of from's movers, so none of its later ends names them (H-A-594 (3), (4), H-A-607 (2)). It +// answers the head's key, false when nothing was added: the head is from itself or has ended. +func (s *Starter) CarryToHead(from seat.Pipeline, group []seat.Mover, entries map[string]catalog.CatalogEntry, view func(branch string) (Half, bool)) (string, bool) { + if s.Store == nil { + return "", false + } + head, ok := s.head(from.Branch) + if !ok || head.Pipeline == from.Pipeline || head.Revision < from.Revision || !openPipeline(head) { + return "", false + } + if _, err := s.AddMovers(head.Pipeline, group, entries, AddedFrom{From: from.Pipeline}, view); err != nil { + s.logf("the movers %v of %s are not added to %s: %v", moduleNames(group), from.Pipeline, head.Pipeline, err) + return "", false + } + for { + cur, rev, err := s.Store.Get(from.Pipeline) + if err != nil { + return head.Pipeline, true + } + cur.Movers = slices.DeleteFunc(slices.Clone(cur.Movers), func(m seat.Mover) bool { + return slices.ContainsFunc(group, func(g seat.Mover) bool { return g.Module == m.Module }) + }) + if _, err := s.Store.Update(from.Pipeline, cur, rev); errors.Is(err, ErrChanged) { + continue + } else if err != nil { + s.logf("%s's movers %v are not taken out after their carry: %v", from.Pipeline, moduleNames(group), err) + } + return head.Pipeline, true + } +} + +func moduleNames(movers []seat.Mover) []string { + var out []string + for _, m := range movers { + out = append(out, m.Module) + } + return out +} diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/carry_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/carry_test.go new file mode 100644 index 000000000..79e225a63 --- /dev/null +++ b/modules/mesh-delivery-pipeline/internal/pipeline/carry_test.go @@ -0,0 +1,120 @@ +package pipeline + +import ( + "encoding/json" + "slices" + "strings" + "testing" + + catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner" +) + +func edgeInto(entries map[string]catalog.CatalogEntry, to, from string, kind int64) { + e := entries[to] + e.Edges = append(e.Edges, catalog.Edge{From: catalog.ArtifactRef(from + ":x"), Kind: kind}) + entries[to] = e +} + +// design 47 H2 H-A-594 (2), H-A-646 (AC-P3b): a mover awaiting a failed one's package (kind 1) or build step (kind 3), directly or +// through another, is tied; one with only a tier edge is not; the tied group is closed under the tier kinds. +func TestTiedMoversAreThoseAwaitingAFailedOne(t *testing.T) { + p := seat.Pipeline{Pipeline: "main.5", Branch: "main", Outcome: seat.PipelineOutcomeFailed} + for _, m := range []string{"engine", "gitea", "far", "tieronly", "mate", "free"} { + p.Movers = append(p.Movers, seat.Mover{Module: m}) + } + entries := map[string]catalog.CatalogEntry{} + edgeInto(entries, "gitea", "engine", 1) // kind 1: awaits engine's package + edgeInto(entries, "far", "gitea", 3) // kind 3 through gitea + edgeInto(entries, "tieronly", "engine", 2) // delivery order only + edgeInto(entries, "mate", "far", 4) // tier edge to a tied mover: the group goes together + tied := tiedTo(p, []string{"gitea", "far", "tieronly", "mate", "free"}, []string{"engine"}, entries) + for _, m := range []string{"gitea", "far", "mate"} { + if !slices.Equal(tied[m], []string{"engine"}) { + t.Errorf("%s is tied to %v, want engine", m, tied[m]) + } + } + for _, m := range []string{"tieronly", "free"} { + if len(tied[m]) != 0 { + t.Errorf("%s is tied to %v, want none", m, tied[m]) + } + } + groups := tierGroups([]string{"tieronly", "free"}, entries) + if len(groups) != 2 { + t.Errorf("groups = %v, want each alone", groups) + } +} + +// design 47 H2 H-A-594 (3), (4), H-A-607 (2), H-A-609 (3): a group left joins the head's pipeline when that waits or runs, said by +// mover-added with from, its key naming the carrier and leaving the failed pipeline's movers; a take-in whose key is held adds +// its entries, one mover-added each with take-in; a carrier that ends otherwise leaves its key naming that pipeline. +func TestAClosedGroupJoinsTheHeadPipelineAndIsSaid(t *testing.T) { + mem := memStore{} + failed := seat.Pipeline{Pipeline: "main.5", Branch: "main", Revision: 5, Outcome: seat.PipelineOutcomeFailed, + Movers: []seat.Mover{{Module: "engine", Fingerprint: "f1"}, {Module: "gitea", Fingerprint: "f2"}, {Module: "free", Fingerprint: "f3"}}, + Steps: []seat.StepResult{{Module: "engine", Step: seat.StepResultStepUnit, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeFailed}, + {Module: "gitea", Step: seat.StepResultStepUnit, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeNotAsked}, + {Module: "free", Step: seat.StepResultStepUnit, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeNotAsked}}} + head := seat.Pipeline{Pipeline: "main.6", Branch: "main", Revision: 6, Outcome: seat.PipelineOutcomeRunning, Movers: []seat.Mover{{Module: "other", Fingerprint: "f9"}}} + mem["main.5"] = struct { + p seat.Pipeline + rev uint64 + }{failed, 1} + mem["main.6"] = struct { + p seat.Pipeline + rev uint64 + }{head, 1} + entries := map[string]catalog.CatalogEntry{} + edgeInto(entries, "gitea", "engine", 1) + type said struct { + subject string + ev seat.MoverAdded + } + var out []said + s := &Starter{Store: mem, Say: func(subject string, body any) error { + if ev, ok := body.(seat.MoverAdded); ok { + out = append(out, said{subject, ev}) + } + return nil + }} + st := memUnfinished{} + u := &Unfinished{Store: st, + Entries: func(string, int64) (map[string]catalog.CatalogEntry, bool) { return entries, true }, + Add: func(from seat.Pipeline, group []seat.Mover) (string, bool) { + return s.CarryToHead(from, group, entries, nil) + }} + u.Ended(failed) + if len(out) != 1 || out[0].ev.Module != "free" || out[0].ev.From != "main.5" || out[0].ev.Pipeline != "main.6" || !strings.HasSuffix(out[0].subject, ".main.6") { + t.Fatalf("mover-added = %+v, want free added to main.6 from main.5 alone: gitea is tied to engine", out) + } + if p, _, _ := mem.Get("main.6"); !Moves(p, "free") || !Moves(p, "other") { + t.Errorf("main.6 moves %+v, want other and free", p.Movers) + } + if p, _, _ := mem.Get("main.5"); Moves(p, "free") || !Moves(p, "gitea") { + t.Errorf("main.5 moves %+v, want free gone and the tied gitea kept", p.Movers) + } + var e UnfinishedEntry + _ = json.Unmarshal([]byte(st["main.free"]), &e) + if e.CarriedBy != "main.6" { + t.Errorf("free's key = %s, want carried by main.6", st["main.free"]) + } + // The carrier ends failed: the key stays, naming that pipeline, with no carrier. + ended := seat.Pipeline{Pipeline: "main.6", Branch: "main", Revision: 6, Outcome: seat.PipelineOutcomeFailed, + Movers: []seat.Mover{{Module: "free", Fingerprint: "f3"}}, + Steps: []seat.StepResult{{Module: "free", Step: seat.StepResultStepUnit, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeFailed}}} + u.Ended(ended) + e = UnfinishedEntry{} + _ = json.Unmarshal([]byte(st["main.free"]), &e) + if e.CarriedBy != "" || e.Pipeline != "main.6" { + t.Errorf("free's key = %s, want pipeline main.6 and no carrier", st["main.free"]) + } + // A take-in whose key is held adds its entries, one mover-added each, with take-in. + out = nil + half := Half{Revision: 6, Source: 9, Entries: map[string]catalog.CatalogEntry{"late": {Commit: "c", Fp: "f7"}}} + s.Unfinished = u + n, err := s.TakenIn(catalog.TakeInTaken{Branch: "main", Source: 9, Revision: 6, Entries: []catalog.TakeInEntry{{Module: "late", Commit: "c", Fingerprint: "f7"}}}, + func(string) (Half, bool) { return half, true }) + if err != nil || n != 0 || len(out) != 1 || out[0].ev.TakeIn != 9 || out[0].ev.From != "" || out[0].ev.Module != "late" { + t.Errorf("a take-in of a held key = %d (%v), said %+v, want late added with take-in 9", n, err, out) + } +} diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/start.go b/modules/mesh-delivery-pipeline/internal/pipeline/start.go index 3f672fc36..ba2795075 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/start.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/start.go @@ -1158,12 +1158,27 @@ func (s *Starter) TakenIn(ev catalog.TakeInTaken, view func(branch string) (Half s.logf("the take-in of %s source %d names no entries: it starts no pipeline", ev.Branch, ev.Source) return 0, nil } + // A take-in whose pipeline key is held already by a pipeline that waits or runs adds its entries to it as movers, one + // mover-added each with the take-in (H-A-594 (1), H-A-609 (3)). + if held && s.Store != nil && ev.Revision > 0 { + key := Key(ev.Branch, ev.Revision) + if cur, _, gerr := s.Store.Get(key); gerr == nil && openPipeline(cur) { + var entries []seat.Mover + for _, m := range movers { + entries = append(entries, m) + } + names, err := s.AddMovers(key, entries, half.Entries, AddedFrom{TakeIn: ev.Source}, view) + s.markCarried(ev.Branch, names, key) + return 0, err + } + } movers = Tiers(movers, half.Entries, s.logf) ok, err := s.Start(Begin{Branch: ev.Branch, Source: ev.Source, Call: ev.Call, Revision: ev.Revision, Movers: movers, Entries: half.Entries, unheld: !held}) started := 0 if ok { started = 1 + s.markCarried(ev.Branch, moduleNames(movers), Key(ev.Branch, keyRevision(Begin{Revision: ev.Revision, Source: ev.Source}))) } // The view was read before the start wrote: when the pipeline was held back for it and it has reached the revision since, // it is placed now, not left for an advance that already came (A-448, §200). @@ -1387,3 +1402,10 @@ func (s *Starter) Revised(ev catalog.CatalogRevisionEvent, read func(branch stri } return 0, err } + +// markCarried names the pipeline key as the carrier of the unfinished keys of modules it carries (H-A-594 (4)). +func (s *Starter) markCarried(branch string, modules []string, key string) { + if s.Unfinished != nil { + s.Unfinished.markCarried(branch, modules, key) + } +} diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go index cfd8622aa..4cfb2818d 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go @@ -470,13 +470,18 @@ func TestTakenInStartsOnlyItsEntries(t *testing.T) { if p, _, _ := mem.Get("main.13"); len(p.Movers) != 2 || !Moves(p, "moved") || !Moves(p, "batched") { t.Errorf("main.13 moves %+v, want moved and the unfinished batched", p.Movers) } - if u.String() != `dev.batched={"fingerprint":"src2:bb"} main.batched={"fingerprint":"src2:bb","revision":11}` { + if u.String() != `dev.batched={"fingerprint":"src2:bb"} main.batched={"fingerprint":"src2:bb","revision":11,"pipeline":"","outcome":"","carried-by":"main.13"}` { t.Errorf("the state unfinished = %s, want main.moved (moved at src2:aa) and main.gone (no entry) gone", u.String()) } if !strings.Contains(strings.Join(journal, "\n"), "revision 13 of main puts batched in a pipeline again") { t.Errorf("the journal says %q, want batched put in a pipeline again", journal) } half.Revision = 14 + // The key's carrier main.13 runs: the carry skips it (H-A-594 (4)); once the carrier ends otherwise, it names no carrier. + if n, err := s.TakenIn(catalog.TakeInTaken{Branch: "main", Source: 11, Revision: 14}, view); err != nil || n != 0 { + t.Fatalf("a take-in moving nothing started %d (%v) while the carrier runs, want none", n, err) + } + u["main.batched"] = `{"fingerprint":"src2:bb","revision":11}` if n, err := s.TakenIn(catalog.TakeInTaken{Branch: "main", Source: 11, Revision: 14}, view); err != nil || n != 1 { t.Fatalf("a take-in moving nothing started %d (%v), want one pipeline for the unfinished", n, err) } diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/unfinished.go b/modules/mesh-delivery-pipeline/internal/pipeline/unfinished.go index dfc20deb6..8746e02d3 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/unfinished.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/unfinished.go @@ -28,6 +28,12 @@ type UnfinishedEntry struct { Revision int64 `json:"revision"` Pipeline string `json:"pipeline"` Outcome string `json:"outcome"` + // CarriedBy is the pipeline that carries the mover now, cleared when it ends otherwise than passed (design 47 H2 H-A-594 + // (4)); the carry skips a key whose carrier is held. + CarriedBy string `json:"carried-by,omitempty"` + // Widens is what the mover's change widens in its first pipeline, kept so a carried or reopened pipeline says the same + // (H-A-595). + Widens []seat.Widening `json:"widens,omitempty"` } // UnfinishedStore is the state unfinished: a key's value (nil when it has none), a put, a delete and the keys matching a @@ -96,6 +102,14 @@ func ownStepsHeld(p seat.Pipeline, module string) bool { type Unfinished struct { Store UnfinishedStore Logf func(string, ...any) + // Entries are the entries of a branch's catalog revision, whose edges say which movers a failed one holds back (H-A-646); + // Pipeline reads a pipeline of the state pipelines; Registered says whether a module is registered still (H-A-610); Add + // puts a closed group of movers a pipeline left in the head's pipeline while that waits or runs, and answers its key + // (H-A-607 (2)). Nil hooks carry nothing at an end and tie nothing. + Entries func(branch string, revision int64) (map[string]catalog.CatalogEntry, bool) + Pipeline func(key string) (seat.Pipeline, bool) + Registered func(module string) bool + Add func(from seat.Pipeline, group []seat.Mover) (string, bool) } func (u *Unfinished) logf(format string, args ...any) { @@ -117,9 +131,106 @@ func (u *Unfinished) read(key string) (UnfinishedEntry, bool, error) { return e, true, nil } +// failedMovers are the movers of p with a required step that failed or could not run: each counts as failed (H-A-566, +// H-A-649). +func failedMovers(p seat.Pipeline) []string { + var out []string + for _, m := range p.Movers { + if len(failedSteps(p, m.Module)) > 0 { + out = append(out, m.Module) + } + } + return out +} + +// tiedTo are the modules in failed that left, of p, holds back: left has an awaited edge from them (kind 1, the package +// published, or kind 3, the build step passed), directly or through others (H-A-646), and so has every mover of its tier +// component, the group being closed under the tier kinds. The answer maps each tied mover to the failed modules it waits on. +func tiedTo(p seat.Pipeline, left, failed []string, entries map[string]catalog.CatalogEntry) map[string][]string { + root := map[string][]string{} + for changed := true; changed; { + changed = false + for _, m := range left { + for _, x := range awaited(p, m, entries) { + from := moduleOf(x.From) + var by []string + if slices.Contains(failed, from) { + by = []string{from} + } else { + by = root[from] + } + for _, f := range by { + if !slices.Contains(root[m], f) { + root[m], changed = append(root[m], f), true + } + } + } + } + } + for _, g := range tierGroups(left, entries) { + var by []string + for _, m := range g { + for _, f := range root[m] { + if !slices.Contains(by, f) { + by = append(by, f) + } + } + } + for _, m := range g { + if len(by) > 0 { + slices.Sort(by) + root[m] = by + } + } + } + return root +} + +// tierGroups are the connected components of modules under the delivery (tier) edges of their entries: the groups a carry +// keeps together (H-A-594 (2)). Each is sorted, the groups in the order of their first module. +func tierGroups(modules []string, entries map[string]catalog.CatalogEntry) [][]string { + parent := map[string]string{} + for _, m := range modules { + parent[m] = m + } + var find func(string) string + find = func(m string) string { + if parent[m] != m { + parent[m] = find(parent[m]) + } + return parent[m] + } + for _, m := range modules { + for _, x := range entries[m].Edges { + if from := moduleOf(x.From); tierKinds[x.Kind] && from != m && slices.Contains(modules, from) { + parent[find(from)] = find(m) + } + } + } + byRoot := map[string][]string{} + var order []string + for _, m := range modules { + r := find(m) + if _, ok := byRoot[r]; !ok { + order = append(order, r) + } + byRoot[r] = append(byRoot[r], m) + } + var out [][]string + for _, r := range order { + slices.Sort(byRoot[r]) + out = append(out, byRoot[r]) + } + slices.SortFunc(out, func(a, b []string) int { return strings.Compare(a[0], b[0]) }) + return out +} + // Ended takes a pipeline's end into the state unfinished (§786): a mover p leaves unfinished is kept under its key; a mover // whose pipeline ended passed, or whose own required step did not pass (it waits for its fingerprint to move), has its key -// deleted when the key names its fingerprint. A run on request's pipeline, and one every mover left, change nothing. +// deleted when the key names its fingerprint. A run on request's pipeline, and one every mover left, change nothing. A key a +// pipeline carried whose carrier ends otherwise than passed names that pipeline and no carrier (H-A-594 (4)). At an end +// failed, cancelled or could-not-run, the groups of the movers it leaves that no failed mover holds back are added at once +// to the head's pipeline when that waits or runs (H-A-607 (2)). func (u *Unfinished) Ended(p seat.Pipeline) { if u == nil || p.Run != "" { return @@ -136,7 +247,7 @@ func (u *Unfinished) Ended(p seat.Pipeline) { } key := UnfinishedKey(p.Branch, m.Module) if slices.Contains(left, m.Module) { - raw, _ := json.Marshal(UnfinishedEntry{Fingerprint: m.Fingerprint, Revision: p.Revision, Pipeline: p.Pipeline, Outcome: p.Outcome}) + raw, _ := json.Marshal(UnfinishedEntry{Fingerprint: m.Fingerprint, Revision: p.Revision, Pipeline: p.Pipeline, Outcome: p.Outcome, Widens: m.Widens}) if err := u.Store.Put(key, raw); err != nil { u.logf("%s ended %s and leaves %s unfinished, but it is not kept in the state unfinished: %v", p.Pipeline, p.Outcome, m.Module, err) continue @@ -152,18 +263,132 @@ func (u *Unfinished) Ended(p seat.Pipeline) { if !held || e.Fingerprint != m.Fingerprint { continue } + if p.Outcome != seat.PipelineOutcomePassed && e.CarriedBy == p.Pipeline { + e.CarriedBy, e.Pipeline, e.Outcome, e.Revision = "", p.Pipeline, p.Outcome, p.Revision + raw, _ := json.Marshal(e) + if err := u.Store.Put(key, raw); err != nil { + u.logf("%s: the unfinished entry of %s is not written: %v", p.Pipeline, m.Module, err) + } + continue + } if err := u.Store.Delete(key); err != nil { u.logf("%s: the unfinished entry of %s is not deleted: %v", p.Pipeline, m.Module, err) continue } u.logf("%s ended %s: %s is no longer unfinished", p.Pipeline, p.Outcome, m.Module) } + u.carryAtEnd(p, left) +} + +// carryAtEnd adds each closed group of the movers left to the head's pipeline (H-A-594 (2), (3), H-A-607 (2)), and names that +// pipeline as the carrier of each of its movers' keys. +func (u *Unfinished) carryAtEnd(p seat.Pipeline, left []string) { + if len(left) == 0 || u.Add == nil || u.Entries == nil { + return + } + entries, ok := u.Entries(p.Branch, p.Revision) + if !ok { + u.logf("%s: revision %d of %s is not read, so the movers it left are carried at the next take-in", p.Pipeline, p.Revision, p.Branch) + return + } + tied := tiedTo(p, left, failedMovers(p), entries) + var free []string + for _, m := range left { + if len(tied[m]) == 0 { + free = append(free, m) + } + } + for _, g := range tierGroups(free, entries) { + var group []seat.Mover + for _, name := range g { + m, _ := MoverOf(p, name) + group = append(group, m) + } + carrier, ok := u.Add(p, group) + if !ok { + continue + } + u.markCarried(p.Branch, g, carrier) + } +} + +// markCarried names carrier as the carrier of the keys of modules (H-A-594 (4)). +func (u *Unfinished) markCarried(branch string, modules []string, carrier string) { + for _, module := range modules { + key := UnfinishedKey(branch, module) + e, held, err := u.read(key) + if err != nil || !held { + continue + } + e.CarriedBy = carrier + raw, _ := json.Marshal(e) + if err := u.Store.Put(key, raw); err != nil { + u.logf("the unfinished entry of %s is not written: %v", module, err) + } + } +} + +// heldBack are the failed modules a mover's key is tied to: the failed movers of its pipeline that the mover awaits at that +// pipeline's revision, directly or through others, and that are registered still (H-A-610, H-A-646). None when the hooks that +// read them are not set. +func (u *Unfinished) heldBack(branch, module string, e UnfinishedEntry) []string { + if u.Pipeline == nil || u.Entries == nil { + return nil + } + p, ok := u.Pipeline(e.Pipeline) + if !ok { + return nil + } + entries, ok := u.Entries(branch, p.Revision) + if !ok { + return nil + } + var left []string + for _, m := range p.Movers { + if _, held, _ := u.read(UnfinishedKey(branch, m.Module)); held && !slices.Contains(failedMovers(p), m.Module) { + left = append(left, m.Module) + } + } + var out []string + for _, f := range tiedTo(p, left, failedMovers(p), entries)[module] { + if u.Registered == nil || u.Registered(f) { + out = append(out, f) + } + } + return out +} + +// HeldBehind are the modules whose unfinished key of branch is tied to module (H-A-625 (2)): the movers a failed module holds +// back, said by its finding pipeline-failed. +func (u *Unfinished) HeldBehind(branch, module string) []string { + if u == nil { + return nil + } + ukeys, err := u.Store.Keys(keys.EscapeName(branch) + ".*") + if err != nil { + return nil + } + slices.Sort(ukeys) + var out []string + for _, key := range ukeys { + _, name, _ := strings.Cut(key, ".") + m, err := keys.UnescapeName(name) + if err != nil { + continue + } + if e, held, _ := u.read(key); held && slices.Contains(u.heldBack(branch, m, e), module) { + out = append(out, m) + } + } + return out } // Carry adds to the movers of a catalog revision of branch each unfinished mover whose fingerprint is still its entry's in // entries, the revision's entries, with one journal line per mover added again (§786). An unfinished mover the revision // moves at a newer fingerprint, or whose entry holds another fingerprint or none, has its key deleted; one it moves at its -// own fingerprint is carried by the revision already. It answers movers with the added ones after them. +// own fingerprint is carried by the revision already. A key whose carrier waits or runs is skipped (H-A-594 (4)); one tied to +// a failed mover the revision does not move is kept, tied, until that mover passes or leaves the registrations (H-A-594 (2), +// H-A-610). It answers movers with the added ones after them. func (u *Unfinished) Carry(branch string, revision int64, entries map[string]catalog.CatalogEntry, movers []seat.Mover) []seat.Mover { if u == nil { return movers @@ -187,6 +412,9 @@ func (u *Unfinished) Carry(branch string, revision int64, entries map[string]cat } continue } + if e.CarriedBy != "" { + continue + } if i := slices.IndexFunc(movers, func(m seat.Mover) bool { return m.Module == module }); i >= 0 { if movers[i].Fingerprint != e.Fingerprint { u.dropMoved(key, module, fmt.Sprintf("revision %d of %s moves it at %s", revision, branch, movers[i].Fingerprint)) @@ -198,7 +426,13 @@ func (u *Unfinished) Carry(branch string, revision int64, entries map[string]cat u.dropMoved(key, module, fmt.Sprintf("revision %d of %s holds it at %q", revision, branch, entry.Fp)) continue } - movers = append(movers, seat.Mover{Module: module, Commit: entry.Commit, Fingerprint: entry.Fp, Repository: entry.Repository}) + if by := u.heldBack(branch, module, e); len(by) > 0 && !slices.ContainsFunc(by, func(f string) bool { + return slices.ContainsFunc(movers, func(m seat.Mover) bool { return m.Module == f }) + }) { + u.logf("revision %d of %s does not carry %s: it is tied to %v, which it does not move (H-A-594 (2))", revision, branch, module, by) + continue + } + movers = append(movers, seat.Mover{Module: module, Commit: entry.Commit, Fingerprint: entry.Fp, Repository: entry.Repository, Widens: e.Widens}) u.logf("revision %d of %s puts %s in a pipeline again: %s left it unfinished (%s) at fingerprint %s (§786)", revision, branch, module, e.Pipeline, e.Outcome, e.Fingerprint) } return movers