Commit 28e6af88 authored by Sean McGivern's avatar Sean McGivern

Fix N+1 for notification recipients on private projects

If we don't call #to_a, we're relying on the members already being loaded from
elsewhere. Otherwise we'll do a separate query for each user:

    [1] pry(main)> Project.first.team.members.include?(User.first)
      Project Load (0.7ms)  SELECT  "projects".* FROM "projects"  ORDER BY "projects"."id" ASC LIMIT 1
      ↳ (pry):3
      User Load (1.8ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
      ↳ (pry):3
      User Exists (0.6ms)  SELECT  1 AS one FROM "users" INNER JOIN "project_authorizations" ON "users"."id" = "project_authorizations"."user_id" WHERE "project_authorizations"."project_id" = $1 AND "users"."id" = $2 LIMIT 1  [["project_id", 1], ["id", 1]]
      ↳ (pry):3
    => true
    [2] pry(main)> Project.first.team.members.to_a.include?(User.first)
      Project Load (12.8ms)  SELECT  "projects".* FROM "projects"  ORDER BY "projects"."id" ASC LIMIT 1
      ↳ (pry):1
      User Load (9.6ms)  SELECT "users".* FROM "users" INNER JOIN "project_authorizations" ON "users"."id" = "project_authorizations"."user_id" WHERE "project_authorizations"."project_id" = $1  [["project_id", 1]]
      ↳ (pry):1
      User Load (0.6ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
      ↳ (pry):1
    => true
parent 819ecd5f
...@@ -390,7 +390,11 @@ class ProjectPolicy < BasePolicy ...@@ -390,7 +390,11 @@ class ProjectPolicy < BasePolicy
greedy_load_subject ||= !@user.persisted? greedy_load_subject ||= !@user.persisted?
if greedy_load_subject if greedy_load_subject
project.team.members.include?(user) # We want to load all the members with one query. Calling #include? on
# project.team.members will perform a separate query for each user, unless
# project.team.members was loaded before somewhere else. Calling #to_a
# ensures it's always loaded before checking for membership.
project.team.members.to_a.include?(user)
else else
# otherwise we just make a specific query for # otherwise we just make a specific query for
# this particular user. # this particular user.
......
...@@ -10,18 +10,9 @@ describe NotificationRecipientService do ...@@ -10,18 +10,9 @@ describe NotificationRecipientService do
let(:issue) { create(:issue, project: project, assignees: [assignee]) } let(:issue) { create(:issue, project: project, assignees: [assignee]) }
let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id) } let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id) }
context 'when there are multiple watchers' do shared_examples 'no N+1 queries' do
def create_watcher
watcher = create(:user)
create(:notification_setting, source: project, user: watcher, level: :watch)
other_projects.each do |other_project|
create(:notification_setting, source: other_project, user: watcher, level: :watch)
end
end
it 'avoids N+1 queries', :request_store do it 'avoids N+1 queries', :request_store do
create_watcher create_user
service.build_new_note_recipients(note) service.build_new_note_recipients(note)
...@@ -29,30 +20,39 @@ describe NotificationRecipientService do ...@@ -29,30 +20,39 @@ describe NotificationRecipientService do
service.build_new_note_recipients(note) service.build_new_note_recipients(note)
end end
create_watcher create_user
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count) expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count)
end end
end end
context 'when there are multiple watchers' do
def create_user
watcher = create(:user)
create(:notification_setting, source: project, user: watcher, level: :watch)
other_projects.each do |other_project|
create(:notification_setting, source: other_project, user: watcher, level: :watch)
end
end
include_examples 'no N+1 queries'
end
context 'when there are multiple subscribers' do context 'when there are multiple subscribers' do
def create_subscriber def create_user
subscriber = create(:user) subscriber = create(:user)
issue.subscriptions.create(user: subscriber, project: project, subscribed: true) issue.subscriptions.create(user: subscriber, project: project, subscribed: true)
end end
it 'avoids N+1 queries' do include_examples 'no N+1 queries'
create_subscriber
service.build_new_note_recipients(note) context 'when the project is private' do
before do
control_count = ActiveRecord::QueryRecorder.new do project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
service.build_new_note_recipients(note)
end end
create_subscriber include_examples 'no N+1 queries'
expect { service.build_new_note_recipients(note) }.not_to exceed_query_limit(control_count)
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment