RN12: the runner runs on request, its pipelines placed from the run's candidate revision
run writes each member's pipeline at once under the run's random id and its named branch and says run-started; the candidate run-<id>'s catalog-revision, handed to the runner by meshcatalogclient's candidates hook, is read whole through the revision verb and places each member's steps and a pipeline of each entry the members change, saying pipeline-started once each. Builds: design 47 G §64, §373, §420, A-160, A-180, A-185, A-193, A-199 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package meshcatalogclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
// WireCandidatesProcess hands each candidate's `catalog-revision` to hook (design 47 G A-185, A-193, A-199): a candidate's
|
||||
// revision no Catalog keeps, heard once on the event's one subscription of the process's reader, beside every watched
|
||||
// branch's Catalog and whether or not the part follows any, so the runner places a run's pipelines on it. A trunk revision
|
||||
// is passed over (each branch's Catalog keeps it); data that is no such event is an error. The part's manifest consumes the
|
||||
// seat's event catalog-revision.
|
||||
func WireCandidatesProcess(r *views.ProcessReader, hook func(ctx context.Context, ev seat.CatalogRevisionEvent) error) error {
|
||||
if hook == nil {
|
||||
return fmt.Errorf("meshcatalogclient: a candidates hook is a function")
|
||||
}
|
||||
return r.AdoptEvent(seat.Seat, seat.EventCatalogRevision, candidateHear(hook))
|
||||
}
|
||||
|
||||
// candidateHear is the handler of catalog-revision that hands a candidate's revision to hook.
|
||||
func candidateHear(hook func(ctx context.Context, ev seat.CatalogRevisionEvent) error) func(context.Context, []byte) error {
|
||||
return func(ctx context.Context, data []byte) error {
|
||||
var ev seat.CatalogRevisionEvent
|
||||
if err := json.Unmarshal(data, &ev); err != nil || ev.Revision <= 0 {
|
||||
return fmt.Errorf("meshcatalogclient: not a %s event: %s", seat.EventCatalogRevision, clipBytes(data))
|
||||
}
|
||||
if ev.Kind != KindCandidate {
|
||||
return nil
|
||||
}
|
||||
return hook(ctx, ev)
|
||||
}
|
||||
}
|
||||
@@ -162,3 +162,29 @@ func TestCatalogIsAViewPerWatchedBranch(t *testing.T) {
|
||||
t.Error("a Catalog of no branch was made")
|
||||
}
|
||||
}
|
||||
|
||||
// A-185, A-193, A-199: a candidate's catalog-revision reaches the hook, adopted on the event's one subscription of the
|
||||
// reader, also with no watched branch's Catalog; a trunk revision does not, and data that is no such event is said.
|
||||
func TestCandidatesReachTheHook(t *testing.T) {
|
||||
var hooked []seat.CatalogRevisionEvent
|
||||
hook := func(_ context.Context, ev seat.CatalogRevisionEvent) error { hooked = append(hooked, ev); return nil }
|
||||
r := &views.ProcessReader{Module: "runner"}
|
||||
if err := WireCandidatesProcess(r, hook); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := WireCandidatesProcess(r, nil); err == nil {
|
||||
t.Error("a nil hook was taken")
|
||||
}
|
||||
hear := candidateHear(hook)
|
||||
for _, data := range [][]byte{revisionEvent("main", KindTrunk, 3), revisionEvent("main", KindCandidate, 4)} {
|
||||
if err := hear(context.Background(), data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := hear(context.Background(), []byte(`not json`)); err == nil {
|
||||
t.Error("data that is no catalog-revision was taken")
|
||||
}
|
||||
if len(hooked) != 1 || hooked[0].Revision != 4 || hooked[0].Kind != KindCandidate {
|
||||
t.Errorf("the hook was handed %+v, want the candidate's revision 4 alone", hooked)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
||||
|
||||
"mesh-delivery-pipeline/internal/pipeline"
|
||||
)
|
||||
|
||||
// readCandidate reads a candidate's revision whole through the catalog seat's verb revision, with the candidate's group and
|
||||
// heads and the revision its event names, page by page (design 47 G A-180).
|
||||
func readCandidate(call func(key string, body any) (json.RawMessage, error), ev catalog.CatalogRevisionEvent) (pipeline.Half, error) {
|
||||
half := pipeline.Half{Revision: ev.Revision, Source: ev.Source, Entries: map[string]catalog.CatalogEntry{}}
|
||||
after := ""
|
||||
for {
|
||||
raw, err := call(meshcatalogclient.RevisionVerb, catalog.RevisionInput{Branch: ev.Branch, Group: ev.Group, Heads: ev.Heads, Revision: ev.Revision, After: after})
|
||||
if err != nil {
|
||||
return half, err
|
||||
}
|
||||
answer, said, ok := buscalls.Unwrap(raw)
|
||||
if !ok {
|
||||
return half, fmt.Errorf("%s answered: %s", meshcatalogclient.RevisionVerb, said)
|
||||
}
|
||||
var a catalog.RevisionAnswer
|
||||
if err := json.Unmarshal(answer, &a); err != nil {
|
||||
return half, fmt.Errorf("%s answered no revision: %w", meshcatalogclient.RevisionVerb, err)
|
||||
}
|
||||
if a.Revision == nil || a.Revision.Revision != ev.Revision || a.Revision.Group != ev.Group {
|
||||
return half, fmt.Errorf("%s answered %+v for the candidate %s's revision %d", meshcatalogclient.RevisionVerb, a.Revision, ev.Group, ev.Revision)
|
||||
}
|
||||
for _, e := range a.Entries {
|
||||
half.Entries[e.Module] = e
|
||||
}
|
||||
if a.Next == "" {
|
||||
return half, nil
|
||||
}
|
||||
after = a.Next
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
)
|
||||
|
||||
// A-180: a run's candidate revision is read whole through the seat's revision verb with the candidate's group, heads and
|
||||
// revision, page by page; an answer of another revision is refused.
|
||||
func TestReadCandidateReadsTheRevisionWhole(t *testing.T) {
|
||||
ev := catalog.CatalogRevisionEvent{Kind: "candidate", Group: "run-0a1b2c3d4e5f", Heads: "h", Revision: 12, Branch: "main"}
|
||||
var asked []catalog.RevisionInput
|
||||
answerRev := int64(12)
|
||||
call := func(key string, body any) (json.RawMessage, error) {
|
||||
in := body.(catalog.RevisionInput)
|
||||
asked = append(asked, in)
|
||||
a := catalog.RevisionAnswer{Revision: &catalog.CatalogRevision{Revision: answerRev, Kind: "candidate", Group: in.Group}}
|
||||
if in.After == "" {
|
||||
a.Entries, a.Next = []catalog.CatalogEntry{{Module: "app", Fp: "src2:a"}}, "app"
|
||||
} else {
|
||||
a.Entries = []catalog.CatalogEntry{{Module: "lib", Fp: "src2:l"}}
|
||||
}
|
||||
return json.Marshal(a)
|
||||
}
|
||||
half, err := readCandidate(call, ev)
|
||||
if err != nil || len(half.Entries) != 2 || half.Revision != 12 || len(asked) != 2 || asked[1].After != "app" ||
|
||||
asked[0].Group != ev.Group || asked[0].Heads != "h" || asked[0].Revision != 12 || asked[0].Branch != "main" {
|
||||
t.Fatalf("read %+v (%v), asked %+v", half, err, asked)
|
||||
}
|
||||
answerRev = 13
|
||||
if _, err := readCandidate(call, ev); err == nil {
|
||||
t.Error("an answer of another revision was taken")
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,13 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
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/process"
|
||||
|
||||
@@ -81,10 +83,16 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
// A branch's view holding its first half asks again the steps placed in its running pipelines that were never asked
|
||||
// (§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]
|
||||
rv, err := startViews(ctx, js, conn, cred, hub, calls.State("findings"), calls.Call, func(branch string) {
|
||||
if f := resumer.Load(); f != nil {
|
||||
(*f)(branch)
|
||||
}
|
||||
}, func(_ context.Context, ev catalog.CatalogRevisionEvent) error {
|
||||
if f := placer.Load(); f != nil {
|
||||
return (*f)(ev)
|
||||
}
|
||||
return fmt.Errorf("the candidate %s's revision %d is heard before the runner can place it", ev.Group, ev.Revision)
|
||||
}, logger.Printf)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -119,6 +127,21 @@ 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)
|
||||
starter.Store = pipelineStore{ctx: ctx, kv: pipelines}
|
||||
// A run's candidate revision places its pipelines' steps, its entries read through the seat's revision verb (A-180).
|
||||
place := func(ev catalog.CatalogRevisionEvent) error {
|
||||
if !strings.HasPrefix(ev.Group, pipeline.RunGroupPrefix) {
|
||||
return nil
|
||||
}
|
||||
half, err := readCandidate(calls.CallKept, ev)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the candidate %s's revision %d is not read, so its run is not placed: %w", ev.Group, ev.Revision, err)
|
||||
}
|
||||
n, err := starter.PlaceRun(ev, half, rv.Catalog.View)
|
||||
logger.Printf("the candidate %s's revision %d placed %d pipelines of its run", ev.Group, ev.Revision, n)
|
||||
return err
|
||||
}
|
||||
placer.Store(&place)
|
||||
// A newer commit or publish supersedes the older running pipelines it replaces, their asks dropped as kept calls (§31,
|
||||
// §350; A-182, A-202).
|
||||
starter.Supersede = func(newer seat.Pipeline) {
|
||||
|
||||
@@ -38,10 +38,11 @@ func answerVerbs(conn *process.Conn, v pipeline.Verbs) (func(), error) {
|
||||
return conn.AnswerSeat(seat.Seat, writeHandlers(v))
|
||||
}
|
||||
|
||||
// writeHandlers are the seat's write verbs the process answers: review, rerun, accept, cancel, start and ask-again
|
||||
// (§306; RN14).
|
||||
// writeHandlers are the seat's write verbs the process answers: run, review, rerun, accept, cancel, start and ask-again
|
||||
// (§64, §306; RN12, RN14).
|
||||
func writeHandlers(v pipeline.Verbs) map[string]process.SeatHandler {
|
||||
return map[string]process.SeatHandler{
|
||||
seat.VerbRun: verbOf(func(c string, in seat.RunInput) (any, error) { return v.Run(c, in) }),
|
||||
seat.VerbReview: verbOf(func(c string, in seat.ReviewInput) (any, error) { return v.Review(c, in) }),
|
||||
seat.VerbRerun: verbOf(func(c string, in seat.RerunInput) (any, error) { return v.Rerun(c, in) }),
|
||||
seat.VerbAccept: verbOf(func(c string, in seat.AcceptInput) (any, error) { return v.Accept(c, in) }),
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestEveryWriteVerbIsServed(t *testing.T) {
|
||||
got = append(got, v)
|
||||
}
|
||||
slices.Sort(got)
|
||||
want := []string{seat.VerbAccept, seat.VerbAskAgain, seat.VerbCancel, seat.VerbRerun, seat.VerbReview, seat.VerbStart}
|
||||
want := []string{seat.VerbAccept, seat.VerbAskAgain, seat.VerbCancel, seat.VerbRerun, seat.VerbReview, seat.VerbStart, seat.VerbRun}
|
||||
slices.Sort(want)
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("the process answers %v, want %v", got, want)
|
||||
|
||||
@@ -25,7 +25,8 @@ type runnerViews struct {
|
||||
// startViews wires the runner's views on hub and asks each first view; register every other handler on hub before or
|
||||
// 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), logf func(string, ...any)) (*runnerViews, error) {
|
||||
call meshcatalogclient.Call, onReady func(branch string), candidate func(ctx context.Context, ev catalog.CatalogRevisionEvent) error,
|
||||
logf func(string, ...any)) (*runnerViews, error) {
|
||||
findings, err := findingWriter(ctx, cred, conn, js, findingsState, logf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -52,6 +53,11 @@ func startViews(ctx context.Context, js jetstream.JetStream, conn *nats.Conn, cr
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// A candidate's revision, heard once on the event's one subscription whatever branches are followed, places a run's
|
||||
// pipelines (A-185, A-193, A-199).
|
||||
if err := meshcatalogclient.WireCandidatesProcess(reader, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader.Start(ctx, js, cred, status)
|
||||
return &runnerViews{Registrations: regs, Catalog: cv}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// RunGroupPrefix is a run on request's candidate group before its id, `run-<id>` (design 47 G A-160).
|
||||
const RunGroupPrefix = "run-"
|
||||
|
||||
// RunKey is the key of a run on request's pipeline, `<branch>.<id>.<module>`: the run's id under the member's named branch
|
||||
// (design 47 G §420, A-193).
|
||||
func RunKey(branch, run, module string) string {
|
||||
return seat.PipelinesByBranchRevisionModuleKey(keys.EscapeName(branch), run, module)
|
||||
}
|
||||
|
||||
// NewRunID is a run's id, a random 12-hex id, so two runs of one commit are two pipelines (A-180).
|
||||
func NewRunID() (string, error) {
|
||||
b := make([]byte, 6)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
// Run starts a run on request (design 47 G §64, §420; A-180, A-193): it writes each member's pipeline at once, running and
|
||||
// with no steps, its `run` the run's new id, and says run-started, from which the indexer builds the run's candidate; the
|
||||
// candidate's revision places the steps (PlaceRun). A member naming no module, commit or branch is refused, the run then
|
||||
// writing nothing. caller is the one the bus proved. newID makes the run's id (NewRunID).
|
||||
func (s *Starter) Run(caller string, in seat.RunInput, newID func() (string, error)) (seat.RunAnswer, error) {
|
||||
if len(in.Members) == 0 {
|
||||
return seat.RunAnswer{}, errors.New("a run names one or more members, each a module at a commit on a branch")
|
||||
}
|
||||
for _, m := range in.Members {
|
||||
if m.Module == "" || m.Commit == "" || m.Branch == "" {
|
||||
return seat.RunAnswer{}, fmt.Errorf("a run's member names its module, commit and branch: %+v", m)
|
||||
}
|
||||
}
|
||||
id, err := newID()
|
||||
if err != nil {
|
||||
return seat.RunAnswer{}, fmt.Errorf("the run's id is not made: %w", err)
|
||||
}
|
||||
now := s.now()
|
||||
var written []string
|
||||
for _, m := range in.Members {
|
||||
key := RunKey(m.Branch, id, m.Module)
|
||||
p := seat.Pipeline{V: 1, Pipeline: key, Branch: m.Branch, Module: m.Module, Commit: m.Commit, Run: id,
|
||||
Outcome: seat.PipelineOutcomeRunning, Started: &now, Steps: []seat.StepResult{}}
|
||||
raw, err := json.Marshal(p)
|
||||
if err == nil {
|
||||
err = s.Pipelines.Create(key, raw)
|
||||
}
|
||||
if err != nil {
|
||||
s.failRun(written, "the run's other pipelines were not written")
|
||||
return seat.RunAnswer{}, fmt.Errorf("the pipeline %s of the run is not written: %w", key, err)
|
||||
}
|
||||
written = append(written, key)
|
||||
}
|
||||
first := in.Members[0]
|
||||
last := first.Commit
|
||||
if len(in.Members) > 1 {
|
||||
last = RunGroupPrefix + id
|
||||
}
|
||||
ev := seat.RunStarted{V: 1, At: &now, By: caller, Members: in.Members, Run: id}
|
||||
if err := s.Say(runnerclient.Subject(seat.EventRunStarted, first.Module, first.Branch, last), ev); err != nil {
|
||||
s.failRun(written, "run-started was not said, so no candidate is built for it")
|
||||
return seat.RunAnswer{}, fmt.Errorf("the run %s is not started: run-started is not said: %w", id, err)
|
||||
}
|
||||
return seat.RunAnswer{Run: id, Pipelines: written}, nil
|
||||
}
|
||||
|
||||
// failRun ends the run's pipelines written before its start failed, failed and saying why, so none stands running.
|
||||
func (s *Starter) failRun(written []string, why string) {
|
||||
if s.Store == nil {
|
||||
return
|
||||
}
|
||||
for _, key := range written {
|
||||
for {
|
||||
p, rev, err := s.Store.Get(key)
|
||||
if err != nil || p.Outcome != seat.PipelineOutcomeRunning {
|
||||
break
|
||||
}
|
||||
now := s.now()
|
||||
p.Outcome, p.Why, p.Ended = seat.PipelineOutcomeFailed, why, &now
|
||||
p.EndCount++
|
||||
if _, err := s.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
|
||||
continue
|
||||
} else if err != nil {
|
||||
s.logf("the pipeline %s of a run not started stays running: %v", key, err)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PlaceRun places a run's pipelines from its candidate's revision (design 47 G §64, §373, A-180, A-193): ev is the
|
||||
// catalog-revision of the candidate `run-<id>`, half its entries read whole through the seat's revision verb. Each member's
|
||||
// pipeline of the run not yet placed takes its fingerprint, the candidate's revision and its steps, by compare-and-set, and
|
||||
// says pipeline-started; each further entry whose fingerprint the members change (another than in base, the branch's catalog
|
||||
// half, or absent there) gets a pipeline of the run of its own, placed the same way. Another candidate's revision places
|
||||
// nothing; a revision heard again places nothing twice. It says how many pipelines it placed.
|
||||
func (s *Starter) PlaceRun(ev catalog.CatalogRevisionEvent, half Half, base func(branch string) (Half, bool)) (int, error) {
|
||||
id, ok := strings.CutPrefix(ev.Group, RunGroupPrefix)
|
||||
if !ok || id == "" {
|
||||
return 0, nil
|
||||
}
|
||||
if s.Store == nil {
|
||||
return 0, errors.New("the runner holds no pipelines to place a run's in")
|
||||
}
|
||||
pkeys, err := s.Store.Keys("*." + id + ".*")
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("the pipelines of the run %s are not listed: %w", id, err)
|
||||
}
|
||||
slices.Sort(pkeys)
|
||||
members := map[string]bool{}
|
||||
branch := ""
|
||||
for _, key := range pkeys {
|
||||
if p, _, err := s.Store.Get(key); err == nil && p.Run == id {
|
||||
members[p.Module] = true
|
||||
if branch == "" {
|
||||
branch = p.Branch
|
||||
}
|
||||
}
|
||||
}
|
||||
if branch == "" {
|
||||
s.logf("the candidate %s's revision %d names a run with no pipeline here: nothing is placed", ev.Group, ev.Revision)
|
||||
return 0, nil
|
||||
}
|
||||
placed := 0
|
||||
var first error
|
||||
keep := func(ok bool, err error) {
|
||||
if err != nil && first == nil {
|
||||
first = err
|
||||
}
|
||||
if ok {
|
||||
placed++
|
||||
}
|
||||
}
|
||||
for _, key := range pkeys {
|
||||
keep(s.placeRunPipeline(key, id, ev.Revision, half))
|
||||
}
|
||||
// The dependents (§373): the candidate's entries the members change, beside the members, on the first member's branch.
|
||||
trunk, ok := base(branch)
|
||||
if b, err := keys.UnescapeName(ev.Branch); err == nil && b != "" && b != branch {
|
||||
if h, held := base(b); held {
|
||||
trunk, ok = h, true
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
s.logf("the run %s's dependents are not known: the catalog half of %s is not held, so its members run alone", id, branch)
|
||||
return placed, first
|
||||
}
|
||||
for _, module := range slices.Sorted(maps.Keys(half.Entries)) {
|
||||
e := half.Entries[module]
|
||||
if members[module] {
|
||||
continue
|
||||
}
|
||||
if was, held := trunk.Entries[module]; held && was.Fp == e.Fp {
|
||||
continue
|
||||
}
|
||||
key := RunKey(branch, id, module)
|
||||
now := s.now()
|
||||
p := seat.Pipeline{V: 1, Pipeline: key, Branch: branch, Module: module, Commit: e.Commit, Run: id,
|
||||
Outcome: seat.PipelineOutcomeRunning, Started: &now, Steps: []seat.StepResult{}}
|
||||
raw, err := json.Marshal(p)
|
||||
if err == nil {
|
||||
err = s.Pipelines.Create(key, raw)
|
||||
}
|
||||
if err != nil && !errors.Is(err, ErrExists) {
|
||||
keep(false, fmt.Errorf("the dependent %s of the run %s is not written: %w", module, id, err))
|
||||
continue
|
||||
}
|
||||
keep(s.placeRunPipeline(key, id, ev.Revision, half))
|
||||
}
|
||||
return placed, first
|
||||
}
|
||||
|
||||
// placeRunPipeline places one pipeline of the run, written and not yet placed (no revision), from the candidate's half.
|
||||
func (s *Starter) placeRunPipeline(key, id string, revision int64, half Half) (bool, error) {
|
||||
for {
|
||||
p, rev, err := s.Store.Get(key)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("the pipeline %s is not read: %w", key, err)
|
||||
}
|
||||
if p.Run != id || p.Outcome != seat.PipelineOutcomeRunning || p.Revision != 0 {
|
||||
return false, nil
|
||||
}
|
||||
now := s.now()
|
||||
e, ok := half.Entries[p.Module]
|
||||
if !ok {
|
||||
s.logf("the candidate of the run %s holds no entry of %s: its pipeline is placed with no fingerprint", id, p.Module)
|
||||
}
|
||||
p.Fingerprint, p.Repository, p.Revision = e.Fp, e.Repository, revision
|
||||
var asks []Asked
|
||||
if s.Registration != nil {
|
||||
if reg, ok := s.Registration(p.Module); ok {
|
||||
p.Steps, asks = Place(p, reg, half.Entries, now, s.Name)
|
||||
} else {
|
||||
s.logf("%s has no registration yet: its pipeline is placed with no step", key)
|
||||
}
|
||||
}
|
||||
if _, err := s.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
|
||||
continue
|
||||
} else if err != nil {
|
||||
return false, fmt.Errorf("the pipeline %s cannot be written: %w", key, err)
|
||||
}
|
||||
ev := seat.PipelineStarted{V: 1, At: &now, Pipeline: key, Branch: p.Branch, Module: p.Module, Commit: p.Commit,
|
||||
Fingerprint: p.Fingerprint, Revision: p.Revision, Run: id}
|
||||
if err := s.Say(runnerclient.Subject(seat.EventPipelineStarted, p.Module, p.Branch, p.Commit), ev); err != nil {
|
||||
s.logf("pipeline-started of %s is not said: %v", key, err)
|
||||
}
|
||||
if s.Steps != nil && len(asks) > 0 {
|
||||
s.carry(key, asks)
|
||||
if err := askAll(s.Steps, s.Arm, key, asks); err != nil {
|
||||
return true, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Starter) now() time.Time {
|
||||
if s.Now != nil {
|
||||
return s.Now().UTC()
|
||||
}
|
||||
return time.Now().UTC()
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"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"
|
||||
)
|
||||
|
||||
// memCreated is memStore written by Create too, as the state pipelines is.
|
||||
type memCreated struct{ memStore }
|
||||
|
||||
func (m memCreated) Create(key string, value []byte) error {
|
||||
if _, ok := m.memStore[key]; ok {
|
||||
return ErrExists
|
||||
}
|
||||
var p seat.Pipeline
|
||||
if err := json.Unmarshal(value, &p); err != nil {
|
||||
return err
|
||||
}
|
||||
m.memStore[key] = struct {
|
||||
p seat.Pipeline
|
||||
rev uint64
|
||||
}{p, 1}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RN12 (design 47 G §64, §373, §420; A-180, A-193): `run` writes each member's pipeline at once under the run's id and its
|
||||
// named branch and says run-started; the candidate run-<id>'s revision places each member's steps and a pipeline of each
|
||||
// entry the members change, says pipeline-started once each, and places nothing twice; a member naming no branch is refused.
|
||||
func TestRunOnRequest(t *testing.T) {
|
||||
reg, entries := placeFixture()
|
||||
store := memCreated{memStore{}}
|
||||
var said []string
|
||||
asks := &stepAsks{}
|
||||
s := &Starter{Pipelines: store, Store: store, Steps: asks, Name: "the-runner",
|
||||
Registration: func(m string) (catalog.Registration, bool) {
|
||||
r := reg
|
||||
r.Module = m
|
||||
return r, m == "app" || m == "lib"
|
||||
},
|
||||
Say: func(subject string, body any) error {
|
||||
said = append(said, subject[strings.Index(subject, ".event.")+7:])
|
||||
return nil
|
||||
}}
|
||||
id := func() (string, error) { return "0a1b2c3d4e5f", nil }
|
||||
if _, err := s.Run("sessions", seat.RunInput{Members: []seat.Member{{Module: "app", Commit: "c1"}}}, id); err == nil {
|
||||
t.Error("a member naming no branch was taken")
|
||||
}
|
||||
if len(store.memStore) != 0 || len(said) != 0 {
|
||||
t.Fatalf("a refused run wrote %v and said %v", store.memStore, said)
|
||||
}
|
||||
a, err := s.Run("sessions", seat.RunInput{Members: []seat.Member{{Module: "app", Commit: "c1", Branch: "fix/1"}}}, id)
|
||||
if err != nil || a.Run != "0a1b2c3d4e5f" || len(a.Pipelines) != 1 {
|
||||
t.Fatalf("run = %+v, %v", a, err)
|
||||
}
|
||||
key := a.Pipelines[0]
|
||||
if key != RunKey("fix/1", "0a1b2c3d4e5f", "app") {
|
||||
t.Errorf("the member's pipeline is %s, want the run's id under its named branch", key)
|
||||
}
|
||||
if p, _, _ := store.Get(key); p.Run != a.Run || p.Outcome != seat.PipelineOutcomeRunning || len(p.Steps) != 0 || p.Branch != "fix/1" || p.Source != 0 {
|
||||
t.Errorf("the member's pipeline %s = %+v", key, p)
|
||||
}
|
||||
if len(said) != 1 || !strings.HasPrefix(said[0], seat.EventRunStarted+".app.") || !strings.HasSuffix(said[0], ".c1") {
|
||||
t.Errorf("said %v, want run-started on the member's module, branch and commit", said)
|
||||
}
|
||||
cand := entries
|
||||
cand["app"] = catalog.CatalogEntry{Module: "app", Commit: "c1", Fp: "src2:new", Repository: "o/app", Artifacts: entries["app"].Artifacts}
|
||||
cand["lib"] = catalog.CatalogEntry{Module: "lib", Commit: "c0", Fp: "src2:lib2", Artifacts: entries["app"].Artifacts}
|
||||
cand["same"] = catalog.CatalogEntry{Module: "same", Fp: "src2:same"}
|
||||
base := func(string) (Half, bool) {
|
||||
return Half{Entries: map[string]catalog.CatalogEntry{"lib": {Fp: "src2:lib1"}, "same": {Fp: "src2:same"}, "mesh-build-go": entries["mesh-build-go"]}}, true
|
||||
}
|
||||
ev := catalog.CatalogRevisionEvent{Kind: "candidate", Group: "run-" + a.Run, Revision: 12}
|
||||
if n, err := s.PlaceRun(catalog.CatalogRevisionEvent{Kind: "candidate", Group: "try-3", Revision: 11}, Half{Entries: cand}, base); n != 0 || err != nil {
|
||||
t.Errorf("a try's candidate placed %d (%v)", n, err)
|
||||
}
|
||||
said = nil
|
||||
n, err := s.PlaceRun(ev, Half{Revision: 12, Entries: cand}, base)
|
||||
if err != nil || n != 2 {
|
||||
t.Fatalf("the candidate placed %d (%v), want the member and the dependent lib", n, err)
|
||||
}
|
||||
p, _, _ := store.Get(key)
|
||||
if p.Fingerprint != "src2:new" || p.Revision != 12 || len(p.Steps) == 0 {
|
||||
t.Errorf("the member's pipeline placed = %+v", p)
|
||||
}
|
||||
dep, _, err := store.Get(RunKey("fix/1", a.Run, "lib"))
|
||||
if err != nil || dep.Run != a.Run || dep.Fingerprint != "src2:lib2" || len(dep.Steps) == 0 {
|
||||
t.Errorf("the dependent's pipeline = %+v (%v)", dep, err)
|
||||
}
|
||||
if _, _, err := store.Get(RunKey("fix/1", a.Run, "same")); err == nil {
|
||||
t.Error("an entry the members do not change got a pipeline")
|
||||
}
|
||||
if len(said) != 2 || !strings.HasPrefix(said[0], seat.EventPipelineStarted+".") || len(asks.ids) == 0 {
|
||||
t.Errorf("placing said %v and asked %d steps", said, len(asks.ids))
|
||||
}
|
||||
if n, _ := s.PlaceRun(ev, Half{Revision: 12, Entries: cand}, base); n != 0 {
|
||||
t.Errorf("the revision heard again placed %d", n)
|
||||
}
|
||||
// A restart asks a run's steps again from no branch's half: they are its candidate's.
|
||||
if n, err := s.Resume(store, "fix/1", Half{Entries: entries}); n != 0 || err != nil {
|
||||
t.Errorf("a restart asked %d steps of the run again (%v)", n, err)
|
||||
}
|
||||
|
||||
// Several members say run-started on the first member's module and branch and run-<id>; a run-started not said fails the
|
||||
// pipelines written, so none stands running.
|
||||
said = nil
|
||||
two, err := s.Run("operator", seat.RunInput{Members: []seat.Member{{Module: "app", Commit: "c2", Branch: "main"}, {Module: "lib", Commit: "c3", Branch: "main"}}},
|
||||
func() (string, error) { return "ffffffffffff", nil })
|
||||
if err != nil || len(two.Pipelines) != 2 || len(said) != 1 || said[0] != seat.EventRunStarted+".app.main.run-ffffffffffff" {
|
||||
t.Errorf("a run of two: %+v (%v), said %v", two, err, said)
|
||||
}
|
||||
s.Say = func(string, any) error { return errors.New("bus away") }
|
||||
three, err := s.Run("operator", seat.RunInput{Members: []seat.Member{{Module: "app", Commit: "c4", Branch: "main"}}}, func() (string, error) { return "eeeeeeeeeeee", nil })
|
||||
if err == nil || three.Run != "" {
|
||||
t.Errorf("a run whose run-started was not said answered %+v", three)
|
||||
}
|
||||
if p, _, _ := store.Get(RunKey("main", "eeeeeeeeeeee", "app")); p.Outcome != seat.PipelineOutcomeFailed {
|
||||
t.Errorf("its pipeline is %s, want failed", p.Outcome)
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,8 @@ type Starter struct {
|
||||
Toolchain func(language string) (*nodebuildgo.ToolchainUsed, error)
|
||||
// Arm arms the bound of an ask put on a build seat's work queue (§535, §647); nil arms none.
|
||||
Arm Arm
|
||||
// Store is the state pipelines read and written by compare-and-set, where a run's pipelines are placed (PlaceRun).
|
||||
Store Updater
|
||||
// Supersede ends the older running pipelines the pipeline just written supersedes (§31; Hearer.Supersede); nil ends none.
|
||||
Supersede func(newer seat.Pipeline)
|
||||
}
|
||||
@@ -156,7 +158,8 @@ func (s *Starter) Resume(store Updater, branch string, half Half) (int, error) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
if p.Branch != branch || p.Outcome != seat.PipelineOutcomeRunning {
|
||||
// A run on request's steps are placed from its candidate, not from the branch's half (PlaceRun).
|
||||
if p.Branch != branch || p.Outcome != seat.PipelineOutcomeRunning || p.Run != "" {
|
||||
continue
|
||||
}
|
||||
reg, ok := s.Registration(p.Module)
|
||||
|
||||
@@ -335,6 +335,15 @@ func (v Verbs) askOf(p seat.Pipeline, s seat.StepResult, caller string, now time
|
||||
return nil, fmt.Errorf("the %s step of %s is placed no ask now", s.Step, p.Pipeline)
|
||||
}
|
||||
|
||||
// Run starts a run on request for the caller the bus proved (design 47 G §64; Starter.Run): its pipelines written, its
|
||||
// run-started said, its steps placed when its candidate's revision is heard.
|
||||
func (v Verbs) Run(caller string, in seat.RunInput) (seat.RunAnswer, error) {
|
||||
if v.Starter == nil {
|
||||
return seat.RunAnswer{}, errors.New("the runner cannot start a run: it holds no starter")
|
||||
}
|
||||
return v.Starter.Run(caller, in, NewRunID)
|
||||
}
|
||||
|
||||
// Start starts by hand a delivering pipeline for the head of a watched branch the runner's catalog half holds, recorded in
|
||||
// the hand-act log first (§306, §394): the pipeline of that branch's source revision and the module, as a take-in would
|
||||
// start it. A head whose pipeline is written already starts nothing and is refused, naming the pipeline.
|
||||
|
||||
Reference in New Issue
Block a user