Skip to content

Fix an N+1 issue in GroupPackagesFinder

David Fernandez requested to merge 267010-improve-group-packages-finder into master

🌲 Context

We're going to fix the GroupPackagesFinder. This finder, as its name implies, is in charge of returning all Packages::Package linked to a Group through the included Projects. It can optionally include packages from sub groups. User permissions are checked.

#267010 (closed) shed some light on issues in the #group_projects_visible_to_current_user function.

Simply put, this function will make use of a .select on an ActiveRecord. This has two downsides:

  1. Eager load all the projects in memory to make them available to the .select
  2. The .select block will call Ability.allowed? which will trigger a database query on project features.

Now imagine having thousands of projects returned by the .select statement = 💥

Note that the packages feature is within the repository feature of a project. As such, the visibility of the repository feature will have a consequence if a user have access to packages or not.

What does this MR do?

The solution to the problem at hand is simply getting rid of the .select statement.

This MR:

  • Updates #group_projects_visible_to_current_user to replace the .select by the corresponding .with_feature_available_for_user scope that will properly select the projects within the SQL query
    • The result is that we keep an ActiveRecord relation = lazy loading is active throughout the execution of the finder.
  • Updates and cleans up the associated spec:
    • Add a spec example to detect the N+1 situation
    • Add a tabled based spec to check permissions. Basically, matrix between project visibility, user role and repository feature visibility.
    • Give some 💚 to the spec execution time by using let_it_be instead of let or let!

Screenshots (strongly suggested)

n / a

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Database

Explain plans

From #group_projects_visible_to_current_user

SELECT "projects".* 
FROM "projects" 
LEFT JOIN project_features ON projects.id = project_features.project_id 
WHERE "projects"."namespace_id" 
IN (WITH RECURSIVE "base_and_descendants" AS (
  (SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = 'Group' AND "namespaces"."id" = 6576322)
  UNION
  (SELECT "namespaces".* FROM "namespaces", "base_and_descendants" WHERE "namespaces"."type" = 'Group' AND "namespaces"."parent_id" = "base_and_descendants"."id")
  ) SELECT "namespaces"."id" FROM "base_and_descendants" AS "namespaces"
) 
AND (EXISTS (SELECT 1 FROM "project_authorizations" WHERE "project_authorizations"."user_id" = 3983112 AND (project_authorizations.project_id = projects.id)) OR projects.visibility_level IN (0,10,20)) AND ("project_features"."repository_access_level" > 0 OR "project_features"."repository_access_level" IS NULL)

plan: https://explain.depesz.com/s/aflt

From #packages_for_group_projects

SELECT "packages_packages".* FROM "packages_packages" WHERE "packages_packages"."project_id" IN (SELECT "projects"."id" FROM "projects" LEFT JOIN project_features ON projects.id = project_features.project_id WHERE "projects"."namespace_id" IN (WITH RECURSIVE "base_and_descendants" AS ((SELECT "namespaces".* FROM "namespaces" WHERE "namespaces"."type" = 'Group' AND "namespaces"."id" = 6576322)
UNION
(SELECT "namespaces".* FROM "namespaces", "base_and_descendants" WHERE "namespaces"."type" = 'Group' AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT "namespaces"."id" FROM "base_and_descendants" AS "namespaces") AND (EXISTS (SELECT 1 FROM "project_authorizations" WHERE "project_authorizations"."user_id" = 3983112 AND (project_authorizations.project_id = projects.id)) OR projects.visibility_level IN (0,10,20)) AND ("project_features"."repository_access_level" > 0 OR "project_features"."repository_access_level" IS NULL)) 
AND ("packages_packages"."package_type" != 4 OR "packages_packages"."name" != 'NuGet.Temporary.Package') AND "packages_packages"."version" IS NOT NULL ORDER BY created_at ASC

plan: https://explain.depesz.com/s/i7wK

Edited by David Fernandez

Merge request reports