diff --git a/ee/lib/gitlab/database/load_balancing/host.rb b/ee/lib/gitlab/database/load_balancing/host.rb index f1850c95ee1327bddfe4c46df19defe40ab1418d..4f4a833f59256f3899219436037a759372e99a71 100644 --- a/ee/lib/gitlab/database/load_balancing/host.rb +++ b/ee/lib/gitlab/database/load_balancing/host.rb @@ -7,7 +7,7 @@ module LoadBalancing class Host attr_reader :pool, :last_checked_at, :intervals, :load_balancer, :host, :port - delegate :connection, :release_connection, :enable_query_cache!, :disable_query_cache!, to: :pool + delegate :connection, :release_connection, :enable_query_cache!, :disable_query_cache!, :query_cache_enabled, to: :pool CONNECTION_ERRORS = if defined?(PG) diff --git a/ee/lib/gitlab/database/load_balancing/load_balancer.rb b/ee/lib/gitlab/database/load_balancing/load_balancer.rb index adaa8990ab47ab5187e62ef47fd1b517ef35ae03..c91033307e4ab238e52ed22fb0d01714a12b21ee 100644 --- a/ee/lib/gitlab/database/load_balancing/load_balancer.rb +++ b/ee/lib/gitlab/database/load_balancing/load_balancer.rb @@ -12,7 +12,6 @@ module LoadBalancing # always returns a connection to the primary. class LoadBalancer CACHE_KEY = :gitlab_load_balancer_host - ENSURE_CACHING_KEY = 'ensure_caching' attr_reader :host_list @@ -103,7 +102,6 @@ def release_host host.release_connection end - RequestStore.delete(ENSURE_CACHING_KEY) RequestStore.delete(CACHE_KEY) end @@ -180,19 +178,8 @@ def serialization_failure?(error) private - # TODO: - # Move enable_query_cache! to ConnectionPool (https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/database.rb#L223) - # when the feature flag is removed in https://gitlab.com/gitlab-org/gitlab/-/issues/276203. def ensure_caching! - # Feature (Flipper gem) reads the data from the database, and it would cause the infinite loop here. - # We need to ensure that the code below is executed only once, until the feature flag is removed. - return if RequestStore[ENSURE_CACHING_KEY] - - RequestStore[ENSURE_CACHING_KEY] = true - - if Feature.enabled?(:query_cache_for_load_balancing, default_enabled: true) - host.enable_query_cache! - end + host.enable_query_cache! unless host.query_cache_enabled end end end diff --git a/ee/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb b/ee/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb index 5f7632f3ded02c77787153138a9af9ebfd2dedbf..21208a29297cbc77a169742cbf3854a6fa694f66 100644 --- a/ee/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb +++ b/ee/spec/lib/gitlab/database/load_balancing/load_balancer_spec.rb @@ -50,29 +50,24 @@ def twice_wrapped_exception(top, middle, original) host = double(:host) allow(lb).to receive(:host).and_return(host) + allow(host).to receive(:query_cache_enabled).and_return(true) + expect(host).to receive(:connection).and_return(connection) - expect(host).to receive(:enable_query_cache!).once expect { |b| lb.read(&b) }.to yield_with_args(connection) - - expect(RequestStore[described_class::ENSURE_CACHING_KEY]).to be true end - context 'when :query_cache_for_load_balancing feature flag is disabled' do - before do - stub_feature_flags(query_cache_for_load_balancing: false) - end + it 'ensures that query cache is enabled' do + connection = double(:connection) + host = double(:host) - it 'yields a connection for a read without enabling query cache' do - connection = double(:connection) - host = double(:host) + allow(lb).to receive(:host).and_return(host) + allow(host).to receive(:query_cache_enabled).and_return(false) + allow(host).to receive(:connection).and_return(connection) - allow(lb).to receive(:host).and_return(host) - expect(host).to receive(:connection).and_return(connection) - expect(host).not_to receive(:enable_query_cache!) + expect(host).to receive(:enable_query_cache!).once - expect { |b| lb.read(&b) }.to yield_with_args(connection) - end + lb.read { 10 } end it 'marks hosts that are offline' do @@ -168,7 +163,6 @@ def twice_wrapped_exception(top, middle, original) lb.release_host expect(RequestStore[described_class::CACHE_KEY]).to be_nil - expect(RequestStore[described_class::ENSURE_CACHING_KEY]).to be_nil end end