Commit b15f7229 authored by Thong Kuah's avatar Thong Kuah Committed by Matthias Käppler

Set db name when creating connection with new pool size

Changelog: fixed
parent 2597de6c
...@@ -20,12 +20,7 @@ Gitlab.ee do ...@@ -20,12 +20,7 @@ Gitlab.ee do
end end
end end
db_config = Gitlab::Database.main.config || ActiveRecord::Base.establish_connection(Gitlab::Database.main.db_config_with_default_pool_size)
Rails.application.config.database_configuration[Rails.env]
ActiveRecord::Base.establish_connection(
db_config.merge(pool: Gitlab::Database.main.default_pool_size)
)
Gitlab.ee do Gitlab.ee do
if Gitlab::Runtime.sidekiq? && Gitlab::Geo.geo_database_configured? if Gitlab::Runtime.sidekiq? && Gitlab::Geo.geo_database_configured?
......
...@@ -75,6 +75,17 @@ module Gitlab ...@@ -75,6 +75,17 @@ module Gitlab
adapter_name.casecmp('postgresql') == 0 adapter_name.casecmp('postgresql') == 0
end end
def db_config_with_default_pool_size
db_config_object = scope.connection_db_config
config = db_config_object.configuration_hash.merge(pool: default_pool_size)
ActiveRecord::DatabaseConfigurations::HashConfig.new(
db_config_object.env_name,
db_config_object.name,
config
)
end
# Disables prepared statements for the current database connection. # Disables prepared statements for the current database connection.
def disable_prepared_statements def disable_prepared_statements
scope.establish_connection(config.merge(prepared_statements: false)) scope.establish_connection(config.merge(prepared_statements: false))
......
...@@ -21,6 +21,14 @@ RSpec.describe 'Database config initializer' do ...@@ -21,6 +21,14 @@ RSpec.describe 'Database config initializer' do
let(:max_threads) { 8 } let(:max_threads) { 8 }
it 'retains the correct database name for the connection' do
previous_db_name = Gitlab::Database.main.scope.connection.pool.db_config.name
subject
expect(Gitlab::Database.main.scope.connection.pool.db_config.name).to eq(previous_db_name)
end
context 'when no custom headroom is specified' do context 'when no custom headroom is specified' do
it 'sets the pool size based on the number of worker threads' do it 'sets the pool size based on the number of worker threads' do
old = ActiveRecord::Base.connection_db_config.pool old = ActiveRecord::Base.connection_db_config.pool
......
...@@ -127,6 +127,20 @@ RSpec.describe Gitlab::Database::Connection do ...@@ -127,6 +127,20 @@ RSpec.describe Gitlab::Database::Connection do
end end
end end
describe '#db_config_with_default_pool_size' do
it 'returns db_config with our default pool size' do
allow(connection).to receive(:default_pool_size).and_return(9)
expect(connection.db_config_with_default_pool_size.pool).to eq(9)
end
it 'returns db_config with the correct database name' do
db_name = connection.scope.connection.pool.db_config.name
expect(connection.db_config_with_default_pool_size.name).to eq(db_name)
end
end
describe '#disable_prepared_statements' do describe '#disable_prepared_statements' do
around do |example| around do |example|
original_config = ::Gitlab::Database.main.config original_config = ::Gitlab::Database.main.config
......
# frozen_string_literal: true # frozen_string_literal: true
RSpec.shared_examples 'store ActiveRecord info in RequestStore' do |db_role| RSpec.shared_examples 'store ActiveRecord info in RequestStore' do |db_role|
let(:db_config_name) { ::Gitlab::Database.db_config_name(ApplicationRecord.connection) } let(:db_config_name) { ::Gitlab::Database.db_config_names.first }
let(:expected_payload_defaults) do let(:expected_payload_defaults) do
metrics = metrics =
...@@ -14,13 +14,21 @@ RSpec.shared_examples 'store ActiveRecord info in RequestStore' do |db_role| ...@@ -14,13 +14,21 @@ RSpec.shared_examples 'store ActiveRecord info in RequestStore' do |db_role|
end end
end end
def transform_hash(hash, another_hash)
another_hash.each do |key, value|
raise "Unexpected key: #{key}" unless hash[key]
end
hash.merge(another_hash)
end
it 'prevents db counters from leaking to the next transaction' do it 'prevents db counters from leaking to the next transaction' do
2.times do 2.times do
Gitlab::WithRequestStore.with_request_store do Gitlab::WithRequestStore.with_request_store do
subscriber.sql(event) subscriber.sql(event)
expected = if db_role == :primary expected = if db_role == :primary
expected_payload_defaults.merge({ transform_hash(expected_payload_defaults, {
db_count: record_query ? 1 : 0, db_count: record_query ? 1 : 0,
db_write_count: record_write_query ? 1 : 0, db_write_count: record_write_query ? 1 : 0,
db_cached_count: record_cached_query ? 1 : 0, db_cached_count: record_cached_query ? 1 : 0,
...@@ -36,7 +44,7 @@ RSpec.shared_examples 'store ActiveRecord info in RequestStore' do |db_role| ...@@ -36,7 +44,7 @@ RSpec.shared_examples 'store ActiveRecord info in RequestStore' do |db_role|
"db_primary_#{db_config_name}_wal_cached_count": record_wal_query && record_cached_query ? 1 : 0 "db_primary_#{db_config_name}_wal_cached_count": record_wal_query && record_cached_query ? 1 : 0
}) })
elsif db_role == :replica elsif db_role == :replica
expected_payload_defaults.merge({ transform_hash(expected_payload_defaults, {
db_count: record_query ? 1 : 0, db_count: record_query ? 1 : 0,
db_write_count: record_write_query ? 1 : 0, db_write_count: record_write_query ? 1 : 0,
db_cached_count: record_cached_query ? 1 : 0, db_cached_count: record_cached_query ? 1 : 0,
......
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