Class: Gitlab::Database::LoadBalancing::PrimaryHost

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/load_balancing/primary_host.rb

Overview

A host that wraps the primary database connection.

This class is used to always enable load balancing as if replicas exist, without the need for extra database connections. This ensures that code using the load balancer doesn’t have to handle the case where load balancing is enabled, but no replicas have been configured (= the default case).

Constant Summary collapse

WAL_ERROR_MESSAGE =
<<~MSG.strip
  Obtaining WAL information when not using any replicas results in
  redundant queries, and may break installations that don't support
  streaming replication (e.g. AWS' Aurora database).
MSG

Instance Method Summary collapse

Constructor Details

#initialize(load_balancer) ⇒ PrimaryHost

Returns a new instance of PrimaryHost.



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

def initialize(load_balancer)
  @load_balancer = load_balancer
end

Instance Method Details

#caught_up?(_location) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 72

def caught_up?(_location)
  true
end

#connectionObject



43
44
45
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 43

def connection
  @load_balancer.pool.connection
end

#database_replica_locationObject

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 68

def database_replica_location
  raise NotImplementedError, WAL_ERROR_MESSAGE
end

#disable_query_cache!Object



34
35
36
37
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 34

def disable_query_cache!
  # This could mess up the primary connection, so we make this a no-op
  nil
end

#disconnect!(timeout: 120) ⇒ Object



47
48
49
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 47

def disconnect!(timeout: 120)
  nil
end

#enable_query_cache!Object



29
30
31
32
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 29

def enable_query_cache!
  # This could mess up the primary connection, so we make this a no-op
  nil
end

#offline!Object



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

def offline!
  ::Gitlab::Database::LoadBalancing::Logger.warn(
    event: :host_offline,
    message: 'Marking primary host as offline'
  )

  nil
end

#online?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 60

def online?
  true
end

#primary_write_locationObject

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 64

def primary_write_location
  raise NotImplementedError, WAL_ERROR_MESSAGE
end

#query_cache_enabledObject



39
40
41
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 39

def query_cache_enabled
  @load_balancer.pool.query_cache_enabled
end

#release_connectionObject



24
25
26
27
# File 'lib/gitlab/database/load_balancing/primary_host.rb', line 24

def release_connection
  # no-op as releasing primary connections isn't needed.
  nil
end