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
|
|
|
|
|
|
|
def self.proxy
|
2021-09-21 18:11:18 +00:00
|
|
|
ActiveRecord::Base.load_balancing_proxy
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Hash containing the load balancing configuration.
|
|
|
|
def self.configuration
|
2021-09-09 15:09:24 +00:00
|
|
|
@configuration ||= Configuration.for_model(ActiveRecord::Base)
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
2021-09-21 18:11:18 +00:00
|
|
|
# Returns `true` if the use of load balancing replicas should be enabled.
|
|
|
|
#
|
|
|
|
# This is disabled for Rake tasks to ensure e.g. database migrations
|
|
|
|
# always produce consistent results.
|
|
|
|
def self.enable_replicas?
|
2021-05-27 06:10:47 +00:00
|
|
|
return false if Gitlab::Runtime.rake?
|
|
|
|
|
2021-09-09 15:09:24 +00:00
|
|
|
configured?
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.configured?
|
2021-09-09 15:09:24 +00:00
|
|
|
configuration.load_balancing_enabled? ||
|
|
|
|
configuration.service_discovery_enabled?
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.start_service_discovery
|
2021-09-09 15:09:24 +00:00
|
|
|
return unless configuration.service_discovery_enabled?
|
2021-05-27 06:10:47 +00:00
|
|
|
|
2021-08-26 15:10:41 +00:00
|
|
|
ServiceDiscovery
|
2021-09-09 15:09:24 +00:00
|
|
|
.new(proxy.load_balancer, **configuration.service_discovery)
|
2021-08-26 15:10:41 +00:00
|
|
|
.start
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
|
2021-09-21 18:11:18 +00:00
|
|
|
def self.perform_service_discovery
|
|
|
|
return unless configuration.service_discovery_enabled?
|
2021-08-18 15:11:07 +00:00
|
|
|
|
2021-09-21 18:11:18 +00:00
|
|
|
ServiceDiscovery
|
|
|
|
.new(proxy.load_balancer, **configuration.service_discovery)
|
|
|
|
.perform_service_discovery
|
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)
|
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
|