Skip to content

fix: a ChangeMembers reaching many scopes redid, for each, work that depends only on the actor's grants - #165

Merged
Michael McQuade (giraffesyo) merged 6 commits into
canaryfrom
fix/givers-walk
Nov 25, 2026
Merged

Michael McQuade (giraffesyo) merged 6 commits into
canaryfrom
fix/givers-walk

Conversation

@giraffesyo

@giraffesyo Michael McQuade (giraffesyo) commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

A ChangeMembers reaching many scopes builds one authority per scope, before its transaction and again under its locks. Each one redid work that depends only on the actor's grants and the call, so cost grew with scopes × the actor's grants, or scopes × what a granted role asks. Decisions, and which conditions are read, when and in what order, are unchanged (see Testing).

  • One conditional grant of a role giving nothing a change asked made every scope walk every role giving what it asked
    • The problem: in readEnding, a role that missed the first-permission shortcut counted in left, and the loop over every asked permission, and every role giving each, stopped only when left reached 0. A role giving nothing any end asks is never found, so left never reached 0. Each authority walked |asked| × |roles giving each|, for every scope, twice. Adding a member to a group granted a role (cluster:grant, listing a role of 999 permissions) on 1,000 clusters, by an actor holding conditional grants of 300 roles giving those permissions plus one grant of a role giving only cluster:odd, took 7.6 s (the audit measured 9.9 s), against 1.4 s without that one grant. The shortcut also misses whenever the granted role is a lister, so that 1.4 s baseline walked too.
    • The fix: a role still left after every permission the ends ask has been looked up gives none of them. It is kept as such in givers.none, by the roles the ends grant, for every authority of the call, so later authorities don't count it. Roles left to find are let go once the ends pass the latest of their grants. Each permission is looked up among the roles giving it or among those left, whichever is fewer. Not used where SetRole checks a role it defines, which resolves that role as it will be. Now 0.53 s with the odd grant and 0.52 s without (b19bb8a: 1.1 s and 1.3 s).
  • What a granted role asks was rebuilt for each scope
    • The problem: asking (what deciding a grant of a role asks: its permissions and those of every role it lists, transitively) was memoized per authority. A group granted a role listing 64 roles of 1,000 permissions on 1,000 clusters, by an actor holding one unread conditional grant of a role giving one of them, took 2.9 s (the audit measured 3.6 s), against 0.1 s without it.
    • The fix: asking keeps its result with the call's shared givers (per book, so per call and per pass), except where SetRole checks a role it defines. It now counts the role entries it reads (readingEntries), as the rest of the change path does. Now 0.12 s.
  • Each scope indexed the actor's grants anew, though they were the same on every one
    • The problem: each authority built its own heldIndex and ran givingAt over every kind of grant, and readEnding over every unread role, even where every scope held the same grants (all on the tenant). Adding a member to a group granted cluster.login under a condition on 5,000 clusters, by an actor holding conditional grants of 300 roles on the tenant ending after, took 4.4 s (the audit measured 5.9 s), against 0.3 s without them. That contradicted the README's "once for all the scopes it reaches".
    • The fix: authorities of one call holding the same grants (the positions proven already keys by) share a heldCommon. It holds what turns on the grants alone: kinds, how long the unconditional ones last, unread grants by role and each role's latest end as first found, and the grants giving each permission. It also holds readEnding's positions for each list of ends, found as though none were read, so readAt reads, of them, the ones each authority hasn't. It is used only for the authority's own grants, before it has read any condition. The shared unread is never changed. Read state (read, given, powers, readTo, unreadN) stays per authority, so the second pass under the locks builds its own, and fix: a check denied, and Permissions and Holding left out, what every moment allowed where a conditional grant was replaced as it ran, as extending one does #160's "false" marking stays per authority. The per-scope key is now built with strconv instead of fmt.Sprint, which took a ninth of the call at 5,000 scopes. Now 1.1 s.
  • Docs: README and docs/architecture.md describe the change path's cost as it now is.

Testing

New tests, each failing without its part (run on 3b65f4f's apply.go, or with only that part reverted, keeping the test hooks):

  • TestAChangeToMembersLooksUpTheRolesGivingWhatItAsksOnceWhateverScopesItReaches (part 1, the none memo). Each cluster's grant ends at its own time, so no scope reuses the positions another found for the same end (part 3). Fix: 18,382 → 22,596 entries from one cluster to 50. Canary fails: read 25542 entries ... and 422050 to one granted it on 50. With only the memo reverted it fails: 18,382 → 42,098.
  • TestAChangeLooksUpNoRolesGivingWhatItAsksForARoleGivingNothingItAsks (part 1, letting go / smaller side). Fix: 17,532 → 18,336 entries with the odd grant. Canary fails: read 17130 entries granting a role, and 25496 beside a grant of a role giving nothing it asks. With only the memo kept it fails the same way.
  • TestAChangeToMembersReadsWhatTheRolesItGrantsListOnceWhateverScopesItReaches (part 2). Each cluster's grant ends at its own time, as above. Fix: 20,805 → 33,839. With the per-call asking reverted it fails: read 20805 entries ... and 818035 to one granted it on 50. On 3b65f4f's apply.go it passes (4,801 → 18,227), as asking didn't count the entries it read there.
  • TestAChangeToMembersWalksTheActorsGrantsOnceWhateverScopesItReaches (part 3). Fix: 720 → 916 grants walked, 1,628 → 1,922 entries. Canary fails: walked 822 grants and read 1624 entries ... and 20814 and 31514. Without the readEnding memo it fails: 720 → 10,716 walked. With giving per authority it fails: 1,628 → 11,918 entries.

Decisions identical, on this branch, on 3b65f4f, and on this branch with a self-checking patch (#163's val.patch adapted). The patch panics on any difference between readEnding's reads and a scan of every unread role for every end, between asking and a fresh walk, between the shared index and one built for the authority alone, between a cached givingAt and a fresh one, between givers and the book, and between under and a scan. Outputs and logs masked for UUIDs and times are byte-identical across all three:

  • fix: a change granting many roles, or reaching many scopes, took seconds where the actor held conditional grants of large roles ending after them #163's review differential, ZZ_V2 60×60: 26,057 lines.
  • Its grant-replacement mode, ZZ_REPL 80×60: 36,899 lines.
  • Change sequences 80×120.
  • decidedAs plain 40×120 (4,777 lines) and biased 60×120 (7,085).
  • Concurrent ending.
  • Many bindings 40×40: 132,966 lines.
  • Conditional power 80×30: 31,892 lines.
  • Straddle 40×60: 28,527 lines.
  • Membership batch differential, plain and straddle, 300 seeds each: 0 mismatches.
  • A differential aimed at what this shares across scopes: ChangeMembers over 30 labeled clusters under two workspaces, by actors holding grants on the tenant, on each workspace and on some clusters alone, so their grants differ by scope, with denies binding them on some scopes, roles giving nothing asked or only a granted role's last permission, and groups granted several roles on a cluster, each until its own end. SetRole, DeleteRole or a replaced conditional grant runs between a change's read and its locks, or in a host transaction between two changes. 60×60: 15,901 lines. With a grant replaced on a change's k-th read of roles, 60×60: 17,102 lines, 1,142 replacements.

The validating build checked 8,928 and 12,322 readEnding calls and 33,357 and 43,808 shared indexes with no panic. The only log difference is its extra stats line.

Merged with canary 165131b (#164), cleanly, and run again on the merge, on 165131b and on the merge with the self-checking patch: #163's differential 60×60 (26,057 lines) and its replacement mode 80×60 (36,899), change sequences 80×120 (9,925), decidedAs 40×120 (4,777), conditional power 80×30 (31,892), straddle 40×60 (28,527) and the scope-sharing differential 60×60 (15,752) are byte-identical, and the batch differential, straddle, 300 seeds, has 0 mismatches. Its replacement mode, 60×60 (17,088 lines), differs on one change, whose grant is replaced under its locks: rerun 8 times, it ends refused or in a conflict on 165131b too (3 and 5 times, here 5 and 3).

Timings: medians in ms, this branch / 3b65f4f / b19bb8a, alternating builds, 3 rounds (per-call: 8 rounds of 100 calls, then 9 interleaved rounds of 300):

shape branch 3b65f4f b19bb8a
ChangeMembers, lister on 1,000 clusters, 300 roles giving what it lists + one odd grant 534 7,594 1,126
same without the odd grant 515 1,365 1,292
same, the 300 roles also giving cluster:grant (shortcut hits) 781 1,169 1,280
ChangeMembers, role listing 64 × 1,000 on 1,000 clusters, one grant giving one listed permission 123 2,920 3,134
same, the grant giving the role's first permission 112 121 2,713
ChangeMembers, cluster.login on 5,000 clusters, 300 conditional roles giving it 1,076 4,434 45,550
same, roles giving nothing asked 593 3,131 87,746
same on 1,000 clusters, giving / disjoint 255 / 134 851 / 606 9,618 / 19,302
round 24 audit 4: 1,000 scopes, 300 roles / D=300 244 / 160 851 / 225 18,334 / 5,018
round 24 audit 3: ChangeMembers 50×20 cond 367 366 367
Apply 200 / 1,000 distinct roles, 300 held 368 / 506 371 / 509 361 / 491
Apply 1,000 built-in roles, none / disjoint / last / first 192 / 361 / 363 / 359 179 / 353 / 342 / 344 183 / 14,402 / 494 / 507
per call: Apply / ChangeMembers / one grant 5.8 / 8.7 / 6.0 5.8 / 8.8 / 5.9 5.7 / 8.8 / 5.8

Single-scope shapes are within noise. Apply of 1,000 built-in roles, "last", first measured at 377.5 vs 360.0 ms over 30 runs each, ran every run of one build before the next; interleaved, rotating the order each round, it is 312.8 vs 322.0 ms (b19bb8a 457.9) over 15 runs each. Interleaved over 12 rounds, Apply of 1,000 distinct catalog roles beside 300 held is 442.0 vs 443.3 ms, and per call Apply 5.39 vs 5.46 ms and ChangeMembers 8.23 vs 8.25 ms.

Full suite (go test -race -shuffle=on -cover ./...) with GODEBUG=fips140=only against PostgreSQL 18, on 38b33fd (merged with canary 165131b): passed (2393.162s, coverage: 94.9% of statements). go tool -modfile=tools/go.mod golangci-lint run ./...: 0 issues; go mod tidy -diff clean; govulncheck: no affecting vulnerabilities.

Checklist

  • make check passes
  • No exported name, stored value or table was renamed (they are contract once released)

🤖 Generated with pw code

…ery role giving each permission it asked where the actor held a conditional grant of a role giving none of them
… grant the actor held there where it held the same on all of them
…ive each scope its own end

With one end on every cluster, the positions read for it, found on the first scope, answered
for every other, so the tests of the roles found to give nothing asked and of what a granted role
asks passed with either reverted. Each cluster's grant now ends at its own time.
@giraffesyo
Michael McQuade (giraffesyo) marked this pull request as ready for review November 25, 2026 07:19
@giraffesyo
Michael McQuade (giraffesyo) merged commit 1e8198a into canary Nov 25, 2026
10 checks passed
@giraffesyo
Michael McQuade (giraffesyo) deleted the fix/givers-walk branch November 25, 2026 07:54
@github-actions github-actions Bot mentioned this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant