Skip to content

Move pool and friends out of DatabaseConfig - #37408

Closed
eileencodes wants to merge 1 commit into
rails:masterfrom
eileencodes:move-pool-out-of-db-config
Closed

eileencodes wants to merge 1 commit into
rails:masterfrom
eileencodes:move-pool-out-of-db-config

Conversation

@eileencodes

Copy link
Copy Markdown
Member

Note: this PR removes WeakRef and no tests failed. If we need it we can add tests to make sure it sticks around in future refactorings.

cc/ @casperisfine @rafaelfranca @matthewd @seejohnrun @tenderlove @jhawthorn

This PR moves pool, disconnect_pools!, discard_pool!,
disconnect_pool!, etc out of the DatabaseConfig and into a new
PoolManager class.

The DatabaseConfig is for storing how databases connect and shouldn't
have any knowledge of their pool.

We also removed the WeakMap since discard (which it calls) already
removes entries from the config_to_pool Hash - and no tests failed.
We should get the same behavior as WeakMap by deleting from the map.

This PR moves `pool`, `disconnect_pools!`, `discard_pool!`,
`disconnect_pool!`, etc out of the `DatabaseConfig` and into a new
`PoolManager` class.

The `DatabaseConfig` is for storing how databases connect and shouldn't
have any knowledge of their pool.

We also removed the WeakMap since discard (which it calls) already
removes entries from the config_to_pool Hash - and no tests failed.
We should get the same behavior as WeakMap by deleting from the map.

Co-authored-by: John Crepezzi <[email protected]>
@eileencodes
eileencodes force-pushed the move-pool-out-of-db-config branch from 08103a6 to 0a1ea4b Compare October 9, 2019 20:52
@casperisfine

Copy link
Copy Markdown
Contributor

We also removed the WeakMap since discard (which it calls) already
removes entries from the config_to_pool Hash - and no tests failed.

Yeah but that's not what it was for. In this PR you have PoolManager.instance.config_to_pool that won't ever be garbage collected.

The WeakMap was important, because if it's a regular hash it make all db_configs and pool, unable to be GCed unless explicitly discarded.

@eileencodes

Copy link
Copy Markdown
Member Author

The WeakMap was important, because if it's a regular hash it make all db_configs and pool, unable to be GCed unless explicitly discarded.

Cool, we can add it back - I'm not sure how to test it. Any ideas?

@casperisfine

Copy link
Copy Markdown
Contributor

I'm not sure how to test it. Any ideas?

I'd say create a new DatabaseConfig, connect to the DB and then make sure it's GCed, e.g.:

def test_gc
  db_config = DatabaseConfig.new(blah)
  pool_ref = WeakRef.new(PoolManager.instance.pool_for(db_config))
  assert_predicate pool_ref, :weakref_alive? 
  4.times { GC.start } # 4 times is mandatory for making sure old gen is GCed as well.
  refute_predicate pool_ref, :weakref_alive? 
end

The problem is that since this hash is now the only reference to the ConnectionPool instances, it can't be a raw WeakMap, otherwise it would be GCed early. So what we'd need here is a WeakMap with only the keys being weakr referenced.

No sure how we could go about that. 🤔

@rafaelfranca

Copy link
Copy Markdown
Member

Is there any reason why we need to remove the poll from DatabaseConfig right now?

I do believe the actual DatabaseConfig should be renamed to something else and a new DatabaseConfig should be created with the only responsibility of holding the config.

I do think we need a object that holds the pool, schema cache and the config. We would call this object Role. The idea behind that is that when we do connect_to :foo instead of going to the ConnectionHandler and asking for the config for :foo, going to the schema cache manager and asking or the cache for :foo and going to the pool manager and asking for the poll for :foo we go to the connection manager and get a object that have all three information in the same place that maps to a database rule.

If we need to remove the poll from DatabaseConfig I'd prefer to go to that direction instead of adding a new singleton since this only add complexity to the codebase. It is already very hard to understand how the singleton ConnectionHandler work.

@eileencodes

Copy link
Copy Markdown
Member Author

Is there any reason why we need to remove the poll from DatabaseConfig right now?

Yes because work that @seejohnrun and I are trying to do to DatabaseConfig is blocked by this due to the DatabaseConfig needing to know about the state of the pool.

I do believe the actual DatabaseConfig should be renamed to something else and a new DatabaseConfig should be created with the only responsibility of holding the config.

I don't think we need to rename DatabaseConfig - it already had only the responsibility of the config and then schema cache and pool were added to it. Instead like you suggested I'd like to make a new class to manage all this instead.

We moved this to a PoolManager class for now because we needed to get it out of DatabaseConfig. However I understand where you're coming from and instead we'll make a new class in this PR to handle db_config, pool, and schema cache. Is that an ok compromise?

@eileencodes

Copy link
Copy Markdown
Member Author

FWIW we were going to move schema cache after this PR, we just were doing it in multiples instead of all at once. We can move everything all at once instead and make a new class to handle this.

@rafaelfranca rafaelfranca left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My main concern if with the fact PoolManager is a singleton. I don't think it needs to be.

When I say rename the class I actually mean doing:

class DatabaseConfig -> class Role

And

class Role
  def initialize(database_config)
  end
end

Role.new(DatabaseConfig.new)

In the end we will have a DatabaseConfig with the previous responsibility, but we will not have a singleton. We need again to have that internal hash config_to_pool similar to the owner_to_config. Having those two identical constructors is a clear small that those two concepts should be the same.

If this unblocks you I'm fine with it, but I don't think we should keep this implementation around.

@rafaelfranca

Copy link
Copy Markdown
Member

Actually, after second thought. I'd prefer to just revert the previous PR than moving to this direction. I think those two singletons should be the same and there is no advantage on splitting them. I prefer we redo the previous PR already implementing the Role class and holding roles instead of configs in the ConnectionHandler

@eileencodes

Copy link
Copy Markdown
Member Author

Previous to #37296 the implementation works like this:

image (1)

We can add a Role class that encapsulates pool, db_config, and schema cache but aren't sure what we're hoping to get from that change given that we could already get from owner to pool and from there reach the db_config and schema_cache in the original implementation.

I really appreciate the work you're doing to add sharding and I don't want to block that work. However we're stuck on our upgrade internally and on the features we need to implement going forward. I want to also make sure we're making things simpler and better and don't add unnecessary complexity.

Maybe we need to revert the #37296 until we have a clearer design of the future state? Can you show us in a graph what you're going for - that might make it easier to get on the same page. I think John and I can also add a lot more tests to better demonstrate our usage requirements.

@rafaelfranca

Copy link
Copy Markdown
Member

Maybe we need to revert the #37296 until we have a clearer design of the future state?

I think that is better in my opinion. It unblocks you and we can work skipping the current intermediate step that we introduced.

I'll let @byroot explain the object model that he had in mind and why it is not possible to implement what we want with the current one.

@eileencodes

Copy link
Copy Markdown
Member Author

Revert opened in #37444

eileencodes added a commit to eileencodes/rails that referenced this pull request Oct 11, 2019
This reverts commit 4ea7769, reversing
changes made to 2ef9eca.

I'm reverting this PR for now at Rafael's recommendation because
some of the changes have caused our Rails upgrade at GitHub to
be blocked (we use some private API's) as well as other work to
use the db_config to establish a connection. See PR and comments
here rails#37408 for more detail.
@byroot

byroot commented Oct 12, 2019

Copy link
Copy Markdown
Member

👋 trying to catch up here. I'll be mostly off line this week end and the coming week, but I'll try to find some time to see if instead of reverting that PR we could just do a quick introduction of the envisioned Role object (with temporary delegation if really necessary).

In short:

  • owner_to_config becomes roles which is a hash of { owner_key => Role}
  • Role has @pool and @db_config.

Would that work for you @eileencodes ? I can't guarantee it will work until I try, but shouldn't be too hard (#famouslastwords)

@byroot

byroot commented Oct 12, 2019

Copy link
Copy Markdown
Member

Oh, just saw your graph, it's probably a good idea to share intents. Here where I'm trying to get at (sorry shitty online graphviz generator):

image

@seejohnrun

Copy link
Copy Markdown
Member

@byroot I might be missing something but can you go into a bit more detail on what we get by replacing pool in the hierarchy with role? A pool already has reference to a db_config and in your diagram it doesn't look like any pool is referenced by multiple role instances. Thank you! ✨

@byroot

byroot commented Oct 15, 2019

Copy link
Copy Markdown
Member

@seejohnrun you are right that at the moment, we could move most of Role responsabilities into ConnectionPool.

However my long term goal is to be able to load the database topology without having to load models or connect to any database.

So in this context using ConnectionPool to represent a role isn't grat because it's spawns a reaper thread etc.

Also in the future I want to move the schema_cache reference up into the object graph, because both the reading and writing role of the animals database should share the same schema_cache, so role will need to have a back reference to it's "group" for that. I think it's much better if ConnectionPool continues to ignore the topology concept.

Let me know if that still isn't clear.

@eileencodes
eileencodes deleted the move-pool-out-of-db-config branch October 15, 2019 14:12
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.

6 participants