Performance regression in GET /api/:version/groups/:id/merge_requests
Summary
The GET /api/:version/groups/:id/merge_requests endpoint is experiencing a performance regression caused by !217325 (merged).
The Review Roulette stopped working due to 500 errors: https://log.gprd.gitlab.net/app/r/s/PN4zq. Sample pipeline: https://gitlab.com/gitlab-org/gitlab-roulette/-/jobs/13067629703
Root Cause
Merge request !217325 (merged) introduced two new methods to MergeRequest:
committer_ids_to_filter_from_approverscommitters_to_filter_from_approvers
These methods are called during approval rule filtering when the approval_policy_rules_individual_approvers_filtering feature flag is enabled. The issue is:
- These methods call
committers()withlazy: true, which triggerspreload_commits_metadata() - This preloading happens for every merge request in the API response
- The ActiveRecord preloader inefficiently loads association data, causing expensive reflection calls in
activerecord/reflection.rb:930:in 'association_primary_key' - This creates N+1 query patterns and excessive preloading overhead
Backtrace Evidence
activerecord (7.2.3) lib/active_record/reflection.rb:930:in `association_primary_key'
activerecord (7.2.3) lib/active_record/associations/preloader/association.rb:162:in `association_key_name'
activerecord (7.2.3) lib/active_record/associations/preloader/association.rb:205:in `block in load_records'
...
app/models/merge_request.rb:907:in `preload_commits_metadata'
app/models/merge_request.rb:913:in `block in committers'
app/models/merge_request.rb:930:in `committers_to_filter_from_approvers'
ee/app/models/approval_wrapped_rule.rb:251:in `filter_committers_for_rule'
Impact
- Slow API response times for endpoints listing merge requests
- Excessive database queries and reflection calls
- Performance degradation scales with the number of merge requests and approval rules
Related Issues
- Related to !217325 (merged) - Filter approval rule approvers individually
- Related to #574854 - Original issue that MR217325 was addressing
Edited by 🤖 GitLab Bot 🤖