Skip to content

Add helpful error when pool size is too small #2259

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

Merged
merged 3 commits into from
Oct 12, 2017

Conversation

blatyo
Copy link
Contributor

@blatyo blatyo commented Oct 3, 2017

No description provided.

@blatyo
Copy link
Contributor Author

blatyo commented Oct 3, 2017

What would be the best way to test this? The transaction checkout in the migration task would raise the exception that this is providing a more descriptive error for.

@fishcakez
Copy link
Member

Unfortunately this would only work with Ecto.Adapters.SQL.Sandbox and not the other pools. Also we don't want to be pattern matching on the string message of the DBConnection.ConnectionError struct because there is no guarantee that we match on correct messages or that message will stay the same.

@josevalim
Copy link
Member

josevalim commented Oct 3, 2017 via email

@fishcakez
Copy link
Member

Are we not able to make it work with single connection by default?

@josevalim
Copy link
Member

@fishcakez copying from the other comment in case some folks find this question first:

It is because we need one transaction to hold the lock and another transaction to run the migrations. The reason we cannot do everything in one transaction is because some commands like ALTER TYPE cannot be run on a transaction, so the migration needs the ability to control what happens in a transaction and what does not.

@josevalim
Copy link
Member

josevalim commented Oct 3, 2017

@blatyo I think instead of trying to wrap the error message, we should proactively check the pool_size in those two places:

  • pool_size = Keyword.get(opts, :pool_size, 2)
    - and raise if pool_size is 1. In this case, the error message should instruct to set --pool-size to another number

  • {:error, {:already_started, _pid}} ->
    - this is only invoked if the repository was already started so our pool size config has no effect. In this case, we should check for something like Keyword.fetch(repo.config, :pool_size) == {:ok, 1} and raise if so. In this case, the error message should instruct to change the pool_size in the config/config.exs file

What do you think?

@blatyo
Copy link
Contributor Author

blatyo commented Oct 3, 2017

@josevalim I was trying to only show an error when there was actually an issue. The SQLite adapter could run with pool_size 1 and be fine.

Although, since it's being defaulted to 2 and very few people would configure their repo with less than two, I guess it's not too bad to error on 1. I'll put something up later tonight.

@blatyo
Copy link
Contributor Author

blatyo commented Oct 3, 2017

@fishcakez Like José said, but put a different way, it could be made to work with a single connection as long as you never disabled transactions on a migration.

@josevalim
Copy link
Member

josevalim commented Oct 3, 2017

@blatyo so maybe we could check for the pool size on lock_for_migration? If set, the pool size is in default_opts (if not set, it defaults to a number > 1, so we don't need to worry): https://github.com/elixir-ecto/ecto/blob/master/lib/ecto/adapters/sql.ex#L606

@blatyo
Copy link
Contributor Author

blatyo commented Oct 3, 2017

@josevalim Is it possible at that point to just ask the pool how many connections it has rather than looking at the options?

@josevalim
Copy link
Member

@blatyo I don't think we can ask the pool, there is no such API in DBConnection, but they should all respect the pool_size option.

@fishcakez
Copy link
Member

@josevalim we have a pool that is always size 1 (DBConnection.Connection, its actually pool I use the most with Ecto at the moment). We can investigate a pool status call to pick this up.

@josevalim
Copy link
Member

@fishcakez that's a great.

Since the goal of the error is to provide a better error message, we don't need to be 100% accurate though. So I would move on with an opts check for now and replace with a status check later on.

What do you think?

@fishcakez
Copy link
Member

fishcakez commented Oct 3, 2017

What do you think?

This sounds good. I expect very few people use this pool because they would need to dive into DBConnection and also not need more than 1 connection so I am not worried about it for a major version change in Ecto. It might be easier if we warn or crash on the single connection pool if pool_size is set other than 1 in DBConnection.Connection itself, and of course do the warning here too.

@blatyo
Copy link
Contributor Author

blatyo commented Oct 4, 2017

I've pushed a new commit that should address everything discussed. I'm unsure where to add a test, as it seems not much in the adapter sql is directly tested.

@josevalim
Copy link
Member

We can probably test it on integration_test/sql/migration.exs. Note we will need to start a pool exclusively for this test and then trigger the migration command.

@@ -6,11 +6,12 @@ defmodule Ecto.Integration.MigratorTest do
import Support.FileHelpers
import Ecto.Migrator, only: [migrated_versions: 1]

alias Ecto.Integration.PoolRepo
alias Ecto.Integration.{PoolRepo, SingleConnectionRepo}

Choose a reason for hiding this comment

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

Most of the time you are using the multiple single line alias/require/import/use directives but here you are using the multi-alias/require/import/use syntax

@blatyo blatyo force-pushed the warn-pool-size branch 3 times, most recently from f28b522 to 9bdc70b Compare October 8, 2017 13:26
@blatyo
Copy link
Contributor Author

blatyo commented Oct 8, 2017

I've added a test case. AFAIK, the failing test is not related to the changes I've made.

@josevalim josevalim merged commit 8300c29 into elixir-ecto:master Oct 12, 2017
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

@blatyo blatyo deleted the warn-pool-size branch October 12, 2017 18:03
bartekupartek pushed a commit to bartekupartek/ecto that referenced this pull request Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants