Commit a3ef0008 authored by Thong Kuah's avatar Thong Kuah

Do not stick to primary after recording last_activity_on

This reduces requests using the primary DB by the number of users using
the site every day (since last_activity_on is update at most once per
day per user).
parent c1757f06
......@@ -37,3 +37,5 @@ module Users
end
end
end
Users::ActivityService.prepend_ee_mod
# frozen_string_literal: true
module EE
module Users
module ActivityService
extend ::Gitlab::Utils::Override
private
override :record_activity
def record_activity
::Gitlab::Database::LoadBalancing::Session.without_sticky_writes { super }
end
end
end
end
---
title: Do not stick to primary after recording User#last_activity_on
merge_request: 57212
author:
type: performance
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Users::ActivityService, '#execute', :request_store, :redis, :clean_gitlab_redis_shared_state do
let(:user) { create(:user, last_activity_on: last_activity_on) }
context 'when last activity is in the past' do
let(:user) { create(:user, last_activity_on: Date.today - 1.week) }
context 'database load balancing is configured' do
before do
# Do not pollute AR for other tests, but rather simulate effect of configure_proxy.
allow(ActiveRecord::Base.singleton_class).to receive(:prepend)
::Gitlab::Database::LoadBalancing.configure_proxy
allow(ActiveRecord::Base).to receive(:connection).and_return(::Gitlab::Database::LoadBalancing.proxy)
end
let(:service) do
service = described_class.new(user)
::Gitlab::Database::LoadBalancing::Session.clear_session
service
end
it 'does not stick to primary' do
expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_performed_write
service.execute
expect(user.last_activity_on).to eq(Date.today)
expect(::Gitlab::Database::LoadBalancing::Session.current).to be_performed_write
expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_using_primary
end
after do
::Gitlab::Database::LoadBalancing.clear_configuration
end
end
context 'database load balancing is not configured' do
let(:service) { described_class.new(user) }
it 'updates user without error' do
service.execute
expect(user.last_activity_on).to eq(Date.today)
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