Commit 74db6d2f authored by Nick Thomas's avatar Nick Thomas

Merge branch 'sh-fix-geo-secondary-check' into 'master'

Geo: Ensure database is connected before attempting to check for secondary status

Closes #3074

See merge request gitlab-org/gitlab-ee!3112
parents 39c285d5 64496105
---
title: 'Geo: Ensure database is connected before attempting to check for secondary
status'
merge_request:
author:
type: fixed
...@@ -5,7 +5,7 @@ if File.exist?(Rails.root.join('config/database_geo.yml')) ...@@ -5,7 +5,7 @@ if File.exist?(Rails.root.join('config/database_geo.yml'))
end end
begin begin
if Gitlab::Geo.primary? if Gitlab::Geo.connected? && Gitlab::Geo.primary?
Gitlab::Geo.current_node&.update_clone_url! Gitlab::Geo.current_node&.update_clone_url!
end end
rescue => e rescue => e
......
...@@ -32,13 +32,20 @@ module Gitlab ...@@ -32,13 +32,20 @@ module Gitlab
self.cache_value(:geo_secondary_nodes) { GeoNode.where(primary: false) } self.cache_value(:geo_secondary_nodes) { GeoNode.where(primary: false) }
end end
def self.connected?
Gitlab::Database.postgresql? && GeoNode.connected? && GeoNode.table_exists?
end
def self.enabled? def self.enabled?
GeoNode.connected? && self.cache_value(:geo_node_enabled) { GeoNode.exists? } cache_value(:geo_node_enabled) { GeoNode.exists? }
rescue => e end
# We can't use the actual classes in rescue because we load only one of them based on database supported
raise e unless %w(PG::UndefinedTable Mysql2::Error).include? e.class.name
false def self.primary?
self.enabled? && self.current_node&.primary?
end
def self.secondary?
self.enabled? && self.current_node&.secondary?
end end
def self.current_node_enabled? def self.current_node_enabled?
...@@ -64,14 +71,6 @@ module Gitlab ...@@ -64,14 +71,6 @@ module Gitlab
::License.feature_available?(:geo) ::License.feature_available?(:geo)
end end
def self.primary?
self.cache_value(:geo_node_primary) { self.enabled? && self.current_node && self.current_node.primary? }
end
def self.secondary?
self.cache_value(:geo_node_secondary) { self.enabled? && self.current_node && self.current_node.secondary? }
end
def self.geo_node?(host:, port:) def self.geo_node?(host:, port:)
GeoNode.where(host: host, port: port).exists? GeoNode.where(host: host, port: port).exists?
end end
......
...@@ -101,31 +101,35 @@ describe Gitlab::Geo, :geo do ...@@ -101,31 +101,35 @@ describe Gitlab::Geo, :geo do
end end
end end
context 'with RequestStore enabled', :request_store do
it 'return false when no GeoNode exists' do
GeoNode.delete_all
expect(GeoNode).to receive(:exists?).once.and_call_original
2.times { expect(described_class.enabled?).to be_falsey }
end
end
end
describe 'connected?' do
context 'when there is a database issue' do context 'when there is a database issue' do
it 'returns false when database connection is down' do it 'returns false when database connection is down' do
allow(GeoNode).to receive(:connected?) { false } allow(GeoNode).to receive(:connected?) { false }
expect(described_class.enabled?).to be_falsey expect(described_class.connected?).to be_falsey
end end
it 'returns false when database schema does not contain required tables' do it 'returns false when the table does not exist' do
if Gitlab::Database.mysql? allow(GeoNode).to receive(:table_exists?) { false }
allow(GeoNode).to receive(:exists?).and_raise(Mysql2::Error, "Table 'gitlabhq_test.geo_nodes' doesn't exist: SHOW FULL FIELDS FROM `geo_nodes`")
else
allow(GeoNode).to receive(:exists?).and_raise(PG::UndefinedTable)
end
expect(described_class.enabled?).to be_falsey expect(described_class.connected?).to be_falsey
end end
end
context 'with RequestStore enabled', :request_store do
it 'return false when no GeoNode exists' do
GeoNode.delete_all
expect(GeoNode).to receive(:exists?).once.and_call_original it 'returns false when MySQL is in use' do
allow(Gitlab::Database).to receive(:postgresql?) { false }
2.times { expect(described_class.enabled?).to be_falsey } expect(described_class.connected?).to be_falsey
end end
end end
end end
...@@ -139,7 +143,7 @@ describe Gitlab::Geo, :geo do ...@@ -139,7 +143,7 @@ describe Gitlab::Geo, :geo do
end end
context 'current node is primary' do context 'current node is primary' do
it 'returns false ' do it 'returns false' do
expect(described_class.secondary?).to be_falsey expect(described_class.secondary?).to be_falsey
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