Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/pins_test.go
T
jschoubben cd16358b21 H-A-496 indexer: pin the revisions a pipeline's movers are kept at
The indexer pinned only a pipeline's own revision, so JudgePins removed
revision 292 while main.291's kept mover still had to read it. HeardPins now
pins every revision pipeline-started names in kept under the pipeline's
record, takes the latest started (said again at a re-pin) as the kept set,
freeing a kept revision it no longer names but never the pipeline's own or
its validate base, and pipeline-ended frees them all. JudgePins is
unchanged.

Builds: design 47 H2 H-A-496, H-A-496 addendum
2026-11-22 21:25:33 +01:00

307 lines
15 KiB
Go

package index
import (
"context"
"encoding/json"
"fmt"
"slices"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
runner "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
)
// Design 47 H2 H-A-496 and its addendum: a pipeline-started pins the pipeline's own revision and each revision its movers
// are kept at under the pipeline's record; a later started (said again at a re-pin) is the pipeline's kept set, freeing a
// kept revision it no longer names but never the pipeline's own; pipeline-ended frees them all.
func TestAPipelinesKeptRevisionsArePinnedUntilItEnds(t *testing.T) {
ctx := context.Background()
pending := uint64(1) // the keeps judge nothing while the consumer is behind
ix := &Indexer{Module: testSelf, Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings(),
Now: func() time.Time { return time.Date(2026, 11, 22, 20, 0, 0, 0, time.UTC) }, PinsPending: func(context.Context) (uint64, error) { return pending, nil }}
kept := func() []string {
n, _ := ix.Revisions.Names(ctx)
return slices.DeleteFunc(n, func(s string) bool { _, _, ok := RevisionGroup(s); return !ok })
}
reader := meshcatalog.CatalogEntry{Module: "reader", Repository: "novox/r", Manifest: map[string]any{}}
for source := int64(1); source <= 6; source++ {
e := reader
e.Commit = fmt.Sprintf("c%d", source)
if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, nil); err != nil {
t.Fatal(err)
}
}
pending = 0
heard := func(started *runner.PipelineStarted, ended *runner.PipelineEnded, want ...string) {
t.Helper()
if err := ix.HeardPins(ctx, started, ended); err != nil {
t.Fatal(err)
}
ix.JudgePins(ctx)
if got := kept(); !slices.Equal(got, want) {
t.Errorf("kept %v, want %v", got, want)
}
}
heard(&runner.PipelineStarted{Branch: "main", Pipeline: "main.1", Revision: 1, Kept: []int64{2, 3, 4}}, nil,
"main.1", "main.2", "main.3", "main.4", "main.6")
if v, _, _, _ := ix.Pins.Get(ctx, "main.3"); !slices.Equal(mustRecords(t, v), []string{"pipeline.main.1"}) {
t.Errorf("the pin of main.3 holds %s, want the pipeline's record", v)
}
heard(&runner.PipelineStarted{Branch: "main", Pipeline: "main.1", Revision: 1, Kept: []int64{3, 4}}, nil,
"main.1", "main.3", "main.4", "main.6")
heard(nil, &runner.PipelineEnded{Branch: "main", Pipeline: "main.1", Revision: 1, Outcome: runner.PipelineEndedOutcomeFailed}, "main.6")
}
// Design 47 H §749 (G §579, A-169, A-171): the indexer pins a revision on a pipeline-started that names it and frees it on its
// pipeline-ended, counts a record once, and removes a revision only while its consumers are caught up, judged against the pins
// its own state holds, never a count of pins stated: one two pipelines pin stays until both free it, one neither pinned nor
// current goes, a revision pinned and not held is said, and unpin clears it.
func TestTheIndexerRemovesARevisionOnlyWhenNoRecordPinsItAndItIsNotCurrent(t *testing.T) {
ctx := context.Background()
clock := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC)
pending := uint64(0)
ix := &Indexer{Module: testSelf, Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings(), Now: func() time.Time { return clock },
PinsPending: func(context.Context) (uint64, error) { return pending, nil }}
kept := func() []string {
n, _ := ix.Revisions.Names(ctx)
return slices.DeleteFunc(n, func(s string) bool { _, _, ok := RevisionGroup(s); return !ok })
}
started := func(pipeline string, revision int64) {
t.Helper()
if err := ix.HeardPins(ctx, &runner.PipelineStarted{Branch: "main", Pipeline: pipeline, Revision: revision}, nil); err != nil {
t.Fatal(err)
}
}
ended := func(pipeline string, revision int64) {
t.Helper()
if err := ix.HeardPins(ctx, nil, &runner.PipelineEnded{Branch: "main", Pipeline: pipeline, Revision: revision}); err != nil {
t.Fatal(err)
}
}
want := func(what string, names ...string) {
t.Helper()
if got := kept(); !slices.Equal(got, names) {
t.Errorf("%s: kept %v, want %v", what, got, names)
}
}
reader := meshcatalog.CatalogEntry{Module: "reader", Repository: "novox/r", Manifest: map[string]any{}}
keep := func(commit string, source int64) {
t.Helper()
e := reader
e.Commit = commit
if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, nil); err != nil {
t.Fatal(err)
}
}
keep("c1", 1)
pending = 3 // pinned before the next keeps, which judge the revisions while the consumers are behind
started("main.5", 1)
started("main.5", 1) // heard twice: pins once
started("main.6", 1)
keep("c2", 2)
keep("c3", 3)
ix.JudgePins(ctx)
want("while messages of the consumer are pending", "main.1", "main.2", "main.3")
pending = 0
ix.JudgePins(ctx)
want("pinned by two records, the middle one neither pinned nor current", "main.1", "main.3")
if v, _, _, _ := ix.Pins.Get(ctx, "main.1"); !slices.Equal(mustRecords(t, v), []string{"pipeline.main.5", "pipeline.main.6"}) {
t.Errorf("the pin of main.1 holds %s, want the two pipelines' records once each", v)
}
ended("main.5", 1)
ix.JudgePins(ctx)
want("one of two records freed it", "main.1", "main.3")
ended("main.6", 1)
ix.JudgePins(ctx)
want("both freed it", "main.3")
if keys, _ := ix.Pins.Keys(ctx); slices.Contains(keys, "main.1") {
t.Errorf("the state holds %v, want no key of the removed revision", keys)
}
// A record pins a revision no longer held: its key says missing and the finding names it; indexer_pin_drop of its key
// (design 47 H §765, H-A-57) drops the pin and clears both.
started("main.7", 1)
if f := ix.Findings.Held(); len(f) != 1 || f[0].Kind != PinMissingKind {
t.Fatalf("findings %v, want one %s", f, PinMissingKind)
}
if r := ix.PinDrop(ctx, "main.1", "operator", "names no running pipeline"); !r.Stored {
t.Fatalf("the pin drop answered %q", r.Answer)
}
if f := ix.Findings.Held(); len(f) != 0 {
t.Errorf("findings %v after the pin drop, want none", f)
}
if keys, _ := ix.Pins.Keys(ctx); slices.Contains(keys, "main.1") {
t.Errorf("the state holds %v after the pin drop, want no main.1", keys)
}
// H-A-168, H-A-170, H-A-173: a passed pipeline's release keeps its revision past a later failed pipeline; a passed one whose
// mover released no version pins nothing and its pipeline-wanted frees nothing; the next one that released a version frees
// the older release on its pipeline-wanted running (its wants written to the end), not on a tier-late one (H-A-174), and the
// revision goes.
mover := []meshcatalog.TakeInEntry{{Module: "reader", Commit: "c", Fingerprint: "f", Repository: "novox/r"}}
take := func(source int64) {
t.Helper()
e := reader
e.Commit = fmt.Sprintf("r%d", source)
if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, mover); err != nil {
t.Fatal(err)
}
}
endOf := func(source, revision int64, outcome, version string) {
t.Helper()
e := runner.PipelineEnded{Branch: "main", Pipeline: fmt.Sprintf("main.%d", revision), Revision: revision, Source: source, Outcome: outcome,
Movers: []runner.MoverEnded{{Module: "reader", Fingerprint: "f", Version: version}}}
if err := ix.HeardPins(ctx, nil, &e); err != nil {
t.Fatal(err)
}
ix.JudgePins(ctx)
}
end := func(source int64, outcome, version string) { t.Helper(); endOf(source, source, outcome, version) }
// An event naming no revision (said before the field) is of the source's lowest-numbered revision, the take-in's (H-A-214).
wanted := func(source int64, outcome string) {
t.Helper()
if err := ix.HeardPipelineWanted(ctx, "main", source, 0, outcome); err != nil {
t.Fatal(err)
}
ix.JudgePins(ctx)
}
take(4)
end(4, runner.PipelineEndedOutcomePassed, "0.1.10")
take(5)
end(5, runner.PipelineEndedOutcomeFailed, "")
want("a passed release past a later failed pipeline", "main.4", "main.5")
take(6)
end(6, runner.PipelineEndedOutcomePassed, "")
wanted(6, meshwants.PipelineWantedOutcomeRunning)
want("a passed pipeline releasing no version frees no older release", "main.4", "main.6")
take(7)
end(7, runner.PipelineEndedOutcomePassed, "0.1.13")
want("two releases, the unreleased one between them gone", "main.4", "main.7")
wanted(7, meshwants.PipelineWantedOutcomeTierLate) // H-A-174: its later tiers' wants are not written, so it frees nothing
want("a tier-late pipeline-wanted", "main.4", "main.7")
wanted(7, meshwants.PipelineWantedOutcomeRunning)
wanted(7, meshwants.PipelineWantedOutcomeRunning) // heard twice: changes nothing more
want("the newer release written everywhere", "main.7")
if v, _, _, _ := ix.Pins.Get(ctx, "main.7"); !slices.Equal(mustRecords(t, v), []string{ReleaseRecord("main", "reader")}) {
t.Errorf("the pin of main.7 holds %s, want its release alone", v)
}
// H-A-214: a re-pin's revision 8 of the same source 7 releases 0.1.14; its pipeline-wanted names revision 8, so the release
// on revision 8 frees the older one on revision 7, which the source's lowest-numbered revision would not.
take(7)
endOf(7, 8, runner.PipelineEndedOutcomePassed, "0.1.14")
want("a re-pin's release beside the take-in's", "main.7", "main.8")
if err := ix.HeardPipelineWanted(ctx, "main", 7, 8, meshwants.PipelineWantedOutcomeRunning); err != nil {
t.Fatal(err)
}
ix.JudgePins(ctx)
want("the re-pin's release written everywhere", "main.8")
}
func mustRecords(t *testing.T, v []byte) []string {
t.Helper()
var pv PinValue
if err := json.Unmarshal(v, &pv); err != nil {
t.Fatal(err)
}
slices.Sort(pv.Records)
return pv.Records
}
// H-A-147 (§743, §749, H-A-108 (3)): a take-in with a mover pins, in its write, the branch's previous revision (the newest kept
// before the write) for the same pipeline record as its own, so the validate step's case before can read it while the pipeline
// runs, and names it as its base on the revision it writes (H-A-151, H-A-154); a take-in with no revision before it pins only
// its own and names no base; the pipeline's end frees both. A passed pipeline's end pins its revision under its mover's release
// record instead (H-A-168, H-A-170), so it stays when a later take-in makes it no longer current.
func TestATakeInPinsTheBranchsPreviousRevisionForItsPipeline(t *testing.T) {
ctx := context.Background()
reader := meshcatalog.CatalogEntry{Module: "reader", Repository: "novox/r", Manifest: map[string]any{}}
mover := []meshcatalog.TakeInEntry{{Module: "reader", Commit: "c", Fingerprint: "f", Repository: "novox/r"}}
for _, tc := range []struct {
name string
before []int64 // sources taken in with no mover before the take-in of source 5
whileRuns []string
afterTheEnd []string
at int64 // the number the take-in of source 5 keeps: the branch's newest + 1 (H-A-213 (1))
base int64 // the base that revision names (H-A-154)
passed bool // main.5's pipeline passed: its release pins main.5
unreleased bool // its mover released no version there, so nothing pins main.5 (H-A-173)
then []int64 // sources taken in with no mover after the end
released []string
}{
{name: "the previous revision kept and pinned", before: []int64{3, 4}, at: 3, whileRuns: []string{"main.2", "main.3"}, afterTheEnd: []string{"main.3"}, base: 2},
{name: "the newest kept before, whatever its source", before: []int64{2}, at: 2, whileRuns: []string{"main.1", "main.2"}, afterTheEnd: []string{"main.2"}, base: 1},
{name: "no revision before it pins only its own", at: 1, whileRuns: []string{"main.1"}, afterTheEnd: []string{"main.1"}},
{name: "a passed pipeline's release keeps its revision once no longer current", before: []int64{4}, at: 2, whileRuns: []string{"main.1", "main.2"},
base: 1, passed: true, then: []int64{6}, afterTheEnd: []string{"main.2", "main.3"}, released: []string{"main.2"}},
{name: "a passed pipeline whose mover released no version pins nothing", before: []int64{4}, at: 2, whileRuns: []string{"main.1", "main.2"},
base: 1, passed: true, unreleased: true, then: []int64{6}, afterTheEnd: []string{"main.3"}},
{name: "a failed pipeline's revision goes once no longer current", before: []int64{4}, at: 2, whileRuns: []string{"main.1", "main.2"},
base: 1, then: []int64{6}, afterTheEnd: []string{"main.3"}},
} {
t.Run(tc.name, func(t *testing.T) {
ix := &Indexer{Module: testSelf, Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings(),
Now: func() time.Time { return time.Date(2026, 11, 20, 16, 0, 0, 0, time.UTC) },
PinsPending: func(context.Context) (uint64, error) { return 0, nil }}
keep := func(source int64, movers []meshcatalog.TakeInEntry) {
t.Helper()
e := reader
e.Commit = fmt.Sprintf("c%d", source)
if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, movers); err != nil {
t.Fatal(err)
}
}
kept := func() []string {
n, _ := ix.Revisions.Names(ctx)
n = slices.DeleteFunc(n, func(s string) bool { _, _, ok := RevisionGroup(s); return !ok })
slices.Sort(n)
return n
}
for _, s := range tc.before {
keep(s, nil)
}
keep(5, mover)
at := fmt.Sprintf("main.%d", tc.at)
if rev, _, err := ReadRevision(ctx, ix.Revisions, at); err != nil || rev.Base != tc.base || rev.Source != 5 {
t.Errorf("%s names the source %d and the base %d (%v), want 5 and %d, the revision its take-in pinned (H-A-154)", at, rev.Source, rev.Base, err, tc.base)
}
if got := kept(); !slices.Equal(got, tc.whileRuns) {
t.Errorf("while the pipeline runs: kept %v, want %v", got, tc.whileRuns)
}
for _, k := range tc.whileRuns {
if v, _, _, _ := ix.Pins.Get(ctx, k); !slices.Equal(mustRecords(t, v), []string{"pipeline." + at}) {
t.Errorf("the pin of %s holds %s, want the pipeline's record", k, v)
}
}
end := runner.PipelineEnded{Branch: "main", Pipeline: at, Revision: tc.at, Outcome: runner.PipelineEndedOutcomeFailed,
Movers: []runner.MoverEnded{{Module: "reader", Fingerprint: "f", Version: "0.1.5"}}}
if tc.unreleased {
end.Movers[0].Version = ""
}
if tc.passed {
end.Outcome = runner.PipelineEndedOutcomePassed
}
if err := ix.HeardPins(ctx, nil, &end); err != nil {
t.Fatal(err)
}
ix.JudgePins(ctx)
for _, s := range tc.then {
keep(s, nil)
}
if got := kept(); !slices.Equal(got, tc.afterTheEnd) {
t.Errorf("after the pipeline's end: kept %v, want %v", got, tc.afterTheEnd)
}
if keys, _ := ix.Pins.Keys(ctx); len(keys) > 0 {
for _, k := range keys {
want := []string{}
if slices.Contains(tc.released, k) {
want = []string{ReleaseRecord("main", "reader")}
}
if v, _, _, _ := ix.Pins.Get(ctx, k); !slices.Equal(append([]string{}, mustRecords(t, v)...), want) {
t.Errorf("after the pipeline's end the pin of %s holds %s, want %v", k, v, want)
}
}
}
})
}
}