Commit 23152fd9 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Disable query cache when outside Rails executor

When outside of the Rails executor context, we do not want to enable the
query cache because the connection may never get released until the
thread terminates.

For example, this happens on console sessions or in long-running threads
that do not work in a request context.

Changelog: fixed
parent 2d65f45b
......@@ -266,7 +266,10 @@ module Gitlab
private
def ensure_caching!
host.enable_query_cache! unless host.query_cache_enabled
return unless Rails.application.executor.active?
return if host.query_cache_enabled
host.enable_query_cache!
end
def request_cache
......
......@@ -89,6 +89,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do
host = double(:host)
allow(lb).to receive(:host).and_return(host)
allow(Rails.application.executor).to receive(:active?).and_return(true)
allow(host).to receive(:query_cache_enabled).and_return(false)
allow(host).to receive(:connection).and_return(connection)
......@@ -97,6 +98,20 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do
lb.read { 10 }
end
it 'does not enable query cache when outside Rails executor context' do
connection = double(:connection)
host = double(:host)
allow(lb).to receive(:host).and_return(host)
allow(Rails.application.executor).to receive(:active?).and_return(false)
allow(host).to receive(:query_cache_enabled).and_return(false)
allow(host).to receive(:connection).and_return(connection)
expect(host).not_to receive(:enable_query_cache!)
lb.read { 10 }
end
it 'marks hosts that are offline' do
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