1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases
eileencodes a2a525bbb6 Add an intermediary called RoleManager to manage connections
This PR is an alternate solution to #37388. While there are benefits
to merging #37388 it changes the public API and swaps around existing
concepts for how connection management works. The changes are
backwards-incompatible and pretty major. This will have a negative impact
on gems and applications relying on how conn management currently works.

**Background:**

Shopify and other applications need sharding but Rails has
made it impossible to do this because a handler can only hold one
connection pool per class. Sharded apps need to hold multiple
connections per handler per class.

This PR aims to solve only that problem.

**What this PR does:**

In this PR we've added a `RoleManager` class that can hold multiple
`Roles`. Each `Role` holds the `db_config`,
`connection_specification_name`, `schema_cache` and `pool`. By default
the `RoleManager` holds a single reference from a `default` key to the
`Role` instance. A sharded/multi-tenant app can pass an optional second
argument to `remove_connection`, `retrieve_connection_pool`,
`establish_connection` and `connected?` on the handler, thus allowing
for multiple connections belonging to the same class/handler without
breaking backwards compatibility.

By using the `RoleManager` we can avoid altering the public API, moving
around handler/role concepts, and achieve the internal needs for
establishing multiple connections per handler per class.

**A note about why we opened this PR:**

We very much appreciate the work that went into #37388 and in no way mean
to diminish that work. However, it breaks the following public APIs:

* `#retrieve_connection`, `#connected?`, and `#remove_connection` are
public methods on handler and can't be changed from taking a spec to a
role.
* The knowledge that the handler keys are symbols relating to a role
(`:writing`/`:reading`) is public - changing how handlers are accessed
will break apps/libraries.

In addition it doesn't solve the problem of mapping a single connection
to a single class since it has a 1:1 mapping of `class (handler) -> role
(writing) -> db_config`. Multiple pools in a writing role can't exist
in that implementation.

The new PR solves this by using the `RoleManager` to hold multiple connection
objects for the same class. This lets a handler hold a role manager
which can hold as many roles for that writer as the app needs.

**Regarding the `Role` name:**

