Module: Gitlab::Database::LoadBalancing

Defined in:
lib/gitlab/database/load_balancing.rb,
lib/gitlab/database/load_balancing/host.rb,
lib/gitlab/database/load_balancing/setup.rb,
lib/gitlab/database/load_balancing/logger.rb,
lib/gitlab/database/load_balancing/session.rb,
lib/gitlab/database/load_balancing/resolver.rb,
lib/gitlab/database/load_balancing/sticking.rb,
lib/gitlab/database/load_balancing/host_list.rb,
lib/gitlab/database/load_balancing/primary_host.rb,
lib/gitlab/database/load_balancing/srv_resolver.rb,
lib/gitlab/database/load_balancing/configuration.rb,
lib/gitlab/database/load_balancing/load_balancer.rb,
lib/gitlab/database/load_balancing/rack_middleware.rb,
lib/gitlab/database/load_balancing/connection_proxy.rb,
lib/gitlab/database/load_balancing/service_discovery.rb,
lib/gitlab/database/load_balancing/action_cable_callbacks.rb,
lib/gitlab/database/load_balancing/sidekiq_client_middleware.rb,
lib/gitlab/database/load_balancing/sidekiq_server_middleware.rb

Defined Under Namespace

Modules: ActionCableCallbacks Classes: Configuration, ConnectionProxy, Host, HostList, LoadBalancer, Logger, PrimaryHost, RackMiddleware, Resolver, ServiceDiscovery, Session, Setup, SidekiqClientMiddleware, SidekiqServerMiddleware, SrvResolver, Sticking

Constant Summary collapse

CONNECTION_ERRORS =

The exceptions raised for 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
DB_ROLES =
[
  ROLE_PRIMARY = :primary,
  ROLE_REPLICA = :replica,
  ROLE_UNKNOWN = :unknown
].freeze

Class Method Summary collapse

Class Method Details

.base_modelsObject


21
22
23
# File 'lib/gitlab/database/load_balancing.rb', line 21

def self.base_models
  @base_models ||= ::Gitlab::Database.database_base_models.values.freeze
end

.db_role_for_connection(connection) ⇒ Object

Returns the role (primary/replica) of the database the connection is connecting to.


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/database/load_balancing.rb', line 49

def self.db_role_for_connection(connection)
  return ROLE_UNKNOWN if connection.is_a?(::Gitlab::Database::LoadBalancing::ConnectionProxy)

  db_config = Database.db_config_for_connection(connection)
  return ROLE_UNKNOWN unless db_config

  if db_config.name.ends_with?(LoadBalancer::REPLICA_SUFFIX)
    ROLE_REPLICA
  else
    ROLE_PRIMARY
  end
end

.each_load_balancerObject


25
26
27
28
29
30
31
# File 'lib/gitlab/database/load_balancing.rb', line 25

def self.each_load_balancer
  return to_enum(__method__) unless block_given?

  base_models.each do |model|
    yield model.load_balancer
  end
end

.primary_only?Boolean

Returns:

  • (Boolean)

33
34
35
# File 'lib/gitlab/database/load_balancing.rb', line 33

def self.primary_only?
  each_load_balancer.all?(&:primary_only?)
end

.release_hostsObject


37
38
39
# File 'lib/gitlab/database/load_balancing.rb', line 37

def self.release_hosts
  each_load_balancer(&:release_host)
end