Commit db8faf09 authored by Aleksei Lipniagov's avatar Aleksei Lipniagov Committed by Matthias Käppler

Pass **kwargs to the conn. for LB defined methods

With Ruby3 kwargs syntax changing, we need to ensure that we explicitly
pass all the keyword arguments from our defined method to the connection
in the Load Balancing.

Changelog: other
parent e7762cd5
......@@ -41,31 +41,31 @@ module Gitlab
def select_all(arel, name = nil, binds = [], preparable: nil)
if arel.respond_to?(:locked) && arel.locked
# SELECT ... FOR UPDATE queries should be sent to the primary.
write_using_load_balancer(:select_all, [arel, name, binds],
write_using_load_balancer(:select_all, arel, name, binds,
sticky: true)
else
read_using_load_balancer(:select_all, [arel, name, binds])
read_using_load_balancer(:select_all, arel, name, binds)
end
end
NON_STICKY_READS.each do |name|
define_method(name) do |*args, &block|
read_using_load_balancer(name, args, &block)
define_method(name) do |*args, **kwargs, &block|
read_using_load_balancer(name, *args, **kwargs, &block)
end
end
STICKY_WRITES.each do |name|
define_method(name) do |*args, &block|
write_using_load_balancer(name, args, sticky: true, &block)
define_method(name) do |*args, **kwargs, &block|
write_using_load_balancer(name, *args, sticky: true, **kwargs, &block)
end
end
def transaction(*args, &block)
def transaction(*args, **kwargs, &block)
if current_session.fallback_to_replicas_for_ambiguous_queries?
track_read_only_transaction!
read_using_load_balancer(:transaction, args, &block)
read_using_load_balancer(:transaction, *args, **kwargs, &block)
else
write_using_load_balancer(:transaction, args, sticky: true, &block)
write_using_load_balancer(:transaction, *args, sticky: true, **kwargs, &block)
end
ensure
......@@ -73,26 +73,26 @@ module Gitlab
end
# Delegates all unknown messages to a read-write connection.
def method_missing(name, *args, &block)
def method_missing(...)
if current_session.fallback_to_replicas_for_ambiguous_queries?
read_using_load_balancer(name, args, &block)
read_using_load_balancer(...)
else
write_using_load_balancer(name, args, &block)
write_using_load_balancer(...)
end
end
# Performs a read using the load balancer.
#
# name - The name of the method to call on a connection object.
def read_using_load_balancer(name, args, &block)
def read_using_load_balancer(...)
if current_session.use_primary? &&
!current_session.use_replicas_for_read_queries?
@load_balancer.read_write do |connection|
connection.send(name, *args, &block)
connection.send(...)
end
else
@load_balancer.read do |connection|
connection.send(name, *args, &block)
connection.send(...)
end
end
end
......@@ -102,7 +102,7 @@ module Gitlab
# name - The name of the method to call on a connection object.
# sticky - If set to true the session will stick to the master after
# the write.
def write_using_load_balancer(name, args, sticky: false, &block)
def write_using_load_balancer(name, *args, sticky: false, **kwargs, &block)
if read_only_transaction?
raise WriteInsideReadOnlyTransactionError, 'A write query is performed inside a read-only transaction'
end
......@@ -113,7 +113,7 @@ module Gitlab
# secondary instead of on a primary (when necessary).
current_session.write! if sticky
connection.send(name, *args, &block)
connection.send(name, *args, **kwargs, &block)
end
end
......
......@@ -7,7 +7,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
describe '#select' do
it 'performs a read' do
expect(proxy).to receive(:read_using_load_balancer).with(:select, ['foo'])
expect(proxy).to receive(:read_using_load_balancer).with(:select, 'foo')
proxy.select('foo')
end
......@@ -26,7 +26,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
arel = double(:arel)
expect(proxy).to receive(:read_using_load_balancer)
.with(:select_all, [arel, 'foo', []])
.with(:select_all, arel, 'foo', [])
proxy.select_all(arel, 'foo')
end
......@@ -37,7 +37,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
arel = double(:arel, locked: true)
expect(proxy).to receive(:write_using_load_balancer)
.with(:select_all, [arel, 'foo', []], sticky: true)
.with(:select_all, arel, 'foo', [], sticky: true)
proxy.select_all(arel, 'foo')
end
......@@ -48,7 +48,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
describe "#{name}" do
it 'runs the query on the replica' do
expect(proxy).to receive(:read_using_load_balancer)
.with(name, ['foo'])
.with(name, 'foo')
proxy.send(name, 'foo')
end
......@@ -59,7 +59,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
describe "#{name}" do
it 'runs the query on the primary and sticks to it' do
expect(proxy).to receive(:write_using_load_balancer)
.with(name, ['foo'], sticky: true)
.with(name, 'foo', sticky: true)
proxy.send(name, 'foo')
end
......@@ -187,7 +187,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
describe '#method_missing' do
it 'runs the query on the primary without sticking to it' do
expect(proxy).to receive(:write_using_load_balancer)
.with(:foo, ['foo'])
.with(:foo, 'foo')
proxy.foo('foo')
end
......@@ -197,7 +197,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(proxy).to receive(:write_using_load_balancer).and_call_original
expect { proxy.case_sensitive_comparison(:table, :attribute, :column, { value: :value, format: :format }) }
expect { proxy.case_sensitive_comparison(:table, :attribute, :column, value: :value, format: :format) }
.not_to raise_error
end
......@@ -212,7 +212,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
end
it 'runs the query on the replica' do
expect(proxy).to receive(:read_using_load_balancer).with(:foo, ['foo'])
expect(proxy).to receive(:read_using_load_balancer).with(:foo, 'foo')
proxy.foo('foo')
end
......@@ -222,7 +222,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(proxy).to receive(:read_using_load_balancer).and_call_original
expect { proxy.case_sensitive_comparison(:table, :attribute, :column, { value: :value, format: :format }) }
expect { proxy.case_sensitive_comparison(:table, :attribute, :column, value: :value, format: :format) }
.not_to raise_error
end
end
......@@ -245,7 +245,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(connection).to receive(:foo).with('foo')
expect(proxy.load_balancer).to receive(:read).and_yield(connection)
proxy.read_using_load_balancer(:foo, ['foo'])
proxy.read_using_load_balancer(:foo, 'foo')
end
end
......@@ -257,7 +257,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(connection).to receive(:foo).with('foo')
expect(proxy.load_balancer).to receive(:read).and_yield(connection)
proxy.read_using_load_balancer(:foo, ['foo'])
proxy.read_using_load_balancer(:foo, 'foo')
end
end
......@@ -269,7 +269,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(connection).to receive(:foo).with('foo')
expect(proxy.load_balancer).to receive(:read).and_yield(connection)
proxy.read_using_load_balancer(:foo, ['foo'])
proxy.read_using_load_balancer(:foo, 'foo')
end
end
......@@ -283,7 +283,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(proxy.load_balancer).to receive(:read_write)
.and_yield(connection)
proxy.read_using_load_balancer(:foo, ['foo'])
proxy.read_using_load_balancer(:foo, 'foo')
end
end
end
......@@ -302,7 +302,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(connection).to receive(:foo).with('foo')
expect(session).not_to receive(:write!)
proxy.write_using_load_balancer(:foo, ['foo'])
proxy.write_using_load_balancer(:foo, 'foo')
end
it 'sticks to the primary when sticking is enabled' do
......@@ -310,7 +310,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
expect(connection).to receive(:foo).with('foo')
expect(session).to receive(:write!)
proxy.write_using_load_balancer(:foo, ['foo'], sticky: true)
proxy.write_using_load_balancer(:foo, 'foo', sticky: true)
end
end
end
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