2021-05-27 06:10:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module LoadBalancing
|
|
|
|
# The exceptions raised for connection errors.
|
2021-09-21 18:11:18 +00:00
|
|
|
CONNECTION_ERRORS = [
|
|
|
|
PG::ConnectionBad,
|
|
|
|
PG::ConnectionDoesNotExist,
|
|
|
|
PG::ConnectionException,
|
|
|
|
PG::ConnectionFailure,
|
|
|
|
PG::UnableToSend,
|
|
|
|
# During a failover this error may be raised when
|
|
|
|
# writing to a primary.
|
|
|
|
PG::ReadOnlySqlTransaction,
|
|
|
|
# This error is raised when we can't connect to the database in the
|
|
|
|
# first place (e.g. it's offline or the hostname is incorrect).
|
|
|
|
ActiveRecord::ConnectionNotEstablished
|
|
|
|
].freeze
|
2021-05-27 06:10:47 +00:00
|
|
|
|
2021-10-11 15:13:01 +00:00
|
|
|
def self.base_models
|
2022-07-19 06:10:05 +00:00
|
|
|
@base_models ||= ::Gitlab::Database.database_base_models_using_load_balancing.values.freeze
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
2021-10-11 15:13:01 +00:00
|
|
|
def self.each_load_balancer
|
|
|
|
return to_enum(__method__) unless block_given?
|
2021-05-27 06:10:47 +00:00
|
|
|
|
2021-10-11 15:13:01 +00:00
|
|
|
base_models.each do |model|
|
2021-11-09 06:13:00 +00:00
|
|
|
yield model.load_balancer
|
2021-10-11 15:13:01 +00:00
|
|
|
end
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
2021-11-22 12:10:20 +00:00
|
|
|
def self.primary_only?
|
|
|
|
each_load_balancer.all?(&:primary_only?)
|
|
|
|
end
|
|
|
|
|
2021-10-11 15:13:01 +00:00
|
|
|
def self.release_hosts
|
|
|
|
each_load_balancer(&:release_host)
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
DB_ROLES = [
|
|
|
|
ROLE_PRIMARY = :primary,
|
|
|
|
ROLE_REPLICA = :replica,
|
|
|
|
ROLE_UNKNOWN = :unknown
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
# Returns the role (primary/replica) of the database the connection is
|
2021-09-15 12:11:13 +00:00
|
|
|
# connecting to.
|
2021-05-27 06:10:47 +00:00
|
|
|
def self.db_role_for_connection(connection)
|
2022-03-16 18:08:16 +00:00
|
|
|
return ROLE_UNKNOWN if connection.is_a?(::Gitlab::Database::LoadBalancing::ConnectionProxy)
|
|
|
|
|
2021-09-15 12:11:13 +00:00
|
|
|
db_config = Database.db_config_for_connection(connection)
|
|
|
|
return ROLE_UNKNOWN unless db_config
|
2021-05-27 06:10:47 +00:00
|
|
|
|
2021-09-15 12:11:13 +00:00
|
|
|
if db_config.name.ends_with?(LoadBalancer::REPLICA_SUFFIX)
|
2021-08-13 03:10:09 +00:00
|
|
|
ROLE_REPLICA
|
|
|
|
else
|
|
|
|
ROLE_PRIMARY
|
|
|
|
end
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|