Skip to content
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

Add dirties option to Model.uncached #51204

Merged
merged 2 commits into from
Mar 3, 2024
Merged

Conversation

djmb
Copy link
Contributor

@djmb djmb commented Feb 27, 2024

Motivation / Background

This is an alternative to #50721.

For Solid Cache, we want to be able to read and write from the cache database without using the query cache.

We also don't want to expire the query cache on other connection pools when we write. Writing to the Rails cache shouldn't invalidate the main query cache.

cc @byroot, @jeremy

Detail

This adds a dirties option to ActiveRecord::Base.uncached and ActiveRecord::ConnectionAdapters::ConnectionPool#uncached.

Setting dirties to false, means database writes to the connection pool will not mark any query caches as dirty.

The option defaults to true which retains the existing behaviour and clears query caches on all connection pools used by the current thread.

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

activerecord/CHANGELOG.md Show resolved Hide resolved
activerecord/lib/active_record/query_cache.rb Outdated Show resolved Hide resolved
}
mw.call({})
end

Copy link
Member

Choose a reason for hiding this comment

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

Great coverage.

@djmb djmb force-pushed the uncached-dirties branch 2 times, most recently from 97126b4 to cee1f28 Compare February 29, 2024 08:50
This adds a `dirties` option to `ActiveRecord::Base.uncached` and
`ActiveRecord::ConnectionAdapters::ConnectionPool#uncached`.

Setting `dirties` to `false`, means database writes to the connection
pool will not mark any query caches as dirty.

The option defaults to `true` which retains the existing behaviour and
clears query caches on all connection pools used by the current thread.
@jeremy jeremy added this to the 7.2.0 milestone Mar 3, 2024
@jeremy jeremy merged commit 5d528ba into rails:main Mar 3, 2024
3 of 4 checks passed
yahonda added a commit to yahonda/rails that referenced this pull request Mar 3, 2024
This commit addresses these errors since rails#51192 renames `.connection` into `.lease_connection`

Follow up rails#51204

```ruby
$ cd activerecord
$ bin/test test/cases/query_cache_test.rb -n /uncached_dirties/
Using sqlite3
Run options: -n /uncached_dirties/ --seed 57207

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:712:in `block (2 levels) in test_query_cache_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:712:in `block in test_query_cache_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:720:in `test_query_cache_uncached_dirties'

bin/test test/cases/query_cache_test.rb:709

E

Error:
QueryCacheTest#test_query_cache_connection_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:726:in `block (2 levels) in test_query_cache_connection_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:726:in `block in test_query_cache_connection_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:734:in `test_query_cache_connection_uncached_dirties'

bin/test test/cases/query_cache_test.rb:723

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties_disabled_with_nested_cache:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:740:in `block (2 levels) in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:194:in `assert_changes'
    test/cases/query_cache_test.rb:740:in `block in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:757:in `test_query_cache_uncached_dirties_disabled_with_nested_cache'

bin/test test/cases/query_cache_test.rb:737

Finished in 0.134429s, 22.3166 runs/s, 0.0000 assertions/s.
3 runs, 0 assertions, 0 failures, 3 errors, 0 skips
$
```
viralpraxis pushed a commit to viralpraxis/rails that referenced this pull request Mar 24, 2024
This adds a `dirties` option to `ActiveRecord::Base.uncached` and
`ActiveRecord::ConnectionAdapters::ConnectionPool#uncached`.

Setting `dirties` to `false`, means database writes to the connection
pool will not mark any query caches as dirty.

The option defaults to `true` which retains the existing behaviour and
clears query caches on all connection pools used by the current thread.

Co-authored-by: Jeremy Daer <jeremy@rubyonrails.org>
viralpraxis pushed a commit to viralpraxis/rails that referenced this pull request Mar 24, 2024
This commit addresses these errors since rails#51192 renames `.connection` into `.lease_connection`

Follow up rails#51204

```ruby
$ cd activerecord
$ bin/test test/cases/query_cache_test.rb -n /uncached_dirties/
Using sqlite3
Run options: -n /uncached_dirties/ --seed 57207

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:712:in `block (2 levels) in test_query_cache_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:712:in `block in test_query_cache_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:720:in `test_query_cache_uncached_dirties'

bin/test test/cases/query_cache_test.rb:709

E

Error:
QueryCacheTest#test_query_cache_connection_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:726:in `block (2 levels) in test_query_cache_connection_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:726:in `block in test_query_cache_connection_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:734:in `test_query_cache_connection_uncached_dirties'

bin/test test/cases/query_cache_test.rb:723

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties_disabled_with_nested_cache:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:740:in `block (2 levels) in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:194:in `assert_changes'
    test/cases/query_cache_test.rb:740:in `block in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:757:in `test_query_cache_uncached_dirties_disabled_with_nested_cache'

