Commit b36051f4 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Added fallback code to database load balancer to with Standby clusters

parent 752745ae
...@@ -135,6 +135,8 @@ module Gitlab ...@@ -135,6 +135,8 @@ module Gitlab
def primary_write_location def primary_write_location
location = read_write do |connection| location = read_write do |connection|
::Gitlab::Database.get_write_location(connection) ::Gitlab::Database.get_write_location(connection)
rescue ActiveRecord::StatementInvalid
::Gitlab::Database.get_replay_write_location(connection)
end end
return location if location return location if location
......
...@@ -255,6 +255,15 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do ...@@ -255,6 +255,15 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do
expect { lb.primary_write_location }.to raise_error(RuntimeError) expect { lb.primary_write_location }.to raise_error(RuntimeError)
end end
it 'fallbacks to #get_replay_write_location when #get_write_location raises error' do
connection = double(:connection)
allow(lb).to receive(:read_write).and_yield(connection)
allow(::Gitlab::Database).to receive(:get_write_location).and_raise(ActiveRecord::StatementInvalid)
expect(::Gitlab::Database).to receive(:get_replay_write_location).and_return('0/C73A0D88')
lb.primary_write_location
end
end end
describe '#all_caught_up?' do describe '#all_caught_up?' do
......
...@@ -256,6 +256,8 @@ module Gitlab ...@@ -256,6 +256,8 @@ module Gitlab
row['system_identifier'] row['system_identifier']
end end
# @param [ActiveRecord::Connection] ar_connection
# @return [String]
def self.get_write_location(ar_connection) def self.get_write_location(ar_connection)
row = ar_connection row = ar_connection
.select_all("SELECT pg_current_wal_insert_lsn()::text AS location") .select_all("SELECT pg_current_wal_insert_lsn()::text AS location")
...@@ -264,6 +266,16 @@ module Gitlab ...@@ -264,6 +266,16 @@ module Gitlab
row['location'] if row row['location'] if row
end end
# @param [ActiveRecord::Connection] ar_connection
# @return [String]
def self.get_replay_write_location(ar_connection)
row = ar_connection
.select_all("SELECT pg_last_wal_replay_lsn()::text AS location")
.first
row['location'] if row
end
private_class_method :database_version private_class_method :database_version
def self.add_post_migrate_path_to_rails(force: false) def self.add_post_migrate_path_to_rails(force: false)
......
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