Commit 1589d640 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '283971-fix-geo-database-load-balancer' into 'master'

Geo: adds database load balancing support to work on standby clusters

See merge request gitlab-org/gitlab!53147
parents 2827e75f 010bc43f
---
title: 'Geo: adds database load balancing support to work on standby clusters'
merge_request: 53147
author:
type: added
......@@ -243,8 +243,8 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do
end
describe '#primary_write_location' do
it 'returns a String' do
expect(lb.primary_write_location).to be_an_instance_of(String)
it 'returns a String in the right format' do
expect(lb.primary_write_location).to match(/[A-E0-9]{1,8}\/[A-E0-9]{1,8}/)
end
it 'raises an error if the write location could not be retrieved' do
......
......@@ -256,11 +256,28 @@ module Gitlab
row['system_identifier']
end
# @param [ActiveRecord::Connection] ar_connection
# @return [String]
def self.get_write_location(ar_connection)
row = ar_connection
.select_all("SELECT pg_current_wal_insert_lsn()::text AS location")
.first
use_new_load_balancer_query = Gitlab::Utils.to_boolean(ENV['USE_NEW_LOAD_BALANCER_QUERY'], default: false)
sql = if use_new_load_balancer_query
<<~NEWSQL
SELECT CASE
WHEN pg_is_in_recovery() = true AND EXISTS (SELECT 1 FROM pg_stat_get_wal_senders())
THEN pg_last_wal_replay_lsn()::text
WHEN pg_is_in_recovery() = false
THEN pg_current_wal_insert_lsn()::text
ELSE NULL
END AS location;
NEWSQL
else
<<~SQL
SELECT pg_current_wal_insert_lsn()::text AS location
SQL
end
row = ar_connection.select_all(sql).first
row['location'] if row
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