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
0
Merge Requests
0
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
Jérome Perrin
gitlab-ce
Commits
5d7ee7a1
Commit
5d7ee7a1
authored
Oct 20, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use optimistic locking
parent
198ae21e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
50 additions
and
25 deletions
+50
-25
app/models/ci/pipeline.rb
app/models/ci/pipeline.rb
+6
-6
app/services/ci/process_pipeline_service.rb
app/services/ci/process_pipeline_service.rb
+11
-12
app/services/ci/register_build_service.rb
app/services/ci/register_build_service.rb
+4
-7
db/migrate/20161021114307_add_lock_version_to_build_and_pipelines.rb
...20161021114307_add_lock_version_to_build_and_pipelines.rb
+14
-0
db/schema.rb
db/schema.rb
+2
-0
lib/gitlab/optimistic_locking.rb
lib/gitlab/optimistic_locking.rb
+13
-0
No files found.
app/models/ci/pipeline.rb
View file @
5d7ee7a1
...
@@ -30,23 +30,23 @@ module Ci
...
@@ -30,23 +30,23 @@ module Ci
end
end
event
:run
do
event
:run
do
transition
any
=>
:running
transition
any
-
[
:running
]
=>
:running
end
end
event
:skip
do
event
:skip
do
transition
any
=>
:skipped
transition
any
-
[
:skipped
]
=>
:skipped
end
end
event
:drop
do
event
:drop
do
transition
any
=>
:failed
transition
any
-
[
:failed
]
=>
:failed
end
end
event
:succeed
do
event
:succeed
do
transition
any
=>
:success
transition
any
-
[
:success
]
=>
:success
end
end
event
:cancel
do
event
:cancel
do
transition
any
=>
:canceled
transition
any
-
[
:canceled
]
=>
:canceled
end
end
# IMPORTANT
# IMPORTANT
...
@@ -260,7 +260,7 @@ module Ci
...
@@ -260,7 +260,7 @@ module Ci
end
end
def
update_status
def
update_status
with_lock
do
Gitlab
::
OptimisticLocking
.
retry_lock
(
build
)
do
case
latest_builds_status
case
latest_builds_status
when
'pending'
then
enqueue
when
'pending'
then
enqueue
when
'running'
then
run
when
'running'
then
run
...
...
app/services/ci/process_pipeline_service.rb
View file @
5d7ee7a1
...
@@ -10,17 +10,14 @@ module Ci
...
@@ -10,17 +10,14 @@ module Ci
create_builds!
create_builds!
end
end
@pipeline
.
with_lock
do
new_builds
=
new_builds
=
stage_indexes_of_created_builds
.
map
do
|
index
|
stage_indexes_of_created_builds
.
map
do
|
index
|
process_stage
(
index
)
process_stage
(
index
)
end
end
@pipeline
.
update_status
@pipeline
.
update_status
# Return a flag if a when builds got enqueued
new_builds
.
flatten
.
any?
new_builds
.
flatten
.
any?
end
end
end
private
private
...
@@ -32,9 +29,11 @@ module Ci
...
@@ -32,9 +29,11 @@ module Ci
def
process_stage
(
index
)
def
process_stage
(
index
)
current_status
=
status_for_prior_stages
(
index
)
current_status
=
status_for_prior_stages
(
index
)
created_builds_in_stage
(
index
).
select
do
|
build
|
if
HasStatus
::
COMPLETED_STATUSES
.
include?
(
current_status
)
if
HasStatus
::
COMPLETED_STATUSES
.
include?
(
current_status
)
created_builds_in_stage
(
index
).
select
do
|
build
|
process_build
(
build
,
current_status
)
Gitlab
::
OptimisticLocking
.
retry_lock
(
build
)
do
|
build
|
process_build
(
build
,
current_status
)
end
end
end
end
end
end
end
...
...
app/services/ci/register_build_service.rb
View file @
5d7ee7a1
...
@@ -28,17 +28,14 @@ module Ci
...
@@ -28,17 +28,14 @@ module Ci
if
build
if
build
# In case when 2 runners try to assign the same build, second runner will be declined
# In case when 2 runners try to assign the same build, second runner will be declined
# with StateMachines::InvalidTransition in run! method.
# with StateMachines::InvalidTransition or StaleObjectError when doing run! or save method.
build
.
with_lock
do
build
.
runner_id
=
current_runner
.
id
build
.
runner_id
=
current_runner
.
id
build
.
run!
build
.
save!
build
.
run!
end
end
end
build
build
rescue
StateMachines
::
InvalidTransition
rescue
StateMachines
::
InvalidTransition
,
StaleObjectError
nil
nil
end
end
...
...
db/migrate/20161021114307_add_lock_version_to_build_and_pipelines.rb
0 → 100644
View file @
5d7ee7a1
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class
AddLockVersionToBuildAndPipelines
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME
=
false
def
change
add_column
:ci_builds
,
:lock_version
,
:integer
add_column
:ci_commits
,
:lock_version
,
:integer
end
end
db/schema.rb
View file @
5d7ee7a1
...
@@ -189,6 +189,7 @@ ActiveRecord::Schema.define(version: 20161024042317) do
...
@@ -189,6 +189,7 @@ ActiveRecord::Schema.define(version: 20161024042317) do
t
.
text
"yaml_variables"
t
.
text
"yaml_variables"
t
.
datetime
"queued_at"
t
.
datetime
"queued_at"
t
.
string
"token"
t
.
string
"token"
t
.
integer
"lock_version"
end
end
add_index
"ci_builds"
,
[
"commit_id"
,
"stage_idx"
,
"created_at"
],
name:
"index_ci_builds_on_commit_id_and_stage_idx_and_created_at"
,
using: :btree
add_index
"ci_builds"
,
[
"commit_id"
,
"stage_idx"
,
"created_at"
],
name:
"index_ci_builds_on_commit_id_and_stage_idx_and_created_at"
,
using: :btree
...
@@ -219,6 +220,7 @@ ActiveRecord::Schema.define(version: 20161024042317) do
...
@@ -219,6 +220,7 @@ ActiveRecord::Schema.define(version: 20161024042317) do
t
.
datetime
"finished_at"
t
.
datetime
"finished_at"
t
.
integer
"duration"
t
.
integer
"duration"
t
.
integer
"user_id"
t
.
integer
"user_id"
t
.
integer
"lock_version"
end
end
add_index
"ci_commits"
,
[
"gl_project_id"
,
"sha"
],
name:
"index_ci_commits_on_gl_project_id_and_sha"
,
using: :btree
add_index
"ci_commits"
,
[
"gl_project_id"
,
"sha"
],
name:
"index_ci_commits_on_gl_project_id_and_sha"
,
using: :btree
...
...
lib/gitlab/optimistic_locking.rb
0 → 100644
View file @
5d7ee7a1
module
Gitlab
module
OptimisticLocking
def
retry_lock
(
subject
,
&
block
)
while
true
do
begin
return
yield
subject
rescue
StaleObjectError
subject
.
reload
end
end
end
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