Skip to content

Conversation

bogdan
Copy link
Contributor

@bogdan bogdan commented Apr 2, 2018

Summary

Reduce number of generated arrays from n+1 to 1 where n is the number of pools.
It can be important for applications that use a techniques like database sharding and can have a hundred of databases used by the same app.

In my project we use 6: 1 primary and 2 shards + a slave for each of them.

Follow up on #28869 (comment)

r? @matthewd

cc @eugeneius

end
ActiveRecord::Base.connection_handler.connection_pool_list.select do |pool|
unless pool.query_cache_enabled
pool.enable_query_cache!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConnectionPool#enable_query_cache! returns nil when there's no active connection:

connection.enable_query_cache! if active_connection?

The query cache will be enabled but the pool won't be returned, so the complete hook won't disable it... which will cause a regression of #26666 🐛

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably clearer to find the connection pools and enable the query cache on them as separate steps:

ActiveRecord::Base.connection_handler.connection_pool_list
  .reject(&:query_cache_enabled).each(&:enable_query_cache!)

Could you update this test to assert that the query cache is disabled afterwards too?

@bogdan bogdan force-pushed the query-cache-optimization branch from 8c5fce0 to 9008464 Compare April 3, 2018 10:09
pool.disable_query_cache! unless caching_was_enabled
end
def self.complete(pools)
pools.each {|pool| pool.disable_query_cache! }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuboCop wants a space after the opening brace here.

@bogdan bogdan force-pushed the query-cache-optimization branch from 9008464 to fb75c89 Compare April 4, 2018 14:49
@rafaelfranca rafaelfranca merged commit e120343 into rails:master Apr 4, 2018
rafaelfranca added a commit that referenced this pull request Apr 4, 2018
Optimize the code inside AR::QueryCache middleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants