Commit 51ba2d76 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Extract DB load balancing setup in specs

Adds an Rspec tag that enables DB load balancing for the specified
context
parent ea8ccad6
...@@ -49,13 +49,11 @@ RSpec.describe Gitlab::Checks::MatchingMergeRequest do ...@@ -49,13 +49,11 @@ RSpec.describe Gitlab::Checks::MatchingMergeRequest do
end end
end end
context 'with load balancing enabled', :request_store, :redis do context 'with load balancing enabled', :db_load_balancing do
let(:session) { ::Gitlab::Database::LoadBalancing::Session.current } let(:session) { ::Gitlab::Database::LoadBalancing::Session.current }
let(:all_caught_up) { true } let(:all_caught_up) { true }
before do before do
allow(ActiveRecord::Base).to receive(:load_balancing_proxy)
expect(::Gitlab::Database::LoadBalancing).to receive(:enable?).at_least(:once).and_return(true)
allow(::Gitlab::Database::LoadBalancing::Sticking).to receive(:all_caught_up?).and_return(all_caught_up) allow(::Gitlab::Database::LoadBalancing::Sticking).to receive(:all_caught_up?).and_return(all_caught_up)
expect(::Gitlab::Database::LoadBalancing::Sticking).to receive(:select_valid_host).with(:project, project.id).and_call_original expect(::Gitlab::Database::LoadBalancing::Sticking).to receive(:select_valid_host).with(:project, project.id).and_call_original
......
...@@ -364,7 +364,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -364,7 +364,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do
# - In each test, we listen to the SQL queries (via sql.active_record # - In each test, we listen to the SQL queries (via sql.active_record
# instrumentation) while triggering real queries from the defined model. # instrumentation) while triggering real queries from the defined model.
# - We assert the desinations (replica/primary) of the queries in order. # - We assert the desinations (replica/primary) of the queries in order.
describe 'LoadBalancing integration tests', :delete do describe 'LoadBalancing integration tests', :db_load_balancing, :delete do
before(:all) do before(:all) do
ActiveRecord::Schema.define do ActiveRecord::Schema.define do
create_table :load_balancing_test, force: true do |t| create_table :load_balancing_test, force: true do |t|
...@@ -379,30 +379,14 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -379,30 +379,14 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
end end
shared_context 'LoadBalancing setup' do let(:model) do
let(:development_db_config) { ActiveRecord::Base.configurations.configs_for(env_name: 'development').first.configuration_hash } Class.new(ApplicationRecord) do
let(:hosts) { [development_db_config[:host]] } self.table_name = "load_balancing_test"
let(:model) do
Class.new(ApplicationRecord) do
self.table_name = "load_balancing_test"
end
end end
end
before do before do
# Preloading testing class model.singleton_class.prepend ::Gitlab::Database::LoadBalancing::ActiveRecordProxy
model.singleton_class.prepend ::Gitlab::Database::LoadBalancing::ActiveRecordProxy
# Setup load balancing
allow(ActiveRecord::Base).to receive(:load_balancing_proxy).and_return(
::Gitlab::Database::LoadBalancing::ConnectionProxy.new(hosts)
)
original_db_config = Gitlab::Database.main.config
modified_db_config = original_db_config.merge(load_balancing: { hosts: hosts })
allow(Gitlab::Database.main).to receive(:config).and_return(modified_db_config)
::Gitlab::Database::LoadBalancing::Session.clear_session
end
end end
where(:queries, :include_transaction, :expected_results) do where(:queries, :include_transaction, :expected_results) do
...@@ -713,8 +697,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -713,8 +697,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
with_them do with_them do
include_context 'LoadBalancing setup'
it 'redirects queries to the right roles' do it 'redirects queries to the right roles' do
roles = [] roles = []
...@@ -783,8 +765,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -783,8 +765,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
with_them do with_them do
include_context 'LoadBalancing setup'
it 'redirects queries to the right roles' do it 'redirects queries to the right roles' do
roles = [] roles = []
...@@ -803,8 +783,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -803,8 +783,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
context 'a write inside a transaction inside fallback_to_replicas_for_ambiguous_queries block' do context 'a write inside a transaction inside fallback_to_replicas_for_ambiguous_queries block' do
include_context 'LoadBalancing setup'
it 'raises an exception' do it 'raises an exception' do
expect do expect do
::Gitlab::Database::LoadBalancing::Session.current.fallback_to_replicas_for_ambiguous_queries do ::Gitlab::Database::LoadBalancing::Session.current.fallback_to_replicas_for_ambiguous_queries do
......
...@@ -291,12 +291,7 @@ RSpec.describe Gitlab::SidekiqLogging::StructuredLogger do ...@@ -291,12 +291,7 @@ RSpec.describe Gitlab::SidekiqLogging::StructuredLogger do
include_examples 'performs database queries' include_examples 'performs database queries'
end end
context 'when load balancing is enabled' do context 'when load balancing is enabled', :db_load_balancing do
before do
allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true)
allow(ActiveRecord::Base).to receive(:load_balancing_proxy)
end
let(:db_config_name) { ::Gitlab::Database.db_config_name(ApplicationRecord.connection) } let(:db_config_name) { ::Gitlab::Database.db_config_name(ApplicationRecord.connection) }
let(:expected_end_payload_with_db) do let(:expected_end_payload_with_db) do
......
...@@ -344,13 +344,9 @@ RSpec.describe Ci::Build do ...@@ -344,13 +344,9 @@ RSpec.describe Ci::Build do
end end
describe '#stick_build_if_status_changed' do describe '#stick_build_if_status_changed' do
it 'sticks the build if the status changed' do it 'sticks the build if the status changed', :db_load_balancing do
job = create(:ci_build, :pending) job = create(:ci_build, :pending)
allow(ActiveRecord::Base).to receive(:load_balancing_proxy)
allow(Gitlab::Database::LoadBalancing).to receive(:enable?)
.and_return(true)
expect(Gitlab::Database::LoadBalancing::Sticking).to receive(:stick) expect(Gitlab::Database::LoadBalancing::Sticking).to receive(:stick)
.with(:build, job.id) .with(:build, job.id)
......
...@@ -133,12 +133,9 @@ RSpec.describe ProjectFeatureUsage, type: :model do ...@@ -133,12 +133,9 @@ RSpec.describe ProjectFeatureUsage, type: :model do
subject { project.feature_usage } subject { project.feature_usage }
context 'database load balancing is configured' do context 'database load balancing is configured', :db_load_balancing do
before do before do
# Do not pollute AR for other tests, but rather simulate effect of configure_proxy. allow(ActiveRecord::Base).to receive(:connection).and_return(::Gitlab::Database::LoadBalancing.proxy)
allow(ActiveRecord::Base).to receive(:load_balancing_proxy=)
proxy = ::Gitlab::Database::LoadBalancing.configure_proxy
allow(ActiveRecord::Base).to receive(:connection).and_return(proxy)
::Gitlab::Database::LoadBalancing::Session.clear_session ::Gitlab::Database::LoadBalancing::Session.clear_session
end end
......
...@@ -14,14 +14,11 @@ module Ci ...@@ -14,14 +14,11 @@ module Ci
let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) } let!(:pending_job) { create(:ci_build, :pending, :queued, pipeline: pipeline) }
describe '#execute' do describe '#execute' do
context 'checks database loadbalancing stickiness' do context 'checks database loadbalancing stickiness', :db_load_balancing do
subject { described_class.new(shared_runner).execute } subject { described_class.new(shared_runner).execute }
before do before do
project.update!(shared_runners_enabled: false) project.update!(shared_runners_enabled: false)
allow(ActiveRecord::Base).to receive(:load_balancing_proxy)
allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true)
end end
it 'result is valid if replica did caught-up' do it 'result is valid if replica did caught-up' do
......
...@@ -85,18 +85,15 @@ RSpec.describe Users::ActivityService do ...@@ -85,18 +85,15 @@ RSpec.describe Users::ActivityService do
end end
end end
context 'with DB Load Balancing', :request_store, :redis, :clean_gitlab_redis_shared_state do context 'with DB Load Balancing' do
let(:user) { create(:user, last_activity_on: last_activity_on) } let(:user) { create(:user, last_activity_on: last_activity_on) }
context 'when last activity is in the past' do context 'when last activity is in the past' do
let(:user) { create(:user, last_activity_on: Date.today - 1.week) } let(:user) { create(:user, last_activity_on: Date.today - 1.week) }
context 'database load balancing is configured' do context 'database load balancing is configured', :db_load_balancing do
before do before do
# Do not pollute AR for other tests, but rather simulate effect of configure_proxy. allow(ActiveRecord::Base).to receive(:connection).and_return(::Gitlab::Database::LoadBalancing.proxy)
allow(ActiveRecord::Base).to receive(:load_balancing_proxy=)
proxy = ::Gitlab::Database::LoadBalancing.configure_proxy
allow(ActiveRecord::Base).to receive(:connection).and_return(proxy)
end end
let(:service) do let(:service) do
......
# frozen_string_literal: true
RSpec.configure do |config|
config.before(:each, :db_load_balancing) do
allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true)
proxy = ::Gitlab::Database::LoadBalancing::ConnectionProxy.new([Gitlab::Database.main.config['host']])
allow(ActiveRecord::Base).to receive(:load_balancing_proxy).and_return(proxy)
::Gitlab::Database::LoadBalancing::Session.clear_session
redis_shared_state_cleanup!
end
config.after(:each, :db_load_balancing) do
::Gitlab::Database::LoadBalancing::Session.clear_session
redis_shared_state_cleanup!
end
end
...@@ -44,12 +44,7 @@ RSpec.describe AuthorizedProjectUpdate::UserRefreshFromReplicaWorker do ...@@ -44,12 +44,7 @@ RSpec.describe AuthorizedProjectUpdate::UserRefreshFromReplicaWorker do
end end
end end
context 'with load balancing enabled' do context 'with load balancing enabled', :db_load_balancing do
before do
allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true)
allow(ActiveRecord::Base).to receive(:load_balancing_proxy)
end
it 'reads from the replica database' do it 'reads from the replica database' do
expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_replicas_for_read_queries).and_call_original expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_replicas_for_read_queries).and_call_original
......
...@@ -156,12 +156,7 @@ RSpec.describe ContainerExpirationPolicyWorker do ...@@ -156,12 +156,7 @@ RSpec.describe ContainerExpirationPolicyWorker do
subject subject
end end
context 'with load balancing enabled' do context 'with load balancing enabled', :db_load_balancing do
before do
allow(Gitlab::Database::LoadBalancing).to receive(:enable?).and_return(true)
allow(ActiveRecord::Base).to receive(:load_balancing_proxy)
end
it 'reads the counts from the replica' do it 'reads the counts from the replica' do
expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_replicas_for_read_queries).and_call_original expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_replicas_for_read_queries).and_call_original
......
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