Commit 6a9fa9a5 authored by pbair's avatar pbair

Use lazy init to correctly populate db list

Populate the list of databases in in Gitlab::Database lazily, and
ensure additional connection pools are connected prior to that
initialization in the boot process.
parent 1e7a2d16
...@@ -49,18 +49,23 @@ module Gitlab ...@@ -49,18 +49,23 @@ module Gitlab
# It does not include the default public schema # It does not include the default public schema
EXTRA_SCHEMAS = [DYNAMIC_PARTITIONS_SCHEMA, STATIC_PARTITIONS_SCHEMA].freeze EXTRA_SCHEMAS = [DYNAMIC_PARTITIONS_SCHEMA, STATIC_PARTITIONS_SCHEMA].freeze
DATABASES = ActiveRecord::Base
.connection_handler
.connection_pools
.each_with_object({}) do |pool, hash|
hash[pool.db_config.name.to_sym] = Connection.new(pool.connection_klass)
end
.freeze
PRIMARY_DATABASE_NAME = ActiveRecord::Base.connection_db_config.name.to_sym PRIMARY_DATABASE_NAME = ActiveRecord::Base.connection_db_config.name.to_sym
def self.database_base_models
@database_base_models ||= {
main: ::ApplicationRecord,
ci: ::Ci::CiDatabaseRecord.connection_class? ? ::Ci::CiDatabaseRecord : nil
}.compact.freeze
end
def self.databases
@databases ||= database_base_models
.transform_values { |connection_class| Connection.new(connection_class) }
.freeze
end
def self.main def self.main
DATABASES[PRIMARY_DATABASE_NAME] databases[PRIMARY_DATABASE_NAME]
end end
# We configure the database connection pool size automatically based on the # We configure the database connection pool size automatically based on the
...@@ -99,7 +104,7 @@ module Gitlab ...@@ -99,7 +104,7 @@ module Gitlab
def self.check_postgres_version_and_print_warning def self.check_postgres_version_and_print_warning
return if Gitlab::Runtime.rails_runner? return if Gitlab::Runtime.rails_runner?
DATABASES.each do |name, connection| databases.each do |name, connection|
next if connection.postgresql_minimum_supported_version? next if connection.postgresql_minimum_supported_version?
Kernel.warn ERB.new(Rainbow.new.wrap(<<~EOS).red).result Kernel.warn ERB.new(Rainbow.new.wrap(<<~EOS).red).result
...@@ -111,7 +116,7 @@ module Gitlab ...@@ -111,7 +116,7 @@ module Gitlab
 ███ ███  ██  ██ ██  ██ ██   ████ ██ ██   ████  ██████    ███ ███  ██  ██ ██  ██ ██   ████ ██ ██   ████  ██████  
****************************************************************************** ******************************************************************************
You are using PostgreSQL <%= Gitlab::Database.main.version %> for the #{name} database, but PostgreSQL >= <%= Gitlab::Database::MINIMUM_POSTGRES_VERSION %> You are using PostgreSQL #{connection.version} for the #{name} database, but PostgreSQL >= <%= Gitlab::Database::MINIMUM_POSTGRES_VERSION %>
is required for this version of GitLab. is required for this version of GitLab.
<% if Rails.env.development? || Rails.env.test? %> <% if Rails.env.development? || Rails.env.test? %>
If using gitlab-development-kit, please find the relevant steps here: If using gitlab-development-kit, please find the relevant steps here:
......
...@@ -31,7 +31,7 @@ module Gitlab ...@@ -31,7 +31,7 @@ module Gitlab
def set_data_consistency_locations!(job) def set_data_consistency_locations!(job)
# Once we add support for multiple databases to our load balancer, we would use something like this: # Once we add support for multiple databases to our load balancer, we would use something like this:
# job['wal_locations'] = Gitlab::Database::DATABASES.transform_values do |connection| # job['wal_locations'] = Gitlab::Database.databases.transform_values do |connection|
# connection.load_balancer.primary_write_location # connection.load_balancer.primary_write_location
# end # end
# #
......
...@@ -92,7 +92,7 @@ module Gitlab ...@@ -92,7 +92,7 @@ module Gitlab
def all_databases_has_replica_caught_up?(wal_locations) def all_databases_has_replica_caught_up?(wal_locations)
wal_locations.all? do |_config_name, location| wal_locations.all? do |_config_name, location|
# Once we add support for multiple databases to our load balancer, we would use something like this: # Once we add support for multiple databases to our load balancer, we would use something like this:
# Gitlab::Database::DATABASES[config_name].load_balancer.select_up_to_date_host(location) # Gitlab::Database.databases[config_name].load_balancer.select_up_to_date_host(location)
load_balancer.select_up_to_date_host(location) load_balancer.select_up_to_date_host(location)
end end
end end
...@@ -101,7 +101,7 @@ module Gitlab ...@@ -101,7 +101,7 @@ module Gitlab
# Once we add support for multiple databases to our load balancer, we would use something like this: # Once we add support for multiple databases to our load balancer, we would use something like this:
# connection.load_balancer.primary_write_location # connection.load_balancer.primary_write_location
# #
# Gitlab::Database::DATABASES.values.each do |connection| # Gitlab::Database.databases.values.each do |connection|
# connection.load_balancer.release_host # connection.load_balancer.release_host
# end # end
load_balancer.release_host load_balancer.release_host
......
...@@ -155,7 +155,7 @@ module Gitlab ...@@ -155,7 +155,7 @@ module Gitlab
end end
def pg_wal_lsn_diff(connection_name) def pg_wal_lsn_diff(connection_name)
Gitlab::Database::DATABASES[connection_name].pg_wal_lsn_diff(job_wal_locations[connection_name], existing_wal_locations[connection_name]) Gitlab::Database.databases[connection_name].pg_wal_lsn_diff(job_wal_locations[connection_name], existing_wal_locations[connection_name])
end end
def strategy def strategy
......
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