Write per-project refreshes through the shared write path

What does this MR do and why?

Moves today's per-project project_authorizations refreshes onto the shared write path, like !268691 (merged) did for the per-user refresh (https://gitlab.com/groups/gitlab-org/-/work_items/24326).

  • ProjectRecalculatePerUserService recalculates its user on its project through RecalculateUserService, under authz_refresh_write_path checked per user, so each user's refreshes either all use the new path or all use today's, as in the rollout issue.
  • ProjectRecalculateService recalculates every user on the project through RecalculateProjectService, behind its own flag, authz_project_refresh_write_path, checked per project. Every refresh on the new path does a full comparison and takes the locks of the users it changes, so it rolls out on its own and its database load and lock waits can be watched separately.
  • Both log the rows they were asked to change next to the rows they wrote (project_authorizations_project_refresh), like RefreshAuthorizedProjectsService does, so the rollout can spot writes that differ.

Both keep today's path when called inside a transaction, because WriteBatchService refuses to run there. Ai::Catalog::ItemConsumers::CreateService adds a member with immediately_sync_authorizations inside a transaction today, so without this it would fail with the flags on.

References

Database

With authz_project_refresh_write_path on, a project refresh runs RecalculateProjectService, reviewed in !268593 (merged), with two changes since that review. Plans are from a production clone for gitlab-org/gitlab (3,196 authorized users), read-only.

Stored rows, read twice from the primary. Both reads use the primary: replicas can time out after a large removal, and the read after wait_for_writers must see what those writers committed. The old path reads them once, also from the primary.

SELECT "project_authorizations"."user_id", "project_authorizations"."access_level"
FROM "project_authorizations" WHERE "project_authorizations"."project_id" = 278964;
Index Only Scan using index_project_authorizations_on_project_user_access_level on project_authorizations
  Index Cond: (project_id = 278964)
  Heap Fetches: 175
Rows: 3,196
Cold: 824.8 ms (812 ms I/O), buffers 2,387 hit / 730 read
Warm: 1.41 ms, buffers 3,117 hit

Blocked-user lookup. The skip from !266622 (merged) looks up the users whose rows would change, in batches of 1,000, then preloads the deletion marks of the blocked ones. The old path already runs it on the same users. Measured with 1,000 of the project's users, 9 of them blocked.

SELECT "users"."id" FROM "users"
WHERE "users"."id" IN (<1,000 IDs>) AND "users"."user_type" = 0
  AND "users"."state" IN ('blocked', 'ldap_blocked', 'blocked_pending_approval', 'banned');
Index Scan using index_users_on_user_type_and_id on users
  Index Cond: ((user_type = 0) AND (id = ANY (<1,000 IDs>)))
  Filter: (state IN ('blocked', 'ldap_blocked', 'blocked_pending_approval', 'banned'))
  Rows Removed by Filter: 990
Cold: 2,379.6 ms (2,347 ms I/O), buffers 2,783 hit / 2,156 read
Warm: 3.54 ms, buffers 4,739 hit
SELECT "user_custom_attributes".* FROM "user_custom_attributes"
WHERE "user_custom_attributes"."user_id" IN (<9 IDs>);
Index Scan using index_user_custom_attributes_on_user_id_and_key on user_custom_attributes
  Index Cond: (user_id = ANY (<9 IDs>))
Rows: 31
Cold: 74.8 ms, buffers 15 hit / 41 read
Warm: 0.09 ms, buffers 56 hit

The cold users time is about 1,000 random heap fetches on a cold clone. On production, with users mostly cached, it should be close to the warm time.

How to set up and validate locally

Not needed beyond the specs. The existing examples run with the flag on and off, plus a partial rollout and a call inside a transaction.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

🤖 Generated with Claude Code

Edited by Diane Russel

Merge request reports

Loading
Loading