2018-06-25 16:49:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# rubocop:disable Style/Documentation
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module BackgroundMigration
|
|
|
|
class DeleteDiffFiles
|
2018-07-04 15:06:30 -04:00
|
|
|
class MergeRequestDiff < ActiveRecord::Base
|
|
|
|
self.table_name = 'merge_request_diffs'
|
|
|
|
|
|
|
|
belongs_to :merge_request
|
2018-07-05 16:15:57 -04:00
|
|
|
has_many :merge_request_diff_files
|
2018-07-04 15:06:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class MergeRequestDiffFile < ActiveRecord::Base
|
|
|
|
self.table_name = 'merge_request_diff_files'
|
|
|
|
end
|
|
|
|
|
2018-07-05 16:15:57 -04:00
|
|
|
DEAD_TUPLES_THRESHOLD = 50_000
|
|
|
|
VACUUM_WAIT_TIME = 5.minutes
|
2018-06-25 16:49:53 -04:00
|
|
|
|
2018-07-09 17:34:30 -04:00
|
|
|
def perform(ids)
|
|
|
|
@ids = ids
|
2018-06-25 16:49:53 -04:00
|
|
|
|
2018-07-06 11:23:51 -04:00
|
|
|
# We should reschedule until deadtuples get in a desirable
|
2018-07-09 17:34:30 -04:00
|
|
|
# state (e.g. < 50_000). That may take more than one reschedule.
|
2018-07-06 11:23:51 -04:00
|
|
|
#
|
|
|
|
if should_wait_deadtuple_vacuum?
|
|
|
|
reschedule
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-07-09 17:34:30 -04:00
|
|
|
prune_diff_files
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_wait_deadtuple_vacuum?
|
|
|
|
return false unless Gitlab::Database.postgresql?
|
2018-07-06 11:23:51 -04:00
|
|
|
|
2018-07-09 17:34:30 -04:00
|
|
|
diff_files_dead_tuples_count >= DEAD_TUPLES_THRESHOLD
|
2018-07-06 11:23:51 -04:00
|
|
|
end
|
|
|
|
|
2018-07-09 17:34:30 -04:00
|
|
|
private
|
|
|
|
|
2018-07-06 11:23:51 -04:00
|
|
|
def reschedule
|
2018-07-09 17:34:30 -04:00
|
|
|
BackgroundMigrationWorker.perform_in(VACUUM_WAIT_TIME, self.class.name.demodulize, [@ids])
|
2018-07-06 11:23:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diffs_collection
|
2018-07-09 17:34:30 -04:00
|
|
|
MergeRequestDiff.where(id: @ids)
|
2018-07-06 11:23:51 -04:00
|
|
|
end
|
|
|
|
|
2018-07-05 16:15:57 -04:00
|
|
|
def diff_files_dead_tuples_count
|
|
|
|
dead_tuple =
|
|
|
|
execute_statement("SELECT n_dead_tup FROM pg_stat_all_tables "\
|
|
|
|
"WHERE relname = 'merge_request_diff_files'")[0]
|
|
|
|
|
2018-07-06 11:23:51 -04:00
|
|
|
dead_tuple&.fetch('n_dead_tup', 0).to_i
|
2018-07-05 16:15:57 -04:00
|
|
|
end
|
2018-06-25 16:49:53 -04:00
|
|
|
|
2018-07-09 17:34:30 -04:00
|
|
|
def prune_diff_files
|
2018-07-05 16:15:57 -04:00
|
|
|
removed = 0
|
|
|
|
updated = 0
|
|
|
|
|
|
|
|
MergeRequestDiff.transaction do
|
2018-07-09 17:34:30 -04:00
|
|
|
updated = diffs_collection.update_all(state: 'without_files')
|
|
|
|
removed = MergeRequestDiffFile.where(merge_request_diff_id: @ids).delete_all
|
2018-07-05 16:15:57 -04:00
|
|
|
end
|
|
|
|
|
2018-07-06 11:23:51 -04:00
|
|
|
log_info("Removed #{removed} merge_request_diff_files rows, "\
|
2018-07-05 16:15:57 -04:00
|
|
|
"updated #{updated} merge_request_diffs rows")
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_statement(sql)
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
|
|
|
end
|
2018-06-25 16:49:53 -04:00
|
|
|
|
2018-07-05 16:15:57 -04:00
|
|
|
def log_info(message)
|
|
|
|
Rails.logger.info("BackgroundMigration::DeleteDiffFiles - #{message}")
|
2018-06-25 16:49:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|