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
e1b019bf
Commit
e1b019bf
authored
Mar 12, 2021
by
Quang-Minh Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce new use_replica method in DB load balancer
Issue
https://gitlab.com/gitlab-org/gitlab/-/issues/322133
parent
8c94271e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
1 deletion
+35
-1
ee/lib/gitlab/database/load_balancing/connection_proxy.rb
ee/lib/gitlab/database/load_balancing/connection_proxy.rb
+13
-1
ee/lib/gitlab/database/load_balancing/session.rb
ee/lib/gitlab/database/load_balancing/session.rb
+22
-0
No files found.
ee/lib/gitlab/database/load_balancing/connection_proxy.rb
View file @
e1b019bf
...
...
@@ -57,9 +57,21 @@ module Gitlab
end
end
def
transaction
(
*
args
,
&
block
)
if
::
Gitlab
::
Database
::
LoadBalancing
::
Session
.
current
.
use_replica?
read_using_load_balancer
(
:transaction
,
args
,
&
block
)
else
write_using_load_balancer
(
:transaction
,
args
,
sticky:
true
,
&
block
)
end
end
# Delegates all unknown messages to a read-write connection.
def
method_missing
(
name
,
*
args
,
&
block
)
write_using_load_balancer
(
name
,
args
,
&
block
)
if
::
Gitlab
::
Database
::
LoadBalancing
::
Session
.
current
.
use_replica?
read_using_load_balancer
(
name
,
args
,
&
block
)
else
write_using_load_balancer
(
name
,
args
,
&
block
)
end
end
# Performs a read using the load balancer.
...
...
ee/lib/gitlab/database/load_balancing/session.rb
View file @
e1b019bf
...
...
@@ -55,6 +55,28 @@ module Gitlab
@ignore_writes
=
false
end
# Indicate that all the SQL statements from anywhere inside this block
# should use a replica. This is a weak enforcement. The queries
# prioritize using a primary over a replica in those cases:
# - If the queries are about to write
# - The current session already performed writes
# - It prefers to use primary, aka, use_primary or use_primary! were called
def
use_replica
(
&
blk
)
return
yield
if
use_primary?
||
performed_write?
begin
used_replica
=
@use_replica
@use_replica
=
true
yield
ensure
@use_replica
=
used_replica
&&
!
use_primary?
&&
!
performed_write?
end
end
def
use_replica?
@use_replica
&&
!
use_primary?
&&
!
performed_write?
end
def
write!
@performed_write
=
true
...
...
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