debian-mirror-gitlab/lib/gitlab/database/load_balancing.rb

58 lines
1.6 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module LoadBalancing
# The exceptions raised for connection errors.
2021-11-18 22:05:49 +05:30
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-09-04 01:27:46 +05:30
2021-11-18 22:05:49 +05:30
def self.base_models
@base_models ||= ::Gitlab::Database.database_base_models.values.freeze
2021-09-04 01:27:46 +05:30
end
2021-11-18 22:05:49 +05:30
def self.each_load_balancer
return to_enum(__method__) unless block_given?
2021-09-04 01:27:46 +05:30
2021-11-18 22:05:49 +05:30
base_models.each do |model|
yield model.connection.load_balancer
end
2021-09-04 01:27:46 +05:30
end
2021-11-18 22:05:49 +05:30
def self.release_hosts
each_load_balancer(&:release_host)
2021-09-04 01:27:46 +05:30
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-11-11 11:23:49 +05:30
# connecting to.
2021-09-04 01:27:46 +05:30
def self.db_role_for_connection(connection)
2021-11-11 11:23:49 +05:30
db_config = Database.db_config_for_connection(connection)
return ROLE_UNKNOWN unless db_config
2021-10-27 15:23:28 +05:30
2021-11-11 11:23:49 +05:30
if db_config.name.ends_with?(LoadBalancer::REPLICA_SUFFIX)
2021-10-27 15:23:28 +05:30
ROLE_REPLICA
else
ROLE_PRIMARY
end
2021-09-04 01:27:46 +05:30
end
end
end
end