New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor QueryCache to be owned by the pool #50938
Conversation
bbf34f3
to
f771d9b
Compare
|
||
private | ||
def query_cache | ||
@thread_query_caches.compute_if_absent(connection_cache_key(ActiveSupport::IsolatedExecutionState.context)) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: we need to prune dead thread/fibers from this map, otherwise code that do queries from short lived threads or fibers would leak memory.
That's likely the reaper's job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(or we should move that in ActiveSupport::IsolatedExecutionState
.
9a8d06d
to
f864c6c
Compare
Ref: rails#50793 If we want to stop caching the checked out connections, then we must persist the cache in the pool, and assign it to the connection when it's checked out. The pool become responsible for managing the cache lifecycle. This also open the door to sharing the cache between multiple connections, which is valuable for read replicas, etc. This change only really make sense if we go through with no longer caching checked out connections. Otherwise it's just extra complexity.
Otherwise they could linger around and leak memory if a user checkout connections from short lived fibers or threads. The undocumented `connection_cache_key` hook point is eliminated because it was essentially add to allow the connection pool to be fiber based rather than thread based, which is now supported out of the box.
f864c6c
to
94fc536
Compare
Followup: rails#50938 The behavior changed to always clear the query cache as soon as it's disabled, on the assumption that once queries have been performed without it, all bets are off. However that isn't quite true, we can apply the same clearing logic than when it's enabled. If you perform read only queries without the cache, there is no reason to clear it.
Followup: rails#50938 The behavior changed to always clear the query cache as soon as it's disabled, on the assumption that once queries have been performed without it, all bets are off. However that isn't quite true, we can apply the same clearing logic than when it's enabled. If you perform read only queries without the cache, there is no reason to clear it.
Followup: rails#50938 The behavior changed to always clear the query cache as soon as it's disabled, on the assumption that once queries have been performed without it, all bets are off. However that isn't quite true, we can apply the same clearing logic than when it's enabled. If you perform read only queries without the cache, there is no reason to clear it.
Followup: rails#50938 The behavior changed to always clear the query cache as soon as it's disabled, on the assumption that once queries have been performed without it, all bets are off. However that isn't quite true, we can apply the same clearing logic than when it's enabled. If you perform read only queries without the cache, there is no reason to clear it.
Followup: rails#50938 The behavior changed to always clear the query cache as soon as it's disabled, on the assumption that once queries have been performed without it, all bets are off. However that isn't quite true, we can apply the same clearing logic than when it's enabled. If you perform read only queries without the cache, there is no reason to clear it.
Followup: rails#50938 The behavior changed to always clear the query cache as soon as it's disabled, on the assumption that once queries have been performed without it, all bets are off. However that isn't quite true, we can apply the same clearing logic than when it's enabled. If you perform read only queries without the cache, there is no reason to clear it.
Followup: rails#50938 The behavior changed to always clear the query cache as soon as it's disabled, on the assumption that once queries have been performed without it, all bets are off. However that isn't quite true, we can apply the same clearing logic than when it's enabled. If you perform read only queries without the cache, there is no reason to clear it.
Ref: #50793
If we want to stop caching the checked out connections, then we must persist the cache in the pool, and assign it to the connection when it's checked out.
The pool become responsible for managing the cache lifecycle.
This also open the door to sharing the cache between multiple connections, which is valuable for read replicas, etc.
This change only really make sense if we go through with no longer caching checked out connections. Otherwise it's just extra complexity.