Avoid n+1 when fetching epics for guest users or project members on group hierarchies

What does this MR do and why?

Follow up of !80955 (merged). The first MR did remove some N+1s, however when the group hierarchy is very big, like having thousands of groups within the same level we still have timeouts.

This avoids iterating through the group objects of the hierarchy, instead does a union of queries to find authorized groups using traversal_ids. It should fix the problem described at #367868 (comment 1026975391) removing N+1 calls when the user is fetching a list of epics but has limited access like guest or authorization via project membership.

This is behind a feature flag already enabled in production :find_epics_performance_improvement.

related to #327901 (closed) and #217937 (closed)

Query to get epics

The query below comes for the worst possible scenario, for a user which is not a member of any group, but can read the hierarchy via project membership and the root group is public. I used the group id of gitlab.com.

The query takes a bit over 100ms, I believe there is room for improvement, but it is still better than timing out.

One important thing to notice is that it takes a long time on the first run on postgres.ai, the same does not happen on the staging console. I am not sure why this happens.

https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/11371/commands/40700

How to set up and validate locally

  1. Enable :find_epics_performance_improvement feature flag
  2. Copy this seed to your local environment and run it like rake db:seed_fu FILTER=36. It will create epics within a huge group hierarchy, it is a similar scenario of a group's structure from production with the same problem. You don't need to wait until it finishes, can be killed after a couple of thousand groups are created. It is possible that traversal_ids are not set for all groups after running the seed. In this case try following these instructions.
  3. Go to the top-level group of the hierarchy created by the seed file and add a guest user to it. Another option is to create a project within the hierarchy and add the user with any access level to it.
  4. With the new user try to view the top-level group epics list.

It should work fine. Turn off :find_epics_performance_improvement and you will probably see a timeout.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Felipe Cardozo

Merge request reports

Loading