1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/rails
eileencodes 31461d8a79
Implement granular role and shard swapping
This change allows for a connection to be swapped on role or shard for a
class. Previously calling `connected_to` would swap all the connections
to a particular role or shard. Granular connection swapping is useful
for swapping one connection to reading while leaving all other
connection classes on writing.

The public methods on connection handler have been updated to behave the
same as they did previously on the different handlers. The difference
however is instead of calling
`ActiveRecord::Base.connection_handlers[:reading].clear_all_connections!`
you now call
`ActiveRecord::Base.connection_handler.clear_all_connections!` which
will clear based on current role set by a `connected_to` block. Outside
the context of a `connected_to` block, `clear_all_connections!` can take
an optional parameter to clear specific connections by role.

The major changes in this PR are:

* We introduced a `legacy_connection_handling` configuration option that
is set to true by default. It will be set to `false` for all new
applications.
* In the new connection handling there will be one only connection
handler. Previously there was a connection handler for each role. Now
the role is stored in the `PoolManager`. In order to maintain backwards
compatibility we introduced a `LegacyPoolManager` to avoid duplicate
conditionals. See diagram in PR body for changes to connection
management.
* `connected_to` will now use a stacked concurrent map to keep track of
the connection for each class. For each opened block the `class`,
`role`, and `shard` will be added to the stack, when the block is exited
the `class`, `role`, `shard` array will be removed from the stack.
* With these changes `ActiveRecord::Base.connected_to` will remain
global. If called all connections in the block will use the `role` and
`shard` that was switched to. If called with a parent class like
`AnimalsRecord.connected_to` only models under `AnimalsRecord` will be
switched and everything else will remain the same.

Examples:

Given an application we have a `User` model that inherits from
`ApplicationRecord` and a `Dog` model that inherits from
`AnimalsRecord`. `AnimalsRecord` and `ApplicationRecord` have writing
and reading connections as well as shard `default`, `one`, and `two`.

```ruby
ActiveRecord::Base.connected_to(role: :reading) do
  User.first # reads from default replica
  Dog.first # reads from default replica

  AnimalsRecord.connected_to(role: :writing, shard: :one) do
    User.first # reads from default replica
    Dog.first # reads from shard one primary
  end

  User.first # reads from default replica
  Dog.first # reads from default replica

  ApplicationRecord.connected_to(role: :writing, shard: :two) do
    User.first # reads from shard two primary
    Dog.first # reads from default replica
  end
end
```

Things this PR does not solve:

* Currently there is no API for swapping more than one but not all
connections. Apps with many primaries may want to swap 3 but not all 10
connections. We plan to build an API for that in a followup PR.
* The middleware remains the same and is using the global switching
methods. Therefore at this time to use this new feature applications
must manually switch connections. We will also address this in a
followup PR.
* The `schema_cache` is currently on the `PoolConfig`. We plan on trying
to move this up to the `PoolManager` or elsewhere later on so each
`PoolConfig` doesn't need to hold a reference to the `schema_cache`.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-10-28 15:11:35 -04:00
..
api Document Arel.sql 2019-09-26 19:37:42 -04:00
application Implement granular role and shard swapping 2020-10-28 15:11:35 -04:00
command Address to false negative for Performance/DeletePrefix,DeleteSuffix 2020-06-14 13:04:47 +09:00
commands Prefer bin/rails for the credentials command 2020-09-02 21:03:56 +01:00
console
engine This kind of wrapper should better not be able to call private methods 2020-10-07 11:12:02 +09:00
generators Implement granular role and shard swapping 2020-10-28 15:11:35 -04:00
plugin
rack fix broken handling of unknown http methods 2020-10-23 23:34:58 +01:00
railtie require "active_support/core_ext/symbol/starts_ends_with" for Ruby 2.6 2020-05-25 05:24:44 +09:00
tasks Combine and deprecate rails db:structure:{dump,load} tasks into rails db:schema:{dump,load} 2020-08-03 19:08:41 +03:00
templates Fix href to replace http with https in the Yay! You’re on Rails! page 2019-09-03 09:43:29 +09:00
test_unit ✂️ [ci skip] 2020-09-16 09:52:10 +09:00
all.rb Import Action Text 2019-01-04 22:22:49 -05:00
app_loader.rb Railities typo fixes. 2019-02-01 16:42:40 +05:30
app_updater.rb Remove --skip-yarn in favor of --skip-javascript 2018-10-22 16:54:10 +03:00
application.rb Load Rails tasks for each new Rake::Application 2020-09-06 14:40:14 -05:00
application_controller.rb Enable Layout/EmptyLinesAroundAccessModifier cop 2019-06-13 12:00:45 +09:00
autoloaders.rb defines Rails.autoloaders.log! 2019-08-13 18:14:50 +02:00
backtrace_cleaner.rb Missing require AS/core_ext/string/access 2019-12-29 14:12:42 +09:00
cli.rb
code_statistics.rb attr_reader methods are public methods 2020-10-07 11:12:01 +09:00
code_statistics_calculator.rb Auto-correct for delete_prefix/delete_suffix 2020-06-05 12:40:39 +09:00
command.rb Isolate ARGV in Rails::Command.invoke 2020-02-18 15:02:56 -06:00
commands.rb
configuration.rb Get rid of unnecessary meta programming 2020-10-07 11:12:02 +09:00
dev_caching.rb
engine.rb fix wording in rail/engine class 2020-05-14 08:51:27 +01:00
gem_version.rb Start Rails 6.1 development 2019-04-24 15:57:14 -04:00
generators.rb Address to false negative for Performance/DeletePrefix,DeleteSuffix 2020-06-14 13:04:47 +09:00
info.rb Remove assumption that a primary config exists 2020-06-04 15:08:59 -04:00
info_controller.rb Use URI::DEFAULT_PARSER rather than instantiate a new one 2020-06-29 23:06:34 +02:00
initializable.rb
mailers_controller.rb Add title tag to mailer previews 2019-09-02 14:44:46 -07:00
paths.rb Bring back feature that allows loading external route iles: 2019-12-06 14:20:12 +01:00
rack.rb
railtie.rb require, require_relative, load by double quotes (#38841) 2020-03-29 16:30:52 -07:00
ruby_version_check.rb Require Ruby 2.5 for Rails 6. 2018-12-19 21:47:50 +01:00
secrets.rb
source_annotation_extractor.rb Prefer no allocation start/end_with? over String#[] == 2020-05-29 10:20:13 +09:00
tasks.rb Implements the task zeitwerk:check 2019-04-23 03:17:34 +02:00
test_help.rb Use testing lazy-load hooks: 2018-07-04 16:34:59 -04:00
version.rb
welcome_controller.rb