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

96 lines
3 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.
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-10-27 15:23:28 +05:30
unless load_balancing_proxy = ActiveRecord::Base.load_balancing_proxy
2021-09-04 01:27:46 +05:30
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-10-27 15:23:28 +05:30
load_balancing_proxy
2021-09-04 01:27:46 +05:30
end
# Returns a Hash containing the load balancing configuration.
def self.configuration
2021-11-11 11:23:49 +05:30
@configuration ||= Configuration.for_model(ActiveRecord::Base)
2021-09-04 01:27:46 +05:30
end
# Returns true if load balancing is to be enabled.
def self.enable?
return false if Gitlab::Runtime.rake?
2021-11-11 11:23:49 +05:30
configured?
2021-09-04 01:27:46 +05:30
end
def self.configured?
2021-11-11 11:23:49 +05:30
configuration.load_balancing_enabled? ||
configuration.service_discovery_enabled?
2021-09-04 01:27:46 +05:30
end
def self.start_service_discovery
2021-11-11 11:23:49 +05:30
return unless configuration.service_discovery_enabled?
2021-09-04 01:27:46 +05:30
2021-11-11 11:23:49 +05:30
ServiceDiscovery
.new(proxy.load_balancer, **configuration.service_discovery)
.start
2021-09-04 01:27:46 +05:30
end
# Configures proxying of requests.
2021-11-11 11:23:49 +05:30
def self.configure_proxy
lb = LoadBalancer.new(configuration, primary_only: !enable?)
ActiveRecord::Base.load_balancing_proxy = ConnectionProxy.new(lb)
2021-09-04 01:27:46 +05:30
2021-10-27 15:23:28 +05:30
# Populate service discovery immediately if it is configured
2021-11-11 11:23:49 +05:30
if configuration.service_discovery_enabled?
ServiceDiscovery
.new(lb, **configuration.service_discovery)
.perform_service_discovery
2021-10-27 15:23:28 +05:30
end
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