2021-05-27 06:10:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module LoadBalancing
|
|
|
|
# The exceptions raised for connection errors.
|
|
|
|
CONNECTION_ERRORS = if defined?(PG)
|
|
|
|
[
|
|
|
|
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
|
|
|
|
].freeze
|
|
|
|
else
|
|
|
|
[].freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
ProxyNotConfiguredError = Class.new(StandardError)
|
|
|
|
|
|
|
|
# The connection proxy to use for load balancing (if enabled).
|
|
|
|
def self.proxy
|
2021-08-05 18:10:13 +00:00
|
|
|
unless load_balancing_proxy = ActiveRecord::Base.load_balancing_proxy
|
2021-05-27 06:10:47 +00:00
|
|
|
Gitlab::ErrorTracking.track_exception(
|
|
|
|
ProxyNotConfiguredError.new(
|
|
|
|
"Attempting to access the database load balancing proxy, but it wasn't configured.\n" \
|
|
|
|
"Did you forget to call '#{self.name}.configure_proxy'?"
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
2021-08-05 18:10:13 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
# Returns true if load balancing is to be enabled.
|
|
|
|
def self.enable?
|
|
|
|
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
|
|
|
|
|
|
|
|
# Configures proxying of requests.
|
2021-09-03 15:10:48 +00:00
|
|
|
def self.configure_proxy
|
2021-09-09 15:09:24 +00:00
|
|
|
lb = LoadBalancer.new(configuration, primary_only: !enable?)
|
2021-09-03 15:10:48 +00:00
|
|
|
ActiveRecord::Base.load_balancing_proxy = ConnectionProxy.new(lb)
|
2021-08-18 15:11:07 +00:00
|
|
|
|
|
|
|
# Populate service discovery immediately if it is configured
|
2021-09-09 15:09:24 +00:00
|
|
|
if configuration.service_discovery_enabled?
|
2021-08-26 15:10:41 +00:00
|
|
|
ServiceDiscovery
|
2021-09-09 15:09:24 +00:00
|
|
|
.new(lb, **configuration.service_discovery)
|
2021-08-26 15:10:41 +00:00
|
|
|
.perform_service_discovery
|
2021-08-18 15:11:07 +00:00
|
|
|
end
|
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
|
|
|
|
# connecting to. At the moment, the connection can only be retrieved by
|
|
|
|
# Gitlab::Database::LoadBalancer#read or #read_write or from the
|
|
|
|
# ActiveRecord directly. Therefore, if the load balancer doesn't
|
|
|
|
# recognize the connection, this method returns the primary role
|
|
|
|
# directly. In future, we may need to check for other sources.
|
|
|
|
def self.db_role_for_connection(connection)
|
2021-08-13 03:10:09 +00:00
|
|
|
return ROLE_UNKNOWN unless connection
|
2021-05-27 06:10:47 +00:00
|
|
|
|
2021-08-13 03:10:09 +00:00
|
|
|
# The connection proxy does not have a role assigned
|
|
|
|
# as this is dependent on a execution context
|
|
|
|
return ROLE_UNKNOWN if connection.is_a?(ConnectionProxy)
|
|
|
|
|
2021-08-13 15:11:15 +00:00
|
|
|
# During application init we might receive `NullPool`
|
|
|
|
return ROLE_UNKNOWN unless connection.respond_to?(:pool) &&
|
|
|
|
connection.pool.respond_to?(:db_config) &&
|
|
|
|
connection.pool.db_config.respond_to?(:name)
|
|
|
|
|
2021-08-13 03:10:09 +00:00
|
|
|
if connection.pool.db_config.name.ends_with?(LoadBalancer::REPLICA_SUFFIX)
|
|
|
|
ROLE_REPLICA
|
|
|
|
else
|
|
|
|
ROLE_PRIMARY
|
|
|
|
end
|
2021-05-27 06:10:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|