Commit 95175efd authored by Adam Hegyi's avatar Adam Hegyi

Merge branch 'reconfigure-all-database-models' into 'master'

Make `LoadBalancer` to configure pool sizes of all classes

See merge request gitlab-org/gitlab!72989
parents dfa79015 fcc216a2
...@@ -10,7 +10,9 @@ Gitlab.ee do ...@@ -10,7 +10,9 @@ Gitlab.ee do
end end
end end
ActiveRecord::Base.establish_connection(Gitlab::Database.main.db_config_with_default_pool_size) unless Gitlab::Utils.to_boolean(ENV["GITLAB_LB_CONFIGURE_CONNECTION"], default: true)
ActiveRecord::Base.establish_connection(Gitlab::Database.main.db_config_with_default_pool_size)
end
Gitlab.ee do Gitlab.ee do
if Gitlab::Runtime.sidekiq? && Gitlab::Geo.geo_database_configured? if Gitlab::Runtime.sidekiq? && Gitlab::Geo.geo_database_configured?
......
...@@ -59,6 +59,7 @@ module Gitlab ...@@ -59,6 +59,7 @@ module Gitlab
adapter_name.casecmp('postgresql') == 0 adapter_name.casecmp('postgresql') == 0
end end
# TODO: To be removed with GITLAB_LB_CONFIGURE_CONNECTION
def db_config_with_default_pool_size def db_config_with_default_pool_size
db_config_object = scope.connection_db_config db_config_object = scope.connection_db_config
config = db_config_object config = db_config_object
...@@ -72,20 +73,6 @@ module Gitlab ...@@ -72,20 +73,6 @@ module Gitlab
) )
end end
# Disables prepared statements for the current database connection.
def disable_prepared_statements
db_config_object = scope.connection_db_config
config = db_config_object.configuration_hash.merge(prepared_statements: false)
hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(
db_config_object.env_name,
db_config_object.name,
config
)
scope.establish_connection(hash_config)
end
# Check whether the underlying database is in read-only mode # Check whether the underlying database is in read-only mode
def db_read_only? def db_read_only?
pg_is_in_recovery = pg_is_in_recovery =
......
...@@ -14,20 +14,30 @@ module Gitlab ...@@ -14,20 +14,30 @@ module Gitlab
end end
def setup def setup
disable_prepared_statements configure_connection
setup_load_balancer setup_load_balancer
setup_service_discovery setup_service_discovery
end end
def disable_prepared_statements def configure_connection
db_config_object = @model.connection_db_config db_config_object = @model.connection_db_config
config =
db_config_object.configuration_hash.merge(prepared_statements: false) hash =
if Gitlab::Utils.to_boolean(ENV["GITLAB_LB_CONFIGURE_CONNECTION"], default: true)
db_config_object.configuration_hash.merge(
prepared_statements: false,
pool: Gitlab::Database.default_pool_size
)
else
db_config_object.configuration_hash.merge(
prepared_statements: false
)
end
hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new( hash_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(
db_config_object.env_name, db_config_object.env_name,
db_config_object.name, db_config_object.name,
config hash
) )
@model.establish_connection(hash_config) @model.establish_connection(hash_config)
......
...@@ -9,6 +9,7 @@ RSpec.describe 'Database config initializer', :reestablished_active_record_base ...@@ -9,6 +9,7 @@ RSpec.describe 'Database config initializer', :reestablished_active_record_base
before do before do
allow(Gitlab::Runtime).to receive(:max_threads).and_return(max_threads) allow(Gitlab::Runtime).to receive(:max_threads).and_return(max_threads)
stub_env('GITLAB_LB_CONFIGURE_CONNECTION', 'false')
end end
let(:max_threads) { 8 } let(:max_threads) { 8 }
...@@ -41,6 +42,16 @@ RSpec.describe 'Database config initializer', :reestablished_active_record_base ...@@ -41,6 +42,16 @@ RSpec.describe 'Database config initializer', :reestablished_active_record_base
expect(ActiveRecord::Base.connection_db_config.pool).to eq(18) expect(ActiveRecord::Base.connection_db_config.pool).to eq(18)
end end
context 'when GITLAB_LB_CONFIGURE_CONNECTION=true' do
before do
stub_env('GITLAB_LB_CONFIGURE_CONNECTION', 'true')
end
it 'does not overwrite custom pool settings' do
expect { subject }.not_to change { ActiveRecord::Base.connection_db_config.pool }
end
end
end end
context "when specifying headroom through an ENV variable" do context "when specifying headroom through an ENV variable" do
......
...@@ -126,39 +126,6 @@ RSpec.describe Gitlab::Database::Connection do ...@@ -126,39 +126,6 @@ RSpec.describe Gitlab::Database::Connection do
end end
end end
describe '#disable_prepared_statements', :reestablished_active_record_base do
it 'disables prepared statements' do
connection.scope.establish_connection(
::Gitlab::Database.main.config.merge(prepared_statements: true)
)
expect(connection.scope.connection.prepared_statements).to eq(true)
connection.disable_prepared_statements
expect(connection.scope.connection.prepared_statements).to eq(false)
end
it 'retains the connection name' do
connection.disable_prepared_statements
expect(connection.scope.connection_db_config.name).to eq('main')
end
context 'with dynamic connection pool size' do
before do
connection.scope.establish_connection(connection.config.merge(pool: 7))
end
it 'retains the set pool size' do
connection.disable_prepared_statements
expect(connection.scope.connection.prepared_statements).to eq(false)
expect(connection.scope.connection.pool.size).to eq(7)
end
end
end
describe '#db_read_only?' do describe '#db_read_only?' do
it 'detects a read-only database' do it 'detects a read-only database' do
allow(connection.scope.connection) allow(connection.scope.connection)
......
...@@ -7,7 +7,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do ...@@ -7,7 +7,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do
it 'sets up the load balancer' do it 'sets up the load balancer' do
setup = described_class.new(ActiveRecord::Base) setup = described_class.new(ActiveRecord::Base)
expect(setup).to receive(:disable_prepared_statements) expect(setup).to receive(:configure_connection)
expect(setup).to receive(:setup_load_balancer) expect(setup).to receive(:setup_load_balancer)
expect(setup).to receive(:setup_service_discovery) expect(setup).to receive(:setup_service_discovery)
...@@ -15,11 +15,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do ...@@ -15,11 +15,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do
end end
end end
describe '#disable_prepared_statements' do describe '#configure_connection' do
it 'disables prepared statements and reconnects to the database' do it 'configures pool, prepared statements and reconnects to the database' do
config = double( config = double(
:config, :config,
configuration_hash: { host: 'localhost' }, configuration_hash: { host: 'localhost', pool: 2, prepared_statements: true },
env_name: 'test', env_name: 'test',
name: 'main' name: 'main'
) )
...@@ -27,7 +27,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do ...@@ -27,7 +27,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do
expect(ActiveRecord::DatabaseConfigurations::HashConfig) expect(ActiveRecord::DatabaseConfigurations::HashConfig)
.to receive(:new) .to receive(:new)
.with('test', 'main', { host: 'localhost', prepared_statements: false }) .with('test', 'main', {
host: 'localhost',
prepared_statements: false,
pool: Gitlab::Database.default_pool_size
})
.and_call_original .and_call_original
# HashConfig doesn't implement its own #==, so we can't directly compare # HashConfig doesn't implement its own #==, so we can't directly compare
...@@ -36,7 +40,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do ...@@ -36,7 +40,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::Setup do
.to receive(:establish_connection) .to receive(:establish_connection)
.with(an_instance_of(ActiveRecord::DatabaseConfigurations::HashConfig)) .with(an_instance_of(ActiveRecord::DatabaseConfigurations::HashConfig))
described_class.new(model).disable_prepared_statements described_class.new(model).configure_connection
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