Commit 9287b27a authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '325291-remove-hardcoded-main-db-from-lb' into 'master'

Replace hardcoded database config name :main

See merge request gitlab-org/gitlab!69551
parents afb83e44 30883076
...@@ -32,12 +32,10 @@ module Gitlab ...@@ -32,12 +32,10 @@ module Gitlab
def set_data_consistency_locations!(job) def set_data_consistency_locations!(job)
# Once we add support for multiple databases to our load balancer, we would use something like this: # Once we add support for multiple databases to our load balancer, we would use something like this:
# job['wal_locations'] = Gitlab::Database::DATABASES.transform_values do |connection| # job['wal_locations'] = Gitlab::Database::DATABASES.transform_values do |connection|
# connection.load_balancer.primary_write_location. # connection.load_balancer.primary_write_location
# end # end
# #
# TODO: Replace hardcoded database config name :main when we merge unification strategy job['wal_locations'] = { Gitlab::Database::MAIN_DATABASE_NAME.to_sym => wal_location } if wal_location
# https://gitlab.com/gitlab-org/gitlab/-/issues/336566
job['wal_locations'] = { main: wal_location } if wal_location
end end
def wal_location def wal_location
......
...@@ -66,7 +66,7 @@ module Gitlab ...@@ -66,7 +66,7 @@ module Gitlab
def legacy_wal_location(job) def legacy_wal_location(job)
wal_location = job['database_write_location'] || job['database_replica_location'] wal_location = job['database_write_location'] || job['database_replica_location']
{ main: wal_location } if wal_location { Gitlab::Database::MAIN_DATABASE_NAME.to_sym => wal_location } if wal_location
end end
def load_balancing_available?(worker_class) def load_balancing_available?(worker_class)
......
...@@ -85,7 +85,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do ...@@ -85,7 +85,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do
end end
it 'passes database_replica_location' do it 'passes database_replica_location' do
expected_location = { main: location } expected_location = { Gitlab::Database::MAIN_DATABASE_NAME.to_sym => location }
expect(load_balancer).to receive_message_chain(:host, "database_replica_location").and_return(location) expect(load_balancer).to receive_message_chain(:host, "database_replica_location").and_return(location)
...@@ -103,7 +103,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do ...@@ -103,7 +103,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do
end end
it 'passes primary write location', :aggregate_failures do it 'passes primary write location', :aggregate_failures do
expected_location = { main: location } expected_location = { Gitlab::Database::MAIN_DATABASE_NAME.to_sym => location }
expect(load_balancer).to receive(:primary_write_location).and_return(location) expect(load_balancer).to receive(:primary_write_location).and_return(location)
...@@ -116,28 +116,43 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do ...@@ -116,28 +116,43 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do
end end
end end
shared_examples_for 'database location was already provided' do context 'when worker cannot be constantized' do
shared_examples_for 'does not set database location again' do |use_primary| let(:worker_class) { 'ActionMailer::MailDeliveryJob' }
before do let(:expected_consistency) { :always }
allow(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_primary?).and_return(use_primary)
end
it 'does not set database locations again' do include_examples 'does not pass database locations'
run_middleware end
expect(job['wal_locations']).to eq({ main: old_location }) context 'when worker class does not include ApplicationWorker' do
end let(:worker_class) { ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper }
end let(:expected_consistency) { :always }
include_examples 'does not pass database locations'
end
context 'database wal location was already provided' do
let(:old_location) { '0/D525E3A8' } let(:old_location) { '0/D525E3A8' }
let(:new_location) { 'AB/12345' } let(:new_location) { 'AB/12345' }
let(:job) { { "job_id" => "a180b47c-3fd6-41b8-81e9-34da61c3400e", 'wal_locations' => { main: old_location } } } let(:wal_locations) { { Gitlab::Database::MAIN_DATABASE_NAME.to_sym => old_location } }
let(:job) { { "job_id" => "a180b47c-3fd6-41b8-81e9-34da61c3400e", 'wal_locations' => wal_locations } }
before do before do
allow(load_balancer).to receive(:primary_write_location).and_return(new_location) allow(load_balancer).to receive(:primary_write_location).and_return(new_location)
allow(load_balancer).to receive(:database_replica_location).and_return(new_location) allow(load_balancer).to receive(:database_replica_location).and_return(new_location)
end end
shared_examples_for 'does not set database location again' do |use_primary|
before do
allow(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_primary?).and_return(use_primary)
end
it 'does not set database locations again' do
run_middleware
expect(job['wal_locations']).to eq(wal_locations)
end
end
context "when write was performed" do context "when write was performed" do
include_examples 'does not set database location again', true include_examples 'does not set database location again', true
end end
...@@ -147,24 +162,6 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do ...@@ -147,24 +162,6 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqClientMiddleware do
end end
end end
context 'when worker cannot be constantized' do
let(:worker_class) { 'ActionMailer::MailDeliveryJob' }
let(:expected_consistency) { :always }
include_examples 'does not pass database locations'
end
context 'when worker class does not include ApplicationWorker' do
let(:worker_class) { ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper }
let(:expected_consistency) { :always }
include_examples 'does not pass database locations'
end
context 'database wal location was already provided' do
include_examples 'database location was already provided'
end
context 'when worker data consistency is :always' do context 'when worker data consistency is :always' do
include_context 'data consistency worker class', :always, :load_balancing_for_test_data_consistency_worker include_context 'data consistency worker class', :always, :load_balancing_for_test_data_consistency_worker
......
...@@ -63,10 +63,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqServerMiddleware do ...@@ -63,10 +63,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqServerMiddleware do
end end
shared_examples_for 'replica is up to date' do |expected_strategy| shared_examples_for 'replica is up to date' do |expected_strategy|
let(:wal_locations) { { main: '0/D525E3A8' } } let(:location) {'0/D525E3A8' }
let(:wal_locations) { { Gitlab::Database::MAIN_DATABASE_NAME.to_sym => location } }
it 'does not stick to the primary', :aggregate_failures do it 'does not stick to the primary', :aggregate_failures do
expect(load_balancer).to receive(:select_up_to_date_host).with(wal_locations[:main]).and_return(true) expect(load_balancer).to receive(:select_up_to_date_host).with(location).and_return(true)
run_middleware do run_middleware do
expect(Gitlab::Database::LoadBalancing::Session.current.use_primary?).not_to be_truthy expect(Gitlab::Database::LoadBalancing::Session.current.use_primary?).not_to be_truthy
...@@ -91,7 +92,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqServerMiddleware do ...@@ -91,7 +92,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::SidekiqServerMiddleware do
let(:job) { { 'job_id' => 'a180b47c-3fd6-41b8-81e9-34da61c3400e', 'wal_locations' => wal_locations } } let(:job) { { 'job_id' => 'a180b47c-3fd6-41b8-81e9-34da61c3400e', 'wal_locations' => wal_locations } }
before do before do
allow(load_balancer).to receive(:select_up_to_date_host).with(wal_locations[:main]).and_return(true) allow(load_balancer).to receive(:select_up_to_date_host).with(location).and_return(true)
end end
it_behaves_like 'replica is up to date', 'replica' it_behaves_like 'replica is up to date', 'replica'
......
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