diff --git a/modules/mesh-delivery-indexer/internal/index/pending.go b/modules/mesh-delivery-indexer/internal/index/pending.go index a599cf88..6a373b1f 100644 --- a/modules/mesh-delivery-indexer/internal/index/pending.go +++ b/modules/mesh-delivery-indexer/internal/index/pending.go @@ -1170,8 +1170,8 @@ func (ix *Indexer) takeInWholeBy(ctx context.Context, entries []PendingEntry, ow ix.forget(claimed...) branch, src, took := ix.takenWhat(ctx, claimed, applied) ix.logf("take-in: %d merges taken in as one revision by %s%s, oldest %s: source revision %d of %s, %d entries", len(claimed), own, byWords(by), claimed[0].Key, src, branch, len(took)) - revision := ix.NewestRevision(ctx, branch) // the revision this take-in just kept (A-448) - ix.PinPipelines(ctx, branch, revision, src, took) // kept for its pipelines before any later take-in can remove it (A-449a) + // The revision this take-in just kept (A-448), pinned for its pipelines before any later keep can remove it (A-449a). + revision := ix.PinPipelines(ctx, branch, src, took) for _, t := range takenWaits(claimed) { t.By, t.Why = who, why t.Branch, t.Source, t.Entries, t.Revision = branch, src, took, revision diff --git a/modules/mesh-delivery-indexer/internal/index/pending_test.go b/modules/mesh-delivery-indexer/internal/index/pending_test.go index 6f8b9c74..f0fc55c6 100644 --- a/modules/mesh-delivery-indexer/internal/index/pending_test.go +++ b/modules/mesh-delivery-indexer/internal/index/pending_test.go @@ -9,6 +9,7 @@ import ( "slices" "strconv" "strings" + "sync" "testing" "time" @@ -1230,3 +1231,53 @@ func readPending(t *testing.T, b *takeInBed) []PendingEntry { } return entries } + +// pinsHeldUnderKeep is a pins state that counts the writes and reads made while keepMu was not held. +type pinsHeldUnderKeep struct { + *memStore + mu *sync.Mutex + outside int +} + +func (p *pinsHeldUnderKeep) seen() { + if p.mu.TryLock() { // it was free: the caller does not hold it + p.mu.Unlock() + p.outside++ + } +} + +func (p *pinsHeldUnderKeep) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) { + p.seen() + return p.memStore.Get(ctx, key) +} + +func (p *pinsHeldUnderKeep) Create(ctx context.Context, key string, value []byte) error { + p.seen() + return p.memStore.Create(ctx, key, value) +} + +func (p *pinsHeldUnderKeep) Update(ctx context.Context, key string, value []byte, revision uint64) error { + p.seen() + return p.memStore.Update(ctx, key, value, revision) +} + +// A-449a: the revision a take-in made is read and pinned holding keepMu, so no other keep can remove it between the two. +func TestATakeInPinsItsRevisionHoldingKeepMu(t *testing.T) { + ctx := context.Background() + ix := &Indexer{Module: testSelf, Revisions: newMemObjects(0)} + pins := &pinsHeldUnderKeep{memStore: newMem(), mu: &ix.keepMu} + ix.Pins = pins + e := meshcatalog.CatalogEntry{Module: "reader", Repository: "novox/r", Manifest: map[string]any{}, Commit: "c1"} + if err := ix.KeepBranchEntries(ctx, "main", 5, map[string]meshcatalog.CatalogEntry{"reader": e}); err != nil { + t.Fatal(err) + } + if rev := ix.PinPipelines(ctx, "main", 5, []meshcatalog.TakeInEntry{{Module: "reader"}}); rev != 1 { + t.Fatalf("PinPipelines answered revision %d, want 1", rev) + } + if _, _, found, _ := pins.memStore.Get(ctx, PinKey("main", 1)); !found { + t.Error("the revision was not pinned") + } + if pins.outside != 0 { + t.Errorf("%d reads and writes of the pins state were made without keepMu", pins.outside) + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/pins.go b/modules/mesh-delivery-indexer/internal/index/pins.go index bd3ce994..a4c0f4ec 100644 --- a/modules/mesh-delivery-indexer/internal/index/pins.go +++ b/modules/mesh-delivery-indexer/internal/index/pins.go @@ -579,14 +579,18 @@ func ConsumerTools(st Store) []stdio.Tool { // `pipeline.` (`..` of each entry, the runner's key), in the indexer's own pins state as a // pinned of the seat mesh-delivery is applied (design 47 G ยง579, A-449a): so no later take-in removes the revision before // the runner hears of it. The runner's pinned at the pipeline's start says the same record (a set), and its freed at the -// pipeline's end removes it. -func (ix *Indexer) PinPipelines(ctx context.Context, branch string, revision, source int64, entries []meshcatalog.TakeInEntry) { +// pipeline's end removes it. It reads the branch's newest revision and pins it holding keepMu, so no other keep (a verdict, a +// source, an import) can make it non-current and remove it between the two; it answers that revision, 0 when none is kept. +func (ix *Indexer) PinPipelines(ctx context.Context, branch string, source int64, entries []meshcatalog.TakeInEntry) int64 { + ix.keepMu.Lock() + defer ix.keepMu.Unlock() + revision := ix.NewestRevision(ctx, branch) if ix.Pins == nil || revision <= 0 || len(entries) == 0 { - return + return revision } if err := ix.PinsMade(ctx); err != nil { ix.logf("pins: the revision %d of %s is not pinned for its pipelines: %v", revision, branch, err) - return + return revision } for _, e := range entries { record := "pipeline." + runner.PipelinesByBranchRevisionModuleKey(keys.EscapeName(branch), strconv.FormatInt(source, 10), e.Module) @@ -594,4 +598,5 @@ func (ix *Indexer) PinPipelines(ctx context.Context, branch string, revision, so ix.logf("pins: the revision %d of %s is not pinned for %s: %v", revision, branch, record, err) } } + return revision }