Class: Gitlab::BackgroundMigration::MigrateMergeRequestDiffCommitUsers

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb

Overview

Migrates author and committer names and emails from merge_request_diff_commits to two columns that point to merge_request_diff_commit_users.

rubocop: disable Metrics/ClassLength

Defined Under Namespace

Classes: MergeRequestDiffCommit, MergeRequestDiffCommitUser

Constant Summary collapse

USER_ROWS_PER_QUERY =

The number of user rows in merge_request_diff_commit_users to get in a single query.

1_000
COMMIT_ROWS_PER_QUERY =

The number of rows in merge_request_diff_commits to get in a single query.

1_000
UPDATES_PER_QUERY =

The number of rows in merge_request_diff_commits to update in a single query.

Tests in staging revealed that increasing the number of updates per query translates to a longer total runtime for a migration. For example, given the same range of rows to migrate, 1000 updates per query required a total of roughly 15 seconds. On the other hand, 5000 updates per query required a total of roughly 25 seconds. For this reason, we use a value of 1000 rows per update.

1_000

Instance Method Summary collapse

Instance Method Details

#already_processed?(start_id, stop_id) ⇒ Boolean

Returns:

  • (Boolean)

99
100
101
102
103
104
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 99

def already_processed?(start_id, stop_id)
  Database::BackgroundMigrationJob
    .for_migration_execution('MigrateMergeRequestDiffCommitUsers', [start_id, stop_id])
    .succeeded
    .any?
end

#arel_tableObject


277
278
279
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 277

def arel_table
  MergeRequestDiffCommit.arel_table
end

#bulk_update_commit_rows(mapping) ⇒ Object

Bulk updates rows in the merge_request_diff_commits table with their new author and/or committer ID values.

Updates are batched together to reduce the overhead of having to produce a single UPDATE for every row, as we may end up having to update thousands of rows at once.

The query produced by this method is along the lines of the following:

UPDATE merge_request_diff_commits
SET commit_author_id =
  CASE
  WHEN (merge_request_diff_id, relative_order) = (x, y) THEN X
  WHEN ...
  END,
committer_id =
  CASE
  WHEN (merge_request_diff_id, relative_order) = (x, y) THEN Y
  WHEN ...
  END
WHERE (merge_request_diff_id, relative_order) IN ( (x, y), ... )

The `mapping` argument is a Hash in the following format:

{ [merge_request_diff_id, relative_order] => [author_id, committer_id] }

rubocop: disable Metrics/AbcSize


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 225

def bulk_update_commit_rows(mapping)
  author_case = Arel::Nodes::Case.new
  committer_case = Arel::Nodes::Case.new
  primary_values = []

  mapping.each do |diff_id_and_order, (author_id, committer_id)|
    primary_value = Arel::Nodes::Grouping.new(diff_id_and_order)

    primary_values << primary_value

    if author_id
      author_case.when(primary_key.eq(primary_value)).then(author_id)
    end

    if committer_id
      committer_case.when(primary_key.eq(primary_value)).then(committer_id)
    end
  end

  if author_case.conditions.empty? && committer_case.conditions.empty?
    return
  end

  fields = []

  # Statements such as `SET x = CASE END` are not valid SQL statements, so
  # we omit setting an ID field if there are no values to populate it
  # with.
  if author_case.conditions.any?
    fields << [arel_table[:commit_author_id], author_case]
  end

  if committer_case.conditions.any?
    fields << [arel_table[:committer_id], committer_case]
  end

  query = Arel::UpdateManager.new
    .table(arel_table)
    .where(primary_key.in(primary_values))
    .set(fields)
    .to_sql

  MergeRequestDiffCommit.connection.execute(query)
end

#create_missing_users(users, mapping) ⇒ Object

Creates any users for which no row exists in merge_request_diff_commit_users.

Not all users queried may exist yet, so we need to create any missing ones; making sure we handle concurrent creations of the same user


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 156

