Class: Gitlab::Database::ConsistencyChecker

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

Constant Summary collapse

BATCH_SIZE =
500
MAX_BATCHES =
20
MAX_RUNTIME =

must be less than the scheduling frequency of the ConsistencyCheck jobs

5.seconds

Instance Method Summary collapse

Constructor Details

#initialize(source_model:, target_model:, source_columns:, target_columns:) ⇒ ConsistencyChecker

Returns a new instance of ConsistencyChecker.



12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/database/consistency_checker.rb', line 12

def initialize(source_model:, target_model:, source_columns:, target_columns:)
  @source_model = source_model
  @target_model = target_model
  @source_columns = source_columns
  @target_columns = target_columns
  @source_sort_column = source_columns.first
  @target_sort_column = target_columns.first
  @result = { matches: 0, mismatches: 0, batches: 0, mismatches_details: [] }
end

Instance Method Details

#execute(start_id:) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/gitlab/database/consistency_checker.rb', line 23

def execute(start_id:)
  current_start_id = start_id

  return build_result(next_start_id: nil) if max_id.nil?
  return build_result(next_start_id: min_id) if current_start_id > max_id

  @start_time = monotonic_time

  MAX_BATCHES.times do
    if (current_start_id <= max_id) && !over_time_limit?
      ids_range = current_start_id...(current_start_id + BATCH_SIZE)
      # rubocop: disable CodeReuse/ActiveRecord
      source_data = source_model.where(source_sort_column => ids_range)
                      .order(source_sort_column => :asc).pluck(*source_columns)
      target_data = target_model.where(target_sort_column => ids_range)
                      .order(target_sort_column => :asc).pluck(*target_columns)
      # rubocop: enable CodeReuse/ActiveRecord

      current_start_id += BATCH_SIZE
      result[:matches] += append_mismatches_details(source_data, target_data)
      result[:batches] += 1
    else
      break
    end
  end

  result[:mismatches] = result[:mismatches_details].length
  metrics_counter.increment({ source_table: source_model.table_name, result: "match" }, result[:matches])
  metrics_counter.increment({ source_table: source_model.table_name, result: "mismatch" }, result[:mismatches])

  build_result(next_start_id: current_start_id > max_id ? min_id : current_start_id)
end