From fa7efca553e325b2aabb087a4eddf4560c356094 Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Fri, 30 Sep 2016 15:26:19 +1000 Subject: Clear the correct query cache This executor currently relies on `ActiveRecord::Base.connection` not changing between `prepare` and `complete`. If something else returns the current ActiveRecord connection to the pool early then this `complete` call will fail to clear the correct query cache and restore the original `query_cache_enabled` status. This has for example been happening in Sidekiq: https://github.com/mperham/sidekiq/pull/3166 We can just keep track of the connection as part of the exector state. --- activerecord/lib/active_record/query_cache.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/query_cache.rb') diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index c45c8c1697..c42c22ab09 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -28,12 +28,12 @@ module ActiveRecord enabled = connection.query_cache_enabled connection.enable_query_cache! - enabled + [connection, enabled] end - def self.complete(enabled) - ActiveRecord::Base.connection.clear_query_cache - ActiveRecord::Base.connection.disable_query_cache! unless enabled + def self.complete((connection, enabled)) + connection.clear_query_cache + connection.disable_query_cache! unless enabled unless ActiveRecord::Base.connected? && ActiveRecord::Base.connection.transaction_open? ActiveRecord::Base.clear_active_connections! -- cgit v1.2.3 From 09b6cc28bf2bd7c37289d5e9a3e04a04a1ec0db3 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Mon, 24 Oct 2016 13:53:00 -0500 Subject: Clear query cache during checkin, instead of an execution callback It doesn't make sense for the query cache to persist while a connection moves through the pool and is assigned to a new thread. [Samuel Cochran & Matthew Draper] --- activerecord/lib/active_record/query_cache.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'activerecord/lib/active_record/query_cache.rb') diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index c42c22ab09..23dae825c2 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -24,17 +24,10 @@ module ActiveRecord end def self.run - connection = ActiveRecord::Base.connection - enabled = connection.query_cache_enabled - connection.enable_query_cache! - - [connection, enabled] + ActiveRecord::Base.connection.enable_query_cache! end - def self.complete((connection, enabled)) - connection.clear_query_cache - connection.disable_query_cache! unless enabled - + def self.complete(_) unless ActiveRecord::Base.connected? && ActiveRecord::Base.connection.transaction_open? ActiveRecord::Base.clear_active_connections! end -- cgit v1.2.3 From a6d14df40f81ae766081e6a5d4e5fa428eca3174 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Sun, 6 Nov 2016 01:50:15 -0500 Subject: Try harder to avoid making a connection while releasing it connected? doesn't mean what we need here. --- activerecord/lib/active_record/query_cache.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/query_cache.rb') diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index 23dae825c2..667bfe96f6 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -28,8 +28,8 @@ module ActiveRecord end def self.complete(_) - unless ActiveRecord::Base.connected? && ActiveRecord::Base.connection.transaction_open? - ActiveRecord::Base.clear_active_connections! + ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool| + pool.release_connection if pool.active_connection? && !pool.connection.transaction_open? end end -- cgit v1.2.3 From 3c785bdceed4bdf67708197aa687ced6728d77f3 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Sun, 6 Nov 2016 02:19:57 -0500 Subject: Configure query caching (per thread) on the connection pool --- activerecord/lib/active_record/query_cache.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/query_cache.rb') diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index 667bfe96f6..ec246e97bc 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -24,10 +24,17 @@ module ActiveRecord end def self.run - ActiveRecord::Base.connection.enable_query_cache! + caching_pool = ActiveRecord::Base.connection_pool + caching_was_enabled = caching_pool.query_cache_enabled + + caching_pool.enable_query_cache! + + [caching_pool, caching_was_enabled] end - def self.complete(_) + def self.complete((caching_pool, caching_was_enabled)) + caching_pool.disable_query_cache! unless caching_was_enabled + ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool| pool.release_connection if pool.active_connection? && !pool.connection.transaction_open? end -- cgit v1.2.3