perf: replace per-repo loop with bulk SQL for owners-team membership changes - #8347
Draft
troglodyne-bot wants to merge 1 commit into
Draft
perf: replace per-repo loop with bulk SQL for owners-team membership changes#8347troglodyne-bot wants to merge 1 commit into
troglodyne-bot wants to merge 1 commit into
Conversation
Adding or removing a user from the owners team (which has access to all org repos) iterated over every repo and called recalculateTeamAccesses for each one. For orgs with 3000 repos this is O(repos × per-repo queries) — on the order of 30,000+ DB round-trips — causing request timeouts. Replace the per-repo loops in AddTeamMember and removeTeamMember with bulk SQL helpers when the team is the owners team: - grantUserOwnerAccess: one UPDATE (upgrade existing records) + one INSERT...SELECT (create records for repos with no entry). Two queries instead of O(repos × queries). - revokeOwnerTeamAccess: one DELETE (clear owner-level records) + one INSERT...SELECT via UNION ALL (reinstate access from remaining teams and direct collaborations). Again two queries regardless of repo count. Fixes gogs#8232.
|
Hi. Said bot is mine. I've reviewed this and it looks? ok to me, but I'm a perl programmer not a go guy. I'll update the PR desc to be in keeping with your other ones shortly, and give a crack at testing it out soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replace O(repos × queries) per-repo access recalculation with two bulk SQL operations when adding or removing a user from an organization's owners team.
Why
Fixes #8232. The owners team has access to every repository in the org. Adding a user to it calls
recalculateTeamAccessesonce per repo, which itself fires multiple DB queries (get collaborators, get all teams, get each team's members, DELETE + INSERT access records). For an org with ~3000 repos this is 30,000+ DB round-trips — enough to time out the request.How
Two new helpers replace the loop:
grantUserOwnerAccess(add to owners team):UPDATE access SET mode = OwnerMode WHERE user_id = ? AND mode < OwnerMode AND repo_id IN (SELECT id FROM repository WHERE owner_id = ?)— upgrade any existing access recordsINSERT INTO access … SELECT … FROM repository WHERE owner_id = ? AND NOT EXISTS (SELECT 1 FROM access …)— create records for repos that had nonerevokeOwnerTeamAccess(remove from owners team):DELETE FROM access WHERE user_id = ? AND repo_id IN (SELECT id FROM repository WHERE owner_id = ?)— wipe the former owner's recordsINSERT INTO access … SELECT ?, repo_id, MAX(mode) FROM (team_user JOINs UNION ALL collaboration JOINs) GROUP BY repo_id— reinstate access from remaining team memberships and direct collaborationsBoth paths reduce from O(repos × DB queries) to exactly 2 queries regardless of org size. The
GetRepositories()call is also skipped for the owners team since the repo list is no longer needed.The SQL is portable: no MySQL-specific quoting, no vendor-specific upsert syntax. Tested against SQLite, compatible with MySQL and PostgreSQL parameterization via xorm's
Exec.Checklist
Testing
The xorm-based team membership functions don't have a test harness in this codebase today (no
org_team_test.go). Correctness was validated by code review:revokeOwnerTeamAccessis called, so the UNION subquery correctly excludes the now-removed owners-team membership