- Notifications
You must be signed in to change notification settings - Fork1.1k
Commit7e8fcb4
authored
perf: optimize prebuilds membership reconciliation to check orgs not presets (#20493)
## DescriptionThe membership reconciliation ensures the prebuilds system user is amember of all organizations with prebuilds configured. To supportprebuilds quota management, each organization must have a prebuildsgroup that the system user belongs to.## ProblemPreviously, membership reconciliation iterated over all presets to checkand update membership status. This meant database queries`GetGroupByOrgAndName` and `InsertGroupMember` were executed for eachpreset. Since presets are unique combinations of `(organization,template, template version, preset)`, this resulted in several redundantchecks for the same organization.In dogfood, `InsertGroupMember` was called thousands of times per day,even though memberships were already configured ([internal Grafanadashboard link](https://grafana.dev.coder.com/goto/46MZ1UgDg?orgId=1))<img width="5382" height="1788" alt="Screenshot 2025-10-28 at 16 01 36"src="https://github.com/user-attachments/assets/757b7253-106f-4f72-8586-8e2ede9f18db"/>## SolutionThis PR introduces `GetOrganizationsWithPrebuildStatus`, a single querythat returns:* All unique organizations with prebuilds configured* Whether the prebuilds user is a member of each organization* Whether the prebuilds group exists in each organization* Whether the prebuilds user is in the prebuilds groupThe membership reconciliation logic now:* Fetches status for all organizations in one query* Only performs inserts for organizations missing required membershipsor groups* Safely handles concurrent operations via unique constraint violations* This reduces database load from `O(presets)` to `O(organizations)` perreconciliation loop, with a single read query when everything isconfigured.## Changes* Add `GetOrganizationsWithPrebuildStatus` SQL query* Update `membership.ReconcileAll` to use organization-basedreconciliation instead of preset-based* Update tests to reflect new behaviorRelated to internal thread:https://codercom.slack.com/archives/C07GRNNRW03/p17605355703813691 parentdd28eef commit7e8fcb4
File tree
10 files changed
+397
-212
lines changed- coderd/database
- dbauthz
- dbmetrics
- dbmock
- queries
- enterprise/coderd/prebuilds
10 files changed
+397
-212
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2648 | 2648 | | |
2649 | 2649 | | |
2650 | 2650 | | |
| 2651 | + | |
| 2652 | + | |
| 2653 | + | |
| 2654 | + | |
| 2655 | + | |
| 2656 | + | |
| 2657 | + | |
2651 | 2658 | | |
2652 | 2659 | | |
2653 | 2660 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3759 | 3759 | | |
3760 | 3760 | | |
3761 | 3761 | | |
| 3762 | + | |
| 3763 | + | |
| 3764 | + | |
| 3765 | + | |
| 3766 | + | |
| 3767 | + | |
| 3768 | + | |
| 3769 | + | |
3762 | 3770 | | |
3763 | 3771 | | |
3764 | 3772 | | |
| |||
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
0 commit comments
Comments
(0)