def create_missing_users(users, mapping)
  create = []

  users.each do |(name, email)|
    create << { name: name, email: email } unless mapping[[name, email]]
  end

  return if create.empty?

  MergeRequestDiffCommitUser
    .insert_all(create, returning: %w[id name email])
    .each do |row|
      mapping[[row['name'], row['email']]] = MergeRequestDiffCommitUser
        .new(id: row['id'], name: row['name'], email: row['email'])
    end

  # It's possible for (name, email) pairs to be inserted concurrently,
  # resulting in the above insert not returning anything. Here we get any
  # remaining users that were created concurrently.
  get_user_rows_in_batches(
    users.reject { |pair| mapping.key?(pair) },
    mapping
  )
end

#get_data_to_update(start_id, stop_id) ⇒ Object

Returns the data we'll use to determine what merge_request_diff_commits rows to update, and what data to use for populating their commit_author_id and committer_id columns.


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 109

def get_data_to_update(start_id, stop_id)
  # This Set is used to retrieve users that already exist in
  # merge_request_diff_commit_users.
  users = Set.new

  # This Hash maps the primary key of every row in
  # merge_request_diff_commits to the (trimmed) author and committer
  # details to use for updating the row.
  to_update = {}

  MergeRequestDiffCommit.each_row_to_migrate(start_id, stop_id) do |row|
    author = [prepare(row.author_name), prepare(row.author_email)]
    committer = [prepare(row.committer_name), prepare(row.committer_email)]

    to_update[[row.merge_request_diff_id, row.relative_order]] =
      [author, committer]

    users << author if author[0] || author[1]
    users << committer if committer[0] || committer[1]
  end

  [users, to_update]
end

#get_user_rows_in_batches(users, user_mapping) ⇒ Object

Gets any existing rows in merge_request_diff_commit_users in batches.

This method may end up having to retrieve lots of rows. To reduce the overhead, we batch queries into a UNION query. We limit the number of queries per UNION so we don't end up sending a single query containing too many SELECT statements.


139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 139

def get_user_rows_in_batches(users, user_mapping)
  users.each_slice(USER_ROWS_PER_QUERY) do |pairs|
    queries = pairs.map do |(name, email)|
      MergeRequestDiffCommitUser.where(name: name, email: email).to_sql
    end

    MergeRequestDiffCommitUser.union(queries).each do |row|
      user_mapping[[row.name.to_s, row.email.to_s]] = row
    end
  end
end

#perform(start_id, stop_id) ⇒ Object

rubocop: enable Style/Documentation


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 80

def perform(start_id, stop_id)
  return if already_processed?(start_id, stop_id)

  # This Hash maps user names + emails to their corresponding rows in
  # merge_request_diff_commit_users.
  user_mapping = {}

  user_details, diff_rows_to_update = get_data_to_update(start_id, stop_id)

  get_user_rows_in_batches(user_details, user_mapping)
  create_missing_users(user_details, user_mapping)
  update_commit_rows(diff_rows_to_update, user_mapping)

  Database::BackgroundMigrationJob.mark_all_as_succeeded(
    'MigrateMergeRequestDiffCommitUsers',
    [start_id, stop_id]
  )
end

#prepare(value) ⇒ Object

Prepares a value to be inserted into a column in the table `merge_request_diff_commit_users`. Values in this table are limited to 512 characters.

We treat empty strings as NULL values, as there's no point in (for example) storing a row where both the name and Email are an empty string. In addition, if we treated them differently we could end up with two rows: one where field X is NULL, and one where field X is an empty string. This is redundant, so we avoid storing such data.


290
291
292
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 290

def prepare(value)
  value.present? ? value[0..511] : nil
end

#primary_keyObject

rubocop: enable Metrics/AbcSize


271
272
273
274
275
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 271

def primary_key
  Arel::Nodes::Grouping.new(
    [arel_table[:merge_request_diff_id], arel_table[:relative_order]]
  )
end

#update_commit_rows(to_update, user_mapping) ⇒ Object

Updates rows in merge_request_diff_commits with their new commit_author_id and committer_id values.


183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/gitlab/background_migration/migrate_merge_request_diff_commit_users.rb', line 183

def update_commit_rows(to_update, user_mapping)
  to_update.each_slice(UPDATES_PER_QUERY) do |slice|
    updates = {}

    slice.each do |(diff_id, order), (author, committer)|
      author_id = user_mapping[author]&.id
      committer_id = user_mapping[committer]&.id

      updates[[diff_id, order]] = [author_id, committer_id]
    end

    bulk_update_commit_rows(updates)
  end
end