When I originally designed the API for multiple databases, it wasn't
accidental that handler and role are the same concept. Handler is the
internal concept (since that's what was there already) and Role was the
public external concept. Meaning, role and handler were meant to
be the same thing. The concept here means that when you switch a
handler/role, Rails automatically can pick up the connection on the
other role by knowing the specification name. Changing this would mean not
just that we need to rework how GitHub and many many gems work, but also
means retraining users of Rails 6.0 that all these concepts changed.

Since this PR doesn't move around the concepts in connection
management and instead creates an intermediary between `handler` and
`role` to manage the connection data (`db_config`, `schema_cache`,
`pool`, and `connection_specification`) we think that `Role` and
`RoleManager` are the wrong name.

We didn't change it yet in this PR because we wanted to keep change
churn low for initial review. We also haven't come up with a better
name yet. 😄

**What this PR does not solve:**

Our PR here solves a small portion of the problem - it allows models to
have multiple connections on a class. It doesn't aim to solve any other
problems than that. Going forward we'll need to still solve the
following problems:

* `DatabaseConfig` doesn't support a sharding configuration
* `connects_to`/`connected_to` still needs a way to switch connections for shards
* Automatic switching of shards
* `connection_specification_name` still exists

**The End**

Thanks for reading this far. These problems aren't easy to solve. John
and I spent a lot of time trying different things and so I hope that
this doesn't come across as if we think we know better. I would have
commented on the other PR what changes to make but we needed to try out
different solutions in order to get here.

Ultimately we're aiming to change as little as the API as possible. Even
if the handler/role -> manager -> db_config/pool/etc isn't how we'd
design connection management if we could start over, we also don't want
to break public APIs. It's important that we make things better while
maintaining compatibility.

The `RoleManager` class makes it possible for us to fix the underlying
problem while maintaining all the backwards compatibility in the public
API.

We all have the same goal; to add sharding support to Rails. Let me know
your thoughts on this change in lieu of #37388 and if you have questions.

Co-authored-by: John Crepezzi <seejohnrun@github.com>
2019-11-05 16:27:56 -05:00
..
adapters Add supports_common_table_expressions? for CTE testing 2019-10-23 20:29:54 +09:00
arel Merge pull request #37358 from louim/doc/update-mysql-doc-to-current 2019-10-05 00:43:55 +09:00
associations Fix random CI failure due to non-deterministic sorting order 2019-10-31 05:55:57 +09:00
attribute_methods
coders
connection_adapters Add an intermediary called RoleManager to manage connections 2019-11-05 16:27:56 -05:00
connection_specification Merge ConnectionSpecification + Role -> Role 2019-10-18 10:26:08 -05:00
database_configurations Fix defaults for database configs 2019-09-25 11:15:34 -04:00
fixture_set
migration Use FileUpdateChecker for Migration::CheckPending 2019-10-08 16:29:12 -07:00
relation Improve deprecation message for nested where.not condition 2019-10-31 05:31:02 +09:00
scoping
tasks Make DatabaseTasks adapters use DatabaseConfig objects 2019-09-25 13:52:14 -04:00
type
validations Move the ActiveModel:Errors#full_message method to the Error class: 2019-07-16 14:28:38 +02:00
adapter_test.rb Add supports_common_table_expressions? for CTE testing 2019-10-23 20:29:54 +09:00
aggregations_test.rb
annotate_test.rb
ar_schema_test.rb
associations_test.rb
attribute_decorators_test.rb
attribute_methods_test.rb
attributes_test.rb
autosave_association_test.rb Ensure Contextual validations fire on associations 2019-10-04 12:17:53 +01:00
base_test.rb Better double checked locking in load_schema 2019-09-24 18:00:05 +02:00
batches_test.rb Fix random CI fail due to non-deterministic order 2019-10-17 14:43:06 -05:00
binary_test.rb
bind_parameter_test.rb
boolean_test.rb
cache_key_test.rb
calculations_test.rb Fix random CI failure due to non-deterministic sorting order 2019-10-11 22:01:20 +09:00
callbacks_test.rb
clone_test.rb
collection_cache_key_test.rb
column_alias_test.rb
column_definition_test.rb
comment_test.rb
connection_management_test.rb
connection_pool_test.rb Merge ConnectionSpecification + Role -> Role 2019-10-18 10:26:08 -05:00
core_test.rb Update for Time#inspect 2019-09-21 16:57:39 +09:00
counter_cache_test.rb
custom_locking_test.rb
database_configurations_test.rb Deprecate to_h and to_legacy_hash 2019-09-17 20:49:38 -04:00
database_selector_test.rb Extend test_can_write_while_reading_from_replicas_if_explicit 2019-08-30 17:46:21 +03:00
database_statements_test.rb
date_test.rb
date_time_precision_test.rb
date_time_test.rb
defaults_test.rb Update MySQL links to the current version of the manual [ci skip] 2019-10-03 20:22:29 -04:00
dirty_test.rb Serialize symbols to strings in ImmutableString serialize method 2019-08-10 23:37:37 +01:00
disconnected_test.rb Merge ConnectionSpecification + Role -> Role 2019-10-18 10:26:08 -05:00
dup_test.rb
enum_test.rb Add silence_warnings for defining 'not_' prefix enum elements 2019-07-30 04:54:55 +09:00
errors_test.rb
explain_subscriber_test.rb
explain_test.rb
filter_attributes_test.rb
finder_respond_to_test.rb
finder_test.rb Correctly find nested association reflections for #37356 2019-10-11 16:20:11 +09:00
fixtures_test.rb Add an intermediary called RoleManager to manage connections 2019-11-05 16:27:56 -05:00
forbidden_attributes_protection_test.rb
habtm_destroy_order_test.rb
helper.rb Instantiate ConnectionPool with a DatabaseConfig rather than a ConnectionSpecification 2019-09-24 15:12:22 +02:00
hot_compatibility_test.rb
i18n_test.rb
inheritance_test.rb
insert_all_test.rb Auto-correct rubocop offences 2019-09-16 05:46:26 +09:00
instrumentation_test.rb Include connection in cached query notifications 2019-10-01 00:35:32 +01:00
integration_test.rb
invalid_connection_test.rb
invertible_migration_test.rb
json_attribute_test.rb
json_serialization_test.rb
json_shared_test_cases.rb
locking_test.rb
log_subscriber_test.rb
migration_test.rb Accept columns passed with options in remove_index 2019-09-10 22:21:20 +01:00
migrator_test.rb
mixin_test.rb
modules_test.rb
multi_db_migrator_test.rb
multiparameter_attributes_test.rb
multiple_db_test.rb
nested_attributes_test.rb
nested_attributes_with_callbacks_test.rb
null_relation_test.rb
numeric_data_test.rb
persistence_test.rb
pooled_connections_test.rb
prepared_statement_status_test.rb Make prepared statement status thread and instance-specific 2019-08-16 02:11:19 -04:00
primary_keys_test.rb
query_cache_test.rb Add an intermediary called RoleManager to manage connections 2019-11-05 16:27:56 -05:00
quoting_test.rb
readonly_test.rb
reaper_test.rb Merge ConnectionSpecification + Role -> Role 2019-10-18 10:26:08 -05:00
reflection_test.rb Silence method redefined warnings 2019-09-17 15:53:36 +09:00
relation_test.rb Use assert_queries since we are only checking for the count 2019-09-30 22:38:00 -03:00
relations_test.rb Fix random CI failure due to non-deterministic sorting order 2019-10-17 11:21:46 +09:00
reload_models_test.rb
reserved_word_test.rb
result_test.rb
sanitize_test.rb
schema_dumper_test.rb
schema_loading_test.rb
secure_token_test.rb
serialization_test.rb
serialized_attribute_test.rb define_attribute takes keyword arguments 2019-09-15 03:05:52 +09:00
statement_cache_test.rb
statement_invalid_test.rb
store_test.rb
suppressor_test.rb
test_case.rb Make has_many inversing opt-in 2019-10-11 15:55:46 -04:00
test_fixtures_test.rb
time_precision_test.rb
timestamp_test.rb
touch_later_test.rb
transaction_callbacks_test.rb Rollback in after_commit should not rollback state that already been succeeded 2019-09-20 12:44:35 +09:00
transaction_isolation_test.rb
transactions_test.rb
type_test.rb
types_test.rb
unconnected_test.rb Revert "Revert "Merge pull request #37296 from Shopify/db-config-pool"" 2019-10-14 13:28:42 -04:00
unsafe_raw_sql_test.rb
validations_repair_helper.rb
validations_test.rb
view_test.rb
yaml_serialization_test.rb