Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
8c413af4
Commit
8c413af4
authored
Aug 20, 2020
by
Alex Kalderimis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add issue rebalancing worker
parent
a0efb830
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
+80
-0
app/services/issue_rebalancing_service.rb
app/services/issue_rebalancing_service.rb
+61
-0
app/workers/issue_rebalancing_worker.rb
app/workers/issue_rebalancing_worker.rb
+19
-0
No files found.
app/services/issue_rebalancing_service.rb
0 → 100644
View file @
8c413af4
# frozen_string_literal: true
module
Issue
class
RebalancingService
<
Issues
::
BaseService
MAX_ISSUE_COUNT
=
100_000
TooManyIssues
=
Class
.
new
(
StandardError
)
attr_reader
:issue
def
initialize
(
issue
)
@issue
=
issue
end
# rubocop: disable CodeReuse/ActiveRecord
def
execute
base
=
Issue
.
relative_positioning_query_base
(
issue
)
n
=
base
.
count
if
n
>
MAX_ISSUE_COUNT
raise
TooManyIssues
,
"
#{
n
}
issues"
end
gaps
=
n
-
1
gap_size
=
0
ratio
=
0.5
while
gap_size
<
RelativePositioning
::
MIN_GAP
&&
ratio
<
1
gap_size
=
(
ratio
*
Gitlab
::
Database
::
MAX_INT_VALUE
)
/
(
gaps
/
2
)
ratio
+=
0.1
end
# If there are 4 billion issues, then we cannot rebalance them
if
gap_size
<
RelativePositioning
::
MIN_GAP
raise
RelativePositioning
::
NoSpaceLeft
end
start
=
0
-
(
gaps
/
2
)
*
gap_size
indexed
=
base
.
reorder
(
:relative_position
,
:id
).
pluck
(
:id
).
each_with_index
indexed
.
each_slice
(
500
)
do
|
pairs
|
values
=
pairs
.
map
do
|
id
,
index
|
"(
#{
id
}
,
#{
start
+
(
index
*
gap_size
)
}
)"
end
.
join
(
', '
)
Issue
.
connection
.
exec_query
(
<<~
SQL
,
"rebalance issue positions"
)
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
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
app/workers/issue_rebalancing_worker.rb
0 → 100644
View file @
8c413af4
# frozen_string_literal: true
class
IssueRebalancingWorker
# rubocop:disable Scalability/IdempotentWorker
include
ApplicationWorker
def
perform
(
issue_id
)
issue
=
Issue
.
find
(
issue_id
)
rebalance
(
issue
)
rescue
ActiveRecord
::
RecordNotFound
=>
e
Sidekiq
.
logger
.
warn
(
e
)
end
private
def
rebalance
(
issue
)
IssueRebalancingService
.
new
(
issue
).
execute
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment