diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens.go index 12fef5af3..a9d5bac82 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens.go @@ -38,8 +38,9 @@ func widenerOf(call func(key string, body any) (json.RawMessage, error), bound t const ( envWidensReadBound = "MESH_RUNNER_WIDENS_READ_BOUND_FILE" envRevisionsKept = "MESH_RUNNER_REVISIONS_KEPT_FILE" - // defaultReadBound is how long one read of widens waits for its answer: a few seconds, never the call's 30. - defaultReadBound = 5 * time.Second + // defaultReadBound is how long one read of widens waits for its answer: 30 s, so a cold grants-for (one that composes a + // revision's grants the grants part did not keep yet) is not cut (design 47 H2 H-A-478). + defaultReadBound = 30 * time.Second // defaultRevisionsKept is how many catalog revisions the runner keeps once read; a revision of the catalog graph is some 1.4 MB. defaultRevisionsKept = 6 ) diff --git a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens_test.go b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens_test.go index 57c7fea7f..03c9cb009 100644 --- a/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens_test.go +++ b/modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/widens_test.go @@ -115,7 +115,7 @@ func TestWidensSettings(t *testing.T) { kept int ok bool }{ - {"defaults", nil, 5 * time.Second, 6, true}, + {"defaults", nil, 30 * time.Second, 6, true}, {"set", map[string]string{envWidensReadBound: "2s\n", envRevisionsKept: "3\n"}, 2 * time.Second, 3, true}, {"no duration", map[string]string{envWidensReadBound: "5"}, 0, 0, false}, {"no number", map[string]string{envRevisionsKept: "0"}, 0, 0, false}, diff --git a/modules/mesh-delivery-pipeline/manifest_test.go b/modules/mesh-delivery-pipeline/manifest_test.go index 4c3989ad5..07d0dbfad 100644 --- a/modules/mesh-delivery-pipeline/manifest_test.go +++ b/modules/mesh-delivery-pipeline/manifest_test.go @@ -237,7 +237,8 @@ func runSettingsOf(raw []byte) bool { } if len(m.Config) != len(want)+len(words) || m.Config["test-runs-cap-mib"]["maximum"] != float64(64) || m.Config["test-runs-cap-mib"]["default"] != float64(16) || m.Config["pipelines-at-once"]["minimum"] != float64(1) || m.Config["pipelines-at-once"]["maximum"] != float64(8) || - m.Config["pipelines-at-once"]["default"] != float64(1) || m.Config["pipelines-waiting-cap"]["default"] != float64(64) { + m.Config["pipelines-at-once"]["default"] != float64(1) || m.Config["pipelines-waiting-cap"]["default"] != float64(64) || + m.Config["widens-read-bound"]["default"] != "30s" { return false } env := map[string]bool{} diff --git a/modules/mesh-delivery-pipeline/module.json b/modules/mesh-delivery-pipeline/module.json index 3c6a71fa8..e1e2bae21 100644 --- a/modules/mesh-delivery-pipeline/module.json +++ b/modules/mesh-delivery-pipeline/module.json @@ -138,9 +138,9 @@ }, "widens-read-bound": { "type": "string", - "description": "how long one read of a pipeline end's widens (the catalog's revision, the seat holders, the grants part) waits for its answer before it is cut and said could not run, a Go duration such as 5s (design 47 G A-472, A-472a).", + "description": "how long one read of a pipeline end's widens (the catalog's revision, the seat holders, the grants part) waits for its answer before it is cut and said could not run, a Go duration such as 30s (design 47 G A-472, A-472a, H2 H-A-478: long enough that a cold grants-for is not cut).", "trusted": false, - "default": "5s" + "default": "30s" }, "revisions-kept": { "type": "number", diff --git a/modules/mesh-grants/cmd/mesh-grants/tools.go b/modules/mesh-grants/cmd/mesh-grants/tools.go index 804287c7b..fccd67400 100644 --- a/modules/mesh-grants/cmd/mesh-grants/tools.go +++ b/modules/mesh-grants/cmd/mesh-grants/tools.go @@ -314,11 +314,14 @@ func recordHandActAs(cause string) func(what, why string) error { func serveTools() error { tv := &toolViews{} gate := newSeatGate() - p := &grants.Part{Read: runtimeReader{views: tv}, Call: runtimeBus{}, Store: grants.BigValues(runtimeStore{}, nil), Emit: runtimeBus{}, SeatList: gate} + // Its journal is its standard error, which the tool runner keeps: grants-for says each stage's time there (H-A-478). + logf := func(f string, a ...any) { fmt.Fprintf(os.Stderr, "mesh-grants: "+f+"\n", a...) } + p := &grants.Part{Read: runtimeReader{views: tv}, Call: runtimeBus{}, Store: grants.BigValues(runtimeStore{}, nil), Emit: runtimeBus{}, SeatList: gate, + Logf: logf} // The reader asks through the runtime, which serves once Serve has its channel. go func() { <-stdio.Served() - gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "mesh-grants: "+f+"\n", a...) }) + gate.Follow(seatlist.StdioBus(), nil, logf) }() tools := append(toolsOf(p), gate.Tools()...) for i := range tools { diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index cb7731f88..7c47f24cf 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -5342,6 +5342,52 @@ func TestGrantsFor(t *testing.T) { } } +// grants-for composes each revision's grants once and keeps them with the revision (design 47 H2 H-A-478 and its addendum): +// a second ask for the same revisions composes nothing again; an ask of another candidate against the same newest composes +// the candidate alone; each stage's time is in the journal. +func TestGrantsForComposesARevisionOnce(t *testing.T) { + candidateX := strings.Replace(xManifest, `"state"`, `"emits":["hello"],"state"`, 1) + 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} + p := partOn(b) + var journal []string + p.Logf = func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) } + ask := func(in meshgrants.GrantsForInput) (meshgrants.GrantsFor, string) { + t.Helper() + journal = nil + got, err := p.GrantsFor(in) + if err != nil || got.Error != "" { + t.Fatalf("grants-for: %+v, %v", got, err) + } + if len(journal) != 1 || !strings.HasPrefix(journal[0], "grants-for main ") || !strings.Contains(journal[0], "the inputs ") { + t.Fatalf("grants-for said %q, want one line of its stages' times", journal) + } + return got, journal[0] + } + first, said := ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) + if len(first.Modules) != 1 || first.Modules[0].Module != "mod-x" || strings.Count(said, "(composed)") != 2 { + t.Errorf("the first ask answered %+v and said %q, want mod-x and both revisions' grants composed", first, said) + } + again, said := ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) + if strings.Contains(said, "composed") || strings.Count(said, "(kept)") != 2 || !reflect.DeepEqual(first, again) { + t.Errorf("a second ask for the same revisions said %q and answered %+v, want both kept and %+v", said, again, first) + } + // Inputs that differ compose both again (H-A-478 second addendum); the same inputs then keep them again. + b.put(grants.ReadResolved, "a.mod-x.greeting", map[string]any{"node": "a", "module": "mod-x", "key": "greeting", "value": "hello"}) + if _, said = ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"}); strings.Count(said, "(composed)") != 2 { + t.Errorf("an ask whose inputs differ said %q, want both revisions' grants composed again", said) + } + if _, said = ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"}); strings.Count(said, "(kept)") != 2 { + t.Errorf("an ask with the same inputs again said %q, want both kept", said) + } + // A run's candidate is another revision: composed once, the newest's grants kept. + _, said = ask(meshgrants.GrantsForInput{Branch: "main", Run: "r1"}) + if !strings.Contains(said, "the newest's grants (kept)") || !strings.Contains(said, "the candidate's grants (composed)") { + t.Errorf("an ask of another candidate said %q, want the newest's grants kept and the candidate's composed", said) + } +} + // grants-for keeps what H-A-195 says of the catalog: so many revisions to a branch as the setting // grants-for-revisions-per-branch says, a run's candidate counted within its branch's; only the followed branches', freed when // one leaves them; and an event below the newest held is the numbering started again, which drops what is kept. @@ -5372,8 +5418,11 @@ func TestGrantsForKeepsWhatItMay(t *testing.T) { perBranch int then func(t *testing.T, b *fakeBus, p *grants.Part, followed *[]string) pages int + // composed is how many of the two revisions' grants the grants-for after it composes again: a revision no longer + // kept has no grants kept either (H-A-478). + composed int }{ - {name: "the setting bounds a branch's revisions: with 2, the candidate is dropped once two newer are kept", pages: 2, + {name: "the setting bounds a branch's revisions: with 2, the candidate is dropped once two newer are kept", pages: 2, composed: 1, then: func(t *testing.T, b *fakeBus, p *grants.Part, _ *[]string) { p.RevisionsPerBranch = setting(t, "2\n") hear(t, p, 10, 0) @@ -5381,14 +5430,14 @@ func TestGrantsForKeepsWhatItMay(t *testing.T) { _, _ = p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) }}, {name: "a run's candidate counts within its branch's bound: with 1, the candidate and the newest each drop the other", run: true, - perBranch: 1, pages: 4, then: func(*testing.T, *fakeBus, *grants.Part, *[]string) {}}, - {name: "a branch that leaves the watched branches frees its revisions", pages: 4, + perBranch: 1, pages: 4, composed: 2, then: func(*testing.T, *fakeBus, *grants.Part, *[]string) {}}, + {name: "a branch that leaves the watched branches frees its revisions", pages: 4, composed: 2, then: func(t *testing.T, _ *fakeBus, p *grants.Part, followed *[]string) { *followed = nil _, _ = p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) *followed = []string{"main"} }}, - {name: "an event below the newest held drops what is kept of the branch", pages: 4, + {name: "an event below the newest held drops what is kept of the branch", pages: 4, composed: 2, then: func(t *testing.T, _ *fakeBus, p *grants.Part, _ *[]string) { hear(t, p, 3, 0) }}, } { t.Run(row.name, func(t *testing.T) { @@ -5411,10 +5460,15 @@ func TestGrantsForKeepsWhatItMay(t *testing.T) { } row.then(t, b, p, &followed) before := len(slices.DeleteFunc(slices.Clone(b.calls), func(c string) bool { return c != grants.CallRevision })) + var said []string + p.Logf = func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) } _, _ = p.GrantsFor(in) if got := len(slices.DeleteFunc(slices.Clone(b.calls), func(c string) bool { return c != grants.CallRevision })) - before; got != row.pages { t.Errorf("grants-for read %d revision pages, want %d", got, row.pages) } + if len(said) != 1 || strings.Count(said[0], "(composed)") != row.composed { + t.Errorf("grants-for said %q, want %d of the revisions' grants composed again", said, row.composed) + } }) } if n, err := grants.RevisionsPerBranchSetting(filepath.Join(t.TempDir(), "none")); n != grants.DefaultRevisionsPerBranch || err != nil { diff --git a/modules/mesh-grants/internal/grants/grantsfor.go b/modules/mesh-grants/internal/grants/grantsfor.go index 11216665c..f8db5685b 100644 --- a/modules/mesh-grants/internal/grants/grantsfor.go +++ b/modules/mesh-grants/internal/grants/grantsfor.go @@ -2,6 +2,8 @@ package grants import ( "context" + "crypto/sha256" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -13,6 +15,7 @@ import ( "strconv" "strings" "sync" + "time" "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshgrants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" @@ -94,9 +97,13 @@ func (p *Part) following(branch string) bool { // 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 + // grants are the grants each kept revision composes to, by module (grantsOfEntries), with the digest of the inputs they were + // composed with: composed once per revision and inputs, kept as long as the revision is (design 47 H2 H-A-478 and its + // addenda). + grants map[string]keptGrants newest map[string]int64 sources map[string]map[int64]int64 sourced map[string][]int64 @@ -113,6 +120,7 @@ func revisionKey(branch string, r meshcatalog.CatalogRevision) string { 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.grants = map[string]keptGrants{} k.sources, k.sourced = map[string]map[int64]int64{}, map[string][]int64{} } } @@ -124,6 +132,44 @@ func (k *revisionsKept) get(branch string, r meshcatalog.CatalogRevision) (map[s return m, ok } +// keptGrants are a revision's composed grants, by module, and the digest of the inputs they were composed with. +type keptGrants struct { + inputs string + grants map[string]map[string]bool +} + +// grantsOf is the grants kept of the revision key names, composed with the inputs of the digest given; false when none is. +func (k *revisionsKept) grantsOf(key, inputs string) (map[string]map[string]bool, bool) { + k.mu.Lock() + defer k.mu.Unlock() + g, ok := k.grants[key] + if !ok || g.inputs != inputs { + return nil, false + } + return g.grants, true +} + +// keepGrants keeps the grants the revision key names composes to with the inputs of the digest given, in place of any kept +// before, while that revision is kept: so the grants are bounded as the revisions are (H-A-195's setting). +func (k *revisionsKept) keepGrants(key, inputs string, g map[string]map[string]bool) { + k.mu.Lock() + defer k.mu.Unlock() + if _, ok := k.kept[key]; ok { + k.grants[key] = keptGrants{inputs: inputs, grants: g} + } +} + +// inputsDigest is the sha256 of the inputs as JSON: what a revision's kept grants are composed with (H-A-478 second +// addendum). Empty when they do not marshal: nothing is kept then. +func inputsDigest(in Inputs) string { + raw, err := json.Marshal(in) + if err != nil { + return "" + } + sum := sha256.Sum256(raw) + return hex.EncodeToString(sum[:]) +} + func (k *revisionsKept) put(branch string, r meshcatalog.CatalogRevision, manifests map[string]catalogue.Manifest, max int) { k.mu.Lock() defer k.mu.Unlock() @@ -135,6 +181,7 @@ func (k *revisionsKept) put(branch string, r meshcatalog.CatalogRevision, manife k.kept[key] = manifests for len(k.order[branch]) > max { delete(k.kept, k.order[branch][0]) + delete(k.grants, k.order[branch][0]) k.order[branch] = k.order[branch][1:] } if r.Kind != "candidate" && r.Source != 0 { @@ -167,6 +214,7 @@ func boolsOf[V any](m map[string]V) map[string]bool { func (k *revisionsKept) dropLocked(branch string) { for _, key := range k.order[branch] { delete(k.kept, key) + delete(k.grants, key) } delete(k.order, branch) delete(k.newest, branch) @@ -250,14 +298,15 @@ func (p *Part) HearCatalogRevision(_ context.Context, data []byte) error { // 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) { +// revision though a newer one is written between them; a revision whose pages name another is not kept. kept names the revision +// as it is kept (revisionKey), empty when it is not: what its grants are kept by (H-A-478). +func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[string]catalogue.Manifest, kept string, none bool, err error) { branch := in.Branch followed := p.following(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 + return m, revisionKey(branch, meshcatalog.CatalogRevision{Revision: n}), false, nil } if newestAsked { in.Revision = n @@ -269,14 +318,14 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri for { raw, err := p.Call.Call(CallRevision, in) if err != nil { - return nil, false, fmt.Errorf("%s does not answer: %w", CallRevision, err) + return nil, "", false, fmt.Errorf("%s does not answer: %w", CallRevision, err) } var a meshcatalog.RevisionAnswer if err := json.Unmarshal(raw, &a); err != nil { - return nil, false, fmt.Errorf("%s answered what is not a revision: %w", CallRevision, err) + return nil, "", false, fmt.Errorf("%s answered what is not a revision: %w", CallRevision, err) } if a.NoneYet { - return nil, true, nil + return nil, "", true, nil } switch { case a.Revision == nil: @@ -286,7 +335,7 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri p.revisions.asked(branch, a.Revision.Revision) } if m, ok := p.revisions.get(branch, *a.Revision); ok { - return m, false, nil + return m, revisionKey(branch, *a.Revision), false, nil } first = a.Revision if newestAsked { @@ -302,15 +351,16 @@ func (p *Part) revisionEntries(in meshcatalog.RevisionInput) (manifests map[stri } m, err := catalogue.Decode(body) if err != nil { - return nil, false, fmt.Errorf("the manifest of %s in the revision does not decode: %w", e.Module, err) + return nil, "", false, fmt.Errorf("the manifest of %s in the revision does not decode: %w", e.Module, err) } manifests[e.Module] = m } if a.Next == "" || a.Next == in.After { if keep && first != nil && followed { p.revisions.put(branch, *first, manifests, p.revisionsPerBranch()) + kept = revisionKey(branch, *first) } - return manifests, false, nil + return manifests, kept, false, nil } in.After = a.Next } @@ -389,26 +439,41 @@ func (p *Part) GrantsFor(in meshgrants.GrantsForInput) (meshgrants.GrantsFor, er default: return meshgrants.GrantsFor{}, errors.New("grants-for names a source revision or a run") } - cand, none, err := p.revisionEntries(candidate) + // Each stage's time in the journal (H-A-478): the inputs, each revision's read and each one's grants, composed or kept. + var stages []string + stage := func(what string, start time.Time) { + stages = append(stages, fmt.Sprintf("%s %s", what, time.Since(start).Round(time.Millisecond))) + } + defer func() { + if p.Logf != nil { + p.Logf("grants-for %s: %s", candidateName(candidate), strings.Join(stages, ", ")) + } + }() + start := time.Now() + cand, candKept, none, err := p.revisionEntries(candidate) + stage("the candidate read", start) if err != nil || none { return meshgrants.GrantsFor{Error: orWhy(err, "the candidate's catalog half is not held: the branch has no revision of it")}, nil } - newest, none, err := p.revisionEntries(meshcatalog.RevisionInput{Branch: in.Branch}) + start = time.Now() + newest, newestKept, none, err := p.revisionEntries(meshcatalog.RevisionInput{Branch: in.Branch}) + stage("the newest read", start) if err != nil || none { return meshgrants.GrantsFor{Error: orWhy(err, "the branch's newest catalog half is not held")}, nil } - base := map[string]catalogue.Manifest{} - for module := range cand { - if m, ok := newest[module]; ok { - base[module] = m - } - } + start = time.Now() input := p.inputs() - was, err := grantsOfEntries(input, base) + digest := inputsDigest(input) + stage("the inputs", start) + start = time.Now() + was, composed, err := p.grantsOfRevision(input, digest, newestKept, newest) + stage("the newest's grants ("+composed+")", start) if err != nil { return meshgrants.GrantsFor{Error: "the branch's newest does not compose: " + err.Error()}, nil } - will, err := grantsOfEntries(input, cand) + start = time.Now() + will, composed, err := p.grantsOfRevision(input, digest, candKept, cand) + stage("the candidate's grants ("+composed+")", start) if err != nil { return meshgrants.GrantsFor{Error: "the candidate does not compose: " + err.Error()}, nil } @@ -435,6 +500,33 @@ func (p *Part) GrantsFor(in meshgrants.GrantsForInput) (meshgrants.GrantsFor, er return out, nil } +// candidateName names the candidate an ask is of, for the journal. +func candidateName(c meshcatalog.RevisionInput) string { + if c.Group != "" { + return c.Branch + " " + c.Group + } + return c.Branch + " source " + strconv.FormatInt(c.Source, 10) +} + +// grantsOfRevision is the grants a revision's manifests compose to with in (of the digest inputs), by module: those kept of the +// revision kept names when they were composed with the same inputs, else composed and kept with it (H-A-478: once per revision +// and inputs). how says which. +func (p *Part) grantsOfRevision(in Inputs, inputs, kept string, manifests map[string]catalogue.Manifest) (grants map[string]map[string]bool, how string, err error) { + if kept != "" && inputs != "" { + if g, ok := p.revisions.grantsOf(kept, inputs); ok { + return g, "kept", nil + } + } + g, err := grantsOfEntries(in, manifests) + if err != nil { + return nil, "composed", err + } + if kept != "" && inputs != "" { + p.revisions.keepGrants(kept, inputs, g) + } + return g, "composed", nil +} + func orWhy(err error, why string) string { if err != nil { return err.Error() diff --git a/modules/mesh-grants/internal/grants/part.go b/modules/mesh-grants/internal/grants/part.go index 30c3ae625..c6aef6f16 100644 --- a/modules/mesh-grants/internal/grants/part.go +++ b/modules/mesh-grants/internal/grants/part.go @@ -170,7 +170,8 @@ type Part struct { Emit Emitter // Create creates a declared stream the bus lacks: the process's connection; nil in the tools, which ask the process. Create Creator - // Logf logs what no state or condition says (an error of acting on an answer): the process's; nil in the tools. + // Logf logs what no state or condition says (an error of acting on an answer): the process's journal, and in the tools + // program its standard error (grants-for's stage times, H-A-478). Logf func(string, ...any) // Objects is the part's object bucket of sealed lists: the process's connection; nil in the tools. Objects Objects