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 78a1e312..f8976c2a 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go @@ -96,6 +96,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // (§88); the starter is made after the views, so it is reached through a pointer set once it is. var resumer atomic.Pointer[func(string)] var placer atomic.Pointer[func(catalog.CatalogRevisionEvent) error] + var registrar atomic.Pointer[func(catalog.Registration)] rv, err := startViews(ctx, js, conn, cred, hub, calls.State("findings"), calls.Call, func(branch string) { if f := resumer.Load(); f != nil { (*f)(branch) @@ -105,6 +106,10 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return (*f)(ev) } return fmt.Errorf("the candidate %s's revision %d is heard before the runner can place it", ev.Group, ev.Revision) + }, func(reg catalog.Registration) { + if f := registrar.Load(); f != nil { + (*f)(reg) + } }, logger.Printf) if err != nil { return err @@ -169,6 +174,13 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro logger.Printf("the steps placed on %s and not yet asked: %d asked again", branch, n) } resumer.Store(&resume) + // A registration-changed places the steps of its module's pipelines started before it, from it (A-327). + register := func(reg catalog.Registration) { + if n, err := starter.Registered(pipelineStore{ctx: ctx, kv: pipelines}, reg, rv.Catalog.View); err != nil || n > 0 { + logger.Printf("the pipelines of %s started before its registration: %d placed (%v)", reg.Module, n, err) + } + } + registrar.Store(®ister) starter.Store = pipelineStore{ctx: ctx, kv: pipelines} // A run's placing lost while the runner was down is taken up from its candidate's newest revision, once the registrations // that place its steps are held (A-222). diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/views.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/views.go index d53851ce..55bbe780 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/views.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/views.go @@ -29,7 +29,7 @@ type runnerViews struct { // after, then start hub once. func startViews(ctx context.Context, js jetstream.JetStream, conn *nats.Conn, cred process.Account, hub *process.EventHub, findingsState process.State, call meshcatalogclient.Call, onReady func(branch string), candidate func(ctx context.Context, ev catalog.CatalogRevisionEvent) error, - logf func(string, ...any)) (*runnerViews, error) { + registered func(reg catalog.Registration), logf func(string, ...any)) (*runnerViews, error) { findings, err := findingWriter(ctx, cred, conn, js, findingsState, logf) if err != nil { return nil, err @@ -51,6 +51,8 @@ func startViews(ctx context.Context, js jetstream.JetStream, conn *nats.Conn, cr regs, _, err := meshcatalogclient.WireRegistrationsProcess(reader, call, func(reg catalog.Registration, gone bool) { if !gone { cv.Registered(reg) + // A pipeline of the module started before this registration gets its steps from it (A-327). + registered(reg) } }) if err != nil { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/start.go b/modules/mesh-delivery-pipeline/internal/pipeline/start.go index 88444cd7..b1a0ccec 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/start.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/start.go @@ -136,7 +136,7 @@ func (s *Starter) Start(b Begin) (bool, error) { if reg, ok := s.Registration(b.Module); ok { p.Steps, asks = Place(p, reg, b.Entries, now, s.Name) } else { - s.logf("%s has no registration yet: its pipeline starts with no step", key) + s.logf("%s has no registration yet: its pipeline starts with no step and waits on the registration of %s, which places them (A-327)", key, b.Module) } } if err := s.create(key, p); errors.Is(err, ErrExists) { @@ -226,6 +226,62 @@ func (s *Starter) Resume(store Updater, branch string, half Half) (int, error) { return asked, first } +// Registered places, on a registration-changed of reg's module, the steps of every running pipeline of that module that has +// none, from the registration (design 47 G §21, §23, A-327): a pipeline started before its module had a registration. Each is +// written by compare-and-set, so it is placed once, its steps asked, and said; one held for the runner's new version is left to +// its release, a run on request to its candidate, and one whose branch has no catalog half yet keeps waiting, the first error +// returned. It says how many it placed. +func (s *Starter) Registered(store Updater, reg catalog.Registration, view func(branch string) (Half, bool)) (int, error) { + if reg.Module == "" { + return 0, nil + } + pkeys, err := store.Keys("*.*." + reg.Module) + if err != nil { + return 0, fmt.Errorf("the pipelines of %s are not listed: %w", reg.Module, err) + } + now := time.Now().UTC() + if s.Now != nil { + now = s.Now().UTC() + } + placed := 0 + var first error + note := func(err error) { + if first == nil { + first = err + } + } + for _, key := range pkeys { + p, rev, err := store.Get(key) + if err != nil { + note(fmt.Errorf("the pipeline %s is not read: %w", key, err)) + continue + } + if p.Module != reg.Module || p.Outcome != seat.PipelineOutcomeRunning || p.Held != nil || p.Run != "" || len(p.Steps) > 0 { + continue + } + half, ok := view(p.Branch) + if !ok { + note(fmt.Errorf("the catalog half of %s is not held yet: %s still waits on its steps", p.Branch, key)) + continue + } + var asks []Asked + p.Steps, asks = Place(p, reg, half.Entries, now, s.Name) + if _, err := store.Update(key, p, rev); err != nil { + s.logf("%s is written meanwhile, so this registration places nothing in it: %v", key, err) + continue + } + placed++ + s.logf("%s: %d steps placed from the registration of %s, which arrived after the pipeline started (A-327)", key, len(p.Steps), reg.Module) + if s.Steps != nil && len(asks) > 0 { + s.carry(key, asks) + if err := askAll(s.Steps, s.Arm, key, asks, s.recovered(key, p.Commit)); err != nil { + note(err) + } + } + } + return placed, first +} + // recovered says whether the commit of pipeline key is named a recovery; a read that fails asks on the stream check, said, since // a step is asked either way (a recovery's only goes ahead of the first round). func (s *Starter) recovered(key, commit string) bool { diff --git a/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go b/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go index 63fef192..62b668da 100644 --- a/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go +++ b/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go @@ -122,3 +122,32 @@ func TestTakenInStarts(t *testing.T) { t.Error("a take-in naming no source revision is refused") } } + +// A-327 (design 47 G §21, §23, §54): a pipeline started before its module has a registration starts with no step; the +// registration-changed that brings it places that pipeline's steps from it and asks them, once: heard again it places nothing. +func TestAPipelineStartedBeforeItsRegistrationIsPlacedWhenItArrives(t *testing.T) { + reg, entries := placeFixture() + store := memCreated{memStore{}} + asks := &stepAsks{} + registered := false + s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Say: func(string, any) error { return nil }, + Registration: func(m string) (catalog.Registration, bool) { return reg, registered && m == "app" }} + view := func(string) (Half, bool) { return Half{Entries: entries}, true } + taken := catalog.TakeInTaken{Key: "m.1", Branch: "main", Source: 4, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}} + if n, err := s.TakenIn(taken, view); n != 1 || err != nil { + t.Fatalf("the take-in started %d (%v)", n, err) + } + if p, _, _ := store.Get("main.4.app"); len(p.Steps) != 0 || len(asks.asks) != 0 { + t.Fatalf("before its registration the pipeline has %d steps and %d asks, want none", len(p.Steps), len(asks.asks)) + } + registered = true + n, err := s.Registered(store.memStore, reg, view) + p, _, _ := store.Get("main.4.app") + if n != 1 || err != nil || len(p.Steps) == 0 || len(asks.asks) == 0 || p.Outcome != seat.PipelineOutcomeRunning { + t.Fatalf("the registration placed %d (%v): %d steps, %d asks, want the pipeline's steps placed and asked", n, err, len(p.Steps), len(asks.asks)) + } + before := len(asks.asks) + if n, err := s.Registered(store.memStore, reg, view); n != 0 || err != nil || len(asks.asks) != before { + t.Errorf("the registration heard again placed %d (%v), asked %d more, want nothing", n, err, len(asks.asks)-before) + } +}