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