The bound's end (ended) raised version-unreleased for a built reservation whose every artifact was published, saying "an artifact is unpublished", while arm past the bound leaves that reservation waiting for its version-released. 1984/AC5 raises only for a built reservation left unmarked past its bound, so ended now raises nothing when no artifact is missing. Builds: issue 1984 note 8 (1984/AC5), design 47 G §369
997 lines
51 KiB
Go
997 lines
51 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
|
"slices"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
"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"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// memReserve is the state reserved in memory.
|
|
type memReserve struct {
|
|
vals map[string][]byte
|
|
revs map[string]uint64
|
|
lists int // how often its keys were listed
|
|
}
|
|
|
|
func (m *memReserve) Get(_ context.Context, key string) ([]byte, uint64, bool, error) {
|
|
v, ok := m.vals[key]
|
|
return v, m.revs[key], ok, nil
|
|
}
|
|
func (m *memReserve) Create(_ context.Context, key string, v []byte) (uint64, error) {
|
|
if _, ok := m.vals[key]; ok {
|
|
return 0, versions.ErrConflict
|
|
}
|
|
m.vals[key], m.revs[key] = v, 1
|
|
return 1, nil
|
|
}
|
|
func (m *memReserve) Update(_ context.Context, key string, v []byte, rev uint64) (uint64, error) {
|
|
if m.revs[key] != rev {
|
|
return 0, versions.ErrConflict
|
|
}
|
|
m.vals[key], m.revs[key] = v, rev+1
|
|
return rev + 1, nil
|
|
}
|
|
func (m *memReserve) KeysOf(_ context.Context, filter string) ([]string, error) {
|
|
m.lists++
|
|
var out []string
|
|
for k := range m.vals {
|
|
if versions.FilterMatches(filter, k) { // as the bus reads a filter
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
func (m *memReserve) Delete(_ context.Context, key string) error {
|
|
delete(m.vals, key)
|
|
return nil
|
|
}
|
|
|
|
// boundsKV keeps the keys created and deleted in the state bounds, and with vals each created key's value.
|
|
type boundsKV struct {
|
|
jetstream.KeyValue
|
|
keys map[string]bool
|
|
vals map[string][]byte
|
|
}
|
|
|
|
func (b *boundsKV) Create(_ context.Context, key string, v []byte, _ ...jetstream.KVCreateOpt) (uint64, error) {
|
|
b.keys[key] = true
|
|
if b.vals != nil {
|
|
b.vals[key] = v
|
|
}
|
|
return 1, nil
|
|
}
|
|
func (b *boundsKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOpt) error {
|
|
delete(b.keys, key)
|
|
return nil
|
|
}
|
|
|
|
// TestTheCatalogsWordsMoveTheReservations: design 47 G §369 (test 102) — a built reservation arms its bound and its end raises version-unreleased naming the artifacts
|
|
// missing; the catalog's version-released deletes the key and the bound and clears the finding; builder_reserved_drop drops one.
|
|
func TestTheCatalogsWordsMoveTheReservations(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{}}
|
|
call := func(addr string, body any) (json.RawMessage, error) {
|
|
if addr != "seat:mesh-catalog.versions" {
|
|
t.Errorf("the released versions were asked of %q", addr)
|
|
}
|
|
return json.Marshal(meshcatalog.VersionsAnswer{Module: "m", Versions: []meshcatalog.VersionEntry{{Module: "m", Version: "1.4.2"}}})
|
|
}
|
|
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, call, func() jetstream.KeyValue { return bounds }, found, 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) }
|
|
var said []meshbuilds.PackagePublished
|
|
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { said = append(said, p); return nil }
|
|
res, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a", "m:b"}, versions.Source{Branch: "main", Revision: 41})
|
|
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)
|
|
}
|
|
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)
|
|
}
|
|
// 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] {
|
|
t.Fatalf("the publish's bound is not armed: %v", bounds.keys)
|
|
}
|
|
rv.published(ctx, versions.Outcome{Name: "m-a", Version: "1.4.3", Digest: "sha256:a"}, nil)
|
|
// Design 47 G §52, A-57: the holder's published is said once, per artifact, as the seat mesh-builds' package-published with the
|
|
// branch and source revision of the reservation; a second published of the same artifact says nothing.
|
|
rv.published(ctx, versions.Outcome{Name: "m-a", Version: "1.4.3", Digest: "sha256:a"}, nil)
|
|
if len(said) != 1 || said[0].Artifact != "m:a" || said[0].Version != "1.4.3" || said[0].Digest != "sha256:a" || said[0].Branch != "main" || said[0].Source != 41 {
|
|
t.Fatalf("package-published said %+v, want one of m:a at 1.4.3 for main at source 41", said)
|
|
}
|
|
if !rv.ended(ctx, purged{key: boundKey}) {
|
|
t.Fatal("a reservation's bound was not taken in")
|
|
}
|
|
key := meshconditionsclient.Key(meshconditionsclient.ScopeModule, "m", versions.VersionUnreleased)
|
|
got, err := decodeFinding(st.vals[key])
|
|
if err != nil || !strings.Contains(got.Summary, "m:b unpublished") || strings.Contains(got.Summary, "m:a unpublished") || !strings.Contains(got.Summary, "1.4.3") {
|
|
t.Fatalf("finding = %+v (%v), want version-unreleased naming m:b of 1.4.3", got, err)
|
|
}
|
|
released := &wakeMsg{subject: meshcatalog.SubjectVersionReleased, data: []byte(`{"v":1,"module":"m","version":"1.4.3","source":1,"artifacts":[]}`)}
|
|
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)
|
|
}
|
|
if _, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a"}, versions.Source{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if answer, err := rv.dropped(ctx, "m", "1.4", "op", "never released", time.Unix(0, 0)); err != nil || !strings.Contains(answer, "dropped") {
|
|
t.Errorf("builder_reserved_drop: %q (%v)", answer, err)
|
|
}
|
|
if _, err := rv.dropped(ctx, "m", "1.4", "op", "again", time.Unix(0, 0)); err == nil {
|
|
t.Error("a reservation that is gone was dropped again")
|
|
}
|
|
}
|
|
|
|
// TestAReleasedVersionWritesNoTag: design 47 H2 H-A-494 — the builder writes no git tag at a release, and numbers a series from the
|
|
// catalog's versions and the registries' alone: the catalog's version-released releases the reservation with no call of the seat
|
|
// git, a reservation written before (one naming a tag) included; the start's take-in of a version the catalog holds released
|
|
// (H-A-484 c) the same; and a reserve reads no git tag, a tag standing at another commit no reason to skip a number.
|
|
func TestAReleasedVersionWritesNoTag(t *testing.T) {
|
|
ctx := context.Background()
|
|
var gitCalls []string // every call of the seat git: none is made
|
|
var catalog []meshcatalog.VersionEntry // the released versions the catalog's verb versions answers
|
|
call := func(addr string, body any) (json.RawMessage, error) {
|
|
if strings.HasPrefix(addr, "seat:git.") {
|
|
gitCalls = append(gitCalls, addr)
|
|
if addr == "seat:git.tags" { // a tag standing at another commit, as the forge may still hold one
|
|
return json.Marshal(map[string]any{"form": 2, "tags": []git.Tag{{Tag: "go/v0.2.0", Commit: "elsewhere"}}})
|
|
}
|
|
return json.Marshal(git.TagAnswer{Performed: true})
|
|
}
|
|
return json.Marshal(meshcatalog.VersionsAnswer{Versions: catalog})
|
|
}
|
|
fw := &memFindings{vals: map[string][]byte{}}
|
|
found := &findings{module: builds.BuilderModule, w: fw, logf: t.Logf, now: func() time.Time { return time.Unix(0, 0) }}
|
|
store := &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}
|
|
rv := newReservations(store, call, func() jetstream.KeyValue { return &boundsKV{keys: map[string]bool{}} }, found, t.Logf)
|
|
rv.names = func(_ context.Context, _, artifact string) (string, bool, error) {
|
|
return "git.example/o/r/go", artifact == "lib", nil
|
|
}
|
|
goPkg := versions.Source{Repository: "o/r", Commit: "c0ffee", Fingerprint: "fpA"}
|
|
// oldRecord rewrites a reservation as one written before H-A-494, naming the tag its release was to be written as.
|
|
oldRecord := func(module, series, tag string) {
|
|
key := versions.ReservedKey(module, series)
|
|
var v versions.ReservedVersion
|
|
if err := json.Unmarshal(store.vals[key], &v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v.Tag = tag
|
|
store.vals[key], _ = json.Marshal(v)
|
|
}
|
|
release := func(module, version string) *wakeMsg {
|
|
m := &wakeMsg{subject: meshcatalog.SubjectVersionReleased, data: []byte(`{"v":1,"module":"` + module + `","version":"` + version + `","source":1,"artifacts":[]}`)}
|
|
rv.heard(ctx, m)
|
|
return m
|
|
}
|
|
// The release path: a Go package's version-released releases it, acknowledged, with no tag written.
|
|
if v, err := rv.Reserve(ctx, "pkg", "0.1", []string{"pkg:lib"}, goPkg); err != nil || v.Tag != "" {
|
|
t.Fatalf("reserved %+v (%v), want a reservation naming no tag", v, err)
|
|
}
|
|
oldRecord("pkg", "0.1", "go/v0.1.0")
|
|
if m := release("pkg", "0.1.0"); !m.acked || m.naked {
|
|
t.Fatalf("the release: acked %v naked %v; want it acknowledged", m.acked, m.naked)
|
|
}
|
|
if len(gitCalls) != 0 {
|
|
t.Errorf("the release called the seat git: %v; want no tag written", gitCalls)
|
|
gitCalls = nil
|
|
}
|
|
if all, _ := rv.r.All(ctx); len(all) != 0 {
|
|
t.Fatalf("the released version still stands: %v", all)
|
|
}
|
|
// Design 47 H2 H-A-514 addendum: a version-released of a version no reservation holds is acknowledged at once from its
|
|
// series' one key, the module's keys never listed.
|
|
if _, err := rv.Reserve(ctx, "late", "0.2", []string{"late:bin"}, versions.Source{Repository: "o/l", Commit: "c1", Fingerprint: "fpL"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store.lists = 0
|
|
if m := release("late", "0.2.7"); !m.acked || m.naked || store.lists != 0 {
|
|
t.Fatalf("a release no reservation holds: acked %v naked %v, the keys listed %d times; want it acknowledged with none", m.acked, m.naked, store.lists)
|
|
}
|
|
if _, standing, _ := rv.r.Of(ctx, "late", "0.2.0"); !standing {
|
|
t.Fatal("a release of another version took the standing reservation late 0.2.0")
|
|
}
|
|
if _, err := rv.r.Drop(ctx, "late", "0.2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The start check (H-A-484 c): a built reservation the catalog holds released is released at the start, no tag written; one
|
|
// not built, or not released, stands.
|
|
// Design 47 H2 H-A-492: at the start, a standing reservation with no digests recorded whose build key is done has them
|
|
// recorded (the builder's RecordAgain), and is then held against the released versions as a built one is.
|
|
if _, err := rv.Reserve(ctx, "rec", "0.1", []string{"rec:bin"}, versions.Source{Repository: "o/q", Commit: "abc", Fingerprint: "fpR"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var recordedAt []string
|
|
rv.recordAgain = func(ctx context.Context, module, fp, when string) (string, error) {
|
|
recordedAt = append(recordedAt, module+" "+fp+" "+when)
|
|
_, _, err := rv.r.Built(ctx, module, "0.1.0", map[string]string{"rec:bin": "sha256:bb"}, nil) // its done key's digest
|
|
return "0.1.0", err
|
|
}
|
|
catalog = []meshcatalog.VersionEntry{{Module: "rec", Version: "0.1.0"}}
|
|
rv.releasedAtStart(ctx)
|
|
if _, standing, _ := rv.r.Of(ctx, "rec", "0.1.0"); standing || !slices.Equal(recordedAt, []string{"rec fpR at the builder's start"}) {
|
|
t.Fatalf("at the start, rec 0.1.0 unrecorded and released: standing %v, recorded %q; want it recorded once at the start and released", standing, recordedAt)
|
|
}
|
|
catalog, rv.recordAgain = nil, nil
|
|
var ended []string
|
|
rv.released = func(_ context.Context, module, version string) { ended = append(ended, module+" "+version) }
|
|
if _, err := rv.Reserve(ctx, "pkg", "0.1", []string{"pkg:lib"}, versions.Source{Repository: "o/r", Commit: "b0b", Fingerprint: "fpB"}); err != nil {
|
|
t.Fatal(err) // 0.1.0 again: the stub catalog holds none released
|
|
}
|
|
oldRecord("pkg", "0.1", "go/v0.1.0")
|
|
rv.releasedAtStart(ctx) // built nothing yet: nothing taken
|
|
if all, _ := rv.r.All(ctx); len(all) != 1 {
|
|
t.Fatalf("at the start with no build done: reservations %v; want the reservation standing", all)
|
|
}
|
|
if _, _, err := rv.r.Built(ctx, "pkg", "0.1.0", map[string]string{"pkg:lib": "sha256:aa"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rv.releasedAtStart(ctx) // the catalog holds none released: it stands
|
|
if all, _ := rv.r.All(ctx); len(all) != 1 {
|
|
t.Fatalf("at the start with nothing released: reservations %v; want the reservation standing", all)
|
|
}
|
|
catalog = []meshcatalog.VersionEntry{{Module: "pkg", Version: "0.1.0"}}
|
|
rv.releasedAtStart(ctx)
|
|
if all, _ := rv.r.All(ctx); len(all) != 0 || !slices.Equal(ended, []string{"pkg 0.1.0"}) {
|
|
t.Fatalf("at the start with 0.1.0 released: reservations %v, items ended %v; want it released and its item ended", all, ended)
|
|
}
|
|
if len(gitCalls) != 0 {
|
|
t.Errorf("the start check called the seat git: %v; want no tag written", gitCalls)
|
|
gitCalls = nil
|
|
}
|
|
catalog, rv.released = nil, nil
|
|
// Numbering reads no tag: the seat git would answer go/v0.2.0 at another commit, and 0.2.0 is reserved all the same.
|
|
if v, err := rv.Reserve(ctx, "pkg", "0.2", []string{"pkg:lib"}, goPkg); err != nil || v.Version != "0.2.0" || v.Tag != "" || len(gitCalls) != 0 {
|
|
t.Errorf("reserved %+v (%v), git calls %v; want 0.2.0 naming no tag, no git tag read", v, err, gitCalls)
|
|
}
|
|
key := meshconditionsclient.Key(meshconditionsclient.ScopeModule, "pkg", versions.VersionTaggedElsewhere)
|
|
if fw.vals[key] != nil {
|
|
t.Errorf("a reserve raised %s", key)
|
|
}
|
|
// Design 47 H2 §805, H-A-420 (2): a package's number newly reserved is said as version-reserved, its path the package's name
|
|
// and its revision the indexer's `<branch>.<n>`; a reservation kept as it stood, an artifact that is no package and a
|
|
// reservation naming no catalog revision say none.
|
|
var said []meshbuilds.VersionReservedEvent
|
|
rv.names = func(_ context.Context, module, artifact string) (string, bool, error) {
|
|
return "git.example/o/r/" + artifact, module == "lib2" && artifact == "pkg", nil
|
|
}
|
|
rv.sayReserved = func(_ context.Context, ev meshbuilds.VersionReservedEvent) error { said = append(said, ev); return nil }
|
|
at := versions.Source{Repository: "o/r", Commit: "c1", Fingerprint: "src2:l2", Branch: "main", CatalogRevision: 158}
|
|
for range 2 {
|
|
if _, err := rv.Reserve(ctx, "lib2", "0.3", []string{"lib2:pkg", "lib2:bin"}, at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := rv.Reserve(ctx, "lib2", "0.4", []string{"lib2:pkg"}, versions.Source{Fingerprint: "src2:none"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := meshbuilds.VersionReservedEvent{Module: "lib2", Artifact: "lib2:pkg", Path: "git.example/o/r/pkg", Version: "0.3.0", Fingerprint: "src2:l2", Revision: "main.158"}
|
|
if len(said) != 1 || said[0] != want {
|
|
t.Errorf("version-reserved said %+v, want once %+v", said, want)
|
|
}
|
|
}
|
|
|
|
// Design 47 G A-415: a published heard before the build recorded its digests is kept on its reservation, and when the build
|
|
// records them it marks its artifact and package-published is said then; a kept one whose digest is none of the built digests
|
|
// 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: fs, logf: t.Logf, now: time.Now}, logf)
|
|
var said []meshbuilds.PackagePublished
|
|
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { said = append(said, p); return nil }
|
|
res, err := rv.Reserve(ctx, "m", "1.4", []string{"m:a", "m:b"}, versions.Source{Branch: "main", Revision: 41})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rv.published(ctx, versions.Outcome{Name: "m-a", Version: res.Version, Digest: "sha256:a"}, nil)
|
|
rv.published(ctx, versions.Outcome{Name: "m-x", Version: res.Version, Digest: "sha256:x"}, nil)
|
|
if len(said) != 0 {
|
|
t.Fatalf("package-published said before the digests are recorded: %+v", said)
|
|
}
|
|
if err := rv.Built(ctx, "m", res.Version, map[string]string{"m:a": "sha256:a", "m:b": "sha256:b"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(said) != 1 || said[0].Artifact != "m:a" || said[0].Version != res.Version || said[0].Digest != "sha256:a" || said[0].Branch != "main" || said[0].Source != 41 {
|
|
t.Fatalf("package-published said %+v, want one of m:a at %s for main at source 41", said, res.Version)
|
|
}
|
|
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"}, nil)
|
|
if len(said) != 1 {
|
|
t.Fatalf("package-published said again: %+v", said)
|
|
}
|
|
}
|
|
|
|
// TestABuildsDigestsAreRecordedInOneWrite: issue 1984 note 8, 1984/AC4 — recording a build's digests is one compare-and-set of
|
|
// its reservation and one reindex for all its artifacts (39 here, as mesh-delivery-contracts 0.1.108 had); a record tried again
|
|
// after one that timed out writes only what that one left (an artifact recorded already is skipped), and nothing when every
|
|
// digest stands recorded.
|
|
func TestABuildsDigestsAreRecordedInOneWrite(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &countedReserve{memReserve: &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, writes: map[string]int{}}
|
|
none := func(string, any) (json.RawMessage, error) { return json.Marshal(meshcatalog.VersionsAnswer{}) }
|
|
rv := newReservations(store, 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}, t.Logf)
|
|
var asked []string
|
|
rv.built = func(_ context.Context, m, v string) error { asked = append(asked, m+" "+v); return nil }
|
|
refsOf := func(module string) ([]string, map[string]string) {
|
|
var refs []string
|
|
digests := map[string]string{}
|
|
for i := range 39 {
|
|
ref := fmt.Sprintf("%s:a%02d", module, i)
|
|
refs, digests[ref] = append(refs, ref), fmt.Sprintf("sha256:%s%02d", module, i)
|
|
}
|
|
return refs, digests
|
|
}
|
|
built := func(module string) versions.ReservedVersion {
|
|
t.Helper()
|
|
v, found, err := rv.r.Of(ctx, module, "")
|
|
if err != nil || !found {
|
|
t.Fatalf("%s: no reservation (%v)", module, err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
refs, digests := refsOf("c")
|
|
res, err := rv.Reserve(ctx, "c", "0.1", refs, versions.Source{Fingerprint: "fpC"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := versions.ReservedKey("c", "0.1")
|
|
clear(store.writes)
|
|
store.indexReads = 0
|
|
if err := rv.Built(ctx, "c", res.Version, digests, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// One reindex reads each index key the built reservation stands under (its version and its 39 digests) and the one it leaves
|
|
// (its version's unbuilt): 41 reads, never one reindex per artifact.
|
|
if w, r := store.writes[key], store.indexReads; w != 1 || r > 41 {
|
|
t.Errorf("39 digests recorded with %d writes of %s and %d reads of the index, want one write and one reindex (at most 41 reads) (1984/AC4)", w, key, r)
|
|
}
|
|
if v := built("c"); v.BuiltAt.IsZero() || len(v.Missing()) != 39 || !slices.Equal(asked, []string{"c " + res.Version}) {
|
|
t.Errorf("after the record: built at %v, %d unpublished, publish items asked %v; want the build ended, 39 awaited and its item asked once", v.BuiltAt, len(v.Missing()), asked)
|
|
}
|
|
|
|
// A record that timed out left 30 of 39 digests written and the build not ended (the form the record of one artifact at a
|
|
// time wrote): the record tried again writes the reservation once, skipping the 30.
|
|
refs, digests = refsOf("d")
|
|
res, err = rv.Reserve(ctx, "d", "0.1", refs, versions.Source{Fingerprint: "fpD"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key = versions.ReservedKey("d", "0.1")
|
|
left := res
|
|
left.Artifacts = map[string]string{}
|
|
for i, ref := range refs {
|
|
left.Artifacts[ref] = map[bool]string{true: digests[ref], false: ""}[i < 30]
|
|
}
|
|
store.vals[key], _ = json.Marshal(left)
|
|
clear(store.writes)
|
|
if err := rv.Built(ctx, "d", res.Version, digests, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := store.writes[key]; w != 1 || built("d").BuiltAt.IsZero() {
|
|
t.Errorf("the record tried again wrote %s %d times (built at %v), want once, the 30 recorded skipped (1984/AC4)", key, w, built("d").BuiltAt)
|
|
}
|
|
// Tried again once more, every digest recorded: nothing is written.
|
|
clear(store.writes)
|
|
if err := rv.Built(ctx, "d", res.Version, digests, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := store.writes[key]; w != 0 {
|
|
t.Errorf("a record of digests all recorded wrote %s %d times, want none (1984/AC4)", key, w)
|
|
}
|
|
}
|
|
|
|
// TestABuiltReservationIsTakenInFromItsState: issue 1984 note 8, 1984/AC5 — what follows a build's record follows the
|
|
// reservation's state, not whether the record before ended: at a look (the builder's RecordAgain hands a recorded reservation to
|
|
// Built with its digests) and at the start, a built reservation with published kept on it takes them in, says package-published
|
|
// and arms its bound, counted from its build's end; one left unmarked past its bound raises version-unreleased, its bound not
|
|
// armed again, and one whose kept published mark every artifact raises nothing. A version-unreleased standing with a holder's
|
|
// words keeps them past the bound and at the bound's end.
|
|
func TestABuiltReservationIsTakenInFromItsState(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}
|
|
none := func(string, any) (json.RawMessage, error) { return json.Marshal(meshcatalog.VersionsAnswer{}) }
|
|
fs := &memFindings{vals: map[string][]byte{}}
|
|
bounds := &boundsKV{keys: map[string]bool{}, vals: map[string][]byte{}}
|
|
rv := newReservations(store, none, func() jetstream.KeyValue { return bounds },
|
|
&findings{module: builds.BuilderModule, w: fs, logf: t.Logf, now: time.Now}, t.Logf)
|
|
var said []string
|
|
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error {
|
|
said = append(said, p.Artifact+"@"+p.Version+" "+p.Digest)
|
|
return nil
|
|
}
|
|
var asked []string
|
|
rv.built = func(_ context.Context, m, v string) error { asked = append(asked, m+" "+v); return nil }
|
|
now := time.Now().UTC()
|
|
// recordedLost is module's reservation as a record whose end was lost leaves it: the published of the artifacts heard (a, b)
|
|
// heard before the digests kept on it, every digest recorded and the build ended at builtAt, and nothing after (no bound, the
|
|
// kept published not taken in).
|
|
recordedLost := func(module string, builtAt time.Time, heard ...string) versions.ReservedVersion {
|
|
t.Helper()
|
|
res, err := rv.Reserve(ctx, module, "0.1", []string{module + ":a", module + ":b"}, versions.Source{Fingerprint: "fp" + module, Branch: "main", Revision: 41})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, a := range heard {
|
|
rv.published(ctx, versions.Outcome{Name: module + "-" + a, Version: res.Version, Digest: "sha256:" + module + a}, nil)
|
|
}
|
|
v, _, _ := rv.r.Of(ctx, module, res.Version)
|
|
if len(v.Kept) != len(heard) {
|
|
t.Fatalf("%s: kept %v, want the published heard before the digests kept", module, v.Kept)
|
|
}
|
|
v.Artifacts = map[string]string{module + ":a": "sha256:" + module + "a", module + ":b": "sha256:" + module + "b"}
|
|
v.BuiltAt = builtAt
|
|
key := versions.ReservedKey(module, "0.1")
|
|
store.vals[key], _ = json.Marshal(v)
|
|
return v
|
|
}
|
|
unreleased := func(module string) string {
|
|
return string(fs.vals[meshconditionsclient.Key(meshconditionsclient.ScopeModule, module, versions.VersionUnreleased)])
|
|
}
|
|
boundEnds := func(module string) time.Time {
|
|
var b versions.Bound
|
|
_ = json.Unmarshal(bounds.vals[versions.ReservedBoundKey(module, "0.1")], &b)
|
|
return b.Ends
|
|
}
|
|
|
|
t.Run("at a look", func(t *testing.T) {
|
|
v := recordedLost("look", now.Add(-5*time.Minute), "a")
|
|
said, asked = nil, nil
|
|
if err := rv.Built(ctx, "look", v.Version, v.Artifacts, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Its publish item is asked as the build's end asks it: it stands until its release (design 47 G A-456b).
|
|
if !slices.Equal(said, []string{"look:a@" + v.Version + " sha256:looka"}) || !slices.Equal(asked, []string{"look " + v.Version}) {
|
|
t.Errorf("at a look: said %v, publish items asked %v; want look:a's package-published said and its item asked", said, asked)
|
|
}
|
|
if ends := boundEnds("look"); !ends.Equal(v.BuiltAt.Add(versions.UnreleasedBound)) {
|
|
t.Errorf("the bound armed at a look ends %v, want %v: counted from the build's end", ends, v.BuiltAt.Add(versions.UnreleasedBound))
|
|
}
|
|
if cur, _, _ := rv.r.Of(ctx, "look", v.Version); len(cur.Kept) != 0 || !cur.Published["look:a"] {
|
|
t.Errorf("after the look: kept %v, published %v; want the kept published taken in", cur.Kept, cur.Published)
|
|
}
|
|
})
|
|
t.Run("at the start", func(t *testing.T) {
|
|
v := recordedLost("start", now.Add(-time.Minute), "a")
|
|
said = nil
|
|
rv.releasedAtStart(ctx)
|
|
if !slices.Contains(said, "start:a@"+v.Version+" sha256:starta") || !bounds.keys[versions.ReservedBoundKey("start", "0.1")] {
|
|
t.Errorf("at the start: said %v, bounds %v; want start:a's package-published said and its bound armed", said, bounds.keys)
|
|
}
|
|
if unreleased("start") != "" {
|
|
t.Errorf("a built reservation within its bound raised %s", unreleased("start"))
|
|
}
|
|
})
|
|
t.Run("past its bound", func(t *testing.T) {
|
|
for _, when := range []string{"a look", "the start"} {
|
|
module := map[string]string{"a look": "pastlook", "the start": "paststart"}[when]
|
|
v := recordedLost(module, now.Add(-versions.UnreleasedBound-time.Minute), "a")
|
|
takeIn := func() {
|
|
if when == "a look" {
|
|
if err := rv.Built(ctx, module, v.Version, v.Artifacts, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else {
|
|
rv.releasedAtStart(ctx)
|
|
}
|
|
}
|
|
takeIn()
|
|
if got := unreleased(module); !strings.Contains(got, module+":b unpublished") || !strings.Contains(got, v.Version) {
|
|
t.Errorf("at %s, a built reservation left unmarked past its bound raised %q, want version-unreleased naming %s:b of %s", when, got, module, v.Version)
|
|
}
|
|
if bounds.keys[versions.ReservedBoundKey(module, "0.1")] {
|
|
t.Errorf("at %s, the bound of a reservation past it was armed again", when)
|
|
}
|
|
// One standing is left as it was raised: the next take-in does not write it again over the holder's words (ADR 0674 §3).
|
|
rv.unreleasedWords(ctx, v, "the holder refused the publish again: no space left")
|
|
takeIn()
|
|
if got := unreleased(module); !strings.Contains(got, "no space left") || strings.Contains(got, "nothing within") {
|
|
t.Errorf("at %s again, version-unreleased reads %q, want the holder's words it was left with", when, got)
|
|
}
|
|
|
|
// Note 8's own case: every artifact's published kept on it is taken in before its bound is judged, so nothing is
|
|
// unpublished and nothing is raised; each is said as package-published.
|
|
module = map[string]string{"a look": "keptlook", "the start": "keptstart"}[when]
|
|
v = recordedLost(module, now.Add(-versions.UnreleasedBound-time.Minute), "a", "b")
|
|
said = nil
|
|
takeIn()
|
|
if got := unreleased(module); got != "" {
|
|
t.Errorf("at %s, a reservation past its bound whose kept published mark every artifact raised %q, want no version-unreleased", when, got)
|
|
}
|
|
if !slices.Contains(said, module+":a@"+v.Version+" sha256:"+module+"a") || !slices.Contains(said, module+":b@"+v.Version+" sha256:"+module+"b") {
|
|
t.Errorf("at %s, said %v, want package-published of %s:a and %s:b", when, said, module, module)
|
|
}
|
|
if bounds.keys[versions.ReservedBoundKey(module, "0.1")] {
|
|
t.Errorf("at %s, the bound of a reservation past it was armed", when)
|
|
}
|
|
}
|
|
})
|
|
t.Run("its bound ending", func(t *testing.T) {
|
|
v := recordedLost("ends", now.Add(-time.Minute), "a")
|
|
if err := rv.Built(ctx, "ends", v.Version, v.Artifacts, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
boundKey := versions.ReservedBoundKey("ends", "0.1")
|
|
// The holder refused a publish again within the bound (ADR 0674 §3): the bound's end keeps those words.
|
|
rv.unreleasedWords(ctx, v, "the holder refused the publish again: no space left")
|
|
if !rv.ended(ctx, purged{key: boundKey}) {
|
|
t.Fatal("the reservation's bound was not taken in")
|
|
}
|
|
if got := unreleased("ends"); !strings.Contains(got, "no space left") || strings.Contains(got, "nothing within") {
|
|
t.Errorf("at the bound's end, version-unreleased reads %q, want the holder's words it was left with", got)
|
|
}
|
|
// With none standing, the bound's end raises it, naming the artifact unpublished.
|
|
_ = fs.Clear(ctx, meshconditionsclient.ScopeModule, "ends", versions.VersionUnreleased, "")
|
|
rv.ended(ctx, purged{key: boundKey})
|
|
if got := unreleased("ends"); !strings.Contains(got, "ends:b unpublished") || !strings.Contains(got, "nothing within") {
|
|
t.Errorf("at the bound's end with none standing, version-unreleased reads %q, want nothing within naming ends:b", got)
|
|
}
|
|
// One whose every artifact is published is not left unmarked (1984/AC5): its bound's end raises nothing, and it waits for
|
|
// its version-released.
|
|
all := recordedLost("endsall", now.Add(-time.Minute), "a", "b")
|
|
if err := rv.Built(ctx, "endsall", all.Version, all.Artifacts, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cur, _, _ := rv.r.Of(ctx, "endsall", all.Version); len(cur.Missing()) != 0 {
|
|
t.Fatalf("endsall: %v unpublished after the take-in, want every artifact published", cur.Missing())
|
|
}
|
|
rv.ended(ctx, purged{key: versions.ReservedBoundKey("endsall", "0.1")})
|
|
if got := unreleased("endsall"); got != "" {
|
|
t.Errorf("at the bound's end, a reservation whose every artifact is published raised %q, want no version-unreleased", got)
|
|
}
|
|
if cur, found, _ := rv.r.Of(ctx, "endsall", all.Version); !found || cur.BuiltAt.IsZero() {
|
|
t.Errorf("at the bound's end, the reservation of endsall %s stands no more, want it waiting for its version-released", all.Version)
|
|
}
|
|
})
|
|
}
|
|
|
|
// 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; and
|
|
// H-A-225 — the older build is withdrawn, so nothing publishes under the dropped number, while a re-run of the newer keeps it.
|
|
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, func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) })
|
|
var withdrawn []string
|
|
rv.withdraw = func(module, fp, why string) { withdrawn = append(withdrawn, module+" "+fp+": "+why) }
|
|
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)
|
|
}
|
|
if len(withdrawn) != 1 || withdrawn[0] != "m fp-old: its reservation 0.1.0 was taken over by the build of fp-new, which reserves 0.1.1" {
|
|
t.Errorf("withdrawn %q; want the build of fp-old withdrawn once", withdrawn)
|
|
}
|
|
if again, err := rv.Reserve(ctx, "m", "0.1", []string{"m:a"}, versions.Source{Fingerprint: "fp-new"}); err != nil || again.Version != "0.1.1" || len(withdrawn) != 1 {
|
|
t.Errorf("a re-run of fp-new reserved %q (%v), withdrew %q; want 0.1.1 kept, nothing withdrawn", again.Version, err, withdrawn)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
// TestARegistrysRecordEndsOrMovesAPublish: design 47 H H-A-274 — the artifacts a registry's record holds with the built digest are
|
|
// marked published from it and package-published is said, as a heard published would; H-A-275 — a done build whose number a
|
|
// registry holds at another digest (H-A-279) has its number withdrawn, raises version-held-elsewhere naming both digests, and is
|
|
// built again: at the next number no registry holds, with a fresh digest, published there; the old number's item ends.
|
|
func TestARegistrysRecordEndsOrMovesAPublish(t *testing.T) {
|
|
ctx := context.Background()
|
|
fs := &memFindings{vals: map[string][]byte{}}
|
|
none := func(string, any) (json.RawMessage, error) {
|
|
return json.Marshal(meshcatalog.VersionsAnswer{Module: "node-tools"})
|
|
}
|
|
bounds := &boundsKV{keys: map[string]bool{}}
|
|
rv := newReservations(&memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, none, func() jetstream.KeyValue { return bounds },
|
|
&findings{module: builds.BuilderModule, w: fs, logf: t.Logf, now: time.Now}, t.Logf)
|
|
var said []meshbuilds.PackagePublished
|
|
rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { said = append(said, p); return nil }
|
|
var asked, ended []string
|
|
rv.built = func(_ context.Context, m, v string) error { asked = append(asked, m+" "+v); return nil }
|
|
rv.released = func(_ context.Context, m, v string) { ended = append(ended, m+" "+v) }
|
|
rv.itemEnded = rv.released // a build withdrawn or superseded ends its publish item too (§793)
|
|
registry := []string{"0.1.37"}
|
|
rv.r.HeldNumbers = func(context.Context, string, string, []string) ([]string, error) { return registry, nil }
|
|
res, err := rv.Reserve(ctx, "node-tools", "0.1", []string{"node-tools:program"}, versions.Source{Fingerprint: "fp1"})
|
|
if err != nil || res.Version != "0.1.0" {
|
|
t.Fatalf("reserved %q (%v)", res.Version, err)
|
|
}
|
|
if err := rv.Built(ctx, "node-tools", res.Version, map[string]string{"node-tools:program": "sha256:7c9b10"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cur, _, _ := rv.r.Of(ctx, "node-tools", res.Version)
|
|
held := versions.HeldArtifact{Ref: "node-tools:program", Seat: versions.SeatArtifacts, Name: "node-tools/program", Version: res.Version, Digest: "sha256:7c9b10"}
|
|
rv.markHeld(ctx, cur, []versions.HeldArtifact{held})
|
|
if len(said) != 1 || said[0].Artifact != "node-tools:program" || said[0].Version != res.Version {
|
|
t.Fatalf("package-published said %+v, want node-tools:program at %s from the registry's record", said, res.Version)
|
|
}
|
|
if cur, _, _ = rv.r.Of(ctx, "node-tools", res.Version); len(cur.Missing()) != 0 {
|
|
t.Errorf("the record marked nothing: missing %v", cur.Missing())
|
|
}
|
|
// Another reservation of the module, done, whose number the registry holds at another digest: withdrawn, built again at the
|
|
// next free number with a fresh digest, and published there (H-A-279).
|
|
if _, err := rv.r.Released(ctx, "node-tools", res.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
asked, ended, said, registry = nil, nil, nil, []string{"0.1.0", "0.1.1", "0.1.2"}
|
|
raw, _ := json.Marshal(versions.ReservedVersion{V: 1, Module: "node-tools", Series: "0.1", Version: "0.1.1", Fingerprint: "fp2",
|
|
Artifacts: map[string]string{"node-tools:program": "sha256:7c9b10"}, BuiltAt: time.Now()})
|
|
if _, err := rv.r.Store.Create(ctx, versions.ReservedKey("node-tools", "0.1"), raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rv.rebuild = func(ctx context.Context, module, fp, _ string) error {
|
|
again, err := rv.Reserve(ctx, module, "0.1", []string{"node-tools:program"}, versions.Source{Fingerprint: fp, Rebuild: true})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := rv.Built(ctx, module, again.Version, map[string]string{"node-tools:program": "sha256:fresh"}, nil); err != nil {
|
|
return err
|
|
}
|
|
rv.published(ctx, versions.Outcome{Name: "node-tools/program", Version: again.Version, Digest: "sha256:fresh"}, nil)
|
|
return nil
|
|
}
|
|
cur, _, _ = rv.r.Of(ctx, "node-tools", "0.1.1")
|
|
elsewhere := held
|
|
elsewhere.Version, elsewhere.Digest = "0.1.1", "sha256:83bec1"
|
|
if err := rv.movedElsewhere(ctx, cur, versions.Judged{Elsewhere: []versions.HeldArtifact{elsewhere}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
again, ok, _ := rv.r.Of(ctx, "node-tools", "")
|
|
if !ok || again.Version != "0.1.3" || again.Withdrawn["0.1.1"] != "fp2" || again.Artifacts["node-tools:program"] != "sha256:fresh" {
|
|
t.Fatalf("built again as %+v, want 0.1.3 with a fresh digest and 0.1.1 withdrawn", again)
|
|
}
|
|
if len(said) != 1 || said[0].Version != "0.1.3" || said[0].Digest != "sha256:fresh" {
|
|
t.Errorf("package-published said %+v, want the fresh build at 0.1.3", said)
|
|
}
|
|
if len(asked) != 1 || asked[0] != "node-tools 0.1.3" || len(ended) != 1 || ended[0] != "node-tools 0.1.1" {
|
|
t.Errorf("asked %v, ended %v: want the new number's publish asked and the old one's item ended", asked, ended)
|
|
}
|
|
raised := ""
|
|
for _, v := range fs.vals {
|
|
raised += string(v)
|
|
}
|
|
if !strings.Contains(raised, versions.VersionHeldElsewhere) || !strings.Contains(raised, "sha256:83bec1") || !strings.Contains(raised, "sha256:7c9b10") {
|
|
t.Errorf("no version-held-elsewhere naming both digests: %s", raised)
|
|
}
|
|
}
|
|
|
|
// countedReserve counts the reads of reservation keys (two tokens) and of the index beside them, and with writes the writes of
|
|
// each key (issue 1984 note 8).
|
|
type countedReserve struct {
|
|
*memReserve
|
|
reads, indexReads int
|
|
writes map[string]int
|
|
}
|
|
|
|
func (c *countedReserve) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) {
|
|
if !strings.HasPrefix(key, "index.") {
|
|
c.reads++
|
|
} else {
|
|
c.indexReads++
|
|
}
|
|
return c.memReserve.Get(ctx, key)
|
|
}
|
|
func (c *countedReserve) Create(ctx context.Context, key string, v []byte) (uint64, error) {
|
|
if c.writes != nil {
|
|
c.writes[key]++
|
|
}
|
|
return c.memReserve.Create(ctx, key, v)
|
|
}
|
|
func (c *countedReserve) Update(ctx context.Context, key string, v []byte, rev uint64) (uint64, error) {
|
|
if c.writes != nil {
|
|
c.writes[key]++
|
|
}
|
|
return c.memReserve.Update(ctx, key, v, rev)
|
|
}
|
|
|
|
// TestAPublishedThatMarksNothingIsSaidFromWhatItRead: design 47 H H-A-280 — a published whose digest no reservation holds, at a
|
|
// version many reservations share, reads only the unbuilt ones there, each once; its journal line is made from those reads with
|
|
// no second read; and the handler is told to say in progress before that pass.
|
|
func TestAPublishedThatMarksNothingIsSaidFromWhatItRead(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := &countedReserve{memReserve: &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}}
|
|
none := func(string, any) (json.RawMessage, error) { return json.Marshal(meshcatalog.VersionsAnswer{}) }
|
|
var logs []string
|
|
rv := newReservations(store, 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},
|
|
func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) })
|
|
for i := range 14 {
|
|
m := fmt.Sprintf("m%02d", i)
|
|
res, err := rv.Reserve(ctx, m, "0.1", []string{m + ":a"}, versions.Source{Fingerprint: "fp" + m})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if i > 0 { // one stays unbuilt
|
|
if err := rv.Built(ctx, m, res.Version, map[string]string{m + ":a": "sha256:" + m}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
store.reads = 0
|
|
told := 0
|
|
rv.published(ctx, versions.Outcome{Name: "x", Version: "0.1.0", Digest: "sha256:none"}, func() { told++ })
|
|
if store.reads != 1 || told != 1 {
|
|
t.Errorf("%d reservation reads, in progress told %d times: want the one unbuilt read once, and in progress said before", store.reads, told)
|
|
}
|
|
if line := strings.Join(logs, "\n"); !strings.Contains(line, "kept on") {
|
|
t.Errorf("the journal says %q, want the published kept on the unbuilt reservation", line)
|
|
}
|
|
logs, store.reads = nil, 0
|
|
// A digest of a built one whose artifact is published already marks nothing: the line names it from the one read.
|
|
rv.published(ctx, versions.Outcome{Name: "m05-a", Version: "0.1.0", Digest: "sha256:m05"}, nil)
|
|
rv.published(ctx, versions.Outcome{Name: "m05-a", Version: "0.1.0", Digest: "sha256:m05"}, nil)
|
|
if store.reads != 2 || !strings.Contains(strings.Join(logs, "\n"), "m05 holds 1 built digests") || !strings.Contains(strings.Join(logs, "\n"), "14 reservations stand at it") {
|
|
t.Errorf("%d reads for two publisheds, journal %q: want one read each and the line made from it", store.reads, logs)
|
|
}
|
|
// Design 47 H2 §794: an event whose handling passed 100 ms is said with how long and the part that took it; one within it
|
|
// is not. And H-A-435: every message the builder's handlers take is noted on its consumer's ledger, so the state consumers
|
|
// that builder_health reads holds a message left unacknowledged until it is acknowledged.
|
|
t.Run("a slow handling is said and a message left unacknowledged is kept", func(t *testing.T) {
|
|
var said []string
|
|
clock := time.Unix(1000, 0)
|
|
now := func() time.Time { return clock }
|
|
slow := []eventTaker{{"the reservations", func(jetstream.Msg) bool { clock = clock.Add(150 * time.Millisecond); return true }}}
|
|
fast := []eventTaker{{"the floor", func(jetstream.Msg) bool { clock = clock.Add(100 * time.Millisecond); return true }}}
|
|
rest := eventTaker{"the artifacts gone", func(jetstream.Msg) bool { return true }}
|
|
takeEvent(&agedTestMsg{subject: "a.published", seq: 1}, slow, rest, defaultSlowEvent, now, func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) })
|
|
takeEvent(&agedTestMsg{subject: "a.floor", seq: 2}, fast, rest, defaultSlowEvent, now, func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) })
|
|
if len(said) != 1 || said[0] != "the event a.published took 150ms to handle, over 100ms: the reservations took it" {
|
|
t.Errorf("said %q, want one line for the handling over 100 ms", said)
|
|
}
|
|
// H-A-461 (2): the bound is the setting slow-event, 100 ms when none is set; at 200ms the 150 ms handling is not said.
|
|
for _, c := range []struct {
|
|
set string
|
|
want time.Duration
|
|
}{{"", defaultSlowEvent}, {"200ms", 200 * time.Millisecond}, {"soon", 0}} {
|
|
t.Setenv(stdio.SettingWord(slowEventSetting), c.set)
|
|
if d, err := slowEvent(); d != c.want || (c.want == 0) != (err != nil) {
|
|
t.Errorf("with slow-event %q the bound is %v (%v), want %v", c.set, d, err, c.want)
|
|
}
|
|
}
|
|
said = nil
|
|
takeEvent(&agedTestMsg{subject: "a.published", seq: 3}, slow, rest, 200*time.Millisecond, now, func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) })
|
|
if len(said) != 0 {
|
|
t.Errorf("said %q with slow-event 200ms, want nothing for a 150 ms handling", said)
|
|
}
|
|
clock = clock.Add(-150 * time.Millisecond) // the ledger's times below read as before this row
|
|
facts := map[string]string{}
|
|
l := &ledgers{now: now, sink: func(_ context.Context, k string, v []byte) error { facts[k] = string(v); return nil }}
|
|
hub := &oneHandler{}
|
|
aged := agedHub{hub: hub, ledgers: l}
|
|
_ = aged.Handle("x.>", func(m jetstream.Msg) {
|
|
if m.Subject() == "x.ack" {
|
|
_ = m.Ack()
|
|
}
|
|
})
|
|
hub.handle(&agedTestMsg{subject: "x.left", seq: 7})
|
|
if !strings.Contains(facts["builder-events"], `"oldest-unacknowledged":"1970-01-01T00:16:40.25Z"`) {
|
|
t.Errorf("facts %q, want the message left unacknowledged kept with its delivery", facts)
|
|
}
|
|
hub.handle(&agedTestMsg{subject: "x.ack", seq: 8})
|
|
if !strings.Contains(facts["builder-events"], `"acknowledged":8`) {
|
|
t.Errorf("facts %q, want the acknowledged message applied", facts)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Design 47 H2 H-A-514 addenda: each event goes to the worker of the name it carries, so a slow handling of one name holds
|
|
// no other name's events, one name's events are handled in the order heard, and an event waiting in its worker's queue is
|
|
// said in progress at each tick, so the bus does not give it again.
|
|
func TestEventsAreHandledByAWorkerPerName(t *testing.T) {
|
|
pub := func(name, v string) *queuedEvent {
|
|
return &queuedEvent{wakeMsg: wakeMsg{subject: gopackageregistry.SubjectPublished, data: []byte(`{"name":"` + name + `","version":"` + v + `","digest":"sha256:aa"}`)}}
|
|
}
|
|
if got := eventKey(&pub("a", "1").wakeMsg); got != "name a" {
|
|
t.Errorf("a published's key is %q, want its name", got)
|
|
}
|
|
if got := eventKey(&wakeMsg{subject: meshcatalog.SubjectVersionReleased, data: []byte(`{"module":"m","version":"0.1.0"}`)}); got != "module m" {
|
|
t.Errorf("a version-released's key is %q, want its module", got)
|
|
}
|
|
if got := eventKey(&wakeMsg{subject: "mesh.seat.mesh-artifact-sweep.event.artifacts-gone", data: []byte(`{}`)}); got != "" {
|
|
t.Errorf("an event naming no name has the key %q, want the shared worker's", got)
|
|
}
|
|
release := make(chan struct{})
|
|
handled := make(chan string, 4)
|
|
w := &eventWorkers{keyOf: eventKey, handle: func(m jetstream.Msg) {
|
|
var o versions.Outcome
|
|
_ = json.Unmarshal(m.Data(), &o)
|
|
if o.Name == "a" && o.Version == "1" {
|
|
<-release
|
|
}
|
|
handled <- o.Name + "@" + o.Version
|
|
}}
|
|
a1, a2, a3, b1 := pub("a", "1"), pub("a", "2"), pub("a", "3"), pub("b", "1")
|
|
w.take(a1)
|
|
w.take(a2)
|
|
w.take(a3)
|
|
w.take(b1)
|
|
if got := <-handled; got != "b@1" {
|
|
t.Fatalf("handled %s first, want b's event while a's first one is slow", got)
|
|
}
|
|
close(release)
|
|
if got := []string{<-handled, <-handled, <-handled}; !slices.Equal(got, []string{"a@1", "a@2", "a@3"}) {
|
|
t.Errorf("a's events handled %v, want in the order heard", got)
|
|
}
|
|
// a's worker said its waiting events in progress at each end: a2 and a3 after a1, a3 again after a2.
|
|
if a1.progressed.Load() != 0 || a2.progressed.Load() != 1 || a3.progressed.Load() != 2 {
|
|
t.Errorf("in progress said %d, %d and %d times of a's events, want 0, 1 and 2", a1.progressed.Load(), a2.progressed.Load(), a3.progressed.Load())
|
|
}
|
|
}
|
|
|
|
// queuedEvent is an event of the builder's durable that counts its in-progress.
|
|
type queuedEvent struct {
|
|
wakeMsg
|
|
progressed atomic.Int32
|
|
}
|
|
|
|
func (m *queuedEvent) InProgress() error { m.progressed.Add(1); return nil }
|
|
|
|
// oneHandler is an event hub with one handler.
|
|
type oneHandler struct{ handle func(jetstream.Msg) }
|
|
|
|
func (h *oneHandler) Handle(_ string, f func(jetstream.Msg)) error { h.handle = f; return nil }
|
|
|
|
// agedTestMsg is a message of the builder's events consumer, with the metadata a ledger reads.
|
|
type agedTestMsg struct {
|
|
jetstream.Msg
|
|
subject string
|
|
seq uint64
|
|
}
|
|
|
|
func (m *agedTestMsg) Subject() string { return m.subject }
|
|
func (m *agedTestMsg) Ack() error { return nil }
|
|
func (m *agedTestMsg) Metadata() (*jetstream.MsgMetadata, error) {
|
|
return &jetstream.MsgMetadata{Sequence: jetstream.SequencePair{Stream: m.seq}, NumDelivered: 1, Consumer: "builder-events", Stream: "EVENTS"}, nil
|
|
}
|