未验证 提交 85ef2196 编写于 作者: E eileencodes

Rename `pool_key` to `shard`

This change renames the following:

* `current_pool_key` -> `current_shard`
* `default_pool_key` -> `default_shard`
* `pool_key` -> `shard`

Originally we had intended to name the `shard` as `pool_key` because
when we implemented the internal private API we weren't sure how it was
going to be used for sharding and wanted to implement behavior without
promising a public API. Now that we have a public API for sharding it's
better to use the same name everywhere rather than have one name for
private APIs and one name for public APIs. This should make
contributions and tracking down bugs easier in the future.

This PR doesn't require any deprecations because the sharding API is
unreleased and so is all the internal code that was using `pool_key`.
Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
上级 0133428f
......@@ -1031,7 +1031,7 @@ def connection_pool_list
end
alias :connection_pools :connection_pool_list
def establish_connection(config, pool_key = Base.default_pool_key, owner_name = Base.name)
def establish_connection(config, shard = Base.default_shard, owner_name = Base.name)
owner_name = config.to_s if config.is_a?(Symbol)
pool_config = resolve_pool_config(config, owner_name)
......@@ -1040,7 +1040,7 @@ def establish_connection(config, pool_key = Base.default_pool_key, owner_name =
# Protects the connection named `ActiveRecord::Base` from being removed
# if the user calls `establish_connection :primary`.
if owner_to_pool_manager.key?(pool_config.connection_specification_name)
remove_connection_pool(pool_config.connection_specification_name, pool_key)
remove_connection_pool(pool_config.connection_specification_name, shard)
end
message_bus = ActiveSupport::Notifications.instrumenter
......@@ -1052,7 +1052,7 @@ def establish_connection(config, pool_key = Base.default_pool_key, owner_name =
owner_to_pool_manager[pool_config.connection_specification_name] ||= PoolManager.new
pool_manager = get_pool_manager(pool_config.connection_specification_name)
pool_manager.set_pool_config(pool_key, pool_config)
pool_manager.set_pool_config(shard, pool_config)
message_bus.instrument("!connection.active_record", payload) do
pool_config.pool
......@@ -1094,12 +1094,12 @@ def flush_idle_connections!
# active or defined connection: if it is the latter, it will be
# opened and set as the active connection for the class it was defined
# for (not necessarily the current class).
def retrieve_connection(spec_name, pool_key = ActiveRecord::Base.default_pool_key) # :nodoc:
pool = retrieve_connection_pool(spec_name, pool_key)
def retrieve_connection(spec_name, shard = ActiveRecord::Base.default_shard) # :nodoc:
pool = retrieve_connection_pool(spec_name, shard)
unless pool
if pool_key != ActiveRecord::Base.default_pool_key
message = "No connection pool for '#{spec_name}' found for the '#{pool_key}' shard."
if shard != ActiveRecord::Base.default_shard
message = "No connection pool for '#{spec_name}' found for the '#{shard}' shard."
elsif ActiveRecord::Base.connection_handler != ActiveRecord::Base.default_connection_handler
message = "No connection pool for '#{spec_name}' found for the '#{ActiveRecord::Base.current_role}' role."
else
......@@ -1114,8 +1114,8 @@ def retrieve_connection(spec_name, pool_key = ActiveRecord::Base.default_pool_ke
# Returns true if a connection that's accessible to this class has
# already been opened.
def connected?(spec_name, pool_key = ActiveRecord::Base.default_pool_key)
pool = retrieve_connection_pool(spec_name, pool_key)
def connected?(spec_name, shard = ActiveRecord::Base.default_shard)
pool = retrieve_connection_pool(spec_name, shard)
pool && pool.connected?
end
......@@ -1123,14 +1123,14 @@ def connected?(spec_name, pool_key = ActiveRecord::Base.default_pool_key)
# connection and the defined connection (if they exist). The result
# can be used as an argument for #establish_connection, for easily
# re-establishing the connection.
def remove_connection(owner, pool_key = ActiveRecord::Base.default_pool_key)
remove_connection_pool(owner, pool_key)&.configuration_hash
def remove_connection(owner, shard = ActiveRecord::Base.default_shard)
remove_connection_pool(owner, shard)&.configuration_hash
end
deprecate remove_connection: "Use #remove_connection_pool, which now returns a DatabaseConfig object instead of a Hash"
def remove_connection_pool(owner, pool_key = ActiveRecord::Base.default_pool_key)
def remove_connection_pool(owner, shard = ActiveRecord::Base.default_shard)
if pool_manager = get_pool_manager(owner)
pool_config = pool_manager.remove_pool_config(pool_key)
pool_config = pool_manager.remove_pool_config(shard)
if pool_config
pool_config.disconnect!
......@@ -1142,8 +1142,8 @@ def remove_connection_pool(owner, pool_key = ActiveRecord::Base.default_pool_key
# Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool_manager.
# This makes retrieving the connection pool O(1) once the process is warm.
# When a connection is established or removed, we invalidate the cache.
def retrieve_connection_pool(owner, pool_key = ActiveRecord::Base.default_pool_key)
pool_config = get_pool_manager(owner)&.get_pool_config(pool_key)
def retrieve_connection_pool(owner, shard = ActiveRecord::Base.default_shard)
pool_config = get_pool_manager(owner)&.get_pool_config(shard)
pool_config&.pool
end
......
......@@ -49,7 +49,7 @@ module ConnectionHandling
def establish_connection(config_or_env = nil)
config_or_env ||= DEFAULT_ENV.call.to_sym
db_config, owner_name = resolve_config_for_connection(config_or_env)
connection_handler.establish_connection(db_config, current_pool_key, owner_name)
connection_handler.establish_connection(db_config, current_shard, owner_name)
end
# Connects a model to the databases specified. The +database+ keyword
......@@ -89,15 +89,15 @@ def connects_to(database: {}, shards: {})
db_config, owner_name = resolve_config_for_connection(database_key)
handler = lookup_connection_handler(role.to_sym)
connections << handler.establish_connection(db_config, default_pool_key, owner_name)
connections << handler.establish_connection(db_config, default_shard, owner_name)
end
shards.each do |pool_key, database_keys|
shards.each do |shard, database_keys|
database_keys.each do |role, database_key|
db_config, owner_name = resolve_config_for_connection(database_key)
handler = lookup_connection_handler(role.to_sym)
connections << handler.establish_connection(db_config, pool_key.to_sym, owner_name)
connections << handler.establish_connection(db_config, shard.to_sym, owner_name)
end
end
......@@ -154,7 +154,7 @@ def connected_to(database: nil, role: nil, shard: nil, prevent_writes: false, &b
db_config, owner_name = resolve_config_for_connection(database)
handler = lookup_connection_handler(role)
handler.establish_connection(db_config, default_pool_key, owner_name)
handler.establish_connection(db_config, default_shard, owner_name)
with_handler(role, &blk)
elsif shard
......@@ -172,8 +172,8 @@ def connected_to(database: nil, role: nil, shard: nil, prevent_writes: false, &b
# ActiveRecord::Base.connected_to?(role: :writing) #=> true
# ActiveRecord::Base.connected_to?(role: :reading) #=> false
# end
def connected_to?(role:, shard: ActiveRecord::Base.default_pool_key)
current_role == role.to_sym && current_pool_key == shard.to_sym
def connected_to?(role:, shard: ActiveRecord::Base.default_shard)
current_role == role.to_sym && current_shard == shard.to_sym
end
# Returns the symbol representing the current connected role.
......@@ -247,16 +247,16 @@ def connection_db_config
end
def connection_pool
connection_handler.retrieve_connection_pool(connection_specification_name, current_pool_key) || raise(ConnectionNotEstablished)
connection_handler.retrieve_connection_pool(connection_specification_name, current_shard) || raise(ConnectionNotEstablished)
end
def retrieve_connection
connection_handler.retrieve_connection(connection_specification_name, current_pool_key)
connection_handler.retrieve_connection(connection_specification_name, current_shard)
end
# Returns +true+ if Active Record is connected.
def connected?
connection_handler.connected?(connection_specification_name, current_pool_key)
connection_handler.connected?(connection_specification_name, current_shard)
end
def remove_connection(name = nil)
......@@ -264,11 +264,11 @@ def remove_connection(name = nil)
# if removing a connection that has a pool, we reset the
# connection_specification_name so it will use the parent
# pool.
if connection_handler.retrieve_connection_pool(name, current_pool_key)
if connection_handler.retrieve_connection_pool(name, current_shard)
self.connection_specification_name = nil
end
connection_handler.remove_connection_pool(name, current_pool_key)
connection_handler.remove_connection_pool(name, current_shard)
end
def clear_cache! # :nodoc:
......@@ -302,15 +302,15 @@ def with_role(role, prevent_writes, &blk)
end
end
def with_shard(pool_key, role, prevent_writes)
old_pool_key = current_pool_key
def with_shard(shard, role, prevent_writes)
old_shard = current_shard
with_role(role, prevent_writes) do
self.current_pool_key = pool_key
self.current_shard = shard
yield
end
ensure
self.current_pool_key = old_pool_key
self.current_shard = old_shard
end
def swap_connection_handler(handler, &blk) # :nodoc:
......
......@@ -135,7 +135,7 @@ def self.configurations
class_attribute :default_connection_handler, instance_writer: false
class_attribute :default_pool_key, instance_writer: false
class_attribute :default_shard, instance_writer: false
self.filter_attributes = []
......@@ -147,16 +147,16 @@ def self.connection_handler=(handler)
Thread.current.thread_variable_set(:ar_connection_handler, handler)
end
def self.current_pool_key
Thread.current.thread_variable_get(:ar_pool_key) || default_pool_key
def self.current_shard
Thread.current.thread_variable_get(:ar_shard) || default_shard
end
def self.current_pool_key=(pool_key)
Thread.current.thread_variable_set(:ar_pool_key, pool_key)
def self.current_shard=(shard)
Thread.current.thread_variable_set(:ar_shard, shard)
end
self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
self.default_pool_key = :default
self.default_shard = :default
end
module ClassMethods
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册