The kept trunk reads the newest revision again when the one named is removed under the read

The pin judge removes a trunk revision, its list and its entries, as soon as a newer one is kept; take-ins keep one every few
seconds, so the read of the newest revision failed with no such object and the first view of the catalog half never came.

Builds: design 47 G A-487, A-487b
Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
2026-11-19 12:10:51 +01:00
parent 7ba0eddd92
commit 1dd16bc783
2 changed files with 64 additions and 5 deletions
@@ -522,6 +522,9 @@ type KeptTrunk struct {
manifests map[string]string
}
// rereadTries is how many times a read names the newest revision again when the one it named was removed under it.
const rereadTries = 5
// Reread reads the newest trunk revision of the bucket and holds its view; a bucket keeping none holds an empty view at 0.
func (k *KeptTrunk) Reread(ctx context.Context, o Objects) error {
k.read.Lock()
@@ -537,16 +540,29 @@ func (k *KeptTrunk) reread(ctx context.Context, o Objects) error {
}
return err
}
newest, any, err := newestTrunk(ctx, o)
// The pin judge removes a trunk revision, its list and the entries only it names, as soon as a newer one is kept, and
// revisions are kept seconds apart while take-ins run: the revision named newest can be gone before its entries are read.
// A newer one exists then, so the read names the newest again, a few times, before it fails.
var (
newest meshcatalog.CatalogRevision
any bool
entries []meshcatalog.CatalogEntry
err error
)
for try := 0; try < rereadTries; try++ {
entries = nil
if newest, any, err = newestTrunk(ctx, o); err == nil && any {
_, entries, err = ReadRevision(ctx, o, RevisionObject(newest))
}
if !errors.Is(err, ErrNoObject) {
break
}
}
if err != nil {
return fail(err)
}
var entries []meshcatalog.CatalogEntry
mods, held, at := Store(newOverlay(nil)), Store(newOverlay(nil)), int64(0)
if any {
if _, entries, err = ReadRevision(ctx, o, RevisionObject(newest)); err != nil {
return fail(err)
}
if mods, held, at, err = viewOfEntries(newest.Revision, entries); err != nil {
return fail(err)
}
@@ -144,3 +144,46 @@ func TestTheVerbModuleReadsOneModule(t *testing.T) {
t.Errorf("module nope = %v, want the trunk holds none", err)
}
}
// goneUnderRead is a bucket whose pin judge runs while the first entry of the revision named newest is read: a newer revision
// is kept and the one named is removed, its list and its entries, as the indexer's judge does.
type goneUnderRead struct {
*memObjects
once atomic.Bool
prune func()
}
func (g *goneUnderRead) Get(ctx context.Context, name string) ([]byte, error) {
if strings.HasPrefix(name, "entry.") && g.once.CompareAndSwap(false, true) {
g.prune()
}
return g.memObjects.Get(ctx, name)
}
// A revision removed while it is read (take-ins keep one every few seconds, each removing the last) is read again as the newest
// one, never failed: on novox the first view of the catalog half never came while revisions 487 to 493 were kept.
func TestTheKeptTrunkReadsTheNewerRevisionWhenTheOneNamedIsRemovedUnderIt(t *testing.T) {
mem := newMemObjects(0)
keep := func(revision int64) {
data, _ := json.Marshal(meshcatalog.CatalogEntry{Module: "avahi", Repository: catalogueRepo, None: "not built"})
digest := fmt.Sprintf("d%d", revision)
rev := meshcatalog.CatalogRevision{Revision: revision, Kind: "trunk", Entries: []meshcatalog.EntryRef{{Module: "avahi", Digest: digest}}}
if err := KeepRevision(context.Background(), mem, rev, map[string][]byte{digest: data}); err != nil {
t.Fatal(err)
}
}
keep(1)
objects := &goneUnderRead{memObjects: mem}
objects.prune = func() {
keep(2)
_ = mem.Delete(context.Background(), RevisionObject(meshcatalog.CatalogRevision{Revision: 1, Kind: "trunk"}))
_ = mem.Delete(context.Background(), EntryObject("d1"))
}
kept := &KeptTrunk{}
if err := kept.Reread(context.Background(), objects); err != nil {
t.Fatalf("the read failed when the revision named was removed under it: %v", err)
}
if rev, _, ok, err := kept.Newest(context.Background(), objects); err != nil || !ok || rev.Revision != 2 {
t.Errorf("the view holds revision %d (%v, %v), want 2", rev.Revision, ok, err)
}
}