Commit e1b019bf authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen

Introduce new use_replica method in DB load balancer

Issue https://gitlab.com/gitlab-org/gitlab/-/issues/322133
parent 8c94271e
...@@ -57,10 +57,22 @@ module Gitlab ...@@ -57,10 +57,22 @@ module Gitlab
end end
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. # Delegates all unknown messages to a read-write connection.
def method_missing(name, *args, &block) def method_missing(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) write_using_load_balancer(name, args, &block)
end end
end
# Performs a read using the load balancer. # Performs a read using the load balancer.
# #
......
...@@ -55,6 +55,28 @@ module Gitlab ...@@ -55,6 +55,28 @@ module Gitlab
@ignore_writes = false @ignore_writes = false
end 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! def write!
@performed_write = true @performed_write = true
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment