Class: Database::ConsistencyCheckService

Inherits:
Object
  • Object
show all
Defined in:
app/services/database/consistency_check_service.rb

Constant Summary collapse

CURSOR_REDIS_KEY_TTL =
7.days
EMPTY_RESULT =
{ matches: 0, mismatches: 0, batches: 0, mismatches_details: [] }.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ConsistencyCheckService.



8
9
10
11
12
13
14
15
# File 'app/services/database/consistency_check_service.rb', line 8

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
end

Instance Method Details

#executeObject

This class takes two ActiveRecord models, and compares the selected columns of the two models tables, for the purposes of checking the consistency of mirroring of tables. For example Namespace and Ci::NamepaceMirror

It compares up to 25 batches (1000 records / batch), or up to 30 seconds for all the batches in total.

It saves the cursor of the next start_id (cursor) in Redis. If the start_id wasn’t saved in Redis, for example, in the first run, it will choose some random start_id

Example:

service = Database::ConsistencyCheckService.new(
  source_model: Namespace,
  target_model: Ci::NamespaceMirror,
  source_columns: %w[id traversal_ids],
  target_columns: %w[namespace_id traversal_ids],
)
result = service.execute

result is a hash that has the following fields:

  • batches: Number of batches checked

  • matches: The number of matched records

  • mismatches: The number of mismatched records

  • mismatches_details: It’s an array that contains details about the mismatched records.

    each record in this array is a hash of format {id: ID, source_table: [...], target_table: [...]}
    Each record represents the attributes of the records in the two tables.
    
  • start_id: The start id cursor of the current batch. <nil> means no records.

  • next_start_id: The ID that can be used for the next batch iteration check. <nil> means no records



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/database/consistency_check_service.rb', line 45

def execute
  start_id = next_start_id

  return EMPTY_RESULT if start_id.nil?

  result = consistency_checker.execute(start_id: start_id)
  result[:start_id] = start_id

  save_next_start_id(result[:next_start_id])

  result
end