A read of the newest revision names the newest again while it is newer than the one that failed

Replaces the five tries: progress bounds the loop, the same revision failing twice is the read's failure. One helper,
readNewestOf, serves the kept trunk's reads, TrunkView and BranchView; a read by number still fails loudly.

Builds: design 47 G A-487d (hq 61bf1215), A-487, A-487b
Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
2026-11-19 12:17:40 +01:00
parent 1dd16bc783
commit 33143527f2
2 changed files with 70 additions and 27 deletions
@@ -182,7 +182,7 @@ func newestOf(ctx context.Context, o Objects, branch string) (meshcatalog.Catalo
name := RevisionObject(meshcatalog.CatalogRevision{Branch: branch, Revision: newest})
data, err := o.Get(ctx, name)
if err != nil {
return meshcatalog.CatalogRevision{}, false, err
return meshcatalog.CatalogRevision{Branch: branch, Revision: newest}, false, err // the number that failed, for readNewestOf
}
var rev meshcatalog.CatalogRevision
if err := json.Unmarshal(data, &rev); err != nil {
@@ -191,6 +191,31 @@ func newestOf(ctx context.Context, o Objects, branch string) (meshcatalog.Catalo
return rev, true, nil
}
// readNewestOf is the newest revision of a branch and its entries whole (design 47 G A-487d). The pin judge removes a revision
// as soon as a newer one is kept, so a revision whose list or entry is removed under the read is read again as the newest,
// while the newest is newer than the one that failed; the same revision failing twice is the read's failure. Progress bounds
// the loop. No revision kept is false.
func readNewestOf(ctx context.Context, o Objects, branch string) (meshcatalog.CatalogRevision, []meshcatalog.CatalogEntry, bool, error) {
var failed int64
for {
newest, any, err := newestOf(ctx, o, branch)
if err == nil && !any {
return newest, nil, false, nil
}
var entries []meshcatalog.CatalogEntry
if err == nil {
_, entries, err = ReadRevision(ctx, o, RevisionObject(newest))
if err == nil {
return newest, entries, true, nil
}
}
if !errors.Is(err, ErrNoObject) || newest.Revision == 0 || newest.Revision <= failed {
return newest, nil, false, err
}
failed = newest.Revision
}
}
// changedEntries are the modules whose entry differs between two revisions' lists, one gone or added included, by name.
func changedEntries(before, after []meshcatalog.EntryRef) []string {
was := map[string]string{}
@@ -438,14 +463,10 @@ func TrunkView(ctx context.Context, o Objects) (modules, nodes Store, at int64,
// BranchView is TrunkView of a watched branch: the view read from the newest revision the bucket keeps of that branch
// (design 47 G §291, A-93), empty and at 0 when it keeps none.
func BranchView(ctx context.Context, o Objects, branch string) (modules, nodes Store, at int64, err error) {
newest, any, err := newestOf(ctx, o, branch)
newest, entries, any, err := readNewestOf(ctx, o, branch)
if err != nil || !any {
return newOverlay(nil), newOverlay(nil), 0, err
}
_, entries, err := ReadRevision(ctx, o, RevisionObject(newest))
if err != nil {
return nil, nil, 0, err
}
return viewOfEntries(newest.Revision, entries)
}
@@ -522,9 +543,6 @@ 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()
@@ -540,24 +558,7 @@ func (k *KeptTrunk) reread(ctx context.Context, o Objects) error {
}
return err
}
// 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
}
}
newest, entries, any, err := readNewestOf(ctx, o, TrunkBranch)
if err != nil {
return fail(err)
}
@@ -187,3 +187,45 @@ func TestTheKeptTrunkReadsTheNewerRevisionWhenTheOneNamedIsRemovedUnderIt(t *tes
t.Errorf("the view holds revision %d (%v, %v), want 2", rev.Revision, ok, err)
}
}
// A-487d: the same revision failing twice is the read's failure, said as it is; a read by number never reads another.
func TestTheKeptTrunkFailsWhenTheSameRevisionIsGoneTwice(t *testing.T) {
mem := newMemObjects(0)
data, _ := json.Marshal(meshcatalog.CatalogEntry{Module: "avahi", Repository: catalogueRepo, None: "not built"})
rev := meshcatalog.CatalogRevision{Revision: 1, Kind: "trunk", Entries: []meshcatalog.EntryRef{{Module: "avahi", Digest: "d1"}}}
if err := KeepRevision(context.Background(), mem, rev, map[string][]byte{"d1": data}); err != nil {
t.Fatal(err)
}
_ = mem.Delete(context.Background(), EntryObject("d1")) // revision 1 stays listed, its entry lost, no newer revision
for name, read := range map[string]func() error{
"kept trunk": func() error { return (&KeptTrunk{}).Reread(context.Background(), mem) },
"TrunkView": func() error { _, _, _, err := TrunkView(context.Background(), mem); return err },
} {
if err := read(); !errors.Is(err, ErrNoObject) {
t.Errorf("%s: %v, want the lost entry said (no such object)", name, err)
}
}
}
// A-487d: BranchView and TrunkView name the newest again when the one named is removed under the read.
func TestTrunkViewReadsTheNewerRevisionWhenTheOneNamedIsRemovedUnderIt(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"))
}
if _, _, at, err := TrunkView(context.Background(), objects); err != nil || at != 2 {
t.Errorf("TrunkView read revision %d (%v), want 2", at, err)
}
}