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
da6f4a75
Commit
da6f4a75
authored
Mar 15, 2021
by
Quang-Minh Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move load-balancing logic in ApplicationRecord to EE
Issue
https://gitlab.com/gitlab-org/gitlab/-/issues/322133
parent
41da482b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
104 additions
and
6 deletions
+104
-6
app/models/application_record.rb
app/models/application_record.rb
+6
-6
ee/app/models/ee/application_record.rb
ee/app/models/ee/application_record.rb
+22
-0
ee/spec/models/ee/application_record_spec.rb
ee/spec/models/ee/application_record_spec.rb
+47
-0
spec/models/application_record_spec.rb
spec/models/application_record_spec.rb
+29
-0
No files found.
app/models/application_record.rb
View file @
da6f4a75
...
...
@@ -52,13 +52,11 @@ class ApplicationRecord < ActiveRecord::Base
# Start a new transaction with a shorter-than-usual statement timeout. This is
# currently one third of the default 15-second timeout
def
self
.
with_fast_read_statement_timeout
::
Gitlab
::
Database
::
LoadBalancing
::
Session
.
current
.
use_replica_if_possible
do
transaction
(
requires_new:
true
)
do
connection
.
exec_query
(
"SET LOCAL statement_timeout = 5000"
)
def
self
.
with_fast_read_statement_timeout
(
timeout_ms
=
5000
)
transaction
(
requires_new:
true
)
do
connection
.
exec_query
(
"SET LOCAL statement_timeout =
#{
timeout_ms
}
"
)
yield
end
yield
end
end
...
...
@@ -81,3 +79,5 @@ class ApplicationRecord < ActiveRecord::Base
enum
(
enum_mod
.
key
=>
values
)
end
end
ApplicationRecord
.
prepend_if_ee
(
'EE::ApplicationRecord'
)
ee/app/models/ee/application_record.rb
0 → 100644
View file @
da6f4a75
# frozen_string_literal: true
module
EE
module
ApplicationRecord
extend
ActiveSupport
::
Concern
class_methods
do
extend
::
Gitlab
::
Utils
::
Override
override
:with_fast_read_statement_timeout
def
with_fast_read_statement_timeout
(
timeout_ms
=
5000
)
::
Gitlab
::
Database
::
LoadBalancing
::
Session
.
current
.
use_replica_if_possible
do
transaction
(
requires_new:
true
)
do
connection
.
exec_query
(
"SET LOCAL statement_timeout =
#{
timeout_ms
}
"
)
yield
end
end
end
end
end
end
ee/spec/models/ee/application_record_spec.rb
0 → 100644
View file @
da6f4a75
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
ApplicationRecord
do
describe
'.with_fast_read_statement_timeout'
do
let
(
:session
)
{
double
(
:session
)
}
before
do
allow
(
::
Gitlab
::
Database
::
LoadBalancing
::
Session
).
to
receive
(
:current
).
and_return
(
session
)
allow
(
session
).
to
receive
(
:use_replica_if_possible
).
and_yield
end
it
'yields control'
do
expect
do
|
blk
|
described_class
.
with_fast_read_statement_timeout
(
&
blk
)
end
.
to
yield_control
.
once
end
context
'when the query runs faster than configured timeout'
do
it
'executes the query without error'
do
result
=
nil
expect
do
described_class
.
with_fast_read_statement_timeout
(
100
)
do
result
=
described_class
.
connection
.
exec_query
(
'SELECT 1'
)
end
end
.
not_to
raise_error
expect
(
result
).
not_to
be_nil
end
end
# This query hangs for 10ms and then gets cancelled. As there is no
# other way to test the timeout for sure, 10ms of waiting seems to be
# reasonable!
context
'when the query runs longer than configured timeout'
do
it
'cancels the query and raiss an exception'
do
expect
do
described_class
.
with_fast_read_statement_timeout
(
10
)
do
described_class
.
connection
.
exec_query
(
'SELECT pg_sleep(0.1)'
)
end
end
.
to
raise_error
(
ActiveRecord
::
QueryCanceled
)
end
end
end
end
spec/models/application_record_spec.rb
View file @
da6f4a75
...
...
@@ -100,4 +100,33 @@ RSpec.describe ApplicationRecord do
expect
(
User
.
where_exists
(
User
.
limit
(
1
))).
to
eq
([
user
])
end
end
describe
'.with_fast_read_statement_timeout'
do
context
'when the query runs faster than configured timeout'
do
it
'executes the query without error'
do
result
=
nil
expect
do
described_class
.
with_fast_read_statement_timeout
(
100
)
do
result
=
described_class
.
connection
.
exec_query
(
'SELECT 1'
)
end
end
.
not_to
raise_error
expect
(
result
).
not_to
be_nil
end
end
# This query hangs for 10ms and then gets cancelled. As there is no
# other way to test the timeout for sure, 10ms of waiting seems to be
# reasonable!
context
'when the query runs longer than configured timeout'
do
it
'cancels the query and raiss an exception'
do
expect
do
described_class
.
with_fast_read_statement_timeout
(
10
)
do
described_class
.
connection
.
exec_query
(
'SELECT pg_sleep(0.1)'
)
end
end
.
to
raise_error
(
ActiveRecord
::
QueryCanceled
)
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