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
93a964d4
Commit
93a964d4
authored
Jul 03, 2018
by
Shinya Maeda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add spec for ExclusiveLeaseHelpers
parent
02e3a624
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
106 additions
and
23 deletions
+106
-23
app/models/ci/build_trace_chunk.rb
app/models/ci/build_trace_chunk.rb
+1
-1
app/services/concerns/exclusive_lease_lock.rb
app/services/concerns/exclusive_lease_lock.rb
+0
-22
lib/gitlab/exclusive_lease_helpers.rb
lib/gitlab/exclusive_lease_helpers.rb
+29
-0
spec/lib/gitlab/exclusive_lease_helpers_spec.rb
spec/lib/gitlab/exclusive_lease_helpers_spec.rb
+76
-0
No files found.
app/models/ci/build_trace_chunk.rb
View file @
93a964d4
module
Ci
class
BuildTraceChunk
<
ActiveRecord
::
Base
include
FastDestroyAll
include
ExclusiveLeaseLock
include
::
Gitlab
::
ExclusiveLeaseHelpers
extend
Gitlab
::
Ci
::
Model
belongs_to
:build
,
class_name:
"Ci::Build"
,
foreign_key: :build_id
...
...
app/services/concerns/exclusive_lease_lock.rb
deleted
100644 → 0
View file @
02e3a624
module
ExclusiveLeaseLock
extend
ActiveSupport
::
Concern
FailedToObtainLockError
=
Class
.
new
(
StandardError
)
def
in_lock
(
key
,
ttl:
1
.
minute
,
retries:
10
,
sleep_sec:
0.01
.
seconds
)
lease
=
Gitlab
::
ExclusiveLease
.
new
(
key
,
timeout:
ttl
)
until
uuid
=
lease
.
try_obtain
# Keep trying until we obtain the lease. To prevent hammering Redis too
# much we'll wait for a bit.
sleep
(
sleep_sec
)
break
if
(
retries
-=
1
)
<
0
end
raise
FailedToObtainLockError
,
'Failed to obtain a lock'
unless
uuid
return
yield
ensure
Gitlab
::
ExclusiveLease
.
cancel
(
key
,
uuid
)
end
end
lib/gitlab/exclusive_lease_helpers.rb
0 → 100644
View file @
93a964d4
module
Gitlab
# This module provides helper methods which are intregrated with GitLab::ExclusiveLease
module
ExclusiveLeaseHelpers
FailedToObtainLockError
=
Class
.
new
(
StandardError
)
##
# This helper method blocks a process/thread until the other process cancel the obrainted lease key.
#
# Note: It's basically discouraged to use this method in the unicorn's thread,
# because it holds the connection until all `retries` is consumed.
# This could potentially eat up all connection pools.
def
in_lock
(
key
,
ttl:
1
.
minute
,
retries:
10
,
sleep_sec:
0.01
.
seconds
)
lease
=
Gitlab
::
ExclusiveLease
.
new
(
key
,
timeout:
ttl
)
until
uuid
=
lease
.
try_obtain
# Keep trying until we obtain the lease. To prevent hammering Redis too
# much we'll wait for a bit.
sleep
(
sleep_sec
)
break
if
(
retries
-=
1
)
<
0
end
raise
FailedToObtainLockError
,
'Failed to obtain a lock'
unless
uuid
return
yield
ensure
Gitlab
::
ExclusiveLease
.
cancel
(
key
,
uuid
)
end
end
end
spec/lib/gitlab/exclusive_lease_helpers_spec.rb
0 → 100644
View file @
93a964d4
require
'spec_helper'
describe
Gitlab
::
ExclusiveLeaseHelpers
,
:clean_gitlab_redis_shared_state
do
include
::
ExclusiveLeaseHelpers
let
(
:class_instance
)
{
(
Class
.
new
{
include
::
Gitlab
::
ExclusiveLeaseHelpers
}).
new
}
let
(
:unique_key
)
{
SecureRandom
.
hex
(
10
)
}
describe
'#in_lock'
do
subject
{
class_instance
.
in_lock
(
unique_key
,
**
options
)
{
}
}
let
(
:options
)
{
{
}
}
context
'when the lease is not obtained yet'
do
before
do
stub_exclusive_lease
(
unique_key
,
'uuid'
)
end
it
'calls the given block'
do
expect
{
|
b
|
class_instance
.
in_lock
(
unique_key
,
&
b
)
}.
to
yield_control
.
once
end
it
'calls the given block continuously'
do
expect
{
|
b
|
class_instance
.
in_lock
(
unique_key
,
&
b
)
}.
to
yield_control
.
once
expect
{
|
b
|
class_instance
.
in_lock
(
unique_key
,
&
b
)
}.
to
yield_control
.
once
expect
{
|
b
|
class_instance
.
in_lock
(
unique_key
,
&
b
)
}.
to
yield_control
.
once
end
it
'cancels the exclusive lease after the block'
do
expect_to_cancel_exclusive_lease
(
unique_key
,
'uuid'
)
subject
end
end
context
'when the lease is obtained already'
do
let!
(
:lease
)
{
stub_exclusive_lease_taken
(
unique_key
)
}
it
'retries to obtain a lease and raises an error'
do
expect
(
lease
).
to
receive
(
:try_obtain
).
exactly
(
11
).
times
expect
{
subject
}.
to
raise_error
(
'Failed to obtain a lock'
)
end
context
'when ttl is specified'
do
let
(
:options
)
{
{
ttl:
10
.
minute
}
}
it
'receives the specified argument'
do
expect
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:new
).
with
(
unique_key
,
{
timeout:
10
.
minute
}
)
expect
{
subject
}.
to
raise_error
(
'Failed to obtain a lock'
)
end
end
context
'when retry count is specified'
do
let
(
:options
)
{
{
retries:
3
}
}
it
'retries for the specified times'
do
expect
(
lease
).
to
receive
(
:try_obtain
).
exactly
(
4
).
times
expect
{
subject
}.
to
raise_error
(
'Failed to obtain a lock'
)
end
end
context
'when sleep second is specified'
do
let
(
:options
)
{
{
retries:
0
,
sleep_sec:
0.05
.
second
}
}
it
'receives the specified argument'
do
expect
(
class_instance
).
to
receive
(
:sleep
).
with
(
0.05
.
second
).
once
expect
{
subject
}.
to
raise_error
(
'Failed to obtain a lock'
)
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