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

write_ahead_log.rb « indicators « health_status « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1614b17df480894f990010bca639af850bb84c16 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

module Gitlab
  module Database
    module HealthStatus
      module Indicators
        class WriteAheadLog
          include Gitlab::Utils::StrongMemoize

          LIMIT = 42
          PENDING_WAL_COUNT_SQL = <<~SQL
            WITH
            current_wal_file AS (
              SELECT pg_walfile_name(pg_current_wal_insert_lsn()) AS pg_walfile_name
            ),
            current_wal AS (
              SELECT
                ('x' || substring(pg_walfile_name, 9, 8))::bit(32)::int AS log,
                ('x' || substring(pg_walfile_name, 17, 8))::bit(32)::int AS seg,
                pg_walfile_name
              FROM current_wal_file
            ),
            archive_wal AS (
              SELECT
                ('x' || substring(last_archived_wal, 9, 8))::bit(32)::int AS log,
                ('x' || substring(last_archived_wal, 17, 8))::bit(32)::int AS seg,
                last_archived_wal
              FROM pg_stat_archiver
            )
            SELECT ((current_wal.log - archive_wal.log) * 256) + (current_wal.seg - archive_wal.seg) AS pending_wal_count
            FROM current_wal, archive_wal
          SQL

          def initialize(context)
            @connection = context.connection
          end

          def evaluate
            return Signals::NotAvailable.new(self.class, reason: 'indicator disabled') unless enabled?

            unless pending_wal_count
              return Signals::NotAvailable.new(self.class, reason: 'WAL archive queue can not be calculated')
            end

            if pending_wal_count > LIMIT
              Signals::Stop.new(self.class, reason: "WAL archive queue is too big")
            else
              Signals::Normal.new(self.class, reason: 'WAL archive queue is within limit')
            end
          end

          private

          attr_reader :connection

          def enabled?
            Feature.enabled?(:batched_migrations_health_status_wal, type: :ops)
          end

          # Returns number of WAL segments pending archival
          def pending_wal_count
            Gitlab::Database::LoadBalancing::Session.current.use_primary do
              connection.execute(PENDING_WAL_COUNT_SQL).to_a.first&.fetch('pending_wal_count')
            end
          end
          strong_memoize_attr :pending_wal_count
        end
      end
    end
  end
end