bin/test test/cases/query_cache_test.rb:737

Finished in 0.134429s, 22.3166 runs/s, 0.0000 assertions/s.
3 runs, 0 assertions, 0 failures, 3 errors, 0 skips
$
```
fractaledmind pushed a commit to fractaledmind/rails that referenced this pull request May 13, 2024
This adds a `dirties` option to `ActiveRecord::Base.uncached` and
`ActiveRecord::ConnectionAdapters::ConnectionPool#uncached`.

Setting `dirties` to `false`, means database writes to the connection
pool will not mark any query caches as dirty.

The option defaults to `true` which retains the existing behaviour and
clears query caches on all connection pools used by the current thread.

Co-authored-by: Jeremy Daer <jeremy@rubyonrails.org>
fractaledmind pushed a commit to fractaledmind/rails that referenced this pull request May 13, 2024
This commit addresses these errors since rails#51192 renames `.connection` into `.lease_connection`

Follow up rails#51204

```ruby
$ cd activerecord
$ bin/test test/cases/query_cache_test.rb -n /uncached_dirties/
Using sqlite3
Run options: -n /uncached_dirties/ --seed 57207

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:712:in `block (2 levels) in test_query_cache_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:712:in `block in test_query_cache_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:720:in `test_query_cache_uncached_dirties'

bin/test test/cases/query_cache_test.rb:709

E

Error:
QueryCacheTest#test_query_cache_connection_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:726:in `block (2 levels) in test_query_cache_connection_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:726:in `block in test_query_cache_connection_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:734:in `test_query_cache_connection_uncached_dirties'

bin/test test/cases/query_cache_test.rb:723

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties_disabled_with_nested_cache:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:740:in `block (2 levels) in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:194:in `assert_changes'
    test/cases/query_cache_test.rb:740:in `block in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:757:in `test_query_cache_uncached_dirties_disabled_with_nested_cache'

bin/test test/cases/query_cache_test.rb:737

Finished in 0.134429s, 22.3166 runs/s, 0.0000 assertions/s.
3 runs, 0 assertions, 0 failures, 3 errors, 0 skips
$
```
xjunior pushed a commit to xjunior/rails that referenced this pull request Jun 9, 2024
This adds a `dirties` option to `ActiveRecord::Base.uncached` and
`ActiveRecord::ConnectionAdapters::ConnectionPool#uncached`.

Setting `dirties` to `false`, means database writes to the connection
pool will not mark any query caches as dirty.

The option defaults to `true` which retains the existing behaviour and
clears query caches on all connection pools used by the current thread.

Co-authored-by: Jeremy Daer <jeremy@rubyonrails.org>
xjunior pushed a commit to xjunior/rails that referenced this pull request Jun 9, 2024
This commit addresses these errors since rails#51192 renames `.connection` into `.lease_connection`

Follow up rails#51204

```ruby
$ cd activerecord
$ bin/test test/cases/query_cache_test.rb -n /uncached_dirties/
Using sqlite3
Run options: -n /uncached_dirties/ --seed 57207

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:712:in `block (2 levels) in test_query_cache_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:712:in `block in test_query_cache_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:720:in `test_query_cache_uncached_dirties'

bin/test test/cases/query_cache_test.rb:709

E

Error:
QueryCacheTest#test_query_cache_connection_uncached_dirties:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:726:in `block (2 levels) in test_query_cache_connection_uncached_dirties'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:241:in `assert_no_changes'
    test/cases/query_cache_test.rb:726:in `block in test_query_cache_connection_uncached_dirties'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:734:in `test_query_cache_connection_uncached_dirties'

bin/test test/cases/query_cache_test.rb:723

E

Error:
QueryCacheTest#test_query_cache_uncached_dirties_disabled_with_nested_cache:
NoMethodError: undefined method `connection' for class ActiveRecord::Base
    lib/active_record/dynamic_matchers.rb:22:in `method_missing'
    test/cases/query_cache_test.rb:740:in `block (2 levels) in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/testing/assertions.rb:194:in `assert_changes'
    test/cases/query_cache_test.rb:740:in `block in test_query_cache_uncached_dirties_disabled_with_nested_cache'
    test/cases/query_cache_test.rb:771:in `block (2 levels) in middleware'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/execution_wrapper.rb:91:in `wrap'
    test/cases/query_cache_test.rb:771:in `block in middleware'
    test/cases/query_cache_test.rb:757:in `test_query_cache_uncached_dirties_disabled_with_nested_cache'

bin/test test/cases/query_cache_test.rb:737

Finished in 0.134429s, 22.3166 runs/s, 0.0000 assertions/s.
3 runs, 0 assertions, 0 failures, 3 errors, 0 skips
$
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants