mesh-grants: grants-for learns the newest revision from the event

A grants-for call still read the first page of each revision (up to
768 KB) only to learn its number. The grants holder already hears the
seat mesh-catalog's catalog-revision event, which names each branch's
newest trunk revision and the source revision it was taken in from; the
part now records both from the event and asks the revision verb only for
a revision it does not hold, by its number. Before the first event after
a start the first ask's answer names the newest once. An event below the
newest held is the numbering started again: what is kept of the branch
is dropped.

Builds: design 47 G §441, §401; H H-A-193
This commit is contained in:
2026-11-20 21:44:06 +01:00
parent 20d59ff4f0
commit a62f7b40c1
3 changed files with 159 additions and 29 deletions
@@ -39,6 +39,7 @@ import (
mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
stdio "git.novox.be/novox/mesh-sdk/go" stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/announce" "git.novox.be/novox/mesh-sdk/go/announce"
@@ -182,6 +183,11 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
Bus: grants.BusAddress{URL: account.URL, Fingerprint: account.Fingerprint}} Bus: grants.BusAddress{URL: account.URL, Fingerprint: account.Fingerprint}}
f.Watch = fv f.Watch = fv
f.Part.ReadingRunning(nodeengine.StreamRunning) f.Part.ReadingRunning(nodeengine.StreamRunning)
// The catalog-revision event names each branch's newest revision, so grants-for asks the seat mesh-catalog's revision only
// for a revision it does not hold (design 47 H H-A-193), heard on the same reader beside the branch follower.
if err := reader.AdoptEvent(meshcatalog.Seat, meshcatalog.EventCatalogRevision, f.Part.HearCatalogRevision); err != nil {
return err
}
f.HearKept = func() error { return hearRunning(ctx, account, js, f, logger.Printf) } f.HearKept = func() error { return hearRunning(ctx, account, js, f, logger.Printf) }
// The first view of the bindings (design 25 bindings-first-view): the seat mesh-broker's list, asked at each start and by // The first view of the bindings (design 25 bindings-first-view): the seat mesh-broker's list, asked at each start and by
// hand, with the seat's events held until its answer is applied or its bound passes. // hand, with the seat's events held until its answer is applied or its bound passes.
+16 -3
View File
@@ -5171,7 +5171,8 @@ func TestGrantsFor(t *testing.T) {
} }
// A revision never changes, so one read whole is kept: a second grants-for of the same revisions reads only the first page // A revision never changes, so one read whole is kept: a second grants-for of the same revisions reads only the first page
// of each, which names a revision kept, and answers the same. // of the candidate, which names a revision kept, and none of the newest, whose number the first ask named (H-A-193: it asks
// once); once the catalog-revision events name both, a grants-for reads no page.
b = fixture() b = fixture()
b.revision = map[string]map[string]string{"newest": {"mod-x": xManifest, "mod-y": xManifest}, "candidate": {"mod-x": candidateX, "mod-y": xManifest}} b.revision = map[string]map[string]string{"newest": {"mod-x": xManifest, "mod-y": xManifest}, "candidate": {"mod-x": candidateX, "mod-y": xManifest}}
b.revisionNumber = map[string]int64{"newest": 9, "candidate": 8} b.revisionNumber = map[string]int64{"newest": 9, "candidate": 8}
@@ -5190,12 +5191,24 @@ func TestGrantsFor(t *testing.T) {
first, _ := p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) first, _ := p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"})
read := pages() read := pages()
again, _ := p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) again, _ := p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"})
if read != 4 || pages()-read != 2 { if read != 4 || pages()-read != 1 {
t.Errorf("the revisions were read in %d pages, then %d again, want 4 then 2 (the first page of each)", read, pages()-read) t.Errorf("the revisions were read in %d pages, then %d again, want 4 then 1 (the candidate's first page)", read, pages()-read)
} }
if len(first.Modules) != 1 || !reflect.DeepEqual(first, again) { if len(first.Modules) != 1 || !reflect.DeepEqual(first, again) {
t.Errorf("grants-for from kept revisions answers %+v, first %+v", again, first) t.Errorf("grants-for from kept revisions answers %+v, first %+v", again, first)
} }
p = partOn(b)
for _, ev := range []string{`{"v":1,"kind":"trunk","branch":"main","revision":8,"source":7}`, `{"v":1,"kind":"trunk","branch":"main","revision":9,"source":8}`} {
if err := p.HearCatalogRevision(context.Background(), []byte(ev)); err != nil {
t.Fatal(err)
}
}
_, _ = p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"})
read = pages()
heard, _ := p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"})
if pages() != read || !reflect.DeepEqual(first, heard) {
t.Errorf("after the events named both revisions held, grants-for read %d pages and answered %+v, want none and %+v", pages()-read, heard, first)
}
} }
// heldHalf is an assignment half holding rev, or none. // heldHalf is an assignment half holding rev, or none.
+137 -26
View File
@@ -1,6 +1,7 @@
package grants package grants
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -28,51 +29,158 @@ const RunGroup = "run-"
// CallRevision is the seat mesh-catalog's `revision`, as the part calls it. // CallRevision is the seat mesh-catalog's `revision`, as the part calls it.
const CallRevision = "seat:" + meshcatalog.Seat + "." + meshcatalog.VerbRevision const CallRevision = "seat:" + meshcatalog.Seat + "." + meshcatalog.VerbRevision
// revisionsKeptPerBranch is how many catalog revisions of one branch grants-for keeps, the oldest read dropped first. // revisionsKeptPerBranch is how many catalog revisions of one branch grants-for keeps, the oldest read dropped first; twice
// as many source revisions are kept named by the revision taken in from each.
const revisionsKeptPerBranch = 4 const revisionsKeptPerBranch = 4
// revisionsKept keeps the catalog revisions grants-for read, whole, as manifests by module: a revision never changes, so one // revisionsKept keeps the catalog revisions grants-for read, whole, as manifests by module: a revision never changes, so one
// read is read for good (as the runner keeps its revisionCache). Each is kept by its branch, kind, group, heads and number, // read is read for good (as the runner keeps its revisionCache). Each is kept by the branch grants-for was asked of (escaped as
// as the first page of its answer names them; the newest few of each branch are kept for the process's life. // a key name is), its kind, group, heads and number; the newest few of each branch are kept for the process's life. It also
// holds what the catalog-revision event names (H-A-193): each branch's newest trunk revision, and the revision each source
// revision was taken in as, so grants-for asks the seat mesh-catalog's revision only for a revision it does not hold, never
// to learn the newest.
type revisionsKept struct { type revisionsKept struct {
mu sync.Mutex mu sync.Mutex
order map[string][]string order map[string][]string
kept map[string]map[string]catalogue.Manifest kept map[string]map[string]catalogue.Manifest
newest map[string]int64
sources map[string]map[int64]int64
sourced map[string][]int64
} }
func revisionKey(r meshcatalog.CatalogRevision) string { // revisionKey names a revision of branch; every kind but a candidate's is the branch's own series.
return strings.Join([]string{r.Branch, r.Kind, r.Group, r.Heads, strconv.FormatInt(r.Revision, 10)}, "\x00") func revisionKey(branch string, r meshcatalog.CatalogRevision) string {
if r.Kind != "candidate" {
r.Kind, r.Group, r.Heads = "trunk", "", ""
}
return strings.Join([]string{branch, r.Kind, r.Group, r.Heads, strconv.FormatInt(r.Revision, 10)}, "\x00")
} }
func (k *revisionsKept) get(r meshcatalog.CatalogRevision) (map[string]catalogue.Manifest, bool) { func (k *revisionsKept) init() {
if k.kept == nil {
k.kept, k.order, k.newest = map[string]map[string]catalogue.Manifest{}, map[string][]string{}, map[string]int64{}
k.sources, k.sourced = map[string]map[int64]int64{}, map[string][]int64{}
}
}
func (k *revisionsKept) get(branch string, r meshcatalog.CatalogRevision) (map[string]catalogue.Manifest, bool) {
k.mu.Lock() k.mu.Lock()
defer k.mu.Unlock() defer k.mu.Unlock()
m, ok := k.kept[revisionKey(r)] m, ok := k.kept[revisionKey(branch, r)]
return m, ok return m, ok
} }
func (k *revisionsKept) put(r meshcatalog.CatalogRevision, manifests map[string]catalogue.Manifest) { func (k *revisionsKept) put(branch string, r meshcatalog.CatalogRevision, manifests map[string]catalogue.Manifest) {
k.mu.Lock() k.mu.Lock()
defer k.mu.Unlock() defer k.mu.Unlock()
if k.kept == nil { k.init()
k.kept, k.order = map[string]map[string]catalogue.Manifest{}, map[string][]string{} key := revisionKey(branch, r)
}
key := revisionKey(r)
if _, ok := k.kept[key]; !ok { if _, ok := k.kept[key]; !ok {
k.order[r.Branch] = append(k.order[r.Branch], key) k.order[branch] = append(k.order[branch], key)
} }
k.kept[key] = manifests k.kept[key] = manifests
for len(k.order[r.Branch]) > revisionsKeptPerBranch { for len(k.order[branch]) > revisionsKeptPerBranch {
delete(k.kept, k.order[r.Branch][0]) delete(k.kept, k.order[branch][0])
k.order[r.Branch] = k.order[r.Branch][1:] k.order[branch] = k.order[branch][1:]
}
if r.Kind != "candidate" && r.Source != 0 {
k.sourceLocked(branch, r.Source, r.Revision)
} }
} }
func (k *revisionsKept) sourceLocked(branch string, source, revision int64) {
if k.sources[branch] == nil {
k.sources[branch] = map[int64]int64{}
}
if _, ok := k.sources[branch][source]; !ok {
k.sourced[branch] = append(k.sourced[branch], source)
}
k.sources[branch][source] = revision
for len(k.sourced[branch]) > 2*revisionsKeptPerBranch {
delete(k.sources[branch], k.sourced[branch][0])
k.sourced[branch] = k.sourced[branch][1:]
}
}
// heard takes a trunk revision a catalog-revision event names as its branch's newest. One below the newest held is the
// numbering started again (design 47 H H-A-141): what is kept of the branch was of the earlier numbering, and is dropped.
func (k *revisionsKept) heard(branch string, revision, source int64) {
k.mu.Lock()
defer k.mu.Unlock()
k.init()
if held, ok := k.newest[branch]; ok && revision < held {
for _, key := range k.order[branch] {
delete(k.kept, key)
}
delete(k.order, branch)
delete(k.sources, branch)
delete(k.sourced, branch)
}
k.newest[branch] = revision
if source != 0 {
k.sourceLocked(branch, source, revision)
}
}
// asked takes the newest revision an ask answered, when no event named one since the start (H-A-193: it asks once).
func (k *revisionsKept) asked(branch string, revision int64) {
k.mu.Lock()
defer k.mu.Unlock()
k.init()
if _, ok := k.newest[branch]; !ok {
k.newest[branch] = revision
}
}
// known is the revision in names that the part knows the number of without asking: the branch's newest an event named, or
// the revision a source revision was taken in as; false when it knows none.
func (k *revisionsKept) known(in meshcatalog.RevisionInput) (int64, bool) {
k.mu.Lock()
defer k.mu.Unlock()
switch {
case in.Group != "" || in.Heads != "" || in.Revision != 0:
return 0, false
case in.Source != 0:
n, ok := k.sources[in.Branch][in.Source]
return n, ok
default:
n, ok := k.newest[in.Branch]
return n, ok
}
}
// HearCatalogRevision takes one catalog-revision event of the seat mesh-catalog, as the part's process hears it (design 47 H
// H-A-193): a trunk revision is its branch's newest, and the revision its source revision was taken in as; a candidate's moves
// nothing here. Data that is no such event is an error.
func (p *Part) HearCatalogRevision(_ context.Context, data []byte) error {
var ev meshcatalog.CatalogRevisionEvent
if err := json.Unmarshal(data, &ev); err != nil || ev.Revision <= 0 {
return fmt.Errorf("not a %s event: %.200s", meshcatalog.EventCatalogRevision, data)
}
if ev.Kind == "candidate" {
return nil
}
p.revisions.heard(ev.Branch, ev.Revision, ev.Source)
return nil
}
// revisionEntries reads a catalog graph revision's entries, page after page, as manifests by module. none says the branch has // revisionEntries reads a catalog graph revision's entries, page after page, as manifests by module. none says the branch has
// no revision yet. A revision read whole is kept: once the first page of an answer names a revision that is kept, it is // no revision yet. A revision whose number the part knows (the branch's newest an event named, a source revision's) and holds
// answered from what is kept, without the pages after it. The branch's newest is read on by the number its first page names, so // is answered from what it holds with no read; one it knows but does not hold is read by that number. Otherwise a revision
// its pages are of one revision though a newer one is written between them; a revision whose pages name another is not kept. // read whole is kept: once the first page of an answer names a revision that is kept, it is answered from what is kept,
// without the pages after it. The branch's newest is read on by the number its first page names, so its pages are of one
// revision though a newer one is written between them; a revision whose pages name another is not kept.
func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[string]catalogue.Manifest, none bool, err error) { func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[string]catalogue.Manifest, none bool, err error) {
branch := in.Branch
newestAsked := in.Group == "" && in.Heads == "" && in.Source == 0 && in.Revision == 0
if n, ok := p.revisions.known(in); ok {
if m, held := p.revisions.get(branch, meshcatalog.CatalogRevision{Revision: n}); held {
return m, false, nil
}
if newestAsked {
in.Revision = n
}
}
manifests = map[string]catalogue.Manifest{} manifests = map[string]catalogue.Manifest{}
var first *meshcatalog.CatalogRevision var first *meshcatalog.CatalogRevision
keep := true keep := true
@@ -92,14 +200,17 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri
case a.Revision == nil: case a.Revision == nil:
keep = false keep = false
case first == nil: case first == nil:
if m, ok := p.revisions.get(*a.Revision); ok { if newestAsked {
p.revisions.asked(branch, a.Revision.Revision)
}
if m, ok := p.revisions.get(branch, *a.Revision); ok {
return m, false, nil return m, false, nil
} }
first = a.Revision first = a.Revision
if in.Group == "" && in.Heads == "" && in.Source == 0 && in.Revision == 0 { if newestAsked {
in.Revision = a.Revision.Revision in.Revision = a.Revision.Revision
} }
case revisionKey(*a.Revision) != revisionKey(*first): case revisionKey(branch, *a.Revision) != revisionKey(branch, *first):
keep = false keep = false
} }
for _, e := range a.Entries { for _, e := range a.Entries {
@@ -115,7 +226,7 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri
} }
if a.Next == "" || a.Next == in.After { if a.Next == "" || a.Next == in.After {
if keep && first != nil { if keep && first != nil {
p.revisions.put(*first, manifests) p.revisions.put(branch, *first, manifests)
} }
return manifests, false, nil return manifests, false, nil
} }