diff --git a/modules/mesh-grants/cmd/mesh-grants/main.go b/modules/mesh-grants/cmd/mesh-grants/main.go index 7ccd151a..c09db75b 100644 --- a/modules/mesh-grants/cmd/mesh-grants/main.go +++ b/modules/mesh-grants/cmd/mesh-grants/main.go @@ -39,6 +39,7 @@ import ( 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/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" stdio "git.novox.be/novox/mesh-sdk/go" "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}} f.Watch = fv 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) } // 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. diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index a0313a5d..c2229f64 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -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 - // 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.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} @@ -5190,12 +5191,24 @@ func TestGrantsFor(t *testing.T) { first, _ := p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) read := pages() again, _ := p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) - if read != 4 || pages()-read != 2 { - t.Errorf("the revisions were read in %d pages, then %d again, want 4 then 2 (the first page of each)", read, pages()-read) + if read != 4 || pages()-read != 1 { + 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) { 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. diff --git a/modules/mesh-grants/internal/grants/grantsfor.go b/modules/mesh-grants/internal/grants/grantsfor.go index 43b34566..21b36ef5 100644 --- a/modules/mesh-grants/internal/grants/grantsfor.go +++ b/modules/mesh-grants/internal/grants/grantsfor.go @@ -1,6 +1,7 @@ package grants import ( + "context" "encoding/json" "errors" "fmt" @@ -28,51 +29,158 @@ const RunGroup = "run-" // CallRevision is the seat mesh-catalog's `revision`, as the part calls it. 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 // 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, -// as the first page of its answer names them; the newest few of each branch are kept for the process's life. +// read is read for good (as the runner keeps its revisionCache). Each is kept by the branch grants-for was asked of (escaped as +// 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 { - mu sync.Mutex - order map[string][]string - kept map[string]map[string]catalogue.Manifest + mu sync.Mutex + order map[string][]string + 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 { - return strings.Join([]string{r.Branch, r.Kind, r.Group, r.Heads, strconv.FormatInt(r.Revision, 10)}, "\x00") +// revisionKey names a revision of branch; every kind but a candidate's is the branch's own series. +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() defer k.mu.Unlock() - m, ok := k.kept[revisionKey(r)] + m, ok := k.kept[revisionKey(branch, r)] 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() defer k.mu.Unlock() - if k.kept == nil { - k.kept, k.order = map[string]map[string]catalogue.Manifest{}, map[string][]string{} - } - key := revisionKey(r) + k.init() + key := revisionKey(branch, r) 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 - for len(k.order[r.Branch]) > revisionsKeptPerBranch { - delete(k.kept, k.order[r.Branch][0]) - k.order[r.Branch] = k.order[r.Branch][1:] + for len(k.order[branch]) > revisionsKeptPerBranch { + delete(k.kept, k.order[branch][0]) + 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 -// no revision yet. A revision 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. +// no revision yet. A revision whose number the part knows (the branch's newest an event named, a source revision's) and holds +// is answered from what it holds with no read; one it knows but does not hold is read by that number. Otherwise a revision +// 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) { + 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{} var first *meshcatalog.CatalogRevision keep := true @@ -92,14 +200,17 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri case a.Revision == nil: keep = false 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 } first = a.Revision - if in.Group == "" && in.Heads == "" && in.Source == 0 && in.Revision == 0 { + if newestAsked { in.Revision = a.Revision.Revision } - case revisionKey(*a.Revision) != revisionKey(*first): + case revisionKey(branch, *a.Revision) != revisionKey(branch, *first): keep = false } 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 keep && first != nil { - p.revisions.put(*first, manifests) + p.revisions.put(branch, *first, manifests) } return manifests, false, nil }