discourse/spec/jobs/automatic_group_membership_spec.rb
Alan Guo Xiang Tan 7c321d3aad
PERF: Update Group#user_count counter cache outside DB transaction (#19256)
While load testing our user creation code path in production, we
identified that executing the DB statement to update the `Group#user_count` column within a
transaction is creating a bottleneck for us. This is because the
creation of a user and addition of the user to the relevant groups are
done in a transaction. When we execute the DB statement to update
`Group#user_count` for the relevant group, a row level lock is held
until the transaction completes. This row level lock acts like a global
lock when the server is creating users that will be added to the same
group in quick succession.

Instead of updating the counter cache within a transaction which the
default ActiveRecord `counter_cache` option does, we simply update the
counter cache outside of the committing transaction.

Co-authored-by: Rafael dos Santos Silva <xfalcox@gmail.com>

Co-authored-by: Rafael dos Santos Silva <xfalcox@gmail.com>
2022-11-30 11:52:08 -03:00

52 lines
1.8 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Jobs::AutomaticGroupMembership do
it "raises an error when the group id is missing" do
expect { Jobs::AutomaticGroupMembership.new.execute({}) }.to raise_error(Discourse::InvalidParameters)
end
it "updates the membership" do
user1 = Fabricate(:user, email: "no@bar.com")
user2 = Fabricate(:user, email: "no@wat.com")
user3 = Fabricate(:user, email: "noo@wat.com", staged: true)
EmailToken.confirm(Fabricate(:email_token, user: user3).token)
user4 = Fabricate(:user, email: "yes@wat.com")
EmailToken.confirm(Fabricate(:email_token, user: user4).token)
user5 = Fabricate(:user, email: "sso@wat.com")
user5.create_single_sign_on_record(external_id: 123, external_email: "hacker@wat.com", last_payload: "")
user6 = Fabricate(:user, email: "sso2@wat.com")
user6.create_single_sign_on_record(external_id: 456, external_email: "sso2@wat.com", last_payload: "")
group = Fabricate(:group, automatic_membership_email_domains: "wat.com")
automatic = nil
called = false
blk = Proc.new do |_u, _g, options|
automatic = options[:automatic]
called = true
end
begin
DiscourseEvent.on(:user_added_to_group, &blk)
Jobs::AutomaticGroupMembership.new.execute(group_id: group.id)
expect(automatic).to eql(true)
expect(called).to eq(true)
ensure
DiscourseEvent.off(:user_added_to_group, &blk)
end
group.reload
expect(group.users.include?(user1)).to eq(false)
expect(group.users.include?(user2)).to eq(false)
expect(group.users.include?(user3)).to eq(false)
expect(group.users.include?(user4)).to eq(true)
expect(group.users.include?(user5)).to eq(false)
expect(group.users.include?(user6)).to eq(true)
expect(group.user_count).to eq(2)
end
end