BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
6ac1caa0
Commit
6ac1caa0
authored
Jun 06, 2017
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '33279_pc_application_settings_performance' into 'master'
redesign caching of application settings See merge request !11894
parents
86b4cd61
d9335282
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
25 deletions
+43
-25
application_setting.rb
app/models/application_setting.rb
+3
-2
current_settings.rb
lib/gitlab/current_settings.rb
+35
-19
merge_requests_controller_spec.rb
spec/controllers/projects/merge_requests_controller_spec.rb
+1
-1
current_settings_spec.rb
spec/lib/gitlab/current_settings_spec.rb
+4
-3
No files found.
app/models/application_setting.rb
View file @
6ac1caa0
...
...
@@ -189,8 +189,9 @@ class ApplicationSetting < ActiveRecord::Base
end
def
self
.
cached
ensure_cache_setup
Rails
.
cache
.
fetch
(
CACHE_KEY
)
value
=
Rails
.
cache
.
read
(
CACHE_KEY
)
ensure_cache_setup
if
value
.
present?
value
end
def
self
.
ensure_cache_setup
...
...
lib/gitlab/current_settings.rb
View file @
6ac1caa0
...
...
@@ -8,39 +8,55 @@ module Gitlab
end
end
def
ensure_application_settings!
return
fake_application_settings
unless
connect_to_db?
delegate
:sidekiq_throttling_enabled?
,
to: :current_application_settings
unless
ENV
[
'IN_MEMORY_APPLICATION_SETTINGS'
]
==
'true'
begin
settings
=
::
ApplicationSetting
.
current
# In case Redis isn't running or the Redis UNIX socket file is not available
rescue
::
Redis
::
BaseError
,
::
Errno
::
ENOENT
settings
=
::
ApplicationSetting
.
last
end
def
fake_application_settings
OpenStruct
.
new
(
::
ApplicationSetting
.
defaults
)
end
settings
||=
::
ApplicationSetting
.
create_from_defaults
private
def
ensure_application_settings!
unless
ENV
[
'IN_MEMORY_APPLICATION_SETTINGS'
]
==
'true'
settings
=
retrieve_settings_from_database?
end
settings
||
in_memory_application_settings
end
delegate
:sidekiq_throttling_enabled?
,
to: :current_application_settings
def
retrieve_settings_from_database?
settings
=
retrieve_settings_from_database_cache?
return
settings
if
settings
.
present?
return
fake_application_settings
unless
connect_to_db?
begin
db_settings
=
::
ApplicationSetting
.
current
# In case Redis isn't running or the Redis UNIX socket file is not available
rescue
::
Redis
::
BaseError
,
::
Errno
::
ENOENT
db_settings
=
::
ApplicationSetting
.
last
end
db_settings
||
::
ApplicationSetting
.
create_from_defaults
end
def
retrieve_settings_from_database_cache?
begin
settings
=
ApplicationSetting
.
cached
rescue
::
Redis
::
BaseError
,
::
Errno
::
ENOENT
# In case Redis isn't running or the Redis UNIX socket file is not available
settings
=
nil
end
settings
end
def
in_memory_application_settings
@in_memory_application_settings
||=
::
ApplicationSetting
.
new
(
::
ApplicationSetting
.
defaults
)
# In case migrations the application_settings table is not created yet,
# we fallback to a simple OpenStruct
rescue
ActiveRecord
::
StatementInvalid
,
ActiveRecord
::
UnknownAttributeError
# In case migrations the application_settings table is not created yet,
# we fallback to a simple OpenStruct
fake_application_settings
end
def
fake_application_settings
OpenStruct
.
new
(
::
ApplicationSetting
.
defaults
)
end
private
def
connect_to_db?
# When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
active_db_connection
=
ActiveRecord
::
Base
.
connection
.
active?
rescue
false
...
...
spec/controllers/projects/merge_requests_controller_spec.rb
View file @
6ac1caa0
...
...
@@ -126,7 +126,7 @@ describe Projects::MergeRequestsController do
recorded
=
ActiveRecord
::
QueryRecorder
.
new
{
go
(
format: :json
)
}
expect
(
recorded
.
count
).
to
be_within
(
5
).
of
(
5
9
)
expect
(
recorded
.
count
).
to
be_within
(
5
).
of
(
5
0
)
expect
(
recorded
.
cached_count
).
to
eq
(
0
)
end
end
...
...
spec/lib/gitlab/current_settings_spec.rb
View file @
6ac1caa0
...
...
@@ -14,20 +14,20 @@ describe Gitlab::CurrentSettings do
end
it
'attempts to use cached values first'
do
expect
(
ApplicationSetting
).
to
receive
(
:current
)
expect
(
ApplicationSetting
).
not_to
receive
(
:last
)
expect
(
ApplicationSetting
).
to
receive
(
:cached
)
expect
(
current_application_settings
).
to
be_a
(
ApplicationSetting
)
end
it
'falls back to DB if Redis returns an empty value'
do
expect
(
ApplicationSetting
).
to
receive
(
:cached
).
and_return
(
nil
)
expect
(
ApplicationSetting
).
to
receive
(
:last
).
and_call_original
expect
(
current_application_settings
).
to
be_a
(
ApplicationSetting
)
end
it
'falls back to DB if Redis fails'
do
expect
(
ApplicationSetting
).
to
receive
(
:c
urrent
).
and_raise
(
::
Redis
::
BaseError
)
expect
(
ApplicationSetting
).
to
receive
(
:c
ached
).
and_raise
(
::
Redis
::
BaseError
)
expect
(
ApplicationSetting
).
to
receive
(
:last
).
and_call_original
expect
(
current_application_settings
).
to
be_a
(
ApplicationSetting
)
...
...
@@ -37,6 +37,7 @@ describe Gitlab::CurrentSettings do
context
'with DB unavailable'
do
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:connect_to_db?
).
and_return
(
false
)
allow_any_instance_of
(
described_class
).
to
receive
(
:retrieve_settings_from_database_cache?
).
and_return
(
nil
)
end
it
'returns an in-memory ApplicationSetting object'
do
...
...
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