Commit 7d0c81a7 authored by Thong Kuah's avatar Thong Kuah

Skip retrying for reads on connection errors if primary only

Retrying is pointless, given we only have the one host, and causes
an infinite loop

Changelog: fixed
parent 734ddb6f
...@@ -54,7 +54,10 @@ module Gitlab ...@@ -54,7 +54,10 @@ module Gitlab
connection = host.connection connection = host.connection
return yield connection return yield connection
rescue StandardError => error rescue StandardError => error
if serialization_failure?(error) if primary_only?
# If we only have primary configured, retrying is pointless
raise error
elsif serialization_failure?(error)
# This error can occur when a query conflicts. See # This error can occur when a query conflicts. See
# https://www.postgresql.org/docs/current/static/hot-standby.html#HOT-STANDBY-CONFLICT # https://www.postgresql.org/docs/current/static/hot-standby.html#HOT-STANDBY-CONFLICT
# for more information. # for more information.
......
...@@ -141,6 +141,24 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do ...@@ -141,6 +141,24 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do
lb.read { raise conflict_error } lb.read { raise conflict_error }
end end
context 'only primary is configured' do
let(:lb) do
config = Gitlab::Database::LoadBalancing::Configuration.new(ActiveRecord::Base)
allow(config).to receive(:load_balancing_enabled?).and_return(false)
described_class.new(config)
end
it 'does not retry a query on connection error if only the primary is configured' do
host = double(:host, query_cache_enabled: true)
allow(lb).to receive(:host).and_return(host)
allow(host).to receive(:connection).and_raise(PG::UnableToSend)
expect { lb.read }.to raise_error(PG::UnableToSend)
end
end
it 'uses the primary if no secondaries are available' do it 'uses the primary if no secondaries are available' do
allow(lb).to receive(:connection_error?).and_return(true) allow(lb).to receive(:connection_error?).and_return(true)
......
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