Welcome to mirror list, hosted at ThFree Co, Russian Federation.

with_lock_retries_outside_transaction.rb « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 175cc493e3667ddde2d5f95a1200721c23815f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

module Gitlab
  module Database
    # This retry method behaves similar to WithLockRetries
    # except it does not wrap itself into a transaction scope.
    #
    # In our context, this is only useful if directly connected to
    # PostgreSQL. When going through pgbouncer, this method **won't work**
    # as it relies on using `SET` outside transactions (and hence can be
    # multiplexed across different connections).
    class WithLockRetriesOutsideTransaction < WithLockRetries
      private

      def run_block_with_lock_timeout
        execute("SET lock_timeout TO '#{current_lock_timeout_in_ms}ms'")

        log(message: 'Lock timeout is set', current_iteration: current_iteration, lock_timeout_in_ms: current_lock_timeout_in_ms)

        run_block

        log(message: 'Migration finished', current_iteration: current_iteration, lock_timeout_in_ms: current_lock_timeout_in_ms)
      end

      def run_block_without_lock_timeout
        log(message: "Couldn't acquire lock to perform the migration", current_iteration: current_iteration)
        log(message: "Executing without lock timeout", current_iteration: current_iteration)

        disable_lock_timeout

        run_block

        log(message: 'Migration finished', current_iteration: current_iteration)
      end

      def disable_lock_timeout
        execute("SET lock_timeout TO '0'")
      end
    end
  end
end