builder_publish_queue no longer lists publish items, the re-ask watch no longer reads mode publish, and at its start the builder carries each answered publish-drop ask onto its reservation as dropped, then deletes each record of mode publish, saying the counts once. Builds: design 47 H2 H-A-749 (1), (2), H-A-746
661 lines
34 KiB
Go
661 lines
34 KiB
Go
package versions
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
|
"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/seatlist"
|
|
)
|
|
|
|
// republishBed is a Republisher over memory states: reservations of module m (a bundle by the oci seat and a Go package by
|
|
// the Go seat), one holder module `oci` that claims the oci seat, a work queue that records its puts (deduplicating by message
|
|
// id when window is set, refusing when full) and sends each put at once as its pull would, and a Send and a Unreleased that
|
|
// record.
|
|
type republishBed struct {
|
|
*Republisher
|
|
reserved *reserveMem
|
|
now time.Time
|
|
mu sync.Mutex // sent and said, which sends at once write
|
|
sent []Publish
|
|
said []string
|
|
ended []string
|
|
refuse string // the words Send fails with, empty for none
|
|
other *OtherBytes // the holder's answer of a version held with other bytes (H-A-729)
|
|
// holders are the registry seats' holders the seat-holders view names (design 47 G A-416).
|
|
holders map[string][]string
|
|
// puts are the items the work queue took; window deduplicates by message id; full refuses every put; held keeps the puts
|
|
// unsent.
|
|
puts []QueuedPublish
|
|
ids map[string]bool
|
|
window, full bool
|
|
held bool
|
|
}
|
|
|
|
func newRepublishBed(t *testing.T) *republishBed {
|
|
t.Helper()
|
|
b := &republishBed{reserved: &reserveMem{mem: newMem()}, now: time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC), ids: map[string]bool{}}
|
|
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, Now: func() time.Time { return b.now },
|
|
Entry: func(_ context.Context, fp string) (meshcatalog.CatalogEntry, error) {
|
|
if fp == "src2:holder" {
|
|
return meshcatalog.CatalogEntry{Serves: []string{SeatArtifacts + ".publish", SeatArtifacts + ".versions"}}, nil
|
|
}
|
|
m := strings.TrimPrefix(fp, "src2:")
|
|
return meshcatalog.CatalogEntry{Artifacts: []meshcatalog.Artifact{
|
|
{Ref: m + ":prog", Kind: "bundle", Language: "go"},
|
|
{Ref: m + ":lib", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.novox.be/novox/" + m + "/go"}}}}, nil
|
|
},
|
|
Send: func(_ context.Context, p Publish) error {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.sent = append(b.sent, p)
|
|
if b.other != nil {
|
|
return b.other
|
|
}
|
|
if b.refuse != "" {
|
|
return errors.New(b.refuse)
|
|
}
|
|
return nil
|
|
},
|
|
Unreleased: func(_ context.Context, v ReservedVersion, why string) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.said = append(b.said, v.Module+" "+v.Version+": "+why)
|
|
},
|
|
Ended: func(_ context.Context, module, version string) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.ended = append(b.ended, module+" "+version)
|
|
},
|
|
Logf: t.Logf}
|
|
b.Put = func(ctx context.Context, q QueuedPublish) (bool, error) {
|
|
if b.full {
|
|
return false, fmt.Errorf("%w: maximum bytes exceeded", ErrQueueFull)
|
|
}
|
|
id := PublishMsgID(q.Holder, q.Class, q.Module, q.Version)
|
|
if b.window && b.ids[id] {
|
|
return true, nil
|
|
}
|
|
b.ids[id] = true
|
|
b.puts = append(b.puts, q)
|
|
if !b.held {
|
|
b.SendQueued(ctx, q)
|
|
}
|
|
return false, nil
|
|
}
|
|
b.Holders = func(seat string) []string { return b.holders[seat] }
|
|
return b
|
|
}
|
|
|
|
// reserve reserves and builds a version of module m (its patch given by the reservation order), its build ended long ago.
|
|
func (b *republishBed) reserve(t *testing.T, module string) ReservedVersion {
|
|
t.Helper()
|
|
b.now = b.now.Add(time.Minute)
|
|
ctx := context.Background()
|
|
v, err := b.Reserved.Reserve(ctx, module, "0.1", []string{module + ":prog", module + ":lib"}, Source{Fingerprint: "src2:" + module})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := b.Reserved.Built(ctx, module, v.Version, map[string]string{module + ":prog": "sha256:aa", module + ":lib": "sha256:bb"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b.now = b.now.Add(UnreleasedBound + time.Minute)
|
|
return v
|
|
}
|
|
|
|
// 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("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)
|
|
}
|
|
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 len(b.puts) != 1 || b.puts[0].Holder != "oci" || b.puts[0].Class != PublishClassLive {
|
|
t.Errorf("the work queue took %+v, want one live item at oci", b.puts)
|
|
}
|
|
})
|
|
t.Run("an entry that cannot be read stops neither the other versions nor the drain", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
b.reserve(t, "m1")
|
|
b.reserve(t, "m2") // newest: queued before m1's entry is met
|
|
entry := b.Entry
|
|
b.Entry = func(ctx context.Context, fp string) (meshcatalog.CatalogEntry, error) {
|
|
if fp == "src2:m1" {
|
|
return meshcatalog.CatalogEntry{}, errors.New("no entry of fingerprint src2:m1 is kept")
|
|
}
|
|
return entry(ctx, fp)
|
|
}
|
|
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/") {
|
|
t.Errorf("sent %+v, want m2 drained though m1's entry cannot be read", b.sent)
|
|
}
|
|
})
|
|
t.Run("an artifact no store holds is said once and not tried again", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
b.reserve(t, "m")
|
|
b.refuse = "oci-artifact-registry's publish of m/prog: not stored: no blob sha256:aa"
|
|
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 reservation with no upstream gets it from the entry once", func(t *testing.T) {
|
|
// design 47 H2 H-A-656: the republisher's read of the entry fills the reservation's upstream by compare-and-set, so the
|
|
// artifact a registry never publishes is no missing one and no entry "names no registry seat" for it
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
entry := b.Entry
|
|
b.Entry = func(ctx context.Context, fp string) (meshcatalog.CatalogEntry, error) {
|
|
e, err := entry(ctx, fp)
|
|
e.Artifacts = append(e.Artifacts, meshcatalog.Artifact{Ref: "m:up", Kind: "upstream"})
|
|
return e, err
|
|
}
|
|
if _, _, err := b.Reserved.Built(ctx, "m", v.Version, map[string]string{"m:up": "sha256:cc"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now))
|
|
cur, _, _ := b.Reserved.At(ctx, ReservedKey("m", "0.1"))
|
|
if !slices.Equal(cur.Upstream, []string{"m:up"}) || slices.Contains(cur.Missing(), "m:up") {
|
|
t.Errorf("the reservation's upstream is %v, missing %v; want m:up filled once and not missing", cur.Upstream, cur.Missing())
|
|
}
|
|
})
|
|
t.Run("a version held with other bytes ends the item at once, no try spent", func(t *testing.T) {
|
|
// design 47 H2 H-A-590 (2), H-A-729: never asked again as it stands; the next build of the fingerprint takes another number
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
b.other = &OtherBytes{Seat: SeatArtifacts, Name: "m/prog", Version: v.Version, Digest: "sha256:zz", Fingerprint: "src2:other"}
|
|
f := b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now))
|
|
if f.Ended == "" || f.Failed != "" || f.Sent || len(b.sent) != 1 || fmt.Sprint(b.ended) != "[m "+v.Version+"]" || !strings.Contains(f.Ended, "sha256:zz") {
|
|
t.Errorf("%+v, sent %d, ended %v: want the item ended at once naming the other digest, and no failure to try again", f, len(b.sent), b.ended)
|
|
}
|
|
})
|
|
t.Run("a version held with other bytes for the publish's own fingerprint ends as published with the held digest", func(t *testing.T) {
|
|
// design 47 H2 H-A-738 (1): equal fingerprints answer as published, nothing stored, the held digest on the reservation
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
var said []string
|
|
b.AsHeld = func(_ context.Context, m Marked, digest string) { said = append(said, m.Artifact+" "+digest) }
|
|
b.other = &OtherBytes{Seat: SeatArtifacts, Name: "m/prog", Version: v.Version, Digest: "sha256:zz", Fingerprint: v.Fingerprint}
|
|
f := b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now))
|
|
cur, _, _ := b.Reserved.At(ctx, ReservedKey("m", "0.1"))
|
|
if f.Ended == "" || f.Failed != "" || f.Sent || len(b.ended) != 0 || !cur.Published["m:prog"] || cur.Artifacts["m:prog"] != "sha256:zz" || fmt.Sprint(said) != "[m:prog sha256:zz]" {
|
|
t.Errorf("%+v, ended %v, published %v, digest %q, said %v: want published with the held digest, said once, no refusal", f, b.ended, cur.Published, cur.Artifacts["m:prog"], said)
|
|
}
|
|
})
|
|
t.Run("the first artifact refused as not stored starts the build again once", func(t *testing.T) {
|
|
// design 47 H2 H-A-662, H-A-703: one start per reservation, whatever number of its artifacts are refused
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
var started []string
|
|
b.Unstored = func(_ context.Context, v ReservedVersion, ref, _ string) {
|
|
started = append(started, v.Version+" "+ref)
|
|
}
|
|
b.refuse = "oci-artifact-registry's publish of m/prog: not stored: no blob sha256:aa"
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now))
|
|
b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now))
|
|
if len(started) != 1 || started[0] != v.Version+" m:prog" {
|
|
t.Errorf("the build again was started %v, want once for %s", started, v.Version)
|
|
}
|
|
})
|
|
t.Run("a refusal in other words is said each time", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
b.reserve(t, "m")
|
|
b.refuse = "the holder is full"
|
|
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)
|
|
b.reserve(t, "m")
|
|
b.full = true
|
|
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("an item put twice within the duplicates window is put once, and stands queued", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
b.window, b.held = true, true
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
q1, _ := b.Republish(ctx, "m", v.Version)
|
|
q2, _ := b.Republish(ctx, "m", v.Version)
|
|
if len(b.puts) != 1 || len(q1) != 1 || len(q2) != 1 {
|
|
t.Errorf("puts %d, queued %v then %v: want one item, queued both times", len(b.puts), q1, q2)
|
|
}
|
|
})
|
|
t.Run("the move puts each item of the old state once, its class as of this run, a superseded one not", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
w := b.reserve(t, "n")
|
|
b.Started = b.now
|
|
b.window, b.held = true, true
|
|
raw, _ := json.Marshal(QueuedPublish{V: 1, Holder: "oci", Module: "m", Version: v.Version, Artifacts: map[string]string{"m:prog": "sha256:aa"}, Queued: b.now, Class: "build-ended-this-run"})
|
|
gone, _ := json.Marshal(QueuedPublish{V: 1, Holder: "oci", Module: "n", Version: w.Version, Artifacts: map[string]string{"n:prog": "sha256:aa"}, Queued: b.now})
|
|
if _, _, err := b.Reserved.Withdraw(ctx, "n", w.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for range 2 {
|
|
if _, err := b.MoveQueued(ctx, []json.RawMessage{raw, gone}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if len(b.puts) != 1 || b.puts[0].Class != PublishClassStart || fmt.Sprint(b.ended) != "[n "+w.Version+" n "+w.Version+"]" {
|
|
t.Errorf("puts %+v, ended %v: want m once as again (its build ended before this run), n's item ended", b.puts, b.ended)
|
|
}
|
|
})
|
|
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)
|
|
b.reserve(t, "m1")
|
|
b.reserve(t, "m2")
|
|
if q, err := b.Republish(ctx, "", ""); err != nil || len(q) != 0 {
|
|
t.Errorf("no holder named: queued %v (%v), want none", q, err)
|
|
}
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
q, err := b.Republish(ctx, "", "")
|
|
if err != nil || fmt.Sprint(q) != "[m2 0.1.0 m1 0.1.0]" || len(b.sent) != 2 || b.sent[0].Seat != SeatArtifacts {
|
|
t.Errorf("the tool with neither queued %v (%v), sent %+v: want both versions at oci, with no limit", q, err, b.sent)
|
|
}
|
|
if _, err := b.Republish(ctx, "m1", "0.1.9"); err == nil {
|
|
t.Error("a version no reservation holds is not refused")
|
|
}
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
_, _ = b.Republish(ctx, "", "")
|
|
})
|
|
}
|
|
|
|
// TestTheRegistriesRecordSaysWhetherAVersionIsHeld: design 47 H H-A-272, H-A-274, H-A-275 — a missing artifact its registry seat
|
|
// holds at the version (the Go form too) with the built digest is held; at the version with another digest it is held elsewhere;
|
|
// a version the seat lacks is said by name. The numbers a registry holds of the module's artifacts are what reserving skips.
|
|
func TestTheRegistriesRecordSaysWhetherAVersionIsHeld(t *testing.T) {
|
|
ctx := context.Background()
|
|
entry := meshcatalog.CatalogEntry{Artifacts: []meshcatalog.Artifact{{Ref: "m:lib", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.example/m"}}}}
|
|
r := &Republisher{Entry: func(context.Context, string) (meshcatalog.CatalogEntry, error) { return entry, nil }}
|
|
v := ReservedVersion{Module: "m", Version: "0.1.37", Fingerprint: "fp", Artifacts: map[string]string{"m:lib": "sha256:a"}}
|
|
for _, c := range []struct {
|
|
name string
|
|
holds []HeldVersion
|
|
all, elsewhere, lacks bool
|
|
}{
|
|
{name: "held in the Go form with the built digest", holds: []HeldVersion{{Version: "v0.1.36", Digest: "sha256:z"}, {Version: "v0.1.37", Digest: "sha256:a"}}, all: true},
|
|
{name: "another digest at the version", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:b"}}, elsewhere: true},
|
|
{name: "the version lacked", holds: []HeldVersion{{Version: "0.1.36", Digest: "sha256:a"}}, lacks: true},
|
|
// H2 H-A-616: no fingerprint kept: held for F when the digest is the reserved one, elsewhere with another
|
|
{name: "no fingerprint kept, the reserved digest", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:a"}}, all: true},
|
|
// H2 H-A-589 (3): a holder that keeps a fingerprint says whose it is, whatever the digest
|
|
{name: "the fingerprint kept is F's", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:b", Fingerprint: "fp"}}, all: true},
|
|
{name: "the fingerprint kept is another's", holds: []HeldVersion{{Version: "0.1.37", Digest: "sha256:a", Fingerprint: "other"}}, elsewhere: true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var seat, name string
|
|
versionsOf := func(_ context.Context, s, n string) ([]HeldVersion, error) { seat, name = s, n; return c.holds, nil }
|
|
j, err := r.Held(ctx, v, versionsOf)
|
|
if err != nil || j.All != c.all || (len(j.Elsewhere) == 1) != c.elsewhere || (j.Lacks != "") != c.lacks || seat != SeatGoPackages || name != "git.example/m" {
|
|
t.Fatalf("judged %+v (%v) asking %s for %s, want all %v, elsewhere %v, lacks %v asking %s for git.example/m", j, err, seat, name, c.all, c.elsewhere, c.lacks, SeatGoPackages)
|
|
}
|
|
if c.lacks && !strings.Contains(j.Lacks, "0.1.37") {
|
|
t.Errorf("what the registry lacks is said as %q", j.Lacks)
|
|
}
|
|
nums, err := r.HeldRecords(ctx, "m", "fp", []string{"m:lib"}, versionsOf)
|
|
if err != nil || len(nums) != len(c.holds) {
|
|
t.Errorf("the held numbers are %v (%v), want one per version the seat holds", nums, err)
|
|
}
|
|
})
|
|
}
|
|
v.Published = map[string]bool{"m:lib": true}
|
|
if j, err := r.Held(ctx, v, nil); err != nil || !j.All {
|
|
t.Errorf("a version with no missing artifact is judged %+v (%v), want all held without a read", j, err)
|
|
}
|
|
// H2 H-A-663: a kept published counts as held while its registry answers it at the version, and no longer when it does not
|
|
v.Published, v.Artifacts = map[string]bool{"m:lib": true, "m:other": true}, map[string]string{"m:lib": "sha256:a", "m:other": "sha256:o"}
|
|
v.Upstream = nil
|
|
entry.Artifacts = append(entry.Artifacts, meshcatalog.Artifact{Ref: "m:other", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Ecosystem: "go", Name: "git.example/o"}})
|
|
v.Published["m:other"] = false
|
|
both := func(_ context.Context, _, n string) ([]HeldVersion, error) {
|
|
if n == "git.example/o" {
|
|
return nil, nil
|
|
}
|
|
return []HeldVersion{{Version: "0.1.37", Digest: "sha256:a"}}, nil
|
|
}
|
|
if j, err := r.Held(ctx, v, both); err != nil || j.All || len(j.Held) != 1 || j.Held[0].Ref != "m:lib" || !strings.Contains(j.Lacks, "git.example/o") {
|
|
t.Errorf("a kept published its registry answers is judged %+v (%v), want it held and the other lacking", j, err)
|
|
}
|
|
}
|
|
|
|
// queueAt writes a queued version of module m at holder oci directly, as a start finds it.
|
|
func (b *republishBed) queueAt(v ReservedVersion, class string, at time.Time) QueuedPublish {
|
|
return QueuedPublish{V: 1, Holder: "oci", Module: v.Module, Version: v.Version, Artifacts: map[string]string{v.Module + ":prog": "sha256:aa"}, Queued: at, Class: class}
|
|
}
|
|
|
|
// TestAQueuedItemIsSentWhileItsBuildStands: design 47 H H-A-290 (2), H-A-291 (1), H2 §793 — an item is put at the holder in the
|
|
// class of its build (live when it ended in this run); a build whose reservation was withdrawn, taken over or superseded by a newer
|
|
// patch or release of its series never publishes, its publish item ended, at the put and at the send; an item whose artifacts are
|
|
// all published or refused as not stored ends unsent.
|
|
func TestAQueuedItemIsSentWhileItsBuildStands(t *testing.T) {
|
|
ctx := context.Background()
|
|
t.Run("a build that ended before this run is put as again, one after it as live", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
old := b.reserve(t, "o")
|
|
b.Started = b.now
|
|
live := b.reserve(t, "l")
|
|
b.held = true
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
_, _ = b.Republish(ctx, "o", old.Version)
|
|
_, _ = b.Republish(ctx, "l", live.Version)
|
|
if len(b.puts) != 2 || b.puts[0].Class != PublishClassStart || b.puts[1].Class != PublishClassLive ||
|
|
PublishEvent("oci", PublishClassLive, "l", live.Version) != "publish.oci.live.l.0=2E1=2E0" {
|
|
t.Errorf("puts %+v, want o again and l live, on publish.<holder>.<class>.<module>.<version>", b.puts)
|
|
}
|
|
})
|
|
for _, at := range []string{"put", "send"} {
|
|
t.Run("a withdrawn build never publishes, at the "+at, func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
if _, ok, err := b.Reserved.Withdraw(ctx, "m", v.Version); err != nil || !ok {
|
|
t.Fatalf("withdrawn %v (%v)", ok, err)
|
|
}
|
|
if at == "put" {
|
|
if q, _ := b.Republish(ctx, "m", v.Version); len(q) != 0 {
|
|
t.Errorf("queued %v", q)
|
|
}
|
|
} else if f := b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now)); f.Ended == "" {
|
|
t.Errorf("the send of a withdrawn build answered %+v, want it ended", f)
|
|
}
|
|
if len(b.sent) != 0 || len(b.puts) != 0 || fmt.Sprint(b.ended) != "[m "+v.Version+"]" {
|
|
t.Errorf("sent %v, puts %v, items ended %v: want nothing sent or put and m %s's item ended", b.sent, b.puts, b.ended, v.Version)
|
|
}
|
|
})
|
|
}
|
|
t.Run("a build a newer patch or release of its series supersedes is ended at the send, never by a newer series", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
if _, err := b.Reserved.Reserve(ctx, "m", "0.2", []string{"m:prog"}, Source{Fingerprint: "src2:m"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f := b.SendQueued(ctx, b.queueAt(v, PublishClassStart, b.now)); !f.Sent || len(b.sent) != 1 || len(b.ended) != 0 {
|
|
t.Errorf("with a standing 0.2: %+v, sent %v, ended %v, want m %s sent, its series not superseded (H-A-293)", f, b.sent, b.ended, v.Version)
|
|
}
|
|
rel := &reserveMem{mem: newMem()}
|
|
raw, _ := json.Marshal(ReleasedRecord{Kept: []ReleasedBuild{{Fingerprint: "f", Version: "0.2.3"}, {Fingerprint: "g", Version: "0.1.9"}}})
|
|
_, _ = rel.Create(ctx, ReleasedKey("m"), raw)
|
|
b.Reserved.Releases = rel
|
|
b.sent = nil
|
|
if f := b.SendQueued(ctx, b.queueAt(v, PublishClassStart, b.now)); f.Ended == "" || len(b.sent) != 0 || fmt.Sprint(b.ended) != "[m "+v.Version+"]" {
|
|
t.Errorf("at the send: %+v, sent %v, ended %v, want m %s retired by the release 0.1.9", f, b.sent, b.ended, v.Version)
|
|
}
|
|
only := &reserveMem{mem: newMem()}
|
|
raw, _ = json.Marshal(ReleasedRecord{Kept: []ReleasedBuild{{Fingerprint: "f", Version: "0.2.0"}}})
|
|
_, _ = only.Create(ctx, ReleasedKey("n"), raw)
|
|
got, err := (&Reserver{Releases: only}).Retired(ctx, []ReservedVersion{{Module: "n", Version: "0.1.4", BuiltAt: b.now}, {Module: "n", Version: "0.1.6"},
|
|
{Module: "n", Version: "0.3.0", BuiltAt: b.now}, {Module: "p", Version: "0.1.2", BuiltAt: b.now}})
|
|
if err != nil || len(got) != 1 || !strings.Contains(got["n 0.1.4"], "reservation 0.1.6") {
|
|
t.Errorf("retired %v (%v), want only n 0.1.4, by the newer patch 0.1.6; n 0.3.0 and p 0.1.2 kept, a release of 0.2 retiring neither", got, err)
|
|
}
|
|
})
|
|
t.Run("a version taken over is not sent, and its item ended", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
if _, err := b.Reserved.Reserve(ctx, "m", "0.1", []string{"m:prog"}, Source{Fingerprint: "src2:other"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f := b.SendQueued(ctx, b.queueAt(v, PublishClassStart, b.now)); f.Ended == "" || len(b.sent) != 0 || fmt.Sprint(b.ended) != "[m "+v.Version+"]" {
|
|
t.Errorf("%+v, sent %v, ended %v: want nothing sent and the item ended", f, b.sent, b.ended)
|
|
}
|
|
})
|
|
t.Run("an item whose artifacts are published or refused as not stored ends unsent", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "m")
|
|
b.refuse = "not stored: no blob"
|
|
if f := b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now)); f.Ended == "" || len(b.sent) != 1 {
|
|
t.Errorf("a not stored refusal answered %+v, sent %d: want it ended after one send", f, len(b.sent))
|
|
}
|
|
if f := b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now)); f.Ended == "" || len(b.sent) != 1 {
|
|
t.Errorf("again: %+v, sent %d: want it ended unsent", f, len(b.sent))
|
|
}
|
|
b.refuse = "the holder is full"
|
|
w := b.reserve(t, "n")
|
|
if f := b.SendQueued(ctx, b.queueAt(w, PublishClassLive, b.now)); f.Failed == "" || f.Sent {
|
|
t.Errorf("a refusal in other words answered %+v, want it failed, handed back", f)
|
|
}
|
|
// design 47 H2 H-A-745: a read of the builder's own state that fails is no answer of a holder: nothing is sent or said and the
|
|
// item is left to be given again, while an answered failure above ended it.
|
|
x := b.reserve(t, "x")
|
|
sent, said := len(b.sent), len(b.said)
|
|
entry := b.Entry
|
|
b.Entry = func(context.Context, string) (meshcatalog.CatalogEntry, error) {
|
|
return meshcatalog.CatalogEntry{}, errors.New("the catalog is down")
|
|
}
|
|
if f := b.SendQueued(ctx, b.queueAt(x, PublishClassLive, b.now)); f.Unread == "" || f.Failed != "" || f.Ended != "" || f.Sent || len(b.sent) != sent || len(b.said) != said {
|
|
t.Errorf("an unreadable entry answered %+v, sent %d more, said %d more: want it unread, nothing sent or said", f, len(b.sent)-sent, len(b.said)-said)
|
|
}
|
|
b.Entry = entry
|
|
b.reserved.mem.down = errors.New("the state is down")
|
|
if f := b.SendQueued(ctx, b.queueAt(x, PublishClassLive, b.now)); f.Unread == "" || f.Failed != "" || f.Ended != "" || f.Sent || len(b.sent) != sent {
|
|
t.Errorf("an unreadable reservation answered %+v: want it unread", f)
|
|
}
|
|
})
|
|
// design 47 H2 H-A-746: a publish dropped by hand is kept on its reservation, which its standing reads: an event does not put it,
|
|
// a send ends it unsent, and builder_publish_again puts it and clears the drop.
|
|
t.Run("a publish dropped by hand is passed over and asked anew by builder_publish_again", func(t *testing.T) {
|
|
b := newRepublishBed(t)
|
|
v := b.reserve(t, "d")
|
|
b.held = true
|
|
b.holders = map[string][]string{SeatArtifacts: {"oci"}}
|
|
if found, err := b.Reserved.MarkDropped(ctx, "d", v.Version, "op@node", "a test drops it", b.now); err != nil || !found {
|
|
t.Fatalf("drop: %v %v", found, err)
|
|
}
|
|
if _, err := b.Republish(ctx, "", ""); err != nil || len(b.puts) != 0 {
|
|
t.Errorf("an event put %d items (%v), want the dropped version passed over", len(b.puts), err)
|
|
}
|
|
if f := b.SendQueued(ctx, b.queueAt(v, PublishClassLive, b.now)); f.Ended == "" || !strings.Contains(f.Ended, "op@node") || len(b.sent) != 0 {
|
|
t.Errorf("a send of the dropped version answered %+v, sent %d: want it ended unsent, naming who dropped it", f, len(b.sent))
|
|
}
|
|
if _, err := b.Republish(ctx, "d", v.Version); err != nil || len(b.puts) != 1 {
|
|
t.Errorf("builder_publish_again put %d items (%v), want the one", len(b.puts), err)
|
|
}
|
|
if cur, ok, err := b.Reserved.Of(ctx, "d", v.Version); err != nil || !ok || cur.Dropped != nil {
|
|
t.Errorf("after builder_publish_again the reservation is %+v (%v), want its drop cleared", cur, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// answerOf is builder_publish_queue whole: the builder's verb publish-queue over its work queue (queue), then the tool over the
|
|
// verb's answer and the state (design 47 H2 §793, H-A-454).
|
|
func answerOf(queue, acted Reader, module, version string) (Queued, error) {
|
|
q, err := PublishQueueOf(queue, module, version)
|
|
if err != nil {
|
|
return Queued{}, err
|
|
}
|
|
raw, err := json.Marshal(q) // the verb's answer travels the bus
|
|
if err != nil {
|
|
return Queued{}, err
|
|
}
|
|
var heard meshbuilds.PublishQueueAnswer
|
|
if err := json.Unmarshal(raw, &heard); err != nil {
|
|
return Queued{}, err
|
|
}
|
|
return AnswerQueue(heard, acted)
|
|
}
|
|
|
|
// TestThePublishQueueToolAnswersAtAnySize: design 47 H H-A-290 (5) — builder_publish_queue over two thousand queued versions answers
|
|
// within a second and small: counts per holder and class with the oldest and newest queue time, a list capped at fifty in stream
|
|
// order (H-A-483, third addendum: each key is its item's stream sequence), and one module's item with its class; each state read
|
|
// in bulk where the reader can.
|
|
func TestThePublishQueueToolAnswersAtAnySize(t *testing.T) {
|
|
at := time.Date(2026, 11, 20, 17, 0, 0, 0, time.UTC)
|
|
vals := map[string]json.RawMessage{}
|
|
for i := range 2000 {
|
|
holder, class := "oci", PublishClassStart
|
|
if i%2 == 1 {
|
|
holder = "go"
|
|
}
|
|
if i >= 1990 {
|
|
class = PublishClassLive
|
|
}
|
|
q := QueuedPublish{V: 1, Holder: holder, Module: fmt.Sprintf("m%d", i), Version: "0.1.0", Artifacts: map[string]string{"a": "sha256:aa"}, Queued: at.Add(time.Duration(i) * time.Second), Class: class}
|
|
vals[strconv.Itoa(i+1)], _ = json.Marshal(q) // the reader keys each item by its stream sequence
|
|
}
|
|
gets := 0
|
|
queue := Reader{All: func() (StatePage, error) { return StatePage{Values: vals}, nil },
|
|
Keys: func() ([]string, error) { return slices.Collect(maps.Keys(vals)), nil },
|
|
Get: func(k string) (json.RawMessage, bool, error) { gets++; v, ok := vals[k]; return v, ok, nil }}
|
|
none := Reader{Keys: func() ([]string, error) { return nil, nil }}
|
|
start := time.Now()
|
|
a, err := answerOf(queue, none, "", "")
|
|
took := time.Since(start)
|
|
raw, _ := json.Marshal(a)
|
|
if err != nil || took > time.Second || a.Total != 2000 || len(a.Queue) != PublishQueueListCap || a.Listed != PublishQueueListCap || len(raw) > 64<<10 || gets != 0 {
|
|
t.Fatalf("answered %d of %d in %s, %d bytes, %d single reads (%v): want all counted, fifty listed, within a second, small, in bulk", len(a.Queue), a.Total, took, len(raw), gets, err)
|
|
}
|
|
if fmt.Sprintln(a.Counts[1].Holder, a.Counts[1].Class, a.Counts[1].Count, a.Counts[1].Oldest.Equal(at.Add(1991*time.Second)), a.Counts[1].Newest.Equal(at.Add(1999*time.Second))) != "go "+PublishClassLive+" 5 true true\n" || len(a.Counts) != 4 {
|
|
t.Errorf("counts %+v, want per holder and class with their oldest and newest", a.Counts)
|
|
}
|
|
for i, q := range a.Queue {
|
|
if q.Module != fmt.Sprintf("m%d", i) {
|
|
t.Fatalf("the list holds %s at %d, want m%d: the items in stream order, whatever their holder or class", q.Module, i, i)
|
|
}
|
|
}
|
|
one, err := answerOf(queue, none, "m1995", "")
|
|
if err != nil || len(one.Queue) != 1 || one.Queue[0].Class != PublishClassLive || one.Total != 2000 {
|
|
t.Errorf("one module's answer %+v (%v), want its item with its class", one.Queue, err)
|
|
}
|
|
if a.Capped || len(a.Read) != 0 {
|
|
t.Errorf("a state one page holds is answered capped: %+v", a.Read)
|
|
}
|
|
queue.All = func() (StatePage, error) { return StatePage{Values: vals, More: true, Keys: 5000, Bytes: 3 << 20}, nil }
|
|
if c, err := answerOf(queue, none, "", ""); err != nil || !c.Capped || len(c.Read) != 1 || c.Read[0].State != PublishesLocal ||
|
|
c.Read[0].KeysRead != 2000 || c.Read[0].StateKeys != 5000 || c.Read[0].StateBytes != 3<<20 || c.Total != 2000 {
|
|
t.Errorf("a state past its one page answered capped %v, read %+v (%v): want capped, 2000 keys read of 5000, its size said (H-A-293)", c.Capped, c.Read, err)
|
|
}
|
|
queue.All = func() (StatePage, error) { return StatePage{}, errors.ErrUnsupported }
|
|
if b, err := answerOf(queue, none, "", ""); err != nil || b.Total != 2000 || gets != 2000 {
|
|
t.Errorf("a reader with no bulk read answered %d (%v) with %d single reads, want every key read one by one", b.Total, err, gets)
|
|
}
|
|
}
|
|
|
|
// Design 47 H2 H-A-720: the forms of the keys of the state acted: marks take a lifetime, the release record none, any other key of no
|
|
// form is deleted at the start.
|
|
func TestActedKeysHaveForms(t *testing.T) {
|
|
for _, c := range []struct {
|
|
key string
|
|
mark, known bool
|
|
}{
|
|
{ActedKey("retake:mesh.seat.node-engine.event.running.ace.m"), true, true},
|
|
{ActedKey("retake-asked:m\x00abc"), true, true},
|
|
{ActedKey("retake-ask:ask-1"), true, true},
|
|
{ReleasedKey("m"), false, true},
|
|
{"junk", false, false},
|
|
{ActedKey("publish:mesh.seat.node-engine.event.running.ace.oci"), true, true},
|
|
} {
|
|
if mark, known := ActedFormOf(c.key); mark != c.mark || known != c.known {
|
|
t.Errorf("%q: mark %v, known %v, want %v, %v", c.key, mark, known, c.mark, c.known)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestAPublishIsPutAgainOnAHoldersEventsOnceBySequence: design 47 H2 H-A-606 (ADR 0674 §2, §3) — a registry holder's running and a
|
|
// returned of its node put each unreleased version again, at that holder, once per stream sequence, at most the bound per event, newest
|
|
// first; a module that holds no registry seat, a stopped running and another node's returned put nothing; a mark is kept for each.
|
|
func TestAPublishIsPutAgainOnAHoldersEventsOnceBySequence(t *testing.T) {
|
|
ctx := context.Background()
|
|
b := newRepublishBed(t)
|
|
b.window = true
|
|
b.reserve(t, "m1")
|
|
b.reserve(t, "m2")
|
|
b.reserve(t, "m3")
|
|
acted := &reserveMem{mem: newMem()}
|
|
a := &Againer{Republisher: b.Republisher, Acted: acted, MaxVersions: 2,
|
|
Nodes: func(seat string) []seatlist.HolderNode {
|
|
if seat == SeatArtifacts {
|
|
return []seatlist.HolderNode{{Module: "holder", Node: "ace"}}
|
|
}
|
|
return nil
|
|
}}
|
|
running := func(module, fp string, stopped bool) []byte {
|
|
raw, _ := json.Marshal(nodeengine.Running{Module: module, Node: "ace", Fingerprint: fp, Stopped: stopped})
|
|
return raw
|
|
}
|
|
sub := "mesh.seat.node-engine.event.running.ace."
|
|
puts := func() []string {
|
|
var out []string
|
|
for _, q := range b.puts {
|
|
out = append(out, q.Holder+" "+q.Module)
|
|
}
|
|
return out
|
|
}
|
|
if handled, err := a.Heard(ctx, "mesh.seat.node-engine.event.other.ace", nil, 1); handled || err != nil {
|
|
t.Fatalf("another subject: handled %v, %v", handled, err)
|
|
}
|
|
for _, c := range []struct {
|
|
name, subject string
|
|
data []byte
|
|
seq uint64
|
|
want []string
|
|
}{
|
|
{"a module that holds no registry seat", sub + "m1", running("m1", "src2:m1", false), 2, nil},
|
|
{"a stopped holder", sub + "holder", running("holder", "src2:holder", true), 3, nil},
|
|
{"the holder's running, two of three versions, newest first", sub + "holder", running("holder", "src2:holder", false), 4, []string{"holder m3", "holder m2"}},
|
|
{"the same sequence heard again", sub + "holder", running("holder", "src2:holder", false), 4, nil},
|
|
{"another node's return", "mesh.seat.node-engine.event.manifest-reads-changed.bee", mustJSON(nodeengine.ManifestReadsChanged{Node: "bee", Why: nodeengine.ManifestReadsChangedWhyReturned}), 5, nil},
|
|
{"a change that is no return", "mesh.seat.node-engine.event.manifest-reads-changed.ace", mustJSON(nodeengine.ManifestReadsChanged{Node: "ace", Why: "changed"}), 6, nil},
|
|
{"the holder's node returned", "mesh.seat.node-engine.event.manifest-reads-changed.ace", mustJSON(nodeengine.ManifestReadsChanged{Node: "ace", Why: nodeengine.ManifestReadsChangedWhyReturned}), 7, []string{"holder m3", "holder m2"}},
|
|
} {
|
|
b.puts, b.ids = nil, map[string]bool{}
|
|
handled, err := a.Heard(ctx, c.subject, c.data, c.seq)
|
|
if !handled || err != nil || !slices.Equal(puts(), c.want) {
|
|
t.Errorf("%s: handled %v, %v, put %v, want %v", c.name, handled, err, puts(), c.want)
|
|
}
|
|
}
|
|
if _, _, found, _ := acted.Get(ctx, againKey(sub+"holder")); !found {
|
|
t.Error("the running's mark is not kept in the state acted")
|
|
}
|
|
}
|
|
|
|
func mustJSON(v any) []byte {
|
|
raw, err := json.Marshal(v)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return raw
|
|
}
|