Class: Gitlab::Database::LoadBalancing::Sticking

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

Overview

Module used for handling sticking connections to a primary, if necessary.

Constant Summary collapse

EXPIRATION =

The number of seconds after which a session should stop reading from the primary.

30

Instance Method Summary collapse

Constructor Details

#initialize(load_balancer) ⇒ Sticking

Returns a new instance of Sticking.



13
14
15
# File 'lib/gitlab/database/load_balancing/sticking.rb', line 13

def initialize(load_balancer)
  @load_balancer = load_balancer
end

Instance Method Details

#bulk_stick(namespace, ids) ⇒ Object



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

def bulk_stick(namespace, ids)
  with_primary_write_location do |location|
    ids.each do |id|
      set_write_location_for(namespace, id, location)
    end
  end

  ::Gitlab::Database::LoadBalancing::Session.current.use_primary!
end

#find_caught_up_replica(namespace, id, use_primary_on_failure: true, use_primary_on_empty_location: false) ⇒ Object

Returns true if any caught up replica is found. This does not mean all replicas are caught up but the found caught up replica will be stored in the SafeRequestStore available as LoadBalancer#host for future queries. With use_primary_on_empty_location: true we will assume you need the primary if we can’t find a matching location for the namespace, id pair. You should only use use_primary_on_empty_location in rare cases because we unstick once we find all replicas are caught up one time so it can be wasteful on the primary.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/database/load_balancing/sticking.rb', line 22

def find_caught_up_replica(namespace, id, use_primary_on_failure: true, use_primary_on_empty_location: false)
  location = last_write_location_for(namespace, id)

  result = if location
             up_to_date_result = @load_balancer.select_up_to_date_host(location)

             unstick(namespace, id) if up_to_date_result == LoadBalancer::ALL_CAUGHT_UP

             up_to_date_result != LoadBalancer::NONE_CAUGHT_UP
           else
             # Some callers want to err on the side of caution and be really sure that a caught up replica was
             # found. If we did not have any location to check then we must force `use_primary!` if they they
             # use_primary_on_empty_location
             !use_primary_on_empty_location
           end

  ::Gitlab::Database::LoadBalancing::Session.current.use_primary! if !result && use_primary_on_failure

  result
end

#stick(namespace, id) ⇒ Object

Starts sticking to the primary for the given namespace and id, using the latest WAL pointer from the primary.



45
46
47
48
49
50
# File 'lib/gitlab/database/load_balancing/sticking.rb', line 45

def stick(namespace, id)
  with_primary_write_location do |location|
    set_write_location_for(namespace, id, location)
  end
  ::Gitlab::Database::LoadBalancing::Session.current.use_primary!
end