Commit 84a80a98 authored by Ruben Davila's avatar Ruben Davila

Merge branch 'master' into ce-to-ee

parents 0da213c9 0150b04f
---
title: Reduce queries needed to check if node is a primary or secondary Geo node
merge_request:
author:
...@@ -3,7 +3,7 @@ module Gitlab ...@@ -3,7 +3,7 @@ module Gitlab
class OauthApplicationUndefinedError < StandardError; end class OauthApplicationUndefinedError < StandardError; end
def self.current_node def self.current_node
RequestStore.store[:geo_node_current] ||= begin self.cache_value(:geo_node_current) do
GeoNode.find_by(host: Gitlab.config.gitlab.host, GeoNode.find_by(host: Gitlab.config.gitlab.host,
port: Gitlab.config.gitlab.port, port: Gitlab.config.gitlab.port,
relative_url_root: Gitlab.config.gitlab.relative_url_root) relative_url_root: Gitlab.config.gitlab.relative_url_root)
...@@ -11,15 +11,15 @@ module Gitlab ...@@ -11,15 +11,15 @@ module Gitlab
end end
def self.primary_node def self.primary_node
RequestStore.store[:geo_primary_node] ||= GeoNode.find_by(primary: true) self.cache_value(:geo_primary_node) { GeoNode.find_by(primary: true) }
end end
def self.secondary_nodes def self.secondary_nodes
RequestStore.store[:geo_secondary_nodes] ||= GeoNode.where(primary: false) self.cache_value(:geo_secondary_nodes) { GeoNode.where(primary: false) }
end end
def self.enabled? def self.enabled?
RequestStore.store[:geo_node_enabled] ||= GeoNode.exists? self.cache_value(:geo_node_enabled) { GeoNode.exists? }
end end
def self.license_allows? def self.license_allows?
...@@ -27,11 +27,11 @@ module Gitlab ...@@ -27,11 +27,11 @@ module Gitlab
end end
def self.primary? def self.primary?
RequestStore.store[:geo_node_primary?] ||= self.enabled? && self.current_node && self.current_node.primary? self.cache_value(:geo_node_primary) { self.enabled? && self.current_node && self.current_node.primary? }
end end
def self.secondary? def self.secondary?
RequestStore.store[:geo_node_secondary] ||= self.enabled? && self.current_node && !self.current_node.primary? self.cache_value(:geo_node_secondary) { self.enabled? && self.current_node && !self.current_node.primary? }
end end
def self.geo_node?(host:, port:) def self.geo_node?(host:, port:)
...@@ -49,8 +49,15 @@ module Gitlab ...@@ -49,8 +49,15 @@ module Gitlab
def self.oauth_authentication def self.oauth_authentication
return false unless Gitlab::Geo.secondary? return false unless Gitlab::Geo.secondary?
RequestStore.store[:geo_oauth_application] ||= self.cache_value(:geo_oauth_application) do
Gitlab::Geo.current_node.oauth_application or raise OauthApplicationUndefinedError Gitlab::Geo.current_node.oauth_application or raise OauthApplicationUndefinedError
end
end
def self.cache_value(key, &block)
return yield unless RequestStore.active?
RequestStore.fetch(key) { yield }
end end
end end
end end
...@@ -37,6 +37,23 @@ describe Gitlab::Geo, lib: true do ...@@ -37,6 +37,23 @@ describe Gitlab::Geo, lib: true do
expect(described_class.enabled?).to be_falsey expect(described_class.enabled?).to be_falsey
end end
end end
context 'with RequestStore enabled' do
before do
RequestStore.begin!
end
after do
RequestStore.end!
RequestStore.clear!
end
it 'return false when no GeoNode exists' do
expect(GeoNode).to receive(:exists?).once.and_call_original
2.times { expect(described_class.enabled?).to be_falsey }
end
end
end end
describe 'readonly?' do describe 'readonly?' do
......
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