Compare commits
38
Commits
go/v0.1.69
...
go/v0.1.73
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ea4b60dbd | ||
|
|
5da42d1775 | ||
|
|
0639ec4bca | ||
|
|
ac833a48f8 | ||
|
|
d9596548f9 | ||
|
|
9a7cd5861d | ||
|
|
95b4b308ad | ||
|
|
b413949ea8 | ||
|
|
69d17f139f | ||
|
|
1175fef241 | ||
|
|
d822259cfa | ||
|
|
ea4fe471e2 | ||
|
|
ff7a27f36f | ||
|
|
7de5814a26 | ||
|
|
dfdfd84efe | ||
|
|
68048619d9 | ||
|
|
8f17d59f5c | ||
|
|
50734000c3 | ||
|
|
2bc9ea9377 | ||
|
|
7617dda5f6 | ||
|
|
13db05ce60 | ||
|
|
334bc54767 | ||
|
|
6f8ba0bf69 | ||
|
|
92196dde3a | ||
|
|
16feb56fd4 | ||
|
|
78adc23d70 | ||
|
|
79bca88c8a | ||
|
|
5bca54bb59 | ||
|
|
ae91c7e73a | ||
|
|
378853a97d | ||
|
|
33e5c3a105 | ||
|
|
91f3ef809c | ||
|
|
9e793e0142 | ||
|
|
65c4dcbf73 | ||
|
|
43ec4017be | ||
|
|
ab8a8ddfc4 | ||
|
|
22f252251c | ||
|
|
bec4d43da6 |
@@ -93,6 +93,11 @@ type seatTools struct {
|
||||
// seats is the seat list the bundle reads (seatlist.go): until it is read, propose judges no mesh seat the bootstrapped
|
||||
// definitions do not name as held once (ADR 0636 §1). Nil reads nothing.
|
||||
seatList *seatlist.Part
|
||||
// watched is the tools process's watched copy of the state seat-holders (tools.go wants.watched, which gate.Holders
|
||||
// also reads: A-298), read by the verb assignments with seats instead of a bus get per key (issue 1982: under load
|
||||
// that took about 14 s, where the watched copy answers at once). Nil, or a call it cannot answer, falls back to a
|
||||
// direct listing.
|
||||
watched *stdio.WatchedStates
|
||||
}
|
||||
|
||||
func newSeatTools(st seatStore, ask func(string, any) (json.RawMessage, error)) *seatTools {
|
||||
@@ -554,25 +559,43 @@ func (s *seatTools) tools() []stdio.Tool {
|
||||
return out
|
||||
}
|
||||
|
||||
// seats is the verb assignments with seats (ADR 0571 §4): who holds each seat and on which node, as the state seat-holders
|
||||
// holds it; with node, the keys of seats held on that node.
|
||||
func (s *seatTools) seats(node string) (any, error) {
|
||||
// seatHolders is every key of the state seat-holders and its raw value: from the watched copy (s.watched) when
|
||||
// there is one and it answers, else a direct listing, key by key, as the verb answered before (issue 1982).
|
||||
func (s *seatTools) seatHolders() (map[string][]byte, error) {
|
||||
if s.watched != nil {
|
||||
if vals, err := s.watched.Values(mma.StateSeatHolders); err == nil {
|
||||
return vals, nil
|
||||
}
|
||||
}
|
||||
keys, err := s.st.keys(mma.StateSeatHolders)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("seat-holders cannot be listed: %w", err)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
out := []mma.SeatHolders{}
|
||||
out := make(map[string][]byte, len(keys))
|
||||
for _, key := range keys {
|
||||
raw, _, found, err := s.st.get(mma.StateSeatHolders, key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("seat-holders %s cannot be read: %w", key, err)
|
||||
}
|
||||
if !found {
|
||||
continue
|
||||
if found {
|
||||
out[key] = raw
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// seats is the verb assignments with seats (ADR 0571 §4): who holds each seat and on which node, as the state seat-holders
|
||||
// holds it; with node, the keys of seats held on that node.
|
||||
func (s *seatTools) seats(node string) (any, error) {
|
||||
raws, err := s.seatHolders()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys := slices.Sorted(maps.Keys(raws))
|
||||
out := []mma.SeatHolders{}
|
||||
for _, key := range keys {
|
||||
var sh mma.SeatHolders
|
||||
if json.Unmarshal(raw, &sh) != nil {
|
||||
if json.Unmarshal(raws[key], &sh) != nil {
|
||||
continue
|
||||
}
|
||||
if node != "" && sh.Node != node && !slices.ContainsFunc(sh.Holders, func(h mma.SeatHoldersHoldersItem) bool { return h.Node == node }) {
|
||||
|
||||
@@ -352,6 +352,53 @@ func TestTheSeatsVerbs(t *testing.T) {
|
||||
t.Errorf("seats on ace = %+v, %v", out, err)
|
||||
}
|
||||
})
|
||||
t.Run("issue 1982: assignments with seats reads the tools process's watched copy of seat-holders, the store only while it cannot", func(t *testing.T) {
|
||||
seeded := map[string][]byte{}
|
||||
put := func(seat, node string, holders ...mma.SeatHoldersHoldersItem) {
|
||||
raw, err := json.Marshal(mma.SeatHolders{Seat: seat, Node: node, Holders: holders})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seeded[seat] = raw
|
||||
}
|
||||
put("mesh-broker", "", mma.SeatHoldersHoldersItem{Module: "nats", Node: "ace"})
|
||||
put("node-shell.g14", "g14")
|
||||
st := &countingSeatStore{memStore: memStore{mma.StateSeatHolders: &memKV{values: seeded}}}
|
||||
sv := newSeatTools(st, nil)
|
||||
watches := 0
|
||||
sv.watched = stdio.WatchedStatesOver(func(state string, handle func(stdio.StateChange) error) error {
|
||||
watches++
|
||||
for k, v := range seeded {
|
||||
if err := handle(stdio.StateChange{Key: k, Op: stdio.OpPut, Value: v, Current: true}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, func(state, key string) ([]byte, bool, error) {
|
||||
v, ok := seeded[key]
|
||||
return v, ok, nil
|
||||
})
|
||||
out, err := sv.seats("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := out.(map[string]any)["seats"].([]mma.SeatHolders); len(got) != 2 || watches != 1 || st.keysCalls != 0 || st.getCalls != 0 {
|
||||
t.Errorf("seats from the watched copy = %+v, watched %d time(s), the store's own keys %d get %d (want none)",
|
||||
got, watches, st.keysCalls, st.getCalls)
|
||||
}
|
||||
// The lead's answer on issue 1982: the verb falls back to the store's own listing only while the watched copy
|
||||
// cannot be read (here, its watch refused); the answer's form does not change.
|
||||
sv.watched = stdio.WatchedStatesOver(func(string, func(stdio.StateChange) error) error {
|
||||
return errors.New("refused")
|
||||
}, func(state, key string) ([]byte, bool, error) { return nil, false, nil })
|
||||
out, err = sv.seats("")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := out.(map[string]any)["seats"].([]mma.SeatHolders); len(got) != 2 || st.keysCalls != 1 || st.getCalls != 2 {
|
||||
t.Errorf("seats falling back to the store = %+v, the store's own keys %d get %d (want 1, 2)", got, st.keysCalls, st.getCalls)
|
||||
}
|
||||
})
|
||||
t.Run("research 072 row 37: a node is written as its keys, and history reads the node key", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
kv := &revKV{values: map[string][]byte{}, revs: map[string]uint64{}}
|
||||
@@ -750,6 +797,23 @@ func readManifest() (catalogue.Manifest, error) {
|
||||
return catalogue.Decode(raw)
|
||||
}
|
||||
|
||||
// countingSeatStore is a memStore that counts its calls to keys and get, to prove a reader takes the state from the
|
||||
// watched copy and the store only while that copy cannot be read (issue 1982).
|
||||
type countingSeatStore struct {
|
||||
memStore
|
||||
keysCalls, getCalls int
|
||||
}
|
||||
|
||||
func (c *countingSeatStore) keys(state string) ([]string, error) {
|
||||
c.keysCalls++
|
||||
return c.memStore.keys(state)
|
||||
}
|
||||
|
||||
func (c *countingSeatStore) get(state, key string) ([]byte, uint64, bool, error) {
|
||||
c.getCalls++
|
||||
return c.memStore.get(state, key)
|
||||
}
|
||||
|
||||
// countingKV is a memKV that counts its puts.
|
||||
type countingKV struct {
|
||||
memKV
|
||||
|
||||
@@ -419,6 +419,8 @@ func serveTools() error {
|
||||
wants.watched = stdio.NewWatchedStates() // the large states read whole from one watched copy (ADR 0580 clause 3)
|
||||
// The callers' seat entries are resolved from the assigner's own seat-holders, its watched copy (A-298).
|
||||
gate.Holders(seatlist.HoldersIn(func() (map[string][]byte, error) { return wants.watched.Values(mma.StateSeatHolders) }))
|
||||
// The verb assignments with seats answers from the same watched copy, so every caller of it is fast too (issue 1982).
|
||||
seatVerbs.watched = wants.watched
|
||||
wants.revisions = runtimeObjects{busstate.ObjectsOf(assigngraph.RevisionsObjects)}
|
||||
tools = append(tools, wants.tools()...)
|
||||
keeper := &inputKeeper{vals: stateobjects.ForState(buckets.InputsState), now: time.Now, logf: func(string, ...any) {}}
|
||||
|
||||
@@ -434,7 +434,7 @@ func main() {
|
||||
Newest: newestVia(stdio.Ask), Steps: r}))
|
||||
tools = append(tools, reservedTools(rr, buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
|
||||
qs, as := stdio.State(versions.PublishQueueState), stdio.State(versions.ActedState)
|
||||
tools = append(tools, republishTools(readerOf(qs), readerOf(as), buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
|
||||
tools = append(tools, republishTools(readerOf(qs), readerOf(as), readerOf(reasks), buildAgain, time.Now, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
|
||||
ws := stdio.State(build.WaitsState)
|
||||
wr := versions.Reader{Keys: ws.Keys, Get: func(key string) (json.RawMessage, bool, error) {
|
||||
e, err := ws.Get(key)
|
||||
|
||||
@@ -28,7 +28,7 @@ func readerOf(s stdio.KeptState) versions.Reader {
|
||||
// republishTools are builder_publish_again and builder_publish_queue (design 47 G §588). The publish again is a hand act:
|
||||
// it is recorded in the hand-act log first, then written to reasks for the builder, which owns the queue, to answer
|
||||
// with the versions it queued.
|
||||
func republishTools(queue, acted versions.Reader, ask queueAsker, now func() time.Time, acts meshhandactsclient.Recorder) []stdio.Tool {
|
||||
func republishTools(queue, acted, reasks versions.Reader, ask queueAsker, now func() time.Time, acts meshhandactsclient.Recorder) []stdio.Tool {
|
||||
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
|
||||
return []stdio.Tool{
|
||||
{Name: versions.PublishAgainTool, Description: versions.PublishAgainDescription,
|
||||
@@ -60,6 +60,6 @@ func republishTools(queue, acted versions.Reader, ask queueAsker, now func() tim
|
||||
}},
|
||||
{Name: versions.PublishQueueTool, Description: versions.PublishQueueDescription,
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{}},
|
||||
Run: func(map[string]any) (any, error) { return versions.AnswerQueue(queue, acted) }},
|
||||
Run: func(map[string]any) (any, error) { return versions.AnswerQueue(queue, acted, reasks) }},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"mesh-delivery-builder/internal/versions"
|
||||
)
|
||||
|
||||
// reservedTools are builder_reserved and builder_reserved_drop (design 47 G section 369), served as tools of the bundle. The drop is a hand act: it is recorded in the hand-act log first, then written to
|
||||
// reservedTools are builder_reserved, builder_reserved_drop and builder_reserve_done (A-455) (design 47 G section 369), served as tools of the bundle. The drop is a hand act: it is recorded in the hand-act log first, then written to
|
||||
// reasks for the builder, which owns the reservations, to answer.
|
||||
//
|
||||
// The seat's verb reserved is the builder's process's (design 47 G A-115, A-127), not the bundle's.
|
||||
@@ -51,5 +51,32 @@ func reservedTools(rr versions.Reader, ask queueAsker, now func() time.Time, act
|
||||
}
|
||||
return map[string]any{"key": key, "answer": a.Answer}, nil
|
||||
}},
|
||||
{Name: versions.ReserveDoneTool, Description: versions.ReserveDoneDescription,
|
||||
Input: map[string]any{"type": "object", "required": []string{"module", "fp", "why"}, "properties": map[string]any{
|
||||
"module": map[string]any{"type": "string", "description": "the module"},
|
||||
"fp": map[string]any{"type": "string", "description": "the fingerprint of its done trunk build"},
|
||||
"why": map[string]any{"type": "string", "description": "why, in plain words"}}},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
key, value, err := build.NewReserveDone(str(args, "module"), str(args, "fp"), str(args, "why"), stdio.CallerOf(args), now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := acts.Record(meshhandactsclient.Request{What: fmt.Sprintf("reserved a version for the done build of %s at %s by hand", str(args, "module"), str(args, "fp")),
|
||||
Why: str(args, "why"), Cause: versions.ReserveDoneTool, Verb: "reserve-done"}); err != nil {
|
||||
return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was reserved: %w", err)
|
||||
}
|
||||
raw, err := ask(key, value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a, err := build.DecodeReAsk(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.Refused {
|
||||
return nil, errors.New(a.Answer)
|
||||
}
|
||||
return map[string]any{"key": key, "answer": a.Answer}, nil
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -44,3 +45,56 @@ func TestWaitsToolsListTheWaitsAndReleaseAfterTheHandAct(t *testing.T) {
|
||||
t.Fatalf("release: %v, %d recorded, asked %+v", err, recorded, asked)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-455: builder_reserve_done records the hand act first and only then asks the builder through reasks; the builder's
|
||||
// answer (the version and its queued publish) is the tool's answer, its refusal the tool's error, and an act that was not
|
||||
// recorded asks nothing.
|
||||
func TestReserveDoneRecordsTheHandActFirstAndAnswersTheBuilders(t *testing.T) {
|
||||
var order []string
|
||||
var asked []build.ReAsk
|
||||
reply := func(refuse bool) func(string, []byte) (json.RawMessage, error) {
|
||||
return func(key string, value []byte) (json.RawMessage, error) {
|
||||
order = append(order, "asked")
|
||||
a, _ := build.DecodeReAsk(value)
|
||||
asked = append(asked, a)
|
||||
a.Answered, a.Answer, a.Refused = time.Now(), "reserved 0.1.13 for node-tools and queued 1 versions to publish: node-tools@0.1.13", refuse
|
||||
if refuse {
|
||||
a.Answer = "the build key is not done"
|
||||
}
|
||||
return json.Marshal(a)
|
||||
}
|
||||
}
|
||||
record := func(fail bool) meshhandactsclient.Recorder {
|
||||
return meshhandactsclient.Recorder{Ask: func(string, any) (json.RawMessage, error) {
|
||||
order = append(order, "recorded")
|
||||
if fail {
|
||||
return nil, errors.New("no responders")
|
||||
}
|
||||
return json.RawMessage(`{}`), nil
|
||||
}}
|
||||
}
|
||||
run := func(ask func(string, []byte) (json.RawMessage, error), acts meshhandactsclient.Recorder, args map[string]any) (any, error) {
|
||||
for _, tl := range reservedTools(versions.Reader{}, ask, time.Now, acts) {
|
||||
if tl.Name == versions.ReserveDoneTool {
|
||||
return tl.Run(args)
|
||||
}
|
||||
}
|
||||
t.Fatal("the bundle serves no builder_reserve_done")
|
||||
return nil, nil
|
||||
}
|
||||
args := map[string]any{"module": "node-tools", "fp": "src2:7df8", "why": "its build ended done with no version"}
|
||||
out, err := run(reply(false), record(false), args)
|
||||
if err != nil || len(order) != 2 || order[0] != "recorded" || order[1] != "asked" || asked[0].Mode != build.ModeReserveDone || asked[0].Name != "node-tools" || asked[0].FP != "src2:7df8" {
|
||||
t.Fatalf("answer %v, %v; order %v, asked %+v", out, err, order, asked)
|
||||
}
|
||||
if ans := out.(map[string]any)["answer"].(string); !strings.Contains(ans, "reserved 0.1.13") || !strings.Contains(ans, "queued 1 versions") {
|
||||
t.Errorf("the answer %q names no version and queued publish", ans)
|
||||
}
|
||||
if _, err := run(reply(true), record(false), args); err == nil || !strings.Contains(err.Error(), "not done") {
|
||||
t.Errorf("the builder's refusal is not the tool's error: %v", err)
|
||||
}
|
||||
order = nil
|
||||
if _, err := run(reply(false), record(true), args); err == nil || len(order) != 1 || order[0] != "recorded" {
|
||||
t.Errorf("an unrecorded act was asked of the builder: %v, %v", err, order)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,18 +234,36 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
acts := versions.Acts{Numberer: numberer, DropReserved: reserved.dropped, Reconsider: b.Reconsider}
|
||||
// A reserved version left unpublished is published again, by the builder on an event or by a person's tool (design 47 G
|
||||
// §588): its states publish-queue and acted, bound once the bus holds them.
|
||||
republishMax, err := republishMaxOf(getenv("MESH_BUILDER_REPUBLISH_MAX_FILE"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
queueKV := process.AwaitBucket(ctx, js, conn, own.queue, "no version is published again on an event or by builder_publish_again", logger.Printf)
|
||||
actedKV := process.AwaitBucket(ctx, js, conn, own.acted, "no version is published again on an event or by builder_publish_again", logger.Printf)
|
||||
republish := &versions.Republisher{Reserved: reserved.r, Queue: awaitedQueue{awaitedReserved{awaitedVersions{b: queueKV, bucket: own.queue}}},
|
||||
Acted: awaitedReserved{awaitedVersions{b: actedKV, bucket: own.acted}}, Entry: entryVia(calls.Call), Send: sendVia(calls.Call),
|
||||
Unreleased: reserved.unreleasedWords, Max: republishMax, Logf: logger.Printf,
|
||||
Entry: entryVia(calls.Call), Send: sendVia(calls.Call),
|
||||
Unreleased: reserved.unreleasedWords, Logf: logger.Printf,
|
||||
Holders: holders} // a registry seat's holder from the seat-holders view, from the start (design 47 G A-416, A-416a)
|
||||
publishSet, err := publishSettingsOf(getenv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
releasedKept, err := numberFile(getenv("MESH_BUILDER_RELEASED_KEPT_FILE"), versions.DefaultReleasedKept)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reasksKV := process.AwaitBucket(ctx, js, conn, builds.StateBucket(cred.Module, build.ReAsksState), "builder_ask_again is not heard; an ask that can no longer be answered is still asked again by itself", logger.Printf)
|
||||
// A version's publish is the builder's own work until its release is written (design 47 G A-457a): an item of the re-ask state.
|
||||
items := &publishItems{reasks: reasksKV.KV, set: publishSet, now: time.Now, logf: logger.Printf,
|
||||
reserved: func(ctx context.Context, module, version string) (versions.ReservedVersion, bool, error) {
|
||||
return reserved.r.Of(ctx, module, version)
|
||||
},
|
||||
republish: republish.Republish, endedWith: reserved.unreleased,
|
||||
allDone: func(ctx context.Context) ([]versions.ReservedVersion, error) {
|
||||
all, err := reserved.r.All(ctx)
|
||||
return builtOf(all), err
|
||||
}}
|
||||
reasksKV.Ready(func(jetstream.KeyValue) { items.AskAllDone(ctx) })
|
||||
wirePublishItems(reserved, items, awaitedReserved{awaitedVersions{b: actedKV, bucket: own.acted}}, releasedKept)
|
||||
acts.Republish = republish.Republish
|
||||
reserved.publishAtEnd = republish.Ended
|
||||
// builder_reserve_done: a done trunk build that reserved none is given a version, and its publish queued as publish again does.
|
||||
acts.ReserveDone = reserveDoneAct(b.ReserveDone, republish.Republish, logger.Printf)
|
||||
again := &republishing{rp: republish}
|
||||
// The operator is asked once a key's event builds are spent (design 47 G §568): the builder's asker on the seat operator-channel.
|
||||
decided := &decisions{subject: oc.SubjectDecided(cred.Module)}
|
||||
@@ -387,9 +405,6 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
// And the four registry seats' published, refused and removed (DLV-B4), which move a numbered version.
|
||||
seatHeard := seatHearer{n: numberer, b: b, logf: logger.Printf, pub: pubs, res: reserved}
|
||||
events := func(m jetstream.Msg) {
|
||||
if again.heard(ctx, m, logger.Printf) { // the publish again's own subjects, left for the bus to give again when its state cannot take them
|
||||
return
|
||||
}
|
||||
if decided.heard(m) { // the operator's answer to an ask about a key (§568)
|
||||
return
|
||||
}
|
||||
@@ -462,8 +477,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
// A person asks a key's build again with builder_ask_again, which writes the reasks state (issue 1945): watched, as
|
||||
// every other input. The builder asks again by itself without it.
|
||||
// A state not on the bus yet at this start is watched once it appears (issue 1790).
|
||||
process.AwaitBucket(ctx, js, conn, builds.StateBucket(cred.Module, build.ReAsksState), "builder_ask_again is not heard; an ask that can no longer be answered is still asked again by itself", logger.Printf).Ready(func(kv jetstream.KeyValue) {
|
||||
eachReAsk := func(e jetstream.KeyValueEntry) { reAsked(ctx, b, acts, kv, e, logger.Printf) }
|
||||
reasksKV.Ready(func(kv jetstream.KeyValue) {
|
||||
eachReAsk := func(e jetstream.KeyValueEntry) { reAsked(ctx, b, acts, items, kv, e, logger.Printf) }
|
||||
if rw, err := settle(ctx, kv, eachReAsk); err != nil {
|
||||
process.NotWatched(logger.Printf, builds.StateBucket(cred.Module, build.ReAsksState), err, "builder_ask_again is not heard until the builder starts again")
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"mesh-delivery-builder/internal/build"
|
||||
"mesh-delivery-builder/internal/versions"
|
||||
)
|
||||
|
||||
// **A version's publish is the builder's own work until its release is written** (design 47 G A-457a, §588): each reservation of a
|
||||
// done build is a publish item in the builder's re-ask state, the mechanism builder_reserve_done and publish again use. It is
|
||||
// asked when the build ends and at the builder's start; each try queues the publish and writes the item again with one more try
|
||||
// and a longer pause; the release deletes it; after its tries it ends loudly and stays readable in builder_publish_queue.
|
||||
|
||||
// publishSettings are the settings publish-pause, publish-pause-max and publish-tries.
|
||||
type publishSettings struct {
|
||||
Pause, PauseMax time.Duration
|
||||
Tries int
|
||||
}
|
||||
|
||||
// defaultPublishSettings are the settings' defaults: 30 s, 30 min and 8 tries.
|
||||
var defaultPublishSettings = publishSettings{Pause: 30 * time.Second, PauseMax: 30 * time.Minute, Tries: 8}
|
||||
|
||||
// pauseAfter is the pause after the try-th try: the setting doubled per try, up to the maximum.
|
||||
func (s publishSettings) pauseAfter(try int) time.Duration {
|
||||
d := s.Pause
|
||||
for i := 1; i < try && d < s.PauseMax; i++ {
|
||||
d *= 2
|
||||
}
|
||||
return min(d, s.PauseMax)
|
||||
}
|
||||
|
||||
// publishItems asks, tries and ends the publish items.
|
||||
type publishItems struct {
|
||||
reasks func() jetstream.KeyValue // the re-ask state, nil until it is bound
|
||||
reserved func(ctx context.Context, module, version string) (versions.ReservedVersion, bool, error)
|
||||
republish func(ctx context.Context, module, version string) ([]string, error)
|
||||
endedWith func(ctx context.Context, v versions.ReservedVersion, why string) // raises the finding version-unreleased
|
||||
allDone func(ctx context.Context) ([]versions.ReservedVersion, error)
|
||||
set publishSettings
|
||||
now func() time.Time
|
||||
after func(time.Duration) <-chan time.Time
|
||||
logf func(string, ...any)
|
||||
}
|
||||
|
||||
func (p *publishItems) wait(ctx context.Context, d time.Duration) bool {
|
||||
if d <= 0 {
|
||||
return ctx.Err() == nil
|
||||
}
|
||||
after := p.after
|
||||
if after == nil {
|
||||
after = time.After
|
||||
}
|
||||
select {
|
||||
case <-after(d):
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Ask writes the publish item of a version whose build is done, when none stands: its first try is now.
|
||||
func (p *publishItems) Ask(ctx context.Context, module, version string) error {
|
||||
kv := p.reasks()
|
||||
if kv == nil {
|
||||
return errors.New("the re-ask state is not on the bus yet")
|
||||
}
|
||||
raw, err := json.Marshal(build.ReAsk{V: 1, Why: "the build ended with its version unreleased", By: "the builder", At: p.now().UTC(), Mode: build.ModePublish, Module: module, Version: version, NextTry: p.now().UTC()})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := kv.Create(ctx, versions.PublishItemKey(module, version), raw); err != nil && !errors.Is(err, jetstream.ErrKeyExists) {
|
||||
return fmt.Errorf("the publish item of %s %s cannot be written: %w", module, version, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AskAllDone asks the publish item of every reservation whose build is done and that has no release: at the builder's start, so
|
||||
// what stood before this change moves too, whatever its artifacts.
|
||||
func (p *publishItems) AskAllDone(ctx context.Context) {
|
||||
vs, err := p.allDone(ctx)
|
||||
if err != nil {
|
||||
p.logf("the reservations of done builds are not asked to publish at the start: %v", err)
|
||||
return
|
||||
}
|
||||
for _, v := range vs {
|
||||
if err := p.Ask(ctx, v.Module, v.Version); err != nil {
|
||||
p.logf("%v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Done deletes a version's publish item: its release is written.
|
||||
func (p *publishItems) Done(ctx context.Context, module, version string) {
|
||||
kv := p.reasks()
|
||||
if kv == nil {
|
||||
return
|
||||
}
|
||||
if err := kv.Delete(ctx, versions.PublishItemKey(module, version)); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
|
||||
p.logf("the publish item of %s %s is not deleted: %v", module, version, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Try waits for the item's next try, then, while the version is still reserved, queues its publish and writes the item again with
|
||||
// one more try and the pause after it; once the tries are spent it ends the item loudly. A version no longer reserved (released,
|
||||
// dropped or taken over) has its item deleted; one whose artifacts are published keeps it until its release is written.
|
||||
func (p *publishItems) Try(ctx context.Context, kv jetstream.KeyValue, e jetstream.KeyValueEntry, r build.ReAsk) {
|
||||
if !p.wait(ctx, r.NextTry.Sub(p.now())) {
|
||||
return
|
||||
}
|
||||
cur, err := kv.Get(ctx, e.Key())
|
||||
if err != nil || cur.Revision() != e.Revision() {
|
||||
return // deleted by the release, or written again
|
||||
}
|
||||
v, held, err := p.reserved(ctx, r.Module, r.Version)
|
||||
if err != nil {
|
||||
p.logf("%s %s: its reservation cannot be read, the publish item waits: %v", r.Module, r.Version, err)
|
||||
return
|
||||
}
|
||||
if !held { // dropped, taken over or released: never because its artifacts are published (A-456b)
|
||||
_ = kv.Delete(ctx, e.Key())
|
||||
return
|
||||
}
|
||||
if r.Tries >= p.set.Tries {
|
||||
r.Answer = fmt.Sprintf("the publish of %s %s was tried %d times and the version is not released; builder_publish_again tries it again", r.Module, r.Version, r.Tries)
|
||||
r.Answered = p.now().UTC()
|
||||
p.logf("%s: %s", e.Key(), r.Answer)
|
||||
if p.endedWith != nil {
|
||||
p.endedWith(ctx, v, "the publish ended after "+fmt.Sprint(r.Tries)+" tries")
|
||||
}
|
||||
} else {
|
||||
r.Tries++
|
||||
if _, err := p.republish(ctx, r.Module, r.Version); err != nil {
|
||||
p.logf("%s %s: try %d queued no publish: %v", r.Module, r.Version, r.Tries, err)
|
||||
}
|
||||
r.NextTry = p.now().UTC().Add(p.set.pauseAfter(r.Tries))
|
||||
}
|
||||
data, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if _, err := kv.Update(ctx, e.Key(), data, e.Revision()); err != nil && !errors.Is(err, jetstream.ErrKeyExists) && !errors.Is(err, jetstream.ErrKeyRevisionMismatch) {
|
||||
p.logf("%s: the publish item could not be written: %v", e.Key(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// numberFile reads a setting's number from the file the manifest declares: no file or an empty one is def; a content that is no
|
||||
// number of at least one is an error naming the file.
|
||||
func numberFile(path string, def int) (int, error) {
|
||||
if path == "" {
|
||||
return def, nil
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return def, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
v := strings.TrimSpace(string(raw))
|
||||
if v == "" {
|
||||
return def, nil
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil || n < 1 {
|
||||
return 0, fmt.Errorf("%s holds %q, which is not a number of at least one", path, v)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// publishSettingsOf reads publish-pause (seconds), publish-pause-max (minutes) and publish-tries from their files.
|
||||
func publishSettingsOf(getenv func(string) string) (publishSettings, error) {
|
||||
s := defaultPublishSettings
|
||||
pause, err := numberFile(getenv("MESH_BUILDER_PUBLISH_PAUSE_FILE"), int(s.Pause/time.Second))
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
pmax, err := numberFile(getenv("MESH_BUILDER_PUBLISH_PAUSE_MAX_FILE"), int(s.PauseMax/time.Minute))
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
tries, err := numberFile(getenv("MESH_BUILDER_PUBLISH_TRIES_FILE"), s.Tries)
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
return publishSettings{Pause: time.Duration(pause) * time.Second, PauseMax: time.Duration(pmax) * time.Minute, Tries: tries}, nil
|
||||
}
|
||||
|
||||
// wirePublishItems joins the reservations to the publish items (design 47 G A-457a, A-456a, A-456b): a done build asks its item, a
|
||||
// release, a drop and a takeover end it, and a release is recorded in releases, the last kept of each module.
|
||||
func wirePublishItems(rv *reservations, items *publishItems, releases versions.ReserveStore, kept int) {
|
||||
rv.built, rv.released = items.Ask, items.Done
|
||||
rv.r.ReleasedKept, rv.r.Releases = kept, releases
|
||||
}
|
||||
|
||||
// builtOf are the reservations whose build is done, whatever their artifacts' state: each is asked its publish item at the start
|
||||
// (design 47 G A-456b), which goes only with its release.
|
||||
func builtOf(all []versions.ReservedVersion) []versions.ReservedVersion {
|
||||
var out []versions.ReservedVersion
|
||||
for _, v := range all {
|
||||
if !v.BuiltAt.IsZero() {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
|
||||
"mesh-delivery-builder/internal/build"
|
||||
"mesh-delivery-builder/internal/versions"
|
||||
)
|
||||
|
||||
// itemKV is the re-ask state in memory: one revision per key.
|
||||
type itemKV struct {
|
||||
jetstream.KeyValue
|
||||
vals map[string][]byte
|
||||
revs map[string]uint64
|
||||
rev uint64
|
||||
}
|
||||
|
||||
type itemEntry struct {
|
||||
jetstream.KeyValueEntry
|
||||
key string
|
||||
value []byte
|
||||
rev uint64
|
||||
}
|
||||
|
||||
func (e itemEntry) Key() string { return e.key }
|
||||
func (e itemEntry) Value() []byte { return e.value }
|
||||
func (e itemEntry) Revision() uint64 { return e.rev }
|
||||
func (e itemEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }
|
||||
|
||||
func (k *itemKV) entry(key string) jetstream.KeyValueEntry {
|
||||
return itemEntry{key: key, value: k.vals[key], rev: k.revs[key]}
|
||||
}
|
||||
func (k *itemKV) Get(_ context.Context, key string) (jetstream.KeyValueEntry, error) {
|
||||
if _, ok := k.vals[key]; !ok {
|
||||
return nil, jetstream.ErrKeyNotFound
|
||||
}
|
||||
return k.entry(key), nil
|
||||
}
|
||||
func (k *itemKV) Create(_ context.Context, key string, v []byte, _ ...jetstream.KVCreateOpt) (uint64, error) {
|
||||
if _, ok := k.vals[key]; ok {
|
||||
return 0, jetstream.ErrKeyExists
|
||||
}
|
||||
k.rev++
|
||||
k.vals[key], k.revs[key] = v, k.rev
|
||||
return k.rev, nil
|
||||
}
|
||||
func (k *itemKV) Update(_ context.Context, key string, v []byte, rev uint64) (uint64, error) {
|
||||
if k.revs[key] != rev {
|
||||
return 0, jetstream.ErrKeyExists
|
||||
}
|
||||
k.rev++
|
||||
k.vals[key], k.revs[key] = v, k.rev
|
||||
return k.rev, nil
|
||||
}
|
||||
func (k *itemKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOpt) error {
|
||||
delete(k.vals, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeItem(t *testing.T, raw []byte) build.ReAsk {
|
||||
t.Helper()
|
||||
r, err := build.DecodeReAsk(raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// TestAPublishItemIsTriedWithAGrowingPauseAndEndsLoudly: design 47 G A-457a — a version's publish item is asked once, each try queues
|
||||
// the publish and writes the item again with one more try and the pause doubled up to its maximum, the release deletes it, and
|
||||
// after the setting's tries it ends with its words, raises the condition and stays; a version no longer reserved has it deleted.
|
||||
func TestAPublishItemIsTriedWithAGrowingPauseAndEndsLoudly(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
kv := &itemKV{vals: map[string][]byte{}, revs: map[string]uint64{}}
|
||||
now := time.Date(2026, 11, 17, 1, 0, 0, 0, time.UTC)
|
||||
var waited []time.Duration
|
||||
var queued, ended []string
|
||||
reserved := true
|
||||
v := versions.ReservedVersion{Module: "m", Version: "0.1.2", Artifacts: map[string]string{"m:a": "sha256:a"}, BuiltAt: now}
|
||||
p := &publishItems{reasks: func() jetstream.KeyValue { return kv }, set: publishSettings{Pause: 30 * time.Second, PauseMax: 2 * time.Minute, Tries: 4},
|
||||
now: func() time.Time { return now }, logf: t.Logf,
|
||||
after: func(d time.Duration) <-chan time.Time {
|
||||
waited = append(waited, d)
|
||||
now = now.Add(d)
|
||||
c := make(chan time.Time, 1)
|
||||
c <- now
|
||||
return c
|
||||
},
|
||||
reserved: func(_ context.Context, _, ver string) (versions.ReservedVersion, bool, error) {
|
||||
if ver == "0.3.0" { // every artifact published, its release not written yet
|
||||
return versions.ReservedVersion{Module: "m", Version: ver, Artifacts: map[string]string{"m:a": "sha256:a"}, Published: map[string]bool{"m:a": true}, BuiltAt: now}, true, nil
|
||||
}
|
||||
return v, reserved, nil
|
||||
},
|
||||
republish: func(_ context.Context, m, ver string) ([]string, error) {
|
||||
queued = append(queued, m+" "+ver)
|
||||
return nil, errors.New("no holder")
|
||||
},
|
||||
endedWith: func(_ context.Context, v versions.ReservedVersion, why string) {
|
||||
ended = append(ended, v.Version+": "+why)
|
||||
}}
|
||||
key := versions.PublishItemKey("m", "0.1.2")
|
||||
if err := p.Ask(ctx, "m", "0.1.2"); err != nil || p.Ask(ctx, "m", "0.1.2") != nil || len(kv.vals) != 1 {
|
||||
t.Fatalf("asked: %v, %d items, want one item however often it is asked", err, len(kv.vals))
|
||||
}
|
||||
for i := 0; i < 5; i++ {
|
||||
e, _ := kv.Get(ctx, key)
|
||||
p.Try(ctx, kv, e, decodeItem(t, e.Value()))
|
||||
}
|
||||
got := decodeItem(t, kv.vals[key])
|
||||
if len(queued) != 4 || got.Tries != 4 || got.Answer == "" || got.Answered.IsZero() || len(ended) != 1 || !strings.Contains(ended[0], "4 tries") {
|
||||
t.Fatalf("queued %v, item %+v, ended %v: want four tries, then the end said with its words and the condition", queued, got, ended)
|
||||
}
|
||||
if want := []time.Duration{30 * time.Second, time.Minute, 2 * time.Minute, 2 * time.Minute}; len(waited) != len(want) || waited[0] != want[0] || waited[1] != want[1] || waited[2] != want[2] || waited[3] != want[3] {
|
||||
t.Errorf("pauses before the tries and the end = %v, want %v (doubled, capped)", waited, want)
|
||||
}
|
||||
// An item whose artifacts are all published stays until its release is written, and is tried (A-456b).
|
||||
queued = nil
|
||||
_ = p.Ask(ctx, "m", "0.3.0")
|
||||
pk := versions.PublishItemKey("m", "0.3.0")
|
||||
pe, _ := kv.Get(ctx, pk)
|
||||
p.Try(ctx, kv, pe, decodeItem(t, pe.Value()))
|
||||
if _, ok := kv.vals[pk]; !ok || len(queued) != 1 || decodeItem(t, kv.vals[pk]).Tries != 1 {
|
||||
t.Errorf("an item whose artifacts are published was deleted or not tried: %v, queued %v", kv.vals, queued)
|
||||
}
|
||||
kv.vals, kv.revs = map[string][]byte{}, map[string]uint64{}
|
||||
// At the start every reservation of a done build with no release is asked, one item each.
|
||||
kv.vals, kv.revs = map[string][]byte{}, map[string]uint64{}
|
||||
p.allDone = func(context.Context) ([]versions.ReservedVersion, error) {
|
||||
published := versions.ReservedVersion{Module: "n", Version: "0.2.0", Artifacts: map[string]string{"n:a": "sha256:a"}, Published: map[string]bool{"n:a": true}, BuiltAt: now}
|
||||
return builtOf([]versions.ReservedVersion{v, published, {Module: "o", Version: "0.1.0"}}), nil // the last build has not ended
|
||||
}
|
||||
p.AskAllDone(ctx)
|
||||
if len(kv.vals) != 2 {
|
||||
t.Errorf("the start asked %d items, want one per done unreleased reservation", len(kv.vals))
|
||||
}
|
||||
// A released version's item is deleted; a version no longer reserved has it deleted at its next try.
|
||||
p.Done(ctx, "m", "0.1.2")
|
||||
p.Done(ctx, "n", "0.2.0")
|
||||
if len(kv.vals) != 0 {
|
||||
t.Errorf("the release left %v", kv.vals)
|
||||
}
|
||||
_ = p.Ask(ctx, "m", "0.1.2")
|
||||
reserved = false
|
||||
e, _ := kv.Get(ctx, key)
|
||||
p.Try(ctx, kv, e, decodeItem(t, e.Value()))
|
||||
if len(kv.vals) != 0 {
|
||||
t.Errorf("a dropped reservation left its item %v", kv.vals)
|
||||
}
|
||||
}
|
||||
|
||||
// TestThePublishSettingsAreReadFromTheirFiles: design 47 G A-457a — the three settings, 30 s, 30 min and 8 tries unless set, and a
|
||||
// content that is no number is refused naming its file.
|
||||
func TestThePublishSettingsAreReadFromTheirFiles(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
write := func(name, v string) string {
|
||||
p := filepath.Join(dir, name)
|
||||
if err := os.WriteFile(p, []byte(v), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
env := map[string]string{}
|
||||
got, err := publishSettingsOf(func(k string) string { return env[k] })
|
||||
if err != nil || got != defaultPublishSettings || got.Pause != 30*time.Second || got.PauseMax != 30*time.Minute || got.Tries != 8 {
|
||||
t.Fatalf("defaults = %+v (%v)", got, err)
|
||||
}
|
||||
env["MESH_BUILDER_PUBLISH_PAUSE_FILE"], env["MESH_BUILDER_PUBLISH_PAUSE_MAX_FILE"], env["MESH_BUILDER_PUBLISH_TRIES_FILE"] = write("p", "5\n"), write("m", "2"), write("t", "3")
|
||||
if got, err = publishSettingsOf(func(k string) string { return env[k] }); err != nil || got != (publishSettings{5 * time.Second, 2 * time.Minute, 3}) {
|
||||
t.Errorf("set = %+v (%v)", got, err)
|
||||
}
|
||||
env["MESH_BUILDER_PUBLISH_TRIES_FILE"] = write("bad", "many")
|
||||
if _, err = publishSettingsOf(func(k string) string { return env[k] }); err == nil || !strings.Contains(err.Error(), "bad") {
|
||||
t.Errorf("a content that is no number: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestThePublishItemsAreWiredToTheReservationsAndTheReAskWatch: design 47 G A-457a, A-456b — a mode-publish value of the re-ask state
|
||||
// reaches the item's try through reAsked; a done build gets an item; the catalog's version-released, a drop and a takeover delete it
|
||||
// and the release is kept by module.
|
||||
func TestThePublishItemsAreWiredToTheReservationsAndTheReAskWatch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
kv := &itemKV{vals: map[string][]byte{}, revs: map[string]uint64{}}
|
||||
st := &memFindings{vals: map[string][]byte{}}
|
||||
found := &findings{module: builds.BuilderModule, w: st, logf: t.Logf, now: func() time.Time { return time.Unix(0, 0) }}
|
||||
call := func(string, any) (json.RawMessage, error) {
|
||||
return json.Marshal(meshcatalog.VersionsAnswer{Module: "m"})
|
||||
}
|
||||
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, call, func() jetstream.KeyValue { return &boundsKV{keys: map[string]bool{}} }, found, nil, t.Logf)
|
||||
rv.filled.Store(true)
|
||||
tried := make(chan string, 4)
|
||||
items := &publishItems{reasks: func() jetstream.KeyValue { return kv }, set: defaultPublishSettings, now: time.Now, logf: t.Logf,
|
||||
reserved: func(ctx context.Context, m, v string) (versions.ReservedVersion, bool, error) {
|
||||
return rv.r.Of(ctx, m, v)
|
||||
},
|
||||
republish: func(_ context.Context, m, v string) ([]string, error) { tried <- m + " " + v; return nil, nil }}
|
||||
releases := &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}
|
||||
wirePublishItems(rv, items, releases, 2)
|
||||
done := func(fp string) versions.ReservedVersion {
|
||||
v, err := rv.Reserve(ctx, "m", "0.1", []string{"m:a"}, versions.Source{Fingerprint: fp})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := rv.Built(ctx, "m", v.Version, map[string]string{"m:a": "sha256:a"}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
first := done("src2:one")
|
||||
key := versions.PublishItemKey("m", first.Version)
|
||||
if _, ok := kv.vals[key]; !ok {
|
||||
t.Fatalf("a done build has no publish item: %v", kv.vals)
|
||||
}
|
||||
// A build whose artifacts are all published still gets its item, until its release is written (A-456b).
|
||||
if _, _, err := rv.r.Published(ctx, versions.NameOf("m", "a"), first.Version, "sha256:a"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items.Done(ctx, "m", first.Version)
|
||||
if err := rv.Built(ctx, "m", first.Version, map[string]string{"m:a": "sha256:a"}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := kv.vals[key]; !ok {
|
||||
t.Fatalf("a build with every artifact published has no item: %v", kv.vals)
|
||||
}
|
||||
// The watch hands the item to its try, which queues the publish.
|
||||
e, _ := kv.Get(ctx, key)
|
||||
reAsked(ctx, nil, versions.Acts{}, items, kv, e, t.Logf)
|
||||
select {
|
||||
case got := <-tried:
|
||||
if got != "m "+first.Version {
|
||||
t.Errorf("tried %q", got)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("a mode-publish value was not handed to the item's try")
|
||||
}
|
||||
// A takeover by another build deletes the older's item and asks the newer's.
|
||||
second := done("src2:two")
|
||||
if _, ok := kv.vals[key]; ok {
|
||||
t.Errorf("the taken-over reservation kept its item")
|
||||
}
|
||||
secondKey := versions.PublishItemKey("m", second.Version)
|
||||
if _, ok := kv.vals[secondKey]; !ok {
|
||||
t.Fatalf("the newer build has no item: %v", kv.vals)
|
||||
}
|
||||
// version-released deletes the item and keeps the release.
|
||||
msg := &wakeMsg{subject: meshcatalog.SubjectVersionReleased, data: []byte(`{"v":1,"module":"m","version":"` + second.Version + `","source":1,"artifacts":[]}`)}
|
||||
if !rv.heard(ctx, msg) || !msg.acked {
|
||||
t.Fatal("version-released was not taken in")
|
||||
}
|
||||
if _, ok := kv.vals[secondKey]; ok {
|
||||
t.Errorf("a released version kept its publish item")
|
||||
}
|
||||
if ok, _ := rv.ReleasedFor(ctx, "m", "src2:two"); !ok {
|
||||
t.Errorf("the release is not kept by fingerprint")
|
||||
}
|
||||
// A drop deletes the item too.
|
||||
third := done("src2:three")
|
||||
if _, err := rv.dropped(ctx, "m", "0.1", "op", "never", time.Unix(0, 0)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := kv.vals[versions.PublishItemKey("m", third.Version)]; ok {
|
||||
t.Errorf("a dropped reservation kept its item")
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ const buildAgainBound = 15 * time.Second
|
||||
// reAsked answers one value of the reasks state (the tool builder_ask_again, issue 1945): a value not yet answered asks the
|
||||
// key's waiting lanes again and is written back, by compare-and-set at its revision, with what the builder did. A value
|
||||
// answered already (this builder's own write) or deleted is left.
|
||||
func reAsked(ctx context.Context, b askAgainer, acts versions.Acts, kv jetstream.KeyValue, e jetstream.KeyValueEntry, logf func(string, ...any)) {
|
||||
func reAsked(ctx context.Context, b askAgainer, acts versions.Acts, items *publishItems, kv jetstream.KeyValue, e jetstream.KeyValueEntry, logf func(string, ...any)) {
|
||||
if e.Operation() != jetstream.KeyValuePut {
|
||||
return
|
||||
}
|
||||
@@ -45,6 +45,13 @@ func reAsked(ctx context.Context, b askAgainer, acts versions.Acts, kv jetstream
|
||||
if r.Answer != "" {
|
||||
return
|
||||
}
|
||||
if r.Mode == build.ModePublish {
|
||||
// A version's publish item (design 47 G A-457a): its try waits for its pause, so it is made beside the watch.
|
||||
if items != nil {
|
||||
go items.Try(ctx, kv, e, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
if r.Mode == build.ModeBuild {
|
||||
// The answer comes when the claim and the asks are written, so it is made beside the watch, not in it.
|
||||
go builtAgain(ctx, b, kv, e, r, logf)
|
||||
@@ -58,7 +65,7 @@ func reAsked(ctx context.Context, b askAgainer, acts versions.Acts, kv jetstream
|
||||
released(ctx, b, kv, e, r, logf)
|
||||
return
|
||||
}
|
||||
if r.Mode == build.ModeRepublish || r.Mode == build.ModeFloorAgain || r.Mode == build.ModeOldFormDrop || r.Mode == build.ModeReservedDrop {
|
||||
if r.Mode == build.ModeRepublish || r.Mode == build.ModeFloorAgain || r.Mode == build.ModeOldFormDrop || r.Mode == build.ModeReservedDrop || r.Mode == build.ModeReserveDone {
|
||||
// A version's hand act (design 51 *Tools of the builder*) may build again, so it is answered beside the watch.
|
||||
go versionActed(ctx, acts, kv, e, r, logf)
|
||||
return
|
||||
@@ -201,6 +208,12 @@ func versionActed(ctx context.Context, acts versions.Acts, kv jetstream.KeyValue
|
||||
r.Answer = "queued " + fmt.Sprint(len(r.Queued)) + " versions to publish again: " + strings.Join(r.Queued, ", ") + "; builder_publish_queue shows them"
|
||||
}
|
||||
}
|
||||
} else if r.Mode == build.ModeReserveDone {
|
||||
if acts.ReserveDone == nil {
|
||||
err = errors.New("this builder keeps no reservations")
|
||||
} else {
|
||||
r.Answer, err = acts.ReserveDone(ctx, r.Name, r.FP, r.By, r.Why, r.At)
|
||||
}
|
||||
} else if r.Mode == build.ModeReservedDrop {
|
||||
if acts.DropReserved == nil {
|
||||
err = errors.New("this builder keeps no reservations")
|
||||
|
||||
@@ -3,17 +3,11 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/gopackageregistry"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
|
||||
@@ -77,32 +71,8 @@ func publishBody(p versions.Publish) gopackageregistry.PublishInput {
|
||||
return gopackageregistry.PublishInput{Commit: p.Commit, Digest: p.Digest, Fingerprint: p.Fingerprint, Kind: p.Kind, Name: p.Name, Transfer: p.Transfer, Version: p.Version}
|
||||
}
|
||||
|
||||
// republishMaxOf reads the setting republish-max-versions from the file the manifest declares (design 47 G §633, A-71's
|
||||
// form): no file or an empty one is the default; a content that is no positive number is an error naming the file.
|
||||
func republishMaxOf(path string) (int, error) {
|
||||
if path == "" {
|
||||
return versions.DefaultRepublishMax, nil
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return versions.DefaultRepublishMax, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
v := strings.TrimSpace(string(raw))
|
||||
if v == "" {
|
||||
return versions.DefaultRepublishMax, nil
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil || n < 1 {
|
||||
return 0, fmt.Errorf("%s holds %q, which is not a number of versions", path, v)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// republishing starts the publish again once both its states are bound: the queue is taken up again and the last running
|
||||
// of every node's module is read (§556), acted on where its sequence is newer than the kept one.
|
||||
// republishing starts the publish again once both its states are bound: the queue is taken up again (design 47 G §588). A version's
|
||||
// publish is asked when its build ends and at the start (A-457a), never waited on a running or manifest-reads-changed.
|
||||
type republishing struct {
|
||||
rp *versions.Republisher
|
||||
ready atomic.Int32
|
||||
@@ -116,32 +86,5 @@ func (g *republishing) bound(ctx context.Context, js jetstream.JetStream, logf f
|
||||
if err := g.rp.Resume(ctx); err != nil {
|
||||
logf("the queued publishes are not taken up again: %v", err)
|
||||
}
|
||||
err := lastPerSubject(ctx, js, nodeengine.StreamRunning, nodeengine.SubjectRunningAll, func(subject string, data []byte, seq uint64) {
|
||||
if _, err := g.rp.Heard(ctx, subject, data, seq); err != nil {
|
||||
logf("the running %s is not acted on at the start: %v", subject, err)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
logf("the running of the node-engine seat is not read for the publish again at the start: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// heard hands a message of the events consumer to the publish again: one of its subjects whose state cannot take it is left
|
||||
// for the bus to give again (Nak); every message goes on to the other parts.
|
||||
func (g *republishing) heard(ctx context.Context, m jetstream.Msg, logf func(string, ...any)) (stop bool) {
|
||||
meta, err := m.Metadata()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
handled, err := g.rp.Heard(ctx, m.Subject(), m.Data(), meta.Sequence.Stream)
|
||||
if handled && err != nil {
|
||||
logf("%v; heard again", err)
|
||||
if nakErr := m.Nak(); nakErr != nil {
|
||||
logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
|
||||
}
|
||||
givenUp(m, "its event", err, logf)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -59,18 +57,6 @@ func TestRepublishSend(t *testing.T) {
|
||||
t.Errorf("%s: asked %q, error %v, want %q", name, asked, err, c.want)
|
||||
}
|
||||
}
|
||||
dir := t.TempDir()
|
||||
for content, want := range map[string]int{"": 50, "7": 7} {
|
||||
path := filepath.Join(dir, "max")
|
||||
_ = os.WriteFile(path, []byte(content), 0o600)
|
||||
if got, err := republishMaxOf(path); err != nil || got != want {
|
||||
t.Errorf("republishMaxOf(%q) = %d, %v, want %d", content, got, err, want)
|
||||
}
|
||||
}
|
||||
_ = os.WriteFile(filepath.Join(dir, "bad"), []byte("0"), 0o600)
|
||||
if _, err := republishMaxOf(filepath.Join(dir, "bad")); err == nil {
|
||||
t.Error("a setting of 0 is accepted")
|
||||
}
|
||||
if _, _, err := build.NewRepublish("", "0.1.3", "why", "me", versionsNow()); err == nil {
|
||||
t.Error("a version with no module is accepted")
|
||||
}
|
||||
|
||||
@@ -40,19 +40,20 @@ func (s awaitedReserved) Delete(ctx context.Context, key string) error {
|
||||
// reservations is the builder's reserving: the Reserver, the fill the catalog said, the bounds of a built reservation's
|
||||
// publish and the findings.
|
||||
type reservations struct {
|
||||
r *versions.Reserver
|
||||
filled atomic.Bool
|
||||
bounds func() jetstream.KeyValue
|
||||
found *findings
|
||||
bound time.Duration
|
||||
wake func(context.Context)
|
||||
logf func(string, ...any)
|
||||
r *versions.Reserver
|
||||
// built asks a done build's publish (design 47 G A-457a), released ends its item; nil are none.
|
||||
built func(ctx context.Context, module, version string) error
|
||||
released func(ctx context.Context, module, version string)
|
||||
filled atomic.Bool
|
||||
bounds func() jetstream.KeyValue
|
||||
found *findings
|
||||
bound time.Duration
|
||||
wake func(context.Context)
|
||||
logf func(string, ...any)
|
||||
// call asks a seat's verb: the git seat's tag (design 47 G §370).
|
||||
call toolCaller
|
||||
// say says package-published on the seat mesh-builds; nil says none.
|
||||
say func(context.Context, meshbuilds.PackagePublished) error
|
||||
// publishAtEnd asks the tagged publish of a reservation whose build has ended (design 47 G A-473); nil asks none.
|
||||
publishAtEnd func(ctx context.Context, module, version string) ([]string, error)
|
||||
}
|
||||
|
||||
// newReservations makes the reserving over store; the released versions of a module are asked of the seat mesh-catalog's
|
||||
@@ -74,11 +75,41 @@ func newReservations(store versions.ReserveStore, call toolCaller, bounds func()
|
||||
}
|
||||
return out, nil
|
||||
}}
|
||||
rv.r.Took = rv.took
|
||||
return rv
|
||||
}
|
||||
|
||||
// took is a build's takeover of the reservation of an older build never released (design 47 G A-455): the older version is
|
||||
// dropped, its bound ended and its finding cleared (the newer build's reservation has its own), and the builder's log says so.
|
||||
func (rv *reservations) took(ctx context.Context, older, newer versions.ReservedVersion) {
|
||||
if older.Fingerprint == newer.Fingerprint {
|
||||
rv.logf("%s: the build of %s reserves again: %s takes the place of %s", newer.Module, newer.Fingerprint, newer.Version, older.Version)
|
||||
} else {
|
||||
rv.logf("%s: the reservation of %s (build of %s, never released) is dropped: the build of %s takes the series over and reserves %s", newer.Module, older.Version, older.Fingerprint, newer.Fingerprint, newer.Version)
|
||||
}
|
||||
if rv.released != nil {
|
||||
rv.released(ctx, older.Module, older.Version) // its publish item goes with its reservation (A-456b)
|
||||
}
|
||||
rv.dropBound(ctx, versions.ReservedKey(older.Module, older.Series))
|
||||
rv.found.clear(ctx, versions.VersionUnreleased, meshconditionsclient.ScopeModule, older.Module)
|
||||
}
|
||||
|
||||
var _ build.Reservations = (*reservations)(nil)
|
||||
|
||||
// ReservedFor is the reservation made for the build of fingerprint.
|
||||
func (rv *reservations) ReservedFor(ctx context.Context, module, fingerprint string) (versions.ReservedVersion, bool, error) {
|
||||
all, err := rv.r.All(ctx)
|
||||
if err != nil {
|
||||
return versions.ReservedVersion{}, false, err
|
||||
}
|
||||
for _, v := range all {
|
||||
if v.Module == module && v.Fingerprint == fingerprint {
|
||||
return v, true, nil
|
||||
}
|
||||
}
|
||||
return versions.ReservedVersion{}, false, nil
|
||||
}
|
||||
|
||||
func (rv *reservations) Filled() bool { return rv.filled.Load() }
|
||||
|
||||
func (rv *reservations) Reserve(ctx context.Context, module, series string, artifacts []string, src versions.Source) (versions.ReservedVersion, error) {
|
||||
@@ -98,6 +129,11 @@ func (rv *reservations) Built(ctx context.Context, module, version string, diges
|
||||
return err
|
||||
}
|
||||
rv.arm(ctx, cur)
|
||||
if rv.built != nil {
|
||||
if err := rv.built(ctx, module, version); err != nil {
|
||||
rv.logf("%v", err)
|
||||
}
|
||||
}
|
||||
// A published heard before the digests were recorded marks its artifact now, said as one heard after (design 47 G A-415).
|
||||
marked, unmatched, err := rv.r.Kept(ctx, module, version)
|
||||
if err != nil {
|
||||
@@ -105,16 +141,14 @@ func (rv *reservations) Built(ctx context.Context, module, version string, diges
|
||||
}
|
||||
for _, h := range unmatched {
|
||||
rv.logf("a published of %s@%s kept on %s %s marks no artifact: its digest %s is none of the built digests %v", h.Name, h.Version, module, version, h.Digest, cur.Artifacts)
|
||||
// A build whose recorded digest differs from what the holder holds at its version fails loudly at once: the version is taken
|
||||
// and no artifact is marked for it, so it is never released (design 47 G A-490j).
|
||||
rv.unreleased(ctx, cur, fmt.Sprintf("the holder holds %s@%s under digest %s, which no artifact of the build carries (%v): the version is taken and is never released",
|
||||
h.Name, h.Version, h.Digest, cur.Artifacts))
|
||||
}
|
||||
for _, m := range marked {
|
||||
rv.sayPublished(ctx, m, m.Reserved.Artifacts[m.Artifact])
|
||||
}
|
||||
// The build's end asks the tagged publish itself, at the holder of each artifact's registry seat (design 47 G A-473).
|
||||
if rv.publishAtEnd != nil {
|
||||
if _, err := rv.publishAtEnd(ctx, module, version); err != nil {
|
||||
rv.logf("the publish of %s %s is not asked at the build's end: %v", module, version, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -215,6 +249,9 @@ func (rv *reservations) heard(ctx context.Context, m jetstream.Msg) bool {
|
||||
for _, k := range gone {
|
||||
rv.dropBound(ctx, k)
|
||||
}
|
||||
if rv.released != nil {
|
||||
rv.released(ctx, ev.Module, ev.Version)
|
||||
}
|
||||
rv.found.clear(ctx, versions.VersionUnreleased, meshconditionsclient.ScopeModule, ev.Module)
|
||||
default:
|
||||
return false
|
||||
@@ -257,10 +294,14 @@ func (rv *reservations) sayPublished(ctx context.Context, m versions.Marked, dig
|
||||
|
||||
// dropped is builder_reserved_drop: the reservation is deleted and its bound ended.
|
||||
func (rv *reservations) dropped(ctx context.Context, module, series, by, why string, at time.Time) (string, error) {
|
||||
cur, held, _ := rv.r.At(ctx, versions.ReservedKey(module, series))
|
||||
ok, err := rv.r.Drop(ctx, module, series)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok && held && rv.released != nil {
|
||||
rv.released(ctx, module, cur.Version) // its publish item goes with its reservation (A-456b)
|
||||
}
|
||||
if !ok {
|
||||
return "", fmt.Errorf("%s holds no reservation of %s", module, series)
|
||||
}
|
||||
@@ -318,3 +359,27 @@ func (rv *reservations) noMark(ctx context.Context, o versions.Outcome) string {
|
||||
}
|
||||
return strings.Join(out, "; ")
|
||||
}
|
||||
|
||||
// reserveDoneAct is builder_reserve_done's act (design 47 G A-455): the next version of the series is reserved for a done trunk build
|
||||
// that reserved none, and its publish is queued as builder_publish_again queues it. Its answer says the version and the queued
|
||||
// publishes; a reservation whose publish could not be queued is said so, with the tool that queues it.
|
||||
func reserveDoneAct(reserve func(ctx context.Context, module, fp string) (versions.ReservedVersion, error),
|
||||
republish func(ctx context.Context, module, version string) ([]string, error), logf func(string, ...any)) func(ctx context.Context, module, fp, by, why string, at time.Time) (string, error) {
|
||||
return func(ctx context.Context, module, fp, by, why string, at time.Time) (string, error) {
|
||||
res, err := reserve(ctx, module, fp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
logf("%s at %s: version %s is reserved by hand (%s, %s) for the done build", module, fp, res.Version, by, why)
|
||||
queued, err := republish(ctx, module, res.Version)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s is reserved for %s at %s, but its publish is not queued (builder_publish_again queues it): %w", res.Version, module, fp, err)
|
||||
}
|
||||
return fmt.Sprintf("reserved %s for %s at %s and queued %d versions to publish: %s; builder_publish_queue shows them", res.Version, module, fp, len(queued), strings.Join(queued, ", ")), nil
|
||||
}
|
||||
}
|
||||
|
||||
// ReleasedFor says whether a version was released for the build of fingerprint (design 47 G A-456a).
|
||||
func (rv *reservations) ReleasedFor(ctx context.Context, module, fingerprint string) (bool, error) {
|
||||
return rv.r.ReleasedFor(ctx, module, fingerprint)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
|
||||
"mesh-delivery-builder/internal/build"
|
||||
"mesh-delivery-builder/internal/versions"
|
||||
)
|
||||
|
||||
@@ -89,6 +91,9 @@ func TestTheCatalogsWordsMoveTheReservations(t *testing.T) {
|
||||
}
|
||||
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, call, func() jetstream.KeyValue { return bounds }, found,
|
||||
func(context.Context) { woke++ }, t.Logf)
|
||||
var items []string // design 47 G A-457a: the end of a build asks its publish, the release ends it
|
||||
rv.built = func(_ context.Context, m, v string) error { items = append(items, "asked "+m+" "+v); return nil }
|
||||
rv.released = func(_ context.Context, m, v string) { items = append(items, "ended "+m+" "+v) }
|
||||
if _, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a"}, versions.Source{}); err == nil || rv.Filled() {
|
||||
t.Fatalf("a version was reserved before versions-filled (%v)", err)
|
||||
}
|
||||
@@ -102,16 +107,13 @@ func TestTheCatalogsWordsMoveTheReservations(t *testing.T) {
|
||||
if err != nil || res.Version != "1.4.3" {
|
||||
t.Fatalf("reserved %q (%v), want 1.4.3 after the catalog's 1.4.2", res.Version, err)
|
||||
}
|
||||
var asked []string
|
||||
rv.publishAtEnd = func(_ context.Context, module, version string) ([]string, error) {
|
||||
asked = append(asked, module+" "+version)
|
||||
return nil, nil
|
||||
}
|
||||
items = nil
|
||||
if err := rv.Built(ctx, "m", "1.4.3", map[string]string{"m:a": "sha256:a", "m:b": "sha256:b"}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(asked) != 1 || asked[0] != "m 1.4.3" {
|
||||
t.Fatalf("the build's end asked the publish of %v, want m 1.4.3 once (A-473)", asked)
|
||||
// The build's end asks the publish item once, the one mechanism (design 47 G A-457a, A-473).
|
||||
if len(items) != 1 || items[0] != "asked m 1.4.3" {
|
||||
t.Fatalf("the build's end asked %v, want the publish item of m 1.4.3 once (A-457a, A-473)", items)
|
||||
}
|
||||
boundKey := versions.ReservedBoundKey("m", "1.4")
|
||||
if !bounds.keys[boundKey] {
|
||||
@@ -136,6 +138,9 @@ func TestTheCatalogsWordsMoveTheReservations(t *testing.T) {
|
||||
if !rv.heard(ctx, released) || !released.acked {
|
||||
t.Fatalf("version-released was not taken in (acked %v)", released.acked)
|
||||
}
|
||||
if strings.Join(items, ", ") != "asked m 1.4.3, ended m 1.4.3" {
|
||||
t.Errorf("publish items %v, want the build's end to ask the publish and the release to end it", items)
|
||||
}
|
||||
if all, _ := rv.r.All(ctx); len(all) != 0 || bounds.keys[boundKey] || len(st.vals) != 0 {
|
||||
t.Errorf("after version-released: reservations %v, bounds %v, findings %v; want none", all, bounds.keys, st.vals)
|
||||
}
|
||||
@@ -216,13 +221,14 @@ func TestAReleasedVersionIsTaggedAtTheCommitItWasBuiltFrom(t *testing.T) {
|
||||
// is said in the log.
|
||||
func TestAPublishedHeardBeforeTheDigestsIsSaidWhenTheBuildRecordsThem(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
fs := &memFindings{vals: map[string][]byte{}}
|
||||
var logs []string
|
||||
logf := func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }
|
||||
none := func(string, any) (json.RawMessage, error) {
|
||||
return json.Marshal(meshcatalog.VersionsAnswer{Module: "m"})
|
||||
}
|
||||
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, none, func() jetstream.KeyValue { return &boundsKV{keys: map[string]bool{}} },
|
||||
&findings{module: builds.BuilderModule, w: &memFindings{vals: map[string][]byte{}}, logf: t.Logf, now: time.Now}, nil, logf)
|
||||
&findings{module: builds.BuilderModule, w: fs, logf: t.Logf, now: time.Now}, nil, logf)
|
||||
var said []meshbuilds.PackagePublished
|
||||
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { said = append(said, p); return nil }
|
||||
rv.filled.Store(true)
|
||||
@@ -244,9 +250,137 @@ func TestAPublishedHeardBeforeTheDigestsIsSaidWhenTheBuildRecordsThem(t *testing
|
||||
if log := strings.Join(logs, "\n"); !strings.Contains(log, "is kept on") || !strings.Contains(log, "m-x@"+res.Version+" kept on m "+res.Version+" marks no artifact") {
|
||||
t.Fatalf("log = %q, want the published kept and the one never matching said", logs)
|
||||
}
|
||||
// A-490j: the one that never matched is raised at once, naming the version taken and the holder's digest.
|
||||
raised := ""
|
||||
for _, v := range fs.vals {
|
||||
raised += string(v)
|
||||
}
|
||||
if !strings.Contains(raised, "sha256:x") || !strings.Contains(raised, "is taken and is never released") {
|
||||
t.Fatalf("no finding raised for the digest the holder holds: %s", raised)
|
||||
}
|
||||
// Taken in once: a published of m:a heard again says nothing more.
|
||||
rv.published(ctx, versions.Outcome{Name: "m-a", Version: res.Version, Digest: "sha256:a"})
|
||||
if len(said) != 1 {
|
||||
t.Fatalf("package-published said again: %+v", said)
|
||||
}
|
||||
}
|
||||
|
||||
// TestABuildTakingOverAnOlderReservationDropsItsBoundAndSaysSo: design 47 G A-455 — a build that reserves while the series is
|
||||
// reserved by an older build never released drops the older one's bound and finding and says so in the builder's journal.
|
||||
func TestABuildTakingOverAnOlderReservationDropsItsBoundAndSaysSo(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
st := &memFindings{vals: map[string][]byte{}}
|
||||
found := &findings{module: builds.BuilderModule, w: st, logf: t.Logf, now: func() time.Time { return time.Unix(0, 0) }}
|
||||
bounds := &boundsKV{keys: map[string]bool{}}
|
||||
var journal []string
|
||||
call := func(string, any) (json.RawMessage, error) {
|
||||
return json.Marshal(meshcatalog.VersionsAnswer{Module: "m"})
|
||||
}
|
||||
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, call, func() jetstream.KeyValue { return bounds }, found,
|
||||
nil, func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) })
|
||||
rv.filled.Store(true)
|
||||
older, err := rv.Reserve(ctx, "m", "0.1", []string{"m:a"}, versions.Source{Fingerprint: "fp-old"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := rv.Built(ctx, "m", older.Version, map[string]string{"m:a": "sha256:a"}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rv.unreleased(ctx, older, "nothing within the bound")
|
||||
if !bounds.keys[versions.ReservedBoundKey("m", "0.1")] || len(st.vals) != 1 {
|
||||
t.Fatalf("the older reservation's bound %v or finding %v is not there", bounds.keys, st.vals)
|
||||
}
|
||||
newer, err := rv.Reserve(ctx, "m", "0.1", []string{"m:a"}, versions.Source{Fingerprint: "fp-new"})
|
||||
if err != nil || newer.Version != "0.1.1" {
|
||||
t.Fatalf("reserved %q (%v), want 0.1.1", newer.Version, err)
|
||||
}
|
||||
if len(bounds.keys) != 0 || len(st.vals) != 0 {
|
||||
t.Errorf("after the takeover: bounds %v, findings %v; want none", bounds.keys, st.vals)
|
||||
}
|
||||
if !slices.ContainsFunc(journal, func(l string) bool {
|
||||
return strings.Contains(l, "0.1.0") && strings.Contains(l, "dropped") && strings.Contains(l, "0.1.1")
|
||||
}) {
|
||||
t.Errorf("journal %q does not say the reservation of 0.1.0 was dropped for 0.1.1", journal)
|
||||
}
|
||||
if got, ok, _ := rv.ReservedFor(ctx, "m", "fp-new"); !ok || got.Version != "0.1.1" {
|
||||
t.Errorf("reserved for fp-new: %+v %v", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// ansKV records the value versionActed writes back to a reasks key.
|
||||
type ansKV struct {
|
||||
jetstream.KeyValue
|
||||
wrote chan []byte
|
||||
}
|
||||
|
||||
func (a *ansKV) Update(_ context.Context, _ string, v []byte, _ uint64) (uint64, error) {
|
||||
a.wrote <- v
|
||||
return 2, nil
|
||||
}
|
||||
|
||||
type ansEntry struct {
|
||||
jetstream.KeyValueEntry
|
||||
key string
|
||||
value []byte
|
||||
}
|
||||
|
||||
func (e ansEntry) Key() string { return e.key }
|
||||
func (e ansEntry) Revision() uint64 { return 1 }
|
||||
func (e ansEntry) Value() []byte { return e.value }
|
||||
func (e ansEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }
|
||||
|
||||
// TestAReserveDoneIsAnsweredWithTheVersionAndItsQueuedPublish: design 47 G A-455 — the reasks value of builder_reserve_done reaches
|
||||
// the act, which reserves for the done build and queues the publish as publish again does, and its answer names both; a
|
||||
// refusal of the reserving queues nothing and is the answer; a publish that cannot be queued says the version is reserved.
|
||||
func TestAReserveDoneIsAnsweredWithTheVersionAndItsQueuedPublish(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
var queued []string
|
||||
var logged []string
|
||||
logf := func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }
|
||||
reserve := func(_ context.Context, module, fp string) (versions.ReservedVersion, error) {
|
||||
if fp == "refused" {
|
||||
return versions.ReservedVersion{}, fmt.Errorf("the build key of %s at %s is not done", module, fp)
|
||||
}
|
||||
return versions.ReservedVersion{Module: module, Version: "0.1.13", Fingerprint: fp}, nil
|
||||
}
|
||||
republish := func(_ context.Context, module, version string) ([]string, error) {
|
||||
if module == "nope" {
|
||||
return nil, fmt.Errorf("the queue is full")
|
||||
}
|
||||
queued = append(queued, module+"@"+version)
|
||||
return []string{module + "@" + version}, nil
|
||||
}
|
||||
acts := versions.Acts{ReserveDone: reserveDoneAct(reserve, republish, logf)}
|
||||
for _, c := range []struct {
|
||||
module, fp, want string
|
||||
refused bool
|
||||
}{
|
||||
{"node-tools", "src2:7df8", "reserved 0.1.13 for node-tools at src2:7df8 and queued 1 versions to publish: node-tools@0.1.13", false},
|
||||
{"node-tools", "refused", "not done", true},
|
||||
{"nope", "src2:1", "0.1.13 is reserved for nope at src2:1, but its publish is not queued", true},
|
||||
} {
|
||||
value, err := json.Marshal(build.ReAsk{V: 1, Why: "w", By: "me", Mode: build.ModeReserveDone, Name: c.module, FP: c.fp})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kv := &ansKV{wrote: make(chan []byte, 1)}
|
||||
// reAsked sends a reserve-done to the version acts (b is not asked), and the answer is written back to the key.
|
||||
reAsked(ctx, nil, acts, nil, kv, ansEntry{key: "reserve-done." + c.module, value: value}, logf)
|
||||
var wrote []byte
|
||||
select {
|
||||
case wrote = <-kv.wrote:
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatalf("%s at %s: no answer was written to the key", c.module, c.fp)
|
||||
}
|
||||
var got build.ReAsk
|
||||
if err := json.Unmarshal(wrote, &got); err != nil || got.Refused != c.refused || !strings.Contains(got.Answer, c.want) || got.Answered.IsZero() {
|
||||
t.Errorf("%s at %s: written %s (%v), want refused=%v containing %q", c.module, c.fp, wrote, err, c.refused, c.want)
|
||||
}
|
||||
}
|
||||
if !slices.Equal(queued, []string{"node-tools@0.1.13"}) {
|
||||
t.Errorf("queued %v, want only node-tools@0.1.13", queued)
|
||||
}
|
||||
if !slices.ContainsFunc(logged, func(l string) bool { return strings.Contains(l, "0.1.13 is reserved by hand") }) {
|
||||
t.Errorf("the reserving by hand is not said in the log: %q", logged)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ func (b *Builder) handEnded(key, why string) {
|
||||
// BuildAgain builds the failed key of module at fp again, as the builder does by itself: the key is written building as a
|
||||
// new revision and the build module asks are put on the build seats. It answers once the asks are put, or at bound. A key
|
||||
// left building that no look of this builder holds is taken as it stands and its asks put if they never were (issue 1966).
|
||||
// A key done or queued is refused with why: done (nothing to do) or waiting (builder_ask_again asks its waiting asks again).
|
||||
// A key done (unless its reserved version is not released) or queued is refused with why: done (nothing to do) or waiting (builder_ask_again asks its waiting asks again).
|
||||
// ctx is the builder's own context: the build it starts lives on it.
|
||||
func (b *Builder) BuildAgain(ctx context.Context, module, fp, why, by string, bound time.Duration) BuildAgainResult {
|
||||
b.init()
|
||||
@@ -109,7 +109,11 @@ func (b *Builder) BuildAgain(ctx context.Context, module, fp, why, by string, bo
|
||||
}
|
||||
switch {
|
||||
case cur.State == builds.BuildDone:
|
||||
return BuildAgainResult{Refused: fmt.Sprintf("%s is done: nothing to do", key)}
|
||||
// A done key whose version is not released is built again, so the build's own publish runs (A-490); one released, or
|
||||
// that reserves no version, has nothing to do.
|
||||
if why := b.rebuildRefused(ctx, module, fp); why != "" {
|
||||
return BuildAgainResult{Refused: fmt.Sprintf("%s is done: nothing to do (%s)", key, why)}
|
||||
}
|
||||
case cur.State == builds.BuildQueued:
|
||||
return BuildAgainResult{Refused: fmt.Sprintf("%s is waiting (%s: %s): %s asks its waiting asks again", key, cur.State, cur.Step, AskAgainTool)}
|
||||
case cur.State == builds.BuildBuilding:
|
||||
@@ -196,3 +200,29 @@ func boolInt(b bool) int {
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// rebuildRefused says why a done key of module at fp is not built again by hand: empty when its build is the first watched
|
||||
// branch's trunk build and its reserved version is not released (a publish refused leaves no copy to publish, A-490).
|
||||
func (b *Builder) rebuildRefused(ctx context.Context, module, fp string) string {
|
||||
if b.Reserved == nil {
|
||||
return "this builder reserves no version"
|
||||
}
|
||||
key, err := builds.BuilderBuildKey(module, fp)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
b.mu.Lock()
|
||||
trunk, first := b.trunkFP[key], b.origin[key].First || b.origin[key] == (Origin{})
|
||||
b.mu.Unlock()
|
||||
if !trunk || !first {
|
||||
return "only the first watched branch's trunk build reserves a version"
|
||||
}
|
||||
released, err := b.Reserved.ReleasedFor(ctx, module, fp)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("whether its version is released cannot be read: %v", err)
|
||||
}
|
||||
if released {
|
||||
return "its version is released"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -54,6 +54,9 @@ type ClaimOptions struct {
|
||||
// ByHand is a person's hand act (BuildAgain): a key failed on a transport fault is taken again only by it, or by a builder
|
||||
// that has just started (TakeOver), never by the builder's own retake (ADR 0664 §3).
|
||||
ByHand bool
|
||||
// Rebuild takes a done key again as a new revision and builds it whole: a person's BuildAgain of a key whose reserved version
|
||||
// is not released (design 47 G A-490: a package whose build's publish was refused is built again, never published from a copy).
|
||||
Rebuild bool
|
||||
// ByEvent is the builder's retake on an event that says a transport fault's cause passed (design 47 G §568): it takes a key
|
||||
// failed on a transport fault, as a new revision that counts one more of EventBuilds.
|
||||
ByEvent bool
|
||||
@@ -189,6 +192,11 @@ func (c Claimer) Claim(ctx context.Context, module, fingerprint, manifest string
|
||||
}
|
||||
switch {
|
||||
case cur.State == builds.BuildDone:
|
||||
if opt.Rebuild {
|
||||
again := building
|
||||
again.Step = "built again by hand: its reserved version is not released"
|
||||
return c.write(ctx, key, again, func(data []byte) (uint64, error) { return c.Store.Update(ctx, key, data, rev) })
|
||||
}
|
||||
if Unreferenced(cur) {
|
||||
return c.write(ctx, key, building, func(data []byte) (uint64, error) { return c.Store.Update(ctx, key, data, rev) })
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
@@ -81,6 +82,15 @@ func (b *Builder) number(ctx context.Context, module string, m meshcatalog.Modul
|
||||
b.logf("%s at %s: %v", module, m.Fp2, fmt.Errorf("not numbered, the versions cannot be used: %w", err))
|
||||
return versions.Numbering{}, true
|
||||
}
|
||||
if done != nil && len(got.Versions) > 0 && b.Reserved != nil {
|
||||
// A late look numbered a done build: G §349 retires numbering at a look, so one with no reservation and no release is
|
||||
// said aloud with the recovery that reserves it (A-455), never taken in silently.
|
||||
_, reserved, rerr := b.Reserved.ReservedFor(ctx, module, m.Fp2)
|
||||
released, derr := b.Reserved.ReleasedFor(ctx, module, m.Fp2)
|
||||
if rerr == nil && derr == nil && !reserved && !released {
|
||||
b.logf("%s at %s: a late look numbered this done build (%v) and no version is reserved for it: builder_reserve_done reserves one and queues its publish", module, m.Fp2, got.Versions)
|
||||
}
|
||||
}
|
||||
if got.Refused != "" {
|
||||
b.logf("%s at %s: %s", module, m.Fp2, got.Refused)
|
||||
return got, true
|
||||
@@ -139,6 +149,10 @@ type Reservations interface {
|
||||
// Built records the digests of the artifacts built under the reserved version, by reference, and what each resolved and was
|
||||
// built with (design 47 G §466, §673).
|
||||
Built(ctx context.Context, module, version string, digests map[string]string, deps map[string][]meshbuilds.Dependency) error
|
||||
// ReservedFor is the reservation made for the build of fingerprint, and whether there is one.
|
||||
ReservedFor(ctx context.Context, module, fingerprint string) (versions.ReservedVersion, bool, error)
|
||||
// ReleasedFor says whether a version was released for the build of fingerprint, read from the builder's own record (A-456a).
|
||||
ReleasedFor(ctx context.Context, module, fingerprint string) (bool, error)
|
||||
}
|
||||
|
||||
// reserves says whether a look at a trunk build is one that reserves: the build has a series of the form MAJOR.MINOR
|
||||
@@ -155,6 +169,11 @@ func (b *Builder) reserves(ctx context.Context, module string, m meshcatalog.Mod
|
||||
return b.doneDigests(ctx, module, m.Fp2) == nil
|
||||
}
|
||||
|
||||
// reservesVersion says whether a plan's build reserves a version: it has artifacts and a MAJOR.MINOR series (§368).
|
||||
func (b *Builder) reservesVersion(plan Plan) bool {
|
||||
return len(plan.Tasks) > 0 && strings.Count(plan.Series, ".") == 1
|
||||
}
|
||||
|
||||
// waitFill holds a build that would reserve until the catalog says versions-filled, saying wait-held naming the fill once
|
||||
// (design 47 G §369, §372). It answers whether the build waits.
|
||||
func (b *Builder) waitFill(module string, m meshcatalog.ModulesByModuleValue) bool {
|
||||
@@ -173,13 +192,13 @@ func (b *Builder) waitFill(module string, m meshcatalog.ModulesByModuleValue) bo
|
||||
}
|
||||
|
||||
// reserve reserves the version of the build that starts; a reservation that cannot be made is said and the build goes on
|
||||
// unreserved, as an unnumbered build does.
|
||||
func (b *Builder) reserve(ctx context.Context, module string, m meshcatalog.ModulesByModuleValue, plan Plan, o Origin) (versions.ReservedVersion, bool) {
|
||||
// unreserved: the caller fails the key, which a look takes again (A-490).
|
||||
func (b *Builder) reserve(ctx context.Context, module string, m meshcatalog.ModulesByModuleValue, plan Plan, o Origin, rebuild bool) (versions.ReservedVersion, bool) {
|
||||
refs := make([]string, 0, len(plan.Tasks))
|
||||
for _, t := range plan.Tasks {
|
||||
refs = append(refs, module+":"+t.Artifact)
|
||||
}
|
||||
src := versions.Source{Repository: m.Repository, Commit: m.Commit, Fingerprint: m.Fp2, Branch: o.Branch, Revision: o.Source}
|
||||
src := versions.Source{Repository: m.Repository, Commit: m.Commit, Fingerprint: m.Fp2, Branch: o.Branch, Revision: o.Source, Rebuild: rebuild}
|
||||
for _, t := range plan.Tasks {
|
||||
src.GoPackage = src.GoPackage || (t.Kind == catalogue.ArtifactPackage && t.Language == "go")
|
||||
}
|
||||
@@ -191,3 +210,70 @@ func (b *Builder) reserve(ctx context.Context, module string, m meshcatalog.Modu
|
||||
b.logf("%s: version %s is reserved for this build", module, res.Version)
|
||||
return res, true
|
||||
}
|
||||
|
||||
// ReserveDone is builder_reserve_done (design 47 G A-455): a done trunk build of the first watched branch that reserved no
|
||||
// version (its key is done, so a look never reserves for it, and a done key cannot be built again) reserves the next version of
|
||||
// its series now, with the digests the done key holds, as a build that started would, and its build ends with every digest
|
||||
// recorded. It answers the reserved version; the caller queues its publish (versions.Republisher). A fingerprint whose key is not
|
||||
// done, whose entry no watched branch carries now, that is no first branch's, or that holds a reservation already is refused, saying which.
|
||||
func (b *Builder) ReserveDone(ctx context.Context, module, fp string) (versions.ReservedVersion, error) {
|
||||
b.init()
|
||||
if b.Reserved == nil {
|
||||
return versions.ReservedVersion{}, errors.New("this builder reserves no versions")
|
||||
}
|
||||
key, err := builds.BuilderBuildKey(module, fp)
|
||||
if err != nil {
|
||||
return versions.ReservedVersion{}, err
|
||||
}
|
||||
b.mu.Lock()
|
||||
var m meshcatalog.ModulesByModuleValue
|
||||
found := false
|
||||
for hk, v := range b.heard {
|
||||
if strings.HasSuffix(hk, "\x00"+module) && v.Fp2 == fp {
|
||||
m, found = v, true
|
||||
}
|
||||
}
|
||||
trunk, first := b.trunkFP[key], b.origin[key].First
|
||||
b.mu.Unlock()
|
||||
switch {
|
||||
case !found:
|
||||
return versions.ReservedVersion{}, fmt.Errorf("no watched branch carries an entry of %s at %s now, so there is nothing to reserve it from", module, fp)
|
||||
case !trunk || !first:
|
||||
return versions.ReservedVersion{}, fmt.Errorf("%s at %s is not the first watched branch's build: only that one reserves a version (§371)", module, fp)
|
||||
}
|
||||
digests := b.doneDigests(ctx, module, fp)
|
||||
if digests == nil {
|
||||
return versions.ReservedVersion{}, fmt.Errorf("the build key of %s at %s is not done", module, fp)
|
||||
}
|
||||
plan, planErr, _ := b.planOf(ctx, m.Manifest)
|
||||
switch {
|
||||
case planErr != nil:
|
||||
return versions.ReservedVersion{}, fmt.Errorf("the plan of %s at %s cannot be read: %w", module, fp, planErr)
|
||||
case len(plan.Tasks) == 0 || strings.Count(plan.Series, ".") != 1:
|
||||
return versions.ReservedVersion{}, fmt.Errorf("%s at %s builds nothing or has no MAJOR.MINOR series (%q): it reserves no version", module, fp, plan.Series)
|
||||
}
|
||||
if cur, ok, err := b.Reserved.ReservedFor(ctx, module, fp); err != nil {
|
||||
return versions.ReservedVersion{}, err
|
||||
} else if ok {
|
||||
return versions.ReservedVersion{}, fmt.Errorf("%s at %s holds the reservation of %s already", module, fp, cur.Version)
|
||||
}
|
||||
if !b.Reserved.Filled() {
|
||||
return versions.ReservedVersion{}, versions.ErrNotFilled
|
||||
}
|
||||
res, ok := b.reserve(ctx, module, m, plan, b.originOf(key), false)
|
||||
if !ok {
|
||||
return versions.ReservedVersion{}, fmt.Errorf("no version could be reserved for %s at %s (the builder's journal says why)", module, fp)
|
||||
}
|
||||
refs := map[string]string{}
|
||||
for _, t := range plan.Tasks {
|
||||
d, have := digests[t.Artifact]
|
||||
if !have {
|
||||
return res, fmt.Errorf("%s is reserved for %s at %s, but the done key holds no digest of %s", res.Version, module, fp, t.Artifact)
|
||||
}
|
||||
refs[module+":"+t.Artifact] = d
|
||||
}
|
||||
if err := b.Reserved.Built(ctx, module, res.Version, refs, nil); err != nil {
|
||||
return res, fmt.Errorf("%s is reserved for %s at %s, but its digests are not recorded: %w", res.Version, module, fp, err)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package build
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
@@ -22,17 +23,39 @@ import (
|
||||
// fakeReservations answers a fill and keeps what the builder reserved and recorded.
|
||||
type fakeReservations struct {
|
||||
filled bool
|
||||
released map[string]bool
|
||||
reserved [][]string
|
||||
series []string
|
||||
built map[string]string
|
||||
sources []versions.Source
|
||||
deps map[string][]meshbuilds.Dependency
|
||||
next string // the version Reserve answers when set, else the series' .5
|
||||
fail error // the reservation cannot be made (the catalog's versions cannot be read)
|
||||
}
|
||||
|
||||
func (f *fakeReservations) ReleasedFor(_ context.Context, _, fp string) (bool, error) {
|
||||
return f.released[fp], nil
|
||||
}
|
||||
|
||||
func (f *fakeReservations) Filled() bool { return f.filled }
|
||||
|
||||
func (f *fakeReservations) ReservedFor(_ context.Context, _, fp string) (versions.ReservedVersion, bool, error) {
|
||||
for _, s := range f.sources {
|
||||
if s.Fingerprint == fp {
|
||||
return versions.ReservedVersion{Version: "held"}, true, nil
|
||||
}
|
||||
}
|
||||
return versions.ReservedVersion{}, false, nil
|
||||
}
|
||||
|
||||
func (f *fakeReservations) Reserve(_ context.Context, module, series string, artifacts []string, src versions.Source) (versions.ReservedVersion, error) {
|
||||
if f.fail != nil {
|
||||
return versions.ReservedVersion{}, f.fail
|
||||
}
|
||||
f.reserved, f.series, f.sources = append(f.reserved, artifacts), append(f.series, series), append(f.sources, src)
|
||||
if f.next != "" {
|
||||
return versions.ReservedVersion{Module: module, Series: series, Version: f.next}, nil
|
||||
}
|
||||
return versions.ReservedVersion{Module: module, Series: series, Version: series + ".5"}, nil
|
||||
}
|
||||
|
||||
@@ -238,3 +261,128 @@ func TestAModuleWhoseOnlyArtifactIsAGoPackageIsBuiltUnderItsReservedVersion(t *t
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAReservingTrunkBuildNeverEndsDoneWithoutItsVersion: design 47 G A-490 (a package whose build's publish was refused is built
|
||||
// again, never published from a copy; the kind met three times on 2026-11-19: a trunk build ended done with "no version is
|
||||
// reserved") — a reservation that cannot be made fails the key with why and builds nothing, so a look or builder_build_again takes
|
||||
// it again; and a done key whose version is not released is built again by hand under a reserved version, a released one is refused.
|
||||
func TestAReservingTrunkBuildNeverEndsDoneWithoutItsVersion(t *testing.T) {
|
||||
setup := func(t *testing.T, res *fakeReservations) *rig {
|
||||
r := newRig(t, goBundle)
|
||||
r.mf.byDigest[mdigest('a')] = catalogue.Manifest{Module: "m", Version: "0.1", Build: &catalogue.Build{Artifacts: []catalogue.Artifact{goBundle}}}
|
||||
r.b.Reserved = res
|
||||
return r
|
||||
}
|
||||
t.Run("a reservation that cannot be made fails the key and asks nothing", func(t *testing.T) {
|
||||
r := setup(t, &fakeReservations{filled: true, fail: errors.New("the catalog's versions cannot be read")})
|
||||
v := r.consider(t, "modules/m")
|
||||
if v.State != builds.BuildFailed || !strings.Contains(v.Error, "no version is reserved") || len(r.asks.Asks()) != 0 {
|
||||
t.Fatalf("key %+v with %d asks; want failed saying no version is reserved, nothing asked", v, len(r.asks.Asks()))
|
||||
}
|
||||
})
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
released bool
|
||||
standing bool // a reservation of 0.1.0 stands, and the catalog now answers 0.1.16 released
|
||||
refuse string
|
||||
}{
|
||||
{name: "a standing 0.1.0 reservation is not kept: the rebuild reserves afresh, 0.1.17 above the released 0.1.16", standing: true},
|
||||
{name: "a done key whose version is not released is built again under a reserved version"},
|
||||
{name: "a done key whose version is released is refused", released: true, refuse: "its version is released"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
r := setup(t, nil)
|
||||
r.b.Reserved = nil
|
||||
if v := r.consider(t, "modules/m"); v.State != builds.BuildDone {
|
||||
t.Fatalf("first build = %+v, want done", v)
|
||||
}
|
||||
res := &fakeReservations{filled: true, released: map[string]bool{fp1: c.released}}
|
||||
if c.standing {
|
||||
res.sources, res.next = []versions.Source{{Fingerprint: fp1}}, "0.1.17" // ReservedFor answers a held reservation
|
||||
}
|
||||
r.b.Reserved = res
|
||||
asked := len(r.asks.Asks())
|
||||
got := r.b.BuildAgain(context.Background(), "m", fp1, "its publish was refused", "shanks.lead", 2*time.Second)
|
||||
r.b.Wait()
|
||||
if c.refuse != "" {
|
||||
if !strings.Contains(got.Refused, c.refuse) || len(r.asks.Asks()) != asked {
|
||||
t.Fatalf("answer %+v; want refused with %q and nothing asked", got, c.refuse)
|
||||
}
|
||||
return
|
||||
}
|
||||
if got.Refused != "" || len(r.asks.Asks()) == asked || len(res.reserved) != 1 {
|
||||
t.Fatalf("answer %+v with %d new asks, reserved %v; want built again under one reserved version", got, len(r.asks.Asks())-asked, res.reserved)
|
||||
}
|
||||
want := "0.1.5"
|
||||
if c.standing {
|
||||
want = "0.1.17"
|
||||
}
|
||||
if _, ok := res.built[want]; !ok || len(res.reserved) != 1 {
|
||||
t.Fatalf("built %v after %d reservations; want the digests recorded under the freshly reserved %s so the build's end asks its publish", res.built, len(res.reserved), want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// realReserve is the real versions.Reserver over a map, answering the builder's Reservations as the running builder's does.
|
||||
type realReserve struct {
|
||||
*versions.Reserver
|
||||
reservations map[string][]byte
|
||||
}
|
||||
|
||||
func (r *realReserve) Filled() bool { return true }
|
||||
func (r *realReserve) ReleasedFor(context.Context, string, string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (r *realReserve) ReservedFor(ctx context.Context, module, fp string) (versions.ReservedVersion, bool, error) {
|
||||
all, err := r.All(ctx)
|
||||
for _, v := range all {
|
||||
if v.Module == module && v.Fingerprint == fp {
|
||||
return v, true, err
|
||||
}
|
||||
}
|
||||
return versions.ReservedVersion{}, false, err
|
||||
}
|
||||
func (r *realReserve) Built(ctx context.Context, module, version string, digests map[string]string, _ map[string][]meshbuilds.Dependency) error {
|
||||
for ref, d := range digests {
|
||||
if _, err := r.Reserver.Built(ctx, module, version, ref, d, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type reserveMap struct{ *memStore }
|
||||
|
||||
func (m reserveMap) KeysOf(ctx context.Context, _ string) ([]string, error) { return m.Keys(ctx) }
|
||||
func (m reserveMap) Delete(_ context.Context, key string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
delete(m.vals, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestABuildAgainReservesAboveTheCatalogsHighestNotItsOwnStandingNumber: design 47 G A-490b, at the build, through the real
|
||||
// reserver — a done key whose own fingerprint has an unreleased 0.1.30 standing, the catalog's highest 0.1.16, is built again
|
||||
// under 0.1.17 (never 0.1.31), the standing reservation taken over.
|
||||
func TestABuildAgainReservesAboveTheCatalogsHighestNotItsOwnStandingNumber(t *testing.T) {
|
||||
r := newRig(t, goBundle)
|
||||
r.mf.byDigest[mdigest('a')] = catalogue.Manifest{Module: "m", Version: "0.1", Build: &catalogue.Build{Artifacts: []catalogue.Artifact{goBundle}}}
|
||||
if v := r.consider(t, "modules/m"); v.State != builds.BuildDone {
|
||||
t.Fatalf("first build = %+v, want done", v)
|
||||
}
|
||||
store := reserveMap{newMem()}
|
||||
real := &realReserve{Reserver: &versions.Reserver{Store: store, Filled: func() bool { return true },
|
||||
ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"0.1.16"}, nil }}}
|
||||
raw, _ := json.Marshal(versions.ReservedVersion{V: 1, Module: "m", Series: "0.1", Version: "0.1.30", Fingerprint: fp1, Artifacts: map[string]string{"m:tool": "d"}})
|
||||
if _, err := store.Create(context.Background(), versions.ReservedKey("m", "0.1"), raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.b.Reserved = real
|
||||
got := r.b.BuildAgain(context.Background(), "m", fp1, "its publish was refused", "shanks.lead", 2*time.Second)
|
||||
r.b.Wait()
|
||||
cur, ok, err := real.Of(context.Background(), "m", "0.1.17")
|
||||
if got.Refused != "" || err != nil || !ok || cur.Fingerprint != fp1 {
|
||||
t.Fatalf("answer %+v; reservation of 0.1.17 = %+v (%v, %v); want built again under 0.1.17, the catalog's highest 0.1.16 plus one", got, cur, ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
||||
)
|
||||
|
||||
// TestADoneTrunkBuildThatReservedNoneIsGivenAVersionByHand: design 47 G A-455 — builder_reserve_done reserves the series' next
|
||||
// version for a done key that reserved none, with the digests the key holds; a key not done, a fingerprint no entry carries and
|
||||
// a build that holds a reservation already are refused; the value it writes is a reasks value of its own key.
|
||||
func TestADoneTrunkBuildThatReservedNoneIsGivenAVersionByHand(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
r := newRig(t, goBundle)
|
||||
r.mf.byDigest[mdigest('a')] = catalogue.Manifest{Module: "m", Version: "0.1", Build: &catalogue.Build{Artifacts: []catalogue.Artifact{goBundle}}}
|
||||
res := &fakeReservations{filled: true}
|
||||
r.b.Reserved = res
|
||||
if _, err := r.b.ReserveDone(ctx, "m", fp1); err == nil {
|
||||
t.Fatal("a version was reserved for a key that is not built")
|
||||
}
|
||||
// The build ran while the builder reserved nothing (as the live key did): done, with no reservation.
|
||||
r.b.Reserved = nil
|
||||
if v := r.consider(t, "modules/m"); v.State != builds.BuildDone {
|
||||
t.Fatalf("build = %+v, want done", v)
|
||||
}
|
||||
r.b.Reserved = res
|
||||
if _, err := r.b.ReserveDone(ctx, "m", "src2:"+strings.Repeat("9", 64)); err == nil || !strings.Contains(err.Error(), "no watched branch carries") {
|
||||
t.Errorf("a fingerprint no entry carries: %v", err)
|
||||
}
|
||||
got, err := r.b.ReserveDone(ctx, "m", fp1)
|
||||
if err != nil || got.Version != "0.1.5" || !slices.Equal(res.series, []string{"0.1"}) || !slices.Equal(res.reserved[0], []string{"m:tool"}) {
|
||||
t.Fatalf("reserved %+v (%v) in %v for %v, want 0.1.5 of m:tool in 0.1", got, err, res.series, res.reserved)
|
||||
}
|
||||
if res.built["m:tool"] != digest || res.sources[0].Fingerprint != fp1 {
|
||||
t.Errorf("recorded %v from %+v, want the done key's digest for the entry %s", res.built, res.sources, fp1)
|
||||
}
|
||||
if _, err := r.b.ReserveDone(ctx, "m", fp1); err == nil || !strings.Contains(err.Error(), "reservation") {
|
||||
t.Errorf("a build that holds a reservation was given another: %v", err)
|
||||
}
|
||||
at := time.Unix(0, 0)
|
||||
key, _, err := NewReserveDone("m", fp1, "why", "op", at)
|
||||
if err != nil || !strings.HasPrefix(key, "reserve-done.") {
|
||||
t.Errorf("key %q (%v)", key, err)
|
||||
}
|
||||
for _, args := range [][3]string{{"", fp1, "why"}, {"m", "", "why"}, {"m", fp1, " "}} {
|
||||
if _, _, err := NewReserveDone(args[0], args[1], args[2], "op", at); err == nil {
|
||||
t.Errorf("%q made a value", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -617,7 +617,10 @@ func (b *Builder) try(ctx context.Context, module string, m meshcatalog.ModulesB
|
||||
}
|
||||
// A key built again by hand is taken over as it stands too: one left building by a look that ended without its write
|
||||
// (issue 1966) is the hand tool's to finish.
|
||||
cl, err := b.Claimer.Claim(ctx, module, m.Fp2, m.Manifest, ClaimOptions{TakeOver: first || byHand, ByHand: byHand, ByEvent: byEvent, ImageBuilder: imageSeat, Seats: b.Seats, Trunk: trunk, Published: b.isPublished,
|
||||
// A done key a person builds again whose reserved version is not released is built whole, under a version reserved afresh (A-490b).
|
||||
rebuild := byHand && trunk && planErr == nil && !nothing && b.Reserved != nil && b.doneDigests(ctx, module, m.Fp2) != nil &&
|
||||
b.rebuildRefused(ctx, module, m.Fp2) == "" && b.reservesVersion(plan)
|
||||
cl, err := b.Claimer.Claim(ctx, module, m.Fp2, m.Manifest, ClaimOptions{Rebuild: rebuild, TakeOver: first || byHand, ByHand: byHand, ByEvent: byEvent, ImageBuilder: imageSeat, Seats: b.Seats, Trunk: trunk, Published: b.isPublished,
|
||||
Stored: b.storedOnce(key)})
|
||||
if err != nil {
|
||||
end("the claim was refused: %v", err)
|
||||
@@ -655,11 +658,22 @@ func (b *Builder) try(ctx context.Context, module string, m meshcatalog.ModulesB
|
||||
}
|
||||
var reserved versions.ReservedVersion
|
||||
hasReserved := false
|
||||
if reserving && cl.Build.State != builds.BuildDone {
|
||||
reserved, hasReserved = b.reserve(ctx, module, m, plan, b.originOf(key))
|
||||
if rebuild && cl.Build.State == builds.BuildBuilding {
|
||||
// Reserved afresh, above the highest the catalog's versions answers, taking over the older unreleased reservation of this
|
||||
// fingerprint (A-455), never its standing number kept (A-490b); the build's own publish then runs.
|
||||
if b.Reserved.Filled() {
|
||||
reserved, hasReserved = b.reserve(ctx, module, m, plan, b.originOf(key), true)
|
||||
}
|
||||
reserving = true
|
||||
} else if reserving && cl.Build.State != builds.BuildDone {
|
||||
reserved, hasReserved = b.reserve(ctx, module, m, plan, b.originOf(key), false)
|
||||
}
|
||||
var result builds.BuilderBuild
|
||||
if numbering.Refused != "" && cl.Build.State != builds.BuildDone {
|
||||
if reserving && !hasReserved && cl.Build.State != builds.BuildDone {
|
||||
// A trunk build that reserves is never built without its version: it would end done and publish nothing, and its key would
|
||||
// stay done, never built again (A-490). The key fails with why and is taken again at the next look or by builder_build_again.
|
||||
result = failed("no version is reserved for this build: the builder's journal says why, and it is built again once one can be")
|
||||
} else if numbering.Refused != "" && cl.Build.State != builds.BuildDone {
|
||||
// Refused, naming the state it could not read, and built against no guess (ADR 0666 §15; design 47, test 70): the key
|
||||
// fails with why, and is taken again at the next look.
|
||||
result = failed("%s", numbering.Refused)
|
||||
|
||||
@@ -468,9 +468,19 @@ func TestATrunkImageCallCarriesPublish(t *testing.T) { // 1705b; design 51 *The
|
||||
t.Errorf("a named package's done build: numbered %d more times with %v, %d image builds; want once with img %s and no build",
|
||||
nums.numbered-looks, nums.done, len(r.bm.callsOf("build_image_build")), digest)
|
||||
}
|
||||
// A late look that numbers a done build with no reservation is said aloud, naming builder_reserve_done (G §349, A-455).
|
||||
late := &logged{}
|
||||
r.b.Logf, r.b.Reserved = late.logf, &fakeReservations{filled: true}
|
||||
r.b.PackageNamed(context.Background(), "m")
|
||||
r.b.Wait()
|
||||
if !late.has("late look numbered", "builder_reserve_done") {
|
||||
t.Errorf("a late look numbered a done build with no reservation and said %v", late.lines)
|
||||
}
|
||||
r.b.Reserved = nil
|
||||
looks = nums.numbered
|
||||
r.b.PackageNamed(context.Background(), "other")
|
||||
r.b.Wait()
|
||||
if nums.numbered != looks+1 {
|
||||
if nums.numbered != looks {
|
||||
t.Errorf("a module the builder does not know was looked at")
|
||||
}
|
||||
// D-B5 (ADR 0666 §15; design 47, test 70): a build whose published packages cannot be read is refused, naming the
|
||||
|
||||
@@ -67,7 +67,7 @@ func (b *Builder) buildEntry(ctx context.Context, e meshbuilds.BuildEntry) (outc
|
||||
defer b.dropEnd(key, ended)
|
||||
if done, failedWords, found := b.keyEnd(ctx, key); found {
|
||||
if done {
|
||||
return meshbuilds.StepEndedOutcomePassed, ""
|
||||
return b.passedWithItsVersion(ctx, entry)
|
||||
}
|
||||
return meshbuilds.StepEndedOutcomeFailed, failedWords
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func (b *Builder) buildEntry(ctx context.Context, e meshbuilds.BuildEntry) (outc
|
||||
select {
|
||||
case log := <-ended:
|
||||
if log.Outcome == builds.BuildDone {
|
||||
return meshbuilds.StepEndedOutcomePassed, ""
|
||||
return b.passedWithItsVersion(ctx, entry)
|
||||
}
|
||||
return meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s at %s: %s %s", log.Module, log.FP, log.Outcome, log.Error)
|
||||
case <-ctx.Done():
|
||||
@@ -85,6 +85,46 @@ func (b *Builder) buildEntry(ctx context.Context, e meshbuilds.BuildEntry) (outc
|
||||
}
|
||||
}
|
||||
|
||||
// passedWithItsVersion is the verdict of a build that ended done (design 47 G A-456): a build that reserves a version (a build
|
||||
// of the first watched branch's trunk, with a MAJOR.MINOR series and artifacts) passes only when a version is reserved for its
|
||||
// fingerprint; one that ended done with none fails loudly, naming builder_reserve_done, so no pipeline says passed for a build
|
||||
// that can never be released. A build that reserves none (a candidate's, a series of before G) passes as it did.
|
||||
func (b *Builder) passedWithItsVersion(ctx context.Context, entry meshcatalog.CatalogEntry) (outcome, said string) {
|
||||
if b.Reserved == nil {
|
||||
return meshbuilds.StepEndedOutcomePassed, ""
|
||||
}
|
||||
key, err := builds.BuilderBuildKey(entry.Module, entry.Fp)
|
||||
if err != nil {
|
||||
return meshbuilds.StepEndedOutcomeCouldNotRun, err.Error()
|
||||
}
|
||||
b.mu.Lock()
|
||||
reserving := b.trunkFP[key] && b.origin[key].First
|
||||
b.mu.Unlock()
|
||||
if !reserving {
|
||||
return meshbuilds.StepEndedOutcomePassed, ""
|
||||
}
|
||||
plan, planErr, _ := b.planOf(ctx, meshcatalog.BareManifestDigest(entry.ManifestDigest))
|
||||
switch {
|
||||
case planErr != nil:
|
||||
return meshbuilds.StepEndedOutcomeCouldNotRun, fmt.Sprintf("%s at %s: the plan cannot be read to judge its version: %v", entry.Module, entry.Fp, planErr)
|
||||
case len(plan.Tasks) == 0 || strings.Count(plan.Series, ".") != 1:
|
||||
return meshbuilds.StepEndedOutcomePassed, ""
|
||||
}
|
||||
_, held, err := b.Reserved.ReservedFor(ctx, entry.Module, entry.Fp)
|
||||
switch {
|
||||
case err != nil:
|
||||
return meshbuilds.StepEndedOutcomeCouldNotRun, fmt.Sprintf("%s at %s: the reservations cannot be read: %v", entry.Module, entry.Fp, err)
|
||||
case !held:
|
||||
if released, err := b.Reserved.ReleasedFor(ctx, entry.Module, entry.Fp); err != nil {
|
||||
return meshbuilds.StepEndedOutcomeCouldNotRun, fmt.Sprintf("%s at %s: the releases cannot be read: %v", entry.Module, entry.Fp, err)
|
||||
} else if released {
|
||||
return meshbuilds.StepEndedOutcomePassed, "" // a released build counts as reserved (A-456a)
|
||||
}
|
||||
return meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s: built at %s, but no version is reserved for it; builder_reserve_done reserves one for the done build", entry.Module, entry.Fp)
|
||||
}
|
||||
return meshbuilds.StepEndedOutcomePassed, ""
|
||||
}
|
||||
|
||||
// keyEnd reads the build key: whether it is done, and its error when it failed for good; found only for a key that has ended.
|
||||
func (b *Builder) keyEnd(ctx context.Context, key string) (done bool, failed string, found bool) {
|
||||
raw, _, ok, err := b.Claimer.Store.Get(ctx, key)
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
|
||||
"mesh-delivery-builder/internal/versions"
|
||||
)
|
||||
|
||||
// The runner's build step ask is the builder's one entry point for a candidate (design 47 G A-97): each entry is read by its
|
||||
@@ -74,3 +76,48 @@ func TestAStepAskIsWokenByItsBuildsEnd(t *testing.T) {
|
||||
t.Errorf("waits left: %v", r.b.stepWaits)
|
||||
}
|
||||
}
|
||||
|
||||
// A build step passes a build that reserves a version only when a version is reserved for its fingerprint (design 47 G A-456):
|
||||
// a trunk build that ended done with none fails naming builder_reserve_done, and passes once one is reserved for it; a build
|
||||
// that reserves none (a manifest of before G, version "1") passes as it did.
|
||||
func TestABuildStepPassesAReservingBuildOnlyWithItsReservedVersion(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name, version string
|
||||
reserve, released, unreadable bool
|
||||
want, said string
|
||||
}{
|
||||
{"no version reserved", "0.1", false, false, false, meshbuilds.StepEndedOutcomeFailed, "built at " + fp1 + ", but no version is reserved for it"},
|
||||
{"a version reserved", "0.1", true, false, false, meshbuilds.StepEndedOutcomePassed, ""},
|
||||
{"a released build whose reservation is gone", "0.1", false, true, false, meshbuilds.StepEndedOutcomePassed, ""},
|
||||
{"the plan cannot be read", "0.1", false, false, true, meshbuilds.StepEndedOutcomeCouldNotRun, "plan cannot be read"},
|
||||
{"a series of before G", "1", false, false, false, meshbuilds.StepEndedOutcomePassed, ""},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
r := newRig(t, goBundle)
|
||||
r.mf.byDigest[mdigest('a')] = catalogue.Manifest{Module: "m", Version: c.version, Build: &catalogue.Build{Artifacts: []catalogue.Artifact{goBundle}}}
|
||||
res := &fakeReservations{filled: true, released: map[string]bool{fp1: c.released}}
|
||||
if v := r.consider(t, "modules/m"); v.State != builds.BuildDone { // built while the builder reserved nothing, as the live key was
|
||||
t.Fatalf("build = %+v, want done", v)
|
||||
}
|
||||
r.b.Reserved = res
|
||||
if c.reserve {
|
||||
res.sources = append(res.sources, versions.Source{Fingerprint: fp1})
|
||||
}
|
||||
r.b.StepEntry = func(context.Context, string) (meshcatalog.CatalogEntry, error) {
|
||||
return meshcatalog.CatalogEntry{Module: "m", Repository: "novox/m", Commit: "c1", Fp: fp1, ManifestDigest: digestOf(c.unreadable), Path: "modules/m"}, nil
|
||||
}
|
||||
end := r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.1.m", Step: meshbuilds.StepRunStepBuild, Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}})
|
||||
if end.Outcome != c.want || !strings.Contains(end.Said, c.said) || (c.want == meshbuilds.StepEndedOutcomeFailed && !strings.Contains(end.Said, "builder_reserve_done")) {
|
||||
t.Errorf("end = %+v, want %s saying %q", end, c.want, c.said)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// digestOf is the manifest digest a step's entry names: the rig's manifest, or one no store holds, whose plan cannot be read.
|
||||
func digestOf(unreadable bool) string {
|
||||
if unreadable {
|
||||
return mdigest('z')
|
||||
}
|
||||
return mdigest('a')
|
||||
}
|
||||
|
||||
@@ -61,8 +61,15 @@ type ReAsk struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Queued []string `json:"queued,omitempty"`
|
||||
// Tries and NextTry are a ModePublish value's (design 47 G A-457a): the publishes tried so far, and when the next is.
|
||||
Tries int `json:"tries,omitempty"`
|
||||
NextTry time.Time `json:"next-try,omitzero"`
|
||||
}
|
||||
|
||||
// ModePublish is the Mode of a reasks value that is a version's publish item (design 47 G A-457a): Module and Version name it,
|
||||
// Tries and NextTry say where it stands; it is written by the builder itself, when a build ends and at its start.
|
||||
const ModePublish = "publish"
|
||||
|
||||
// ModeRepublish is the Mode of a reasks value of builder_publish_again (design 47 G §588): Module and Version, each
|
||||
// optional, name what to publish again; the builder answers with the versions it queued (Queued).
|
||||
const ModeRepublish = "republish"
|
||||
@@ -137,6 +144,24 @@ func NewReservedDrop(module, series, why, by string, at time.Time) (string, []by
|
||||
return "reserved." + versions.ReservedKey(module, series), data, err
|
||||
}
|
||||
|
||||
// ModeReserveDone is the Mode of a reasks value of builder_reserve_done (design 47 G A-455): Name is the module and FP the
|
||||
// fingerprint of a done trunk build that reserved no version.
|
||||
const ModeReserveDone = "reserve-done"
|
||||
|
||||
// NewReserveDone is the value builder_reserve_done writes: the key `reserve-done.<module>.<fingerprint>` (never a build key) and
|
||||
// the value.
|
||||
func NewReserveDone(module, fp, why, by string, at time.Time) (string, []byte, error) {
|
||||
module, fp = strings.TrimSpace(module), strings.TrimSpace(fp)
|
||||
if module == "" || fp == "" {
|
||||
return "", nil, errors.New("module and fp are required")
|
||||
}
|
||||
if strings.TrimSpace(why) == "" {
|
||||
return "", nil, errors.New("say why a version is reserved for the done build (why)")
|
||||
}
|
||||
data, err := json.Marshal(ReAsk{V: 1, Why: strings.TrimSpace(why), By: by, At: at.UTC(), Mode: ModeReserveDone, Name: module, FP: fp})
|
||||
return "reserve-done." + versions.EscapeToken(module) + "." + versions.EscapeToken(fp), data, err
|
||||
}
|
||||
|
||||
// ModeBuild is the Mode of a reasks value that asks the key to be built again, not its waiting asks to be asked again.
|
||||
const ModeBuild = "build"
|
||||
|
||||
|
||||
@@ -24,6 +24,9 @@ type Acts struct {
|
||||
Republish func(ctx context.Context, module, version string) ([]string, error)
|
||||
// DropOldForm drops a retired node from the old form's count by hand (builder_old_form): its answer, or why not.
|
||||
DropOldForm func(ctx context.Context, node, by, why string, at time.Time) (string, error)
|
||||
// ReserveDone reserves the next version for a done trunk build that reserved none and queues its publish (builder_reserve_done):
|
||||
// its answer, or why not.
|
||||
ReserveDone func(ctx context.Context, module, fp, by, why string, at time.Time) (string, error)
|
||||
// DropReserved drops a module's reservation of one MAJOR.MINOR by hand (builder_reserved_drop): its answer, or why not.
|
||||
DropReserved func(ctx context.Context, module, series, by, why string, at time.Time) (string, error)
|
||||
}
|
||||
|
||||
@@ -5,22 +5,20 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
)
|
||||
|
||||
// **A reserved version left unpublished is published again by the builder, on an event or by a person's tool** (design 47
|
||||
// G §588, ADR 0674): on the node-engine seat's `running` of a module whose catalog graph entry claims the registry seat
|
||||
// of the missing artifacts' kind, and on its `manifest-reads-changed` with `why` `returned`, at each registry seat's holder
|
||||
// the seat-holders view names (A-416, A-416a: known from the start, not from the running heard). Each message is acted on once, by its stream sequence (state `acted`); each holder has one queue of versions,
|
||||
// kept in the state `publish-queue`, sent one at a time. A send is the registry seat's `publish` with the digest and no
|
||||
// parts (A-73): the holder takes the bytes from the artifact store by digest, and refuses what no store holds.
|
||||
// **A reserved version of a done build is published by the builder until its release is written** (design 47 G §588, A-457a): the
|
||||
// publish is an item of the builder's re-ask state, asked when the build ends and at its start, tried with a growing pause and
|
||||
// ended loudly after its tries; each registry seat's holder, as the seat-holders view names it (A-416, A-416a), has one queue of
|
||||
// versions, kept in the state `publish-queue`, sent one at a time. A send is the registry seat's `publish` with the digest and no
|
||||
// parts (A-73): the holder takes the bytes from the artifact store by digest, and refuses what no store holds. Nothing waits on a
|
||||
// node-engine `running` or `manifest-reads-changed` any more.
|
||||
|
||||
const (
|
||||
// PublishQueueState is the builder's old state section of the queues, capped at PublishQueueMiB.
|
||||
@@ -31,8 +29,6 @@ const (
|
||||
ActedMiB = 1
|
||||
// PublishQueueTool is builder_publish_queue.
|
||||
PublishQueueTool = "builder_publish_queue"
|
||||
// DefaultRepublishMax is the setting republish-max-versions unless set (§633).
|
||||
DefaultRepublishMax = 50
|
||||
// QueueFull is what version-unreleased says of a version the queue's cap refused.
|
||||
QueueFull = "queue full"
|
||||
// NotStoredWords opens what a refused artifact is said as.
|
||||
@@ -106,17 +102,14 @@ type Publish struct {
|
||||
type Republisher struct {
|
||||
Reserved *Reserver
|
||||
Queue QueueStore
|
||||
Acted ReserveStore
|
||||
// Entry reads the catalog graph entry of a fingerprint (the seat mesh-catalog's verb entry).
|
||||
Entry func(ctx context.Context, fingerprint string) (meshcatalog.CatalogEntry, error)
|
||||
// Send sends one publish to a registry seat; an error is the holder's refusal or the call's fault, in its words.
|
||||
Send func(ctx context.Context, p Publish) error
|
||||
// Unreleased says version-unreleased about a version with the holder's words.
|
||||
Unreleased func(ctx context.Context, v ReservedVersion, why string)
|
||||
// Max is the setting republish-max-versions: the most versions one event queues.
|
||||
Max int
|
||||
Now func() time.Time
|
||||
Logf func(string, ...any)
|
||||
Now func() time.Time
|
||||
Logf func(string, ...any)
|
||||
// Go starts the sending of a holder's queue; nil starts a goroutine.
|
||||
Go func(func())
|
||||
// Holders answers a registry seat's holders: the catalog's SeatHolders, the part's one holder function that its FollowSeat
|
||||
@@ -159,97 +152,6 @@ func (r *Republisher) logf(f string, a ...any) {
|
||||
// ActedKey is the acted key of a subject.
|
||||
func ActedKey(subject string) string { return EscapeToken(subject) }
|
||||
|
||||
// Heard takes one message of the node-engine seat's running or manifest-reads-changed in, at stream sequence seq: it acts
|
||||
// on it only when its sequence is newer than the one kept for its subject, and keeps it after. A subject that is neither is
|
||||
// not its; an error leaves the message not acted on, so it is read again.
|
||||
func (r *Republisher) Heard(ctx context.Context, subject string, data []byte, seq uint64) (bool, error) {
|
||||
running := strings.HasPrefix(subject, strings.TrimSuffix(nodeengine.SubjectRunningAll, "*.*"))
|
||||
changed := strings.HasPrefix(subject, "mesh.seat.node-engine.event."+nodeengine.EventManifestReadsChanged+".")
|
||||
if !running && !changed {
|
||||
return false, nil
|
||||
}
|
||||
key := ActedKey(subject)
|
||||
raw, rev, found, err := r.Acted.Get(ctx, key)
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("the acted sequence of %s cannot be read: %w", subject, err)
|
||||
}
|
||||
if found {
|
||||
var a Acted
|
||||
if json.Unmarshal(raw, &a) == nil && a.Sequence >= seq {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
if running {
|
||||
err = r.running(ctx, data)
|
||||
} else {
|
||||
err = r.returned(ctx, data)
|
||||
}
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
val, _ := json.Marshal(Acted{Sequence: seq})
|
||||
if found {
|
||||
_, err = r.Acted.Update(ctx, key, val, rev)
|
||||
} else {
|
||||
_, err = r.Acted.Create(ctx, key, val)
|
||||
}
|
||||
if err != nil && !errors.Is(err, ErrConflict) {
|
||||
return true, fmt.Errorf("the acted sequence of %s cannot be kept: %w", subject, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// registrySeatsOf are the registry seats the module of the catalog entry fingerprint claims, by their publish verb.
|
||||
func (r *Republisher) registrySeatsOf(ctx context.Context, fingerprint string) ([]string, error) {
|
||||
e, err := r.Entry(ctx, fingerprint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []string
|
||||
for _, seat := range RegistrySeats {
|
||||
if slices.Contains(e.Serves, seat+"."+VerbPublish) {
|
||||
out = append(out, seat)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// running takes a running in: one of a module whose entry claims a registry seat queues the versions waiting on it (§588). It
|
||||
// is not how a holder is known: Holders answers that (design 47 G A-416).
|
||||
func (r *Republisher) running(ctx context.Context, data []byte) error {
|
||||
var ev nodeengine.Running
|
||||
if err := json.Unmarshal(data, &ev); err != nil || ev.Module == "" || ev.Node == "" {
|
||||
r.logf("a running that does not read is not acted on: %v", err)
|
||||
return nil
|
||||
}
|
||||
if ev.Stopped || ev.Fingerprint == "" {
|
||||
return nil
|
||||
}
|
||||
seats, err := r.registrySeatsOf(ctx, ev.Fingerprint)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the catalog entry %s of %s cannot be read: %w", ev.Fingerprint, ev.Module, err)
|
||||
}
|
||||
if len(seats) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err = r.queueEvent(ctx, map[string][]string{ev.Module: seats})
|
||||
return err
|
||||
}
|
||||
|
||||
// heldSeats are the registry seats' holders as Holders answers them: holder module -> the registry seats it holds.
|
||||
func (r *Republisher) heldSeats() map[string][]string {
|
||||
held := map[string][]string{}
|
||||
if r.Holders == nil {
|
||||
return held
|
||||
}
|
||||
for _, seat := range RegistrySeats {
|
||||
for _, h := range r.Holders(seat) {
|
||||
held[h] = append(held[h], seat)
|
||||
}
|
||||
}
|
||||
return held
|
||||
}
|
||||
|
||||
// holdersFn answers a seat's holders from Holders.
|
||||
func (r *Republisher) holdersFn(seat string) []string {
|
||||
if r.Holders == nil {
|
||||
@@ -258,59 +160,12 @@ func (r *Republisher) holdersFn(seat string) []string {
|
||||
return r.Holders(seat)
|
||||
}
|
||||
|
||||
// returned takes a manifest-reads-changed in: only one with why returned acts, for every registry seat's holder Holders
|
||||
// answers (design 47 G A-416a); a version queued at a holder already is not queued again.
|
||||
func (r *Republisher) returned(ctx context.Context, data []byte) error {
|
||||
var ev nodeengine.ManifestReadsChanged
|
||||
if err := json.Unmarshal(data, &ev); err != nil || ev.Node == "" {
|
||||
r.logf("a manifest-reads-changed that does not read is not acted on: %v", err)
|
||||
return nil
|
||||
}
|
||||
if ev.Why != nodeengine.ManifestReadsChangedWhyReturned {
|
||||
return nil
|
||||
}
|
||||
holders := r.heldSeats()
|
||||
if len(holders) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err := r.queueEvent(ctx, holders)
|
||||
return err
|
||||
}
|
||||
|
||||
// view is a reserved version with the artifacts of it a holder would publish.
|
||||
type view struct {
|
||||
v ReservedVersion
|
||||
arts map[string]meshcatalog.Artifact // reference -> its catalog artifact
|
||||
}
|
||||
|
||||
// queueEvent queues, for each holder (module -> the seats it claims), the unreleased versions whose missing artifacts
|
||||
// that holder's seats take: newest keys first, at most Max versions in all (one key per module and MAJOR.MINOR is the
|
||||
// reservation's own).
|
||||
func (r *Republisher) queueEvent(ctx context.Context, holders map[string][]string) ([]string, error) {
|
||||
over, err := r.Reserved.Overdue(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var vs []ReservedVersion
|
||||
for _, o := range over {
|
||||
if v, ok, err := r.Reserved.Of(ctx, o.Module, o.Version); err == nil && ok {
|
||||
vs = append(vs, v)
|
||||
}
|
||||
}
|
||||
return r.queue(ctx, vs, func(seat string) []string { return holdersOf(holders, seat) }, r.Max)
|
||||
}
|
||||
|
||||
func holdersOf(holders map[string][]string, seat string) []string {
|
||||
var out []string
|
||||
for m, seats := range holders {
|
||||
if slices.Contains(seats, seat) {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// queue queues vs newest first, at most max of them (0 is no limit), each artifact to the first holder holdersOf names for
|
||||
// its seat. It answers the versions queued, `<module> <version>`.
|
||||
func (r *Republisher) queue(ctx context.Context, vs []ReservedVersion, holdersOf func(seat string) []string, max int) ([]string, error) {
|
||||
@@ -444,28 +299,39 @@ func (r *Republisher) Republish(ctx context.Context, module, version string) ([]
|
||||
return r.queue(ctx, vs, r.holdersFn, 0)
|
||||
}
|
||||
|
||||
// Ended queues a reserved version whose build has just ended (built-at set, every artifact's digest known) at the holder of
|
||||
// each artifact's registry seat, as a holder's running does, and drains it (design 47 G A-473). A version with no built-at, or
|
||||
// whose artifacts are all published, queues nothing. A holder that refuses or does not answer leaves the version in
|
||||
// version-unreleased with its words (send says it); a seat with no holder leaves it for the holder's running.
|
||||
func (r *Republisher) Ended(ctx context.Context, module, version string) ([]string, error) {
|
||||
v, ok, err := r.Reserved.Of(ctx, module, version)
|
||||
if err != nil || !ok || v.BuiltAt.IsZero() || len(v.Missing()) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
return r.queue(ctx, []ReservedVersion{v}, r.holdersFn, 0)
|
||||
}
|
||||
|
||||
// Queued are the queued versions, per holder in the order they are sent, and the acted keys with their sequences:
|
||||
// builder_publish_queue.
|
||||
type Queued struct {
|
||||
Queue []QueuedPublish `json:"queue"`
|
||||
Acted map[string]uint64 `json:"acted"`
|
||||
// Items are the publish items (design 47 G A-457a), each with its tries and its next try; an item that ended says why.
|
||||
Items []PublishItem `json:"items"`
|
||||
}
|
||||
|
||||
// PublishItemKeyPrefix opens the key of a publish item in the builder's re-ask state.
|
||||
const PublishItemKeyPrefix = "publish."
|
||||
|
||||
// PublishItem is the builder's own record of a version's publish (design 47 G A-457a): a value of the re-ask state, its tries and
|
||||
// its next try. It is asked when the build ends and at the builder's start, rewritten with one more try and a longer pause after
|
||||
// each, and deleted when the version is released; after the setting's number of tries it ends, saying so (Answer), and stays.
|
||||
type PublishItem struct {
|
||||
Module string `json:"module"`
|
||||
Version string `json:"version"`
|
||||
Tries int `json:"tries"`
|
||||
NextTry time.Time `json:"next-try"`
|
||||
Why string `json:"why,omitempty"`
|
||||
Answer string `json:"answer,omitempty"`
|
||||
Answered time.Time `json:"answered,omitzero"`
|
||||
}
|
||||
|
||||
// PublishItemKey is the key of module's version's publish item.
|
||||
func PublishItemKey(module, version string) string {
|
||||
return PublishItemKeyPrefix + EscapeToken(module) + "." + EscapeToken(version)
|
||||
}
|
||||
|
||||
// AnswerQueue is builder_publish_queue over the two sections as the tools bundle reads them.
|
||||
func AnswerQueue(queue, acted Reader) (Queued, error) {
|
||||
out := Queued{Queue: []QueuedPublish{}, Acted: map[string]uint64{}}
|
||||
func AnswerQueue(queue, acted, reasks Reader) (Queued, error) {
|
||||
out := Queued{Queue: []QueuedPublish{}, Acted: map[string]uint64{}, Items: []PublishItem{}}
|
||||
keys, err := queue.Keys()
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("the state %s cannot be listed: %w", PublishQueueState, err)
|
||||
@@ -500,18 +366,37 @@ func AnswerQueue(queue, acted Reader) (Queued, error) {
|
||||
out.Acted[k] = a.Sequence
|
||||
}
|
||||
}
|
||||
rkeys, err := reasks.Keys()
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("the re-ask state cannot be listed: %w", err)
|
||||
}
|
||||
for _, k := range rkeys {
|
||||
if !strings.HasPrefix(k, PublishItemKeyPrefix) {
|
||||
continue
|
||||
}
|
||||
raw, ok, err := reasks.Get(k)
|
||||
if err != nil || !ok {
|
||||
continue
|
||||
}
|
||||
var it PublishItem
|
||||
if json.Unmarshal(raw, &it) == nil {
|
||||
out.Items = append(out.Items, it)
|
||||
}
|
||||
}
|
||||
sort.SliceStable(out.Items, func(i, j int) bool { return out.Items[i].NextTry.Before(out.Items[j].NextTry) })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PublishQueueDescription is builder_publish_queue's description.
|
||||
const PublishQueueDescription = "The versions the builder will publish again (its state " + PublishQueueState + ", design 47 G section 588): per registry " +
|
||||
"seat holder, in the order they are sent, each with its artifacts' digests, when it was queued and whether its publish has begun; and the " +
|
||||
"keys of the state " + ActedState + ", the last stream sequence the builder acted on for each running and manifest-reads-changed subject. Read-only."
|
||||
"keys of the state " + ActedState + ", the last stream sequence the builder acted on for each subject; and the publish items (design 47 G " +
|
||||
"A-457a), each version's tries and next try, and why one ended after its tries. Read-only."
|
||||
|
||||
// PublishAgainDescription is builder_publish_again's description.
|
||||
const PublishAgainDescription = "Publish reserved versions again (design 47 G section 588), which builds nothing: with module and version, that reserved " +
|
||||
"version's missing artifacts; with module alone, each reserved version of that module whose artifacts are not all published; with neither, every " +
|
||||
"version the finding version-unreleased names, newest first. It queues them as the builder does on a registry holder's running, and answers at once " +
|
||||
"version the finding version-unreleased names, newest first. It queues them as the builder's publish items do, and answers at once " +
|
||||
"with the versions it queued; each outcome is said as package-published, or left in version-unreleased with the holder's words. A hand act, " +
|
||||
"recorded in the hand-act log."
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
)
|
||||
|
||||
@@ -26,7 +25,6 @@ type republishBed struct {
|
||||
*Republisher
|
||||
reserved *reserveMem
|
||||
queue *queueMem
|
||||
acted *reserveMem
|
||||
now time.Time
|
||||
sent []Publish
|
||||
said []string
|
||||
@@ -35,11 +33,11 @@ type republishBed struct {
|
||||
holders map[string][]string
|
||||
}
|
||||
|
||||
func newRepublishBed(t *testing.T, max int) *republishBed {
|
||||
func newRepublishBed(t *testing.T) *republishBed {
|
||||
t.Helper()
|
||||
b := &republishBed{reserved: &reserveMem{mem: newMem()}, queue: &queueMem{reserveMem: &reserveMem{mem: newMem()}}, acted: &reserveMem{mem: newMem()}, now: time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)}
|
||||
b := &republishBed{reserved: &reserveMem{mem: newMem()}, queue: &queueMem{reserveMem: &reserveMem{mem: newMem()}}, now: time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)}
|
||||
res := &Reserver{Store: b.reserved, Now: func() time.Time { return b.now }, ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }}
|
||||
b.Republisher = &Republisher{Reserved: res, Queue: b.queue, Acted: b.acted, Max: max, Now: func() time.Time { return b.now }, Go: func(f func()) { f() },
|
||||
b.Republisher = &Republisher{Reserved: res, Queue: b.queue, Now: func() time.Time { return b.now }, Go: func(f func()) { f() },
|
||||
Entry: func(_ context.Context, fp string) (meshcatalog.CatalogEntry, error) {
|
||||
if fp == "src2:holder" {
|
||||
return meshcatalog.CatalogEntry{Serves: []string{SeatArtifacts + ".publish", SeatArtifacts + ".versions"}}, nil
|
||||
@@ -82,97 +80,32 @@ func (b *republishBed) reserve(t *testing.T, module string) ReservedVersion {
|
||||
return v
|
||||
}
|
||||
|
||||
func running(node, module, fp string, stopped bool) (string, []byte) {
|
||||
data, _ := json.Marshal(nodeengine.Running{Node: node, Module: module, Fingerprint: fp, Stopped: stopped, At: time.Unix(1, 0)})
|
||||
return nodeengine.SubjectRunning(node, module), data
|
||||
}
|
||||
|
||||
// TestRepublish: design 47 G §588 (A-73, A-76): a reserved version left unpublished is published again, on a registry
|
||||
// holder's running and on its node's returned, once per message by its stream sequence, as a publish by digest with no parts,
|
||||
// at most the setting's number per event, newest first; an artifact no store holds is said once and not tried again.
|
||||
func TestRepublish(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
t.Run("running of a holder, once per sequence", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
t.Run("a done build is published as a digest with no parts, to its seat's holder", func(t *testing.T) {
|
||||
b := newRepublishBed(t)
|
||||
v := b.reserve(t, "m")
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
if v.Fingerprint != "src2:m" {
|
||||
t.Fatalf("the reservation keeps the entry's fingerprint: %q", v.Fingerprint)
|
||||
}
|
||||
subject, data := running("n1", "oci", "src2:holder", false)
|
||||
for _, seq := range []uint64{5, 4} { // 4 is older than the acted 5: it starts nothing
|
||||
if _, err := b.Heard(ctx, subject, data, seq); err != nil || len(b.sent) != 1 {
|
||||
t.Fatalf("seq %d: sent %d (%v), want 1", seq, len(b.sent), err)
|
||||
}
|
||||
if q, err := b.Republish(ctx, "", ""); err != nil || len(b.sent) != 1 || fmt.Sprint(q) != "[m "+v.Version+"]" {
|
||||
t.Fatalf("queued %v sent %d (%v), want 1", q, len(b.sent), err)
|
||||
}
|
||||
p := b.sent[0]
|
||||
if p.Seat != SeatArtifacts || p.Name != "m/prog" || p.Kind != "bundle" || p.Version != v.Version || p.Digest != "sha256:aa" || p.Fingerprint != "src2:m" ||
|
||||
p.Transfer != "republish-m-"+strings.ReplaceAll(v.Version, ".", "-")+"-prog" || strings.ContainsAny(p.Transfer, "/. *>") {
|
||||
t.Errorf("sent %+v, want the bundle by digest to the oci seat, the Go package left to its own seat's holder", p)
|
||||
}
|
||||
if _, err := b.Heard(ctx, subject, data, 5); err != nil || len(b.sent) != 1 {
|
||||
t.Errorf("the same sequence read again sent %d, want still 1", len(b.sent))
|
||||
}
|
||||
if _, err := b.Heard(ctx, subject, data, 6); err != nil || len(b.sent) != 2 {
|
||||
t.Errorf("a newer sequence sent %d, want 2", len(b.sent))
|
||||
}
|
||||
if keys, _ := b.queue.KeysOf(ctx, AllFilter); len(keys) != 0 {
|
||||
t.Errorf("a key stays in the queue after its outcome: %v", keys)
|
||||
}
|
||||
})
|
||||
t.Run("a build's end queues its version at the holders, without any event (A-473)", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
b.now = b.now.Add(time.Minute)
|
||||
v, err := b.Reserved.Reserve(ctx, "m", "0.1", []string{"m:prog", "m:lib"}, Source{Fingerprint: "src2:m"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := b.Ended(ctx, "m", v.Version); err != nil || len(b.sent) != 0 {
|
||||
t.Fatalf("a build not ended sent %d (%v)", len(b.sent), err)
|
||||
}
|
||||
for ref, d := range map[string]string{"m:prog": "sha256:aa", "m:lib": "sha256:bb"} {
|
||||
if _, err := b.Reserved.Built(ctx, "m", v.Version, ref, d, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if queued, err := b.Ended(ctx, "m", v.Version); err != nil || len(queued) != 1 || len(b.sent) != 1 || b.sent[0].Name != "m/prog" || b.sent[0].Digest != "sha256:aa" {
|
||||
t.Fatalf("the ended build queued %v, sent %+v (%v), want the bundle at the oci holder", queued, b.sent, err)
|
||||
}
|
||||
})
|
||||
t.Run("returned acts at the seat's holder, started does not", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
b.reserve(t, "m")
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
for i, why := range []string{nodeengine.ManifestReadsChangedWhyStarted, "", nodeengine.ManifestReadsChangedWhyReturned} {
|
||||
ev, _ := json.Marshal(nodeengine.ManifestReadsChanged{Node: "n1", Why: why})
|
||||
handled, err := b.Heard(ctx, nodeengine.SubjectManifestReadsChanged("n1"), ev, uint64(10+i))
|
||||
if !handled || err != nil {
|
||||
t.Fatalf("%q: handled %v, %v", why, handled, err)
|
||||
}
|
||||
if want := map[string]int{"started": 0, "": 0, "returned": 1}[why]; len(b.sent) != want {
|
||||
t.Errorf("why %q: sent %d, want %d", why, len(b.sent), want)
|
||||
}
|
||||
}
|
||||
if handled, _ := b.Heard(ctx, "mesh.seat.node-engine.event.composed.n1", nil, 1); handled {
|
||||
t.Error("a subject that is neither is taken as its own")
|
||||
}
|
||||
})
|
||||
t.Run("the setting limits an event, newest first", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 2)
|
||||
var versions []string
|
||||
for _, m := range []string{"m1", "m2", "m3"} {
|
||||
versions = append(versions, b.reserve(t, m).Module)
|
||||
}
|
||||
subject, data := running("n1", "oci", "src2:holder", false)
|
||||
if _, err := b.Heard(ctx, subject, data, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(b.sent) != 2 || !strings.HasPrefix(b.sent[0].Name, "m3/") || !strings.HasPrefix(b.sent[1].Name, "m2/") {
|
||||
t.Errorf("sent %+v, want the two newest, m3 then m2", b.sent)
|
||||
}
|
||||
})
|
||||
t.Run("an entry that cannot be read stops neither the other versions nor the drain", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
b := newRepublishBed(t)
|
||||
b.reserve(t, "m1")
|
||||
b.reserve(t, "m2") // newest: queued before m1's entry is met
|
||||
entry := b.Entry
|
||||
@@ -182,8 +115,8 @@ func TestRepublish(t *testing.T) {
|
||||
}
|
||||
return entry(ctx, fp)
|
||||
}
|
||||
subject, data := running("n1", "oci", "src2:holder", false)
|
||||
if _, err := b.Heard(ctx, subject, data, 1); err == nil || !strings.Contains(err.Error(), "src2:m1") {
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
if _, err := b.Republish(ctx, "", ""); err == nil || !strings.Contains(err.Error(), "src2:m1") {
|
||||
t.Errorf("the unreadable entry is not said: %v", err)
|
||||
}
|
||||
if len(b.sent) != 1 || !strings.HasPrefix(b.sent[0].Name, "m2/") {
|
||||
@@ -191,39 +124,39 @@ func TestRepublish(t *testing.T) {
|
||||
}
|
||||
})
|
||||
t.Run("an artifact no store holds is said once and not tried again", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
b := newRepublishBed(t)
|
||||
b.reserve(t, "m")
|
||||
b.refuse = "oci-artifact-registry's publish of m/prog: not stored: no blob sha256:aa"
|
||||
subject, data := running("n1", "oci", "src2:holder", false)
|
||||
_, _ = b.Heard(ctx, subject, data, 1)
|
||||
_, _ = b.Heard(ctx, subject, data, 2)
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
_, _ = b.Republish(ctx, "", "")
|
||||
_, _ = b.Republish(ctx, "", "")
|
||||
if len(b.sent) != 1 || len(b.said) != 1 || !strings.Contains(b.said[0], "not stored: ") {
|
||||
t.Errorf("sent %d, said %v: want one try and one saying of not stored", len(b.sent), b.said)
|
||||
}
|
||||
})
|
||||
t.Run("a refusal in other words is said each time", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
b := newRepublishBed(t)
|
||||
b.reserve(t, "m")
|
||||
b.refuse = "the holder is full"
|
||||
subject, data := running("n1", "oci", "src2:holder", false)
|
||||
_, _ = b.Heard(ctx, subject, data, 1)
|
||||
_, _ = b.Heard(ctx, subject, data, 2)
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
_, _ = b.Republish(ctx, "", "")
|
||||
_, _ = b.Republish(ctx, "", "")
|
||||
if len(b.sent) != 2 || len(b.said) != 2 {
|
||||
t.Errorf("sent %d, said %v: want both events tried and said", len(b.sent), b.said)
|
||||
}
|
||||
})
|
||||
t.Run("a queue past its cap says queue full", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
b := newRepublishBed(t)
|
||||
b.reserve(t, "m")
|
||||
b.queue.bytes = PublishQueueMiB << 20
|
||||
subject, data := running("n1", "oci", "src2:holder", false)
|
||||
_, _ = b.Heard(ctx, subject, data, 1)
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
_, _ = b.Republish(ctx, "", "")
|
||||
if len(b.sent) != 0 || len(b.said) != 1 || !strings.HasSuffix(b.said[0], QueueFull) {
|
||||
t.Errorf("sent %d, said %v, want nothing sent and queue full said", len(b.sent), b.said)
|
||||
}
|
||||
})
|
||||
t.Run("a restart sends the queued again, a begun one anew", func(t *testing.T) {
|
||||
b := newRepublishBed(t, 50)
|
||||
b := newRepublishBed(t)
|
||||
v := b.reserve(t, "m")
|
||||
raw, _ := json.Marshal(QueuedPublish{V: 1, Holder: "oci", Module: "m", Version: v.Version, Artifacts: map[string]string{"m:prog": "sha256:aa"}, Queued: b.now, Begun: true})
|
||||
if _, err := b.queue.Create(ctx, QueueKey("oci", "m", v.Version), raw); err != nil {
|
||||
@@ -236,7 +169,7 @@ func TestRepublish(t *testing.T) {
|
||||
t.Run("after a fresh start the tool queues at the seat's holder, no running heard", func(t *testing.T) {
|
||||
// Design 47 G A-416, A-416a: the holder is the one the seat-holders view names from the start, not one learnt from a
|
||||
// running heard since; a seat with no holder queues nothing.
|
||||
b := newRepublishBed(t, 1)
|
||||
b := newRepublishBed(t)
|
||||
b.reserve(t, "m1")
|
||||
b.reserve(t, "m2")
|
||||
if q, err := b.Republish(ctx, "", ""); err != nil || len(q) != 0 {
|
||||
@@ -250,12 +183,16 @@ func TestRepublish(t *testing.T) {
|
||||
if _, err := b.Republish(ctx, "m1", "0.1.9"); err == nil {
|
||||
t.Error("a version no reservation holds is not refused")
|
||||
}
|
||||
subject, data := running("n1", "oci", "src2:holder", false)
|
||||
_, _ = b.Heard(ctx, subject, data, 1)
|
||||
answer, err := AnswerQueue(Reader{Keys: func() ([]string, error) { return nil, nil }}, Reader{Keys: func() ([]string, error) { return b.acted.KeysOf(ctx, AllFilter) },
|
||||
Get: func(k string) (json.RawMessage, bool, error) { v, _, ok, err := b.acted.Get(ctx, k); return v, ok, err }})
|
||||
if err != nil || answer.Acted[ActedKey(subject)] != 1 {
|
||||
t.Errorf("builder_publish_queue acted = %v (%v), want the running's sequence 1", answer.Acted, err)
|
||||
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
||||
_, _ = b.Republish(ctx, "", "")
|
||||
items := Reader{Keys: func() ([]string, error) { return []string{"publish.m.0.1.0", "reserve-done.m.x"}, nil },
|
||||
Get: func(k string) (json.RawMessage, bool, error) {
|
||||
return json.RawMessage(`{"mode":"publish","module":"m","version":"0.1.0","tries":3,"next-try":"2026-11-17T01:00:00Z","why":"w"}`), true, nil
|
||||
}}
|
||||
none := Reader{Keys: func() ([]string, error) { return nil, nil }}
|
||||
answer, err := AnswerQueue(none, none, items)
|
||||
if err != nil || len(answer.Items) != 1 || answer.Items[0].Tries != 3 || answer.Items[0].Version != "0.1.0" {
|
||||
t.Errorf("builder_publish_queue items = %+v (%v), want the one publish item with its tries", answer.Items, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ const (
|
||||
// ReservedTool is builder_reserved, ReservedDropTool builder_reserved_drop.
|
||||
ReservedTool = "builder_reserved"
|
||||
ReservedDropTool = "builder_reserved_drop"
|
||||
// ReserveDoneTool is builder_reserve_done (design 47 G A-455).
|
||||
ReserveDoneTool = "builder_reserve_done"
|
||||
// UnreleasedBound is the publish's bound: past it a built reservation with an artifact unpublished is the finding
|
||||
// version-unreleased (§369).
|
||||
UnreleasedBound = 10 * time.Minute
|
||||
@@ -89,6 +91,9 @@ type Source struct {
|
||||
// that catalog revision (design 47 G §371, A-57), kept on the reservation for the publication.
|
||||
Branch string
|
||||
Revision int64
|
||||
// Rebuild marks a build again of a done key whose version is not released: the standing reservation of Fingerprint is replaced
|
||||
// and is no floor, so the number is the next above the catalog's highest alone (design 47 G A-490b).
|
||||
Rebuild bool
|
||||
}
|
||||
|
||||
// ReservedKey is the key of module's reservation of series (MAJOR.MINOR).
|
||||
@@ -110,7 +115,16 @@ type Reserver struct {
|
||||
ReleasedOf func(ctx context.Context, module string) ([]string, error)
|
||||
// Filled says the catalog said versions-filled.
|
||||
Filled func() bool
|
||||
Now func() time.Time
|
||||
// Took is told when a build took over the series' reservation of an older build never released (design 47 G A-455): the
|
||||
// older reservation, which no want follows any more, is gone from the key, replaced by the newer's. Nil is told none.
|
||||
Took func(ctx context.Context, older, newer ReservedVersion)
|
||||
// Releases is the builder's own record of the versions it saw released, by module and fingerprint (design 47 G A-456a): the
|
||||
// reservation is deleted at release, so a rerun of a released build's step reads it here, never from the catalog. Nil keeps none.
|
||||
Releases ReserveStore
|
||||
// ReleasedKept is how many releases per module the record keeps, the oldest dropped (the setting released-kept; A-456b); 0 is
|
||||
// DefaultReleasedKept.
|
||||
ReleasedKept int
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
func (r *Reserver) now() time.Time {
|
||||
@@ -157,7 +171,7 @@ func (r *Reserver) Reserve(ctx context.Context, module, series string, artifacts
|
||||
return ReservedVersion{}, err
|
||||
}
|
||||
next := high
|
||||
if found {
|
||||
if found && !(src.Rebuild && cur.Fingerprint == src.Fingerprint) {
|
||||
if _, p, err := SplitVersion(cur.Version); err == nil && p > next {
|
||||
next = p
|
||||
}
|
||||
@@ -182,6 +196,9 @@ func (r *Reserver) Reserve(ctx context.Context, module, series string, artifacts
|
||||
if err != nil {
|
||||
return ReservedVersion{}, fmt.Errorf("%s cannot be reserved: %w", key, err)
|
||||
}
|
||||
if found && r.Took != nil {
|
||||
r.Took(ctx, cur, out)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
@@ -240,6 +257,82 @@ func allSet(m map[string]string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DefaultReleasedKept is the setting released-kept unless set (design 47 G A-456b).
|
||||
const DefaultReleasedKept = 16
|
||||
|
||||
// ReleasedKey is the key of a module's record of its last releases (design 47 G A-456a, A-456b), in the state acted.
|
||||
func ReleasedKey(module string) string { return "released." + EscapeToken(module) }
|
||||
|
||||
// ReleasedRecord is that key's value: the module's last releases, oldest first.
|
||||
type ReleasedRecord struct {
|
||||
Kept []ReleasedBuild `json:"kept"`
|
||||
}
|
||||
|
||||
// ReleasedBuild is one release: the fingerprint of the build and its version.
|
||||
type ReleasedBuild struct {
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// ReleasedFor says whether the builder saw a version released for the build of fingerprint among the module's last releases.
|
||||
func (r *Reserver) ReleasedFor(ctx context.Context, module, fingerprint string) (bool, error) {
|
||||
if r.Releases == nil {
|
||||
return false, nil
|
||||
}
|
||||
raw, _, found, err := r.Releases.Get(ctx, ReleasedKey(module))
|
||||
if err != nil || !found {
|
||||
return false, err
|
||||
}
|
||||
var rec ReleasedRecord
|
||||
if json.Unmarshal(raw, &rec) != nil {
|
||||
return false, nil
|
||||
}
|
||||
for _, k := range rec.Kept {
|
||||
if k.Fingerprint == fingerprint {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// keepRelease adds a release to the module's record, dropping the oldest past ReleasedKept.
|
||||
func (r *Reserver) keepRelease(ctx context.Context, module, fingerprint, version string) error {
|
||||
n := r.ReleasedKept
|
||||
if n < 1 {
|
||||
n = DefaultReleasedKept
|
||||
}
|
||||
for {
|
||||
raw, rev, found, err := r.Releases.Get(ctx, ReleasedKey(module))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var rec ReleasedRecord
|
||||
if found {
|
||||
_ = json.Unmarshal(raw, &rec)
|
||||
}
|
||||
kept := rec.Kept[:0:0]
|
||||
for _, k := range rec.Kept {
|
||||
if k.Fingerprint != fingerprint {
|
||||
kept = append(kept, k)
|
||||
}
|
||||
}
|
||||
kept = append(kept, ReleasedBuild{Fingerprint: fingerprint, Version: version})
|
||||
if len(kept) > n {
|
||||
kept = kept[len(kept)-n:]
|
||||
}
|
||||
out, _ := json.Marshal(ReleasedRecord{Kept: kept})
|
||||
if found {
|
||||
_, err = r.Releases.Update(ctx, ReleasedKey(module), out, rev)
|
||||
} else {
|
||||
_, err = r.Releases.Create(ctx, ReleasedKey(module), out)
|
||||
}
|
||||
if errors.Is(err, ErrConflict) {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Released takes the catalog's version-released in: the key of that version is deleted, and every key of module's other
|
||||
// series (the manifest moved on). A key holding a newer reserved version of the same series stays. It answers the keys
|
||||
// deleted.
|
||||
@@ -261,6 +354,11 @@ func (r *Reserver) Released(ctx context.Context, module, version string) ([]stri
|
||||
if !found || (cur.Series == series && cur.Version != version) {
|
||||
continue
|
||||
}
|
||||
if cur.Version == version && cur.Fingerprint != "" && r.Releases != nil {
|
||||
if err := r.keepRelease(ctx, module, cur.Fingerprint, version); err != nil {
|
||||
return gone, fmt.Errorf("the release of %s %s cannot be recorded: %w", module, version, err)
|
||||
}
|
||||
}
|
||||
if err := r.Store.Delete(ctx, k); err != nil {
|
||||
return gone, fmt.Errorf("%s cannot be deleted: %w", k, err)
|
||||
}
|
||||
@@ -622,6 +720,10 @@ const (
|
||||
"seat said published. With module, that module's only. A key goes when the catalog says version-released for it. Read-only."
|
||||
ReservedDropDescription = "Drop a module's reservation of one MAJOR.MINOR by hand (design 47 G section 369), when its version will never be " +
|
||||
"released; the next build takes the next after the highest released. A hand act: it is recorded in the hand-act log."
|
||||
ReserveDoneDescription = "Reserve a version for a done trunk build that reserved none (design 47 G A-455): given module and fp, the next version " +
|
||||
"of the series is reserved for that fingerprint with the digests of the build's artifacts, and its publish is queued as builder_publish_again " +
|
||||
"queues it; builds nothing. For a done key that cannot be built again. Refused when the key is not done, is no first watched branch's, or " +
|
||||
"holds a reservation already. A hand act: it is recorded in the hand-act log."
|
||||
// ReservedVerb is the seat mesh-builds' verb served as a tool of the same name.
|
||||
ReservedVerb = meshbuilds.VerbReserved
|
||||
)
|
||||
|
||||
@@ -58,7 +58,14 @@ func TestReservation(t *testing.T) {
|
||||
if err != nil || first.Version != "0.1.20" {
|
||||
t.Fatalf("first build reserves %q (%v), want 0.1.20 after the highest released 0.1.19", first.Version, err)
|
||||
}
|
||||
var tookOlder, tookNewer []string
|
||||
r.Took = func(_ context.Context, older, newer ReservedVersion) {
|
||||
tookOlder, tookNewer = append(tookOlder, older.Version), append(tookNewer, newer.Version)
|
||||
}
|
||||
second, err := r.Reserve(ctx, "m", "0.1", arts, Source{})
|
||||
if len(tookOlder) != 1 || tookOlder[0] != "0.1.20" || tookNewer[0] != "0.1.21" {
|
||||
t.Fatalf("takeover of the older reservation told %v by %v, want 0.1.20 by 0.1.21 (design 47 G A-455)", tookOlder, tookNewer)
|
||||
}
|
||||
if err != nil || second.Version != "0.1.21" {
|
||||
t.Fatalf("second build reserves %q (%v), want 0.1.21: never the first's", second.Version, err)
|
||||
}
|
||||
@@ -166,3 +173,83 @@ func TestReservationIsReadAndMarkedPublished(t *testing.T) {
|
||||
t.Errorf("the bound key does not name the reservation back: %q %v", key, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes: design 47 G A-456a — the version-released of a version records
|
||||
// that the build of its fingerprint was released, so a rerun of its step finds it after the reservation is deleted.
|
||||
func TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
rel := &reserveMem{mem: newMem()}
|
||||
r := &Reserver{Store: &reserveMem{mem: newMem()}, Releases: rel, ReleasedKept: 2, Filled: func() bool { return true },
|
||||
ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }}
|
||||
v, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "src2:aa"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := r.ReleasedFor(ctx, "m", "src2:aa"); ok {
|
||||
t.Fatal("a build not released is said released")
|
||||
}
|
||||
if _, err := r.Released(ctx, "m", v.Version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, err := r.ReleasedFor(ctx, "m", "src2:aa"); !ok || err != nil {
|
||||
t.Errorf("released for the fingerprint: %v (%v), want true", ok, err)
|
||||
}
|
||||
if ok, _ := r.ReleasedFor(ctx, "m", "src2:bb"); ok {
|
||||
t.Error("another fingerprint is said released")
|
||||
}
|
||||
if all, _ := r.All(ctx); len(all) != 0 {
|
||||
t.Errorf("reservations left: %v", all)
|
||||
}
|
||||
// One key per module holds the last releases, the oldest dropped (A-456b).
|
||||
for _, fp := range []string{"src2:b1", "src2:b2"} {
|
||||
n, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: fp})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.Released(ctx, "m", n.Version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if keys, _ := rel.KeysOf(ctx, AllFilter); len(keys) != 1 {
|
||||
t.Errorf("release keys %v, want one per module", keys)
|
||||
}
|
||||
if old, _ := r.ReleasedFor(ctx, "m", "src2:aa"); old {
|
||||
t.Error("the oldest release is kept past released-kept")
|
||||
}
|
||||
if ok, _ := r.ReleasedFor(ctx, "m", "src2:b2"); !ok {
|
||||
t.Error("the newest release is not kept")
|
||||
}
|
||||
}
|
||||
|
||||
// TestARebuildReservesAboveTheCatalogsHighestWhateverItsStandingNumberWas: design 47 G A-490b — a build again of a done key replaces
|
||||
// its fingerprint's standing reservation (and tells Took) and numbers above the catalog's highest alone, whether the standing number
|
||||
// is below it or above it; another fingerprint's standing reservation stays a floor.
|
||||
func TestARebuildReservesAboveTheCatalogsHighestWhateverItsStandingNumberWas(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, c := range []struct {
|
||||
name, standing, standingFp string
|
||||
want string
|
||||
}{
|
||||
{name: "standing below the catalog's highest", standing: "0.1.0", standingFp: "fp", want: "0.1.17"},
|
||||
{name: "standing above the catalog's highest", standing: "0.1.30", standingFp: "fp", want: "0.1.17"},
|
||||
{name: "another fingerprint's standing number stays a floor", standing: "0.1.30", standingFp: "other", want: "0.1.31"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
store := &reserveMem{mem: newMem()}
|
||||
r := &Reserver{Store: store, Filled: func() bool { return true },
|
||||
ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"0.1.16"}, nil }}
|
||||
raw, _ := json.Marshal(ReservedVersion{V: 1, Module: "m", Series: "0.1", Version: c.standing, Fingerprint: c.standingFp, Artifacts: map[string]string{"m:a": "d"}})
|
||||
if _, err := store.Create(ctx, ReservedKey("m", "0.1"), raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var took []string
|
||||
r.Took = func(_ context.Context, older, newer ReservedVersion) {
|
||||
took = append(took, older.Version+">"+newer.Version)
|
||||
}
|
||||
got, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "fp", Rebuild: true})
|
||||
if err != nil || got.Version != c.want || len(took) != 1 || took[0] != c.standing+">"+c.want {
|
||||
t.Fatalf("reserved %q (%v), took %v; want %s replacing %s", got.Version, err, took, c.want, c.standing)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1
|
||||
}
|
||||
// 1945: the builder's tools (builder_seat_queue: note 62), served by its tools bundle, and the event it says when it asks an ask again.
|
||||
if want := []string{"builder_announce_again", "builder_ask_again", "builder_bounds", "builder_build", "builder_build_again", "builder_builds_since", "builder_engine_reads", "builder_first_view_again", "builder_first_views",
|
||||
"builder_floor_again", "builder_floor_waits", "builder_old_form", "builder_publish_again", "builder_publish_queue", "builder_release", "builder_reserved", "builder_reserved_drop", "builder_seat_list", "builder_seat_list_again", "builder_seat_queue", "builder_stuck", "builder_toolchains", "builder_toolchains_drop", "builder_versions", "builder_waits"}; !reflect.DeepEqual(sorted(m.Tools), want) {
|
||||
"builder_floor_again", "builder_floor_waits", "builder_old_form", "builder_publish_again", "builder_publish_queue", "builder_release", "builder_reserve_done", "builder_reserved", "builder_reserved_drop", "builder_seat_list", "builder_seat_list_again", "builder_seat_queue", "builder_stuck", "builder_toolchains", "builder_toolchains_drop", "builder_versions", "builder_waits"}; !reflect.DeepEqual(sorted(m.Tools), want) {
|
||||
t.Errorf("tools = %v, want %v", m.Tools, want)
|
||||
}
|
||||
// Design 47 G §672: the toolchain waits' stream, kept 7 days within 16 MiB, and the bound's setting, 60 minutes.
|
||||
@@ -185,8 +185,9 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1
|
||||
m.Streams[2].Name != "mesh-delivery-builder-findings" || !reflect.DeepEqual(m.Streams[2].Events, []string{"finding-written.*", "finding-cleared.*", "findings-stated.*"}) {
|
||||
t.Errorf("streams = %+v, want waits with wait-held.** and wait-ended.**, 7 days, 16 MiB, the builds' stream (build.ended.**, 7 days, kept last per subject: design 47 G A-181) and the findings' stream", m.Streams)
|
||||
}
|
||||
if c, ok := m.Config["toolchain-wait-minutes"]; !ok || c.Default != float64(60) || len(m.Config) != 2 || m.Config["republish-max-versions"].Default != float64(50) {
|
||||
t.Errorf("config = %+v, want toolchain-wait-minutes of 60 and republish-max-versions of 50 (design 47 G §588, §633)", m.Config)
|
||||
if c, ok := m.Config["toolchain-wait-minutes"]; !ok || c.Default != float64(60) || len(m.Config) != 5 ||
|
||||
m.Config["publish-pause"].Default != float64(30) || m.Config["publish-pause-max"].Default != float64(30) || m.Config["publish-tries"].Default != float64(8) {
|
||||
t.Errorf("config = %+v, want toolchain-wait-minutes of 60, the publish settings 30 s, 30 min and 8 tries (design 47 G §588, §633, A-457a)", m.Config)
|
||||
}
|
||||
// Design 47 G §349: version.numbered is no longer said; old-form-closed on the family version, apart from the builds' ends.
|
||||
if want := []string{"build.asked-again", "version.old-form-closed"}; !reflect.DeepEqual(m.Emits, want) {
|
||||
@@ -239,7 +240,7 @@ func TestManifestProcess(t *testing.T) { // 1607/AC1
|
||||
t.Errorf("process = %v", p)
|
||||
}
|
||||
env, _ := p["env"].(map[string]any)
|
||||
if len(env) != 3 || env["MESH_BROKER_FILE"] != "${dir:state}/broker" || env["MESH_BUILDER_REPUBLISH_MAX_FILE"] != "${dir:state}/republish-max-versions" || env["MESH_BUILDER_TOOLCHAIN_WAIT_FILE"] != "${dir:state}/toolchain-wait-minutes" {
|
||||
if len(env) != 6 || env["MESH_BUILDER_PUBLISH_TRIES_FILE"] != "${dir:state}/publish-tries" || env["MESH_BROKER_FILE"] != "${dir:state}/broker" || env["MESH_BUILDER_TOOLCHAIN_WAIT_FILE"] != "${dir:state}/toolchain-wait-minutes" {
|
||||
t.Errorf("env = %v, want the broker file and the path of the toolchain wait's file, never the setting itself (design 47 G §672, A-71)", p["env"])
|
||||
}
|
||||
for k, v := range env {
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
"builder_reserved",
|
||||
"builder_release",
|
||||
"builder_reserved_drop",
|
||||
"builder_reserve_done",
|
||||
"builder_seat_queue",
|
||||
"builder_stuck",
|
||||
"builder_toolchains",
|
||||
@@ -146,13 +147,37 @@
|
||||
"trusted": false,
|
||||
"default": 60
|
||||
},
|
||||
"republish-max-versions": {
|
||||
"publish-pause": {
|
||||
"type": "number",
|
||||
"description": "the most versions one event of a registry holder queues to publish again, newest first (design 47 G section 588); the rest wait for the next event or builder_publish_again.",
|
||||
"description": "seconds before a version's second publish try, doubled per try up to publish-pause-max (design 47 G A-457a).",
|
||||
"minimum": 1,
|
||||
"maximum": 10000,
|
||||
"maximum": 3600,
|
||||
"trusted": false,
|
||||
"default": 50
|
||||
"default": 30
|
||||
},
|
||||
"publish-pause-max": {
|
||||
"type": "number",
|
||||
"description": "minutes, the most a pause between a version's publish tries grows to (design 47 G A-457a).",
|
||||
"minimum": 1,
|
||||
"maximum": 1440,
|
||||
"trusted": false,
|
||||
"default": 30
|
||||
},
|
||||
"released-kept": {
|
||||
"type": "number",
|
||||
"description": "how many of a module's last releases the builder keeps, by fingerprint, to judge a rerun of a released build's step (design 47 G A-456b); the oldest is dropped.",
|
||||
"minimum": 1,
|
||||
"maximum": 1000,
|
||||
"trusted": false,
|
||||
"default": 16
|
||||
},
|
||||
"publish-tries": {
|
||||
"type": "number",
|
||||
"description": "how many times the builder tries a done build's publish before it ends loudly (log and condition) and leaves it readable in builder_publish_queue (design 47 G A-457a).",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"trusted": false,
|
||||
"default": 8
|
||||
}
|
||||
},
|
||||
"logs": [
|
||||
@@ -199,12 +224,36 @@
|
||||
"content": "${config:toolchain-wait-minutes}"
|
||||
},
|
||||
{
|
||||
"id": "republish-max",
|
||||
"id": "publish-pause",
|
||||
"type": "file",
|
||||
"path": "${dir:state}/republish-max-versions",
|
||||
"path": "${dir:state}/publish-pause",
|
||||
"owner": "mesh-delivery-builder",
|
||||
"mode": "0600",
|
||||
"content": "${config:republish-max-versions}"
|
||||
"content": "${config:publish-pause}"
|
||||
},
|
||||
{
|
||||
"id": "publish-pause-max",
|
||||
"type": "file",
|
||||
"path": "${dir:state}/publish-pause-max",
|
||||
"owner": "mesh-delivery-builder",
|
||||
"mode": "0600",
|
||||
"content": "${config:publish-pause-max}"
|
||||
},
|
||||
{
|
||||
"id": "released-kept",
|
||||
"type": "file",
|
||||
"path": "${dir:state}/released-kept",
|
||||
"owner": "mesh-delivery-builder",
|
||||
"mode": "0600",
|
||||
"content": "${config:released-kept}"
|
||||
},
|
||||
{
|
||||
"id": "publish-tries",
|
||||
"type": "file",
|
||||
"path": "${dir:state}/publish-tries",
|
||||
"owner": "mesh-delivery-builder",
|
||||
"mode": "0600",
|
||||
"content": "${config:publish-tries}"
|
||||
},
|
||||
{
|
||||
"id": "builder",
|
||||
@@ -220,7 +269,10 @@
|
||||
"env": {
|
||||
"MESH_BROKER_FILE": "${dir:state}/broker",
|
||||
"MESH_BUILDER_TOOLCHAIN_WAIT_FILE": "${dir:state}/toolchain-wait-minutes",
|
||||
"MESH_BUILDER_REPUBLISH_MAX_FILE": "${dir:state}/republish-max-versions"
|
||||
"MESH_BUILDER_PUBLISH_PAUSE_FILE": "${dir:state}/publish-pause",
|
||||
"MESH_BUILDER_PUBLISH_PAUSE_MAX_FILE": "${dir:state}/publish-pause-max",
|
||||
"MESH_BUILDER_PUBLISH_TRIES_FILE": "${dir:state}/publish-tries",
|
||||
"MESH_BUILDER_RELEASED_KEPT_FILE": "${dir:state}/released-kept"
|
||||
},
|
||||
"user": "mesh-delivery-builder",
|
||||
"health": {
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
//go:build integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-sdk/go/busretry"
|
||||
|
||||
"mesh-delivery-indexer/internal/index"
|
||||
)
|
||||
|
||||
// TestIndexerFillsTheFloorOfAModuleWithTags (design 47 G §412 as A-490c to A-490h amend it): the whole indexer on a real bus, the
|
||||
// seat git and the package holders stood in. A module of the trunk with a Go package and no version in `versions` stays unfilled
|
||||
// while the forge does not answer, and is filled from the highest of its tags and the holder's versions once the forge answers,
|
||||
// with no restart and no catalog revision: the kept call gives up after its bound (one second here, so that it is spent before the
|
||||
// forge answers) and the fill asks it again at once (A-490i, ADR 0668 §3). The finding after five minutes is in the unit tests (the
|
||||
// five minutes are not waited for here).
|
||||
func TestIndexerFillsTheFloorOfAModuleWithTags(t *testing.T) {
|
||||
t.Cleanup(busretry.Waits(time.Second, 50*time.Millisecond, 200*time.Millisecond)) // the form gives up after one second
|
||||
const pkg = "git.novox.be/novox/mesh-delivery/modules/" + testIndexer
|
||||
var serveTags func()
|
||||
var prefix atomic.Value
|
||||
js, ctx, _, _ := startIndexer(t, func(context.Context, buildLog) {}, func(ctx context.Context, js jetstream.JetStream) {
|
||||
for _, bucket := range []string{index.Bucket(testIndexer, index.VersionsState), index.Bucket(testIndexer, index.HeardState)} {
|
||||
if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: bucket, History: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// the module's artifacts are Go packages here, so that the indexer's own entries carry their names (read from go.mod, §676)
|
||||
e := trunkIndexerEntryOf(t, ctx, js, func(raw []byte) []byte {
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m["build"] = map[string]any{"artifacts": []any{map[string]any{"name": "go", "kind": "package", "language": "go"}}}
|
||||
out, _ := json.Marshal(m)
|
||||
return out
|
||||
})
|
||||
for i := range e.Artifacts {
|
||||
e.Artifacts[i].Package = &meshcatalog.ArtifactPackage{Ecosystem: index.EcosystemGo, Name: pkg}
|
||||
}
|
||||
seedBuildModules(t, ctx, js, map[string]meshcatalog.CatalogEntry{testIndexer: e})
|
||||
reply := func(m *nats.Msg, body map[string]any) {
|
||||
body["node"] = "n0"
|
||||
raw, _ := json.Marshal(body)
|
||||
_ = m.Respond(raw)
|
||||
}
|
||||
serve := func(subject string, h func(m *nats.Msg)) {
|
||||
if _, err := js.Conn().Subscribe(subject, h); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// the forge answers no one until the test lets it (nobody serves the verb: a transport fault, which the kept call asks again)
|
||||
serveTags = func() {
|
||||
serve("mesh.seat.git.tool.tags", func(m *nats.Msg) {
|
||||
var in git.TagsInput
|
||||
_ = json.Unmarshal(m.Data, &in)
|
||||
prefix.Store(in.Prefix)
|
||||
reply(m, map[string]any{"result": git.TagsAnswer{Repository: in.Repository, Tags: []string{in.Prefix + "0.4.2", in.Prefix + "0.4.10", "unrelated"}}})
|
||||
})
|
||||
}
|
||||
// the package's name is read from the revision's own files (§676): its go.mod
|
||||
serve("mesh.seat.git.tool.file", func(m *nats.Msg) {
|
||||
var in git.FileInput
|
||||
_ = json.Unmarshal(m.Data, &in)
|
||||
if strings.HasSuffix(in.Path, "go.mod") {
|
||||
reply(m, map[string]any{"result": git.FileAnswer{Path: in.Path, Ref: in.Ref, Content: "module " + pkg + "\n\ngo 1.26\n"}})
|
||||
return
|
||||
}
|
||||
reply(m, map[string]any{"result": git.FileAnswer{Path: in.Path, Ref: in.Ref, Absent: true}})
|
||||
})
|
||||
serve("mesh.seat.go-package-registry.tool.versions", func(m *nats.Msg) {
|
||||
reply(m, map[string]any{"result": map[string]any{"name": pkg, "versions": []any{}}})
|
||||
})
|
||||
})
|
||||
versions, err := js.KeyValue(ctx, index.Bucket(testIndexer, index.VersionsState))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// the forge is asked at the start and does not answer: the form gives up and the module's clock is kept in the state heard
|
||||
heard, err := js.KeyValue(ctx, index.Bucket(testIndexer, index.HeardState))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for deadline := time.Now().Add(30 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
|
||||
if _, err := heard.Get(ctx, index.FloorSinceKey(testIndexer)); err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if _, err := heard.Get(ctx, index.FloorSinceKey(testIndexer)); err != nil {
|
||||
t.Fatalf("the unanswered floor read of the module is not kept within 30 s: %v", err)
|
||||
}
|
||||
if keys, _ := versions.Keys(ctx); len(keys) != 0 {
|
||||
t.Fatalf("versions holds %v while the forge does not answer, want nothing", keys)
|
||||
}
|
||||
serveTags()
|
||||
var got []string
|
||||
for deadline := time.Now().Add(30 * time.Second); time.Now().Before(deadline); time.Sleep(100 * time.Millisecond) {
|
||||
if got, _ = versions.Keys(ctx); len(got) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(got) != 1 || !strings.Contains(got[0], "0.4.10") {
|
||||
t.Fatalf("versions holds %v within 30 s of the forge answering, want the floor 0.4.10 of its tags", got)
|
||||
}
|
||||
if p, _ := prefix.Load().(string); p != "modules/"+testIndexer+"/v" {
|
||||
t.Errorf("the tags were read with the prefix %q, want the package's directory and /v", p)
|
||||
}
|
||||
}
|
||||
@@ -392,11 +392,17 @@ func TestIndexerWhole(t *testing.T) { // 1238/AC2, AC10; 1585/T14, T15
|
||||
|
||||
// trunkIndexerEntry keeps the indexer's own manifest by its digest, accepted, and answers its trunk entry at an earlier commit.
|
||||
func trunkIndexerEntry(t *testing.T, ctx context.Context, js jetstream.JetStream) meshcatalog.CatalogEntry {
|
||||
return trunkIndexerEntryOf(t, ctx, js, func(raw []byte) []byte { return raw })
|
||||
}
|
||||
|
||||
// trunkIndexerEntryOf is trunkIndexerEntry with the manifest's bytes changed by edit first.
|
||||
func trunkIndexerEntryOf(t *testing.T, ctx context.Context, js jetstream.JetStream, edit func([]byte) []byte) meshcatalog.CatalogEntry {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile("../../module.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw = edit(raw)
|
||||
var m catalogue.Manifest
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -491,7 +491,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
ix.Registry = registry
|
||||
named.Changed = ix.RemoveUnlisted
|
||||
// The first fill of the versions (design 47 G §372): at the start, and again at each catalog revision kept until it is made.
|
||||
fill := &versionsFill{ix: ix, ctx: ctx, ask: versionsVia(calls.CallKeptWhileOpen, ix.ModuleInNewestView, logger.Printf), logf: logger.Printf}
|
||||
fill := &versionsFill{ix: ix, ctx: ctx, ask: versionsVia(calls.CallKeptWhileOpen, ix.ModuleInNewestView, logger.Printf), floor: index.FloorSources{Tags: tagsVia(calls.CallKept), Holder: holderVia(calls.CallKept)}, logf: logger.Printf}
|
||||
fillFollow.Store(fill)
|
||||
fill.kick()
|
||||
ix.SeedModules = func(ctx context.Context) ([]index.SeedModule, error) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -12,10 +13,14 @@ import (
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/gopackageregistry"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/npmpackageregistry"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
|
||||
"git.novox.be/novox/mesh-sdk/go/process"
|
||||
|
||||
"mesh-delivery-indexer/internal/index"
|
||||
@@ -187,10 +192,11 @@ func versionsVia(call openCaller, inView func(ctx context.Context, module string
|
||||
// versionsFill is the first fill of the versions in the background (design 47 G §372): started at the start and at each
|
||||
// catalog revision kept, until it is made; one runs at a time, and a fill that fails is said and made again at the next.
|
||||
type versionsFill struct {
|
||||
ix *index.Indexer
|
||||
ctx context.Context
|
||||
ask index.VersionsAsker
|
||||
logf func(string, ...any)
|
||||
ix *index.Indexer
|
||||
ctx context.Context
|
||||
ask index.VersionsAsker
|
||||
floor index.FloorSources // what the floor reads (A-490g); a nil reader fills none
|
||||
logf func(string, ...any)
|
||||
}
|
||||
|
||||
func (f *versionsFill) kick() {
|
||||
@@ -199,6 +205,28 @@ func (f *versionsFill) kick() {
|
||||
f.logf("versions: the first fill waits: %v", err)
|
||||
}
|
||||
}()
|
||||
go f.fillFloors()
|
||||
}
|
||||
|
||||
// fillFloors fills the floor of each module the versions hold none of, each on its own so that one unanswered read holds no other
|
||||
// module's (A-490d, A-490f, A-490i): at every start and every revision, not waiting for the builder's half. A read is a kept call
|
||||
// asked again until it answers, and the indexer holds no timer of its own (ADR 0437 §8).
|
||||
func (f *versionsFill) fillFloors() {
|
||||
if f.floor.Tags == nil && f.floor.Holder == nil {
|
||||
return
|
||||
}
|
||||
modules, err := f.ix.FloorModules(f.ctx)
|
||||
if err != nil {
|
||||
f.logf("versions: the floor waits: %v", err)
|
||||
return
|
||||
}
|
||||
for _, m := range modules {
|
||||
go func() {
|
||||
if err := f.ix.FillFloor(f.ctx, m, f.floor); err != nil {
|
||||
f.logf("versions: the floor of %s waits for the next start or revision: %v", m, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// afterRevision runs the fill's kick after each catalog revision kept, once the fill is set.
|
||||
@@ -266,3 +294,49 @@ func runningWholeVia(js jetstream.JetStream) index.RunningReader {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// tagsVia reads every tag of a repository that starts with a prefix from the seat git's verb tags as a kept call, page by
|
||||
// page until none is more (design 47 G §412, A-490c: protected or not).
|
||||
func tagsVia(call toolCaller) index.TagsReader {
|
||||
return func(_ context.Context, repository, prefix string) ([]string, error) {
|
||||
var out []string
|
||||
for page := int64(1); ; page++ {
|
||||
var a git.TagsAnswer
|
||||
in := git.TagsInput{Repository: trunk.RepositoryName(repository), Prefix: prefix, Page: page}
|
||||
if !strings.Contains(in.Repository, "/") {
|
||||
return nil, fmt.Errorf("%q is not <owner>/<name>", repository)
|
||||
}
|
||||
if err := askGit(call, git.VerbTags, in, &a); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, a.Tags...)
|
||||
if !a.More {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// holderVia reads the versions a package holder answers for a package: the seat npm-package-registry's or go-package-registry's
|
||||
// verb versions (A-490g), as a kept call.
|
||||
func holderVia(call toolCaller) index.HolderReader {
|
||||
return func(_ context.Context, ecosystem, name string) ([]string, error) {
|
||||
seat := npmpackageregistry.Seat
|
||||
if ecosystem == index.EcosystemGo {
|
||||
seat = gopackageregistry.Seat
|
||||
}
|
||||
raw, err := call("seat:"+seat+"."+npmpackageregistry.VerbVersions, npmpackageregistry.VersionsInput{Name: name, Kind: npmpackageregistry.ArtifactKindPackage})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var a npmpackageregistry.VersionsAnswer
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("the answer of %s.versions is not a package's versions: %w", seat, err)
|
||||
}
|
||||
var out []string
|
||||
for _, v := range a.Versions {
|
||||
out = append(out, v.Version)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,6 +174,7 @@ type Indexer struct {
|
||||
previews map[string]meshdeliverychecker.PreviewsByRepositoryHeadTrunkValue // by previews key (previews.go)
|
||||
toolchains map[string]map[string]nodebuildgo.Toolchain // language, then node (toolchains.go)
|
||||
buildModules map[string]BuildModule // by language: the build modules whose toolchains are taken in (buildmodules.go)
|
||||
floors sync.Map // the modules whose floor FillFloor reads now (tag_floor.go), one fill each
|
||||
versionsFilling atomic.Bool // a FillVersions runs (versions_fill.go)
|
||||
stepsSince map[string]time.Time // by build seat: since when its steps is unanswered (seed_steps.go)
|
||||
tried map[string]bool // repository, commit and directory a registered module's manifest was tried at (registered.go)
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-sdk/go/busretry"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
)
|
||||
|
||||
// The floor of the catalog's versions (design 47 G §412 as A-490c to A-490h amend it): for every module the state versions
|
||||
// holds no version of, the highest version among every package its catalog entry names is its first version: the Go
|
||||
// package's tags in the repository the entry was taken in from, read from the seat git's verb tags, protected or not
|
||||
// (A-490c, A-490e), and what each package holder answers for the package through its seat's verb versions (A-490g). It is
|
||||
// filled per module whenever the indexer starts and at each catalog revision kept, not once (A-490d, A-490f): the key
|
||||
// versions-filled closes only the builder's half of the fill.
|
||||
|
||||
// FloorCall is the id of the finding call-unanswered when a floor read (the forge's tags or a package holder's versions) is
|
||||
// unanswered five minutes after the first (A-490h).
|
||||
const FloorCall = "mesh-catalog.versions-floor"
|
||||
|
||||
// TagsReader reads every tag of a repository that starts with prefix, all pages (the seat git's verb tags).
|
||||
type TagsReader func(ctx context.Context, repository, prefix string) ([]string, error)
|
||||
|
||||
// HolderReader reads the versions a package holder answers for a package of an ecosystem (EcosystemGo, EcosystemNpm)
|
||||
// through its seat's verb versions (A-490g).
|
||||
type HolderReader func(ctx context.Context, ecosystem, name string) ([]string, error)
|
||||
|
||||
// FloorSources are what the floor reads: the forge's tags and each package holder's versions.
|
||||
type FloorSources struct {
|
||||
Tags TagsReader
|
||||
Holder HolderReader
|
||||
}
|
||||
|
||||
// tagPrefixOf is the prefix Go gives the tags of a Go package in its repository (A-490e): the package's directory in the
|
||||
// repository followed by "/v" ("go/v" for the sdk, whose Go module lives in go/), "v" at the root; none when the package's
|
||||
// name does not lie in the repository.
|
||||
func tagPrefixOf(repository string, a meshcatalog.Artifact) (string, bool) {
|
||||
if a.Package == nil || a.Package.Ecosystem != EcosystemGo || repository == "" {
|
||||
return "", false
|
||||
}
|
||||
i := strings.Index(a.Package.Name, "/"+repository)
|
||||
if i < 0 {
|
||||
return "", false
|
||||
}
|
||||
switch rest := a.Package.Name[i+1+len(repository):]; {
|
||||
case rest == "":
|
||||
return "v", true
|
||||
case strings.HasPrefix(rest, "/"):
|
||||
return rest[1:] + "/v", true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// FloorModules are the modules of the newest trunk revision the versions hold none of and that name a package a floor can be read
|
||||
// of; each is filled by its own FillFloor, so one unanswered read holds no other module's floor (A-490i).
|
||||
func (ix *Indexer) FloorModules(ctx context.Context) ([]string, error) {
|
||||
if ix.Versions == nil || ix.Revisions == nil {
|
||||
return nil, errors.New("the state versions or the catalog graph is not kept here")
|
||||
}
|
||||
_, entries, found, err := readNewestOf(ctx, ix.Revisions, TrunkBranch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !found {
|
||||
return nil, errors.New("the indexer keeps no trunk revision yet: the floor waits for its first")
|
||||
}
|
||||
var out []string
|
||||
for _, e := range entries {
|
||||
if !slices.ContainsFunc(e.Artifacts, func(a meshcatalog.Artifact) bool {
|
||||
return a.Package != nil && (a.Package.Ecosystem == EcosystemGo || a.Package.Ecosystem == EcosystemNpm)
|
||||
}) {
|
||||
continue
|
||||
}
|
||||
if have, err := ModuleVersions(ctx, ix.Versions, e.Module); err != nil {
|
||||
return nil, err
|
||||
} else if len(have) == 0 {
|
||||
out = append(out, e.Module)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FillFloor fills one module's floor, the highest version among its packages' tags and holders' versions (A-490g). Its reads are
|
||||
// kept calls made through mesh-sdk's form, which asks again through every transport fault; a read it gives up on (its own
|
||||
// pause spent) is asked again at once, never given up, so the read has no bound (A-490i). The first unanswered try starts the
|
||||
// module's clock: five minutes later the finding call-unanswered is written, and cleared when a read answers. A read the
|
||||
// callee answered with a failure ends the fill, left for the next start or revision. One fill of a module runs at a time.
|
||||
func (ix *Indexer) FillFloor(ctx context.Context, module string, src FloorSources) error {
|
||||
if _, running := ix.floors.LoadOrStore(module, true); running {
|
||||
return nil
|
||||
}
|
||||
defer ix.floors.Delete(module)
|
||||
// Whatever way the fill ends but by the end of ctx, the module's call is no longer unanswered: it answered, or it left the
|
||||
// trunk, or a release gave it a version, or the callee answered with a failure (ADR 0668 §3: the finding clears when the call
|
||||
// is answered), and FloorModules never picks the module again to clear it.
|
||||
defer func() {
|
||||
if ctx.Err() == nil {
|
||||
ix.floorAnswered(ctx, module)
|
||||
}
|
||||
}()
|
||||
for ctx.Err() == nil {
|
||||
entry, found, err := ix.newestEntry(ctx, module)
|
||||
if err != nil || !found {
|
||||
return err
|
||||
}
|
||||
if have, err := ModuleVersions(ctx, ix.Versions, module); err != nil || len(have) > 0 {
|
||||
return err
|
||||
}
|
||||
first := ix.now() // the clock of the module's unanswered call starts at its try, not when the form gives up
|
||||
top, err := ix.floorOf(ctx, entry, src)
|
||||
if err == nil {
|
||||
return ix.keepFloor(ctx, module, top)
|
||||
}
|
||||
if !busretry.Undelivered(err) && !busretry.TimedOut(err) {
|
||||
return err // answered with a failure: the fill ends, said by the caller
|
||||
}
|
||||
ix.floorUnanswered(ctx, module, first, err)
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func (ix *Indexer) newestEntry(ctx context.Context, module string) (meshcatalog.CatalogEntry, bool, error) {
|
||||
_, entries, found, err := readNewestOf(ctx, ix.Revisions, TrunkBranch)
|
||||
if err != nil || !found {
|
||||
return meshcatalog.CatalogEntry{}, false, err
|
||||
}
|
||||
for _, e := range entries {
|
||||
if e.Module == module {
|
||||
return e, true, nil
|
||||
}
|
||||
}
|
||||
return meshcatalog.CatalogEntry{}, false, nil
|
||||
}
|
||||
|
||||
// floorOf reads the highest version of the entry's packages; "" when none is held.
|
||||
func (ix *Indexer) floorOf(ctx context.Context, e meshcatalog.CatalogEntry, src FloorSources) (string, error) {
|
||||
top := ""
|
||||
take := func(vs []string, prefix string) {
|
||||
for _, t := range vs {
|
||||
if !strings.HasPrefix(t, prefix) {
|
||||
continue
|
||||
}
|
||||
if v := strings.TrimPrefix(t, prefix); versionOK(v) && (top == "" || compareVersions(v, top) > 0) {
|
||||
top = v
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, a := range e.Artifacts {
|
||||
if a.Package == nil {
|
||||
continue
|
||||
}
|
||||
if prefix, ok := tagPrefixOf(e.Repository, a); ok && src.Tags != nil {
|
||||
tags, err := src.Tags(ctx, e.Repository, prefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the tags of %s: %w", e.Repository, err)
|
||||
}
|
||||
take(tags, prefix)
|
||||
}
|
||||
if (a.Package.Ecosystem == EcosystemGo || a.Package.Ecosystem == EcosystemNpm) && src.Holder != nil {
|
||||
vs, err := src.Holder(ctx, a.Package.Ecosystem, a.Package.Name)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the versions of %s %s: %w", a.Package.Ecosystem, a.Package.Name, err)
|
||||
}
|
||||
take(vs, "")
|
||||
}
|
||||
}
|
||||
return top, nil
|
||||
}
|
||||
|
||||
func (ix *Indexer) keepFloor(ctx context.Context, module, top string) error {
|
||||
if top == "" {
|
||||
return nil
|
||||
}
|
||||
now := ix.now().UTC()
|
||||
data, err := json.Marshal(meshcatalog.VersionEntry{V: int64(keys.Version), Module: module, Version: top, At: &now})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ix.Versions.Create(ctx, meshcatalog.VersionsByModuleVersionKey(module, top), data); err != nil && !errors.Is(err, ErrConflict) {
|
||||
return fmt.Errorf("the floor %s %s is not kept: %w", module, top, err)
|
||||
}
|
||||
ix.logf("versions: %s has none, filled with %s, the highest of its packages' tags and holders' versions", module, top)
|
||||
return nil
|
||||
}
|
||||
|
||||
func floorKey(module string) string { return FloorCall + "." + module }
|
||||
|
||||
// FloorSinceKey is the key of the state heard that holds when a module's floor call was first unanswered.
|
||||
func FloorSinceKey(module string) string { return floorSinceKey(module) }
|
||||
|
||||
func floorSinceKey(module string) string { return "call-since." + keys.EscapeName(floorKey(module)) }
|
||||
|
||||
// floorUnanswered writes call-unanswered once the module's floor read is unanswered five minutes (the time kept in the own state).
|
||||
func (ix *Indexer) floorUnanswered(ctx context.Context, module string, first time.Time, cause error) {
|
||||
now := ix.now()
|
||||
since := ix.unansweredSince(ctx, floorSinceKey(module), first)
|
||||
if ix.Findings == nil || now.Sub(since) < mcc.CallUnansweredAfter {
|
||||
return
|
||||
}
|
||||
at := since.UTC().Format(time.RFC3339)
|
||||
f := ix.observed(mcc.ScopeCall, floorKey(module), mcc.KindCallUnanswered,
|
||||
fmt.Sprintf("the floor of %s's versions is unanswered since %s", module, at),
|
||||
"A call whose answer is kept is not answered",
|
||||
fmt.Sprintf("The floor of %s's versions reads the forge's tags and the package holders' versions; a read is not answered since %s (%v), so it has no version.", module, at, cause),
|
||||
"indexer."+mcc.KindCallUnanswered)
|
||||
if err := ix.Findings.Write(ctx, f); err != nil {
|
||||
ix.logf("floor: the finding %s is not written: %v", floorKey(module), err)
|
||||
}
|
||||
}
|
||||
|
||||
func (ix *Indexer) floorAnswered(ctx context.Context, module string) {
|
||||
ix.answered(ctx, floorSinceKey(module))
|
||||
if ix.Findings != nil {
|
||||
if err := ix.clearFinding(ctx, mcc.ScopeCall, floorKey(module), mcc.KindCallUnanswered, "the floor's reads answer"); err != nil {
|
||||
ix.logf("floor: the finding %s is not cleared: %v", floorKey(module), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func versionOK(v string) bool { _, ok := versionParts(v); return ok }
|
||||
@@ -120,9 +120,9 @@ func (ix *Indexer) HearPackagePublished(ctx context.Context, p meshbuilds.Packag
|
||||
return ix.hearBranchBuild(ctx, p) // releases no version; its edges are kept in build-edges (A-83)
|
||||
}
|
||||
}
|
||||
if _, _, found, err := ix.Versions.Get(ctx, meshcatalog.VersionsByModuleVersionKey(p.Module, p.Version)); err != nil {
|
||||
if raw, _, found, err := ix.Versions.Get(ctx, meshcatalog.VersionsByModuleVersionKey(p.Module, p.Version)); err != nil {
|
||||
return err
|
||||
} else if found {
|
||||
} else if found && !isFloor(raw) {
|
||||
ix.logf("versions: %s %s is released already, so the package-published of %s is heard again and changes nothing", p.Module, p.Version, p.Artifact)
|
||||
return nil
|
||||
}
|
||||
@@ -212,7 +212,14 @@ func (ix *Indexer) releaseVersion(ctx context.Context, p meshbuilds.PackagePubli
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ix.Versions.Create(ctx, key, data); err != nil && !errors.Is(err, ErrConflict) {
|
||||
if err := ix.Versions.Create(ctx, key, data); errors.Is(err, ErrConflict) {
|
||||
// A floor entry (A-490h) holds no artifacts: this publish is that version's release, so it takes the entry's place.
|
||||
if raw, rev, found, gerr := ix.Versions.Get(ctx, key); gerr == nil && found && isFloor(raw) {
|
||||
if err := ix.Versions.Update(ctx, key, data, rev); err != nil && !errors.Is(err, ErrConflict) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if late {
|
||||
@@ -339,3 +346,9 @@ func OutdatedTool(ix *Indexer) stdio.Tool {
|
||||
return ix.Outdated(context.Background(), in)
|
||||
}}
|
||||
}
|
||||
|
||||
// isFloor says whether a kept version is a floor entry (A-490h): it holds no artifact, as no release does.
|
||||
func isFloor(raw []byte) bool {
|
||||
var v meshcatalog.VersionEntry
|
||||
return json.Unmarshal(raw, &v) == nil && len(v.Artifacts) == 0
|
||||
}
|
||||
|
||||
@@ -534,3 +534,187 @@ func TestAnInputIsDeletedAtTheStepEndedOfItsAsk(t *testing.T) {
|
||||
t.Errorf("indexer_inputs counts a key by ask: %+v, %v", a, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G §412 as A-490c, A-490d and A-490e amend it: a module the versions hold none of is filled from the highest tag of its
|
||||
// Go package in the repository it was taken in from, per module and not once; a forge that does not answer fills nothing and
|
||||
// raises call-unanswered after five minutes.
|
||||
func TestTheTagFloorFillsAModuleWithNoVersionFromItsGoTags(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
now := time.Date(2026, 11, 19, 17, 0, 0, 0, time.UTC)
|
||||
w := newFindings()
|
||||
ix := &Indexer{Module: testSelf, Versions: newMem(), Heard: newMem(), Findings: w, Revisions: newMemObjects(1 << 20), Now: func() time.Time { return now }}
|
||||
entries := map[string]meshcatalog.CatalogEntry{
|
||||
"mesh-sdk": {Module: "mesh-sdk", Repository: "novox/mesh-sdk", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{
|
||||
{Ref: "mesh-sdk:go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.novox.be/novox/mesh-sdk/go"}},
|
||||
{Ref: "mesh-sdk:ts", Package: &meshcatalog.ArtifactPackage{Ecosystem: "npm", Name: "@novox/mesh-sdk"}}}},
|
||||
"root": {Module: "root", Repository: "novox/root", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{
|
||||
{Ref: "root:go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.novox.be/novox/root"}}}},
|
||||
"held": {Module: "held", Repository: "novox/held", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{{Ref: "held:go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.novox.be/novox/held"}}}},
|
||||
"image": {Module: "image", Repository: "novox/image", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{{Ref: "image:image", Kind: "image"}}},
|
||||
}
|
||||
rev, objects, err := CatalogRevisionOf(meshcatalog.CatalogRevision{Revision: 3, Kind: "trunk"}, entries)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := KeepRevision(ctx, ix.Revisions, rev, objects); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ix.Versions.Create(ctx, meshcatalog.VersionsByModuleVersionKey("held", "0.2.0"), []byte(`{"v":1,"module":"held","version":"0.2.0"}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tries := 0
|
||||
var asked []string
|
||||
read := func(_ context.Context, repo, prefix string) ([]string, error) {
|
||||
asked = append(asked, repo+" "+prefix)
|
||||
if repo == "novox/mesh-sdk" {
|
||||
// the forge does not answer the sdk's tags for six minutes, a minute at a time: the fill asks again, never gives up
|
||||
tries++
|
||||
now = now.Add(time.Minute)
|
||||
if tries == 5 && len(w.Held()) != 0 {
|
||||
t.Error("the finding is written before five minutes from the first unanswered try")
|
||||
}
|
||||
if tries <= 6 {
|
||||
return nil, errors.New("no responders available for request")
|
||||
}
|
||||
if len(w.Held()) != 1 || w.Held()[0].ID != FloorCall+".mesh-sdk" {
|
||||
t.Errorf("the findings after six minutes: %+v", w.Held())
|
||||
}
|
||||
return []string{"go/v0.1.9", "go/v0.1.16", "go/v0.1.12", "go/vnext"}, nil
|
||||
}
|
||||
if repo == "novox/root" {
|
||||
return []string{"v1.2.3", "vendor-x"}, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
holder := func(_ context.Context, eco, name string) ([]string, error) {
|
||||
if eco == "npm" && name == "@novox/mesh-sdk" {
|
||||
return []string{"0.1.18", "0.1.17"}, nil // A-490g: the npm holder is above the Go tags
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
src := FloorSources{Tags: read, Holder: holder}
|
||||
modules, err := ix.FloorModules(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !slices.Equal(modules, []string{"mesh-sdk", "root"}) {
|
||||
t.Fatalf("the modules with a floor to read: %v (held has a version, image no package)", modules)
|
||||
}
|
||||
// each module's floor is read on its own: root is filled whatever the sdk's read waits for
|
||||
if err := ix.FillFloor(ctx, "root", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ix.FillFloor(ctx, "mesh-sdk", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(w.Held()) != 0 {
|
||||
t.Errorf("the finding stands after the forge answered: %+v", w.Held())
|
||||
}
|
||||
if v, _ := ModuleVersions(ctx, ix.Versions, "mesh-sdk"); len(v) != 1 || v[0].Version != "0.1.18" {
|
||||
t.Errorf("mesh-sdk's floor: %+v (the highest of its Go tags 0.1.16 and the npm holder's 0.1.18)", v)
|
||||
}
|
||||
if v, _ := ModuleVersions(ctx, ix.Versions, "root"); len(v) != 1 || v[0].Version != "1.2.3" {
|
||||
t.Errorf("a root module's floor: %+v", v)
|
||||
}
|
||||
if v, _ := ModuleVersions(ctx, ix.Versions, "held"); len(v) != 1 || v[0].Version != "0.2.0" {
|
||||
t.Errorf("a module with a version is filled: %+v", v)
|
||||
}
|
||||
for _, a := range asked {
|
||||
if strings.HasPrefix(a, "novox/held") || strings.HasPrefix(a, "novox/image") {
|
||||
t.Errorf("tags read for a module with a version or no Go package: %s", a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-490h: a floor entry holds no artifacts, so the package-published heard later at its version is that version's
|
||||
// release (the entry takes its artifacts and version-released is said), never dropped as released already.
|
||||
func TestAFloorEntryIsReplacedByThePublishHeardAtItsVersion(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
now := time.Date(2026, 11, 19, 17, 0, 0, 0, time.UTC)
|
||||
var said []meshcatalog.VersionReleasedEvent
|
||||
ix := &Indexer{Module: testSelf, Versions: newMem(), Heard: newMem(), Findings: newFindings(), Revisions: newMemObjects(1 << 20), Now: func() time.Time { return now },
|
||||
SayVersionReleased: func(_ context.Context, e meshcatalog.VersionReleasedEvent, _ string) error {
|
||||
said = append(said, e)
|
||||
return nil
|
||||
}}
|
||||
entries := map[string]meshcatalog.CatalogEntry{"mod": {Module: "mod", Repository: "novox/mod", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{
|
||||
{Ref: "mod:go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.novox.be/novox/mod"}}}}}
|
||||
rev, objects, err := CatalogRevisionOf(meshcatalog.CatalogRevision{Revision: 1, Kind: "trunk"}, entries)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := KeepRevision(ctx, ix.Revisions, rev, objects); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := FloorSources{Tags: func(context.Context, string, string) ([]string, error) { return []string{"v0.3.0"}, nil }}
|
||||
if err := ix.FillFloor(ctx, "mod", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ix.HearPackagePublished(ctx, meshbuilds.PackagePublished{Module: "mod", Artifact: "mod:go", Version: "0.3.0", Digest: "sha256:aa", Branch: "main", Source: 5}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
v, _ := ModuleVersions(ctx, ix.Versions, "mod")
|
||||
if len(v) != 1 || len(v[0].Artifacts) != 1 || v[0].Artifacts[0].Digest != "sha256:aa" || len(said) != 1 {
|
||||
t.Fatalf("the floor 0.3.0 and its publish: versions %+v, version-released said %d", v, len(said))
|
||||
}
|
||||
}
|
||||
|
||||
// ADR 0668 §3, design 47 G A-490i: the finding call-unanswered of a module's floor clears, with its call-since key, on every way the
|
||||
// fill ends but the end of its context: a release gave the module a version, the module left the trunk, or the callee answered with a
|
||||
// failure (FloorModules never picks the module again to clear it).
|
||||
func TestTheFloorFindingClearsOnEveryExitOfTheFill(t *testing.T) {
|
||||
for name, exit := range map[string]func(ctx context.Context, t *testing.T, ix *Indexer) error{
|
||||
"a release gave it a version": func(ctx context.Context, t *testing.T, ix *Indexer) error {
|
||||
if err := ix.Versions.Create(ctx, meshcatalog.VersionsByModuleVersionKey("mod", "0.5.0"), []byte(`{"v":1,"module":"mod","version":"0.5.0","artifacts":[{"artifact":"mod:go"}]}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return errors.New("no responders available for request")
|
||||
},
|
||||
"it left the trunk": func(ctx context.Context, t *testing.T, ix *Indexer) error {
|
||||
rev, objects, err := CatalogRevisionOf(meshcatalog.CatalogRevision{Revision: 2, Kind: "trunk"}, map[string]meshcatalog.CatalogEntry{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := KeepRevision(ctx, ix.Revisions, rev, objects); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return errors.New("no responders available for request")
|
||||
},
|
||||
"the callee answered with a failure": func(context.Context, *testing.T, *Indexer) error { return errors.New("the forge refused") },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
now := time.Date(2026, 11, 19, 17, 0, 0, 0, time.UTC)
|
||||
w := newFindings()
|
||||
ix := &Indexer{Module: testSelf, Versions: newMem(), Heard: newMem(), Findings: w, Revisions: newMemObjects(1 << 20), Now: func() time.Time { return now }}
|
||||
entries := map[string]meshcatalog.CatalogEntry{"mod": {Module: "mod", Repository: "novox/mod", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{
|
||||
{Ref: "mod:go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.novox.be/novox/mod"}}}}}
|
||||
rev, objects, err := CatalogRevisionOf(meshcatalog.CatalogRevision{Revision: 1, Kind: "trunk"}, entries)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := KeepRevision(ctx, ix.Revisions, rev, objects); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tries := 0
|
||||
read := func(context.Context, string, string) ([]string, error) {
|
||||
tries++
|
||||
now = now.Add(time.Minute)
|
||||
if tries <= 6 {
|
||||
return nil, errors.New("no responders available for request")
|
||||
}
|
||||
if len(w.Held()) != 1 {
|
||||
t.Fatalf("the finding is not written before the exit: %+v", w.Held())
|
||||
}
|
||||
return nil, exit(ctx, t, ix)
|
||||
}
|
||||
_ = ix.FillFloor(ctx, "mod", FloorSources{Tags: read})
|
||||
if h := w.Held(); len(h) != 0 {
|
||||
t.Errorf("the finding stands after the fill ended: %+v", h)
|
||||
}
|
||||
if _, _, found, _ := ix.Heard.Get(ctx, FloorSinceKey("mod")); found {
|
||||
t.Error("the call-since key stands after the fill ended")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ func TestManifestDeclarations(t *testing.T) { // 1238/AC1, 1444 (reads the build
|
||||
// Three invokes: the file of a registered module's manifest and lock files (registered.go), the forge's trees (the named paths' objects, since the indexer is the one reader of a manifest's paths:
|
||||
// ADR 0499 clauses 2 and 3, ADR 0495 clause 1; and the module directories of the trunk, issue 1900) and whether a
|
||||
// commit is in another (issue 1900).
|
||||
if want := []string{"seat:git.trees", "seat:git.file", "seat:git.contains", "seat:git.open-pulls", "seat:git.branch", "mesh-delivery-forge-bridge.bridge_merged_message", "seat:node-engine.composition", "seat:mesh-delivery.source", "seat:mesh-module-assigner.assignments", "seat:mesh-module-assigner.placement", "seat:mesh-hand-acts.hand-act", "seat:mesh-watchdogs.presence", "seat:node-build-go.steps",
|
||||
if want := []string{"seat:git.trees", "seat:git.tags", "seat:go-package-registry.versions", "seat:npm-package-registry.versions", "seat:git.file", "seat:git.contains", "seat:git.open-pulls", "seat:git.branch", "mesh-delivery-forge-bridge.bridge_merged_message", "seat:node-engine.composition", "seat:mesh-delivery.source", "seat:mesh-module-assigner.assignments", "seat:mesh-module-assigner.placement", "seat:mesh-hand-acts.hand-act", "seat:mesh-watchdogs.presence", "seat:node-build-go.steps",
|
||||
"seat:node-build-node.steps", "seat:node-build-image.steps", "mesh-delivery-builder.builder_versions"}; !reflect.DeepEqual(m.Invokes, want) {
|
||||
t.Errorf("invokes = %v, want %v", m.Invokes, want)
|
||||
}
|
||||
@@ -416,7 +416,7 @@ func TestEveryGiteaToolTheCodeCallsIsInInvokes(t *testing.T) {
|
||||
if err != nil || len(files) == 0 {
|
||||
t.Fatalf("no program files: %v", err)
|
||||
}
|
||||
verbs := map[string]string{"VerbTrees": git.VerbTrees, "VerbFile": git.VerbFile, "VerbContains": git.VerbContains, "VerbOpenPulls": git.VerbOpenPulls, "VerbBranch": git.VerbBranch}
|
||||
verbs := map[string]string{"VerbTrees": git.VerbTrees, "VerbFile": git.VerbFile, "VerbContains": git.VerbContains, "VerbOpenPulls": git.VerbOpenPulls, "VerbBranch": git.VerbBranch, "VerbTags": git.VerbTags}
|
||||
forge, asked := regexp.MustCompile(`gitea\.gitea_[a-z_]+`), regexp.MustCompile(`git\.Verb[A-Za-z]+`)
|
||||
seen := 0
|
||||
for _, f := range files {
|
||||
|
||||
@@ -167,6 +167,9 @@
|
||||
],
|
||||
"invokes": [
|
||||
"seat:git.trees",
|
||||
"seat:git.tags",
|
||||
"seat:go-package-registry.versions",
|
||||
"seat:npm-package-registry.versions",
|
||||
"seat:git.file",
|
||||
"seat:git.contains",
|
||||
"seat:git.open-pulls",
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
// Package publishconformance holds the cases every holder of a registry seat's `publish` is held to, so that two holders of
|
||||
// one contract cannot each read it their own way (novox/hq design 47 G A-73, A-417, A-490): a publish with a version, a
|
||||
// digest and no parts is the builder's publish again. A holder holding what the digest names under the name says it published
|
||||
// (it moves nothing, or, where the bytes sit in the artifact store, as distribution's A-417 takes them, tags them); a holder
|
||||
// that holds none of it, or holds the version under another digest, refuses it as not stored, naming the digest, and says it
|
||||
// refused (A-490, A-490a). The cases live here beside the build modules' conformance kit (design 47 G §680, A-490a). A
|
||||
// holder's own test runs Run over its own test bed, for every seat it fills.
|
||||
package publishconformance
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Name, Version and Digest are what the cases publish.
|
||||
const (
|
||||
Name = "novox/example"
|
||||
Version = "0.1.0"
|
||||
Digest = "sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
// Other is a digest the holder holds under the version in the case where it holds another; it does not contain Digest.
|
||||
Other = "sha256:fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
|
||||
)
|
||||
|
||||
// Outcome is what a holder did with a publish: whether the transfer ended with it, how many parts it asked for, the event
|
||||
// it said on its seat (published or refused) with its reason, and the error it answered.
|
||||
type Outcome struct {
|
||||
Ended bool
|
||||
Lacks int
|
||||
Event string
|
||||
Reason string
|
||||
Err error
|
||||
}
|
||||
|
||||
// Bed is one holder under test, fresh for a case.
|
||||
type Bed struct {
|
||||
// Hold makes the holder hold the digest under Name at Version, as the builder's reservation of Version would find it.
|
||||
Hold func(digest string)
|
||||
// Publish sends the publish of name at version with the digest and no parts, and reports what the holder did.
|
||||
Publish func(name, version, digest string) Outcome
|
||||
}
|
||||
|
||||
// Run runs the cases over beds made by fresh.
|
||||
func Run(t *testing.T, fresh func(t *testing.T) Bed) {
|
||||
t.Helper()
|
||||
t.Run("a publish with a digest and no parts is published when the holder holds it", func(t *testing.T) {
|
||||
b := fresh(t)
|
||||
b.Hold(Digest)
|
||||
o := b.Publish(Name, Version, Digest)
|
||||
if o.Err != nil || !o.Ended || o.Lacks != 0 || o.Event != "published" {
|
||||
t.Fatalf("a held digest answered %+v; want ended, no part lacked, published said", o)
|
||||
}
|
||||
})
|
||||
t.Run("a publish with a digest and no parts is refused as not stored, naming the digest, when the holder holds none", func(t *testing.T) {
|
||||
b := fresh(t)
|
||||
o := b.Publish(Name, Version, Digest)
|
||||
if o.Err == nil || !strings.Contains(o.Err.Error(), "not stored") || !strings.Contains(o.Err.Error(), Digest) {
|
||||
t.Fatalf("a digest not held answered %+v; want the error to say not stored and name %s", o, Digest)
|
||||
}
|
||||
if o.Event != "refused" || !strings.Contains(o.Reason, "not stored") || !strings.Contains(o.Reason, Digest) || o.Lacks != 0 || !o.Ended {
|
||||
t.Errorf("a digest not held answered %+v; want refused said with the digest in its reason, ended, no part lacked", o)
|
||||
}
|
||||
})
|
||||
t.Run("a publish with a digest and no parts is refused as not stored, naming the digest, when the holder holds the version under another digest", func(t *testing.T) {
|
||||
b := fresh(t)
|
||||
b.Hold(Other)
|
||||
o := b.Publish(Name, Version, Digest)
|
||||
if o.Err == nil || !strings.Contains(o.Err.Error(), "not stored") || !strings.Contains(o.Err.Error(), Digest) {
|
||||
t.Fatalf("another digest held answered %+v; want the error to say not stored and name %s", o, Digest)
|
||||
}
|
||||
if o.Event != "refused" || !strings.Contains(o.Reason, "not stored") || !strings.Contains(o.Reason, Digest) || o.Lacks != 0 || !o.Ended {
|
||||
t.Errorf("another digest held answered %+v; want refused said as not stored with the digest, ended, no part lacked", o)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// What the builder's reservation holds of the artifact a case publishes.
|
||||
type Reservation int
|
||||
|
||||
const (
|
||||
// NoDigestYet is an artifact whose build has not recorded its digest (the build runs, design 47 G §369, A-415).
|
||||
NoDigestYet Reservation = iota + 1
|
||||
// SameDigest is the digest of the file the case publishes.
|
||||
SameDigest
|
||||
// OtherDigest is another digest.
|
||||
OtherDigest
|
||||
)
|
||||
|
||||
// ReservedBed is one holder under test for the one-part publish a build module makes while it builds, fresh for a case. The
|
||||
// holder's check asks the builder's reservation through its real Reserved check; the bed answers as the builder's verb `reserved`
|
||||
// does.
|
||||
type ReservedBed struct {
|
||||
// Reserve makes the builder hold the case's name at its version as reserved, its artifact as r says. A case that never calls
|
||||
// it has the builder hold no reservation.
|
||||
Reserve func(r Reservation)
|
||||
// Hold makes the holder hold the case's name at its version under another digest than the file's, as the first publish of it
|
||||
// stored (A-490, A-490j): a second publish with another digest is refused as the holder holds the first.
|
||||
Hold func()
|
||||
// PublishWhole sends the one-part publish of the case's file (its own digest) and reports what the holder did.
|
||||
PublishWhole func() Outcome
|
||||
}
|
||||
|
||||
// RunReserved runs the cases of a publish made while the build runs, over beds made by fresh (design 47 G A-490j): the builder
|
||||
// records a build's digests only when it ends, so a reserved artifact naming no digest yet takes the publish while the holder's own refusal of a version it holds under another digest stands. Package holders only: an image holder is no case of it (A-490j).
|
||||
func RunReserved(t *testing.T, fresh func(t *testing.T) ReservedBed) {
|
||||
t.Helper()
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
reserve Reservation // 0: nothing reserved
|
||||
held bool // the holder holds the version under another digest already
|
||||
ok bool
|
||||
words string
|
||||
}{
|
||||
{name: "reserved, no digest yet: published", reserve: NoDigestYet, ok: true},
|
||||
{name: "reserved with the digest: published", reserve: SameDigest, ok: true},
|
||||
{name: "reserved with no digest yet, the version held under another digest: refused, nothing published", reserve: NoDigestYet, held: true, words: "is published with"},
|
||||
{name: "reserved for another digest: refused, naming the reservation", reserve: OtherDigest, words: "did not reserve"},
|
||||
{name: "nothing reserved: refused", words: "did not"},
|
||||
} {
|
||||
t.Run("a one-part publish while the build runs, "+c.name, func(t *testing.T) {
|
||||
b := fresh(t)
|
||||
if c.reserve != 0 {
|
||||
b.Reserve(c.reserve)
|
||||
}
|
||||
if c.held {
|
||||
b.Hold()
|
||||
}
|
||||
o := b.PublishWhole()
|
||||
if c.held {
|
||||
// Refused whether it ends with an error or only with the event refused, and never said published.
|
||||
if o.Event != "refused" || !strings.Contains(o.Reason, c.words) {
|
||||
t.Fatalf("answered %+v; want refused with %q in its reason, and nothing published", o, c.words)
|
||||
}
|
||||
return
|
||||
}
|
||||
if c.ok {
|
||||
if o.Err != nil || o.Event != "published" {
|
||||
t.Fatalf("answered %+v; want no error and published said", o)
|
||||
}
|
||||
return
|
||||
}
|
||||
if o.Err == nil || o.Event == "published" || !strings.Contains(o.Err.Error()+o.Reason, c.words) {
|
||||
t.Fatalf("answered %+v; want refused, with %q in its words, and nothing published", o, c.words)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -107,16 +107,41 @@ func (p *Puller) refuseAsker(msg *Pulled, ask sdkstep.StepAsk, why string) {
|
||||
return
|
||||
}
|
||||
p.logf("the ask %s is refused on %s: %s", ask.ID, p.Node, why)
|
||||
p.sayAskEnded(msg, ask, sdkstep.OutcomeFailed, why)
|
||||
_ = msg.Ack() // A-489: ended and written, so it leaves the work queue
|
||||
}
|
||||
|
||||
// sayAskEnded says the end of an ask this holder ends without running it: its step-ended, an ask outside a pipeline as
|
||||
// sayEnded says it, a pipeline step as its own form (design 47 G §602), so the runner that asked hears it.
|
||||
func (p *Puller) sayAskEnded(msg *Pulled, ask sdkstep.StepAsk, outcome, said string) {
|
||||
if ask.Step == nil {
|
||||
p.sayEnded(msg, ask, sdkstep.OutcomeFailed, why, nil, "")
|
||||
} else if end, err := sdkstep.EndOf(ask, p.Node, sdkstep.OutcomeFailed); err == nil {
|
||||
p.sayEnded(msg, ask, outcome, said, nil, "")
|
||||
} else if end, err := sdkstep.EndOf(ask, p.Node, outcome); err == nil {
|
||||
// A pipeline step's end names its pipeline, step, commit and fingerprint (its own form, §602).
|
||||
end.Said, end.At, end.Asker = why, now().UTC(), askerOf(msg, ask)
|
||||
end.Said, end.At, end.Asker = said, now().UTC(), askerOf(msg, ask)
|
||||
if body, err := sdkstep.EncodeStepEnded(end); err == nil {
|
||||
p.say("step-ended", json.RawMessage(body), "step-ended."+ask.ID)
|
||||
}
|
||||
}
|
||||
_ = msg.Term()
|
||||
}
|
||||
|
||||
// PriorRuns is Puller.Prior over a steps state's read (a module's ReadSteps.Get): the Run of the step a key holds. Nil for no
|
||||
// read (a test's), so the bus's delivery count stands.
|
||||
func PriorRuns(get func(key string) (json.RawMessage, error)) func(string) int {
|
||||
if get == nil {
|
||||
return nil
|
||||
}
|
||||
return func(key string) int {
|
||||
raw, err := get(key)
|
||||
if err != nil || raw == nil {
|
||||
return 0
|
||||
}
|
||||
var s sdkstep.Step
|
||||
if json.Unmarshal(raw, &s) != nil {
|
||||
return 0
|
||||
}
|
||||
return s.Run
|
||||
}
|
||||
}
|
||||
|
||||
// giveBack hands an ask back to be judged again a little later, once the seat list says more. Why it cannot be judged is
|
||||
|
||||
@@ -65,7 +65,7 @@ func TestPullRefusesAKindItsAskerMayNotAsk(t *testing.T) {
|
||||
key, _ := sdkstep.AskStepKey(asker, "shanks", c.ask.ID)
|
||||
waitFor(t, "the step keyed "+key, func() bool { _, ok := st.get(key); return ok })
|
||||
case "refused":
|
||||
waitFor(t, "the ask ended", func() bool { _, termed, _ := src.Fates(); return len(termed) == 1 })
|
||||
waitFor(t, "the ask ended", func() bool { acked, _, _ := src.Fates(); return len(acked) == 1 }) // A-489: ended, written, acknowledged
|
||||
key, _ := sdkstep.AskStepKey(asker, "shanks", c.ask.ID)
|
||||
s, _ := st.get(key)
|
||||
if s.State != sdkstep.StepFailed || !strings.Contains(s.Reason, KindOf(c.ask)) || (strings.Contains(c.subject, "mesh-catalog") && !strings.Contains(s.Reason, asker)) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package steprun
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"path"
|
||||
@@ -56,6 +57,20 @@ type AskSteps struct {
|
||||
SayStep func(end sdkstep.StepEnded) error
|
||||
}
|
||||
|
||||
// A-489b (G-22-lead-answers-2.md): SaysItsEnds is nil when these steps can be asked a pipeline step and say its end: a node to name in the end and a SayStep to
|
||||
// publish it (design 47 G *Asked on a work queue, ended by an event*, §24). A build module whose steps cannot is refused where
|
||||
// it is built (conformance.StepEnds, which every build module's test runs, and the catalogue check failurekit), never
|
||||
// discovered live as a step ask refused with "says no step's end".
|
||||
func (s AskSteps) SaysItsEnds() error {
|
||||
switch {
|
||||
case s.SayStep == nil:
|
||||
return errors.New("its AskSteps has no SayStep, so a step ask is refused: set it to SayStepOn the seat's language and node")
|
||||
case s.Node == "":
|
||||
return errors.New("its AskSteps names no Node, so the end of a step ask names no node")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunAsk runs ask's job with steps and answers a sdkstep.BuildAnswer or a sdkstep.CheckAnswer.
|
||||
func RunAsk(ctx context.Context, steps AskSteps, ask sdkstep.StepAsk) (any, error) {
|
||||
if ask.Build != nil {
|
||||
@@ -150,7 +165,9 @@ func (s AskSteps) fetch(ctx context.Context, fetch *sdkstep.Fetch) (json.RawMess
|
||||
|
||||
// onToolchain is the toolchain this node builds with when the ask names one and this module reports its own, nil when
|
||||
// either is absent. A node on another toolchain than the ask names answers a Declining: it is not on the agreed one and
|
||||
// sits out, for a node that is to take the ask (novox/hq ADR 0584 clause 2).
|
||||
// sits out, for a node that is to take the ask (novox/hq ADR 0584 clause 2). The recipe is compared only when the ask names
|
||||
// one: the runner names a step ask's language and version alone, the recipe and the node's digest being the seat's to report,
|
||||
// not the runner's to know (design 47 G §416, A-104).
|
||||
func (s AskSteps) onToolchain(ctx context.Context, want *sdkstep.ToolchainUsed) (*sdkstep.ToolchainUsed, error) {
|
||||
if want == nil || s.Toolchain == nil {
|
||||
return nil, nil
|
||||
@@ -159,7 +176,7 @@ func (s AskSteps) onToolchain(ctx context.Context, want *sdkstep.ToolchainUsed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !sdkstep.SameToolchainVersion(tc.Version, want.Version) || tc.Recipe != want.Recipe {
|
||||
if !sdkstep.SameToolchainVersion(tc.Version, want.Version) || (want.Recipe != "" && tc.Recipe != want.Recipe) {
|
||||
return nil, &Declining{Lacks: []string{fmt.Sprintf("the agreed toolchain %s (recipe %s)", sdkstep.LanguageVersion(want.Version), want.Recipe)},
|
||||
Text: fmt.Sprintf("the ask is for %s (recipe %s) and this node builds with %s (recipe %s): it sits out, and the ask is left to a node on the agreed toolchain",
|
||||
sdkstep.LanguageVersion(want.Version), want.Recipe, sdkstep.LanguageVersion(tc.Version), tc.Recipe)}
|
||||
|
||||
@@ -512,3 +512,27 @@ func TestRunAskRunsAPipelineStepAndSaysItsEnd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G §416, A-104: the runner names a step ask's toolchain by language and version alone, so a node on that version
|
||||
// takes it whatever its recipe; an ask that names a recipe still declines a node on another.
|
||||
func TestAnAskNamingNoRecipeIsNotDeclinedForItsRecipe(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
want sdkstep.ToolchainUsed
|
||||
decline bool
|
||||
}{
|
||||
{"version, no recipe", sdkstep.ToolchainUsed{Version: "go1.26"}, false},
|
||||
{"the same recipe", sdkstep.ToolchainUsed{Version: "go1.26", Recipe: "3"}, false},
|
||||
{"another recipe", sdkstep.ToolchainUsed{Version: "go1.26", Recipe: "4"}, true},
|
||||
{"another version, no recipe", sdkstep.ToolchainUsed{Version: "go1.27"}, true},
|
||||
} {
|
||||
s := AskSteps{Toolchain: func(context.Context) (sdkstep.ToolchainUsed, error) {
|
||||
return sdkstep.ToolchainUsed{Version: "go1.26.2", Recipe: "3"}, nil
|
||||
}}
|
||||
_, err := s.onToolchain(context.Background(), &c.want)
|
||||
var dec *Declining
|
||||
if errors.As(err, &dec) != c.decline || (err != nil && !c.decline) {
|
||||
t.Errorf("%s: err %v, want declined %v", c.name, err, c.decline)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,10 @@ package conformance
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -277,3 +280,34 @@ func judgePassing(c PassingCase, failed Failed) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// A-489b: StepEnds is the case every build module runs on the steps it asks with: a pipeline step ask must be able to say its end
|
||||
// (steprun.AskSteps.SaysItsEnds), or the module would refuse every step ask the runner sends, as mesh-build-node did on its
|
||||
// first live pipeline (design 47 G §24). A catalogue check (failurekit) refuses a build module whose tests do not call it.
|
||||
func StepEnds(t *testing.T, steps steprun.AskSteps) {
|
||||
t.Helper()
|
||||
if err := steps.SaysItsEnds(); err != nil {
|
||||
t.Errorf("the build module's steps cannot say a step's end: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A-489c: RunsCounted is the case every build module runs on its puller (A-489a): the poison rule and a transport fault's run count the
|
||||
// runs the module's steps state says started, so the puller is built with Prior over the module's own read of that state. The
|
||||
// module builds its puller with read as its steps state's read; a step key it holds with a started run must count as that run.
|
||||
func RunsCounted(t *testing.T, puller func(read func(key string) (json.RawMessage, error)) *steprun.Puller) {
|
||||
t.Helper()
|
||||
if err := runsCounted(puller); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func runsCounted(puller func(read func(key string) (json.RawMessage, error)) *steprun.Puller) error {
|
||||
p := puller(func(string) (json.RawMessage, error) { return json.RawMessage(`{"run":2}`), nil })
|
||||
switch {
|
||||
case p == nil || p.Prior == nil:
|
||||
return errors.New("the build module's puller has no Prior: an ask given back would count toward poison")
|
||||
case p.Prior("any") != 2:
|
||||
return fmt.Errorf("Prior over a step key holding run 2 = %d, want 2: the puller does not read the module's steps state", p.Prior("any"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -181,3 +181,33 @@ func TestTheFailureContract(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A build module whose steps cannot say a step's end fails the case; one that can passes.
|
||||
func TestStepEndsRefusesStepsThatCannotSayTheirEnd(t *testing.T) {
|
||||
say := func(sdkstep.StepEnded) error { return nil }
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
s steprun.AskSteps
|
||||
bad bool
|
||||
}{{"no SayStep", steprun.AskSteps{Node: "n"}, true}, {"no node", steprun.AskSteps{SayStep: say}, true}, {"both", steprun.AskSteps{Node: "n", SayStep: say}, false}} {
|
||||
if (c.s.SaysItsEnds() != nil) != c.bad {
|
||||
t.Errorf("%s: SaysItsEnds = %v, want refused %v", c.name, c.s.SaysItsEnds(), c.bad)
|
||||
}
|
||||
}
|
||||
StepEnds(t, steprun.AskSteps{Node: "n", SayStep: say})
|
||||
}
|
||||
|
||||
// A puller built without Prior, or with one that does not read the module's steps state, fails the case.
|
||||
func TestRunsCountedRefusesAPullerThatDoesNotReadTheStepsState(t *testing.T) {
|
||||
wired := func(read func(string) (json.RawMessage, error)) *steprun.Puller {
|
||||
return &steprun.Puller{Prior: steprun.PriorRuns(read)}
|
||||
}
|
||||
if err := runsCounted(wired); err != nil {
|
||||
t.Errorf("a wired puller: %v", err)
|
||||
}
|
||||
for name, p := range map[string]*steprun.Puller{"none": {}, "constant": {Prior: func(string) int { return 0 }}} {
|
||||
if runsCounted(func(func(string) (json.RawMessage, error)) *steprun.Puller { return p }) == nil {
|
||||
t.Errorf("%s: the case passed a puller that does not count the state's runs", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ type Pulled struct {
|
||||
Subject string
|
||||
// Delivered is how many times the bus has handed it over, this time included.
|
||||
Delivered uint64
|
||||
// Sequence is its sequence in the stream (0 for a source that has none), which the journal names for an ask that cannot be read.
|
||||
Sequence uint64
|
||||
// Ack ends it: it is never delivered again. InProgress tells the bus it is still being worked, restarting its
|
||||
// acknowledgement wait. Nak hands it back to be delivered again at once (to this node or another). Term drops it.
|
||||
Ack, InProgress, Nak, Term func() error
|
||||
@@ -87,7 +89,11 @@ type Puller struct {
|
||||
KeepOutput func(object string, body []byte) error
|
||||
// Callers says which seats the build seat's definition lists as callers of an accepted kind (CallersFromDefinitions);
|
||||
// found is false while the seat list holds no such kind. Nil checks no named asker (design 47 G §543, A-163).
|
||||
Callers func(kind string) (callers []string, found bool)
|
||||
Callers func(kind string) (callers []string, found bool)
|
||||
// Prior says how many runs of the ask under a step key started before (the Run of the step the key holds, PriorRuns; 0 for
|
||||
// none), so the poison rule and the transport fault's run count only runs that started, never an ask given back before it
|
||||
// ran. Nil counts the bus's deliveries (a test's).
|
||||
Prior func(key string) int
|
||||
ProgressEvery time.Duration
|
||||
Logf func(format string, args ...any)
|
||||
// Load reads one key of the state the node's pause is kept in (nil and no error when it is absent). When set, Pull
|
||||
@@ -576,7 +582,8 @@ func (p *Puller) decline(msg *Pulled, ask sdkstep.StepAsk, lacks []string) {
|
||||
return
|
||||
}
|
||||
p.logf("the ask %s is declined on %s: it needs %s, and this node lacks %s", ask.ID, p.Node, strings.Join(ask.Needs, ", "), strings.Join(lacks, ", "))
|
||||
_ = msg.Term()
|
||||
p.sayAskEnded(msg, ask, sdkstep.OutcomeCouldNotRun, "this node lacks "+strings.Join(lacks, ", "))
|
||||
_ = msg.Ack() // A-489a: its end is written
|
||||
p.mu.Lock()
|
||||
if p.declined == nil {
|
||||
p.declined = map[string]bool{}
|
||||
@@ -598,7 +605,7 @@ func (p *Puller) declineRun(msg *Pulled, ask sdkstep.StepAsk, key string, step s
|
||||
return
|
||||
}
|
||||
p.logf("the ask %s is declined on %s: %s", ask.ID, p.Node, d.Text)
|
||||
_ = msg.Term()
|
||||
_ = msg.Ack() // A-489a: its end is written (run() says it)
|
||||
p.mu.Lock()
|
||||
if p.declined == nil {
|
||||
p.declined = map[string]bool{}
|
||||
@@ -625,16 +632,18 @@ func (p *Puller) refuseUnread(msg *Pulled, why error) {
|
||||
step := sdkstep.Step{ID: probe.ID, Tool: tool, Node: p.Node, State: sdkstep.StepRefused, Started: at, Ended: at,
|
||||
Reason: fmt.Sprintf("this build module on %s cannot read the ask: %v", p.Node, why), Owner: buildrun.Owner}
|
||||
if werr := writeEnd(p.Put, key, step); werr != nil {
|
||||
p.logf("the unread ask %s cannot be answered on %s: %v", probe.ID, p.Node, werr)
|
||||
p.logf("the unread ask %s cannot be answered on %s, so it is given back: %v", probe.ID, p.Node, werr)
|
||||
_ = msg.Nak() // A-489a: an end that could not be written is given back, never acknowledged
|
||||
return
|
||||
} else {
|
||||
p.logf("an ask refused: %v", why)
|
||||
_ = msg.Term()
|
||||
_ = msg.Ack() // A-489: ended and written, so it leaves the work queue
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
p.logf("an ask dropped: %v", why)
|
||||
_ = msg.Term()
|
||||
p.logf("an ask dropped, no id to answer it by, acknowledged with this line as its only record (stream sequence %d): %v", msg.Sequence, why)
|
||||
_ = msg.Ack() // A-489a
|
||||
}
|
||||
|
||||
func withKind(msg *Pulled, kind string) *Pulled {
|
||||
@@ -644,6 +653,37 @@ func withKind(msg *Pulled, kind string) *Pulled {
|
||||
return msg
|
||||
}
|
||||
|
||||
// priorRuns is how many runs of the ask started before this delivery: its step key's (Puller.Prior), else the bus's delivery
|
||||
// count less this one. The bus's count also counts an ask given back before it ran, which is no run.
|
||||
func (p *Puller) priorRuns(msg *Pulled, ask sdkstep.StepAsk) int {
|
||||
if p.Prior != nil {
|
||||
if akey, kerr := sdkstep.StepKeyOf(keyAsker(msg), p.Node, ask.ID); kerr == nil {
|
||||
return p.Prior(akey)
|
||||
}
|
||||
}
|
||||
return int(msg.Delivered) - 1
|
||||
}
|
||||
|
||||
// endUnrun ends an ask that is run no more (A-489, A-489a): poison when runs of it started and none ended, out of its
|
||||
// deliveries when none did. Its step ends and its step-ended is said, and only then it is acknowledged; an end that could not
|
||||
// be written gives it back, never acknowledged.
|
||||
func (p *Puller) endUnrun(msg *Pulled, ask sdkstep.StepAsk, key string, step sdkstep.Step, prior int) {
|
||||
var why error
|
||||
if prior > 0 {
|
||||
why = fmt.Errorf("the ask was taken %d times and no run of it ended: each took its build node down or died with it, so it is run no more (poison)", prior)
|
||||
} else {
|
||||
why = fmt.Errorf("the ask is on its last delivery (%d) and no run of it started: it is out of its deliveries and run no more", msg.Delivered)
|
||||
}
|
||||
p.logf("the ask %s is ended on %s: %v", ask.ID, p.Node, why)
|
||||
if _, ferr := finish(p.Put, p.KeepOutput, key, step, nil, why, p.Logf); ferr != nil {
|
||||
p.logf("the end of the ask %s could not be written on %s, so it is given back: %v", ask.ID, p.Node, ferr)
|
||||
_ = msg.Nak()
|
||||
return
|
||||
}
|
||||
p.sayAskEnded(msg, ask, sdkstep.OutcomeCouldNotRun, why.Error())
|
||||
_ = msg.Ack()
|
||||
}
|
||||
|
||||
// take starts one ask: it decodes it, refuses one that is not of this seat, and runs it as a step under its own id.
|
||||
func (p *Puller) take(ctx context.Context, msg *Pulled) {
|
||||
kind := msg.Kind
|
||||
@@ -658,6 +698,18 @@ func (p *Puller) take(ctx context.Context, msg *Pulled) {
|
||||
p.refuseUnread(msg, err)
|
||||
return
|
||||
}
|
||||
if msg.Delivered >= sdkstep.AskMaxDeliver {
|
||||
// Its last delivery (A-489a): the bus gives it no more, so it is ended here, run or not, never given back.
|
||||
prior := p.priorRuns(msg, ask)
|
||||
step, key, err := beginRun(p.Put, keyAsker(msg), p.Node, ask.Kind+" ask", ask.ID, buildrun.AskWhat(askRead(ask)), prior+1)
|
||||
if err != nil {
|
||||
p.logf("the ask %s is on its last delivery and cannot start its end on %s: %v", ask.ID, p.Node, err)
|
||||
_ = msg.Nak()
|
||||
return
|
||||
}
|
||||
p.endUnrun(msg, ask, key, step, prior)
|
||||
return
|
||||
}
|
||||
if why, judged := p.askerRefusal(msg, ask); !judged {
|
||||
p.giveBack(msg, ask)
|
||||
return
|
||||
@@ -673,19 +725,15 @@ func (p *Puller) take(ctx context.Context, msg *Pulled) {
|
||||
p.decline(msg, ask, lacks)
|
||||
return
|
||||
}
|
||||
step, key, err := beginRun(p.Put, keyAsker(msg), p.Node, ask.Kind+" ask", ask.ID, buildrun.AskWhat(askRead(ask)), int(msg.Delivered))
|
||||
prior := p.priorRuns(msg, ask)
|
||||
step, key, err := beginRun(p.Put, keyAsker(msg), p.Node, ask.Kind+" ask", ask.ID, buildrun.AskWhat(askRead(ask)), prior+1)
|
||||
if err != nil {
|
||||
p.logf("the ask %s cannot start on %s: %v", ask.ID, p.Node, err)
|
||||
_ = msg.Nak()
|
||||
return
|
||||
}
|
||||
if msg.Delivered > sdkstep.AskRuns {
|
||||
// Run AskRuns times and never ended: each run took its node down or died with it. It is run no more, and said
|
||||
// (issue 1907): the asker's key fails with why.
|
||||
why := fmt.Errorf("the ask was taken %d times and no run of it ended: each took its build node down or died with it, so it is run no more (poison)", msg.Delivered-1)
|
||||
p.logf("the ask %s is ended on %s: %v", ask.ID, p.Node, why)
|
||||
_, _ = finish(p.Put, p.KeepOutput, key, step, nil, why, p.Logf) // run no more either way: the asker's bound says it then
|
||||
_ = msg.Term()
|
||||
if prior >= sdkstep.AskRuns {
|
||||
p.endUnrun(msg, ask, key, step, prior)
|
||||
return
|
||||
}
|
||||
module := ""
|
||||
@@ -768,14 +816,14 @@ func (p *Puller) run(ctx context.Context, msg *Pulled, ask sdkstep.StepAsk, key
|
||||
var dec *Declining
|
||||
if errors.As(err, &dec) && ctx.Err() == nil {
|
||||
p.declineRun(msg, ask, key, step, dec)
|
||||
p.sayEnded(msg, ask, sdkstep.OutcomeCouldNotRun, dec.Text, nil, "")
|
||||
p.sayAskEnded(msg, ask, sdkstep.OutcomeCouldNotRun, dec.Text)
|
||||
return
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
// The process stops: the ask is neither ended nor acknowledged here, so the stream gives it to a node that runs.
|
||||
return
|
||||
}
|
||||
if run := int(msg.Delivered); err != nil && sdkstep.IsPassing(err) && run < sdkstep.AskRuns && msg.NakAfter != nil {
|
||||
if run := step.Run; err != nil && sdkstep.IsPassing(err) && run < sdkstep.AskRuns && msg.NakAfter != nil {
|
||||
// A transport fault (ADR 0664 §1): the step says it is given again and the bus gives it after a delay, to this node or
|
||||
// another; it is not acknowledged, so its asker sees no end. Run AskRuns times it ends failed, said as a transport fault.
|
||||
delay := sdkstep.AgainAfter(run)
|
||||
|
||||
@@ -116,11 +116,11 @@ func (s *JetStreamAsks) fetch(ctx context.Context, c jetstream.Consumer, wait ti
|
||||
if !ok {
|
||||
return nil, batch.Error()
|
||||
}
|
||||
var delivered uint64 = 1
|
||||
var delivered, sequence uint64 = 1, 0
|
||||
if meta, err := msg.Metadata(); err == nil && meta != nil {
|
||||
delivered = meta.NumDelivered
|
||||
delivered, sequence = meta.NumDelivered, meta.Sequence.Stream
|
||||
}
|
||||
return &Pulled{Data: append([]byte(nil), msg.Data()...), Subject: msg.Subject(), Delivered: delivered,
|
||||
return &Pulled{Data: append([]byte(nil), msg.Data()...), Subject: msg.Subject(), Delivered: delivered, Sequence: sequence,
|
||||
Ack: msg.Ack, InProgress: msg.InProgress, Nak: msg.Nak, Term: msg.Term,
|
||||
NakAfter: func(delay time.Duration) error { return msg.NakWithDelay(delay) }}, nil
|
||||
case <-ctx.Done():
|
||||
@@ -146,12 +146,12 @@ func DeclareAskWorkers(ctx context.Context, js jetstream.JetStream, language str
|
||||
subject, _ := sdkstep.AskSubject(language, kind)
|
||||
// Two workers whose filters do not overlap: main's subject and the asker's, `<main's>.<asker>` (design 47 G §544).
|
||||
if _, err := js.CreateOrUpdateConsumer(ctx, stream, jetstream.ConsumerConfig{Durable: worker, FilterSubjects: []string{subject},
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: 5}); err != nil {
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: sdkstep.AskMaxDeliver}); err != nil {
|
||||
return fmt.Errorf("the worker %s: %w", worker, err)
|
||||
}
|
||||
named, _ := sdkstep.AskNamedWorker(language, kind)
|
||||
if _, err := js.CreateOrUpdateConsumer(ctx, stream, jetstream.ConsumerConfig{Durable: named, FilterSubjects: []string{subject + ".*"},
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: 5}); err != nil {
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: sdkstep.AskMaxDeliver}); err != nil {
|
||||
return fmt.Errorf("the worker %s: %w", named, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,8 @@ func (p *Puller) refuseDropped(msg *Pulled, ask sdkstep.StepAsk, d *nodebuildgo.
|
||||
return
|
||||
}
|
||||
p.logf("the ask %s is refused on %s: it was dropped (%s)", ask.ID, p.Node, d.Why)
|
||||
_ = msg.Term()
|
||||
p.sayAskEnded(msg, ask, sdkstep.OutcomeCouldNotRun, "it was dropped: "+d.Why)
|
||||
_ = msg.Ack() // A-489a: its end is written
|
||||
p.forgetDrop(ask.ID)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
package steprun
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
)
|
||||
|
||||
// Design 47 G *Asked on a work queue, ended by an event* (§24): the bus counts an ask given back before it ran (a seat list
|
||||
// not read yet) as a delivery, and that is no run. Only runs that started count toward the poison rule (Puller.Prior, from the
|
||||
// step key), and a poisoned pipeline step is ended by its step-ended event, so the runner that asked hears it.
|
||||
func TestOnlyRunsThatStartedCountTowardPoison(t *testing.T) {
|
||||
stepAsk := func() sdkstep.StepAsk {
|
||||
return sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskStep, Language: sdkstep.AskGo, Asker: "mesh-delivery-pipeline", At: time.Now(),
|
||||
Step: &sdkstep.StepRun{Pipeline: "main.12.m", Step: sdkstep.StepLint, Module: "m", Artifact: "m:program", Commit: strings.Repeat("a", 40),
|
||||
Fingerprint: "fp-1", Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: strings.Repeat("a", 40), Dir: "modules/m"}}}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
delivered uint64
|
||||
prior int
|
||||
ends string // what its end says; "" when it must run
|
||||
}{
|
||||
{"given back three times, never run", sdkstep.AskRuns + 1, 0, ""},
|
||||
{"three runs started and none ended", 3, sdkstep.AskRuns, "poison"},
|
||||
{"three runs started, delivered many times", sdkstep.AskRuns + 4, sdkstep.AskRuns, "poison"},
|
||||
{"its last delivery, given back before and never run", sdkstep.AskMaxDeliver, 0, "out of its deliveries"},
|
||||
{"its last delivery after a run that started", sdkstep.AskMaxDeliver, 1, "poison"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
src, st := NewFakeAsks(), newSteps()
|
||||
src.Delivered = c.delivered
|
||||
a := stepAsk()
|
||||
raw, err := sdkstep.EncodeStepAsk(a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src.Publish(sdkstep.AskCheck, raw) // a pipeline step rides the check queue
|
||||
var mu sync.Mutex
|
||||
var ended []sdkstep.StepEnded
|
||||
ran := make(chan struct{}, 1)
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Logf: t.Logf, Node: "shanks", Put: st.put, Capacity: func() int { return 1 },
|
||||
PullWait: 10 * time.Millisecond, Heartbeat: time.Hour, Prior: func(string) int { return c.prior },
|
||||
Say: func(event string, body any, id string) error {
|
||||
if event == "step-ended" {
|
||||
var e sdkstep.StepEnded
|
||||
raw, _ := json.Marshal(body)
|
||||
if err := json.Unmarshal(raw, &e); err == nil {
|
||||
mu.Lock()
|
||||
ended = append(ended, e)
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) { ran <- struct{}{}; return nil, nil }}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
if c.ends != "" {
|
||||
waitFor(t, "the poisoned ask's end said", func() bool { mu.Lock(); defer mu.Unlock(); return len(ended) == 1 })
|
||||
mu.Lock()
|
||||
e := ended[0]
|
||||
mu.Unlock()
|
||||
if e.Outcome != sdkstep.OutcomeCouldNotRun || !strings.Contains(e.Said, c.ends) || e.Pipeline != "main.12.m" {
|
||||
t.Fatalf("the end said %+v, want could not run, poison, naming its pipeline", e)
|
||||
}
|
||||
if acked, termed, naked := src.Fates(); len(acked) != 1 || len(termed)+len(naked) != 0 {
|
||||
t.Errorf("acked %v termed %v naked %v, want it acknowledged once its end is written (A-489a)", acked, termed, naked)
|
||||
}
|
||||
select {
|
||||
case <-ran:
|
||||
t.Fatal("an ended ask ran")
|
||||
default:
|
||||
}
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ran:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("an ask only given back was ended as poison, and never ran")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A-489a: an end that could not be written is given back, never acknowledged: the ask's start is written, its end is not
|
||||
// (poison), and an unreadable ask with an id whose refusal cannot be written.
|
||||
func TestAnEndThatCannotBeWrittenIsGivenBack(t *testing.T) {
|
||||
was := EndAttempts
|
||||
EndAttempts = 1
|
||||
defer func() { EndAttempts = was }()
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
data func() []byte
|
||||
prior int
|
||||
}{
|
||||
{"poison", func() []byte { raw, _ := sdkstep.EncodeStepAsk(testAsk(sdkstep.AskDelivery)); return raw }, sdkstep.AskRuns},
|
||||
{"unread with an id", func() []byte { return []byte(`{"id":"mwn1abcdef0123","kind":"check","v":99}`) }, 0},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
src := NewFakeAsks()
|
||||
src.Publish(sdkstep.AskCheck, c.data())
|
||||
if c.prior > 0 { // a delivery of the delivery queue
|
||||
src = NewFakeAsks()
|
||||
src.Publish(sdkstep.AskDelivery, c.data())
|
||||
}
|
||||
var mu sync.Mutex
|
||||
puts := 0
|
||||
put := func(string, any) (uint64, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
puts++
|
||||
if puts > 1 || c.prior == 0 { // the start is written, the end is not
|
||||
return 0, errors.New("the state is full")
|
||||
}
|
||||
return 1, nil
|
||||
}
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond,
|
||||
Prior: func(string) int { return c.prior }, Run: func(context.Context, sdkstep.StepAsk) (any, error) { return nil, nil }}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "the ask given back", func() bool { _, _, nk := src.Fates(); return len(nk) >= 1 })
|
||||
if acked, termed, _ := src.Fates(); len(acked)+len(termed) != 0 {
|
||||
t.Errorf("acked %v termed %v, want none: no end was written", acked, termed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A-489a: a decline at the take (a need the node lacks), a refused run (Declining) and a dropped ask each say their pipeline step's end as could not
|
||||
// run, naming its pipeline, and are acknowledged once it is written.
|
||||
func TestAnEndedPipelineStepIsSaidAndAcknowledged(t *testing.T) {
|
||||
for _, c := range []string{"declined", "declined in its run", "dropped"} {
|
||||
t.Run(c, func(t *testing.T) {
|
||||
src, st := NewFakeAsks(), newSteps()
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskStep, Language: sdkstep.AskGo, Asker: "mesh-delivery-pipeline", At: time.Now(),
|
||||
Step: &sdkstep.StepRun{Pipeline: "main.12.m", Step: sdkstep.StepLint, Module: "m", Artifact: "m:program", Commit: strings.Repeat("a", 40),
|
||||
Fingerprint: "fp-1", Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: strings.Repeat("a", 40), Dir: "modules/m"}}}
|
||||
raw, eerr := sdkstep.EncodeStepAsk(a)
|
||||
if eerr != nil {
|
||||
t.Fatal(eerr)
|
||||
}
|
||||
src.Publish(sdkstep.AskCheck, raw)
|
||||
var mu sync.Mutex
|
||||
var ended []sdkstep.StepEnded
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond,
|
||||
Say: func(event string, body any, id string) error {
|
||||
if event == "step-ended" {
|
||||
var e sdkstep.StepEnded
|
||||
b, _ := json.Marshal(body)
|
||||
if json.Unmarshal(b, &e) == nil {
|
||||
mu.Lock()
|
||||
ended = append(ended, e)
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Run: func(context.Context, sdkstep.StepAsk) (any, error) {
|
||||
return nil, &Declining{Text: "this node builds with another toolchain", Lacks: []string{"the toolchain"}}
|
||||
}}
|
||||
if c == "dropped" {
|
||||
drop, _ := json.Marshal(nodebuildgo.Drop{V: int64(sdkstep.Version), By: "mesh-delivery-builder", Why: "superseded", At: ptr(time.Now())})
|
||||
p.Load = func(key string) ([]byte, error) {
|
||||
if key == sdkstep.DropKey(a.ID) {
|
||||
return drop, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
if c == "declined" {
|
||||
// The decoder refuses needs on a pipeline step ask, so no ask of the queue reaches the decline; the line is held
|
||||
// by calling it with the ask built whole.
|
||||
acked := 0
|
||||
p.decline(&Pulled{Ack: func() error { acked++; return nil }, Term: func() error { return nil }, Nak: func() error { return nil }}, a, []string{"node-build-node"})
|
||||
if acked != 1 {
|
||||
t.Errorf("acked %d times, want once", acked)
|
||||
}
|
||||
src.Acked = append(src.Acked, "declined")
|
||||
} else {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
}
|
||||
waitFor(t, "the end said", func() bool { mu.Lock(); defer mu.Unlock(); return len(ended) >= 1 })
|
||||
waitFor(t, "the ask acknowledged", func() bool { ak, _, _ := src.Fates(); return len(ak) == 1 })
|
||||
mu.Lock()
|
||||
e := ended[0]
|
||||
mu.Unlock()
|
||||
if e.Outcome != sdkstep.OutcomeCouldNotRun || e.Pipeline != "main.12.m" {
|
||||
t.Errorf("the end said %+v, want could not run naming its pipeline", e)
|
||||
}
|
||||
if _, termed, naked := src.Fates(); len(termed)+len(naked) != 0 {
|
||||
t.Errorf("termed %v naked %v, want it acknowledged alone", termed, naked)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ADR 0664 §1 with the bus's count above the runs that started: the ask was given back twice before its first run, so the
|
||||
// bus says delivered 3 while the step key says 1 run began; a transport fault is run 2 (given again after 4 minutes), not 3.
|
||||
func TestATransportFaultCountsRunsThatStartedNotDeliveries(t *testing.T) {
|
||||
src, st := NewFakeAsks(), newSteps()
|
||||
src.Delivered = 3
|
||||
a := testAsk(sdkstep.AskDelivery)
|
||||
_ = src.PublishAsk(a)
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond,
|
||||
Prior: func(string) int { return 1 },
|
||||
Run: func(context.Context, sdkstep.StepAsk) (any, error) {
|
||||
return nil, &sdkstep.Passing{Err: errors.New("lookup proxy: i/o timeout")}
|
||||
}}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "the ask given again", func() bool { src.mu.Lock(); defer src.mu.Unlock(); return len(src.Delayed) == 1 })
|
||||
key, _ := sdkstep.AskStepKey("", "shanks", a.ID)
|
||||
s, _ := st.get(key)
|
||||
src.mu.Lock()
|
||||
delays := append([]time.Duration(nil), src.Delays...)
|
||||
src.mu.Unlock()
|
||||
if s.State != sdkstep.StepAgain || s.Run != 2 || len(delays) != 1 || delays[0] != sdkstep.AgainAfter(2) {
|
||||
t.Errorf("step %+v, delays %v, want again in run 2 after %s", s, delays, sdkstep.AgainAfter(2))
|
||||
}
|
||||
}
|
||||
|
||||
// Puller.Prior over a steps state's read: the Run of the step the key holds, 0 for an absent key, a failed read or what is no
|
||||
// step, and nil for no read.
|
||||
func TestPriorRunsReadsTheRunOfTheStepAKeyHolds(t *testing.T) {
|
||||
read := func(key string) (json.RawMessage, error) {
|
||||
switch key {
|
||||
case "running":
|
||||
return json.RawMessage(`{"id":"x","run":2}`), nil
|
||||
case "junk":
|
||||
return json.RawMessage(`[`), nil
|
||||
case "broken":
|
||||
return nil, errors.New("no bus")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
prior := PriorRuns(read)
|
||||
for key, want := range map[string]int{"running": 2, "absent": 0, "junk": 0, "broken": 0} {
|
||||
if got := prior(key); got != want {
|
||||
t.Errorf("PriorRuns(%s) = %d, want %d", key, got, want)
|
||||
}
|
||||
}
|
||||
if PriorRuns(nil) != nil {
|
||||
t.Error("PriorRuns(nil) is not nil")
|
||||
}
|
||||
}
|
||||
@@ -244,7 +244,7 @@ func TestPullDropsWhatIsNoAsk(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "three termed", func() bool { _, tm, _ := src.Fates(); return len(tm) == 3 })
|
||||
waitFor(t, "three acknowledged", func() bool { ak, _, _ := src.Fates(); return len(ak) == 3 })
|
||||
if ran {
|
||||
t.Fatal("a wrong or a declined ask ran")
|
||||
}
|
||||
@@ -269,7 +269,7 @@ func TestPullDropsWhatIsNoAsk(t *testing.T) {
|
||||
return nil, &Declining{Text: "the ask is for 26.13.0 and this node builds with 26.12.1", Lacks: []string{"the agreed toolchain 26.13.0 (recipe 3)"}}
|
||||
}}
|
||||
go s2.Pull(ctx)
|
||||
waitFor(t, "the ask termed on the node that sits out", func() bool { _, tm, _ := sit.Fates(); return len(tm) == 1 })
|
||||
waitFor(t, "the ask acknowledged on the node that sits out", func() bool { ak, _, _ := sit.Fates(); return len(ak) == 1 })
|
||||
sk, _ := sdkstep.AskStepKey("", "ace", toolchain.ID)
|
||||
ss, _ := sitSteps.get(sk)
|
||||
if d, ok := sdkstep.DeclinedOf(ss); !ok || len(d.Lacks) != 1 || !strings.Contains(ss.Reason, "26.12.1") {
|
||||
@@ -538,14 +538,14 @@ func TestAPoisonedAskIsSaidAndRunNoMore(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "the ask termed", func() bool { _, tm, _ := src.Fates(); return len(tm) == 1 })
|
||||
waitFor(t, "the ask acknowledged", func() bool { ak, _, _ := src.Fates(); return len(ak) == 1 })
|
||||
key, _ := sdkstep.AskStepKey("", "shanks", a.ID)
|
||||
s, ok := st.get(key)
|
||||
if ran || !ok || s.State != sdkstep.StepFailed || !strings.Contains(s.Reason, "taken 3 times") || !strings.Contains(s.Reason, "poison") {
|
||||
t.Fatalf("ran %v, step %+v", ran, s)
|
||||
}
|
||||
if acked, _, naked := src.Fates(); len(acked)+len(naked) != 0 {
|
||||
t.Errorf("acked %v naked %v, want it termed alone", acked, naked)
|
||||
if _, termed, naked := src.Fates(); len(termed)+len(naked) != 0 { // A-489: its end written, it leaves the queue by ack
|
||||
t.Errorf("termed %v naked %v, want it acknowledged alone", termed, naked)
|
||||
}
|
||||
// ADR 0664 §1 (issue 1748): a run that ends on a transport fault below AskRuns is written again, with its run and when the
|
||||
// bus gives it, and handed back with that delay, not acknowledged; the AskRuns-th ends failed, passing; a fault of the
|
||||
@@ -620,7 +620,7 @@ func TestPullAnswersAnAskItCannotRead(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "both termed", func() bool { _, tm, _ := src.Fates(); return len(tm) == 2 })
|
||||
waitFor(t, "both acknowledged", func() bool { ak, _, _ := src.Fates(); return len(ak) == 2 })
|
||||
key, _ := sdkstep.AskStepKey("", "ace", good.ID)
|
||||
s, ok := st.get(key)
|
||||
if !ok || s.State != sdkstep.StepRefused || !strings.Contains(s.Reason, "cannot read the ask") || !strings.Contains(s.Reason, "exactly one") {
|
||||
|
||||
Reference in New Issue
Block a user