debian-mirror-gitlab/app/services/issue_rebalancing_service.rb

94 lines
2.5 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
class IssueRebalancingService
MAX_ISSUE_COUNT = 10_000
2021-04-17 20:07:23 +05:30
BATCH_SIZE = 100
2020-11-24 15:15:51 +05:30
TooManyIssues = Class.new(StandardError)
def initialize(issue)
@issue = issue
@base = Issue.relative_positioning_query_base(issue)
end
def execute
gates = [issue.project, issue.project.group].compact
return unless gates.any? { |gate| Feature.enabled?(:rebalance_issues, gate) }
raise TooManyIssues, "#{issue_count} issues" if issue_count > MAX_ISSUE_COUNT
start = RelativePositioning::START_POSITION - (gaps / 2) * gap_size
2021-03-11 19:13:27 +05:30
if Feature.enabled?(:issue_rebalancing_optimization)
Issue.transaction do
assign_positions(start, indexed_ids)
.sort_by(&:first)
2021-04-17 20:07:23 +05:30
.each_slice(BATCH_SIZE) do |pairs_with_position|
2021-03-11 19:13:27 +05:30
update_positions(pairs_with_position, 'rebalance issue positions in batches ordered by id')
end
end
else
Issue.transaction do
2021-04-17 20:07:23 +05:30
indexed_ids.each_slice(BATCH_SIZE) do |pairs|
2021-03-11 19:13:27 +05:30
pairs_with_position = assign_positions(start, pairs)
update_positions(pairs_with_position, 'rebalance issue positions')
end
end
2020-11-24 15:15:51 +05:30
end
end
private
attr_reader :issue, :base
# rubocop: disable CodeReuse/ActiveRecord
def indexed_ids
base.reorder(:relative_position, :id).pluck(:id).each_with_index
end
# rubocop: enable CodeReuse/ActiveRecord
2021-03-11 19:13:27 +05:30
def assign_positions(start, pairs)
pairs.map do |id, index|
[id, start + (index * gap_size)]
end
end
def update_positions(pairs_with_position, query_name)
values = pairs_with_position.map do |id, index|
"(#{id}, #{index})"
2020-11-24 15:15:51 +05:30
end.join(', ')
2021-03-11 19:13:27 +05:30
run_update_query(values, query_name)
end
def run_update_query(values, query_name)
Issue.connection.exec_query(<<~SQL, query_name)
2020-11-24 15:15:51 +05:30
WITH cte(cte_id, new_pos) AS (
SELECT *
FROM (VALUES #{values}) as t (id, pos)
)
UPDATE #{Issue.table_name}
SET relative_position = cte.new_pos
FROM cte
WHERE cte_id = id
SQL
end
def issue_count
@issue_count ||= base.count
end
def gaps
issue_count - 1
end
def gap_size
# We could try to split the available range over the number of gaps we need,
# but IDEAL_DISTANCE * MAX_ISSUE_COUNT is only 0.1% of the available range,
# so we are guaranteed not to exhaust it by using this static value.
#
# If we raise MAX_ISSUE_COUNT or IDEAL_DISTANCE significantly, this may
# change!
RelativePositioning::IDEAL_DISTANCE
end
end