Skip Redis in lock checks for projects without locks

What does this MR do and why?

The path lock and LFS lock mergeability checks read a cached result from Redis for every merge request, even when the check is inactive (the cached value is a constant) or the project has no locks at all. On merge request lists that is one or two Redis round trips per merge request for an answer that needs no I/O.

Behind mergeability_lock_checks_skip_redis, inactive checks and lock-free projects skip the results cache, and whether a project has locks is looked up once per project per request. Projects with locks keep today's cached path. Merges run the checks without the cache, so they still query locks fresh.

On GitLab.com these two checks are about 4% of the request time of GET /groups/:id/merge_requests. A page runs about two lock checks per project, so each project costs about 5 ms today, against one indexed EXISTS query after this change.

Supersedes !263466 (closed), rebased on current master.

References

Screenshots or screen recordings

Not a UI change.

How to set up and validate locally

  1. Enable the flag in a Rails console:
    Feature.enable(:mergeability_lock_checks_skip_redis)
  2. Call the group list for a group with open merge requests, once with the flag on and once off, and compare the detailed_merge_status values. They should be identical:
    curl --header "PRIVATE-TOKEN: <token>" "http://gdk.test:3000/api/v4/groups/<id>/merge_requests?state=opened&per_page=100"
  3. Lock a file in one of the projects (Repository > Files > Lock) and repeat. Merge requests changing that file by another author report locked_paths, as before.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Detailed context for AI agents

Change

  • CheckPathLocksService#cacheable? (EE) and CheckLfsFileLocksService#cacheable? (CE) return false when the flag is on and either the check is inactive (no file_locks licence or target is not the default branch; LFS disabled) or the project has no locks. RunChecksService#run_check then calls execute directly, which answers inactive or success without Redis.
  • Lock presence for cacheable? comes from path_locks_present_in_request? / lfs_file_locks_present_in_request?: project.path_locks.exists? / project.lfs_file_locks.any? memoized in Gitlab::SafeRequestStore per project id, so a page with many merge requests of one project queries once.
  • execute reuses that request-wide answer only when cacheable? fetched it on the same check instance (strong_memoized?). RunChecksService calls cacheable? only when use_cache is true, so merges (use_cache: false) never read the request memo and query locks fresh. This follows the review on !263468 (merged) (approval check), where a memoized read on the merge path was raised as a blocker.
  • Projects with locks keep the existing cache key (merge request id, diff head sha, lock epoch) and TTL.
  • Flag off: master behaviour, apart from the Feature.enabled? call. path_locks_present? runs the same single exists? query contains_locked_paths? ran inline.

Before and after per merge request

  • Inactive check: before 1 Redis GET of a constant key (plus a SET when cold); after none.
  • Active check, project without locks: before 1 Redis GET for the lock epoch (not memoized) plus 1 GET for the cached result, and on a miss 1 SQL plus a SET; after 1 SQL per project per request.
  • Active check, project with locks: 1 extra SQL per project per request to decide, then today's cached path.
  • Merge (use_cache: false): unchanged, 1 SQL per check.

Staleness

The request-scoped memo means a lock created during the same request is not seen by later cached checks in that request. Lock and unlock requests (PathLocks::LockService, Lfs::LockFileService and their unlock counterparts) do not run mergeability checks afterwards, today's epoch-keyed cache already relies on a per-request epoch read, and merges do not use the memo.

Trade-off and possible follow-up

The change swaps per-merge-request Redis reads for one indexed EXISTS per project per page. On GitLab.com that is about 5 extra replica queries per request on average for this endpoint, about 5% more than it runs today. A follow-up could batch lock presence for every project on the page into one query per lock type (for example from the project ids Gitlab::Git::RefPreloader already collects), which would remove the added queries. It is left out to keep this change small; the per-project version is already a net win.

Verification

  • ee/spec/services/merge_requests/mergeability/check_path_locks_service_spec.rb and spec/services/merge_requests/mergeability/check_lfs_file_locks_service_spec.rb: #cacheable? for locked, lock-free and inactive cases, flag off, a request-store example asserting zero queries for a second check of the same project, and an example where a cached check earlier in the request found no locks, a lock is then created, and execute still reports failed.
  • Mutation runs: making execute read the request memo unconditionally fails the new failed examples in both files.
  • All 44 examples pass locally on master as of 2026-10-29. RuboCop clean.

Production context

Kibana, GET /api/:version/groups/:id/merge_requests, one hour on 2026-10-29:

  • path_locks and lfs_file_locks are about 2.1% and 2.0% of the endpoint's request time. Inactive results are only about 6% of that time, so almost all of the gain is from lock-free projects.
  • path_locks is active (licensed, default-branch target) for about 80% of checks and lfs_file_locks for about 97%.
  • From a sample of requests, using mergeability_project_id on the per-MR lines: about 2.3 active LFS checks and 2.0 active path lock checks per project per request, costing about 4.6 ms and 5.1 ms per project today. Redis cache calls on this endpoint average about 0.75 ms each.
  • The per-MR check timings come from the application_json lines that share the request's correlation_id (mergeability.<check>.duration_s.values).

This is one of four changes from the investigation in #631601, each behind its own gitlab_com_derisk flag. !263467 (merged) (security policy check) and !263468 (merged) (approval check) are merged; the conflict check change is !266181 (merged).

Merge request reports

Loading
Loading